/collection-plugins/mongodb/src/test/java/com/springsource/insight/plugin/mongodb/MongoDbOperationCollectionAspectTest.java

https://github.com/spring-projects/spring-insight-plugins · Java · 92 lines · 59 code · 15 blank · 18 comment · 2 complexity · 77d56964f71c92aad10fa3d7f320d316 MD5 · raw file

  1. /**
  2. * Copyright (c) 2009-2011 VMware, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.springsource.insight.plugin.mongodb;
  17. import static org.mockito.Mockito.mock;
  18. import static org.mockito.Mockito.when;
  19. import org.junit.Test;
  20. import com.mongodb.DB;
  21. import com.mongodb.Mongo;
  22. import com.mongodb.ServerAddress;
  23. import com.springsource.insight.collection.test.OperationCollectionAspectTestSupport;
  24. import com.springsource.insight.intercept.operation.Operation;
  25. import com.springsource.insight.intercept.operation.OperationList;
  26. import com.springsource.insight.util.StringUtil;
  27. /**
  28. */
  29. public class MongoDbOperationCollectionAspectTest
  30. extends OperationCollectionAspectTestSupport {
  31. public MongoDbOperationCollectionAspectTest() {
  32. super();
  33. }
  34. // execution(CommandResult DB.command(..));
  35. @SuppressWarnings("boxing")
  36. @Test
  37. public void testDbCommandWithHost() throws Exception {
  38. final String HOST = "7.3.6.5";
  39. final int PORT = 27017;
  40. Mongo mongo = mock(Mongo.class);
  41. ServerAddress address = mock(ServerAddress.class);
  42. when(address.getHost()).thenReturn(HOST);
  43. when(address.getPort()).thenReturn(PORT);
  44. when(mongo.getAddress()).thenReturn(address);
  45. Operation op = assertCommandOperation(new DBDummy(mongo, "testDbCommandWithHost"));
  46. assertEquals("Mismatched host", HOST, op.get("host", String.class));
  47. assertEquals("Mismatched port", 27017, op.getInt("port", (-1)));
  48. }
  49. @Test
  50. public void testDbCommandNoHost() throws Exception {
  51. Mongo mongo = mock(Mongo.class);
  52. Operation op = assertCommandOperation(new DBDummy(mongo, "testDbCommandNoHost"));
  53. for (String key : new String[]{"host", "port"}) {
  54. assertNullValue("Unexpected value for " + key, op.get(key));
  55. }
  56. }
  57. private Operation assertCommandOperation(DB db) {
  58. final String argVal = db.getName() + "-arg";
  59. db.command(argVal);
  60. Operation op = getLastEntered();
  61. assertNotNull("No operation extracted", op);
  62. assertEquals("Mismatched operation type", MongoDBOperationExternalResourceAnalyzer.TYPE, op.getType());
  63. assertEquals("Mismatched operation label", "MongoDB: DB.command()", op.getLabel());
  64. assertEquals("Mismatched DB name", db.getName(), op.get("dbName", String.class));
  65. if (!StringUtil.isEmpty(argVal)) {
  66. OperationList argsList = op.get("args", OperationList.class);
  67. assertNotNull("Missing arguments list");
  68. String actVal = argsList.get(0, String.class);
  69. assertEquals("Mismatched operation arguments", argVal, actVal);
  70. }
  71. return op;
  72. }
  73. @Override
  74. public MongoDbOperationCollectionAspect getAspect() {
  75. return MongoDbOperationCollectionAspect.aspectOf();
  76. }
  77. }