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

modified ExerciseTest

parent 4f59d1c7
No related branches found
No related tags found
2 merge requests!31M2,!30M2 exercise
Pipeline #
......@@ -2,7 +2,8 @@ package org.fuseri.moduleexercise.exercise;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.fuseri.model.dto.common.Result;
//import org.fuseri.model.dto.common.Result;
import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.exercise.ExerciseCreateDto;
import org.fuseri.model.dto.exercise.ExerciseDto;
import org.junit.jupiter.api.AfterEach;
......@@ -11,9 +12,18 @@ 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.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
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.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
......@@ -30,6 +40,10 @@ public class ExerciseTest {
@Autowired
private MockMvc mockMvc;
@MockBean
ExerciseFacade facade;
public static String asJsonString(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
......@@ -38,20 +52,42 @@ public class ExerciseTest {
}
}
private ExerciseCreateDto exercise1;
private ExerciseCreateDto exercise2;
private ExerciseCreateDto exercise3;
// private ExerciseCreateDto exercise;
private ExerciseDto exerciseDto;
private ExerciseDto exerciseDto1;
private ExerciseDto exerciseDto2;
private ExerciseCreateDto exerciseCreateDto;
private ExerciseCreateDto exerciseCreateDto1;
private ExerciseCreateDto exerciseCreateDto2;
@BeforeEach
void init() {
exercise1 = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0);
exercise2 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0);
exercise3 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L);
exerciseDto = new ExerciseDto();
exerciseDto.setName("idioms");
exerciseDto.setDescription("exercise on basic idioms");
exerciseDto.setDifficulty(2);
exerciseDto.setCourseId(0);
exerciseDto1 = new ExerciseDto();
exerciseDto1.setName("idioms1");
exerciseDto1.setDescription("exercise on basic idioms");
exerciseDto1.setDifficulty(2);
exerciseDto1.setCourseId(0);
exerciseDto2 = new ExerciseDto();
exerciseDto2.setName("idioms2");
exerciseDto2.setDescription("exercise on basic idioms");
exerciseDto2.setDifficulty(1);
exerciseDto2.setCourseId(0);
exerciseCreateDto = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0);
exerciseCreateDto1 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0);
exerciseCreateDto2 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L);
try {
mockMvc.perform(post("/exercises").content(asJsonString(exercise1)).contentType(MediaType.APPLICATION_JSON));
mockMvc.perform(post("/exercises").content(asJsonString(exercise2)).contentType(MediaType.APPLICATION_JSON));
mockMvc.perform(post("/exercises").content(asJsonString(exercise3)).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(exerciseCreateDto2)).contentType(MediaType.APPLICATION_JSON));
} catch (Exception e) {
assert (false);
}
......@@ -72,14 +108,15 @@ public class ExerciseTest {
@Test
void getExercise() {
long id = 1L;
when(facade.find(id)).thenReturn(exerciseDto);
try {
var theId = String.format("/exercises/%s", id);
var smth = mockMvc.perform(get(theId));
smth.andExpect(status().isOk())
.andExpect(jsonPath("$.name", is(exercise1.getName())))
.andExpect(jsonPath("$.description", is(exercise1.getDescription())))
.andExpect(jsonPath("$.courseId", is((int) exercise1.getCourseId())))
.andExpect(jsonPath("$.difficulty", is(exercise1.getDifficulty())));
.andExpect(jsonPath("$.name", is(exerciseCreateDto.getName())))
.andExpect(jsonPath("$.description", is(exerciseCreateDto.getDescription())))
.andExpect(jsonPath("$.courseId", is((int) exerciseCreateDto.getCourseId())))
.andExpect(jsonPath("$.difficulty", is(exerciseCreateDto.getDifficulty())));
} catch (Exception e) {
//do absolutely nothing
}
......@@ -88,6 +125,7 @@ public class ExerciseTest {
@Test
void deleteExercise() {
long id = 1L;
// when(facade.delete(id));
try {
var theId = String.format("/exercises/%s", id);
var smth = mockMvc.perform(delete(theId));
......@@ -113,6 +151,7 @@ public class ExerciseTest {
@Test
void getExercise_notFound() {
long id = 999999L;
when(facade.find(id)).thenThrow(new EntityNotFoundException(""));
try {
var theId = String.format("/exercises/%s", id);
var smth = mockMvc.perform(get(theId));
......@@ -125,24 +164,14 @@ public class ExerciseTest {
@Test
void FindAll() {
when(facade.findAll(0)).thenReturn(new PageImpl<>(new ArrayList<>()));
try {
var dis = mockMvc.perform(get("/exercises").param("page", "0"));
dis.andExpect(status().isOk())
.andExpect(jsonPath("$.total", is(3)))
.andExpect(jsonPath("$.items[0].name", is(exercise1.getName())))
.andExpect(jsonPath("$.items[0].description", is(exercise1.getDescription())))
.andExpect(jsonPath("$.items[0].difficulty", is(exercise1.getDifficulty())))
.andExpect(jsonPath("$.items[0].courseId", is((int) exercise1.getCourseId())))
.andExpect(jsonPath("$.items[1].name", is(exercise2.getName())))
.andExpect(jsonPath("$.items[1].description", is(exercise2.getDescription())))
.andExpect(jsonPath("$.items[1].difficulty", is(exercise2.getDifficulty())))
.andExpect(jsonPath("$.items[1].courseId", is((int) exercise2.getCourseId())))
.andExpect(jsonPath("$.items[2].name", is(exercise3.getName())))
.andExpect(jsonPath("$.items[2].description", is(exercise3.getDescription())))
.andExpect(jsonPath("$.items[2].difficulty", is(exercise3.getDifficulty())))
.andExpect(jsonPath("$.items[2].courseId", is((int) exercise3.getCourseId())));
var response = mockMvc.perform(get("/exercises").param("page", "0"));
var dis = response.andExpect(status().isOk())
.andExpect(status().is2xxSuccessful()).andReturn().getResponse().getContentAsString();
assertTrue(dis.contains("\"content\":[]"));
} catch (Exception e) {
throw new RuntimeException(e);
}
......@@ -152,40 +181,21 @@ public class ExerciseTest {
@Test
void getFiltered() {
when(facade.findByCourseIdAndDifficulty(0,2,0)).thenReturn(new PageImpl<>(new ArrayList<>()));
try {
var filtered = mockMvc.perform(get("/exercises/filter").param("page", "0").param("courseId", "0").param("difficulty", "2"));
var content = filtered.andReturn().getResponse().getContentAsString();
var res = objectMapper.readValue(content, new TypeReference<Result<ExerciseDto>>() {
});
assert (res.getTotal() == 2);
assertTrue(content.contains("\"content\":[]"));
} catch (Exception e) {
assert (false);
}
}
// @Test
// void getByExercise() throws Exception {
// var theId = String.format("/questions/exercise/%s", 9);
//
// var smth = mockMvc.perform(get(theId).param("page", "0"));
//
// var content = smth.andReturn().getResponse().getContentAsString();
//
// var res = objectMapper.readValue(content, new TypeReference<Result<QuestionDto>>() {
// });
//
//
//
//// assert (res.getItems().get(0).equals(question));
// }
@Test
void testCreateExercise() throws Exception {
var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L);
mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON))
when(facade.create(exerciseCreateDto)).thenReturn(exerciseDto);
mockMvc.perform(post("/exercises").content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name").value("idioms"))
.andExpect(jsonPath("$.description").value("exercise on basic idioms"))
......@@ -196,7 +206,6 @@ public class ExerciseTest {
@Test
void testCreateExerciseEmptyBody() throws Exception {
var postExercise = "";
mockMvc.perform(post("/exercises").content((postExercise)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is4xxClientError()).andReturn().getResponse().getContentAsString();
}
......@@ -261,47 +270,24 @@ public class ExerciseTest {
@Test
void testUpdate() {
var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L);
long id = 1L;
try {
var dis = mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON));
var ok = dis.andReturn().getResponse().getContentAsString();
var ll = objectMapper.readValue(ok, ExerciseDto.class);
id = ll.getId();
} catch (Exception e) {
assert (false);
}
var expectedExercise = new ExerciseDto();
expectedExercise.setId(id);
expectedExercise.setName("idioms");
expectedExercise.setDifficulty(2);
expectedExercise.setCourseId(0L);
expectedExercise.setDescription("exercise on basic idioms");
var content = """
{
"name": "idioms",
"description": "exercise on basic idioms",
"difficulty": 2,
"courseId": 0
}
""";
when(facade.update(id,exerciseCreateDto)).thenReturn(exerciseDto);
try {
var theId = String.format("/exercises/%d", id);
var dis = mockMvc.perform(put(theId).content(content).contentType(MediaType.APPLICATION_JSON));
var dis = mockMvc.perform(put(theId).content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON));
var str = dis.andReturn().getResponse().getContentAsString();
var res = objectMapper.readValue(str, ExerciseDto.class);
assert res.equals(expectedExercise);
dis.andExpect(status().isOk()).andExpect(jsonPath("$.name",is(exerciseDto.getName())))
.andExpect(jsonPath("$.difficulty",is(exerciseDto.getDifficulty())))
.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);
......@@ -311,18 +297,12 @@ public class ExerciseTest {
@Test
void testUpdateNotFound() {
long id = 999999L;
var content = """
{
"name": "idioms",
"description": "exercise on basic idioms",
"difficulty": 2,
"courseId": 0
}
""";
long id = 9999L;
when(facade.update(id,exerciseCreateDto)).thenThrow(new EntityNotFoundException());
try {
var theId = String.format("/exercises/%d", id);
mockMvc.perform(put(theId).content(content).contentType(MediaType.APPLICATION_JSON))
mockMvc.perform(put(theId).content(asJsonString(exerciseCreateDto)).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
} catch (Exception 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