TraceMongoCommandListener implements CommandListener { @Nullable String getCollectionName(BsonDocument command, String commandName) { if (COMMANDS_WITH_COLLECTION_NAME.contains(commandName)) { String collectionName = getNonEmptyBsonString(command.get(commandName)); if (collectionName != null) { return collectionName; } } return getNonEmptyBsonString(command.get("collection")); } TraceMongoCommandListener(MongoDBTracing mongoDBTracing);   TraceMongoCommandListener(ThreadLocalSpan threadLocalSpan); @Override void commandStarted(CommandStartedEvent event); @Override void commandSucceeded(CommandSucceededEvent event); @Override void commandFailed(CommandFailedEvent event);  }
@Test public void getCollectionName_allowListedCommand() { assertThat(listener.getCollectionName(new BsonDocument("find", new BsonString(" bar ")), "find")).isEqualTo("bar"); }
@Test public void getCollectionName_collectionFieldOnly() { assertThat(listener.getCollectionName(new BsonDocument("collection", new BsonString(" bar ")), "find")).isEqualTo("bar"); }
@Test public void getCollectionName_allowListedCommandAndCollectionField() { BsonDocument command = new BsonDocument(Arrays.asList( new BsonElement("collection", new BsonString("coll")), new BsonElement("find", new BsonString("bar")) )); assertThat(listener.getCollectionName(command, "find")).isEqualTo("bar"); }
@Test public void getCollectionName_notAllowListedCommandAndCollectionField() { BsonDocument command = new BsonDocument(Arrays.asList( new BsonElement("collection", new BsonString("coll")), new BsonElement("cmd", new BsonString("bar")) )); assertThat(listener.getCollectionName(command, "cmd")).isEqualTo( "coll"); }
@Test public void getCollectionName_missingCommand() { assertThat(listener.getCollectionName(new BsonDocument(), "find")).isNull(); }
@Test public void getCollectionName_notStringCommandArgument() { assertThat( listener.getCollectionName(new BsonDocument("find", BsonBoolean.TRUE), "find")).isNull(); }
@Test public void getCollectionName_emptyStringCommandArgument() { assertThat( listener.getCollectionName(new BsonDocument("find", new BsonString(" ")), "find")).isNull(); }
@Test public void getCollectionName_notAllowListedCommand() { assertThat( listener.getCollectionName(new BsonDocument("cmd", new BsonString(" bar ")), "cmd")).isNull(); }