-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert mongo-common tests from groovy to java (#12382)
- Loading branch information
Showing
22 changed files
with
1,947 additions
and
1,555 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
instrumentation/mongo/mongo-3.1/javaagent/src/test/groovy/MongoClientTest.groovy
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
.../src/test/java/io/opentelemetry/javaagent/instrumentation/mongo/v3_1/MongoClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.mongo.v3_1; | ||
|
||
import com.mongodb.MongoClientOptions; | ||
import io.opentelemetry.instrumentation.mongo.v3_1.AbstractMongo31ClientTest; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class MongoClientTest extends AbstractMongo31ClientTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected void configureMongoClientOptions(MongoClientOptions.Builder options) {} | ||
|
||
@Override | ||
protected InstrumentationExtension testing() { | ||
return testing; | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
...ibrary/src/test/groovy/io/opentelemetry/instrumentation/mongo/v3_1/MongoClientTest.groovy
This file was deleted.
Oops, something went wrong.
88 changes: 0 additions & 88 deletions
88
...est/groovy/io/opentelemetry/instrumentation/mongo/v3_1/MongoDbAttributesGetterTest.groovy
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
...test/groovy/io/opentelemetry/instrumentation/mongo/v3_1/MongoSpanNameExtractorTest.groovy
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
....1/library/src/test/java/io/opentelemetry/instrumentation/mongo/v3_1/MongoClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.mongo.v3_1; | ||
|
||
import com.mongodb.MongoClientOptions; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class MongoClientTest extends AbstractMongo31ClientTest { | ||
|
||
@RegisterExtension | ||
private static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
|
||
@Override | ||
protected void configureMongoClientOptions(MongoClientOptions.Builder options) { | ||
options.addCommandListener( | ||
MongoTelemetry.create(testing().getOpenTelemetry()).newCommandListener()); | ||
} | ||
|
||
@Override | ||
protected InstrumentationExtension testing() { | ||
return testing; | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...rc/test/java/io/opentelemetry/instrumentation/mongo/v3_1/MongoDbAttributesGetterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.mongo.v3_1; | ||
|
||
import static io.opentelemetry.instrumentation.mongo.v3_1.MongoTelemetryBuilder.DEFAULT_MAX_NORMALIZED_QUERY_LENGTH; | ||
import static java.util.Arrays.asList; | ||
import static java.util.Collections.singletonList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.bson.BsonArray; | ||
import org.bson.BsonDocument; | ||
import org.bson.BsonInt32; | ||
import org.bson.BsonString; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class MongoDbAttributesGetterTest { | ||
|
||
@Test | ||
@DisplayName("should sanitize statements to json") | ||
void shouldSanitizeStatementsToJson() { | ||
MongoDbAttributesGetter extractor = | ||
new MongoDbAttributesGetter(true, DEFAULT_MAX_NORMALIZED_QUERY_LENGTH); | ||
|
||
assertThat( | ||
sanitizeStatementAcrossVersions(extractor, new BsonDocument("cmd", new BsonInt32(1)))) | ||
.isEqualTo("{\"cmd\": \"?\"}"); | ||
|
||
assertThat( | ||
sanitizeStatementAcrossVersions( | ||
extractor, | ||
new BsonDocument("cmd", new BsonInt32(1)) | ||
.append("sub", new BsonDocument("a", new BsonInt32(1))))) | ||
.isEqualTo("{\"cmd\": \"?\", \"sub\": {\"a\": \"?\"}}"); | ||
|
||
assertThat( | ||
sanitizeStatementAcrossVersions( | ||
extractor, | ||
new BsonDocument("cmd", new BsonInt32(1)) | ||
.append("sub", new BsonArray(singletonList(new BsonInt32(1)))))) | ||
.isEqualTo("{\"cmd\": \"?\", \"sub\": [\"?\"]}"); | ||
} | ||
|
||
@Test | ||
@DisplayName("should only preserve string value if it is the value of the first top-level key") | ||
void shouldOnlyPreserveStringValueIfItIsTheValueOfTheFirstTopLevelKey() { | ||
MongoDbAttributesGetter extractor = | ||
new MongoDbAttributesGetter(true, DEFAULT_MAX_NORMALIZED_QUERY_LENGTH); | ||
|
||
assertThat( | ||
sanitizeStatementAcrossVersions( | ||
extractor, | ||
new BsonDocument("cmd", new BsonString("c")) | ||
.append("f", new BsonString("c")) | ||
.append("sub", new BsonString("c")))) | ||
.isEqualTo("{\"cmd\": \"c\", \"f\": \"?\", \"sub\": \"?\"}"); | ||
} | ||
|
||
@Test | ||
@DisplayName("should truncate simple command") | ||
void shouldTruncateSimpleCommand() { | ||
MongoDbAttributesGetter extractor = new MongoDbAttributesGetter(true, 20); | ||
|
||
String normalized = | ||
sanitizeStatementAcrossVersions( | ||
extractor, | ||
new BsonDocument("cmd", new BsonString("c")) | ||
.append("f1", new BsonString("c1")) | ||
.append("f2", new BsonString("c2"))); | ||
|
||
// This can vary because of different whitespace for different MongoDB versions | ||
assertThat(normalized).isIn("{\"cmd\": \"c\", \"f1\": \"", "{\"cmd\": \"c\", \"f1\" "); | ||
} | ||
|
||
@Test | ||
@DisplayName("should truncate array") | ||
void shouldTruncateArray() { | ||
MongoDbAttributesGetter extractor = new MongoDbAttributesGetter(true, 27); | ||
|
||
String normalized = | ||
sanitizeStatementAcrossVersions( | ||
extractor, | ||
new BsonDocument("cmd", new BsonString("c")) | ||
.append("f1", new BsonArray(asList(new BsonString("c1"), new BsonString("c2")))) | ||
.append("f2", new BsonString("c3"))); | ||
|
||
// This can vary because of different whitespace for different MongoDB versions | ||
assertThat(normalized) | ||
.isIn("{\"cmd\": \"c\", \"f1\": [\"?\", \"?", "{\"cmd\": \"c\", \"f1\": [\"?\","); | ||
} | ||
|
||
static String sanitizeStatementAcrossVersions( | ||
MongoDbAttributesGetter extractor, BsonDocument query) { | ||
return sanitizeAcrossVersions(extractor.sanitizeStatement(query)); | ||
} | ||
|
||
static String sanitizeAcrossVersions(String json) { | ||
json = json.replaceAll("\\{ ", "{"); | ||
json = json.replaceAll(" }", "}"); | ||
json = json.replaceAll(" :", ":"); | ||
return json; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...src/test/java/io/opentelemetry/instrumentation/mongo/v3_1/MongoSpanNameExtractorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.mongo.v3_1; | ||
|
||
import static io.opentelemetry.instrumentation.mongo.v3_1.MongoTelemetryBuilder.DEFAULT_MAX_NORMALIZED_QUERY_LENGTH; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.mongodb.event.CommandStartedEvent; | ||
import org.bson.BsonDocument; | ||
import org.bson.BsonInt32; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class MongoSpanNameExtractorTest { | ||
|
||
@Test | ||
@DisplayName("test span name with no dbName") | ||
void testSpanNameWithNoDbName() { | ||
MongoSpanNameExtractor nameExtractor = | ||
new MongoSpanNameExtractor( | ||
new MongoDbAttributesGetter(true, DEFAULT_MAX_NORMALIZED_QUERY_LENGTH), | ||
new MongoAttributesExtractor()); | ||
|
||
String command = "listDatabases"; | ||
CommandStartedEvent event = | ||
new CommandStartedEvent( | ||
0, null, null, command, new BsonDocument(command, new BsonInt32(1))); | ||
|
||
String spanName = nameExtractor.extract(event); | ||
|
||
assertThat(spanName).isEqualTo(command); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.