diff --git a/openbas-api/src/test/java/io/openbas/injects/InjectCrudTest.java b/openbas-api/src/test/java/io/openbas/injects/InjectCrudTest.java index d87b348cd2..a79ed151ac 100644 --- a/openbas-api/src/test/java/io/openbas/injects/InjectCrudTest.java +++ b/openbas-api/src/test/java/io/openbas/injects/InjectCrudTest.java @@ -1,5 +1,11 @@ package io.openbas.injects; +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; + import io.openbas.IntegrationTest; import io.openbas.database.model.Exercise; import io.openbas.database.model.Inject; @@ -15,24 +21,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; -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; - @TestInstance(PER_CLASS) class InjectCrudTest extends IntegrationTest { - @Autowired - private InjectorContractRepository injectorContractRepository; - @Autowired - private InjectRepository injectRepository; + @Autowired private InjectorContractRepository injectorContractRepository; + @Autowired private InjectRepository injectRepository; - @Autowired - private ExerciseService exerciseService; - @Autowired - private ExerciseRepository exerciseRepository; + @Autowired private ExerciseService exerciseService; + @Autowired private ExerciseRepository exerciseRepository; private static String EXERCISE_ID; private static String INJECT_ID; @@ -49,13 +45,25 @@ void afterAll() { void createInjectSuccess() { // -- PREPARE -- Exercise exercise = this.exerciseService.createExercise(getExercise()); + assertNotNull(exercise.getId(), "Exercise ID should not be null after creation."); EXERCISE_ID = exercise.getId(); - InjectorContract contract = injectorContractRepository.findById(EMAIL_DEFAULT).orElseThrow(); + + InjectorContract contract = + injectorContractRepository + .findById(EMAIL_DEFAULT) + .orElseThrow(() -> new IllegalStateException("Injector contract not found.")); Inject inject = createDefaultInjectEmail(contract); + inject.setExercise(exercise); // -- EXECUTE -- Inject injectCreated = this.injectRepository.save(inject); + assertNotNull(injectCreated.getId(), "Inject ID should not be null after saving."); INJECT_ID = injectCreated.getId(); - assertNotNull(injectCreated); + + // -- VALIDATE -- + assertNotNull(injectCreated.getExercise(), "Inject should reference a valid Exercise."); + assertNotNull( + injectRepository.findById(injectCreated.getId()), + "Inject should be persisted in the database."); } } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java index 56d6866416..d2d2cd8003 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/InjectFixture.java @@ -27,8 +27,7 @@ public static Inject createDefaultInjectEmail(InjectorContract injectorContract) } public static Inject createDefaultInjectChallenge( - InjectorContract injectorContract, ObjectMapper objectMapper, List challengeIds - ) { + InjectorContract injectorContract, ObjectMapper objectMapper, List challengeIds) { Inject inject = createInject(injectorContract, INJECT_CHALLENGE_NAME); ChallengeContent content = new ChallengeContent(); @@ -38,8 +37,7 @@ public static Inject createDefaultInjectChallenge( } public static Inject createInjectCommandPayload( - InjectorContract injectorContract, Map payloadArguments - ) { + InjectorContract injectorContract, Map payloadArguments) { Inject inject = createInject(injectorContract, "Inject title"); ObjectMapper objectMapper = new ObjectMapper();