Skip to content

Commit

Permalink
feat: Junit tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Feb 5, 2024
1 parent 01c5a67 commit 309af2d
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 59 deletions.
6 changes: 4 additions & 2 deletions src/test/java/org/obd/metrics/api/AdapterErrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void parameterizedTest(String given, String expctedErrorMessage) throws I
// Enabling batch commands
final Adjustments optional = Adjustments
.builder()
.debugEnabled(true)
.debugEnabled(false)
.vehicleDtcCleaningEnabled(false)
.vehicleDtcReadingEnabled(false)
.vehicleMetadataReadingEnabled(false)
Expand Down Expand Up @@ -93,9 +93,11 @@ public void parameterizedTest(String given, String expctedErrorMessage) throws I
.build();

WorkflowExecutionStatus status = workflow.start(connection, query, optional);
WorkflowMonitor.waitUntilRunning(workflow);

Assertions.assertThat(status).isEqualTo(WorkflowExecutionStatus.STARTED);

WorkflowFinalizer.finalizeAfter(workflow,1000);
WorkflowFinalizer.finalizeAfter(workflow,500);

Assertions.assertThat(lifecycle.isErrorOccurred()).isTrue();
Assertions.assertThat(lifecycle.getMessage())
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/org/obd/metrics/api/AdaptiveTimingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ public void adaptiveTimeoutPolicyTest() throws IOException, InterruptedException

//Start background threads, that call the adapter,decode the raw data, and populates OBD metrics
workflow.start(connection, query,Init.DEFAULT,optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

PidDefinition rpm = workflow.getPidRegistry().findBy(6004l);

// Starting the workflow completion job, it will end workflow after some period of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 1000, ()-> workflow.getDiagnostics().rate().findBy(RateType.MEAN,rpm).get().getValue() > targetCommandFrequency + 2);
WorkflowFinalizer.finalizeAfter(workflow, 500, ()-> workflow.getDiagnostics().rate().findBy(RateType.MEAN,rpm).get().getValue() > targetCommandFrequency + 2);

// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/org/obd/metrics/api/AlertingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ public void inAlertUpperTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, optional);

WorkflowFinalizer.waitUntilRunning(workflow);
// Workflow is running
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,2000);
WorkflowFinalizer.finalizeAfter(workflow, 500);

// Ensure batch commands were sent out
Assertions.assertThat(connection.recordedQueries())
Expand Down Expand Up @@ -170,14 +169,13 @@ public void notInAlertUpperTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, optional);

WorkflowFinalizer.waitUntilRunning(workflow);
// Workflow is running
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();


// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,1500);
WorkflowFinalizer.finalizeAfter(workflow, 500);

// Ensure batch commands were sent out
Assertions.assertThat(connection.recordedQueries())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public void bigQueryTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 1500);
Expand Down
22 changes: 17 additions & 5 deletions src/test/java/org/obd/metrics/api/BatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ public void mode22() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,1000);
WorkflowFinalizer.finalizeAfter(workflow, 500);

// Ensure batch commands were sent out
Assertions.assertThat(connection.recordedQueries())
Expand Down Expand Up @@ -174,10 +177,13 @@ public void moreThan6PriorityCommands() throws IOException, InterruptedException
// Start background threads, that call the adapter,decode the raw data, and
// populates OBD metrics
workflow.start(connection, query, optional);


WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,1500);
WorkflowFinalizer.finalizeAfter(workflow, 500);

// // Ensure batch commands were sent out
Assertions.assertThat(connection.recordedQueries())
Expand Down Expand Up @@ -233,9 +239,12 @@ public void batchTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,1500L);
WorkflowFinalizer.finalizeAfter(workflow, 500L);

// Ensure batch commands were sent out
Assertions.assertThat(connection.recordedQueries())
Expand Down Expand Up @@ -278,7 +287,10 @@ public void batchLessThan6Test() throws IOException, InterruptedException {

workflow.start(connection, query, optional);

WorkflowFinalizer.finalizeAfter(workflow,2000);
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

WorkflowFinalizer.finalizeAfter(workflow, 500);

// Ensure batch commands were sent out
Assertions.assertThat(connection.recordedQueries())
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/org/obd/metrics/api/CANMessageHeaderManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ public void singleModeTest(boolean batchEnabled, String adapterGivenResponse,
// populates OBD metrics
workflow.start(connection, query, init, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 1500);
WorkflowFinalizer.finalizeAfter(workflow, 500);

final BlockingDeque<String> recordedQueries = (BlockingDeque<String>) connection.recordedQueries();

Expand Down Expand Up @@ -367,9 +370,12 @@ public void dtcReadTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,2000);
WorkflowFinalizer.finalizeAfter(workflow,500);

final BlockingDeque<String> recordedQueries = (BlockingDeque<String>) connection.recordedQueries();

Expand Down Expand Up @@ -496,9 +502,12 @@ public void dtcClearTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 2000);
WorkflowFinalizer.finalizeAfter(workflow, 500);

final BlockingDeque<String> recordedQueries = (BlockingDeque<String>) connection.recordedQueries();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ public void dtcClearOk() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,800);
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();
WorkflowFinalizer.finalize(workflow);


// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down Expand Up @@ -187,9 +188,10 @@ public void dtcClearErr() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,800);
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();
WorkflowFinalizer.finalize(workflow);


// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down Expand Up @@ -267,9 +269,10 @@ public void dtcClearDisabled() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,800);
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();
WorkflowFinalizer.finalize(workflow);


// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ public void dtcReadEnabled() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,800);
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();
WorkflowFinalizer.finalize(workflow);


// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down Expand Up @@ -185,9 +186,10 @@ public void dtcReadDisabled() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow,800);
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();
WorkflowFinalizer.finalize(workflow);


// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/org/obd/metrics/api/DriveCouplingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ public void batchOffTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 1500);
WorkflowFinalizer.finalizeAfter(workflow, 500);

final BlockingDeque<String> recordedQueries = (BlockingDeque<String>) connection.recordedQueries();

Expand Down Expand Up @@ -199,9 +202,12 @@ public void batchOnfTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, init, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 1500);
WorkflowFinalizer.finalizeAfter(workflow, 500);

final BlockingDeque<String> recordedQueries = (BlockingDeque<String>) connection.recordedQueries();

Expand Down
7 changes: 5 additions & 2 deletions src/test/java/org/obd/metrics/api/GenericWorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ public void recieveReplyTest() throws IOException, InterruptedException {
// populates OBD metrics
workflow.start(connection, query, optional);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();

PidDefinition rpm = workflow.getPidRegistry().findBy(6004l);

// Workflow completion thread, it will end workflow after some period of time
// (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 3000, ()-> workflow.getDiagnostics().rate().findBy(RateType.MEAN,rpm).get().getValue() > 5);
WorkflowFinalizer.finalizeAfter(workflow, 1000, ()-> workflow.getDiagnostics().rate().findBy(RateType.MEAN,rpm).get().getValue() > 5);

// Ensure we receive AT command as well
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/obd/metrics/api/MultipleDecodersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public void t0() throws IOException, InterruptedException, ExecutionException {

workflow.start(connection, query);

WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();
WorkflowFinalizer.finalize(workflow);

WorkflowFinalizer.finalizeAfter(workflow,800);

PidDefinitionRegistry pids = workflow.getPidRegistry();
Diagnostics diagnostics = workflow.getDiagnostics();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/obd/metrics/api/PIDsPriorityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public void overrideTest() throws IOException, InterruptedException {
PidDefinition p1 = pidRegistry.findBy(8l);
PidDefinition p2 = pidRegistry.findBy(22l);

WorkflowFinalizer.finalizeAfter(workflow, 2500);
WorkflowMonitor.waitUntilRunning(workflow);
WorkflowFinalizer.finalizeAfter(workflow, 700);

RateSupplier rateCollector = workflow.getDiagnostics().rate();
double rate1 = rateCollector.findBy(RateType.MEAN,p1).get().getValue();
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/obd/metrics/api/STNxxxDecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void giulia_gme_2_0() throws IOException, InterruptedException {
workflow.start(connection, query,init,optional);

// Starting the workflow completion job, it will end workflow after given period of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 2800);
WorkflowFinalizer.finalizeAfter(workflow, 800);

// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/org/obd/metrics/api/StatisticsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public void mode01WorkflowTest() throws IOException, InterruptedException {

workflow.start(connection, query, optional);

WorkflowFinalizer.finalizeAfter(workflow, 1000l);
WorkflowMonitor.waitUntilRunning(workflow);
Assertions.assertThat(workflow.isRunning()).isTrue();
WorkflowFinalizer.finalize(workflow);


PidDefinitionRegistry pids = workflow.getPidRegistry();

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/obd/metrics/api/VinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void correctTest() throws IOException, InterruptedException {

// Starting the workflow completion job, it will end workflow after some period
// of time (helper method)
WorkflowFinalizer.finalizeAfter(workflow, 500l);
WorkflowFinalizer.finalize(workflow);

// Ensure we receive AT command
Assertions.assertThat(collector.findATResetCommand()).isNotNull();
Expand Down
13 changes: 2 additions & 11 deletions src/test/java/org/obd/metrics/api/WorkflowFinalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
public interface WorkflowFinalizer {

public static final int DEFAULT_FINALIZE_TIME = 200;

static void finalizeAfter(final Workflow workflow, long sleepTime, Supplier<Boolean> condition)
throws InterruptedException {
final Callable<String> end = () -> {
ConditionalSleep conditionalSleep = ConditionalSleep
final ConditionalSleep conditionalSleep = ConditionalSleep
.builder()
.condition(condition)
.slice(10l)
Expand All @@ -47,15 +47,6 @@ static void finalizeAfter(final Workflow workflow, long sleepTime, Supplier<Bool
newFixedThreadPool.shutdown();
}

static void waitUntilRunning(final Workflow workflow) {
// Workflow is running
while (!workflow.isRunning()) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {}
}
}

static void finalizeAfter(final Workflow workflow, long sleepTime) throws InterruptedException {
finalizeAfter(workflow, sleepTime, () -> false);
}
Expand Down
Loading

0 comments on commit 309af2d

Please sign in to comment.