Skip to content

Commit

Permalink
LRA testing feature #8469
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkec committed Aug 8, 2024
1 parent a1025a8 commit ff7a406
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/microprofile/lra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.lra</groupId>
<artifactId>helidon-microprofile-lra-testing</artifactId>
<scope>test</scope>
<!-- TODO: REMOVE ME!!! -->
<version>4.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.helidon.microprofile.example.lra;

import java.time.Duration;

import io.helidon.lra.coordinator.Lra;
import io.helidon.microprofile.testing.junit5.AddBean;
import io.helidon.microprofile.testing.junit5.HelidonTest;
import io.helidon.microprofile.testing.lra.TestLraCoordinator;

import jakarta.inject.Inject;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.eclipse.microprofile.lra.annotation.LRAStatus;
import org.eclipse.microprofile.lra.annotation.ws.rs.LRA;
import org.junit.jupiter.api.Test;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@HelidonTest
@AddBean(TestLraCoordinator.class)
public class LRAExampleResourceTest {

@Inject
private TestLraCoordinator coordinator;

@Inject
private WebTarget target;

@Test
public void testComplete() {
try (Response res = target
.path("/example/start-example")
.request()
.put(Entity.entity("lra rocks!", 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));
}
}

@Test
public void testCompensation() {
try (Response res = target
.path("/example/start-example")
.request()
.put(Entity.entity("BOOM", MediaType.TEXT_PLAIN_TYPE))) {
assertThat(res.getStatus(), is(500));
String lraId = res.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER);
Lra lra = coordinator.lra(lraId);
assertThat(lra.status(), is(LRAStatus.Cancelled));
}
}

@Test
public void testTimeout() {
try (Response res = target
.path("/example/start-example")
.request()
.put(Entity.entity("TIMEOUT", MediaType.TEXT_PLAIN_TYPE))) {
assertThat(res.getStatus(), is(200));
String lraId = res.getHeaderString(LRA.LRA_HTTP_CONTEXT_HEADER);
Lra lra = coordinator.lra(lraId);
await()
.atMost(Duration.ofSeconds(15))
.until(() -> lra.status() == LRAStatus.Cancelled);
assertThat(lra.status(), is(LRAStatus.Cancelled));
}
}
}

0 comments on commit ff7a406

Please sign in to comment.