Skip to content

Commit

Permalink
4.x: Fix and add test to database SE example
Browse files Browse the repository at this point in the history
Signed-off-by: tvallin <[email protected]>
  • Loading branch information
tvallin authored Dec 13, 2023
1 parent e492423 commit 50dd6c0
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ private void getPokemon(ServerRequest req, ServerResponse res) {
private void listPokemons(ServerRequest req, ServerResponse res) {
res.send(dbClient.execute()
.namedQuery("select-all")
.map(it -> it.as(JsonObject.class)));
.map(it -> it.as(JsonObject.class))
.toList());
}

/**
Expand Down
30 changes: 30 additions & 0 deletions examples/dbclient/jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.logging</groupId>
<artifactId>helidon-logging-jul</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.helidon.tracing</groupId>
<artifactId>helidon-tracing</artifactId>
Expand Down Expand Up @@ -127,6 +132,31 @@
<artifactId>helidon-metrics-system-meters</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver.testing.junit5</groupId>
<artifactId>helidon-webserver-testing-junit5</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 1 addition & 7 deletions examples/dbclient/jdbc/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ db:
# name prefix defaults to "db.pool." - if you have more than one client within a JVM, you may want to distinguish between them
name-prefix: "hikari."
health-check:
- type: "query"
type: "query"
statementName: "health-check"
services:
tracing:
Expand All @@ -68,12 +68,6 @@ db:
# would trace all delete statements
- statement-types: ["DELETE"]
metrics:
- type: METER
name-format: "db.meter.overall"
- type: METER
# meter per statement name (default name format)
- type: METER
name-format: "db.meter.%1$s"
- type: TIMER
errors: false
statement-names: ["select-.*"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (c) 2023 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.List;
import java.util.Map;

import io.helidon.config.Config;
import io.helidon.dbclient.DbClient;
import io.helidon.http.Status;
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 jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonBuilderFactory;
import jakarta.json.JsonObject;
import org.junit.jupiter.api.Test;

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

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

private final Http1Client client;

MainTest(Http1Client client) {
this.client = client;
}

@SetUpRoute
static void routing(HttpRouting.Builder routing) {
JdbcExampleMain.routing(routing, DbClient.create(Config.global().get("db")));
}

@Test
void testListAndDeleteAllPokemons() {
List<String> names = listAllPokemons();
assertThat(names.isEmpty(), is(true));

String endpoint = String.format("/db/%s/type/%s", "Raticate", 1);
ClientResponseTyped<String> response = client.post(endpoint).request(String.class);
assertThat(response.status(), is(Status.OK_200));

names = listAllPokemons();
assertThat(names.size(), is(1));
assertThat(names.getFirst(), is("Raticate"));

response = client.delete("/db").request(String.class);
assertThat(response.status(), is(Status.OK_200));

names = listAllPokemons();
assertThat(names.isEmpty(), is(true));
}

@Test
void testAddUpdateDeletePokemon() {
ClientResponseTyped<String> response;
ClientResponseTyped<JsonObject> jsonResponse;
JsonObject pokemon = JSON_FACTORY.createObjectBuilder()
.add("type", 1)
.add("name", "Raticate")
.build();

// Add new pokemon
response = client.put("/db").submit(pokemon, String.class);
assertThat(response.entity(), is("Inserted: 1 values"));

// 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"));

// Update pokemon
response = client.put("/db/Raticate/type/2").request(String.class);
assertThat(response.status(), is(Status.OK_200));

// 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"));

// Delete Pokemon
response = client.delete("/db/Raticate").request(String.class);
assertThat(response.status(), is(Status.OK_200));

// Verify pokemon is correctly deleted
response = client.get("/db/Raticate").request(String.class);
assertThat(response.status(), is(Status.NOT_FOUND_404));
}

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();
}
}
19 changes: 19 additions & 0 deletions examples/dbclient/jdbc/src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright (c) 2023 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.
#

db:
connection:
url: jdbc:h2:mem:test
4 changes: 2 additions & 2 deletions examples/dbclient/pokemons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ curl http://localhost:8080/db/pokemon/2
curl http://localhost:8080/db/pokemon/name/Squirtle
# Add a new Pokémon Rattata
curl -i -X POST -d '{"id":7,"name":"Rattata","idType":1}' http://localhost:8080/db/pokemon
curl -i -X POST -H 'Content-type: application/json' -d '{"id":7,"name":"Rattata","idType":1}' http://localhost:8080/db/pokemon
# Rename Pokémon with id 7 to Raticate
curl -i -X PUT -d '{"id":7,"name":"Raticate","idType":2}' http://localhost:8080/db/pokemon
curl -i -X PUT -H 'Content-type: application/json' -d '{"id":7,"name":"Raticate","idType":2}' http://localhost:8080/db/pokemon
# Delete Pokémon with id 7
curl -i -X DELETE http://localhost:8080/db/pokemon/7
Expand Down

0 comments on commit 50dd6c0

Please sign in to comment.