Skip to content

Commit

Permalink
* LRA test coordinator example
Browse files Browse the repository at this point in the history
* Run tests on random ports
  • Loading branch information
danielkec committed Dec 5, 2024
1 parent 363cb77 commit 9c112e4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 40 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;
}
}
2 changes: 0 additions & 2 deletions examples/microprofile/lra/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@
<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>
Expand Down

0 comments on commit 9c112e4

Please sign in to comment.