diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f5ceaf33f2e4269e53716884151d63c5957e9b21 --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseTest.java @@ -0,0 +1,98 @@ +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(); + } + +}