Skip to content
Snippets Groups Projects
Commit a10b9c15 authored by Martin Gargalovič's avatar Martin Gargalovič
Browse files

modified ExerciseTest

parent 01e399a5
No related branches found
No related tags found
2 merge requests!31M2,!30M2 exercise
...@@ -84,25 +84,25 @@ public class ExerciseTest { ...@@ -84,25 +84,25 @@ public class ExerciseTest {
exerciseCreateDto = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0); exerciseCreateDto = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0);
exerciseCreateDto1 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0); exerciseCreateDto1 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0);
exerciseCreateDto2 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L); exerciseCreateDto2 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L);
try { // try {
mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON)); // 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(exerciseCreateDto1)).contentType(MediaType.APPLICATION_JSON));
mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto2)).contentType(MediaType.APPLICATION_JSON)); // mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto2)).contentType(MediaType.APPLICATION_JSON));
} catch (Exception e) { // } catch (Exception e) {
assert (false); // assert (false);
} // }
} }
@AfterEach @AfterEach
void cleanup() { void cleanup() {
try { // try {
mockMvc.perform(delete("/questions/reset")); // mockMvc.perform(delete("/questions/reset"));
mockMvc.perform(delete("/exercises/reset")); // mockMvc.perform(delete("/exercises/reset"));
mockMvc.perform(delete("/answers/reset")); // mockMvc.perform(delete("/answers/reset"));
} catch (Exception ex) { // } catch (Exception ex) {
throw new RuntimeException(ex); // throw new RuntimeException(ex);
} // }
} }
@Test @Test
......
package org.fuseri.moduleexercise.question; package org.fuseri.moduleexercise.question;
import com.fasterxml.jackson.databind.ObjectMapper; 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.AnswerInQuestionCreateDto;
import org.fuseri.model.dto.exercise.ExerciseCreateDto; import org.fuseri.model.dto.exercise.ExerciseCreateDto;
import org.fuseri.model.dto.exercise.QuestionCreateDto; import org.fuseri.model.dto.exercise.QuestionCreateDto;
import org.fuseri.model.dto.exercise.QuestionDto; 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.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.ResultActions;
import static org.hamcrest.Matchers.is; 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.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
...@@ -36,6 +47,11 @@ public class QuestionTest { ...@@ -36,6 +47,11 @@ public class QuestionTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean
QuestionFacade facade;
private QuestionDto qston;
public static String asJsonString(final Object obj) { public static String asJsonString(final Object obj) {
try { try {
return new ObjectMapper().writeValueAsString(obj); return new ObjectMapper().writeValueAsString(obj);
...@@ -58,41 +74,28 @@ public class QuestionTest { ...@@ -58,41 +74,28 @@ public class QuestionTest {
@BeforeEach @BeforeEach
void init() { void init() {
createExercise(); // createExercise();
qston = new QuestionDto("\"what is the meaning of: costs an arm and leg\"",1,new ArrayList<>());
long id = 2; long id = 2;
var question = new QuestionCreateDto("this statement is false", id, List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true))); 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))); 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; 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 @Test
void testCreateQuestion() throws Exception { void testCreateQuestion() throws Exception {
var exerciseId = 1L; 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))); 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)); var posted = mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON));
posted.andExpect(status().isCreated()) posted.andExpect(status().isCreated())
.andExpect(jsonPath("$.text", is("what is the meaning of: costs an arm and leg"))) .andExpect(jsonPath("$.text", is(qston.getText())))
.andExpect(jsonPath("$.exerciseId", is((int)exerciseId))) .andExpect(jsonPath("$.exerciseId", is((int)qston.getExerciseId())));
.andExpect(jsonPath("$.answers[0].text", is("dis very expencive")));
} }
@Test @Test
...@@ -145,12 +148,15 @@ public class QuestionTest { ...@@ -145,12 +148,15 @@ public class QuestionTest {
@Test @Test
void getQuestion() throws Exception { 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")); var gets = mockMvc.perform(get("/questions/1"));
gets.andExpect(status().isOk()).andExpect(jsonPath("$.text", is("this statement is false"))); gets.andExpect(status().isOk()).andExpect(jsonPath("$.text", is("this statement is false")));
} }
@Test @Test
void getQuestionNotFound() throws Exception { void getQuestionNotFound() throws Exception {
when(facade.find(9999)).thenThrow(new EntityNotFoundException());
var gets = mockMvc.perform(get("/questions/9999")); var gets = mockMvc.perform(get("/questions/9999"));
gets.andExpect(status().isNotFound()); gets.andExpect(status().isNotFound());
} }
...@@ -158,6 +164,10 @@ public class QuestionTest { ...@@ -158,6 +164,10 @@ public class QuestionTest {
@Test @Test
void getAnswer() throws Exception { 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")); var gets = mockMvc.perform(get("/questions/2/answers"));
gets.andExpect(status().isOk()) gets.andExpect(status().isOk())
.andExpect(jsonPath("$[0].text", is("February"))) .andExpect(jsonPath("$[0].text", is("February")))
...@@ -175,10 +185,12 @@ public class QuestionTest { ...@@ -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)); var gets = mockMvc.perform(put("/questions/1").content(updated).contentType(MediaType.APPLICATION_JSON));
gets.andExpect(status().isOk()) gets.andExpect(status().isOk());
.andExpect(jsonPath("$.text", is("wat a paradox?")));
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment