From a5e2987077e1338e287af5b7982fa5fef84ca8fa Mon Sep 17 00:00:00 2001 From: xstefank Date: Fri, 10 May 2019 16:05:43 +0200 Subject: [PATCH] issue35 Add non-JAX-RS CompletionStage TCK Signed-off-by: xstefank --- .../lra/tck/TckParticipantTests.java | 28 ++++++- .../nonjaxrs/ValidCSParticipant.java | 84 +++++++++++++++++++ 2 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/nonjaxrs/ValidCSParticipant.java diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckParticipantTests.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckParticipantTests.java index d2c9cda6..2ca97849 100644 --- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckParticipantTests.java +++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckParticipantTests.java @@ -19,6 +19,7 @@ *******************************************************************************/ package org.eclipse.microprofile.lra.tck; +import org.eclipse.microprofile.lra.tck.participant.nonjaxrs.ValidCSParticipant; import org.eclipse.microprofile.lra.tck.participant.nonjaxrs.ValidLRAParticipant; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; @@ -47,7 +48,7 @@ public class TckParticipantTests extends TckTestBase { @Deployment(name = VALID_DEPLOYMENT) public static WebArchive deployValidParticipant() { return TckTestBase.deploy(VALID_DEPLOYMENT) - .addClasses(ValidLRAParticipant.class); + .addClasses(ValidLRAParticipant.class, ValidCSParticipant.class); } @Before @@ -98,6 +99,21 @@ public void validSignaturesChainTest() throws InterruptedException { beforeForgetCount + 1, getForgetCount()); } + + @Test + public void testNonJaxRsParticipantMethodReturningCompletionStage() { + int beforeCompensateCount = getCompensateCount(ValidCSParticipant.ROOT_PATH); + + Response response = tckSuiteTarget.path(ValidCSParticipant.ROOT_PATH) + .path(ValidCSParticipant.ENLIST_WITH_COMPENSATE) + .request() + .get(); + + assertEquals("The 500 status response is expected", + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + assertEquals("Non JAX-RS @Compensate method with CompletionStage should have been called", + beforeCompensateCount + 1, getCompensateCount(ValidCSParticipant.ROOT_PATH)); + } private int getCompletedCount() { Response response = tckSuiteTarget.path(ValidLRAParticipant.RESOURCE_PATH) @@ -112,9 +128,7 @@ private int getCompensateCount() { } private int getStatusCount() { - Response response = tckSuiteTarget.path(ValidLRAParticipant.RESOURCE_PATH) - .path(ValidLRAParticipant.STATUS_COUNT_PATH).request().get(); - return response.readEntity(Integer.class); + return getCompensateCount(ValidLRAParticipant.RESOURCE_PATH); } private int getForgetCount() { @@ -122,4 +136,10 @@ private int getForgetCount() { .path(ValidLRAParticipant.FORGET_COUNT_PATH).request().get(); return response.readEntity(Integer.class); } + + private int getCompensateCount(String path) { + Response response = tckSuiteTarget.path(path) + .path(ValidLRAParticipant.COMPENSATED_COUNT_PATH).request().get(); + return response.readEntity(Integer.class); + } } diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/nonjaxrs/ValidCSParticipant.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/nonjaxrs/ValidCSParticipant.java new file mode 100644 index 00000000..5abbf8df --- /dev/null +++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/nonjaxrs/ValidCSParticipant.java @@ -0,0 +1,84 @@ +/* + ******************************************************************************* + * Copyright (c) 2019 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package org.eclipse.microprofile.lra.tck.participant.nonjaxrs; + +import org.eclipse.microprofile.lra.annotation.Compensate; +import org.eclipse.microprofile.lra.annotation.ParticipantStatus; +import org.eclipse.microprofile.lra.annotation.ws.rs.LRA; + +import javax.enterprise.context.ApplicationScoped; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.net.URI; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.atomic.AtomicInteger; + +@ApplicationScoped +@Path(ValidCSParticipant.ROOT_PATH) +public class ValidCSParticipant { + + public static final String ROOT_PATH = "valid-cs-participant"; + public static final String ENLIST_WITH_COMPENSATE = "enlist"; + + private final AtomicInteger compensatedCount = new AtomicInteger(0); + + @GET + @Path(ENLIST_WITH_COMPENSATE) + @LRA(value = LRA.Type.REQUIRED, cancelOn = Response.Status.INTERNAL_SERVER_ERROR) + public Response enlist(@HeaderParam(LRA.LRA_HTTP_CONTEXT_HEADER) String lraId) { + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + } + + @Compensate + public CompletionStage compensate(URI lraId) { + assert lraId != null; + + compensatedCount.incrementAndGet(); + + return CompletableFuture.runAsync(() -> { + simulateLongRunningCompensation(); + + + }).handle((s, e) -> e != null ? ParticipantStatus.Compensated : ParticipantStatus.FailedToCompensate); + } + + private void simulateLongRunningCompensation() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + @GET + @Path(ValidLRAParticipant.COMPENSATED_COUNT_PATH) + @Produces(MediaType.TEXT_PLAIN) + public Response compensated() { + return Response.ok(compensatedCount.toString()).build(); + } + + + +}