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

initial tests

parent 2e39b5a6
No related branches found
No related tags found
3 merge requests!31M2,!28M2 user,!27Draft: M2 user
package org.fuseri.moduleexercise.exercise;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityNotFoundException;
import org.fuseri.model.dto.exercise.ExerciseCreateDto;
import org.fuseri.model.dto.exercise.ExerciseDto;
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.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.MatcherAssert.assertThat;
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.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@SpringBootTest
@AutoConfigureMockMvc
public class ExerciseTest {
@Autowired
ObjectMapper objectMapper;
@Autowired
private MockMvc mockMvc;
public static String asJsonString(final Object obj) {
try {
return new ObjectMapper().writeValueAsString(obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
void getExercise() {
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);
}
// no underlying database
try {
var theId = String.format("/exercises/%s",id);
var smth = mockMvc.perform(get(theId));
} catch (Exception e) {
// throw new RuntimeException(e);
}
}
@Test
void testCreateExercise() throws Exception {
var expectedResponse = new ExerciseDto();
var postExercise = new ExerciseCreateDto("idioms","exercise on basic idioms",2,"0");
mockMvc.perform(post("/exercises")
.content(asJsonString(postExercise))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("idioms"))
.andExpect(jsonPath("$.description").value("exercise on basic idioms"))
.andExpect(jsonPath("$.difficulty").value(2))
.andExpect(jsonPath("$.courseId").value("0"))
.andReturn().getResponse().getContentAsString();
}
}
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