Skip to content

Commit

Permalink
[3.x] - Fix Intermittent TestJBatchEndpoint.runJob (#5557)
Browse files Browse the repository at this point in the history
* Fix JBatch intermittent test fail

* refactor to String and Hamcrest

* refactor to JSON Object compare
  • Loading branch information
dalexandrov authored Nov 30, 2022
1 parent cdcf045 commit 56ba8de
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
9 changes: 7 additions & 2 deletions examples/jbatch/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<name>jbatch-example</name>

<properties>
<version.lib.jbatch-api>2.1.0-M2</version.lib.jbatch-api>
<version.lib.jbatch.container>2.1.0-M2</version.lib.jbatch.container>
<version.lib.jbatch-api>2.1.0</version.lib.jbatch-api>
<version.lib.jbatch.container>2.1.0</version.lib.jbatch.container>
<version.lib.derby>10.14.2.0</version.lib.derby>
<version.lib.jaxb-api>3.0.1</version.lib.jaxb-api>
</properties>
Expand Down Expand Up @@ -106,6 +106,11 @@
<artifactId>helidon-microprofile-tests-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,61 @@
package io.helidon.examples.jbatch;

import io.helidon.microprofile.tests.junit5.HelidonTest;
import jakarta.json.Json;
import jakarta.json.JsonBuilderFactory;
import org.junit.jupiter.api.Test;

import jakarta.inject.Inject;
import jakarta.json.JsonObject;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

@HelidonTest
public class TestJBatchEndpoint {

private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());

@Inject
private WebTarget webTarget;

@Test
public void runJob() throws InterruptedException {

JsonObject expectedJson = JSON.createObjectBuilder()
.add("Steps executed", "[step1, step2]")
.add("Status", "COMPLETED")
.build();

//Start the job
JsonObject jsonObject = webTarget
.path("/batch")
.request(MediaType.APPLICATION_JSON_TYPE)
.get(JsonObject.class);

Integer responseJobId = jsonObject.getInt("Started a job with Execution ID: ");
assertNotNull(responseJobId, "Response Job Id");
assertThat(responseJobId, is(notNullValue()));
JsonObject result = null;
for (int i = 1; i < 10; i++) {
//Wait a bit for it to complete
Thread.sleep(i*1000);

//Wait a bit for it to complete
Thread.sleep(3000);
//Examine the results
result = webTarget
.path("batch/status/" + responseJobId)
.request(MediaType.APPLICATION_JSON_TYPE)
.get(JsonObject.class);

//Examine the results
jsonObject = webTarget
.path("batch/status/" + responseJobId)
.request(MediaType.APPLICATION_JSON_TYPE)
.get(JsonObject.class);
if (result.equals(expectedJson)){
break;
}

String responseString = jsonObject.toString();
}

assertEquals("{\"Steps executed\":\"[step1, step2]\",\"Status\":\"COMPLETED\"}",
responseString, "Job Result string");
assertThat(result, equalTo(expectedJson));
}
}

0 comments on commit 56ba8de

Please sign in to comment.