Search in sources :

Example 11 with LRAData

use of io.narayana.lra.LRAData in project narayana by jbosstm.

the class CoordinatorApiIT method startCloseLRA.

/**
 * POST - /start?TimeLimit=...&ClientID=...&ParentLRA=...
 * PUT - /{lraId}/close
 * Starting and closing an LRA.
 */
@Test
public void startCloseLRA() throws UnsupportedEncodingException {
    URI lraId1, lraId2;
    try (Response response = client.target(coordinatorUrl).path("start").queryParam(CLIENT_ID_PARAM_NAME, testRule.getMethodName() + "_1").queryParam(TIME_LIMIT_PARAM_NAME, // negative time limit is permitted by spec
    "-42").request().header(LRA_API_VERSION_HEADER_NAME, version).post(null)) {
        Assert.assertEquals("Creating top-level LRA should be successful, POST/201 is expected.", Status.CREATED.getStatusCode(), response.getStatus());
        lraId1 = URI.create(response.readEntity(String.class));
        Assert.assertNotNull("Expected non null LRA id to be returned from start call", lraId1);
        lrasToAfterFinish.add(lraId1);
        URI lraIdFromLocationHeader = URI.create(response.getHeaderString(HttpHeaders.LOCATION));
        Assert.assertEquals("Expected the LOCATION header containing the started top-level LRA id", lraId1, lraIdFromLocationHeader);
        // context header is returned strangely to client, some investigation will be needed
        // URI lraIdFromLRAContextHeader = URI.create(response.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER));
        // Assert.assertEquals("Expecting the LRA context header configures the same LRA id as entity content on starting top-level LRA",
        // lraId1, lraIdFromLRAContextHeader);
        Assert.assertEquals("Expecting to get the same API version as used for the request on top-level LRA start", version, response.getHeaderString(LRA_API_VERSION_HEADER_NAME));
    }
    String encodedLraId1 = URLEncoder.encode(lraId1.toString(), StandardCharsets.UTF_8.name());
    try (Response response = client.target(coordinatorUrl).path("start").queryParam(CLIENT_ID_PARAM_NAME, testRule.getMethodName() + "_2").queryParam(PARENT_LRA_PARAM_NAME, encodedLraId1).request().header(LRA_API_VERSION_HEADER_NAME, version).post(null)) {
        Assert.assertEquals("Creating nested LRA should be successful, POST/201 is expected.", Status.CREATED.getStatusCode(), response.getStatus());
        lraId2 = URI.create(response.readEntity(String.class));
        Assert.assertNotNull("Expected non null nested LRA id being returned in the response body", lraId2);
        // the nested LRA id is in format <nested LRA id>?ParentLRA=<parent LRA id>
        URI lraIdFromLocationHeader = URI.create(response.getHeaderString(HttpHeaders.LOCATION));
        Assert.assertEquals("Expected the LOCATION header containing the started nested LRA id", lraId2, lraIdFromLocationHeader);
        // context header is returned strangely to client, some investigation will be needed
        // String lraContextHeader = response.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER);
        // the context header is in format <parent LRA id>,<nested LRA id>?ParentLRA=<parent LRA id>
        // MatcherAssert.assertThat("Expected the nested LRA context header gives the parent LRA id at first",
        // lraContextHeader, startsWith(lraId1.toASCIIString()));
        // MatcherAssert.assertThat("Expected the nested LRA context header provides LRA id of started nested LRA",
        // lraContextHeader, containsString("," + lraId2.toASCIIString()));
        Assert.assertEquals("Expecting to get the same API version as used for the request on nested LRA start", version, response.getHeaderString(LRA_API_VERSION_HEADER_NAME));
    }
    Collection<URI> returnedLraIds = lraClient.getAllLRAs().stream().map(LRAData::getLraId).collect(Collectors.toList());
    MatcherAssert.assertThat("Expected the coordinator knows about the top-level LRA", returnedLraIds, hasItem(lraId1));
    MatcherAssert.assertThat("Expected the coordinator knows about the nested LRA", returnedLraIds, hasItem(lraId2));
    try (Response response = client.target(coordinatorUrl).path(encodedLraId1 + "/close").request().header(LRA_API_VERSION_HEADER_NAME, version).put(null)) {
        // we've closed the LRA manually here, skipping the @After
        lrasToAfterFinish.clear();
        Assert.assertEquals("Closing top-level LRA should be successful, PUT/200 is expected.", Status.OK.getStatusCode(), response.getStatus());
        Assert.assertEquals("Closing top-level LRA should return the right status.", LRAStatus.Closed.name(), response.readEntity(String.class));
        Assert.assertEquals("Expecting to get the same API version as used for the request to close top-level LRA", version, response.getHeaderString(LRA_API_VERSION_HEADER_NAME));
    }
    Collection<LRAData> activeLRAsAfterClosing = lraClient.getAllLRAs().stream().filter(data -> data.getLraId().equals(lraId1) || data.getLraId().equals(lraId2)).filter(data -> data.getStatus() != LRAStatus.Closing && data.getStatus() != LRAStatus.Closed).collect(Collectors.toList());
    MatcherAssert.assertThat("Expecting the started LRAs are no more active after closing the top-level one", activeLRAsAfterClosing, emptyCollectionOf(LRAData.class));
}
Also used : Response(javax.ws.rs.core.Response) Arrays(java.util.Arrays) URLDecoder(java.net.URLDecoder) Logger(org.jboss.logging.Logger) RunWith(org.junit.runner.RunWith) TestBase(io.narayana.lra.arquillian.TestBase) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) IsCollectionContaining.hasItem(org.hamcrest.core.IsCollectionContaining.hasItem) LRAData(io.narayana.lra.LRAData) LRAConstants(io.narayana.lra.LRAConstants) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Matchers.emptyCollectionOf(org.hamcrest.Matchers.emptyCollectionOf) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) Parameterized(org.junit.runners.Parameterized) IsNot.not(org.hamcrest.core.IsNot.not) ArquillianParametrized(io.narayana.lra.arquillian.ArquillianParametrized) Collection(java.util.Collection) Test(org.junit.Test) Instant(java.time.Instant) Entity(javax.ws.rs.client.Entity) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) GenericType(javax.ws.rs.core.GenericType) URLEncoder(java.net.URLEncoder) List(java.util.List) LRAStatus(org.eclipse.microprofile.lra.annotation.LRAStatus) MatcherAssert(org.hamcrest.MatcherAssert) Rule(org.junit.Rule) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) IsCollectionContaining.hasItems(org.hamcrest.core.IsCollectionContaining.hasItems) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Assert(org.junit.Assert) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Matchers.containsString(org.hamcrest.Matchers.containsString) Link(javax.ws.rs.core.Link) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) LRAData(io.narayana.lra.LRAData) Test(org.junit.Test)

