Skip to content

Commit

Permalink
24/12: test SongController
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan committed Dec 24, 2023
1 parent aacdea9 commit d9f0a87
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 23 deletions.
23 changes: 0 additions & 23 deletions src/main/java/com/spotify/app/controller/SongController.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,4 @@ public ResponseEntity<?> increaseViewCountSong(
songService.increaseView(songId);
return ResponseEntity.ok().body(String.format("increased view of song %d", songId));
}


//////////////////////////////////// S3 SERVICE ////////////////////////////////////////
// @GetMapping(value = "/audio/{songId}",produces = "audio/mpeg")
// public ResponseEntity<?> streamAudio(
// @PathVariable("songId") Long songId
// ) {
//
// return ResponseEntity.ok()
// .body(songService.getSongAudio(songId));
// }
//
// @GetMapping(
// value = "/view/image/{songId}",
// produces = {MediaType.IMAGE_PNG_VALUE,MediaType.IMAGE_JPEG_VALUE}
// )
// public ResponseEntity<?> viewImage(@PathVariable("songId") Long songId) {
//
// return ResponseEntity.ok()
// .body(songService.getSongImage(songId));
// }


}
127 changes: 127 additions & 0 deletions src/test/java/com/spotify/app/song/SongControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,131 @@
package com.spotify.app.song;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spotify.app.controller.SongController;
import com.spotify.app.dto.request.SongRequest;
import com.spotify.app.dto.response.SongResponse;
import com.spotify.app.service.SongService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.MethodArgumentNotValidException;

import java.util.HashMap;
import java.util.Map;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.Mockito.when;

@WebMvcTest(
controllers = SongController.class,
excludeAutoConfiguration = {
UserDetailsServiceAutoConfiguration.class, SecurityAutoConfiguration.class
})

public class SongControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private SongService songService;

@Autowired
private ObjectMapper objectMapper;

private String urlPrefix = "/api/v1/song";

private SongRequest songRequest;
@BeforeEach
public void setUp () {
songRequest = new SongRequest(
"song_name",
"CLASSICAL",
2,
"lyric",
12,
12,
2023,
"happy",
1L
);
}
@Test
public void canSaveSong () throws Exception {
// given
Long savedSongId = 1L;
SongResponse expectResponse = new SongResponse(1L);
String url = urlPrefix.concat("/save");
String requestJson = objectMapper.writeValueAsString(songRequest);

// when
when(songService.saveSong(songRequest)).thenReturn(savedSongId);
when(songService.getById(savedSongId)).thenReturn(expectResponse);

// then
this.mockMvc
.perform(post(url).contentType(MediaType.APPLICATION_JSON).content(requestJson))
.andDo(print())
.andExpect(status().isOk())
// .andExpect(jsonPath("$", Matchers.is(expectResponse)))
.andExpect(jsonPath("$.id", Matchers.is(1)));
}

// @Test
// public void shouldThrowExceptionWhenSaveSong () throws Exception {
// // given
// Long savedSongId = 1L;
// SongResponse expectResponse = new SongResponse(1L);
// String url = urlPrefix.concat("/save");
// String requestJson = objectMapper.writeValueAsString(songRequest);
// Map< String, String > errors = new HashMap<>();
// errors.put("nane","name of song must not be greater than 50 char");
// // when
// when(songService.saveSong(songRequest)).thenThrow(new MethodArgumentNotValidException(errors));
// when(songService.getById(savedSongId)).thenReturn(expectResponse);
//
// // then
// this.mockMvc
// .perform(post(url).contentType(MediaType.APPLICATION_JSON).content(requestJson))
// .andDo(print())
// .andExpect(status().isBadRequest());
// }

@Test
public void canUpdateSong () throws Exception {
Long songId = 1L;
SongResponse expectResponse = new SongResponse(1L);
String url = urlPrefix.concat("/update/" + songId);
String requestJson = objectMapper.writeValueAsString(songRequest);

// when
when(songService.getById(songId)).thenReturn(expectResponse);

// then
this.mockMvc
.perform(put(url).contentType(MediaType.APPLICATION_JSON).content(requestJson))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", Matchers.is(1)));
}

@Test
public void canGetById () throws Exception {
Long songId = 1L;
SongResponse expectResponse = new SongResponse(1L);
when(songService.getById(songId)).thenReturn(expectResponse);
String url = urlPrefix.concat("/" + songId);
this.mockMvc.perform(get(url)).andExpect(status().isOk());
}
}

0 comments on commit d9f0a87

Please sign in to comment.