Skip to content

Commit

Permalink
[backend] Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Dec 19, 2024
1 parent da4a14d commit c09fe52
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 50 deletions.
61 changes: 36 additions & 25 deletions openbas-api/src/test/java/io/openbas/injects/InjectCrudTest.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
package io.openbas.injects;

import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.openbas.IntegrationTest;
import io.openbas.database.model.Exercise;
import io.openbas.database.model.Inject;
import io.openbas.database.model.InjectorContract;
import io.openbas.database.repository.ExerciseRepository;
import io.openbas.database.repository.InjectRepository;
import io.openbas.database.repository.InjectorContractRepository;
import java.util.List;
import io.openbas.rest.exercise.service.ExerciseService;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
class InjectCrudTest {
import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.utils.fixtures.ExerciseFixture.getExercise;
import static io.openbas.utils.fixtures.InjectFixture.createDefaultInjectEmail;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;

@Autowired private InjectRepository injectRepository;
@TestInstance(PER_CLASS)
class InjectCrudTest extends IntegrationTest {

@Autowired private ExerciseRepository exerciseRepository;
@Autowired
private InjectorContractRepository injectorContractRepository;
@Autowired
private InjectRepository injectRepository;

@Autowired private InjectorContractRepository injectorContractRepository;
@Autowired
private ExerciseService exerciseService;
@Autowired
private ExerciseRepository exerciseRepository;

private static String EXERCISE_ID;
private static String INJECT_ID;

@AfterAll
void afterAll() {
this.exerciseRepository.deleteById(EXERCISE_ID);
this.injectRepository.deleteById(INJECT_ID);
}

@DisplayName("Test inject creation with non null depends duration")
@Test
@Transactional
void createInjectSuccess() {
// -- PREPARE --
Exercise exercise = new Exercise();
exercise.setName("Exercise name");
exercise.setFrom("[email protected]");
exercise.setReplyTos(List.of("[email protected]"));
Exercise exerciseCreated = this.exerciseRepository.save(exercise);
Inject inject = new Inject();
inject.setTitle("test");
inject.setInjectorContract(
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setExercise(exerciseCreated);
inject.setDependsDuration(0L);
Exercise exercise = this.exerciseService.createExercise(getExercise());
EXERCISE_ID = exercise.getId();
InjectorContract contract = injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject inject = createDefaultInjectEmail(contract);

// -- EXECUTE --
Inject injectCreated = this.injectRepository.save(inject);
INJECT_ID = injectCreated.getId();
assertNotNull(injectCreated);

// -- CLEAN --
this.exerciseRepository.delete(exercise);
this.injectRepository.delete(inject);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public class AtomicTestingApiTest extends IntegrationTest {
@BeforeAll
void beforeAll() {
INJECTOR_CONTRACT = injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject injectWithoutPayload = InjectFixture.getInjectForEmailContract(INJECTOR_CONTRACT);
Inject injectWithoutPayload = InjectFixture.createDefaultInjectEmail(INJECTOR_CONTRACT);
INJECT_WITHOUT_STATUS = injectRepository.save(injectWithoutPayload);

Inject injectWithPayload = InjectFixture.getInjectForEmailContract(INJECTOR_CONTRACT);
Inject injectWithPayload = InjectFixture.createDefaultInjectEmail(INJECTOR_CONTRACT);
INJECT_WITH_STATUS_AND_COMMAND_LINES = injectRepository.save(injectWithPayload);
InjectStatus injectStatus = InjectStatusFixture.createDefaultInjectStatus();
injectStatus.setInject(injectWithPayload);
Expand Down
8 changes: 4 additions & 4 deletions openbas-api/src/test/java/io/openbas/rest/InjectApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static io.openbas.rest.inject.InjectApi.INJECT_URI;
import static io.openbas.rest.scenario.ScenarioApi.SCENARIO_URI;
import static io.openbas.utils.JsonUtils.asJsonString;
import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract;
import static io.openbas.utils.fixtures.InjectFixture.createDefaultInjectEmail;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -389,7 +389,7 @@ void executeEmailInjectForExerciseTest() throws Exception {
// -- PREPARE --
InjectorContract injectorContract =
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject inject = getInjectForEmailContract(injectorContract);
Inject inject = createDefaultInjectEmail(injectorContract);
User user = userRepository.findById(currentUser().getId()).orElseThrow();
DirectInjectInput input = new DirectInjectInput();
input.setTitle(inject.getTitle());
Expand Down Expand Up @@ -450,7 +450,7 @@ void executeEmailInjectForExerciseWithNoTeam() throws Exception {
// -- PREPARE --
InjectorContract injectorContract =
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject inject = getInjectForEmailContract(injectorContract);
Inject inject = createDefaultInjectEmail(injectorContract);

DirectInjectInput input = new DirectInjectInput();
input.setTitle(inject.getTitle());
Expand Down Expand Up @@ -489,7 +489,7 @@ void executeEmailInjectForExerciseWithNoContentTest() throws Exception {
// -- PREPARE --
InjectorContract injectorContract =
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject inject = getInjectForEmailContract(injectorContract);
Inject inject = createDefaultInjectEmail(injectorContract);

DirectInjectInput input = new DirectInjectInput();
input.setTitle(inject.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.rest.scenario.ScenarioApi.SCENARIO_URI;
import static io.openbas.utils.JsonUtils.asJsonString;
import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract;
import static io.openbas.utils.fixtures.InjectFixture.createDefaultInjectEmail;
import static io.openbas.utils.fixtures.ScenarioFixture.createDefaultCrisisScenario;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand Down Expand Up @@ -52,14 +52,14 @@ void beforeAll() {
Scenario scenarioSaved = this.scenarioRepository.save(scenario);
SCENARIO_ID = scenarioSaved.getId();

Inject injectDefaultEmail = getInjectForEmailContract(injectorContract);
Inject injectDefaultEmail = createDefaultInjectEmail(injectorContract);
injectDefaultEmail.setScenario(scenarioSaved);
injectDefaultEmail.setTitle("Inject default email");
injectDefaultEmail.setDependsDuration(1L);
Inject injectDefaultEmailSaved = this.injectRepository.save(injectDefaultEmail);
INJECT_IDS.add(injectDefaultEmailSaved.getId());

Inject injectDefaultGlobal = getInjectForEmailContract(injectorContract);
Inject injectDefaultGlobal = createDefaultInjectEmail(injectorContract);
injectDefaultGlobal.setScenario(scenarioSaved);
injectDefaultGlobal.setTitle("Inject global email");
Inject injectDefaultGlobalSaved = this.injectRepository.save(injectDefaultGlobal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.rest.exercise.ExerciseApi.EXERCISE_URI;
import static io.openbas.utils.JsonUtils.asJsonString;
import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract;
import static io.openbas.utils.fixtures.InjectFixture.createDefaultInjectEmail;
import static java.time.Instant.now;
import static java.time.temporal.ChronoUnit.MINUTES;
import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -92,26 +92,26 @@ void beforeAll() {

InjectorContract injectorContract =
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject inject1 = getInjectForEmailContract(injectorContract);
Inject inject1 = createDefaultInjectEmail(injectorContract);
EmailContent content = new EmailContent();
content.setSubject("Subject email");
content.setBody("A body");
inject1.setContent(this.mapper.valueToTree(content));
inject1.setExercise(scheduledExercise);

Inject inject2 = getInjectForEmailContract(injectorContract);
Inject inject2 = createDefaultInjectEmail(injectorContract);
inject2.setContent(this.mapper.valueToTree(content));
inject2.setExercise(runningExercise);

Inject inject3 = getInjectForEmailContract(injectorContract);
Inject inject3 = createDefaultInjectEmail(injectorContract);
inject3.setContent(this.mapper.valueToTree(content));
inject3.setExercise(pausedExercise);

Inject inject4 = getInjectForEmailContract(injectorContract);
Inject inject4 = createDefaultInjectEmail(injectorContract);
inject4.setContent(this.mapper.valueToTree(content));
inject4.setExercise(canceledExercise);

Inject inject5 = getInjectForEmailContract(injectorContract);
Inject inject5 = createDefaultInjectEmail(injectorContract);
inject5.setContent(this.mapper.valueToTree(content));
inject5.setExercise(finishedExercise);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static io.openbas.utils.fixtures.ArticleFixture.ARTICLE_NAME;
import static io.openbas.utils.fixtures.ArticleFixture.getArticle;
import static io.openbas.utils.fixtures.DocumentFixture.getDocumentJpeg;
import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract;
import static io.openbas.utils.fixtures.InjectFixture.createDefaultInjectEmail;
import static io.openbas.utils.fixtures.ObjectiveFixture.OBJECTIVE_NAME;
import static io.openbas.utils.fixtures.ObjectiveFixture.getObjective;
import static io.openbas.utils.fixtures.TagFixture.getTag;
Expand Down Expand Up @@ -211,7 +211,7 @@ void scenarioToExerciseTest() {

// Inject
Inject inject =
getInjectForEmailContract(
createDefaultInjectEmail(
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow());
inject.setTeams(
new ArrayList<>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static io.openbas.database.specification.TeamSpecification.fromExercise;
import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.utils.fixtures.ExerciseFixture.getExercise;
import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract;
import static io.openbas.utils.fixtures.InjectFixture.createDefaultInjectEmail;
import static io.openbas.utils.fixtures.TeamFixture.getTeam;
import static io.openbas.utils.fixtures.UserFixture.getUser;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -134,7 +134,7 @@ void testRemoveTeams() {

InjectorContract injectorContract =
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject injectDefaultEmail = getInjectForEmailContract(injectorContract);
Inject injectDefaultEmail = createDefaultInjectEmail(injectorContract);
injectDefaultEmail.setExercise(exerciseSaved);
injectDefaultEmail.setTeams(List.of(teamSaved));
Inject injectDefaultEmailSaved = this.injectRepository.saveAndFlush(injectDefaultEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static io.openbas.database.specification.TeamSpecification.fromScenario;
import static io.openbas.injectors.email.EmailContract.EMAIL_DEFAULT;
import static io.openbas.utils.fixtures.InjectFixture.getInjectForEmailContract;
import static io.openbas.utils.fixtures.InjectFixture.createDefaultInjectEmail;
import static io.openbas.utils.fixtures.TeamFixture.getTeam;
import static io.openbas.utils.fixtures.UserFixture.getUser;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -156,7 +156,7 @@ void testRemoveTeams() {

InjectorContract injectorContract =
this.injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow();
Inject injectDefaultEmail = getInjectForEmailContract(injectorContract);
Inject injectDefaultEmail = createDefaultInjectEmail(injectorContract);
injectDefaultEmail.setScenario(scenarioSaved);
injectDefaultEmail.setTeams(List.of(teamSaved));
Inject injectDefaultEmailSaved = this.injectRepository.saveAndFlush(injectDefaultEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ public class InjectFixture {

private static Inject createInject(InjectorContract injectorContract, String title) {
Inject inject = new Inject();
inject.setTitle(INJECT_EMAIL_NAME);
inject.setTitle(title);
inject.setInjectorContract(injectorContract);
inject.setEnabled(true);
inject.setDependsDuration(0L);
return inject;
}

public static Inject getInjectForEmailContract(InjectorContract injectorContract) {
public static Inject createDefaultInjectEmail(InjectorContract injectorContract) {
return createInject(injectorContract, INJECT_EMAIL_NAME);
}

public static Inject createDefaultInjectChallenge(
InjectorContract injectorContract, ObjectMapper objectMapper, List<String> challengeIds) {
InjectorContract injectorContract, ObjectMapper objectMapper, List<String> challengeIds
) {
Inject inject = createInject(injectorContract, INJECT_CHALLENGE_NAME);

ChallengeContent content = new ChallengeContent();
Expand All @@ -37,7 +38,8 @@ public static Inject createDefaultInjectChallenge(
}

public static Inject createInjectCommandPayload(
InjectorContract injectorContract, Map<String, String> payloadArguments) {
InjectorContract injectorContract, Map<String, String> payloadArguments
) {

Inject inject = createInject(injectorContract, "Inject title");
ObjectMapper objectMapper = new ObjectMapper();
Expand Down

0 comments on commit c09fe52

Please sign in to comment.