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

added tests to exercise

parent 4a131daf
No related branches found
No related tags found
3 merge requests!31M2,!28M2 user,!27Draft: M2 user
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 jakarta.persistence.EntityNotFoundException; import org.fuseri.model.dto.common.Result;
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.Test; import org.junit.jupiter.api.Test;
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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.MatcherAssert.assertThat; import java.util.Map;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.equalTo;
//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
//import static org.assertj.core.api.ClassBasedNavigableIterableAssert.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 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.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.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc @AutoConfigureMockMvc
public class ExerciseTest { public class ExerciseTest {
...@@ -42,47 +39,83 @@ public class ExerciseTest { ...@@ -42,47 +39,83 @@ public class ExerciseTest {
} }
@Test @Test
void getExercise() { void getExercise() {
var postExercise = new ExerciseCreateDto("idioms","exercise on basic idioms",2,"0"); var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, "0");
String id = ""; String id = "";
try { try {
var dis = mockMvc.perform(post("/exercises") var dis = mockMvc.perform(post("/exercises")
.content(asJsonString(postExercise)) .content(asJsonString(postExercise))
.contentType(MediaType.APPLICATION_JSON)); .contentType(MediaType.APPLICATION_JSON));
var ok = dis.andReturn().getResponse().getContentAsString(); var ok = dis.andReturn().getResponse().getContentAsString();
var ll = objectMapper.readValue(ok,ExerciseDto.class); var ll = objectMapper.readValue(ok, ExerciseDto.class);
id = ll.getId(); id = ll.getId();
} } catch (Exception e) {
catch (Exception e) { assert (false);
assert(false);
} }
// no underlying database
try { try {
var theId = String.format("/exercises/%s",id); var theId = String.format("/exercises/%s", id);
var smth = mockMvc.perform(get(theId)); var smth = mockMvc.perform(get(theId));
} catch (Exception e) { } catch (Exception e) {
// throw new RuntimeException(e); //do absolutely nothing
} }
}
@Test
void getFiltered() {
var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 0, "0");
var postExercise1 = new ExerciseCreateDto("idioms1", "exercise on basic idioms", 0, "0");
var postExercise2 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, "0");
try {
var exercise1 = mockMvc.perform(post("/exercises")
.content(asJsonString(postExercise))
.contentType(MediaType.APPLICATION_JSON));
var exercise2 = mockMvc.perform(post("/exercises")
.content(asJsonString(postExercise1))
.contentType(MediaType.APPLICATION_JSON));
var exercise3 = mockMvc.perform(post("/exercises")
.content(asJsonString(postExercise2))
.contentType(MediaType.APPLICATION_JSON));
} catch (Exception e) {
//do absolutly nothing
}
Map<String, String> params;
// params.put("page","0")
try {
var filtered = mockMvc.perform(get("/exercises/filter")
.param("page", "0")
.param("courseId", "0")
.param("difficulty", "0"));
var content = filtered.andReturn().getResponse().getContentAsString();
var res = objectMapper.readValue(content, new TypeReference<Result<ExerciseDto>>() {
});
assert (res.getTotal() == 2);
} catch (Exception e) {
assert (false);
}
} }
@Test @Test
void testCreateExercise() throws Exception { void testCreateExercise() throws Exception {
var expectedResponse = new ExerciseDto(); var expectedResponse = new ExerciseDto();
var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, "0");
var postExercise = new ExerciseCreateDto("idioms","exercise on basic idioms",2,"0");
mockMvc.perform(post("/exercises") mockMvc.perform(post("/exercises")
.content(asJsonString(postExercise)) .content(asJsonString(postExercise))
...@@ -95,4 +128,58 @@ public class ExerciseTest { ...@@ -95,4 +128,58 @@ public class ExerciseTest {
.andReturn().getResponse().getContentAsString(); .andReturn().getResponse().getContentAsString();
} }
@Test
void TestUpdate() {
var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, "0");
String id = "";
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("idioms");
expectedExercise.setDescription("exercise on basic idioms");
var content = """
{
"name": "idioms",
"description": "exercise on basic idioms",
"difficulty": 2,
"courseId": "idioms"
}
""";
try {
var theId = String.format("/exercises/%s", id);
var dis = mockMvc.perform(put(theId).content(content).contentType(MediaType.APPLICATION_JSON));
var str = dis.andReturn().getResponse().getContentAsString();
var res = objectMapper.readValue(str, ExerciseDto.class);
var eq = res.equals(expectedExercise);
assert res.equals(expectedExercise);
} catch (Exception 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