|
| 1 | +/* |
| 2 | + * Copyright 2022 NAVER Corp. |
| 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.navercorp.pinpoint.plugin.mongodb; |
| 18 | + |
| 19 | +import com.mongodb.WriteConcern; |
| 20 | +import com.mongodb.client.MongoCollection; |
| 21 | +import com.mongodb.client.MongoCursor; |
| 22 | +import com.mongodb.client.MongoDatabase; |
| 23 | +import com.mongodb.client.result.DeleteResult; |
| 24 | +import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedAnnotation; |
| 25 | +import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier; |
| 26 | +import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifierHolder; |
| 27 | +import com.navercorp.pinpoint.common.util.StringStringValue; |
| 28 | +import com.navercorp.pinpoint.plugin.mongo.MongoConstants; |
| 29 | +import com.navercorp.pinpoint.plugin.mongo.MongoUtil; |
| 30 | +import com.navercorp.pinpoint.plugin.mongo.NormalizedBson; |
| 31 | +import org.bson.BsonArray; |
| 32 | +import org.bson.BsonBinary; |
| 33 | +import org.bson.BsonBinarySubType; |
| 34 | +import org.bson.BsonBoolean; |
| 35 | +import org.bson.BsonDateTime; |
| 36 | +import org.bson.BsonDbPointer; |
| 37 | +import org.bson.BsonDecimal128; |
| 38 | +import org.bson.BsonDocument; |
| 39 | +import org.bson.BsonDouble; |
| 40 | +import org.bson.BsonInt32; |
| 41 | +import org.bson.BsonInt64; |
| 42 | +import org.bson.BsonJavaScript; |
| 43 | +import org.bson.BsonJavaScriptWithScope; |
| 44 | +import org.bson.BsonNull; |
| 45 | +import org.bson.BsonObjectId; |
| 46 | +import org.bson.BsonRegularExpression; |
| 47 | +import org.bson.BsonString; |
| 48 | +import org.bson.BsonSymbol; |
| 49 | +import org.bson.BsonTimestamp; |
| 50 | +import org.bson.BsonUndefined; |
| 51 | +import org.bson.BsonValue; |
| 52 | +import org.bson.Document; |
| 53 | +import org.bson.conversions.Bson; |
| 54 | +import org.bson.types.Decimal128; |
| 55 | +import org.bson.types.ObjectId; |
| 56 | +import org.junit.Assert; |
| 57 | + |
| 58 | +import java.lang.reflect.Method; |
| 59 | +import java.util.Arrays; |
| 60 | +import java.util.Date; |
| 61 | + |
| 62 | +import static com.mongodb.client.model.Filters.and; |
| 63 | +import static com.mongodb.client.model.Filters.eq; |
| 64 | +import static com.mongodb.client.model.Filters.exists; |
| 65 | +import static com.mongodb.client.model.Filters.nin; |
| 66 | +import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event; |
| 67 | + |
| 68 | +public class MongoDBITHelper { |
| 69 | + protected static final String MONGO_EXECUTE_QUERY = "MONGO_EXECUTE_QUERY"; |
| 70 | + |
| 71 | + public void testConnection30(MongoDBITBase mongoDBITBase, String address, final MongoDatabase database, Class<?> mongoDatabaseImplClass, String secondCollectionDefaultOption) throws Exception { |
| 72 | + PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); |
| 73 | + |
| 74 | + MongoCollection<Document> collection = database.getCollection("customers"); |
| 75 | + MongoCollection<Document> collection2 = database.getCollection("customers2").withWriteConcern(WriteConcern.ACKNOWLEDGED); |
| 76 | + |
| 77 | + insertComlexBsonValueData30(verifier, address, collection, mongoDatabaseImplClass, "customers", "MAJORITY"); |
| 78 | + insertData(verifier, address, collection, mongoDatabaseImplClass, "customers", "MAJORITY"); |
| 79 | + insertData(verifier, address, collection2, mongoDatabaseImplClass, "customers2", secondCollectionDefaultOption); |
| 80 | + updateData(verifier, address, collection, mongoDatabaseImplClass); |
| 81 | + readData(verifier, address, collection, mongoDatabaseImplClass); |
| 82 | + filterData(verifier, address, collection, mongoDatabaseImplClass); |
| 83 | + filterData2(verifier, address, collection, mongoDatabaseImplClass); |
| 84 | + deleteData(verifier, address, collection, mongoDatabaseImplClass); |
| 85 | + |
| 86 | + stopDB(collection); |
| 87 | + } |
| 88 | + |
| 89 | + public void testConnection34(MongoDBITBase mongoDBITBase, String address, final MongoDatabase database, Class<?> mongoDatabaseImplClass, String secondCollectionDefaultOption) throws Exception { |
| 90 | + PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance(); |
| 91 | + |
| 92 | + MongoCollection<Document> collection = database.getCollection("customers"); |
| 93 | + MongoCollection<Document> collection2 = database.getCollection("customers2").withWriteConcern(WriteConcern.ACKNOWLEDGED); |
| 94 | + |
| 95 | + insertComlexBsonValueData34(verifier, address, collection, mongoDatabaseImplClass, "customers", "MAJORITY"); |
| 96 | + insertData(verifier, address, collection, mongoDatabaseImplClass, "customers", "MAJORITY"); |
| 97 | + insertData(verifier, address, collection2, mongoDatabaseImplClass, "customers2", secondCollectionDefaultOption); |
| 98 | + updateData(verifier, address, collection, mongoDatabaseImplClass); |
| 99 | + readData(verifier, address, collection, mongoDatabaseImplClass); |
| 100 | + filterData(verifier, address, collection, mongoDatabaseImplClass); |
| 101 | + filterData2(verifier, address, collection, mongoDatabaseImplClass); |
| 102 | + deleteData(verifier, address, collection, mongoDatabaseImplClass); |
| 103 | + |
| 104 | + stopDB(collection); |
| 105 | + } |
| 106 | + |
| 107 | + public void stopDB(MongoCollection<Document> collection) throws Exception { |
| 108 | + try { |
| 109 | + collection.drop(); |
| 110 | + } catch (Exception ex) { |
| 111 | + throw new RuntimeException("drop() failure", ex); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void insertComlexBsonValueData30(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl, String collectionInfo, String collectionOption) { |
| 116 | + //insert Data |
| 117 | + Document document = createComplexDocument(); |
| 118 | + |
| 119 | + collection.insertOne(document); |
| 120 | + |
| 121 | + Method insertOneMethod = getMethod(mongoDatabaseImpl, "insertOne", Object.class); |
| 122 | + NormalizedBson parsedBson = parseBson(document); |
| 123 | + |
| 124 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, insertOneMethod, null, address, null |
| 125 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), collectionInfo) |
| 126 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), collectionOption) |
| 127 | + , new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter())))); |
| 128 | + } |
| 129 | + |
| 130 | + public NormalizedBson parseBson(Object... documents) { |
| 131 | + Object[] objects = Arrays.copyOf(documents, documents.length); |
| 132 | + return MongoUtil.parseBson(objects, true); |
| 133 | + } |
| 134 | + |
| 135 | + private Document createComplexDocument() { |
| 136 | + //insert Data |
| 137 | + BsonValue a = new BsonString("stest"); |
| 138 | + BsonValue b = new BsonDouble(111); |
| 139 | + BsonValue c = new BsonBoolean(true); |
| 140 | + |
| 141 | + Document document = new Document() |
| 142 | + .append("int32", new BsonInt32(12)) |
| 143 | + .append("int64", new BsonInt64(77L)) |
| 144 | + .append("bo\"olean", new BsonBoolean(true)) |
| 145 | + .append("date", new BsonDateTime(new Date().getTime())) |
| 146 | + .append("double", new BsonDouble(12.3)) |
| 147 | + .append("string", new BsonString("pinpoint")) |
| 148 | + .append("objectId", new BsonObjectId(new ObjectId())) |
| 149 | + .append("code", new BsonJavaScript("int i = 10;")) |
| 150 | + .append("codeWithScope", new BsonJavaScriptWithScope("int x = y", new BsonDocument("y", new BsonInt32(1)))) |
| 151 | + .append("regex", new BsonRegularExpression("^test.*regex.*xyz$", "big")) |
| 152 | + .append("symbol", new BsonSymbol("wow")) |
| 153 | + .append("timestamp", new BsonTimestamp(0x12345678, 5)) |
| 154 | + .append("undefined", new BsonUndefined()) |
| 155 | + .append("binary1", new BsonBinary(new byte[]{(byte) 0xe0, 0x4f, (byte) 0xd0, 0x20})) |
| 156 | + .append("oldBinary", new BsonBinary(BsonBinarySubType.OLD_BINARY, new byte[]{1, 1, 1, 1, 1})) |
| 157 | + .append("arrayInt", new BsonArray(Arrays.asList(a, b, c, new BsonInt32(7)))) |
| 158 | + .append("document", new BsonDocument("a", new BsonInt32(77))) |
| 159 | + .append("dbPointer", new BsonDbPointer("db.coll", new ObjectId())) |
| 160 | + .append("null", new BsonNull()); |
| 161 | + return document; |
| 162 | + } |
| 163 | + |
| 164 | + private Method getMethod(Class<?> mongoDatabaseImpl, String name, Class<?>... parameterTypes) { |
| 165 | + try { |
| 166 | + return mongoDatabaseImpl.getDeclaredMethod(name, parameterTypes); |
| 167 | + } catch (NoSuchMethodException ex) { |
| 168 | + throw new RuntimeException(ex); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + public void insertComlexBsonValueData34(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl, String collectionInfo, String collectionOption) { |
| 173 | + //insert Data |
| 174 | + Document document = createComplexDocument(); |
| 175 | + document.append("decimal128", new BsonDecimal128(new Decimal128(55))); |
| 176 | + |
| 177 | + collection.insertOne(document); |
| 178 | + |
| 179 | + Method insertOneMethod = getMethod(mongoDatabaseImpl, "insertOne", Object.class); |
| 180 | + NormalizedBson parsedBson = parseBson(document); |
| 181 | + |
| 182 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, insertOneMethod, null, address, null |
| 183 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), collectionInfo) |
| 184 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), collectionOption) |
| 185 | + , new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter())))); |
| 186 | + } |
| 187 | + |
| 188 | + public void insertData(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl, String collectionInfo, String collectionOption) { |
| 189 | + //insert Data |
| 190 | + Document doc = new Document("name", "Roy").append("company", "Naver"); |
| 191 | + collection.insertOne(doc); |
| 192 | + |
| 193 | + Method insertOneMethod = getMethod(mongoDatabaseImpl, "insertOne", Object.class); |
| 194 | + NormalizedBson parsedBson = parseBson(doc); |
| 195 | + |
| 196 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, insertOneMethod, null, address, null |
| 197 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), collectionInfo) |
| 198 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), collectionOption) |
| 199 | + , new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter())))); |
| 200 | + } |
| 201 | + |
| 202 | + public void updateData(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl) { |
| 203 | + //update Data |
| 204 | + Document doc = new Document("name", "Roy").append("company", "Naver"); |
| 205 | + Document doc2 = new Document("$set", new Document("name", "Roy3")); |
| 206 | + System.out.println("## Update Result=" + collection.updateOne(doc, doc2)); |
| 207 | + |
| 208 | + Method updateOne = getMethod(mongoDatabaseImpl, "updateOne", Bson.class, Bson.class); |
| 209 | + NormalizedBson parsedBson = parseBson(doc, doc2); |
| 210 | + |
| 211 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, updateOne, null, address, null |
| 212 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), "customers") |
| 213 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), "MAJORITY") |
| 214 | + , new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter())))); |
| 215 | + } |
| 216 | + |
| 217 | + |
| 218 | + public void readData(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl) { |
| 219 | + //read data |
| 220 | + MongoCursor<Document> cursor = collection.find().iterator(); |
| 221 | + |
| 222 | + Method find = getMethod(mongoDatabaseImpl, "find"); |
| 223 | + |
| 224 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, find, null, address, null |
| 225 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), "customers") |
| 226 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), "secondaryPreferred"))); |
| 227 | + |
| 228 | + assertResultSize("Unexpected read data",2, cursor); |
| 229 | + } |
| 230 | + |
| 231 | + private void assertResultSize(String message, int expected, MongoCursor<Document> cursor) { |
| 232 | + int resultCount = 0; |
| 233 | + try { |
| 234 | + while (cursor.hasNext()) { |
| 235 | + resultCount++; |
| 236 | + Document document = cursor.next(); |
| 237 | + System.out.println("## Result=" + document); |
| 238 | + } |
| 239 | + } finally { |
| 240 | + cursor.close(); |
| 241 | + } |
| 242 | + Assert.assertEquals(message, expected, resultCount); |
| 243 | + } |
| 244 | + |
| 245 | + public void deleteData(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl) { |
| 246 | + //delete data |
| 247 | + Document doc = new Document("name", "Roy3"); |
| 248 | + DeleteResult deleteResult = collection.deleteMany(doc); |
| 249 | + |
| 250 | + Method deleteMany = getMethod(mongoDatabaseImpl, "deleteMany", Bson.class); |
| 251 | + NormalizedBson parsedBson = parseBson(doc); |
| 252 | + |
| 253 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, deleteMany, null, address, null |
| 254 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), "customers") |
| 255 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), "MAJORITY") |
| 256 | + , new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter())))); |
| 257 | + |
| 258 | + Assert.assertEquals("unexcepted delete count",1, deleteResult.getDeletedCount()); |
| 259 | + } |
| 260 | + |
| 261 | + public void filterData(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl) { |
| 262 | + Method find = getMethod(mongoDatabaseImpl, "find", Bson.class); |
| 263 | + Bson bson = eq("name", "Roy3"); |
| 264 | + NormalizedBson parsedBson = parseBson(bson); |
| 265 | + |
| 266 | + MongoCursor<Document> cursor = collection.find(bson).iterator(); |
| 267 | + |
| 268 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, find, null, address, null |
| 269 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), "customers") |
| 270 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), "secondaryPreferred") |
| 271 | + , new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter())))); |
| 272 | + |
| 273 | + assertResultSize("Unexpected filter data",1, cursor); |
| 274 | + } |
| 275 | + |
| 276 | + public void filterData2(PluginTestVerifier verifier, String address, MongoCollection<Document> collection, Class<?> mongoDatabaseImpl) { |
| 277 | + Method find = getMethod(mongoDatabaseImpl, "find", Bson.class); |
| 278 | + Bson bson = and(exists("name"), nin("name", 5, 15)); |
| 279 | + NormalizedBson parsedBson = parseBson(bson); |
| 280 | + |
| 281 | + MongoCursor<Document> cursor = collection.find(bson).iterator(); |
| 282 | + |
| 283 | + verifier.verifyTrace(event(MONGO_EXECUTE_QUERY, find, null, address, null |
| 284 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_INFO.getName(), "customers") |
| 285 | + , new ExpectedAnnotation(MongoConstants.MONGO_COLLECTION_OPTION.getName(), "secondaryPreferred") |
| 286 | + , new ExpectedAnnotation(MongoConstants.MONGO_JSON_DATA.getName(), new StringStringValue(parsedBson.getNormalizedBson(), parsedBson.getParameter())))); |
| 287 | + |
| 288 | + assertResultSize("Unexpected filter data2",1, cursor); |
| 289 | + } |
| 290 | +} |
0 commit comments