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 0b3fbb1 commit 4fbde0a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
18 changes: 2 additions & 16 deletions openbas-api/src/test/java/io/openbas/injects/InjectCrudTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
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 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;
Expand All @@ -28,25 +26,14 @@ class InjectCrudTest extends IntegrationTest {
@Autowired private InjectRepository injectRepository;

@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 = this.exerciseService.createExercise(getExercise());
assertNotNull(exercise.getId(), "Exercise ID should not be null after creation.");
EXERCISE_ID = exercise.getId();
assertNotNull(exercise.getId(), "Exercise should be successfully created");

InjectorContract contract =
injectorContractRepository
Expand All @@ -57,8 +44,7 @@ void createInjectSuccess() {

// -- EXECUTE --
Inject injectCreated = this.injectRepository.save(inject);
assertNotNull(injectCreated.getId(), "Inject ID should not be null after saving.");
INJECT_ID = injectCreated.getId();
assertNotNull(injectCreated.getId(), "Inject should be successfully created");

// -- VALIDATE --
assertNotNull(injectCreated.getExercise(), "Inject should reference a valid Exercise.");
Expand Down
30 changes: 7 additions & 23 deletions openbas-api/src/test/java/io/openbas/rest/ChallengeApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(PER_CLASS)
class ChallengeApiTest extends IntegrationTest {

Expand All @@ -41,50 +41,34 @@ class ChallengeApiTest extends IntegrationTest {
@Autowired private InjectorContractRepository injectorContractRepository;
@Resource private ObjectMapper objectMapper;

private static String SCENARIO_ID;
private static String CHALLENGE_ID;
private static String INJECT_ID;

@AfterAll
void afterAll() {
this.scenarioService.deleteScenario(SCENARIO_ID);
this.challengeRepository.deleteById(CHALLENGE_ID);
this.injectRepository.deleteById(INJECT_ID);
}

// -- SCENARIOS --

@DisplayName("Retrieve challenges for scenario")
@Test
@Order(1)
@Transactional
@WithMockObserverUser
void retrieveChallengesVariableForScenarioTest() throws Exception {
// -- PREPARE --
Scenario scenario = createDefaultCrisisScenario();
Scenario scenarioCreated = this.scenarioService.createScenario(scenario);
Scenario scenarioCreated = this.scenarioService.createScenario(createDefaultCrisisScenario());
assertNotNull(scenarioCreated, "Scenario should be successfully created");
SCENARIO_ID = scenarioCreated.getId();

Challenge challenge = createDefaultChallenge();
Challenge challengeCreated = this.challengeRepository.save(challenge);
Challenge challengeCreated = this.challengeRepository.save(createDefaultChallenge());
assertNotNull(challengeCreated, "Challenge should be successfully created");
CHALLENGE_ID = challengeCreated.getId();

Inject inject =
createDefaultInjectChallenge(
this.injectorContractRepository.findById(CHALLENGE_PUBLISH).orElseThrow(),
this.objectMapper,
List.of(CHALLENGE_ID));
List.of(challengeCreated.getId()));
inject.setScenario(scenarioCreated);
Inject injectCreated = this.injectRepository.save(inject);
assertNotNull(injectCreated, "Inject should be successfully created");
INJECT_ID = injectCreated.getId();

// -- EXECUTE --
String response =
this.mvc
.perform(
get(SCENARIO_URI + "/" + SCENARIO_ID + "/challenges")
get(SCENARIO_URI + "/" + scenarioCreated.getId() + "/challenges")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful())
.andReturn()
Expand All @@ -94,7 +78,7 @@ void retrieveChallengesVariableForScenarioTest() throws Exception {
// -- ASSERT --
assertNotNull(response, "Response should not be null");
assertEquals(
challenge.getName(),
challengeCreated.getName(),
JsonPath.read(response, "$[0].challenge_name"),
"Challenge name should match the expected value");
}
Expand Down
17 changes: 16 additions & 1 deletion openbas-api/src/test/java/io/openbas/rest/PayloadApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

@TestInstance(PER_CLASS)
class PayloadApiTest extends IntegrationTest {
Expand All @@ -52,11 +53,11 @@ void beforeAll() {
@AfterAll
void afterAll() {
this.documentRepository.deleteAll(List.of(EXECUTABLE_FILE));
this.payloadRepository.deleteAll();
}

@Test
@DisplayName("Create Payload")
@Transactional
@WithMockAdminUser
void createExecutablePayload() throws Exception {
PayloadCreateInput input = PayloadInputFixture.createDefaultPayloadCreateInputForExecutable();
Expand All @@ -77,6 +78,7 @@ void createExecutablePayload() throws Exception {

@Test
@DisplayName("Creating a Payload with a null as arch should fail")
@Transactional
@WithMockAdminUser
void createPayloadWithNullArch() throws Exception {
PayloadCreateInput input = PayloadInputFixture.createDefaultPayloadCreateInputForCommandLine();
Expand All @@ -89,6 +91,7 @@ void createPayloadWithNullArch() throws Exception {
@Test
@DisplayName(
"Creating an executable Payload with an arch different from x86_64 or arm64 should fail")
@Transactional
@WithMockAdminUser
void createExecutablePayloadWithoutArch() throws Exception {
PayloadCreateInput input = PayloadInputFixture.createDefaultPayloadCreateInputForExecutable();
Expand All @@ -107,6 +110,7 @@ void createExecutablePayloadWithoutArch() throws Exception {

@Test
@DisplayName("Update Executable Payload")
@Transactional
@WithMockAdminUser
void updateExecutablePayload() throws Exception {
PayloadCreateInput createInput =
Expand Down Expand Up @@ -147,6 +151,7 @@ void updateExecutablePayload() throws Exception {

@Test
@DisplayName("Updating an Executed Payload with null as arch should fail")
@Transactional
@WithMockAdminUser
void updateExecutablePayloadWithoutArch() throws Exception {
PayloadCreateInput createInput =
Expand Down Expand Up @@ -183,6 +188,7 @@ void updateExecutablePayloadWithoutArch() throws Exception {

@Test
@DisplayName("Updating a Payload no Executable without arch should set ALL_ARCHITECTURES")
@Transactional
@WithMockAdminUser
void updatePayloadNoExecutableWithoutArch() throws Exception {
PayloadCreateInput createInput =
Expand Down Expand Up @@ -217,6 +223,7 @@ void updatePayloadNoExecutableWithoutArch() throws Exception {

@Test
@DisplayName("Upsert architecture of a Payload")
@Transactional
@WithMockPlannerUser
void upsertCommandPayloadToValidateArchitecture() throws Exception {
Payload payload = payloadRepository.save(PayloadFixture.createDefaultCommand());
Expand Down Expand Up @@ -252,6 +259,7 @@ void upsertCommandPayloadToValidateArchitecture() throws Exception {

@Test
@DisplayName("Creating Command Line payload with both set executor and content should succeed")
@Transactional
@WithMockAdminUser
void createCommandLinePayloadWithBothSetExecutorAndContent() throws Exception {
PayloadCreateInput createInput =
Expand All @@ -270,6 +278,7 @@ void createCommandLinePayloadWithBothSetExecutorAndContent() throws Exception {
@Test
@DisplayName(
"Creating Command Line payload with both null cleanup executor and command should succeed")
@Transactional
@WithMockAdminUser
void createCommandLinePayloadWithBothNullCleanupExecutorAndCommand() throws Exception {
PayloadCreateInput createInput =
Expand All @@ -288,6 +297,7 @@ void createCommandLinePayloadWithBothNullCleanupExecutorAndCommand() throws Exce
@Test
@DisplayName(
"Creating Command Line payload with both set cleanup executor and command should succeed")
@Transactional
@WithMockAdminUser
void createCommandLinePayloadWithBothSetCleanupExecutorAndCommand() throws Exception {
PayloadCreateInput createInput =
Expand All @@ -306,6 +316,7 @@ void createCommandLinePayloadWithBothSetCleanupExecutorAndCommand() throws Excep
@Test
@DisplayName(
"Creating Command Line payload with only set cleanup executor and null command should fail")
@Transactional
@WithMockAdminUser
void createCommandLinePayloadWithOnlySetCleanupExecutorAndNullCommand() throws Exception {
PayloadCreateInput createInput =
Expand All @@ -324,6 +335,7 @@ void createCommandLinePayloadWithOnlySetCleanupExecutorAndNullCommand() throws E
@Test
@DisplayName(
"Creating Command Line payload with only set cleanup command and null executor should fail")
@Transactional
@WithMockAdminUser
void createCommandLinePayloadWithOnlySetCommandAndNullExecutor() throws Exception {
PayloadCreateInput createInput =
Expand All @@ -342,6 +354,7 @@ void createCommandLinePayloadWithOnlySetCommandAndNullExecutor() throws Exceptio
@Test
@DisplayName(
"Updating Command Line payload with only set cleanup command and null executor should fail")
@Transactional
@WithMockAdminUser
void updateCommandLinePayloadWithOnlySetCommandAndNullExecutor() throws Exception {
PayloadCreateInput createInput =
Expand Down Expand Up @@ -380,6 +393,7 @@ void updateCommandLinePayloadWithOnlySetCommandAndNullExecutor() throws Exceptio
@Test
@DisplayName(
"Duplicating a Community and Verified Payload should result in a Manual and Unverified Payload")
@Transactional
@WithMockAdminUser
void duplicateExecutablePayload() throws Exception {
PayloadCreateInput createInput =
Expand Down Expand Up @@ -420,6 +434,7 @@ void duplicateExecutablePayload() throws Exception {

@Test
@DisplayName("Process Deprecated Payloads")
@Transactional
@WithMockAdminUser
void processDeprecatedPayloads() throws Exception {
String collectorId = "039eee9b-b95d-4b11-95bb-a9ac233f1738";
Expand Down

0 comments on commit 4fbde0a

Please sign in to comment.