Package io.helidon.microprofile.testing.lra


package io.helidon.microprofile.testing.lra
Test LRA coordinator. Allows simplified testing of LRA enabled JAX-RS resources.

 @HelidonTest
 @AddBean(TestLraCoordinator.class)
 @Path("/test")
 public class LraTest {

     private final WebTarget target;
     private final Set<String> completedLras;
     private final Set<String> cancelledLras;
     private final TestLraCoordinator coordinator;

     @Inject
     public LraTest(WebTarget target,
                    TestLraCoordinator coordinator) {
         this.target = target;
         this.coordinator = coordinator;
         this.completedLras = new CopyOnWriteArraySet<>();
         this.cancelledLras = new CopyOnWriteArraySet<>();
     }

     @PUT
     @Path("/withdraw")
     @LRA(LRA.Type.REQUIRES_NEW)
     public Response withdraw(@HeaderParam(LRA.LRA_HTTP_CONTEXT_HEADER) Optional<URI> lraId, String content) {
         ...
         return Response.ok().build();
     }

     @Complete
     public void complete(URI lraId) {
         completedLras.add(lraId.toString());
     }

     @Compensate
     public void rollback(URI lraId) {
         cancelledLras.add(lraId.toString());
     }

     @Test
     public void testLra() {
         try (Response res = target
                 .path("/test/withdraw")
                 .request()
                 .put(Entity.entity("test", MediaType.TEXT_PLAIN_TYPE))) {
             assertThat(res.getStatus(), is(200));
             String lraId = res.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER);
             Lra lra = coordinator.lra(lraId);
             assertThat(lra.status(), is(LRAStatus.Closed));
             assertThat(completedLras, contains(lraId));
         }
     }
 }