Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Improve examples test coverage #8338

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,16 @@
<artifactId>mongodb</artifactId>
<version>${version.lib.testcontainers}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>${version.lib.testcontainers}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId>
<version>${version.lib.testcontainers}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-kahadb-store</artifactId>
Expand Down
38 changes: 37 additions & 1 deletion examples/dbclient/jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,26 @@
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.integrations.db</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
Expand Down Expand Up @@ -170,6 +189,23 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemProperties>
<oracle.jdbc.timezoneAsRegion>false</oracle.jdbc.timezoneAsRegion>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2023 Oracle and/or its affiliates.
* Copyright (c) 2019, 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.
Expand All @@ -21,7 +21,7 @@
import io.helidon.dbclient.health.DbClientHealthCheck;
import io.helidon.logging.common.LogConfig;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.http.HttpRouting;
import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.observe.ObserveFeature;
import io.helidon.webserver.observe.health.HealthObserver;

Expand All @@ -45,6 +45,13 @@ public static void main(String[] args) {
// load logging configuration
LogConfig.configureRuntime();

// Prepare routing for the server
WebServer server = setupServer(WebServer.builder());

System.out.println("WEB server is up! http://localhost:" + server.port() + "/");
}

static WebServer setupServer(WebServerConfig.Builder builder) {
// By default, this will pick up application.yaml from the classpath
Config config = Config.global();

Expand All @@ -54,22 +61,15 @@ public static void main(String[] args) {
ObserveFeature observe = ObserveFeature.builder()
.config(config.get("server.features.observe"))
.addObserver(HealthObserver.builder()
.addCheck(DbClientHealthCheck.create(dbClient, dbConfig.get("health-check")))
.build())
.addCheck(DbClientHealthCheck.create(dbClient, dbConfig.get("health-check")))
.build())
.build();

// Prepare routing for the server
WebServer server = WebServer.builder()
return builder
.config(config.get("server"))
.addFeature(observe)
.routing(routing -> routing(routing, dbClient))
.routing(routing -> routing.register("/db", new PokemonService(dbClient)))
.build()
.start();

System.out.println("WEB server is up! http://localhost:" + server.port() + "/");
}

