Fix MongoDB testLatestDeps (#1703)

* Fix MongoDB testLatestDeps

* Codenarc
This commit is contained in:
Trask Stalnaker 2020-11-19 23:34:25 -08:00 committed by GitHub
parent 7bfb6b6dbd
commit c3e1a321d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 42 deletions

View File

@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
import static java.util.Arrays.asList
import io.opentelemetry.javaagent.instrumentation.mongo.MongoClientTracer
import org.bson.BsonArray
import org.bson.BsonDocument
@ -10,24 +12,22 @@ import org.bson.BsonInt32
import org.bson.BsonString
import spock.lang.Specification
import static java.util.Arrays.asList
class MongoClientTracerTest extends Specification {
def 'should normalize queries to json'() {
setup:
def tracer = new MongoClientTracer()
expect:
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonInt32(1))) ==
'{"cmd": "?"}'
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonInt32(1))
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
'{"cmd": "?", "sub": {"a": "?"}}'
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonInt32(1))
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
'{"cmd": "?", "sub": ["?"]}'
@ -38,7 +38,7 @@ class MongoClientTracerTest extends Specification {
def tracer = new MongoClientTracer()
expect:
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonString("c"))
.append("f", new BsonString("c"))
.append("sub", new BsonString("c"))) ==
@ -49,23 +49,36 @@ class MongoClientTracerTest extends Specification {
setup:
def tracer = new MongoClientTracer(20)
expect:
tracer.normalizeQuery(
def normalized = normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonString("c"))
.append("f1", new BsonString("c1"))
.append("f2", new BsonString("c2"))) ==
'{ "cmd" : "c", "f1" '
.append("f2", new BsonString("c2")))
expect:
// this can vary because of different whitespace for different mongo versions
normalized == '{"cmd": "c", "f1": "' || normalized == '{"cmd": "c", "f1" '
}
def 'should truncate array'() {
setup:
def tracer = new MongoClientTracer(27)
expect:
tracer.normalizeQuery(
def normalized = normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonString("c"))
.append("f1", new BsonArray(asList(new BsonString("c1"), new BsonString("c2"))))
.append("f2", new BsonString("c3"))) ==
'{ "cmd" : "c", "f1" : ["?",'
.append("f1", new BsonArray(Arrays.asList(new BsonString("c1"), new BsonString("c2"))))
.append("f2", new BsonString("c3")))
expect:
// this can vary because of different whitespace for different mongo versions
normalized == '{"cmd": "c", "f1": ["?", "?' || normalized == '{"cmd": "c", "f1": ["?",'
}
def normalizeQueryAcrossVersions(MongoClientTracer tracer, BsonDocument query) {
return normalizeAcrossVersions(tracer.normalizeQuery(query))
}
def normalizeAcrossVersions(String json) {
json = json.replaceAll('\\{ ', '{')
json = json.replaceAll(' }', '}')
json = json.replaceAll(' :', ':')
return json
}
}

View File

@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
import static java.util.Arrays.asList
import io.opentelemetry.javaagent.instrumentation.mongo.MongoClientTracer
import org.bson.BsonArray
import org.bson.BsonDocument
@ -10,24 +12,22 @@ import org.bson.BsonInt32
import org.bson.BsonString
import spock.lang.Specification
import static java.util.Arrays.asList
class MongoClientTracerTest extends Specification {
def 'should normalize queries to json'() {
setup:
def tracer = new MongoClientTracer()
expect:
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonInt32(1))) ==
'{"cmd": "?"}'
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonInt32(1))
.append("sub", new BsonDocument("a", new BsonInt32(1)))) ==
'{"cmd": "?", "sub": {"a": "?"}}'
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonInt32(1))
.append("sub", new BsonArray(asList(new BsonInt32(1))))) ==
'{"cmd": "?", "sub": ["?"]}'
@ -38,7 +38,7 @@ class MongoClientTracerTest extends Specification {
def tracer = new MongoClientTracer()
expect:
tracer.normalizeQuery(
normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonString("c"))
.append("f", new BsonString("c"))
.append("sub", new BsonString("c"))) ==
@ -49,23 +49,36 @@ class MongoClientTracerTest extends Specification {
setup:
def tracer = new MongoClientTracer(20)
expect:
tracer.normalizeQuery(
def normalized = normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonString("c"))
.append("f1", new BsonString("c1"))
.append("f2", new BsonString("c2"))) ==
'{ "cmd" : "c", "f1" '
.append("f2", new BsonString("c2")))
expect:
// this can vary because of different whitespace for different mongo versions
normalized == '{"cmd": "c", "f1": "' || normalized == '{"cmd": "c", "f1" '
}
def 'should truncate array'() {
setup:
def tracer = new MongoClientTracer(27)
expect:
tracer.normalizeQuery(
def normalized = normalizeQueryAcrossVersions(tracer,
new BsonDocument("cmd", new BsonString("c"))
.append("f1", new BsonArray(asList(new BsonString("c1"), new BsonString("c2"))))
.append("f2", new BsonString("c3"))) ==
'{ "cmd" : "c", "f1" : ["?",'
.append("f1", new BsonArray(Arrays.asList(new BsonString("c1"), new BsonString("c2"))))
.append("f2", new BsonString("c3")))
expect:
// this can vary because of different whitespace for different mongo versions
normalized == '{"cmd": "c", "f1": ["?", "?' || normalized == '{"cmd": "c", "f1": ["?",'
}
def normalizeQueryAcrossVersions(MongoClientTracer tracer, BsonDocument query) {
return normalizeAcrossVersions(tracer.normalizeQuery(query))
}
def normalizeAcrossVersions(String json) {
json = json.replaceAll('\\{ ', '{')
json = json.replaceAll(' }', '}')
json = json.replaceAll(' :', ':')
return json
}
}