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