static void routing(HttpRouting.Builder routing, DbClient dbClient) {
routing.register("/db", new PokemonService(dbClient));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 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.
Expand All @@ -19,14 +19,11 @@
import java.util.List;
import java.util.Map;

import io.helidon.config.Config;
import io.helidon.dbclient.DbClient;
import io.helidon.http.Status;
import io.helidon.http.media.jsonp.JsonpSupport;
import io.helidon.webclient.api.ClientResponseTyped;
import io.helidon.webclient.http1.Http1Client;
import io.helidon.webserver.http.HttpRouting;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;
import io.helidon.webclient.api.WebClient;
import io.helidon.webserver.WebServer;

import jakarta.json.Json;
import jakarta.json.JsonArray;
Expand All @@ -37,19 +34,22 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

@ServerTest
public class MainTest {
abstract class AbstractPokemonServiceTest {
private static final JsonBuilderFactory JSON_FACTORY = Json.createBuilderFactory(Map.of());

private final Http1Client client;
private static WebServer server;
private static WebClient client;

MainTest(Http1Client client) {
this.client = client;
static void beforeAll() {
server = JdbcExampleMain.setupServer(WebServer.builder());
client = WebClient.create(config -> config.baseUri("http://localhost:" + server.port())
.addMediaSupport(JsonpSupport.create()));
}

@SetUpRoute
static void routing(HttpRouting.Builder routing) {
JdbcExampleMain.routing(routing, DbClient.create(Config.global().get("db")));
static void afterAll() {
if (server != null && server.isRunning()) {
server.stop();
}
}

@Test
Expand Down Expand Up @@ -88,8 +88,8 @@ void testAddUpdateDeletePokemon() {
// Get the new pokemon added
jsonResponse = client.get("/db/Raticate").request(JsonObject.class);
assertThat(jsonResponse.status(), is(Status.OK_200));
assertThat(jsonResponse.entity().getString("NAME"), is("Raticate"));
assertThat(jsonResponse.entity().getString("TYPE"), is("1"));
assertThat(getName(jsonResponse.entity()), is("Raticate"));
assertThat(getType(jsonResponse.entity()), is("1"));

// Update pokemon
response = client.put("/db/Raticate/type/2").request(String.class);
Expand All @@ -98,8 +98,8 @@ void testAddUpdateDeletePokemon() {
// Verify updated pokemon
jsonResponse = client.get("/db/Raticate").request(JsonObject.class);
assertThat(jsonResponse.status(), is(Status.OK_200));
assertThat(jsonResponse.entity().getString("NAME"), is("Raticate"));
assertThat(jsonResponse.entity().getString("TYPE"), is("2"));
assertThat(getName(jsonResponse.entity()), is("Raticate"));
assertThat(getType(jsonResponse.entity()), is("2"));

// Delete Pokemon
response = client.delete("/db/Raticate").request(String.class);
Expand All @@ -113,6 +113,18 @@ void testAddUpdateDeletePokemon() {
private List<String> listAllPokemons() {
ClientResponseTyped<JsonArray> response = client.get("/db").request(JsonArray.class);
assertThat(response.status(), is(Status.OK_200));
return response.entity().stream().map(e -> e.asJsonObject().getString("NAME")).toList();
return response.entity().stream().map(e -> getName(e.asJsonObject())).toList();
}

private String getName(JsonObject json) {
return json.containsKey("name")
? json.getString("name")
: json.getString("NAME");
}

private String getType(JsonObject json) {
return json.containsKey("type")
? json.getString("type")
: json.getString("TYPE");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2023, 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.examples.dbclient.jdbc;

import java.util.Map;

import io.helidon.config.Config;
import io.helidon.config.ConfigSources;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static io.helidon.config.ConfigSources.classpath;

@Testcontainers(disabledWithoutDocker = true)
class PokemonServiceH2IT extends AbstractPokemonServiceTest {
private static final DockerImageName H2_IMAGE = DockerImageName.parse("nemerosa/h2");

@Container
static GenericContainer<?> container = new GenericContainer<>(H2_IMAGE)
.withExposedPorts(9082)
.waitingFor(Wait.forLogMessage("(.*)Web Console server running at(.*)", 1));

@BeforeAll
static void start() {
String url = String.format("jdbc:h2:tcp://localhost:%s/~./test", container.getMappedPort(9082));
Config.global(Config.builder()
.addSource(ConfigSources.create(Map.of("db.connection.url", url)))
.addSource(classpath("application-h2-test.yaml"))
.build());
beforeAll();
}

@AfterAll
static void stop() {
afterAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023, 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.examples.dbclient.jdbc;

import java.util.Map;

import io.helidon.config.Config;
import io.helidon.config.ConfigSources;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static io.helidon.config.ConfigSources.classpath;

@Testcontainers(disabledWithoutDocker = true)
class PokemonServiceMySQLIT extends AbstractPokemonServiceTest {

@Container
static MySQLContainer<?> container = new MySQLContainer<>("mysql:8.0.36")
.withUsername("user")
.withPassword("password")
.withNetworkAliases("mysql")
.withDatabaseName("pokemon");

@BeforeAll
static void start() {
Config.global(Config.builder()
.addSource(ConfigSources.create(Map.of("db.connection.url", container.getJdbcUrl())))
.addSource(classpath("application-mysql-test.yaml"))
.build());
beforeAll();
}

@AfterAll
static void stop() {
afterAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.examples.dbclient.jdbc;

import java.util.Map;

import io.helidon.config.Config;
import io.helidon.config.ConfigSources;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.OracleContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import static io.helidon.config.ConfigSources.classpath;

@Testcontainers(disabledWithoutDocker = true)
public class PokemonServiceOracleIT extends AbstractPokemonServiceTest {

private static final DockerImageName image = DockerImageName.parse("wnameless/oracle-xe-11g-r2")
.asCompatibleSubstituteFor("gvenzl/oracle-xe");

@Container
static OracleContainer container = new OracleContainer(image)
.withExposedPorts(1521, 8080)
.withDatabaseName("XE")
.usingSid()
.waitingFor(Wait.forListeningPorts(1521, 8080));

@BeforeAll
static void start() {
Config.global(Config.builder()
.addSource(ConfigSources.create(Map.of("db.connection.url", container.getJdbcUrl())))
.addSource(classpath("application-oracle-test.yaml"))
.build());
beforeAll();
}

@AfterAll
static void stop() {
afterAll();
}
}
Loading
Loading