Repository: eclipse/microprofile-lra (java)

Issue:
Test that nested participants with Forget methods are notified when the parent LRA is closed
#206 added/made explicit the requirement that a "nested participant" with a Forget method is notified if the the parent LRA is closed. We need a TCK test for this behaviour.

Pull request:
issue208 Add test that nested participants with Forget methods are no…
…tified when the parent LRA is closed

resolves #208 

Signed-off-by: xstefank <xstefank122@gmail.com>

Patch:
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/ContextTckResource.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/ContextTckResource.java
index ebd1561..459575d 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/ContextTckResource.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/participant/api/ContextTckResource.java
@@ -70,6 +70,7 @@ public class ContextTckResource {
     public static final String NEW_LRA_PATH = "/new-lra";
     public static final String REQUIRED_LRA_PATH = "/required-lra";
     public static final String NESTED_LRA_PATH = "/nested-lra";
+    public static final String NESTED_LRA_PATH_WITH_CLOSE = "/nested-lra-with-close";
     // resource path for testing that context on outbound and inbound calls made from an
     // method annotated with @LRA are spec compliant
     public static final String CONTEXT_CHECK_LRA_PATH = "/context-check-lra";
@@ -198,6 +199,14 @@ public class ContextTckResource {
         return Response.ok().entity(nestedLRA.toASCIIString() + "," + parentLRA.toASCIIString()).build();
     }
 
+    @LRA(value = LRA.Type.NESTED)
+    @PUT
+    @Path(NESTED_LRA_PATH_WITH_CLOSE)
+    public Response nestedLRAWithClose(@HeaderParam(LRA_HTTP_CONTEXT_HEADER) URI nestedLRA,
+                              @HeaderParam(LRA_HTTP_PARENT_CONTEXT_HEADER) URI parentLRA) {
+        return Response.ok().entity(nestedLRA.toASCIIString() + "," + parentLRA.toASCIIString()).build();
+    }
+
     // test that outgoing calls do not affect the calling context
     @LRA(value = LRA.Type.REQUIRED)
     @PUT


Test patch:
diff --git a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckContextTests.java b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckContextTests.java
index 683f382..7afe46a 100644
--- a/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckContextTests.java
+++ b/tck/src/main/java/org/eclipse/microprofile/lra/tck/TckContextTests.java
@@ -52,6 +52,7 @@ import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResourc
 import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResource.LRA_TCK_HTTP_CONTEXT_HEADER;
 import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResource.METRIC_PATH;
 import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResource.NESTED_LRA_PATH;
+import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResource.NESTED_LRA_PATH_WITH_CLOSE;
 import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResource.NEW_LRA_PATH;
 import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResource.REQUIRED_LRA_PATH;
 import static org.eclipse.microprofile.lra.tck.participant.api.ContextTckResource.RESET_PATH;
@@ -94,7 +95,7 @@ public class TckContextTests extends TckTestBase {
         // verify that the resource was asked to complete
         int completions = lraMetricService.getMetric(LRAMetricType.Completed, lra);
 
-        assertEquals(testName.getMethodName() + ": Resource was not asked to complete", 
+        assertEquals(testName.getMethodName() + ": Resource was not asked to complete",
                 1, completions);
     }
 
@@ -212,6 +213,41 @@ public class TckContextTests extends TckTestBase {
                 1, endCallsWithParentContextHeaderPresent);
     }
 
+    @Test
+    public void testForgetCalledForNestedParticipantsWhenParentIsClosed() {
+        // start an LRA
+        URI topLevelLRA = URI.create(invoke(NEW_LRA_PATH, PUT, null));
+        // start a nested LRA
+        String result = invoke(NESTED_LRA_PATH_WITH_CLOSE, PUT, topLevelLRA);
+        // the resource method should return the nested LRA and the top level LRA separated by a comma
+        assertTrue(result.contains(","));
+        assertEquals(testName.getMethodName() + ": wrong parent LRA", topLevelLRA, URI.create(result.split(",")[1]));
+
+        URI nestedLRA = URI.create(result.split(",")[0]);
+        lraTestService.waitForCallbacks(nestedLRA);
+
+        // nested LRA should be closed
+        assertEquals(testName.getMethodName() + ": resource should have completed for the nested LRA",
+            1, lraMetricService.getMetric(LRAMetricType.Completed, nestedLRA));
+
+        // end the top level LRA
+        invoke(REQUIRED_LRA_PATH, PUT, topLevelLRA);
+        lraTestService.waitForCallbacks(topLevelLRA);
+
+        assertEquals(testName.getMethodName() + ": resource should have completed for the top level LRA",
+            1, lraMetricService.getMetric(LRAMetricType.Completed, topLevelLRA));
+        // nested LRA Complete method should not be replayed when parent is closed
+        assertEquals(testName.getMethodName() + ": resource should not have completed for the nested LRA again",
+            1, lraMetricService.getMetric(LRAMetricType.Completed, nestedLRA));
+        // nested LRA Forget should be called when parent is closed
+        assertEquals(testName.getMethodName() + ": resource should have called forget for the nested LRA",
+            1, lraMetricService.getMetric(LRAMetricType.Forget, nestedLRA));
+        // nested is incremented twice: once for repeated Complete and once for Forget
+        assertEquals(testName.getMethodName() + ": parent context not included in complete or forget callbacks",
+            2, lraMetricService.getMetric(LRAMetricType.Nested, topLevelLRA));
+
+    }
+
     // invoke a method in an LRA context which performs various outgoing calls checking that the notion of active context
     // conforms with what is written in the specification
     @Test
@@ -244,7 +280,7 @@ public class TckContextTests extends TckTestBase {
     @Test
     public void testAsync3Support() {
         // invoke an async resource that throws an exception which cancels the LRA
-        URI lra = URI.create(invoke(ASYNC_LRA_PATH3, PUT, null, 404, 
+        URI lra = URI.create(invoke(ASYNC_LRA_PATH3, PUT, null, 404,
             ContextTckResource.EndPhase.SUCCESS, 200));
         lraTestService.waitForCallbacks(lra);