diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerControllerTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7516ea804112f3074c88c9252806f9e61e22bc13 --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerControllerTest.java @@ -0,0 +1,115 @@ +package org.fuseri.moduleexercise.answer; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.persistence.EntityNotFoundException; +import org.fuseri.model.dto.exercise.AnswerCreateDto; +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.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.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.List; + +import static org.hamcrest.Matchers.is; +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.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +public class AnswerControllerTest { + + @Autowired + ObjectMapper objectMapper; + + @Autowired + private MockMvc mockMvc; + + @MockBean + private AnswerFacade answerFacade; + long exerciseId = 1L; + ExerciseCreateDto exercise; + QuestionCreateDto question1; + QuestionCreateDto question2; + + public static String asJsonString(final Object obj) { + try { + return new ObjectMapper().writeValueAsString(obj); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @BeforeEach + void init() { + exercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0); + question1 = new QuestionCreateDto("this statement is false", exerciseId, + List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true))); + question2 = new QuestionCreateDto("What month of the year has 28 days?", exerciseId, + List.of(new AnswerInQuestionCreateDto("February", false), + new AnswerInQuestionCreateDto("All of them", true))); + } + + @Test + void testUpdate() throws Exception { + long id = 1L; + var updated = new AnswerDto("dis true", false); + when(answerFacade.update(ArgumentMatchers.eq(id), ArgumentMatchers.isA(AnswerCreateDto.class))).thenReturn(updated); + + mockMvc.perform(put("/answers/{id}", id) + .content(asJsonString(updated)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.text", is("dis true"))) + .andExpect(jsonPath("$.correct", is(false))); + } + + @Test + void testUpdateNotFoundAnswer() throws Exception { + long id = 1L; + var toUpdate = new AnswerCreateDto("dis true", false, 1); + when(answerFacade.update(ArgumentMatchers.eq(id), ArgumentMatchers.isA(AnswerCreateDto.class))).thenThrow(new EntityNotFoundException()); + mockMvc.perform(put("/answers/{id}", id) + .content(asJsonString(toUpdate)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } + + @Test + void testUpdateEmptyText() throws Exception { + var updated = new AnswerCreateDto("", false, 1); + mockMvc.perform(put("/answers/1") + .content(asJsonString(updated)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + } + + @Test + void testUpdateMissingField() throws Exception { + var updated = """ + { + "correct": false, + "questionId": 1 + } + """; + + mockMvc.perform(put("/answers/1") + .content(updated).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + } + + @Test + void testDelete() throws Exception { + mockMvc.perform(delete("/answers/1")) + .andExpect(status().is2xxSuccessful()); + } +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerTest.java deleted file mode 100644 index 86634c70695fa183633deec9e06549fd63d3c5ea..0000000000000000000000000000000000000000 --- a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerTest.java +++ /dev/null @@ -1,228 +0,0 @@ -package org.fuseri.moduleexercise.answer; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.fuseri.model.dto.exercise.AnswerCreateDto; -import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto; -import org.fuseri.model.dto.exercise.AnswersCreateDto; -import org.fuseri.model.dto.exercise.ExerciseCreateDto; -import org.fuseri.model.dto.exercise.QuestionCreateDto; -import static org.hamcrest.Matchers.is; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -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.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -import java.util.List; -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.put; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@SpringBootTest -@AutoConfigureMockMvc -public class AnswerTest { - - @Autowired - ObjectMapper objectMapper; - @Autowired - private MockMvc mockMvc; - - - public static String asJsonString(final Object obj) { - try { - return new ObjectMapper().writeValueAsString(obj); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @BeforeEach - void init() { - createExercise(); - int exerciseId = 1; - - var question = new QuestionCreateDto("this statement is false", exerciseId, - List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true))); - var question2 = new QuestionCreateDto("What month of the year has 28 days?", exerciseId, List.of(new AnswerInQuestionCreateDto("February", false), new AnswerInQuestionCreateDto("All of them", true))); - - 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 - public void cleanup() { - try { - mockMvc.perform(delete("/answers/reset")); - mockMvc.perform(delete("/questions/reset")); - mockMvc.perform(delete("/exercises/reset")); - } catch (Exception ex) { - throw new RuntimeException(ex); - - } - } - - - - private void createExercise() { - var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0); - - try { - mockMvc.perform(post("/exercises") - .content(asJsonString(postExercise)) - .contentType(MediaType.APPLICATION_JSON)); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - - @Test - void testCreateAnswer() throws Exception { - - var incorrect1 = new AnswerInQuestionCreateDto("True", false); - var incorrect2 = new AnswerInQuestionCreateDto("False", false); - - var createAnswer = new AnswersCreateDto(1, List.of(incorrect1, incorrect2)); - - var posted = mockMvc.perform(post("/answers") - .content(asJsonString(createAnswer)) - .contentType(MediaType.APPLICATION_JSON)); - - posted.andExpect(status().isCreated()) - .andExpect(jsonPath("$[0].text",is("True"))) - .andExpect(jsonPath("$[0].correct", is(false))) - .andExpect(jsonPath("$[1].text",is("False"))) - .andExpect(jsonPath("$[1].correct",is(false))); - } - - - @Test - void testCreateAnswerEmptyText() throws Exception { - - var incorrect1 = new AnswerInQuestionCreateDto("", false); - - var createAnswer = new AnswersCreateDto(1, List.of(incorrect1)); - - var posted = mockMvc.perform(post("/answers") - .content(asJsonString(createAnswer)) - .contentType(MediaType.APPLICATION_JSON)); - - posted.andExpect(status().is4xxClientError()); - } - - @Test - void testCreateAnswerMissingText() throws Exception { - - var prompt = """ - { - "questionId": 1, - "answers": [ - { - "text": "something", - "correct": false - } - ] - } - """; - - var posted = mockMvc.perform(post("/answers") - .content(prompt) - .contentType(MediaType.APPLICATION_JSON)); - - posted.andExpect(status().isCreated()); - } - - @Test - void testCreateAnswerMissingCorrect() throws Exception { - - var prompt = """ - { - "questionId": 1, - "answers": [ - { - "text": "something" - } - ] - } - """; - - var posted = mockMvc.perform(post("/answers") - .content(prompt) - .contentType(MediaType.APPLICATION_JSON)); - - posted.andExpect(status().isCreated()); - } - - @Test - void testUpdate() throws Exception { - var updated = new AnswerCreateDto("dis true",false,1); - mockMvc.perform(put("/answers/1") - .content(asJsonString(updated)).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.text",is("dis true"))) - .andExpect(jsonPath("$.correct",is(false))); - } - - @Test - void testUpdateNotFoundAnswer() throws Exception { - var updated = new AnswerCreateDto("dis true",false,1); - mockMvc.perform(put("/answers/9999") - .content(asJsonString(updated)).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); - } - - @Test - void testUpdateEmptyText() throws Exception { - var updated = new AnswerCreateDto("",false,1); - mockMvc.perform(put("/answers/1") - .content(asJsonString(updated)).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().is4xxClientError()); - } - - @Test - void testUpdateMissingField() throws Exception { - var updated = """ - { - "correct": false, - "questionId": 1 - } - """; - - mockMvc.perform(put("/answers/1") - .content(updated).contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().is4xxClientError()); - } - - @Test - void testDeleteExisting() { - try { - mockMvc.perform(delete("/answers/1")) - .andExpect(status().isNoContent()); - } catch (Exception e) { - assert(false); - } - } - - @Test - void testDeleteNotFound() { - try { - mockMvc.perform(delete("/answers/9999")) - .andExpect(status().isNotFound()); - } catch (Exception e) { - assert(false); - } - } -}