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/ExerciseTest.java index 3fc47c6832ee67ca78f4fe8d307ba356eb66300b..fa318736e31d4ad1b26ec32d233e241be620028b 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/ExerciseTest.java @@ -84,25 +84,25 @@ 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); - } +// 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); - } +// try { +// mockMvc.perform(delete("/questions/reset")); +// mockMvc.perform(delete("/exercises/reset")); +// mockMvc.perform(delete("/answers/reset")); +// } catch (Exception ex) { +// throw new RuntimeException(ex); +// } } @Test diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java index 2a97bb2184abca3ad12574fb584869115f359aaa..8383971d23f186ac9740263ebb47ed4ab303be1f 100644 --- a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java @@ -1,25 +1,36 @@ package org.fuseri.moduleexercise.question; import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.persistence.EntityNotFoundException; +import org.fuseri.model.dto.exercise.AnswerDto; import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto; import org.fuseri.model.dto.exercise.ExerciseCreateDto; import org.fuseri.model.dto.exercise.QuestionCreateDto; import org.fuseri.model.dto.exercise.QuestionDto; +import org.fuseri.model.dto.exercise.QuestionUpdateDto; +import org.fuseri.moduleexercise.exercise.Exercise; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatcher; +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.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import java.util.ArrayList; import java.util.List; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; @@ -36,6 +47,11 @@ public class QuestionTest { @Autowired private MockMvc mockMvc; + @MockBean + QuestionFacade facade; + + private QuestionDto qston; + public static String asJsonString(final Object obj) { try { return new ObjectMapper().writeValueAsString(obj); @@ -58,41 +74,28 @@ public class QuestionTest { @BeforeEach void init() { - createExercise(); +// createExercise(); + + qston = new QuestionDto("\"what is the meaning of: costs an arm and leg\"",1,new ArrayList<>()); long id = 2; var question = new QuestionCreateDto("this statement is false", id, List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true))); var question2 = new QuestionCreateDto("What month of the year has 28 days?", id, List.of(new AnswerInQuestionCreateDto("February", false), new AnswerInQuestionCreateDto("All of them", true))); ResultActions posted = null; - try { - mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON)); - mockMvc.perform(post("/questions").content(asJsonString(question2)).contentType(MediaType.APPLICATION_JSON)); - } catch (Exception e) { - throw new RuntimeException(e); - } } - @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 testCreateQuestion() throws Exception { var exerciseId = 1L; var question = new QuestionCreateDto("what is the meaning of: costs an arm and leg", exerciseId, List.of(new AnswerInQuestionCreateDto("dis very expencive", true), new AnswerInQuestionCreateDto("FMA refference", false))); + when(facade.create(ArgumentMatchers.isA(QuestionCreateDto.class))).thenReturn(qston); var posted = mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON)); posted.andExpect(status().isCreated()) - .andExpect(jsonPath("$.text", is("what is the meaning of: costs an arm and leg"))) - .andExpect(jsonPath("$.exerciseId", is((int)exerciseId))) - .andExpect(jsonPath("$.answers[0].text", is("dis very expencive"))); + .andExpect(jsonPath("$.text", is(qston.getText()))) + .andExpect(jsonPath("$.exerciseId", is((int)qston.getExerciseId()))); + } @Test @@ -145,12 +148,15 @@ public class QuestionTest { @Test void getQuestion() throws Exception { + var question = new QuestionDto("this statement is false",1L,new ArrayList<>()); + when(facade.find(1)).thenReturn(question); var gets = mockMvc.perform(get("/questions/1")); gets.andExpect(status().isOk()).andExpect(jsonPath("$.text", is("this statement is false"))); } @Test void getQuestionNotFound() throws Exception { + when(facade.find(9999)).thenThrow(new EntityNotFoundException()); var gets = mockMvc.perform(get("/questions/9999")); gets.andExpect(status().isNotFound()); } @@ -158,6 +164,10 @@ public class QuestionTest { @Test void getAnswer() throws Exception { + + + var sss = List.of(new AnswerDto("February", false), new AnswerDto("All of them", true)); + when(facade.getQuestionAnswers(2)).thenReturn(sss); var gets = mockMvc.perform(get("/questions/2/answers")); gets.andExpect(status().isOk()) .andExpect(jsonPath("$[0].text", is("February"))) @@ -175,10 +185,12 @@ public class QuestionTest { } """; + var question = new QuestionDto("wat a paradox?",1,new ArrayList<>()); + + when(facade.patchUpdate(ArgumentMatchers.eq(1), ArgumentMatchers.isA(QuestionUpdateDto.class))).thenReturn(question); var gets = mockMvc.perform(put("/questions/1").content(updated).contentType(MediaType.APPLICATION_JSON)); - gets.andExpect(status().isOk()) - .andExpect(jsonPath("$.text", is("wat a paradox?"))); + gets.andExpect(status().isOk()); } @Test