From ff7a4067b9b7a0745c623684b38d92cdbf57c92b Mon Sep 17 00:00:00 2001 From: Daniel Kec Date: Thu, 8 Aug 2024 14:54:44 +0200 Subject: [PATCH] LRA testing feature #8469 --- examples/microprofile/lra/pom.xml | 29 ++++++++ .../example/lra/LRAExampleResourceTest.java | 74 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 examples/microprofile/lra/src/test/java/io/helidon/microprofile/example/lra/LRAExampleResourceTest.java diff --git a/examples/microprofile/lra/pom.xml b/examples/microprofile/lra/pom.xml index afd4c8a9..30e88cca 100644 --- a/examples/microprofile/lra/pom.xml +++ b/examples/microprofile/lra/pom.xml @@ -58,6 +58,35 @@ runtime true + + + org.junit.jupiter + junit-jupiter-api + test + + + io.helidon.microprofile.lra + helidon-microprofile-lra-testing + test + + 4.1.0-SNAPSHOT + + + org.hamcrest + hamcrest-all + test + + + io.helidon.microprofile.testing + helidon-microprofile-testing-junit5 + test + + + org.awaitility + awaitility + 4.2.0 + test + diff --git a/examples/microprofile/lra/src/test/java/io/helidon/microprofile/example/lra/LRAExampleResourceTest.java b/examples/microprofile/lra/src/test/java/io/helidon/microprofile/example/lra/LRAExampleResourceTest.java new file mode 100644 index 00000000..6b5c1872 --- /dev/null +++ b/examples/microprofile/lra/src/test/java/io/helidon/microprofile/example/lra/LRAExampleResourceTest.java @@ -0,0 +1,74 @@ +package io.helidon.microprofile.example.lra; + +import java.time.Duration; + +import io.helidon.lra.coordinator.Lra; +import io.helidon.microprofile.testing.junit5.AddBean; +import io.helidon.microprofile.testing.junit5.HelidonTest; +import io.helidon.microprofile.testing.lra.TestLraCoordinator; + +import jakarta.inject.Inject; +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; +import org.eclipse.microprofile.lra.annotation.LRAStatus; +import org.eclipse.microprofile.lra.annotation.ws.rs.LRA; +import org.junit.jupiter.api.Test; + +import static org.awaitility.Awaitility.await; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +@HelidonTest +@AddBean(TestLraCoordinator.class) +public class LRAExampleResourceTest { + + @Inject + private TestLraCoordinator coordinator; + + @Inject + private WebTarget target; + + @Test + public void testComplete() { + try (Response res = target + .path("/example/start-example") + .request() + .put(Entity.entity("lra rocks!", MediaType.TEXT_PLAIN_TYPE))) { + assertThat(res.getStatus(), is(200)); + String lraId = res.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER); + Lra lra = coordinator.lra(lraId); + assertThat(lra.status(), is(LRAStatus.Closed)); + } + } + + @Test + public void testCompensation() { + try (Response res = target + .path("/example/start-example") + .request() + .put(Entity.entity("BOOM", MediaType.TEXT_PLAIN_TYPE))) { + assertThat(res.getStatus(), is(500)); + String lraId = res.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER); + Lra lra = coordinator.lra(lraId); + assertThat(lra.status(), is(LRAStatus.Cancelled)); + } + } + + @Test + public void testTimeout() { + try (Response res = target + .path("/example/start-example") + .request() + .put(Entity.entity("TIMEOUT", MediaType.TEXT_PLAIN_TYPE))) { + assertThat(res.getStatus(), is(200)); + String lraId = res.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER); + Lra lra = coordinator.lra(lraId); + await() + .atMost(Duration.ofSeconds(15)) + .until(() -> lra.status() == LRAStatus.Cancelled); + assertThat(lra.status(), is(LRAStatus.Cancelled)); + } + } +}