|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.helidon.examples.dbclient.mongo; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import io.helidon.config.Config; |
| 23 | +import io.helidon.http.Status; |
| 24 | +import io.helidon.http.media.jsonb.JsonbSupport; |
| 25 | +import io.helidon.http.media.jsonp.JsonpSupport; |
| 26 | +import io.helidon.webclient.api.ClientResponseTyped; |
| 27 | +import io.helidon.webclient.api.WebClient; |
| 28 | +import io.helidon.webserver.WebServer; |
| 29 | + |
| 30 | +import jakarta.json.Json; |
| 31 | +import jakarta.json.JsonArray; |
| 32 | +import jakarta.json.JsonBuilderFactory; |
| 33 | +import jakarta.json.JsonObject; |
| 34 | +import org.junit.jupiter.api.AfterAll; |
| 35 | +import org.junit.jupiter.api.BeforeAll; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.testcontainers.containers.MongoDBContainer; |
| 38 | +import org.testcontainers.junit.jupiter.Container; |
| 39 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 40 | + |
| 41 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | +import static org.hamcrest.Matchers.is; |
| 43 | + |
| 44 | +@Testcontainers(disabledWithoutDocker = true) |
| 45 | +public class MainIT { |
| 46 | + |
| 47 | + @Container |
| 48 | + static final MongoDBContainer container = new MongoDBContainer("mongo") |
| 49 | + .withExposedPorts(27017); |
| 50 | + |
| 51 | + private static final JsonBuilderFactory JSON_FACTORY = Json.createBuilderFactory(Map.of()); |
| 52 | + private static final String CONNECTION_URL_KEY = "db.connection.url"; |
| 53 | + |
| 54 | + private static WebServer server; |
| 55 | + private static WebClient client; |
| 56 | + |
| 57 | + @BeforeAll |
| 58 | + static void beforeAll() { |
| 59 | + String url = String.format("mongodb://127.0.0.1:%s/pokemon", container.getMappedPort(27017)); |
| 60 | + System.setProperty(CONNECTION_URL_KEY, url); |
| 61 | + server = MongoDbExampleMain.setupServer(WebServer.builder()); |
| 62 | + client = WebClient.create(config -> config.baseUri("http://localhost:" + server.port()) |
| 63 | + .addMediaSupport(JsonbSupport.create(Config.create())) |
| 64 | + .addMediaSupport(JsonpSupport.create())); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterAll |
| 68 | + static void afterAll() { |
| 69 | + if (server != null && server.isRunning()) { |
| 70 | + server.stop(); |
| 71 | + } |
| 72 | + System.clearProperty(CONNECTION_URL_KEY); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void testListAndDeleteAllPokemons() { |
| 77 | + List<String> names = listAllPokemons(); |
| 78 | + assertThat(names.isEmpty(), is(true)); |
| 79 | + |
| 80 | + String endpoint = String.format("/db/%s/type/%s", "Raticate", 1); |
| 81 | + ClientResponseTyped<String> response = client.post(endpoint).request(String.class); |
| 82 | + assertThat(response.status(), is(Status.OK_200)); |
| 83 | + |
| 84 | + names = listAllPokemons(); |
| 85 | + assertThat(names.size(), is(1)); |
| 86 | + assertThat(names.getFirst(), is("Raticate")); |
| 87 | + |
| 88 | + response = client.delete("/db").request(String.class); |
| 89 | + assertThat(response.status(), is(Status.OK_200)); |
| 90 | + |
| 91 | + names = listAllPokemons(); |
| 92 | + assertThat(names.isEmpty(), is(true)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testAddUpdateDeletePokemon() { |
| 97 | + ClientResponseTyped<String> response; |
| 98 | + ClientResponseTyped<JsonObject> jsonResponse; |
| 99 | + JsonObject pokemon = JSON_FACTORY.createObjectBuilder() |
| 100 | + .add("type", 1) |
| 101 | + .add("name", "Raticate") |
| 102 | + .build(); |
| 103 | + |
| 104 | + // Add new pokemon |
| 105 | + response = client.put("/db").submit(pokemon, String.class); |
| 106 | + assertThat(response.entity(), is("Inserted: 1 values")); |
| 107 | + |
| 108 | + // Get the new pokemon added |
| 109 | + jsonResponse = client.get("/db/Raticate").request(JsonObject.class); |
| 110 | + assertThat(jsonResponse.status(), is(Status.OK_200)); |
| 111 | + assertThat(jsonResponse.entity().getString("_id"), is("Raticate")); |
| 112 | + assertThat(jsonResponse.entity().getString("type"), is("1")); |
| 113 | + |
| 114 | + // Update pokemon |
| 115 | + response = client.put("/db/Raticate/type/2").request(String.class); |
| 116 | + assertThat(response.status(), is(Status.OK_200)); |
| 117 | + |
| 118 | + // Verify updated pokemon |
| 119 | + jsonResponse = client.get("/db/Raticate").request(JsonObject.class); |
| 120 | + assertThat(jsonResponse.status(), is(Status.OK_200)); |
| 121 | + assertThat(jsonResponse.entity().getString("_id"), is("Raticate")); |
| 122 | + assertThat(jsonResponse.entity().getString("type"), is("2")); |
| 123 | + |
| 124 | + // Delete Pokemon |
| 125 | + response = client.delete("/db/Raticate").request(String.class); |
| 126 | + assertThat(response.status(), is(Status.OK_200)); |
| 127 | + |
| 128 | + // Verify pokemon is correctly deleted |
| 129 | + response = client.get("/db/Raticate").request(String.class); |
| 130 | + assertThat(response.status(), is(Status.NOT_FOUND_404)); |
| 131 | + } |
| 132 | + |
| 133 | + private List<String> listAllPokemons() { |
| 134 | + ClientResponseTyped<JsonArray> response = client.get("/db").request(JsonArray.class); |
| 135 | + assertThat(response.status(), is(Status.OK_200)); |
| 136 | + return response.entity().stream().map(e -> e.asJsonObject().getString("_id")).toList(); |
| 137 | + } |
| 138 | +} |
0 commit comments