|
1 | 1 | /* |
2 | | - * Copyright (c) 2021, 2023 Oracle and/or its affiliates. |
| 2 | + * Copyright (c) 2021, 2024 Oracle and/or its affiliates. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 | package io.helidon.dbclient.mongodb; |
17 | 17 |
|
18 | 18 | import java.lang.System.Logger.Level; |
| 19 | +import java.util.concurrent.CompletableFuture; |
| 20 | +import java.util.concurrent.CompletionStage; |
| 21 | +import java.util.function.Consumer; |
19 | 22 |
|
| 23 | +import io.helidon.dbclient.DbClientService; |
| 24 | +import io.helidon.dbclient.DbClientServiceContext; |
20 | 25 | import io.helidon.dbclient.DbExecute; |
21 | 26 |
|
22 | 27 | import com.mongodb.client.MongoClient; |
23 | 28 | import com.mongodb.client.MongoCollection; |
24 | 29 | import com.mongodb.client.MongoDatabase; |
25 | 30 |
|
| 31 | +import org.bson.Document; |
26 | 32 | import org.junit.jupiter.api.BeforeAll; |
27 | 33 | import org.junit.jupiter.api.Test; |
28 | 34 | import org.mockito.Mockito; |
29 | 35 |
|
30 | 36 | import static org.hamcrest.MatcherAssert.assertThat; |
31 | 37 | import static org.hamcrest.Matchers.notNullValue; |
| 38 | +import static org.hamcrest.Matchers.is; |
32 | 39 | import static org.junit.jupiter.api.Assertions.fail; |
33 | 40 | import static org.mockito.ArgumentMatchers.any; |
34 | 41 | import static org.mockito.Mockito.when; |
35 | 42 |
|
36 | | -public class MongoDbClientTest { |
| 43 | +@SuppressWarnings("resource") |
| 44 | +class MongoDbClientTest { |
37 | 45 |
|
38 | 46 | private static final System.Logger LOGGER = System.getLogger(MongoDbClientTest.class.getName()); |
39 | 47 | private static MongoDbClient dbClient; |
40 | 48 |
|
41 | 49 | @BeforeAll |
42 | 50 | static void setup() { |
43 | | - MongoClient client = Mockito.mock(MongoClient.class); |
44 | | - MongoDatabase db = Mockito.mock(MongoDatabase.class); |
45 | | - when(db.runCommand(any())).thenReturn(MongoDbStatement.EMPTY); |
46 | | - dbClient = new MongoDbClient(new MongoDbClientBuilder(), client, db); |
| 51 | + dbClient = createClient(null); |
47 | 52 | } |
48 | 53 |
|
49 | 54 | @Test |
@@ -85,4 +90,70 @@ void testUnsupportedUnwrapExecutorClass() { |
85 | 90 | exec.query("{\"operation\": \"command\", \"query\": { ping: 1 }}"); |
86 | 91 | } |
87 | 92 |
|
| 93 | + @Test |
| 94 | + void testDbClientServiceQuery() { |
| 95 | + TestDbClientService service = new TestDbClientService(); |
| 96 | + MongoDbClient dbClient = createClient(builder -> builder.addService(service)); |
| 97 | + DbExecute exec = dbClient.execute(); |
| 98 | + long ignored = exec.query("{\"operation\": \"command\", \"query\": { ping: 1 }}").count(); |
| 99 | + assertThat(service.resultFuture.isDone(), is(true)); |
| 100 | + assertThat(service.resultFuture.isCompletedExceptionally(), is(false)); |
| 101 | + assertThat(service.statementFuture.isDone(), is(true)); |
| 102 | + assertThat(service.statementFuture.isCompletedExceptionally(), is(false)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testDbClientServiceDml() { |
| 107 | + TestDbClientService service = new TestDbClientService(); |
| 108 | + MongoDbClient dbClient = createClient(builder -> builder.addService(service)); |
| 109 | + DbExecute exec = dbClient.execute(); |
| 110 | + long ignored = exec.insert("{" |
| 111 | + + "\"collection\": \"foo\"," |
| 112 | + + "\"operation\": \"insert\"," |
| 113 | + + "\"value\": { \"name\": \"bar\" }" |
| 114 | + + "}"); |
| 115 | + assertThat(service.resultFuture.isDone(), is(true)); |
| 116 | + assertThat(service.resultFuture.isCompletedExceptionally(), is(false)); |
| 117 | + assertThat(service.statementFuture.isDone(), is(true)); |
| 118 | + assertThat(service.statementFuture.isCompletedExceptionally(), is(false)); |
| 119 | + } |
| 120 | + |
| 121 | + @SuppressWarnings("unchecked") |
| 122 | + static MongoDbClient createClient(Consumer<MongoDbClientBuilder> consumer) { |
| 123 | + MongoClient client = Mockito.mock(MongoClient.class); |
| 124 | + MongoDatabase db = Mockito.mock(MongoDatabase.class); |
| 125 | + MongoCollection<Document> collection = Mockito.mock(MongoCollection.class); |
| 126 | + when(db.getCollection(any())).thenReturn(collection); |
| 127 | + when(db.runCommand(any())).thenReturn(MongoDbStatement.EMPTY); |
| 128 | + MongoDbClientBuilder builder = new MongoDbClientBuilder(); |
| 129 | + if (consumer != null) { |
| 130 | + consumer.accept(builder); |
| 131 | + } |
| 132 | + return new MongoDbClient(builder, client, db); |
| 133 | + } |
| 134 | + |
| 135 | + record TestDbClientService(CompletableFuture<Long> resultFuture, |
| 136 | + CompletableFuture<Void> statementFuture) implements DbClientService { |
| 137 | + |
| 138 | + TestDbClientService() { |
| 139 | + this(new CompletableFuture<>(), new CompletableFuture<>()); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public DbClientServiceContext statement(DbClientServiceContext context) { |
| 144 | + setup(context.resultFuture(), resultFuture); |
| 145 | + setup(context.statementFuture(), statementFuture); |
| 146 | + return context; |
| 147 | + } |
| 148 | + |
| 149 | + static <T> void setup(CompletionStage<T> stage, CompletableFuture<T> future) { |
| 150 | + stage.whenComplete((v, ex) -> { |
| 151 | + if (ex != null) { |
| 152 | + future.completeExceptionally(ex); |
| 153 | + } else { |
| 154 | + future.complete(v); |
| 155 | + } |
| 156 | + }); |
| 157 | + } |
| 158 | + } |
88 | 159 | } |
0 commit comments