### Question:
MongoDatabaseHandler extends SimpleChannelInboundHandler<ClientRequest> { CompletionStage<Document> handleCommandAsync(MongoQuery query) { String collectionName = query.getCollectionName(); if ("$cmd.sys.inprog".equals(collectionName)) { return FutureUtils.wrap(() -> mongoBackend.getCurrentOperations(query)) .thenApply(currentOperations -> new Document("inprog", currentOperations)); } else if ("$cmd".equals(collectionName)) { String command = query.getQuery().keySet().iterator().next(); switch (command) { case "serverStatus": return FutureUtils.wrap(mongoBackend::getServerStatus); case "ping": return FutureUtils.wrap(() -> { Document response = new Document(); Utils.markOkay(response); return response; }); default: Document actualQuery = query.getQuery(); if ("$query".equals(command)) { command = ((Document) query.getQuery().get("$query")).keySet().iterator().next(); actualQuery = (Document) actualQuery.get("$query"); } return mongoBackend.handleCommandAsync(query.getChannel(), query.getDatabaseName(), command, actualQuery); } } return FutureUtils.failedFuture(new MongoServerException("unknown collection: " + collectionName)); } MongoDatabaseHandler(MongoBackend mongoBackend, ChannelGroup channelGroup); @Override void channelActive(ChannelHandlerContext ctx); @Override void channelInactive(ChannelHandlerContext ctx);  }### Answer:
@Test void testWrappedCommand() throws Exception { final MongoBackend backend = mock(MongoBackend.class); final Channel channel = mock(Channel.class); final Document queryDoc = json("'$query': { 'count': 'collectionName' }, '$readPreference': { 'mode': 'secondaryPreferred' }"); final Document subQueryDoc = json("'count': 'collectionName'"); final MongoQuery query = new MongoQuery(channel, null, "dbName.$cmd", 0, 0, queryDoc, null); final MongoDatabaseHandler handler = new MongoDatabaseHandler(backend, null); handler.handleCommandAsync(query); verify(backend).handleCommandAsync(channel, "dbName", "count", subQueryDoc); }
@Test void testNonWrappedCommand() throws Exception { final MongoBackend backend = mock(MongoBackend.class); final Channel channel = mock(Channel.class); final Document queryDoc = json("'count': 'collectionName'"); final MongoQuery query = new MongoQuery(channel, null, "dbName.$cmd", 0, 0, queryDoc, null); final MongoDatabaseHandler handler = new MongoDatabaseHandler(backend, null); handler.handleCommandAsync(query); verify(backend).handleCommandAsync(channel, "dbName", "count", queryDoc); }