- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1k
 
Convert mongo-common tests from groovy to java #12382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            trask
  merged 15 commits into
  open-telemetry:main
from
xiepuhuan:mongo-common-java-tests
  
      
      
   
  Oct 17, 2024 
      
    
  
     Merged
                    Changes from 10 commits
      Commits
    
    
            Show all changes
          
          
            15 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ce745ec
              
                Convert mongo tests from groovy to java
              
              
                xiepuhuan 25b4d92
              
                Merge branch 'main' into mongo-common-java-tests
              
              
                xiepuhuan d5e1efc
              
                Fix unit test errors
              
              
                xiepuhuan bec97ce
              
                Spotless apply
              
              
                xiepuhuan 3f52cab
              
                1. Rename the function names "setupSpec" and "cleanupSpec" to "setup"…
              
              
                xiepuhuan ad90b35
              
                Make function to package private
              
              
                xiepuhuan b65591d
              
                Remove local variable definition
              
              
                xiepuhuan c3b848b
              
                1. Remove useless lines of code.
              
              
                xiepuhuan 533d2f6
              
                Delete useless dependencies.
              
              
                xiepuhuan dda22cb
              
                1. Use assertThat instead of waitAndAssertTraces
              
              
                xiepuhuan 3beaf38
              
                1. Removing Annotations for Simple Names @DisplayName.
              
              
                xiepuhuan 0295025
              
                Merge branch 'main' into mongo-common-java-tests
              
              
                xiepuhuan 66f55ce
              
                Fix testLatestDeps
              
              
                xiepuhuan a3be8d9
              
                change get back to join
              
              
                laurit f9f8003
              
                simplify
              
              
                laurit File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
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 hidden or 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 hidden or 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 hidden or 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).containsAnyOf("{\"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) | ||
| .containsAnyOf("{\"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 hidden or 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 hidden or 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.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.