Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test]Simulation lesson learned survey sending #1644

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions openbas-api/src/test/java/io/openbas/rest/ExerciseLessonsApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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.util.List;

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.*;


@SpringBootTest
@AutoConfigureMockMvc
@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() {
LESSONCATEGORY = getLessonCategory();
}

@AfterAll
void afterAll() {
this.exerciseRepository.delete(EXERCISE);
this.lessonsCategoryRepository.delete(LESSONCATEGORY);
this.teamRepository.delete(TEAM);
this.userRepository.delete(USER);
}


private LessonsCategory getLessonCategory() {
johanah29 marked this conversation as resolved.
Show resolved Hide resolved
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
@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()));
}


}
Original file line number Diff line number Diff line change
@@ -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<Team> 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;
}

}