diff --git a/application/model/src/main/java/org/fuseri/model/dto/exercise/ExerciseCreateDto.java b/application/model/src/main/java/org/fuseri/model/dto/exercise/ExerciseCreateDto.java index 21e7ee336865644ad6583481d791573688b8fb1e..6d1cb1a7727b669e683f775e63aafc0e781816fe 100644 --- a/application/model/src/main/java/org/fuseri/model/dto/exercise/ExerciseCreateDto.java +++ b/application/model/src/main/java/org/fuseri/model/dto/exercise/ExerciseCreateDto.java @@ -9,7 +9,6 @@ import lombok.Getter; @AllArgsConstructor @Getter public class ExerciseCreateDto { - @NotBlank private String name; diff --git a/application/module-exercise/pom.xml b/application/module-exercise/pom.xml index d42b3eae2bc1ebdeaef9e5caf06e5267d99c6d9f..0623aed03e08f85c20ba8ee71fa32cbcd31bbfe0 100644 --- a/application/module-exercise/pom.xml +++ b/application/module-exercise/pom.xml @@ -44,6 +44,12 @@ <version>0.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>org.fuseri</groupId> + <artifactId>sprachschulsystem</artifactId> + <version>0.0.1-SNAPSHOT</version> + <scope>test</scope> + </dependency> </dependencies> <build> diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java index 696c370354ea121b8f76fff0fc02773a0378f63d..d1eeba8b0e11d735fd939dea331037f6f8b5f0ee 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java @@ -76,4 +76,11 @@ public class AnswerController { return ResponseEntity.notFound().build(); } } + +// @TestOnly + @DeleteMapping("/reset") + public void resetTable() { + facade.resetTable(); + } + } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java index 5f885b00b11525ecd2cfb0ddbbffe43a0f40f071..99022c65f0c2b0a9b5b2066cb8ca8f7e224971cd 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java @@ -47,4 +47,9 @@ public class AnswerFacade { public void delete(long id) { answerService.delete(id); } + + public void resetTable() { + answerService.reset(); + answerService.resetId(); + } } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerRepository.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerRepository.java index 8d0844a898915658d73220923586a5630ac351a7..f722165117de22968c9fb25690dc77fe51d3f5ac 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerRepository.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerRepository.java @@ -1,8 +1,11 @@ package org.fuseri.moduleexercise.answer; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -19,4 +22,9 @@ public interface AnswerRepository extends JpaRepository<Answer, Long> { * @return a list of all answers to the specified question */ List<Answer> findByQuestionId(@Param("questionId") long questionId); + + @Transactional + @Modifying + @Query( value = "ALTER TABLE Answer ALTER COLUMN id RESTART WITH 1",nativeQuery = true) + void reserId(); } \ No newline at end of file diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerService.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerService.java index f5346fc5993874b4de7ca7d49fe39ba8c17ee6ba..f7883456a055fab400e6258a1548bd6bc327dd5e 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerService.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerService.java @@ -58,4 +58,8 @@ public class AnswerService extends DomainService<Answer> { return repository.findById(id) .orElseThrow(() -> new EntityNotFoundException("Answer '" + id + "' not found.")); } + + public void resetId() { + repository.reserId(); + } } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java index 9e15af6f915bbfc3269950a3466858a84a1d6551..ee083a04770e021365bd3565897bde39b265d7f8 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/common/DomainService.java @@ -54,4 +54,9 @@ public abstract class DomainService<T extends DomainObject> { public void delete(long id) { getRepository().deleteById(id); } + + public void reset() { + getRepository().deleteAll(); +// getRepository().id/ + } } \ No newline at end of file diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java index 96f91fc77b92040b231cc51218ff393a26184530..607b3814f604dcdfee01541f7a4a6d752784416f 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java @@ -181,4 +181,10 @@ public class ExerciseController { return ResponseEntity.noContent().build(); } +// @TestOnly + @DeleteMapping("/reset") + public void resetTable() { + facade.resetTable(); + } + } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java index c6206ea03352eab77bb932ca5f603dad6f9ef3c2..35cea2f61c3bf72d6c94266cfb369eb0e0c70fcc 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java @@ -117,4 +117,9 @@ public class ExerciseFacade { public void delete(long id) { exerciseService.delete(id); } + + public void resetTable() { + exerciseService.reset(); + exerciseService.resetId(); + } } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseMapper.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseMapper.java index 3c211aaa5d034ecba3b54e738da0ebb4315b51bf..3b44db86873b1f10c1911149a9a018f25dfb423a 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseMapper.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseMapper.java @@ -1,9 +1,14 @@ package org.fuseri.moduleexercise.exercise; +import org.fuseri.model.dto.course.CourseDto; import org.fuseri.model.dto.exercise.ExerciseCreateDto; import org.fuseri.model.dto.exercise.ExerciseDto; import org.fuseri.moduleexercise.common.DomainMapper; import org.mapstruct.Mapper; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; + +import java.util.List; /** * Mapper between Exercises and their corresponding DTOs @@ -18,4 +23,10 @@ public interface ExerciseMapper extends DomainMapper<Exercise, ExerciseDto> { * @return corresponding Exercise entity created from DTO */ Exercise fromCreateDto(ExerciseCreateDto dto); + + List<ExerciseDto> mapToList(List<Exercise> persons); + + default Page<ExerciseDto> mapToPageDto(Page<Exercise> courses) { + return new PageImpl<>(mapToList(courses.getContent()), courses.getPageable(), courses.getTotalPages()); + } } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseRepository.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseRepository.java index 052c19636029c08ba811b891205c727348a8cf8f..02cbc047a790b6dcad94a4a88157e2c0bb850e9c 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseRepository.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseRepository.java @@ -5,9 +5,11 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; /** * A repository interface for managing Exercise entities @@ -30,4 +32,12 @@ public interface ExerciseRepository extends JpaRepository<Exercise, Long> { @Query("SELECT q FROM Exercise e JOIN e.questions q WHERE e.id = :exerciseId") Page<Question> getQuestions(PageRequest pageRequest, @Param("exerciseId") Long exerciseId); + + + @Modifying + @Transactional + @Query( value = "ALTER TABLE Exercise ALTER COLUMN id RESTART WITH 1",nativeQuery = true) + void resetId(); + + } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseService.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseService.java index 01f31e5c060855d957d997cb5b13401d15481cbe..0caf8b0d14a100701416d872e7f78fa104237620 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseService.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseService.java @@ -84,4 +84,9 @@ public class ExerciseService extends DomainService<Exercise> { PageRequest.of(page, DomainService.DEFAULT_PAGE_SIZE), exerciseId); } + + @Transactional + public void resetId() { + repository.resetId(); + } } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java index 8fe1e2a19f4fd2663aa3a22e0e5f9848829f8800..d25c06dbe6f0a05c61eae2f89fe2467aa14e6625 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionFacade.java @@ -96,4 +96,15 @@ public class QuestionFacade { public void delete(long id) { questionService.delete(id); } + + //@TestOnly + public void reset() { + questionService.reset(); + questionService.resetId(); + } + + public void resetTable() { + questionService.reset(); + questionService.resetId(); + } } diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java index b5b2adc4d3208451e1d1789654def49121feb14f..0c11e6ecb29d9e026066678822213adef314d1c1 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java @@ -1,11 +1,21 @@ package org.fuseri.moduleexercise.question; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; /** * A repository interface for managing Question entities */ @Repository public interface QuestionRepository extends JpaRepository<Question, Long> { + + //@TestOnly + @Modifying + @Transactional + @Query( value = "ALTER TABLE Question ALTER COLUMN id RESTART WITH 1",nativeQuery = true) + void resetId(); + } \ No newline at end of file diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerFacadeTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerFacadeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..af06853f22d3467c1365aefdf7318e58701c754d --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerFacadeTest.java @@ -0,0 +1,91 @@ +package org.fuseri.moduleexercise.answer; + +import org.fuseri.model.dto.exercise.AnswerCreateDto; +import org.fuseri.model.dto.exercise.AnswerDto; +import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto; +import org.fuseri.model.dto.exercise.AnswersCreateDto; +import org.fuseri.model.dto.exercise.QuestionUpdateDto; +import org.fuseri.moduleexercise.exercise.Exercise; +import org.fuseri.moduleexercise.question.Question; +import org.fuseri.moduleexercise.question.QuestionService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import java.util.HashSet; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +public class AnswerFacadeTest { + + private final Exercise exercise = new Exercise(); + private final Question question = new Question("text",new HashSet<>(),exercise); + + + private final AnswerDto answerDto = new AnswerDto("name",true); + + private final AnswerCreateDto answerCreateDto = new AnswerCreateDto("name",true,1L); + private final AnswerInQuestionCreateDto inQuestionCreateDto = new AnswerInQuestionCreateDto("name",true); + private final AnswersCreateDto answersCreateDto = new AnswersCreateDto(1L, List.of(inQuestionCreateDto)); + + private final QuestionUpdateDto questionUpdateDto = QuestionUpdateDto.builder().build(); + + private final Answer answer = new Answer("name",true,question); + + @MockBean + AnswerRepository repository; + @Autowired + AnswerFacade facade; + + @MockBean + AnswerService service; + + @MockBean + QuestionService questionService; + + @MockBean + AnswerMapper mapper; + @Test + void create() { + long id = 1; + when(service.find(id)).thenReturn(answer); + when(mapper.fromCreateDto(inQuestionCreateDto)).thenReturn(answer); + when(service.create(answer)).thenReturn(answer); + when(mapper.toDto(answer)).thenReturn(answerDto); + when(questionService.find(id)).thenReturn(question); + when(mapper.toDtoList(List.of(answer))).thenReturn(List.of(answerDto)); + + var actualDto = facade.createMultiple(answersCreateDto); + + assertEquals(List.of(answerDto), actualDto); + + } + + @Test + void update() { + Long id = 1L; + when(repository.existsById(id)).thenReturn(true); + when(service.find(id)).thenReturn(answer); + when(mapper.fromCreateDto(answerCreateDto)).thenReturn(answer); + when(service.update(answer)).thenReturn(answer); + when(mapper.toDto(answer)).thenReturn(answerDto); + AnswerDto actualDto = facade.update(id,answerCreateDto); + + + assertEquals(answerDto, actualDto); + } + @Test + void testDelete() { + Long id = 1L; + when(service.find(id)).thenReturn(answer); + when(questionService.find(answer.getId())).thenReturn(question); + facade.delete(id); + verify(service).delete(id); + } + +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerRepositoryTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerRepositoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9a4179ea8973bf1bf74b7e58b1c6d63ffd08093d --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerRepositoryTest.java @@ -0,0 +1,101 @@ +package org.fuseri.moduleexercise.answer; + +import org.fuseri.moduleexercise.exercise.Exercise; +import org.fuseri.moduleexercise.exercise.ExerciseRepository; +import org.fuseri.moduleexercise.question.Question; +import org.fuseri.moduleexercise.question.QuestionRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; + +@DataJpaTest +public class AnswerRepositoryTest { + + @Autowired + AnswerRepository repository; + + @Autowired + QuestionRepository questionRepository; + + @Autowired + ExerciseRepository exerciseRepository; + + @Autowired + TestEntityManager entityManager; + + +// Question question = new Question("name","desc",2,1L,new HashSet<>()); + + Exercise exercise = new Exercise("name","desc",2,1L,new HashSet<>()); + + Question question = new Question("text",new HashSet<>(),exercise); + + Answer answer = new Answer("text",false,question); + Answer answer2 = new Answer("text2",true,question); + + @BeforeEach + void init() { + exerciseRepository.save(exercise); + questionRepository.save(question); + } + + @Test + void saveQuestion() { + Question saved = questionRepository.save(question); + + Assertions.assertNotNull(saved); + Assertions.assertEquals(question, saved); + } + + @Test + void findById() { + entityManager.persist(answer); + entityManager.flush(); + + Answer found = repository.findById(answer.getId()).orElse(null); +// Question found = questionRepository.findById(question.getId()).orElse(null); + + Assertions.assertNotNull(found); + Assertions.assertEquals(found, answer); + } + + + @Test + void testFindAllQuestions() { + entityManager.persist(answer); + entityManager.persist(answer2); + + Page<Answer> coursePage = repository.findAll(PageRequest.of(0, 42)); + + Assertions.assertEquals(2, coursePage.getTotalElements()); + Assertions.assertEquals(coursePage.getContent(), Arrays.asList(answer, answer2)); + } + + + @Test + void testFindAllQuestionsEmpty() { + Page<Answer> coursePage = repository.findAll(PageRequest.of(0, 42)); + + Assertions.assertEquals(0, coursePage.getTotalElements()); + Assertions.assertEquals(coursePage.getContent(), new ArrayList<>()); + } + + @Test + void testDeleteQuestion() { + Long id = entityManager.persist(answer).getId(); + entityManager.flush(); + + questionRepository.deleteById(id); + + Assertions.assertTrue(questionRepository.findById(id).isEmpty()); + } +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerServiceTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..2c67bdc3bf83a6dd03a22983cbd6eb79a4a35501 --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerServiceTest.java @@ -0,0 +1,82 @@ +package org.fuseri.moduleexercise.answer; + +import jakarta.persistence.EntityNotFoundException; +import org.fuseri.moduleexercise.exercise.Exercise; +import org.fuseri.moduleexercise.question.Question; +import org.fuseri.moduleexercise.question.QuestionRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +public class AnswerServiceTest { + @MockBean + AnswerRepository repository; + + @MockBean + QuestionRepository questionRepository; + + @Autowired + AnswerService service; + + Answer answer; + Question question; + Exercise exercise; + + @BeforeEach + void setup() { + exercise = new Exercise("idioms", "exercise on basic idioms", 2, 1L, new HashSet<Question>()); + question = new Question("text", new HashSet<>(), exercise); + answer = new Answer("text", false, question); + } + + + @Test + void create() { + when(repository.save(answer)).thenReturn(answer); + Answer result = service.create(answer); + Assertions.assertEquals(answer, result); + verify(repository).save(answer); + } + + @Test + void notFound() { + when(repository.findById(anyLong())).thenReturn(Optional.empty()); + + Assertions.assertThrows(EntityNotFoundException.class, () -> service.find(anyLong())); + } + + @Test + void find() { + when(repository.findById(anyLong())).thenReturn(Optional.of(answer)); + + Answer result = service.find(anyLong()); + + Assertions.assertEquals(answer, result); + verify(repository).findById(anyLong()); + } + + @Test + void findByQuestionId() { + long id = 1; + List<Answer> list = Collections.emptyList(); + + when(repository.existsById(id)).thenReturn(true); + when(repository.findByQuestionId(id)).thenReturn(list); + List<Answer> result = service.findAllByQuestionId(1L); + + Assertions.assertEquals(list, result); + verify(repository).findByQuestionId(id); + } +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerTest.java index 46f2097e2410bb935a7591e2d09213791faa9884..86634c70695fa183633deec9e06549fd63d3c5ea 100644 --- a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerTest.java +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/answer/AnswerTest.java @@ -1,14 +1,14 @@ package org.fuseri.moduleexercise.answer; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.fuseri.model.dto.exercise.AnswerDto; +import org.fuseri.model.dto.exercise.AnswerCreateDto; import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto; import org.fuseri.model.dto.exercise.AnswersCreateDto; import org.fuseri.model.dto.exercise.ExerciseCreateDto; -import org.fuseri.model.dto.exercise.ExerciseDto; import org.fuseri.model.dto.exercise.QuestionCreateDto; -import org.fuseri.model.dto.exercise.QuestionDto; +import static org.hamcrest.Matchers.is; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -16,8 +16,11 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import java.util.List; +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.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc @@ -28,6 +31,7 @@ public class AnswerTest { @Autowired private MockMvc mockMvc; + public static String asJsonString(final Object obj) { try { return new ObjectMapper().writeValueAsString(obj); @@ -36,120 +40,189 @@ public class AnswerTest { } } - private QuestionDto createQuestion(long id) throws Exception { - var question = new QuestionCreateDto("this statement is false", id, + @BeforeEach + void init() { + createExercise(); + int exerciseId = 1; + + var question = new QuestionCreateDto("this statement is false", exerciseId, List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true))); + var question2 = new QuestionCreateDto("What month of the year has 28 days?", exerciseId, List.of(new AnswerInQuestionCreateDto("February", false), new AnswerInQuestionCreateDto("All of them", true))); + try { + mockMvc.perform(post("/questions") + .content(asJsonString(question)) + .contentType(MediaType.APPLICATION_JSON)); + mockMvc.perform(post("/questions") + .content(asJsonString(question2)) + .contentType(MediaType.APPLICATION_JSON)); - var posted = mockMvc.perform(post("/questions") - .content(asJsonString(question)) - .contentType(MediaType.APPLICATION_JSON)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @AfterEach + public void cleanup() { + try { + mockMvc.perform(delete("/answers/reset")); + mockMvc.perform(delete("/questions/reset")); + mockMvc.perform(delete("/exercises/reset")); + } catch (Exception ex) { + throw new RuntimeException(ex); - var cont = posted.andReturn().getResponse().getContentAsString(); - var res = objectMapper.readValue(cont, QuestionDto.class); - return res; + } } - private long createExercise() { - var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0); - long id = 0L; + private void createExercise() { + var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0); try { - var dis = mockMvc.perform(post("/exercises") + 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); + throw new RuntimeException(e); } - return id; } @Test void testCreateAnswer() throws Exception { - List<AnswerDto> res = createAnswer(); + var incorrect1 = new AnswerInQuestionCreateDto("True", false); + var incorrect2 = new AnswerInQuestionCreateDto("False", false); - var expected1 = new AnswerDto("True", false); - var expected2 = new AnswerDto("False", false); + var createAnswer = new AnswersCreateDto(1, List.of(incorrect1, incorrect2)); - assert (res.get(0).equals(expected1)); - assert (res.get(1).equals(expected2)); + var posted = mockMvc.perform(post("/answers") + .content(asJsonString(createAnswer)) + .contentType(MediaType.APPLICATION_JSON)); + posted.andExpect(status().isCreated()) + .andExpect(jsonPath("$[0].text",is("True"))) + .andExpect(jsonPath("$[0].correct", is(false))) + .andExpect(jsonPath("$[1].text",is("False"))) + .andExpect(jsonPath("$[1].correct",is(false))); } - private List<AnswerDto> createAnswer() throws Exception { - var exerciseId = createExercise(); - var question = createQuestion(exerciseId); - var incorrect1 = new AnswerInQuestionCreateDto("True", false); - var incorrect2 = new AnswerInQuestionCreateDto("False", false); + @Test + void testCreateAnswerEmptyText() throws Exception { - var createAnswer = new AnswersCreateDto(question.getId(), List.of(incorrect1, incorrect2)); + var incorrect1 = new AnswerInQuestionCreateDto("", false); + + var createAnswer = new AnswersCreateDto(1, List.of(incorrect1)); var posted = mockMvc.perform(post("/answers") .content(asJsonString(createAnswer)) .contentType(MediaType.APPLICATION_JSON)); - var asStr = posted.andReturn().getResponse().getContentAsString(); - - var res = objectMapper.readValue(asStr, new TypeReference<List<AnswerDto>>() { - }); - return res; + posted.andExpect(status().is4xxClientError()); } @Test - void testUpdate() throws Exception { + void testCreateAnswerMissingText() throws Exception { - var exerciseId = createExercise(); - var question = createQuestion(exerciseId); + var prompt = """ + { + "questionId": 1, + "answers": [ + { + "text": "something", + "correct": false + } + ] + } + """; - var incorrect1 = new AnswerInQuestionCreateDto("True", false); - var incorrect2 = new AnswerInQuestionCreateDto("False", false); + var posted = mockMvc.perform(post("/answers") + .content(prompt) + .contentType(MediaType.APPLICATION_JSON)); + posted.andExpect(status().isCreated()); + } - var createAnswer = new AnswersCreateDto(question.getId(), List.of(incorrect1, incorrect2)); + @Test + void testCreateAnswerMissingCorrect() throws Exception { + var prompt = """ + { + "questionId": 1, + "answers": [ + { + "text": "something" + } + ] + } + """; var posted = mockMvc.perform(post("/answers") - .content(asJsonString(createAnswer)) + .content(prompt) .contentType(MediaType.APPLICATION_JSON)); - var asStr = posted.andReturn().getResponse().getContentAsString(); + posted.andExpect(status().isCreated()); + } - var res = objectMapper.readValue(asStr, new TypeReference<List<AnswerDto>>() { - }); + @Test + void testUpdate() throws Exception { + var updated = new AnswerCreateDto("dis true",false,1); + mockMvc.perform(put("/answers/1") + .content(asJsonString(updated)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.text",is("dis true"))) + .andExpect(jsonPath("$.correct",is(false))); + } + @Test + void testUpdateNotFoundAnswer() throws Exception { + var updated = new AnswerCreateDto("dis true",false,1); + mockMvc.perform(put("/answers/9999") + .content(asJsonString(updated)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + } + @Test + void testUpdateEmptyText() throws Exception { + var updated = new AnswerCreateDto("",false,1); + mockMvc.perform(put("/answers/1") + .content(asJsonString(updated)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + } + + @Test + void testUpdateMissingField() throws Exception { var updated = """ { - "text": "dis true", - "correct": false, - "questionId": "%s" + "correct": false, + "questionId": 1 } """; + mockMvc.perform(put("/answers/1") + .content(updated).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + } -// updated = String.format(updated, question.getId()); -// -// var puts = mockMvc.perform(put(String.format("/answers/%s", res.get(0).getId())) -// .content(updated).contentType(MediaType.APPLICATION_JSON)); -// -// var content = puts.andReturn().getResponse().getContentAsString(); -// -// var res2 = objectMapper.readValue(content, AnswerDto.class); -// -// var expected = new AnswerDto("dis true", false); -// -// assert res2.equals(expected); - + @Test + void testDeleteExisting() { + try { + mockMvc.perform(delete("/answers/1")) + .andExpect(status().isNoContent()); + } catch (Exception e) { + assert(false); + } } + @Test + void testDeleteNotFound() { + try { + mockMvc.perform(delete("/answers/9999")) + .andExpect(status().isNotFound()); + } catch (Exception e) { + assert(false); + } + } } diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseFacadeTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseFacadeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d53519ac86aa812e3ccd98dc986892a7db616e1b --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseFacadeTest.java @@ -0,0 +1,106 @@ +package org.fuseri.moduleexercise.exercise; + +import org.fuseri.model.dto.common.Result; +import org.fuseri.model.dto.exercise.ExerciseDto; +import org.fuseri.model.dto.exercise.ExerciseCreateDto; +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.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +@AutoConfigureMockMvc +public class ExerciseFacadeTest { + + private final ExerciseDto exerciseDto = new ExerciseDto(); + + private final ExerciseCreateDto exerciseCreateDto = new ExerciseCreateDto("name","desc",2,1); + + private final Exercise exercise = new Exercise(); + + @Autowired + ExerciseFacade facade; + + @MockBean + ExerciseService service; + + @MockBean + ExerciseMapper mapper; + + + + @Test + void create() { + + when(mapper.fromCreateDto(exerciseCreateDto)).thenReturn(exercise); + when(service.create(exercise)).thenReturn(exercise); + when(mapper.toDto(exercise)).thenReturn(exerciseDto); + + ExerciseDto actualDto = facade.create(exerciseCreateDto); + + assertEquals(exerciseDto, actualDto); + + } + + @Test + void testFindById() { + Long id = 0L; + + when(service.find(id)).thenReturn(exercise); + when(mapper.toDto(exercise)).thenReturn(exerciseDto); + + ExerciseDto actualDto = facade.find(id); + + assertNotNull(actualDto); + assertEquals(exerciseDto, actualDto); + } + + + @Test + void testFindAll() { + int page = 0; + Pageable pageable = PageRequest.of(0, 10); + Page<Exercise> exercisePage = new PageImpl<>(List.of(exercise), pageable, 0); + Result<ExerciseDto> expectedPageDto = new Result<>(); + + + when(service.findAll(page)).thenReturn(exercisePage); + when(mapper.toResult(exercisePage)).thenReturn(expectedPageDto); + + Result<ExerciseDto> actualPageDto = facade.findAll(page); + + assertEquals(expectedPageDto, actualPageDto); + } + + @Test + void update() { + Long id = 1L; + when(mapper.fromCreateDto(exerciseCreateDto)).thenReturn(exercise); + when(service.update(exercise)).thenReturn(exercise); + when(mapper.toDto(exercise)).thenReturn(exerciseDto); + + ExerciseDto actualDto = facade.update(id, exerciseCreateDto); + + assertEquals(exerciseDto, actualDto); + } + + @Test + void testDelete() { + Long id = 1L; + facade.delete(id); + verify(service).delete(id); + } + +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseRepositoryTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseRepositoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6d9187ed60e9daf029e732cd05a553ef3c74d880 --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseRepositoryTest.java @@ -0,0 +1,106 @@ +package org.fuseri.moduleexercise.exercise; + +import org.fuseri.moduleexercise.question.Question; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + + +@DataJpaTest +class ExerciseRepositoryTest { + + @Autowired + ExerciseRepository repository; + + @Autowired + TestEntityManager entityManager; + + + Exercise exercise = new Exercise("name", "desc", 2, 1L, new HashSet<>()); + + Question question = new Question("text", new HashSet<>(), exercise); + + @Test + void saveExercise() { + Exercise saved = repository.save(exercise); + + Assertions.assertNotNull(saved); + Assertions.assertEquals(exercise, saved); + } + + @Test + void findById() { + entityManager.persist(exercise); + entityManager.flush(); + + Exercise found = repository.findById(exercise.getId()).orElse(null); + + Assertions.assertNotNull(found); + Assertions.assertEquals(found, exercise); + } + + + @Test + void filterPerDiffPerCourse() { + entityManager.persist(exercise); + entityManager.flush(); + + Page<Exercise> found = repository.filterPerDifficultyPerCourse(PageRequest.of(0, 10), 1L, 2); + + Assertions.assertEquals(1, found.getTotalElements()); + Assertions.assertEquals(found.getContent().get(0), exercise); + } + + @Test + void testFindAllExercises() { + Exercise exercise1 = new Exercise(); + + entityManager.persist(exercise); + entityManager.persist(exercise1); + + Page<Exercise> coursePage = repository.findAll(PageRequest.of(0, 42)); + + Assertions.assertEquals(2, coursePage.getTotalElements()); + Assertions.assertEquals(coursePage.getContent(), Arrays.asList(exercise, exercise1)); + } + + @Test + void getQuestionsEmptyQuestions() { + entityManager.persist(exercise); + + var result = repository.getQuestions(PageRequest.of(0, 10), 1L); + + Assertions.assertEquals(0, result.getTotalElements()); + } + + @Test + void getQuestions() { + exercise.setQuestions(Set.of(question)); + entityManager.persist(exercise); + + var result = repository.getQuestions(PageRequest.of(0, 10), 1L); + + Assertions.assertEquals(1, result.getTotalElements()); + Assertions.assertEquals(result.getContent().get(0), question); + } + + @Test + void testDeleteExercise() { + Long id = entityManager.persist(exercise).getId(); + entityManager.flush(); + + repository.deleteById(id); + + Assertions.assertTrue(repository.findById(id).isEmpty()); + } + + +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseServiceTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..4624d55445b4a0c78cd64d51796a1b61d02ae23f --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/exercise/ExerciseServiceTest.java @@ -0,0 +1,115 @@ +package org.fuseri.moduleexercise.exercise; + +import jakarta.persistence.EntityNotFoundException; +import org.fuseri.model.dto.exercise.ExerciseCreateDto; +//import org.fuseri.modulelanguageschool.common.ResourceNotFoundException; + +import org.fuseri.model.dto.exercise.ExerciseDto; +import org.fuseri.moduleexercise.question.Question; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +@SpringBootTest +public class ExerciseServiceTest { + + @MockBean + ExerciseRepository repository; + + @Autowired + ExerciseService service; + + Exercise exercise; + ExerciseCreateDto exercise2; + ExerciseCreateDto exercise3; + + @BeforeEach + void setup() { + exercise = new Exercise("idioms", "exercise on basic idioms",2,1L,new HashSet<Question>()); + exercise2 = new ExerciseCreateDto("idioms1", "exercise on intermediate idioms", 2, 0); + exercise3 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L); + } + + + @Test + void create() { + when(repository.save(exercise)).thenReturn(exercise); + Exercise result = service.create(exercise); + Assertions.assertEquals(exercise, result); + verify(repository).save(exercise); + } + + @Test + void notFound() { + when(repository.findById(anyLong())).thenReturn(Optional.empty()); + + Assertions.assertThrows(EntityNotFoundException.class, () -> service.find(anyLong())); + } + + @Test + void find() { + when(repository.findById(anyLong())).thenReturn(Optional.of(exercise)); + + Exercise result = service.find(anyLong()); + + Assertions.assertEquals(exercise, result); + verify(repository).findById(anyLong()); + } + + @Test + void findAll() { + Pageable pageable = PageRequest.of(0, 10); + Page<Exercise> page = new PageImpl<>(Collections.emptyList(), pageable, 0); + + when(repository.findAll(pageable)).thenReturn(page); + Page<Exercise> result = service.findAll(0); + + Assertions.assertEquals(page, result); + verify(repository).findAll(pageable); + } + + @Test + void findPerDiffPerCourse() { + PageRequest pageable = PageRequest.of(0, 10); + Page<Exercise> page = new PageImpl<>(Collections.emptyList(), pageable, 0); + + when(repository.filterPerDifficultyPerCourse(pageable,1L,2)).thenReturn(page); + + Page<Exercise> result = service.findPerDifficultyPerCourse(0,1L,2); + + Assertions.assertEquals(page, result); + verify(repository).filterPerDifficultyPerCourse(pageable,1L,2); + } + + + @Test + void getQuestions() { + PageRequest pageable = PageRequest.of(0, 10); + Page<Question> page = new PageImpl<>(Collections.emptyList(), pageable, 0); + + when(repository.getQuestions(pageable,1L)).thenReturn(page); + when(repository.existsById(1L)).thenReturn(true); + + + Page<Question> result = service.getQuestions(1L,0); + + Assertions.assertEquals(page, result); + verify(repository).getQuestions(pageable,1L); + } +} 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 index 92481d501ab4189e69bc0659fad3b6eed609d353..30a025a3eb1f3f64b1162539f1b55653301dfc5a 100644 --- 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 @@ -5,20 +5,20 @@ import com.fasterxml.jackson.databind.ObjectMapper; import org.fuseri.model.dto.common.Result; import org.fuseri.model.dto.exercise.ExerciseCreateDto; import org.fuseri.model.dto.exercise.ExerciseDto; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; 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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; - -import java.util.Map; - +import static org.hamcrest.Matchers.is; +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; 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; @@ -38,59 +38,124 @@ public class ExerciseTest { } } + private ExerciseCreateDto exercise1; + private ExerciseCreateDto exercise2; + private ExerciseCreateDto exercise3; - @Test - void getExercise() { - var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L); - - long id = 0L; + @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); 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(); + 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)); } 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 + void getExercise() { + long id = 1L; 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()))); + } catch (Exception e) { + //do absolutely nothing + } + } + @Test + void deleteExercise() { + long id = 1L; + 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 - void getFiltered() { + void deleteExercise_notFound() { + long id = 999999L; + try { + var theId = String.format("/exercises/%s", id); + var smth = mockMvc.perform(delete(theId)); + smth.andExpect(status().isNoContent()); + } catch (Exception e) { + //do absolutely nothing + } + } - var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 0, 0L); - var postExercise1 = new ExerciseCreateDto("idioms1", "exercise on basic idioms", 0, 0L); - var postExercise2 = new ExerciseCreateDto("idioms2", "exercise on basic idioms", 1, 0L); + @Test + void getExercise_notFound() { + long id = 999999L; + try { + var theId = String.format("/exercises/%s", id); + var smth = mockMvc.perform(get(theId)); + smth.andExpect(status().isNotFound()); + } catch (Exception e) { + //do absolutely nothing + } + } + + @Test + void FindAll() { try { - var exercise1 = mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON)); + 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 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 + throw new RuntimeException(e); } - Map<String, String> params; + } - try { - var filtered = mockMvc.perform(get("/exercises/filter").param("page", "0").param("courseId", "0").param("difficulty", "0")); + @Test + void getFiltered() { + 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>>() { }); @@ -102,11 +167,7 @@ public class ExerciseTest { // @Test // void getByExercise() throws Exception { -// -// var exerciseId = createExercise(); -// var question = createQuestion(exerciseId); -// -// var theId = String.format("/questions/exercise/%s", exerciseId); +// var theId = String.format("/questions/exercise/%s", 9); // // var smth = mockMvc.perform(get(theId).param("page", "0")); // @@ -115,24 +176,94 @@ public class ExerciseTest { // var res = objectMapper.readValue(content, new TypeReference<Result<QuestionDto>>() { // }); // -// Map<String, String> params; // -// assert (res.getItems().get(0).equals(question)); +// +//// assert (res.getItems().get(0).equals(question)); // } @Test void testCreateExercise() throws Exception { - var expectedResponse = new ExerciseDto(); var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L); - mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isCreated()).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(); + mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()) + .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(); } + @Test + void testCreateExerciseEmptyBody() throws Exception { + var postExercise = ""; + + mockMvc.perform(post("/exercises").content((postExercise)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()).andReturn().getResponse().getContentAsString(); + } + + + @Test + void testCreateExerciseMissingDesc() throws Exception { + var postExercise = """ + { + "name": "idioms", + "difficulty": 2, + "courseId": 0, + } + """; + + mockMvc.perform(post("/exercises").content((postExercise)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()).andReturn().getResponse().getContentAsString(); + } + + @Test + void testCreateExerciseMissingName() throws Exception { + var postExercise = """ + { + "description: "exercise on basic idioms", + "difficulty": 2, + "courseId": 0, + } + """; + + mockMvc.perform(post("/exercises").content((postExercise)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()).andReturn().getResponse().getContentAsString(); + } + + @Test + void testCreateExerciseMissingDifficulty() throws Exception { + var postExercise = """ + { + "name": "idioms" + "description: "exercise on basic idioms", + "courseId": 0 + } + """; + + mockMvc.perform(post("/exercises").content((postExercise)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()).andReturn().getResponse().getContentAsString(); + } + + @Test + void testCreateExerciseMissingCourseId() throws Exception { + var postExercise = """ + { + "name": "idioms" + "description: "exercise on basic idioms", + "difficulty": 0 + } + """; + + mockMvc.perform(post("/exercises").content((postExercise)).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()).andReturn().getResponse().getContentAsString(); + } + + @Test void testUpdate() { var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L); - long id = 0L; + long id = 1L; try { var dis = mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON)); @@ -178,4 +309,45 @@ public class ExerciseTest { } + @Test + void testUpdateNotFound() { + long id = 999999L; + var content = """ + { + "name": "idioms", + "description": "exercise on basic idioms", + "difficulty": 2, + "courseId": 0 + } + """; + try { + var theId = String.format("/exercises/%d", id); + mockMvc.perform(put(theId).content(content).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + void testUpdateIncorrectBody() { + long id = 1L; + var content = """ + { + "description": "exercise on basic idioms", + "difficulty": 2, + "courseId": 0 + } + """; + try { + var theId = String.format("/exercises/%d", id); + mockMvc.perform(put(theId).content(content).contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionFacadeTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionFacadeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..bf9cb27ea4664c08ecbf53af067e788f1292da25 --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionFacadeTest.java @@ -0,0 +1,112 @@ +package org.fuseri.moduleexercise.question; + + +import org.fuseri.model.dto.exercise.QuestionCreateDto; +import org.fuseri.model.dto.exercise.QuestionDto; +import org.fuseri.model.dto.exercise.QuestionUpdateDto; +import org.fuseri.moduleexercise.answer.AnswerService; +import org.fuseri.moduleexercise.exercise.Exercise; +import org.fuseri.moduleexercise.exercise.ExerciseService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +public class QuestionFacadeTest { + private final QuestionDto questionDto = new QuestionDto(); + + private final QuestionCreateDto questionCreateDto = new QuestionCreateDto("name",1L,new ArrayList<>()); + + private final QuestionUpdateDto questionUpdateDto = QuestionUpdateDto.builder().build(); + + private final Exercise exercise = new Exercise(); + private final Question question = new Question("text",new HashSet<>(),exercise); + + @MockBean + QuestionRepository repository; + @Autowired + QuestionFacade facade; + + @MockBean + QuestionService service; + + @MockBean + AnswerService answerService; + + @MockBean + QuestionMapper mapper; + + + @MockBean + ExerciseService exerciseService; +// +// @MockBean +// QuestionMapper mapper; + + @Test + void create() { + long id = 1; + when(exerciseService.find(id)).thenReturn(exercise); + when(mapper.fromCreateDto(questionCreateDto)).thenReturn(question); + when(service.create(question)).thenReturn(question); + when(mapper.toDto(question)).thenReturn(questionDto); + + QuestionDto actualDto = facade.create(questionCreateDto); + + assertEquals(questionDto, actualDto); + + } + + @Test + void testFindById() { + Long id = 0L; + + when(service.find(id)).thenReturn(question); + when(mapper.toDto(question)).thenReturn(questionDto); + + QuestionDto actualDto = facade.find(id); + + assertNotNull(actualDto); + assertEquals(questionDto, actualDto); + } + + + + + @Test + void update() { + Long id = 1L; + when(repository.existsById(id)).thenReturn(true); +// when(repository.find) + when(service.find(id)).thenReturn(question); + when(mapper.fromCreateDto(questionCreateDto)).thenReturn(question); + when(service.update(question)).thenReturn(question); + when(mapper.toDto(question)).thenReturn(questionDto); + when(mapper.fromUpdateDto(questionUpdateDto)).thenReturn(question); + when(answerService.findAllByQuestionId(id)).thenReturn(Collections.emptyList()); + QuestionDto actualDto = facade.update(id,questionUpdateDto); + + + assertEquals(questionDto, actualDto); + } + + @Test + void testDelete() { + Long id = 1L; + when(service.find(id)).thenReturn(question); + + facade.delete(id); + verify(service).delete(id); + } + +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionRepositoryTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionRepositoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3c0eb33e9b5b83677709c5d43c1f8c47d94db32d --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionRepositoryTest.java @@ -0,0 +1,88 @@ +package org.fuseri.moduleexercise.question; + +import org.fuseri.moduleexercise.exercise.Exercise; +import org.fuseri.moduleexercise.exercise.ExerciseRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; + +@DataJpaTest +public class QuestionRepositoryTest { + + @Autowired + QuestionRepository repository; + + @Autowired + ExerciseRepository exerciseRepository; + + @Autowired + TestEntityManager entityManager; + + + Exercise exercise = new Exercise("name","desc",2,1L,new HashSet<>()); + + Question question = new Question("text",new HashSet<>(),exercise); + Question question2 = new Question("text2",new HashSet<>(),exercise); + + @BeforeEach + void init() { + exerciseRepository.save(exercise); + } + + @Test + void saveQuestion() { + Question saved = repository.save(question); + + Assertions.assertNotNull(saved); + Assertions.assertEquals(question, saved); + } + + @Test + void findById() { + entityManager.persist(question); + entityManager.flush(); + + Question found = repository.findById(question.getId()).orElse(null); + + Assertions.assertNotNull(found); + Assertions.assertEquals(found, question); + } + + + @Test + void testFindAllQuestions() { + entityManager.persist(question); + entityManager.persist(question2); + + Page<Question> coursePage = repository.findAll(PageRequest.of(0, 42)); + + Assertions.assertEquals(2, coursePage.getTotalElements()); + Assertions.assertEquals(coursePage.getContent(), Arrays.asList(question, question2)); + } + @Test + void testFindAllQuestionsEmpty() { + Page<Question> coursePage = repository.findAll(PageRequest.of(0, 42)); + + Assertions.assertEquals(0, coursePage.getTotalElements()); + Assertions.assertEquals(coursePage.getContent(), new ArrayList<>()); + } + + @Test + void testDeleteQuestion() { + Long id = entityManager.persist(question).getId(); + entityManager.flush(); + + repository.deleteById(id); + + Assertions.assertTrue(repository.findById(id).isEmpty()); + } +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionServiceTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..e2085f855a1ee0af10166607a56f9b1563e11cb8 --- /dev/null +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionServiceTest.java @@ -0,0 +1,66 @@ +package org.fuseri.moduleexercise.question; + +import jakarta.persistence.EntityNotFoundException; +import org.fuseri.moduleexercise.exercise.Exercise; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import java.util.HashSet; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@SpringBootTest +public class QuestionServiceTest { + + + + @MockBean + QuestionRepository repository; + + @Autowired + QuestionService service; + + Exercise exercise; + Question question; + Question question1; + + @BeforeEach + void setup() { + exercise = new Exercise("idioms", "exercise on basic idioms",2,1L,new HashSet<Question>()); + question = new Question("text",new HashSet<>(),exercise); + question1 = new Question("text2",new HashSet<>(),exercise); + } + + + @Test + void create() { + when(repository.save(question)).thenReturn(question); + Question result = service.create(question); + Assertions.assertEquals(question, result); + verify(repository).save(question); + } + + @Test + void notFound() { + when(repository.findById(anyLong())).thenReturn(Optional.empty()); + + Assertions.assertThrows(EntityNotFoundException.class, () -> service.find(anyLong())); + } + + @Test + void find() { + when(repository.findById(anyLong())).thenReturn(Optional.of(question)); + + Question result = service.find(anyLong()); + + Assertions.assertEquals(question, result); + verify(repository).findById(anyLong()); + } + +} diff --git a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java index c248a76380962a53889db274582d0d499827ead1..2a97bb2184abca3ad12574fb584869115f359aaa 100644 --- a/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java +++ b/application/module-exercise/src/test/java/org/fuseri/moduleexercise/question/QuestionTest.java @@ -1,22 +1,28 @@ package org.fuseri.moduleexercise.question; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.fuseri.model.dto.exercise.AnswerDto; import org.fuseri.model.dto.exercise.AnswerInQuestionCreateDto; import org.fuseri.model.dto.exercise.ExerciseCreateDto; -import org.fuseri.model.dto.exercise.ExerciseDto; import org.fuseri.model.dto.exercise.QuestionCreateDto; import org.fuseri.model.dto.exercise.QuestionDto; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; 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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; + +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.List; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -38,109 +44,150 @@ public class QuestionTest { } } - @Test - void testCreateQuestion() throws Exception { - long id = createExercise(); - var answr = new AnswerDto("dis a logical paradox", true); - QuestionDto res = createQuestion(id); - var expected = new QuestionDto(); - expected.setAnswers(List.of(answr)); - expected.setExerciseId(id); - expected.setId(res.getId()); - expected.setText("this statement is false"); - -// assert expected.equals(res); + private void createExercise() { + var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L); + var postExercise2 = new ExerciseCreateDto("riddles", "simple english riddles", 2, 0L); + try { + mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON)); + mockMvc.perform(post("/exercises").content(asJsonString(postExercise2)).contentType(MediaType.APPLICATION_JSON)); + + } catch (Exception e) { + assert (false); + } } - private QuestionDto createQuestion(long id) throws Exception { + @BeforeEach + void init() { + createExercise(); + long id = 2; + var question = new QuestionCreateDto("this statement is false", id, List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true))); + var question2 = new QuestionCreateDto("What month of the year has 28 days?", id, List.of(new AnswerInQuestionCreateDto("February", false), new AnswerInQuestionCreateDto("All of them", true))); + ResultActions posted = null; + try { + mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON)); + mockMvc.perform(post("/questions").content(asJsonString(question2)).contentType(MediaType.APPLICATION_JSON)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @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 + void testCreateQuestion() throws Exception { + var exerciseId = 1L; + var question = new QuestionCreateDto("what is the meaning of: costs an arm and leg", exerciseId, List.of(new AnswerInQuestionCreateDto("dis very expencive", true), new AnswerInQuestionCreateDto("FMA refference", false))); var posted = mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON)); - - var cont = posted.andReturn().getResponse().getContentAsString(); - var res = objectMapper.readValue(cont, QuestionDto.class); - return res; + posted.andExpect(status().isCreated()) + .andExpect(jsonPath("$.text", is("what is the meaning of: costs an arm and leg"))) + .andExpect(jsonPath("$.exerciseId", is((int)exerciseId))) + .andExpect(jsonPath("$.answers[0].text", is("dis very expencive"))); } - private long createExercise() { - var postExercise = new ExerciseCreateDto("idioms", "exercise on basic idioms", 2, 0L); - - long id = 0L; - - try { - var dis = mockMvc.perform(post("/exercises").content(asJsonString(postExercise)).contentType(MediaType.APPLICATION_JSON)); - var ok = dis.andReturn().getResponse().getContentAsString(); + @Test + void testCreateQuestionEmptyQuestions() throws Exception { + var prompt = """ + { + "text": "this is a question without exercise Id", + "exerciseId": 1, + "answers": [] + } + """; - var ll = objectMapper.readValue(ok, ExerciseDto.class); + var posted = mockMvc.perform(post("/questions").content(prompt).contentType(MediaType.APPLICATION_JSON)); - id = ll.getId(); - } catch (Exception e) { - assert (false); - } - return id; + posted.andExpect(status().isCreated()); } @Test - void getQuestion() throws Exception { - + void testCreateQuestionEmptyField() throws Exception { + var exerciseId = 1L; + var question = new QuestionCreateDto("", exerciseId, List.of(new AnswerInQuestionCreateDto("dis very expencive", true), new AnswerInQuestionCreateDto("FMA refference", false))); + var posted = mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON)); - long exerciseId = createExercise(); - var question = createQuestion(exerciseId); + posted.andExpect(status().is4xxClientError()); + } - var theId = String.format("/questions/%s", question.getId()); + @Test + void testCreateQuestionMissingField() throws Exception { + var prompt = """ + { + "text": "this is a question without exercise Id, + "answers" : [] + } + """; + var posted = mockMvc.perform(post("/questions").content(prompt).contentType(MediaType.APPLICATION_JSON)); - var gets = mockMvc.perform(get(theId)); + posted.andExpect(status().is4xxClientError()); + } - var content = gets.andReturn().getResponse().getContentAsString(); - var res = objectMapper.readValue(content, QuestionDto.class); + private QuestionDto createQuestion(long id) throws Exception { + var question = new QuestionCreateDto("this statement is false", id, List.of(new AnswerInQuestionCreateDto("dis a logical paradox", true))); + var posted = mockMvc.perform(post("/questions").content(asJsonString(question)).contentType(MediaType.APPLICATION_JSON)); + var cont = posted.andReturn().getResponse().getContentAsString(); + var res = objectMapper.readValue(cont, QuestionDto.class); + return res; + } - assert res.equals(question); + @Test + void getQuestion() throws Exception { + var gets = mockMvc.perform(get("/questions/1")); + gets.andExpect(status().isOk()).andExpect(jsonPath("$.text", is("this statement is false"))); } @Test - void getAnswer() throws Exception { - var exerciseId = createExercise(); - var question = createQuestion(exerciseId); - - var gets = mockMvc.perform(get(String.format("/questions/%s/answers", question.getId()))); + void getQuestionNotFound() throws Exception { + var gets = mockMvc.perform(get("/questions/9999")); + gets.andExpect(status().isNotFound()); + } - var content2 = gets.andReturn().getResponse().getContentAsString(); - var res = objectMapper.readValue(content2, new TypeReference<List<AnswerDto>>() { - }); + @Test + void getAnswer() throws Exception { + var gets = mockMvc.perform(get("/questions/2/answers")); + gets.andExpect(status().isOk()) + .andExpect(jsonPath("$[0].text", is("February"))) + .andExpect(jsonPath("$[1].text", is("All of them"))); - assert (res.equals(question.getAnswers())); } @Test void TestUpdate() throws Exception { - long id = createExercise(); - var question = createQuestion(id); - var updated = """ { "text": "wat a paradox?", - "exerciseId": "%s" + "exerciseId": "1" } """; - updated = String.format(updated, id); -// -// var smth = mockMvc.perform(put(String.format("/questions/%s", question.getId())).content(updated).contentType(MediaType.APPLICATION_JSON)); -// -// var content = smth.andReturn().getResponse().getContentAsString(); - -// var res = objectMapper.readValue(content, QuestionDto.class); + var gets = mockMvc.perform(put("/questions/1").content(updated).contentType(MediaType.APPLICATION_JSON)); -// question.setText("wat a paradox?"); - -// assert (question.equals(res)); + gets.andExpect(status().isOk()) + .andExpect(jsonPath("$.text", is("wat a paradox?"))); + } +@Test + void deleteExisting() { + try { + mockMvc.perform(delete("/questions/1")).andExpect(status().isNoContent()); + } catch (Exception e) { + throw new RuntimeException(e); } } + +}