Repository: eclipse/microprofile-lra
Pull request #291 (2020-03-30 16:41:12)

Use ArquillianResource instead of lra.tck.base.url
For app servers which don't deploy the arquillian tests to the same
context root each time, the base url will change for every test.
Hence for these app servers (such as Open Liberty), setting a single
base url property is unreliable.

Signed-off-by: ilewis <ilewis@uk.ibm.com>


Patch:
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/LraTckConfigBean.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/LraTckConfigBean.java
index c13d9be..6c58166 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/LraTckConfigBean.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/LraTckConfigBean.java
@@ -62,7 +62,4 @@ public class LraTckConfigBean {
         return timeoutFactor;
     }
 
-    public String tckSuiteBaseUrl() {
-        return tckSuiteBaseUrl;
-    }
 }
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/LraCancelOnResource.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/LraCancelOnResource.java
index f5cd53b..4942e5f 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/LraCancelOnResource.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/LraCancelOnResource.java
@@ -21,10 +21,7 @@ package org.eclipse.microprofile.lra.tck.participant.api;
 
 import static org.eclipse.microprofile.lra.annotation.ws.rs.LRA.LRA_HTTP_CONTEXT_HEADER;
 
-import java.net.MalformedURLException;
 import java.net.URI;
-import java.net.URL;
-import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.enterprise.context.ApplicationScoped;
@@ -35,15 +32,16 @@ import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.Response.Status.Family;
+import javax.ws.rs.core.UriInfo;
 
 import org.eclipse.microprofile.lra.annotation.Compensate;
 import org.eclipse.microprofile.lra.annotation.Complete;
 import org.eclipse.microprofile.lra.annotation.ws.rs.LRA;
 import org.eclipse.microprofile.lra.annotation.ws.rs.LRA.Type;
-import org.eclipse.microprofile.lra.tck.LraTckConfigBean;
 import org.eclipse.microprofile.lra.tck.service.LRAMetricService;
 import org.eclipse.microprofile.lra.tck.service.LRAMetricType;
 
