diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseControllerTest.java similarity index 64% rename from application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java rename to application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseControllerTest.java index fa318736e31d4ad1b26ec32d233e241be620028b..dce5d42843d4279b7813b5a9aa65038c41d245ff 100644 --- a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseControllerTest.java @@ -1,46 +1,40 @@ package org.fuseri.moduleexercise.exercise; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -//import org.fuseri.model.dto.common.Result; import jakarta.persistence.EntityNotFoundException; import org.fuseri.model.dto.exercise.ExerciseCreateDto; import org.fuseri.model.dto.exercise.ExerciseDto; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.PageImpl; import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; import org.springframework.test.web.servlet.MockMvc; import java.util.ArrayList; -import java.util.List; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc -public class ExerciseTest { +public class ExerciseControllerTest { + @Autowired ObjectMapper objectMapper; + @Autowired private MockMvc mockMvc; - @MockBean ExerciseFacade facade; @@ -52,7 +46,6 @@ public class ExerciseTest { } } -// private ExerciseCreateDto exercise; private ExerciseDto exerciseDto; private ExerciseDto exerciseDto1; private ExerciseDto exerciseDto2; @@ -84,92 +77,41 @@ public class ExerciseTest { exerciseCreateDto = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0); exerciseCreateDto1 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0); exerciseCreateDto2 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L); -// try { -// mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON)); -// mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto1)).contentType(MediaType.APPLICATION_JSON)); -// mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto2)).contentType(MediaType.APPLICATION_JSON)); -// } catch (Exception e) { -// assert (false); -// } - } - - @AfterEach - void cleanup() { - -// try { -// mockMvc.perform(delete("/questions/reset")); -// mockMvc.perform(delete("/exercises/reset")); -// mockMvc.perform(delete("/answers/reset")); -// } catch (Exception ex) { -// throw new RuntimeException(ex); -// } } @Test - void getExercise() { + void getExercise() throws Exception { long id = 1L; when(facade.find(id)).thenReturn(exerciseDto); - try { - var theId = String.format("/exercises/%s", id); - var smth = mockMvc.perform(get(theId)); - smth.andExpect(status().isOk()) - .andExpect(jsonPath("$.name", is(exerciseCreateDto.getName()))) - .andExpect(jsonPath("$.description", is(exerciseCreateDto.getDescription()))) - .andExpect(jsonPath("$.courseId", is((int) exerciseCreateDto.getCourseId()))) - .andExpect(jsonPath("$.difficulty", is(exerciseCreateDto.getDifficulty()))); - } catch (Exception e) { - //do absolutely nothing - } - } - - @Test - void deleteExercise() { - long id = 1L; -// when(facade.delete(id)); - try { - var theId = String.format("/exercises/%s", id); - var smth = mockMvc.perform(delete(theId)); - smth.andExpect(status().isNoContent()); - } catch (Exception e) { - //do absolutely nothing - } + mockMvc.perform(get("/exercises/{id}", id)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name", is(exerciseCreateDto.getName()))) + .andExpect(jsonPath("$.description", is(exerciseCreateDto.getDescription()))) + .andExpect(jsonPath("$.courseId", is((int) exerciseCreateDto.getCourseId()))) + .andExpect(jsonPath("$.difficulty", is(exerciseCreateDto.getDifficulty()))); } @Test - void deleteExercise_notFound() { - long id = 999999L; - try { - var theId = String.format("/exercises/%s", id); - var smth = mockMvc.perform(delete(theId)); - smth.andExpect(status().isNoContent()); - } catch (Exception e) { - //do absolutely nothing - } + void testDelete() throws Exception { + mockMvc.perform(delete("/exercises/1")) + .andExpect(status().is2xxSuccessful()); } - @Test - void getExercise_notFound() { - long id = 999999L; + void getExercise_notFound() throws Exception { + long id = 1L; when(facade.find(id)).thenThrow(new EntityNotFoundException("")); - try { - var theId = String.format("/exercises/%s", id); - var smth = mockMvc.perform(get(theId)); - smth.andExpect(status().isNotFound()); - } catch (Exception e) { - //do absolutely nothing - } + mockMvc.perform(get("/exercises/{id}", id)) + .andExpect(status().isNotFound()); } - @Test void FindAll() { - when(facade.findAll(0)).thenReturn(new PageImpl<>(new ArrayList<>())); try { var response = mockMvc.perform(get("/exercises").param("page", "0")); - var dis = response.andExpect(status().isOk()) + var dis = response.andExpect(status().isOk()) .andExpect(status().is2xxSuccessful()).andReturn().getResponse().getContentAsString(); assertTrue(dis.contains("\"content\":[]")); } catch (Exception e) { @@ -181,7 +123,7 @@ public class ExerciseTest { @Test void getFiltered() { - when(facade.findByCourseIdAndDifficulty(0,2,0)).thenReturn(new PageImpl<>(new ArrayList<>())); + when(facade.findByCourseIdAndDifficulty(0, 2, 0)).thenReturn(new PageImpl<>(new ArrayList<>())); try { var filtered = mockMvc.perform(get("/exercises/filter").param("page", "0").param("courseId", "0").param("difficulty", "2")); var content = filtered.andReturn().getResponse().getContentAsString(); @@ -194,7 +136,7 @@ public class ExerciseTest { @Test void testCreateExercise() throws Exception { - when(facade.create(exerciseCreateDto)).thenReturn(exerciseDto); + when(facade.create(ArgumentMatchers.isA(ExerciseCreateDto.class))).thenReturn(exerciseDto); mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isCreated()) .andExpect(jsonPath("$.name").value("idioms")) @@ -269,36 +211,22 @@ public class ExerciseTest { @Test - void testUpdate() { + void testUpdate() throws Exception { long id = 1L; - when(facade.update(id,exerciseCreateDto)).thenReturn(exerciseDto); - - try { - var theId = String.format("/exercises/%d", id); - var dis = mockMvc.perform(put(theId).content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON)); - + when(facade.update(ArgumentMatchers.eq(id), ArgumentMatchers.isA(ExerciseCreateDto.class))).thenReturn(exerciseDto); - dis.andExpect(status().isOk()).andExpect(jsonPath("$.name",is(exerciseDto.getName()))) - .andExpect(jsonPath("$.difficulty",is(exerciseDto.getDifficulty()))) - .andExpect(jsonPath("$.description",is(exerciseDto.getDescription()))) - .andExpect(jsonPath("$.courseId",is(exerciseDto.getCourseId()))); -// -// var str = dis.andReturn().getResponse().getContentAsString(); -// -// var res = objectMapper.readValue(str, ExerciseDto.class); -// -// assert res.equals(exerciseDto); - - } catch (Exception e) { - throw new RuntimeException(e); - } + var theId = String.format("/exercises/%d", id); + var dis = mockMvc.perform(put(theId).content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON)); + dis.andExpect(status().isOk()).andExpect(jsonPath("$.name", is(exerciseDto.getName()))) + .andExpect(jsonPath("$.difficulty", is(exerciseDto.getDifficulty()))) + .andExpect(jsonPath("$.description", is(exerciseDto.getDescription()))); } @Test void testUpdateNotFound() { long id = 9999L; - when(facade.update(id,exerciseCreateDto)).thenThrow(new EntityNotFoundException()); + when(facade.update(ArgumentMatchers.eq(id), ArgumentMatchers.isA(ExerciseCreateDto.class))).thenThrow(new EntityNotFoundException()); try { var theId = String.format("/exercises/%d", id);