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));
}
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());
}
}
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)));
}
Aggregations