@@ -53,9 +51,6 @@ public class LraCancelOnResource {
     private static final Logger LOGGER = Logger.getLogger(LraCancelOnResource.class.getName());
     public static final String LRA_CANCEL_ON_RESOURCE_PATH = "lraresource-cancelon";
 
-    @Inject
-    private LraTckConfigBean config;
-
     @Inject
     private LRAMetricService lraMetricService;
 
@@ -141,7 +136,7 @@ public class LraCancelOnResource {
      * <p>
      * The remote REST call invokes the same resource class {@link LraCancelOnResource}
      * That assumes the call to the representative of the same LRA participant
-     * as it's already enlisted by the method {@link #cancelFromRemoteCall(java.net.URI)} invoked by the test.
+     * as it's already enlisted by the method {@link #cancelFromRemoteCall(java.net.URI, javax.ws.rs.core.UriInfo)} invoked by the test.
      * Because the specification mandates that the same participant can be enlisted
      * only once per LRA instance then
      * the {@link Compensate} method {@link #compensateWork(URI, String)}
@@ -153,19 +148,16 @@ public class LraCancelOnResource {
     @GET
     @Path(CANCEL_FROM_REMOTE_CALL)
     @LRA(value = Type.REQUIRES_NEW)
-    public Response cancelFromRemoteCall(@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI lraId) {
+    public Response cancelFromRemoteCall(@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI lraId, @Context UriInfo uriInfo) {
         Client client = ClientBuilder.newClient();
+
         try {
             Response response = client
-                    .target(URI.create(new URL(config.tckSuiteBaseUrl()).toExternalForm()))
+                    .target(uriInfo.getBaseUri())
                     .path(LRA_CANCEL_ON_RESOURCE_PATH)
                     .path(LraCancelOnResource.CANCEL_ON_FAMILY_DEFAULT_5XX)
                     .request().get();
             assert response.getStatus() == 500;
-        } catch (MalformedURLException murle) {
-            LOGGER.log(Level.SEVERE, "Cannot create url from string '"
-                    + config.tckSuiteBaseUrl() + "'", murle);
-            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(lraId).build();
         } finally {
             client.close();
         }
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/RecoveryResource.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/RecoveryResource.java
index be9afa1..72df8a3 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/RecoveryResource.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/RecoveryResource.java
@@ -27,8 +27,6 @@ import org.eclipse.microprofile.lra.tck.service.LRAMetricService;
 import org.eclipse.microprofile.lra.tck.service.LRAMetricType;
 import org.eclipse.microprofile.lra.tck.service.LRATestService;
 
-import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
 import javax.ws.rs.DefaultValue;
@@ -63,16 +61,6 @@ public class RecoveryResource {
     @Inject
     LRATestService lraTestService;
 
-    @PostConstruct
-    public void postConstruct() {
-        lraTestService.start();
-    }
-
-    @PreDestroy
-    public void preDestroy() {
-        lraTestService.stop();
-    }
-
     /**
      * Starts a new LRA and enlists an instance of this class with it as a participant
      *


Test patch:
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckCancelOnTests.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckCancelOnTests.java
index c4f6256..4118f61 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckCancelOnTests.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckCancelOnTests.java
@@ -148,7 +148,7 @@ public class TckCancelOnTests extends TckTestBase {
     }
 
     /**
-     * See {@link LraCancelOnResource#cancelFromRemoteCall(java.net.URI)}
+     * See {@link LraCancelOnResource#cancelFromRemoteCall(java.net.URI, javax.ws.rs.core.UriInfo)}
      */
     @Test
     public void cancelFromRemoteCall() {
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckTestBase.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckTestBase.java
index fb1acab..5fbf6f2 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckTestBase.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckTestBase.java
@@ -24,6 +24,7 @@ import org.eclipse.microprofile.lra.tck.participant.api.LraResource;
 import org.eclipse.microprofile.lra.tck.service.LRAMetricService;
 import org.eclipse.microprofile.lra.tck.service.LRATestService;
 import org.eclipse.microprofile.lra.tck.service.spi.LRARecoveryService;
+import org.jboss.arquillian.test.api.ArquillianResource;
 import org.jboss.shrinkwrap.api.ShrinkWrap;
 import org.jboss.shrinkwrap.api.asset.EmptyAsset;
 import org.jboss.shrinkwrap.api.spec.WebArchive;
@@ -35,6 +36,8 @@ import org.junit.rules.TestName;
 import javax.inject.Inject;
 import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.Response;
+
+import java.net.URL;
 import java.util.logging.Logger;
 
 import static org.junit.Assert.assertEquals;
@@ -49,6 +52,9 @@ public class TckTestBase {
     
     @Inject
     private LRATestService lraTestService;
+    
+    @ArquillianResource
+    private URL deploymentURL;
 
     LRAClientOps lraClient;
 
@@ -74,7 +80,7 @@ public class TckTestBase {
     public void before() {
         LOGGER.info("Running test: " + testName.getMethodName());
         
-        lraTestService.start();
+        lraTestService.start(deploymentURL);
         this.lraClient = lraTestService.getLRAClient();
         this.tckSuiteTarget = lraTestService.getTCKSuiteTarget();
     }
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/service/LRATestService.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/service/LRATestService.java
index 0071952..d6ac11b 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/service/LRATestService.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/service/LRATestService.java
@@ -30,7 +30,6 @@ import javax.inject.Inject;
 import javax.ws.rs.client.Client;
 import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.client.WebTarget;
-import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
 import java.util.Iterator;
@@ -53,16 +52,11 @@ public class LRATestService {
 
     private LRARecoveryService lraRecoveryService = loadService(LRARecoveryService.class);
 
-    public void start() {
+    public void start(URL deploymentURL) {
         tckSuiteClient = ClientBuilder.newClient();
         lraMetricService.clear();
-
-        try {
-            tckSuiteTarget = tckSuiteClient.target(URI.create(new URL(config.tckSuiteBaseUrl()).toExternalForm()));
-            lraClient = new LRAClientOps(tckSuiteTarget);
-        } catch (MalformedURLException mfe) {
-            throw new IllegalStateException("Cannot create URL for the LRA TCK suite base url " + config.tckSuiteBaseUrl(), mfe);
-        }
+        tckSuiteTarget = tckSuiteClient.target(URI.create(deploymentURL.toExternalForm()));
+        lraClient = new LRAClientOps(tckSuiteTarget);
     }
 
     public void stop() {