|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 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 com.mongodb.internal.operation; |
| 18 | + |
| 19 | +import com.mongodb.MongoNamespace; |
| 20 | +import com.mongodb.ServerAddress; |
| 21 | +import com.mongodb.WriteConcern; |
| 22 | +import com.mongodb.client.model.Filters; |
| 23 | +import com.mongodb.client.model.bulk.ClientBulkWriteOptions; |
| 24 | +import com.mongodb.client.model.bulk.ClientBulkWriteResult; |
| 25 | +import com.mongodb.client.model.bulk.ClientNamespacedReplaceOneModel; |
| 26 | +import com.mongodb.client.model.bulk.ClientNamespacedWriteModel; |
| 27 | +import com.mongodb.connection.ClusterId; |
| 28 | +import com.mongodb.connection.ConnectionDescription; |
| 29 | +import com.mongodb.connection.ServerConnectionState; |
| 30 | +import com.mongodb.connection.ServerDescription; |
| 31 | +import com.mongodb.connection.ServerId; |
| 32 | +import com.mongodb.connection.ServerType; |
| 33 | +import com.mongodb.internal.binding.ConnectionSource; |
| 34 | +import com.mongodb.internal.binding.ReadWriteBinding; |
| 35 | +import com.mongodb.internal.client.model.bulk.AcknowledgedSummaryClientBulkWriteResult; |
| 36 | +import com.mongodb.internal.connection.Connection; |
| 37 | +import com.mongodb.internal.connection.DualMessageSequences; |
| 38 | +import com.mongodb.internal.connection.OperationContext; |
| 39 | +import com.mongodb.internal.mockito.MongoMockito; |
| 40 | +import org.bson.BsonBinaryWriter; |
| 41 | +import org.bson.BsonDocument; |
| 42 | +import org.bson.Document; |
| 43 | +import org.bson.codecs.Codec; |
| 44 | +import org.bson.codecs.DecoderContext; |
| 45 | +import org.bson.io.BasicOutputBuffer; |
| 46 | +import org.bson.json.JsonReader; |
| 47 | +import org.junit.jupiter.api.BeforeEach; |
| 48 | +import org.junit.jupiter.api.Test; |
| 49 | + |
| 50 | +import java.util.List; |
| 51 | + |
| 52 | +import static com.mongodb.ClusterFixture.OPERATION_CONTEXT; |
| 53 | +import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry; |
| 54 | +import static com.mongodb.client.model.bulk.ClientReplaceOneOptions.clientReplaceOneOptions; |
| 55 | +import static java.util.Collections.singletonList; |
| 56 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 57 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 58 | +import static org.mockito.ArgumentMatchers.any; |
| 59 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 60 | +import static org.mockito.ArgumentMatchers.anyString; |
| 61 | +import static org.mockito.ArgumentMatchers.isNull; |
| 62 | +import static org.mockito.Mockito.doAnswer; |
| 63 | +import static org.mockito.Mockito.doReturn; |
| 64 | + |
| 65 | +class ClientBulkWriteOperationTest { |
| 66 | + private static final MongoNamespace NAMESPACE = new MongoNamespace("testDb.testCol"); |
| 67 | + private Connection connection; |
| 68 | + private ConnectionSource connectionSource; |
| 69 | + private ReadWriteBinding binding; |
| 70 | + |
| 71 | + @BeforeEach |
| 72 | + void setUp() { |
| 73 | + connection = MongoMockito.mock(Connection.class); |
| 74 | + connectionSource = MongoMockito.mock(ConnectionSource.class); |
| 75 | + binding = MongoMockito.mock(ReadWriteBinding.class); |
| 76 | + |
| 77 | + doReturn(new ConnectionDescription(new ServerId(new ClusterId("test"), new ServerAddress()))).when(connection).getDescription(); |
| 78 | + doReturn(connection).when(connectionSource).getConnection(); |
| 79 | + doReturn(0).when(connectionSource).release(); |
| 80 | + doReturn(0).when(connection).release(); |
| 81 | + |
| 82 | + doReturn(ServerDescription.builder().address(new ServerAddress()) |
| 83 | + .state(ServerConnectionState.CONNECTED) |
| 84 | + .type(ServerType.STANDALONE) |
| 85 | + .build()).when(connectionSource).getServerDescription(); |
| 86 | + doReturn(OPERATION_CONTEXT).when(connectionSource).getOperationContext(); |
| 87 | + |
| 88 | + doReturn(connectionSource).when(binding).getWriteConnectionSource(); |
| 89 | + doReturn(OPERATION_CONTEXT).when(binding).getOperationContext(); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /** |
| 94 | + * This test exists due to SERVER-113344 bug. |
| 95 | + */ |
| 96 | + //TODO-JAVA-6002 |
| 97 | + @Test |
| 98 | + void shouldIgnoreSuccessfulCursorResultWhenVerboseResultIsFalse() { |
| 99 | + //given |
| 100 | + mockCommandExecutionResult( |
| 101 | + "{'cursor': {" |
| 102 | + + " 'id': NumberLong(0)," |
| 103 | + + " 'firstBatch': [ { 'ok': 1, 'idx': 0, 'n': 1, 'upserted': { '_id': 1 } } ]," |
| 104 | + + " 'ns': 'admin.$cmd.bulkWrite'" |
| 105 | + + "}," |
| 106 | + + " 'nErrors': 0," |
| 107 | + + " 'nInserted': 0," |
| 108 | + + " 'nMatched': 0," |
| 109 | + + " 'nModified': 0," |
| 110 | + + " 'nUpserted': 1," |
| 111 | + + " 'nDeleted': 0," |
| 112 | + + " 'ok': 1" |
| 113 | + + "}" |
| 114 | + ); |
| 115 | + ClientBulkWriteOptions options = ClientBulkWriteOptions.clientBulkWriteOptions() |
| 116 | + .ordered(false).verboseResults(false); |
| 117 | + List<ClientNamespacedReplaceOneModel> clientNamespacedReplaceOneModels = singletonList(ClientNamespacedWriteModel.replaceOne( |
| 118 | + NAMESPACE, |
| 119 | + Filters.empty(), |
| 120 | + new Document(), |
| 121 | + clientReplaceOneOptions().upsert(true) |
| 122 | + )); |
| 123 | + ClientBulkWriteOperation op = new ClientBulkWriteOperation( |
| 124 | + clientNamespacedReplaceOneModels, |
| 125 | + options, |
| 126 | + WriteConcern.ACKNOWLEDGED, |
| 127 | + false, |
| 128 | + getDefaultCodecRegistry()); |
| 129 | + //when |
| 130 | + ClientBulkWriteResult result = op.execute(binding); |
| 131 | + |
| 132 | + //then |
| 133 | + assertEquals( |
| 134 | + new AcknowledgedSummaryClientBulkWriteResult(0, 1, 0, 0, 0), |
| 135 | + result); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * This test exists due to SERVER-113026 bug. |
| 140 | + */ |
| 141 | + //TODO-JAVA-6005 |
| 142 | + @Test |
| 143 | + void shouldUseDefaultNumberOfModifiedDocumentsWhenMissingInCursor() { |
| 144 | + //given |
| 145 | + mockCommandExecutionResult("{" |
| 146 | + + " cursor: {" |
| 147 | + + " id: NumberLong(0)," |
| 148 | + + " firstBatch: [ {" |
| 149 | + + " 'ok': 1.0," |
| 150 | + + " 'idx': 0," |
| 151 | + + " 'n': 1," |
| 152 | + //nModified field is missing here |
| 153 | + + " 'upserted': {" |
| 154 | + + " '_id': 1" |
| 155 | + + " }" |
| 156 | + + " }]," |
| 157 | + + " ns: 'admin.$cmd.bulkWrite'" |
| 158 | + + " }," |
| 159 | + + " nErrors: 0," |
| 160 | + + " nInserted: 1," |
| 161 | + + " nMatched: 0," |
| 162 | + + " nModified: 0," |
| 163 | + + " nUpserted: 1," |
| 164 | + + " nDeleted: 0," |
| 165 | + + " ok: 1" |
| 166 | + + "}"); |
| 167 | + ClientBulkWriteOptions options = ClientBulkWriteOptions.clientBulkWriteOptions() |
| 168 | + .ordered(false).verboseResults(true); |
| 169 | + List<ClientNamespacedReplaceOneModel> clientNamespacedReplaceOneModels = singletonList(ClientNamespacedWriteModel.replaceOne( |
| 170 | + NAMESPACE, |
| 171 | + Filters.empty(), |
| 172 | + new Document(), |
| 173 | + clientReplaceOneOptions().upsert(true) |
| 174 | + )); |
| 175 | + ClientBulkWriteOperation op = new ClientBulkWriteOperation( |
| 176 | + clientNamespacedReplaceOneModels, |
| 177 | + options, |
| 178 | + WriteConcern.ACKNOWLEDGED, |
| 179 | + false, |
| 180 | + getDefaultCodecRegistry()); |
| 181 | + //when |
| 182 | + ClientBulkWriteResult result = op.execute(binding); |
| 183 | + |
| 184 | + //then |
| 185 | + assertEquals(1, result.getInsertedCount()); |
| 186 | + assertEquals(1, result.getUpsertedCount()); |
| 187 | + assertEquals(0, result.getMatchedCount()); |
| 188 | + assertEquals(0, result.getModifiedCount()); |
| 189 | + assertEquals(0, result.getDeletedCount()); |
| 190 | + assertTrue(result.getVerboseResults().isPresent()); |
| 191 | + } |
| 192 | + |
| 193 | + private void mockCommandExecutionResult(final String serverResponse) { |
| 194 | + doAnswer(invocationOnMock -> { |
| 195 | + DualMessageSequences dualMessageSequences = invocationOnMock.getArgument(7); |
| 196 | + dualMessageSequences.encodeDocuments(write -> { |
| 197 | + write.doAndGetBatchCount(new BsonBinaryWriter(new BasicOutputBuffer()), new BsonBinaryWriter(new BasicOutputBuffer())); |
| 198 | + return DualMessageSequences.WritersProviderAndLimitsChecker.WriteResult.OK_LIMIT_NOT_REACHED; |
| 199 | + }); |
| 200 | + return toBsonDocument(serverResponse); |
| 201 | + }).when(connection).command( |
| 202 | + anyString(), |
| 203 | + any(BsonDocument.class), |
| 204 | + any(), |
| 205 | + isNull(), |
| 206 | + any(), |
| 207 | + any(OperationContext.class), |
| 208 | + anyBoolean(), |
| 209 | + any(DualMessageSequences.class) |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + private static BsonDocument toBsonDocument(final String serverResponse) { |
| 214 | + Codec<BsonDocument> bsonDocumentCodec = |
| 215 | + CommandResultDocumentCodec.create(getDefaultCodecRegistry().get(BsonDocument.class), CommandBatchCursorHelper.FIRST_BATCH); |
| 216 | + return bsonDocumentCodec.decode(new JsonReader(serverResponse), DecoderContext.builder().build()); |
| 217 | + } |
| 218 | +} |
0 commit comments