Skip to content

Commit

Permalink
LRA testing feature #8469 (#74)
Browse files Browse the repository at this point in the history
* LRA test coordinator example
* Run tests on random ports
  • Loading branch information
danielkec authored Dec 5, 2024
1 parent 1d4a9d7 commit abc7e95
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 38 deletions.
10 changes: 10 additions & 0 deletions examples/integrations/cdi/pokemons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.logging</groupId>
<artifactId>helidon-logging-jul</artifactId>
<scope>runtime</scope>
</dependency>


<!-- Runtime-scoped dependencies. -->
Expand Down Expand Up @@ -133,6 +138,11 @@
</dependency>

<!-- Test-scoped dependencies. -->
<dependency>
<groupId>io.helidon.microprofile.testing</groupId>
<artifactId>helidon-microprofile-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright (c) 2024 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Example Logging Configuration File
# For more information see $JAVA_HOME/jre/lib/logging.properties

# Send messages to the console
handlers=io.helidon.logging.jul.HelidonConsoleHandler

# HelidonConsoleHandler uses a SimpleFormatter subclass that replaces "!thread!" with the current thread
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n

# Global logging level. Can be overridden by specific loggers
.level=INFO
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,45 @@

package io.helidon.examples.integrations.cdi.pokemon;

import io.helidon.microprofile.server.Server;
import io.helidon.microprofile.testing.junit5.HelidonTest;

import jakarta.enterprise.inject.se.SeContainer;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.json.JsonArray;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
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.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@HelidonTest
class MainTest {

private static Server server;
private static Client client;

@BeforeAll
public static void startTheServer() {
client = ClientBuilder.newClient();
server = Server.create().start();
}

@AfterAll
static void destroyClass() {
CDI<Object> current = CDI.current();
((SeContainer) current).close();
}

@Test
void testPokemonTypes() {
JsonArray types = client.target(getConnectionString("/type"))
void testPokemonTypes(WebTarget client) {
JsonArray types = client.path("/type")
.request()
.get(JsonArray.class);
assertThat(types.size(), is(18));
}

@Test
void testPokemon() {
assertThat(getPokemonCount(), is(6));
void testPokemon(WebTarget client) {
assertThat(getPokemonCount(client), is(6));

Pokemon pokemon = client.target(getConnectionString("/pokemon/1"))
Pokemon pokemon = client.path("/pokemon/1")
.request()
.get(Pokemon.class);
assertThat(pokemon.getName(), is("Bulbasaur"));

pokemon = client.target(getConnectionString("/pokemon/name/Charmander"))
pokemon = client.path("/pokemon/name/Charmander")
.request()
.get(Pokemon.class);
assertThat(pokemon.getType(), is(10));

try (Response response = client.target(getConnectionString("/pokemon/1"))
try (Response response = client.path("/pokemon/1")
.request()
.get()) {
assertThat(response.getStatus(), is(200));
Expand All @@ -82,29 +64,25 @@ void testPokemon() {
test.setType(1);
test.setId(100);
test.setName("Test");
try (Response response = client.target(getConnectionString("/pokemon"))
try (Response response = client.path("/pokemon")
.request()
.post(Entity.entity(test, MediaType.APPLICATION_JSON))) {
assertThat(response.getStatus(), is(204));
assertThat(getPokemonCount(), is(7));
assertThat(getPokemonCount(client), is(7));
}

try (Response response = client.target(getConnectionString("/pokemon/100"))
try (Response response = client.path("/pokemon/100")
.request()
.delete()) {
assertThat(response.getStatus(), is(204));
assertThat(getPokemonCount(), is(6));
assertThat(getPokemonCount(client), is(6));
}
}

private int getPokemonCount() {
JsonArray pokemons = client.target(getConnectionString("/pokemon"))
private int getPokemonCount(WebTarget client) {
JsonArray pokemons = client.path("/pokemon")
.request()
.get(JsonArray.class);
return pokemons.size();
}

private String getConnectionString(String path) {
return "http://localhost:" + server.port() + path;
}
}
31 changes: 31 additions & 0 deletions examples/microprofile/lra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
Microprofile Long Running Actions Example
</description>

<properties>
<version.lib.awaitility>4.2.0</version.lib.awaitility>
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
Expand All @@ -59,6 +63,33 @@
<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>
</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>${version.lib.awaitility}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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 abc7e95

Please sign in to comment.