From 4954fa6628bbbcf8360bb2573e987f5113ebd3ef Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Wed, 30 Nov 2022 19:33:52 +0200 Subject: [PATCH] Use Hamcrest and Json Object compare --- examples/jbatch/pom.xml | 9 ++++-- .../examples/jbatch/TestJBatchEndpoint.java | 32 ++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/examples/jbatch/pom.xml b/examples/jbatch/pom.xml index 36b20fec254..cc204f16819 100644 --- a/examples/jbatch/pom.xml +++ b/examples/jbatch/pom.xml @@ -31,8 +31,8 @@ jbatch-example - 2.1.0-M2 - 2.1.0-M2 + 2.1.0 + 2.1.0 10.14.2.0 3.0.1 @@ -106,6 +106,11 @@ helidon-microprofile-tests-junit5 test + + org.hamcrest + hamcrest-core + test + diff --git a/examples/jbatch/src/test/java/io/helidon/examples/jbatch/TestJBatchEndpoint.java b/examples/jbatch/src/test/java/io/helidon/examples/jbatch/TestJBatchEndpoint.java index ac3fa679e5c..5e5bdd608cd 100644 --- a/examples/jbatch/src/test/java/io/helidon/examples/jbatch/TestJBatchEndpoint.java +++ b/examples/jbatch/src/test/java/io/helidon/examples/jbatch/TestJBatchEndpoint.java @@ -14,28 +14,37 @@ * limitations under the License. */ 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 org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +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") @@ -43,25 +52,24 @@ public void runJob() throws InterruptedException { .get(JsonObject.class); Integer responseJobId = jsonObject.getInt("Started a job with Execution ID: "); - assertNotNull(responseJobId, "Response Job Id"); - - boolean result = false; + 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); //Examine the results - jsonObject = webTarget + result = webTarget .path("batch/status/" + responseJobId) .request(MediaType.APPLICATION_JSON_TYPE) .get(JsonObject.class); - String responseString = jsonObject.toString(); - result = responseString.equals("{\"Steps executed\":\"[step1, step2]\",\"Status\":\"COMPLETED\"}"); + if (result.equals(expectedJson)){ + break; + } - if (result) break; } - assertTrue(result, "Job Result string"); + assertThat(result, equalTo(expectedJson)); } }