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

modified ExerciseTest

parents a10b9c15 09b2b0cd
No related branches found
No related tags found
2 merge requests!31M2,!30M2 exercise
Pipeline #
package org.fuseri.moduleexercise.exercise; package org.fuseri.moduleexercise.exercise;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
//import org.fuseri.model.dto.common.Result;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.exercise.ExerciseCreateDto; import org.fuseri.model.dto.exercise.ExerciseCreateDto;
import org.fuseri.model.dto.exercise.ExerciseDto; import org.fuseri.model.dto.exercise.ExerciseDto;
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.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.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
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 java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when; 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.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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;
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
public class ExerciseTest { public class ExerciseControllerTest {
@Autowired @Autowired
ObjectMapper objectMapper; ObjectMapper objectMapper;
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
ExerciseFacade facade; ExerciseFacade facade;
...@@ -52,7 +46,6 @@ public class ExerciseTest { ...@@ -52,7 +46,6 @@ public class ExerciseTest {
} }
} }
// private ExerciseCreateDto exercise;
private ExerciseDto exerciseDto; private ExerciseDto exerciseDto;
private ExerciseDto exerciseDto1; private ExerciseDto exerciseDto1;
private ExerciseDto exerciseDto2; private ExerciseDto exerciseDto2;
...@@ -84,92 +77,41 @@ public class ExerciseTest { ...@@ -84,92 +77,41 @@ 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 {
// 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);
// }
} }
@Test @Test
void getExercise() { void getExercise() throws Exception {
long id = 1L; long id = 1L;
when(facade.find(id)).thenReturn(exerciseDto); when(facade.find(id)).thenReturn(exerciseDto);
try { mockMvc.perform(get("/exercises/{id}", id))
var theId = String.format("/exercises/%s", id); .andExpect(status().isOk())
var smth = mockMvc.perform(get(theId)); .andExpect(jsonPath("$.name", is(exerciseCreateDto.getName())))
smth.andExpect(status().isOk()) .andExpect(jsonPath("$.description", is(exerciseCreateDto.getDescription())))
.andExpect(jsonPath("$.name", is(exerciseCreateDto.getName()))) .andExpect(jsonPath("$.courseId", is((int) exerciseCreateDto.getCourseId())))
.andExpect(jsonPath("$.description", is(exerciseCreateDto.getDescription()))) .andExpect(jsonPath("$.difficulty", is(exerciseCreateDto.getDifficulty())));
.andExpect(jsonPath("$.courseId", is((int) exerciseCreateDto.getCourseId())))
.andExpect(jsonPath("$.difficulty", is(exerciseCreateDto.getDifficulty())));
} catch (Exception e) {
//do absolutely nothing
}
}
@Test
void deleteExercise() {
long id = 1L;
// when(facade.delete(id));
try {
var theId = String.format("/exercises/%s", id);
var smth = mockMvc.perform(delete(theId));
smth.andExpect(status().isNoContent());
} catch (Exception e) {
//do absolutely nothing
}
} }
@Test @Test
void deleteExercise_notFound() { void testDelete() throws Exception {
long id = 999999L; mockMvc.perform(delete("/exercises/1"))
try { .andExpect(status().is2xxSuccessful());
var theId = String.format("/exercises/%s", id);
var smth = mockMvc.perform(delete(theId));
smth.andExpect(status().isNoContent());
} catch (Exception e) {
//do absolutely nothing
}
} }
@Test @Test
void getExercise_notFound() { void getExercise_notFound() throws Exception {
long id = 999999L; long id = 1L;
when(facade.find(id)).thenThrow(new EntityNotFoundException("")); when(facade.find(id)).thenThrow(new EntityNotFoundException(""));
try { mockMvc.perform(get("/exercises/{id}", id))
var theId = String.format("/exercises/%s", id); .andExpect(status().isNotFound());
var smth = mockMvc.perform(get(theId));
smth.andExpect(status().isNotFound());
} catch (Exception e) {
//do absolutely nothing
}
} }
@Test @Test
void FindAll() { void FindAll() {
when(facade.findAll(0)).thenReturn(new PageImpl<>(new ArrayList<>())); when(facade.findAll(0)).thenReturn(new PageImpl<>(new ArrayList<>()));
try { try {
var response = mockMvc.perform(get("/exercises").param("page", "0")); var response = mockMvc.perform(get("/exercises").param("page", "0"));
var dis = response.andExpect(status().isOk()) var dis = response.andExpect(status().isOk())
.andExpect(status().is2xxSuccessful()).andReturn().getResponse().getContentAsString(); .andExpect(status().is2xxSuccessful()).andReturn().getResponse().getContentAsString();
assertTrue(dis.contains("\"content\":[]")); assertTrue(dis.contains("\"content\":[]"));
} catch (Exception e) { } catch (Exception e) {
...@@ -181,7 +123,7 @@ public class ExerciseTest { ...@@ -181,7 +123,7 @@ public class ExerciseTest {
@Test @Test
void getFiltered() { void getFiltered() {
when(facade.findByCourseIdAndDifficulty(0,2,0)).thenReturn(new PageImpl<>(new ArrayList<>())); when(facade.findByCourseIdAndDifficulty(0, 2, 0)).thenReturn(new PageImpl<>(new ArrayList<>()));
try { try {
var filtered = mockMvc.perform(get("/exercises/filter").param("page", "0").param("courseId", "0").param("difficulty", "2")); var filtered = mockMvc.perform(get("/exercises/filter").param("page", "0").param("courseId", "0").param("difficulty", "2"));
var content = filtered.andReturn().getResponse().getContentAsString(); var content = filtered.andReturn().getResponse().getContentAsString();
...@@ -194,7 +136,7 @@ public class ExerciseTest { ...@@ -194,7 +136,7 @@ public class ExerciseTest {
@Test @Test
void testCreateExercise() throws Exception { void testCreateExercise() throws Exception {
when(facade.create(exerciseCreateDto)).thenReturn(exerciseDto); when(facade.create(ArgumentMatchers.isA(ExerciseCreateDto.class))).thenReturn(exerciseDto);
mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON)) mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated()) .andExpect(status().isCreated())
.andExpect(jsonPath("$.name").value("idioms")) .andExpect(jsonPath("$.name").value("idioms"))
...@@ -269,36 +211,22 @@ public class ExerciseTest { ...@@ -269,36 +211,22 @@ public class ExerciseTest {
@Test @Test
void testUpdate() { void testUpdate() throws Exception {
long id = 1L; long id = 1L;
when(facade.update(id,exerciseCreateDto)).thenReturn(exerciseDto); when(facade.update(ArgumentMatchers.eq(id), ArgumentMatchers.isA(ExerciseCreateDto.class))).thenReturn(exerciseDto);
try {
var theId = String.format("/exercises/%d", id);
var dis = mockMvc.perform(put(theId).content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON));
dis.andExpect(status().isOk()).andExpect(jsonPath("$.name",is(exerciseDto.getName()))) var theId = String.format("/exercises/%d", id);
.andExpect(jsonPath("$.difficulty",is(exerciseDto.getDifficulty()))) var dis = mockMvc.perform(put(theId).content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON));
.andExpect(jsonPath("$.description",is(exerciseDto.getDescription())))
.andExpect(jsonPath("$.courseId",is(exerciseDto.getCourseId())));
//
// var str = dis.andReturn().getResponse().getContentAsString();
//
// var res = objectMapper.readValue(str, ExerciseDto.class);
//
// assert res.equals(exerciseDto);
} catch (Exception e) {
throw new RuntimeException(e);
}
dis.andExpect(status().isOk()).andExpect(jsonPath("$.name", is(exerciseDto.getName())))
.andExpect(jsonPath("$.difficulty", is(exerciseDto.getDifficulty())))
.andExpect(jsonPath("$.description", is(exerciseDto.getDescription())));
} }
@Test @Test
void testUpdateNotFound() { void testUpdateNotFound() {
long id = 9999L; long id = 9999L;
when(facade.update(id,exerciseCreateDto)).thenThrow(new EntityNotFoundException()); when(facade.update(ArgumentMatchers.eq(id), ArgumentMatchers.isA(ExerciseCreateDto.class))).thenThrow(new EntityNotFoundException());
try { try {
var theId = String.format("/exercises/%d", id); var theId = String.format("/exercises/%d", id);
......
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