From 9c112e4adc4a68d37edb82de3fe3f353d95693be Mon Sep 17 00:00:00 2001 From: Daniel Kec Date: Thu, 5 Dec 2024 15:53:55 +0100 Subject: [PATCH] * LRA test coordinator example * Run tests on random ports --- examples/integrations/cdi/pokemons/pom.xml | 10 ++++ .../src/main/resources/logging.properties | 27 ++++++++++ .../integrations/cdi/pokemon/MainTest.java | 54 ++++++------------- examples/microprofile/lra/pom.xml | 2 - 4 files changed, 53 insertions(+), 40 deletions(-) create mode 100644 examples/integrations/cdi/pokemons/src/main/resources/logging.properties diff --git a/examples/integrations/cdi/pokemons/pom.xml b/examples/integrations/cdi/pokemons/pom.xml index b4b12ffaf..3fbd0445d 100644 --- a/examples/integrations/cdi/pokemons/pom.xml +++ b/examples/integrations/cdi/pokemons/pom.xml @@ -62,6 +62,11 @@ jakarta.transaction jakarta.transaction-api + + io.helidon.logging + helidon-logging-jul + runtime + @@ -133,6 +138,11 @@ + + io.helidon.microprofile.testing + helidon-microprofile-testing-junit5 + test + org.junit.jupiter junit-jupiter-api diff --git a/examples/integrations/cdi/pokemons/src/main/resources/logging.properties b/examples/integrations/cdi/pokemons/src/main/resources/logging.properties new file mode 100644 index 000000000..63d9d8f95 --- /dev/null +++ b/examples/integrations/cdi/pokemons/src/main/resources/logging.properties @@ -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 \ No newline at end of file diff --git a/examples/integrations/cdi/pokemons/src/test/java/io/helidon/examples/integrations/cdi/pokemon/MainTest.java b/examples/integrations/cdi/pokemons/src/test/java/io/helidon/examples/integrations/cdi/pokemon/MainTest.java index ec72ed297..f344b6faf 100644 --- a/examples/integrations/cdi/pokemons/src/test/java/io/helidon/examples/integrations/cdi/pokemon/MainTest.java +++ b/examples/integrations/cdi/pokemons/src/test/java/io/helidon/examples/integrations/cdi/pokemon/MainTest.java @@ -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 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)); @@ -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; - } } diff --git a/examples/microprofile/lra/pom.xml b/examples/microprofile/lra/pom.xml index 4c3405383..5a8a0ca4f 100644 --- a/examples/microprofile/lra/pom.xml +++ b/examples/microprofile/lra/pom.xml @@ -69,8 +69,6 @@ io.helidon.microprofile.lra helidon-microprofile-lra-testing test - - 4.1.0-SNAPSHOT org.hamcrest