-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix pause computation in inject start date
Signed-off-by: Antoine MAZEAS <[email protected]>
- Loading branch information
1 parent
d910eb9
commit 5444f43
Showing
8 changed files
with
267 additions
and
21 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
openbas-api/src/main/java/io/openbas/migration/V3_62__make_exercise_pause_tz_aware.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.openbas.migration; | ||
|
||
import java.sql.Connection; | ||
import java.sql.Statement; | ||
import org.flywaydb.core.api.migration.BaseJavaMigration; | ||
import org.flywaydb.core.api.migration.Context; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class V3_62__make_exercise_pause_tz_aware extends BaseJavaMigration { | ||
|
||
@Override | ||
public void migrate(Context context) throws Exception { | ||
Connection connection = context.getConnection(); | ||
Statement statement = connection.createStatement(); | ||
|
||
statement.execute( | ||
""" | ||
ALTER TABLE exercises ADD COLUMN exercise_pause_date_tempwithtz TIMESTAMP WITH TIME ZONE; | ||
UPDATE exercises SET exercise_pause_date_tempwithtz = exercise_pause_date; | ||
ALTER TABLE exercises DROP COLUMN exercise_pause_date; | ||
ALTER TABLE exercises RENAME COLUMN exercise_pause_date_tempwithtz TO exercise_pause_date; | ||
"""); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
openbas-api/src/test/java/io/openbas/database/model/ExerciseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.openbas.database.model; | ||
|
||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
|
||
import io.openbas.database.raw.RawExercise; | ||
import io.openbas.database.repository.ExerciseRepository; | ||
import io.openbas.utils.fixtures.ExerciseFixture; | ||
import io.openbas.utils.fixtures.composers.ExerciseComposer; | ||
import jakarta.persistence.EntityManager; | ||
import java.time.Instant; | ||
import org.junit.jupiter.api.Assertions; | ||
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 | ||
@TestInstance(PER_CLASS) | ||
@Transactional | ||
public class ExerciseTest { | ||
@Autowired private ExerciseComposer exerciseComposer; | ||
@Autowired private ExerciseRepository exerciseRepository; | ||
@Autowired private EntityManager entityManager; | ||
|
||
private final Instant exerciseStartTime = Instant.parse("2012-11-21T04:00:00Z"); | ||
|
||
@Test | ||
@DisplayName("Given a persisted exercise, current pause from raw query is correctly persisted.") | ||
public void GivenAnExercise_CurrentPauseFromRawQueryIsCorrectlyPersisted() { | ||
Instant expectedCurrentPauseTime = Instant.parse("2012-11-21T04:05:00Z"); | ||
ExerciseComposer.Composer wrapper = | ||
exerciseComposer | ||
.forExercise(ExerciseFixture.createDefaultAttackExercise(exerciseStartTime)); | ||
wrapper.get().setCurrentPause(expectedCurrentPauseTime); // current pause at T+5 minutes | ||
|
||
Exercise expected = wrapper.persist().get(); | ||
|
||
// reset JPA | ||
entityManager.flush(); | ||
entityManager.clear(); | ||
|
||
RawExercise dbExercise = exerciseRepository.rawDetailsById(expected.getId()); | ||
|
||
Assertions.assertTrue( | ||
expected.getCurrentPause().isPresent(), | ||
"Current pause should be present for expected exercise"); | ||
Assertions.assertEquals(expected.getCurrentPause().get(), dbExercise.getExercise_pause_date()); | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
openbas-api/src/test/java/io/openbas/database/model/InjectTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package io.openbas.database.model; | ||
|
||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; | ||
|
||
import io.openbas.utils.fixtures.ExerciseFixture; | ||
import io.openbas.utils.fixtures.InjectFixture; | ||
import io.openbas.utils.fixtures.PauseFixture; | ||
import io.openbas.utils.fixtures.composers.ExerciseComposer; | ||
import io.openbas.utils.fixtures.composers.InjectComposer; | ||
import io.openbas.utils.fixtures.composers.PauseComposer; | ||
import java.time.Instant; | ||
import org.junit.jupiter.api.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@SpringBootTest | ||
@TestInstance(PER_CLASS) | ||
@Transactional | ||
public class InjectTest { | ||
|
||
@Autowired private ExerciseComposer exerciseComposer; | ||
@Autowired private InjectComposer injectComposer; | ||
@Autowired private PauseComposer pauseComposer; | ||
|
||
@Nested | ||
@DisplayName("Given valid exercise") | ||
public class GivenValidExerciseWithTwoPauses { | ||
|
||
private final Instant exerciseStartTime = Instant.parse("2012-11-21T04:00:00Z"); | ||
|
||
public ExerciseComposer.Composer getExerciseComposer() { | ||
return exerciseComposer | ||
.forExercise(ExerciseFixture.createRunningAttackExercise(exerciseStartTime)) | ||
.withInject( | ||
injectComposer.forInject( | ||
InjectFixture.getDefaultInjectWithDuration(600L))); // run time 04:10 | ||
} | ||
|
||
@Nested | ||
@DisplayName("With two pauses, one of which starts after original inject time") | ||
public class WithTwoPauses { | ||
|
||
private final Instant firstPauseStartTime = Instant.parse("2012-11-21T04:02:00Z"); | ||
private final Instant secondPauseStartTime = Instant.parse("2012-11-21T04:15:00Z"); | ||
|
||
@Test | ||
@DisplayName( | ||
"When the inject was affected by both pauses its starting date should account for both pauses") | ||
public void WhenInjectEffectivelyWasPausedTwice_InjectDateAccountsForBothPauses() { | ||
Exercise exercise = | ||
getExerciseComposer() | ||
.withPause( | ||
pauseComposer.forPause( | ||
// first pause duration brings wakeup close to second pause start | ||
// so that inject does not run in between | ||
PauseFixture.createPause(firstPauseStartTime, 600L))) // wakeup 04:12 | ||
.withPause( | ||
pauseComposer.forPause( | ||
PauseFixture.createPause(secondPauseStartTime, 3600L))) // wakeup 05:15 | ||
.get(); | ||
Instant expected_instant = Instant.parse("2012-11-21T05:20:00Z"); | ||
|
||
Inject inject = exercise.getInjects().getFirst(); | ||
|
||
Assertions.assertTrue(inject.getDate().isPresent(), "Inject has no date."); | ||
Assertions.assertEquals(expected_instant, inject.getDate().get()); | ||
} | ||
|
||
@Test | ||
@DisplayName( | ||
"When the inject was affected by first pause only its starting date should account for first pause only") | ||
public void WhenInjectEffectivelyWasPausedOnce_InjectDateAccountsForSinglePause() { | ||
Exercise exercise = | ||
getExerciseComposer() | ||
.withPause( | ||
pauseComposer.forPause( | ||
// first pause is short enough so that inject runs before second pause | ||
// the pause will effectively delay inject by a single minute | ||
PauseFixture.createPause( | ||
firstPauseStartTime, 15L))) // wakeup 04:02:15, effective 04:03:00 | ||
.withPause( | ||
pauseComposer.forPause( | ||
PauseFixture.createPause(secondPauseStartTime, 3600L))) // wakeup 05:15 | ||
.get(); | ||
Instant expected_instant = Instant.parse("2012-11-21T04:11:00Z"); | ||
|
||
Inject inject = exercise.getInjects().getFirst(); | ||
|
||
Assertions.assertTrue(inject.getDate().isPresent(), "Inject has no date."); | ||
Assertions.assertEquals(expected_instant, inject.getDate().get()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
openbas-api/src/test/java/io/openbas/utils/fixtures/PauseFixture.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.openbas.utils.fixtures; | ||
|
||
import io.openbas.database.model.Pause; | ||
import java.time.Instant; | ||
|
||
public class PauseFixture { | ||
public static Pause createPause(Instant start, long duration) { | ||
Pause pause = new Pause(); | ||
pause.setDuration(duration); | ||
pause.setDate(start); | ||
return pause; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
openbas-api/src/test/java/io/openbas/utils/fixtures/composers/PauseComposer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.openbas.utils.fixtures.composers; | ||
|
||
import io.openbas.database.model.Pause; | ||
import io.openbas.database.repository.PauseRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PauseComposer extends ComposerBase<Pause> { | ||
@Autowired private PauseRepository pauseRepository; | ||
|
||
public class Composer extends InnerComposerBase<Pause> { | ||
private final Pause pause; | ||
|
||
public Composer(Pause pause) { | ||
this.pause = pause; | ||
} | ||
|
||
@Override | ||
public Composer persist() { | ||
pauseRepository.save(pause); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Composer delete() { | ||
pauseRepository.delete(pause); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Pause get() { | ||
return this.pause; | ||
} | ||
} | ||
|
||
public Composer forPause(Pause pause) { | ||
this.generatedItems.add(pause); | ||
return new Composer(pause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters