From 7790c0e8a2662b02dfb32d7a7364c9da128909ee Mon Sep 17 00:00:00 2001
From: Dominika Zemanovicova <xzemanov@fi.muni.cz>
Date: Fri, 28 Apr 2023 22:18:22 +0200
Subject: [PATCH] Rename QuestionTest, reformat and delete unused

---
 ...nTest.java => QuestionControllerTest.java} | 72 +++++--------------
 1 file changed, 16 insertions(+), 56 deletions(-)
 rename application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/{QuestionTest.java => QuestionControllerTest.java} (68%)

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/QuestionControllerTest.java
similarity index 68%
rename from application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java
rename to application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionControllerTest.java
index 8383971d..0f5cbfa7 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/QuestionControllerTest.java
@@ -4,43 +4,32 @@ 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 java.util.ArrayList;
+import java.util.List;
 
 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.*;
 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;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
-
 
 @SpringBootTest
 @AutoConfigureMockMvc
-public class QuestionTest {
+public class QuestionControllerTest {
 
     @Autowired
     ObjectMapper objectMapper;
@@ -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
     void init() {
-//        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;
+        qston = new QuestionDto("\"what is the meaning of: costs an arm and leg\"", 1, new ArrayList<>());
     }
 
 
@@ -94,7 +64,7 @@ public class QuestionTest {
 
         posted.andExpect(status().isCreated())
                 .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 {
         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
     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);
         var gets = mockMvc.perform(get("/questions/1"));
         gets.andExpect(status().isOk()).andExpect(jsonPath("$.text", is("this statement is false")));
@@ -164,8 +126,6 @@ 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"));
@@ -177,7 +137,7 @@ public class QuestionTest {
     }
 
     @Test
-    void TestUpdate() throws Exception {
+    void testUpdate() throws Exception {
         var updated = """
                 {
                   "text": "wat a paradox?",
@@ -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);
         var gets = mockMvc.perform(put("/questions/1").content(updated).contentType(MediaType.APPLICATION_JSON));
@@ -193,13 +153,13 @@ public class QuestionTest {
         gets.andExpect(status().isOk());
     }
 
-@Test
+    @Test
     void deleteExisting() {
-    try {
-        mockMvc.perform(delete("/questions/1")).andExpect(status().isNoContent());
-    } catch (Exception e) {
-        throw new RuntimeException(e);
+        try {
+            mockMvc.perform(delete("/questions/1")).andExpect(status().isNoContent());
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
-}
 
 }
-- 
GitLab