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 c09fe52 commit 0b3fbb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
40 changes: 24 additions & 16 deletions openbas-api/src/test/java/io/openbas/injects/InjectCrudTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public static Inject createDefaultInjectEmail(InjectorContract injectorContract)
}

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 @@ -38,8 +37,7 @@ 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 0b3fbb1

Please sign in to comment.