Example 12 with LRAData

use of io.narayana.lra.LRAData in project narayana by jbosstm.

the class CoordinatorApiIT method getLRAInfo.

/**
 * GET - /{lraId}
 * Obtaining info of a started LRA.
 */
@Test
public void getLRAInfo() throws UnsupportedEncodingException {
    URI lraId = lraClient.startLRA(testRule.getMethodName());
    lrasToAfterFinish.add(lraId);
    String encodedLraId = URLEncoder.encode(lraId.toString(), StandardCharsets.UTF_8.name());
    try (Response response = client.target(coordinatorUrl).path(encodedLraId).request().header(LRA_API_VERSION_HEADER_NAME, version).get()) {
        Assert.assertEquals("Expected that the get status call succeeds, GET/200.", Status.OK.getStatusCode(), response.getStatus());
        Assert.assertEquals("Expected API header to be returned with the version provided in request", version, response.getHeaderString(LRA_API_VERSION_HEADER_NAME));
        LRAData data = response.readEntity(new GenericType<LRAData>() {
        });
        Assert.assertEquals("Expected the returned LRA to be the one that was started by test", lraId, data.getLraId());
        Assert.assertEquals("Expected the returned LRA being Active", LRAStatus.Active, data.getStatus());
        Assert.assertTrue("Expected the returned LRA is top-level", data.isTopLevel());
        Assert.assertEquals("Expected the returned LRA get HTTP status as active, HTTP status 204.", Status.NO_CONTENT.getStatusCode(), data.getHttpStatus());
    }
}
Also used : Response(javax.ws.rs.core.Response) Matchers.containsString(org.hamcrest.Matchers.containsString) URI(java.net.URI) LRAData(io.narayana.lra.LRAData) Test(org.junit.Test)

Example 13 with LRAData

use of io.narayana.lra.LRAData in project narayana by jbosstm.

the class NarayanaLRAClientIT method testGetAllLRAs.

@Test
public void testGetAllLRAs() {
    URI lra = lraClient.startLRA("test-lra");
    lrasToAfterFinish.add(lra);
    List<LRAData> allLRAs = lraClient.getAllLRAs();
    Assert.assertTrue("Expected to find the LRA " + lra + " amongst all active ones: " + allLRAs, allLRAs.stream().anyMatch(lraData -> lraData.getLraId().equals(lra)));
    lraClient.closeLRA(lra);
    allLRAs = lraClient.getAllLRAs();
    Assert.assertTrue("LRA " + lra + " was closed but is still referred as active one at: " + allLRAs, allLRAs.stream().noneMatch(lraData -> lraData.getLraId().equals(lra)));
}
Also used : List(java.util.List) Rule(org.junit.Rule) LRAData(io.narayana.lra.LRAData) TestName(org.junit.rules.TestName) Logger(org.jboss.logging.Logger) Test(org.junit.Test) URI(java.net.URI) TestBase(io.narayana.lra.arquillian.TestBase) Assert(org.junit.Assert) URI(java.net.URI) LRAData(io.narayana.lra.LRAData) Test(org.junit.Test)

Aggregations

LRAData (io.narayana.lra.LRAData)13 URI (java.net.URI)12 Test (org.junit.Test)9 List (java.util.List)6 Response (javax.ws.rs.core.Response)6 LRAStatus (org.eclipse.microprofile.lra.annotation.LRAStatus)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 TestBase (io.narayana.lra.arquillian.TestBase)5 Logger (org.jboss.logging.Logger)5 Assert (org.junit.Assert)5 Rule (org.junit.Rule)5 LRAConstants (io.narayana.lra.LRAConstants)4 ArquillianParametrized (io.narayana.lra.arquillian.ArquillianParametrized)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URLDecoder (java.net.URLDecoder)4 URLEncoder (java.net.URLEncoder)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Instant (java.time.Instant)4 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4