From 455695ed1286d3e6479826f726c93286376b067f Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Tue, 15 Oct 2024 16:55:11 +0200 Subject: [PATCH 1/2] [Test]Simulation lesson learned survey sending --- .../openbas/rest/ExerciseLessonsApiTest.java | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java diff --git a/openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java b/openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java new file mode 100644 index 0000000000..5177bbc9a9 --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java @@ -0,0 +1,133 @@ +package io.openbas.rest; + +import io.openbas.IntegrationTest; +import io.openbas.database.model.*; +import io.openbas.database.repository.ExerciseRepository; +import io.openbas.database.repository.LessonsCategoryRepository; +import io.openbas.database.repository.TeamRepository; +import io.openbas.database.repository.UserRepository; +import io.openbas.rest.exercise.ExerciseService; +import io.openbas.rest.lessons.form.LessonsSendInput; +import io.openbas.service.MailingService; +import io.openbas.utils.mockUser.WithMockPlannerUser; +import org.junit.jupiter.api.*; +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.SpyBean; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.Instant; +import java.util.List; + +import static io.openbas.database.model.ExerciseStatus.SCHEDULED; +import static io.openbas.rest.exercise.ExerciseApi.EXERCISE_URI; +import static io.openbas.utils.JsonUtils.asJsonString; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.mockito.Mockito.*; + + +@SpringBootTest +@AutoConfigureMockMvc +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class ExerciseLessonsApiTest extends IntegrationTest { + + static Exercise EXERCISE; + static LessonsCategory LESSONCATEGORY; + static Team TEAM; + static User USER; + + @Autowired + private MockMvc mvc; + @Autowired + private ExerciseService exerciseService; + @Autowired + private ExerciseRepository exerciseRepository; + @Autowired + private LessonsCategoryRepository lessonsCategoryRepository; + @SpyBean + private MailingService mailingService; + @Autowired + private TeamRepository teamRepository; + @Autowired + private UserRepository userRepository; + + @BeforeAll + void beforeAll() { + EXERCISE = getExercise(); + LESSONCATEGORY = getLessonCategory(); + } + + @AfterAll + void afterAll() { + this.exerciseRepository.delete(EXERCISE); + this.lessonsCategoryRepository.delete(LESSONCATEGORY); + this.teamRepository.delete(TEAM); + this.userRepository.delete(USER); + } + + + private LessonsCategory getLessonCategory() { + LessonsCategory lessonsCategory = new LessonsCategory(); + lessonsCategory.setExercise(EXERCISE); + lessonsCategory.setName("Category"); + lessonsCategory.setDescription("Description"); + lessonsCategory.setOrder(0); + lessonsCategory.setTeams(List.of(getTeam())); + return this.lessonsCategoryRepository.save(lessonsCategory); + } + + private Team getTeam() { + Team team = new Team(); + team.setName("My team"); + + User user = new User(); + user.setEmail("testSurvey@gmail.com"); + USER = this.userRepository.save(user); + + team.setUsers(List.of(USER)); + TEAM = this.teamRepository.save(team); + return TEAM; + } + + private Exercise getExercise() { + Exercise exercise = new Exercise(); + exercise.setName("Exercice name"); + exercise.setStatus(SCHEDULED); + exercise.setFrom("test@test.com"); + exercise.setReplyTos(List.of("test@test.com")); + exercise.setStart(Instant.now()); + return this.exerciseService.createExercise(exercise); + } + + @DisplayName("Send surveys for exercise lessons") + @Test + @Order(1) + @WithMockPlannerUser + void sendExerciseLessonsTest() throws Exception { + + // -- PREPARE -- + String lessonSubject = "Subject"; + String lessonBody = "This is a lesson"; + LessonsSendInput lessonsSendInput = new LessonsSendInput(); + lessonsSendInput.setSubject(lessonSubject); + lessonsSendInput.setBody(lessonBody); + User user = userRepository.findById(LESSONCATEGORY.getUsers().getFirst()).orElseThrow(); + + // -- EXECUTE -- + mvc.perform(post(EXERCISE_URI + "/" + EXERCISE.getId() + "/lessons_send") + .content(asJsonString(lessonsSendInput)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()); + + // -- ASSERT -- + verify(mailingService).sendEmail(lessonSubject, lessonBody, List.of(user), + exerciseRepository.findById(EXERCISE.getId())); + } + + +} From 3d84ca0dc36bb6804ab56695d9370775dce1f0e5 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Thu, 17 Oct 2024 10:02:36 +0200 Subject: [PATCH 2/2] Resolve PR comments --- .../openbas/rest/ExerciseLessonsApiTest.java | 43 ++++--------------- .../ExerciseLessonsCategoryFixture.java | 25 +++++++++++ 2 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseLessonsCategoryFixture.java diff --git a/openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java b/openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java index 5177bbc9a9..c7700fa257 100644 --- a/openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java @@ -18,12 +18,14 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -import java.time.Instant; import java.util.List; -import static io.openbas.database.model.ExerciseStatus.SCHEDULED; import static io.openbas.rest.exercise.ExerciseApi.EXERCISE_URI; import static io.openbas.utils.JsonUtils.asJsonString; +import static io.openbas.utils.fixtures.ExerciseFixture.getExercise; +import static io.openbas.utils.fixtures.ExerciseLessonsCategoryFixture.getLessonsCategory; +import static io.openbas.utils.fixtures.TeamFixture.getTeam; +import static io.openbas.utils.fixtures.UserFixture.getUser; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.mockito.Mockito.*; @@ -31,7 +33,6 @@ @SpringBootTest @AutoConfigureMockMvc -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class ExerciseLessonsApiTest extends IntegrationTest { @@ -57,7 +58,6 @@ public class ExerciseLessonsApiTest extends IntegrationTest { @BeforeAll void beforeAll() { - EXERCISE = getExercise(); LESSONCATEGORY = getLessonCategory(); } @@ -71,41 +71,14 @@ void afterAll() { private LessonsCategory getLessonCategory() { - LessonsCategory lessonsCategory = new LessonsCategory(); - lessonsCategory.setExercise(EXERCISE); - lessonsCategory.setName("Category"); - lessonsCategory.setDescription("Description"); - lessonsCategory.setOrder(0); - lessonsCategory.setTeams(List.of(getTeam())); - return this.lessonsCategoryRepository.save(lessonsCategory); - } - - private Team getTeam() { - Team team = new Team(); - team.setName("My team"); - - User user = new User(); - user.setEmail("testSurvey@gmail.com"); - USER = this.userRepository.save(user); - - team.setUsers(List.of(USER)); - TEAM = this.teamRepository.save(team); - return TEAM; - } - - private Exercise getExercise() { - Exercise exercise = new Exercise(); - exercise.setName("Exercice name"); - exercise.setStatus(SCHEDULED); - exercise.setFrom("test@test.com"); - exercise.setReplyTos(List.of("test@test.com")); - exercise.setStart(Instant.now()); - return this.exerciseService.createExercise(exercise); + USER = this.userRepository.save(getUser()); + TEAM = teamRepository.save(getTeam(USER, "My team", false)); + EXERCISE = this.exerciseService.createExercise(getExercise(List.of(TEAM))); + return this.lessonsCategoryRepository.save(getLessonsCategory(EXERCISE, List.of(TEAM))); } @DisplayName("Send surveys for exercise lessons") @Test - @Order(1) @WithMockPlannerUser void sendExerciseLessonsTest() throws Exception { diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseLessonsCategoryFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseLessonsCategoryFixture.java new file mode 100644 index 0000000000..7c3855ba78 --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/ExerciseLessonsCategoryFixture.java @@ -0,0 +1,25 @@ +package io.openbas.utils.fixtures; + +import io.openbas.database.model.Exercise; +import io.openbas.database.model.LessonsCategory; +import io.openbas.database.model.Team; + +import java.util.List; + +public class ExerciseLessonsCategoryFixture { + + public static final String LESSON_CATEGORY_NAME = "Category name"; + public static final String LESSON_CATEGORY_DESCRIPTION = "Category description"; + public static final int LESSON_CATEGORY_ORDER = 0; + + public static LessonsCategory getLessonsCategory(Exercise exercise, List categoryTeams) { + LessonsCategory lessonsCategory = new LessonsCategory(); + lessonsCategory.setExercise(exercise); + lessonsCategory.setName(LESSON_CATEGORY_NAME); + lessonsCategory.setDescription(LESSON_CATEGORY_DESCRIPTION); + lessonsCategory.setOrder(LESSON_CATEGORY_ORDER); + lessonsCategory.setTeams(categoryTeams); + return lessonsCategory; + } + +}