Skip to content
Snippets Groups Projects
Commit 7790c0e8 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Rename QuestionTest, reformat and delete unused

parent 32dabc3c
No related branches found
No related tags found
1 merge request!32M2 fix exercise
Pipeline #
...@@ -4,43 +4,32 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -4,43 +4,32 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.exercise.AnswerDto; 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.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.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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers; 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.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 java.util.ArrayList;
import java.util.List;
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.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; 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.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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
public class QuestionTest { public class QuestionControllerTest {
@Autowired @Autowired
ObjectMapper objectMapper; ObjectMapper objectMapper;
...@@ -60,28 +49,9 @@ public class QuestionTest { ...@@ -60,28 +49,9 @@ public class QuestionTest {
} }
} }
private void createExercise() {
var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L);
var postExercise2 = new ExerciseCreateDto("riddles", "simple english riddles", 2, 0L);
try {
mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON));
mockMvc.perform(post("/exercises").content(asJsonString(postExercise2)).contentType(MediaType.APPLICATION_JSON));
} catch (Exception e) {
assert (false);
}
}
@BeforeEach @BeforeEach
void init() { void init() {
// createExercise(); qston = new QuestionDto("\"what is the meaning of: costs an arm and leg\"", 1, new ArrayList<>());
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;
} }
...@@ -94,7 +64,7 @@ public class QuestionTest { ...@@ -94,7 +64,7 @@ public class QuestionTest {
posted.andExpect(status().isCreated()) posted.andExpect(status().isCreated())
.andExpect(jsonPath("$.text", is(qston.getText()))) .andExpect(jsonPath("$.text", is(qston.getText())))
.andExpect(jsonPath("$.exerciseId", is((int)qston.getExerciseId()))); .andExpect(jsonPath("$.exerciseId", is((int) qston.getExerciseId())));
} }
...@@ -137,18 +107,10 @@ public class QuestionTest { ...@@ -137,18 +107,10 @@ public class QuestionTest {
posted.andExpect(status().is4xxClientError()); posted.andExpect(status().is4xxClientError());
} }
private QuestionDto createQuestion(long id) throws Exception {
var question = new QuestionCreateDto("this statement is false", id, List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true)));
var posted = mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON));
var cont = posted.andReturn().getResponse().getContentAsString();
var res = objectMapper.readValue(cont, QuestionDto.class);
return res;
}
@Test @Test
void getQuestion() throws Exception { void getQuestion() throws Exception {
var question = new QuestionDto("this statement is false",1L,new ArrayList<>()); var question = new QuestionDto("this statement is false", 1L, new ArrayList<>());
when(facade.find(1)).thenReturn(question); 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")));
...@@ -164,8 +126,6 @@ public class QuestionTest { ...@@ -164,8 +126,6 @@ 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)); var sss = List.of(new AnswerDto("February", false), new AnswerDto("All of them", true));
when(facade.getQuestionAnswers(2)).thenReturn(sss); when(facade.getQuestionAnswers(2)).thenReturn(sss);
var gets = mockMvc.perform(get("/questions/2/answers")); var gets = mockMvc.perform(get("/questions/2/answers"));
...@@ -177,7 +137,7 @@ public class QuestionTest { ...@@ -177,7 +137,7 @@ public class QuestionTest {
} }
@Test @Test
void TestUpdate() throws Exception { void testUpdate() throws Exception {
var updated = """ var updated = """
{ {
"text": "wat a paradox?", "text": "wat a paradox?",
...@@ -185,7 +145,7 @@ public class QuestionTest { ...@@ -185,7 +145,7 @@ public class QuestionTest {
} }
"""; """;
var question = new QuestionDto("wat a paradox?",1,new ArrayList<>()); var question = new QuestionDto("wat a paradox?", 1, new ArrayList<>());
when(facade.patchUpdate(ArgumentMatchers.eq(1), ArgumentMatchers.isA(QuestionUpdateDto.class))).thenReturn(question); 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));
...@@ -193,13 +153,13 @@ public class QuestionTest { ...@@ -193,13 +153,13 @@ public class QuestionTest {
gets.andExpect(status().isOk()); gets.andExpect(status().isOk());
} }
@Test @Test
void deleteExisting() { void deleteExisting() {
try { try {
mockMvc.perform(delete("/questions/1")).andExpect(status().isNoContent()); mockMvc.perform(delete("/questions/1")).andExpect(status().isNoContent());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
}
} }
}
} }
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