Skip to content

Commit

Permalink
issue35 Add non-JAX-RS CompletionStage TCK
Browse files Browse the repository at this point in the history
Signed-off-by: xstefank <[email protected]>
  • Loading branch information
xstefank committed May 10, 2019
1 parent 430e04e commit a5e2987
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -112,14 +128,18 @@ 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() {
Response response = tckSuiteTarget.path(ValidLRAParticipant.RESOURCE_PATH)
.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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ParticipantStatus> 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();
}



}

0 comments on commit a5e2987

Please sign in to comment.