/cloudfoundry-runtime/src/test/java/org/cloudfoundry/runtime/service/CloudServicesScannerTest.java

https://github.com/Abadasoft/vcap-java · Java · 197 lines · 168 code · 19 blank · 10 comment · 0 complexity · 6b4a517bc65509c3596fea8d277601fd MD5 · raw file

  1. package org.cloudfoundry.runtime.service;
  2. import static org.mockito.Mockito.when;
  3. import static org.mockito.Mockito.times;
  4. import static org.mockito.Mockito.verify;
  5. import static org.mockito.Matchers.eq;
  6. import static org.mockito.Matchers.any;
  7. import static org.mockito.Matchers.anyString;
  8. import static org.mockito.Matchers.anyObject;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import javax.sql.DataSource;
  12. import org.cloudfoundry.runtime.env.CloudEnvironment;
  13. import org.cloudfoundry.runtime.env.CloudServiceException;
  14. import org.cloudfoundry.runtime.env.MongoServiceInfo;
  15. import org.cloudfoundry.runtime.env.RabbitServiceInfo;
  16. import org.cloudfoundry.runtime.env.RdbmsServiceInfo;
  17. import org.cloudfoundry.runtime.env.RedisServiceInfo;
  18. import org.junit.Before;
  19. import org.junit.Test;
  20. import org.mockito.Mock;
  21. import org.mockito.MockitoAnnotations;
  22. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  23. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  24. import org.springframework.data.mongodb.MongoDbFactory;
  25. import org.springframework.data.redis.connection.RedisConnectionFactory;
  26. /**
  27. * Unit test of the {@link CloudServicesScanner}
  28. *
  29. * @author Jennifer Hickey
  30. *
  31. */
  32. public class CloudServicesScannerTest {
  33. @Mock
  34. private CloudEnvironment mockEnvironment;
  35. @Mock
  36. private ConfigurableListableBeanFactory beanFactory;
  37. @Mock
  38. MongoServiceInfo mongoServiceInfo;
  39. @Mock
  40. RdbmsServiceInfo rdbmsServiceInfo;
  41. @Mock
  42. RedisServiceInfo redisServiceInfo;
  43. @Mock
  44. RabbitServiceInfo rabbitServiceInfo;
  45. private CloudServicesScanner cloudServicesScanner = new CloudServicesScanner();
  46. @Before
  47. public void setup() {
  48. MockitoAnnotations.initMocks(this);
  49. }
  50. @Test
  51. public void createCloudServiceBeans() {
  52. when(beanFactory.getBeanNamesForType(CloudEnvironment.class)).thenReturn(new String[] { "cloudenv" });
  53. when(beanFactory.getBean(CloudEnvironment.class)).thenReturn(mockEnvironment);
  54. List<MongoServiceInfo> mongoSvcs = new ArrayList<MongoServiceInfo>();
  55. mongoSvcs.add(mongoServiceInfo);
  56. when(mockEnvironment.getServiceInfos(MongoServiceInfo.class)).thenReturn(mongoSvcs);
  57. when(mongoServiceInfo.getServiceName()).thenReturn("mongo1");
  58. when(mongoServiceInfo.getDatabase()).thenReturn("stocks");
  59. List<RedisServiceInfo> redisSvcs = new ArrayList<RedisServiceInfo>();
  60. redisSvcs.add(redisServiceInfo);
  61. when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(redisSvcs);
  62. when(redisServiceInfo.getServiceName()).thenReturn("redis1");
  63. List<RabbitServiceInfo> rabbitSvcs = new ArrayList<RabbitServiceInfo>();
  64. rabbitSvcs.add(rabbitServiceInfo);
  65. when(mockEnvironment.getServiceInfos(RabbitServiceInfo.class)).thenReturn(rabbitSvcs);
  66. when(rabbitServiceInfo.getServiceName()).thenReturn("rabbit1");
  67. List<RdbmsServiceInfo> rdbmsSvcs = new ArrayList<RdbmsServiceInfo>();
  68. rdbmsSvcs.add(rdbmsServiceInfo);
  69. when(mockEnvironment.getServiceInfos(RdbmsServiceInfo.class)).thenReturn(rdbmsSvcs);
  70. when(rdbmsServiceInfo.getServiceName()).thenReturn("data1");
  71. cloudServicesScanner.postProcessBeanFactory(beanFactory);
  72. verify(beanFactory).registerSingleton(eq("mongo1"), any(MongoDbFactory.class));
  73. verify(beanFactory).registerSingleton(eq("redis1"), any(RedisConnectionFactory.class));
  74. verify(beanFactory).registerSingleton(eq("rabbit1"), any(ConnectionFactory.class));
  75. verify(beanFactory).registerSingleton(eq("data1"), any(DataSource.class));
  76. }
  77. @Test(expected = CloudServiceException.class)
  78. public void postProcessBeanFactoryMultipleCloudEnvs() {
  79. when(beanFactory.getBeanNamesForType(CloudEnvironment.class)).thenReturn(
  80. new String[] { "cloudenv1", "cloudenv2" });
  81. cloudServicesScanner.postProcessBeanFactory(beanFactory);
  82. }
  83. @Test
  84. public void noMongoSvcs() {
  85. List<MongoServiceInfo> mongoSvcs = new ArrayList<MongoServiceInfo>();
  86. when(mockEnvironment.getServiceInfos(MongoServiceInfo.class)).thenReturn(mongoSvcs);
  87. List<RedisServiceInfo> redisSvcs = new ArrayList<RedisServiceInfo>();
  88. redisSvcs.add(redisServiceInfo);
  89. when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(redisSvcs);
  90. when(redisServiceInfo.getServiceName()).thenReturn("redis1");
  91. List<RabbitServiceInfo> rabbitSvcs = new ArrayList<RabbitServiceInfo>();
  92. rabbitSvcs.add(rabbitServiceInfo);
  93. when(mockEnvironment.getServiceInfos(RabbitServiceInfo.class)).thenReturn(rabbitSvcs);
  94. when(rabbitServiceInfo.getServiceName()).thenReturn("rabbit1");
  95. List<RdbmsServiceInfo> rdbmsSvcs = new ArrayList<RdbmsServiceInfo>();
  96. rdbmsSvcs.add(rdbmsServiceInfo);
  97. when(mockEnvironment.getServiceInfos(RdbmsServiceInfo.class)).thenReturn(rdbmsSvcs);
  98. when(rdbmsServiceInfo.getServiceName()).thenReturn("data1");
  99. cloudServicesScanner.createCloudServiceBeans(beanFactory, mockEnvironment);
  100. verify(beanFactory).registerSingleton(eq("redis1"), any(RedisConnectionFactory.class));
  101. verify(beanFactory).registerSingleton(eq("rabbit1"), any(ConnectionFactory.class));
  102. verify(beanFactory).registerSingleton(eq("data1"), any(DataSource.class));
  103. // Verify we registered only the above 3 beans
  104. verify(beanFactory, times(3)).registerSingleton(anyString(), anyObject());
  105. }
  106. @Test
  107. public void noRedisSvcs() {
  108. List<MongoServiceInfo> mongoSvcs = new ArrayList<MongoServiceInfo>();
  109. mongoSvcs.add(mongoServiceInfo);
  110. when(mockEnvironment.getServiceInfos(MongoServiceInfo.class)).thenReturn(mongoSvcs);
  111. when(mongoServiceInfo.getServiceName()).thenReturn("mongo1");
  112. when(mongoServiceInfo.getDatabase()).thenReturn("stocks");
  113. List<RedisServiceInfo> redisSvcs = new ArrayList<RedisServiceInfo>();
  114. when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(redisSvcs);
  115. List<RabbitServiceInfo> rabbitSvcs = new ArrayList<RabbitServiceInfo>();
  116. rabbitSvcs.add(rabbitServiceInfo);
  117. when(mockEnvironment.getServiceInfos(RabbitServiceInfo.class)).thenReturn(rabbitSvcs);
  118. when(rabbitServiceInfo.getServiceName()).thenReturn("rabbit1");
  119. List<RdbmsServiceInfo> rdbmsSvcs = new ArrayList<RdbmsServiceInfo>();
  120. rdbmsSvcs.add(rdbmsServiceInfo);
  121. when(mockEnvironment.getServiceInfos(RdbmsServiceInfo.class)).thenReturn(rdbmsSvcs);
  122. when(rdbmsServiceInfo.getServiceName()).thenReturn("data1");
  123. cloudServicesScanner.createCloudServiceBeans(beanFactory, mockEnvironment);
  124. verify(beanFactory).registerSingleton(eq("mongo1"), any(MongoDbFactory.class));
  125. verify(beanFactory).registerSingleton(eq("rabbit1"), any(ConnectionFactory.class));
  126. verify(beanFactory).registerSingleton(eq("data1"), any(DataSource.class));
  127. // Verify we registered only the above 3 beans
  128. verify(beanFactory, times(3)).registerSingleton(anyString(), anyObject());
  129. }
  130. @Test
  131. public void noRabbitSvcs() {
  132. List<MongoServiceInfo> mongoSvcs = new ArrayList<MongoServiceInfo>();
  133. mongoSvcs.add(mongoServiceInfo);
  134. when(mockEnvironment.getServiceInfos(MongoServiceInfo.class)).thenReturn(mongoSvcs);
  135. when(mongoServiceInfo.getServiceName()).thenReturn("mongo1");
  136. when(mongoServiceInfo.getDatabase()).thenReturn("stocks");
  137. List<RedisServiceInfo> redisSvcs = new ArrayList<RedisServiceInfo>();
  138. redisSvcs.add(redisServiceInfo);
  139. when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(redisSvcs);
  140. when(redisServiceInfo.getServiceName()).thenReturn("redis1");
  141. List<RabbitServiceInfo> rabbitSvcs = new ArrayList<RabbitServiceInfo>();
  142. when(mockEnvironment.getServiceInfos(RabbitServiceInfo.class)).thenReturn(rabbitSvcs);
  143. List<RdbmsServiceInfo> rdbmsSvcs = new ArrayList<RdbmsServiceInfo>();
  144. rdbmsSvcs.add(rdbmsServiceInfo);
  145. when(mockEnvironment.getServiceInfos(RdbmsServiceInfo.class)).thenReturn(rdbmsSvcs);
  146. when(rdbmsServiceInfo.getServiceName()).thenReturn("data1");
  147. cloudServicesScanner.createCloudServiceBeans(beanFactory, mockEnvironment);
  148. verify(beanFactory).registerSingleton(eq("mongo1"), any(MongoDbFactory.class));
  149. verify(beanFactory).registerSingleton(eq("redis1"), any(RedisConnectionFactory.class));
  150. verify(beanFactory).registerSingleton(eq("data1"), any(DataSource.class));
  151. // Verify we registered only the above 3 beans
  152. verify(beanFactory, times(3)).registerSingleton(anyString(), anyObject());
  153. }
  154. @Test
  155. public void noRdbmsSvcs() {
  156. List<MongoServiceInfo> mongoSvcs = new ArrayList<MongoServiceInfo>();
  157. mongoSvcs.add(mongoServiceInfo);
  158. when(mockEnvironment.getServiceInfos(MongoServiceInfo.class)).thenReturn(mongoSvcs);
  159. when(mongoServiceInfo.getServiceName()).thenReturn("mongo1");
  160. when(mongoServiceInfo.getDatabase()).thenReturn("stocks");
  161. List<RedisServiceInfo> redisSvcs = new ArrayList<RedisServiceInfo>();
  162. redisSvcs.add(redisServiceInfo);
  163. when(mockEnvironment.getServiceInfos(RedisServiceInfo.class)).thenReturn(redisSvcs);
  164. when(redisServiceInfo.getServiceName()).thenReturn("redis1");
  165. List<RabbitServiceInfo> rabbitSvcs = new ArrayList<RabbitServiceInfo>();
  166. rabbitSvcs.add(rabbitServiceInfo);
  167. when(mockEnvironment.getServiceInfos(RabbitServiceInfo.class)).thenReturn(rabbitSvcs);
  168. when(rabbitServiceInfo.getServiceName()).thenReturn("rabbit1");
  169. List<RdbmsServiceInfo> rdbmsSvcs = new ArrayList<RdbmsServiceInfo>();
  170. when(mockEnvironment.getServiceInfos(RdbmsServiceInfo.class)).thenReturn(rdbmsSvcs);
  171. cloudServicesScanner.createCloudServiceBeans(beanFactory, mockEnvironment);
  172. verify(beanFactory).registerSingleton(eq("mongo1"), any(MongoDbFactory.class));
  173. verify(beanFactory).registerSingleton(eq("redis1"), any(RedisConnectionFactory.class));
  174. verify(beanFactory).registerSingleton(eq("rabbit1"), any(ConnectionFactory.class));
  175. // Verify we registered only the above 3 beans
  176. verify(beanFactory, times(3)).registerSingleton(anyString(), anyObject());
  177. }
  178. }