PageRenderTime 4396ms CodeModel.GetById 30ms RepoModel.GetById 5ms app.codeStats 0ms

/src/test/java/com/proofpoint/discovery/TestDiscoveryServer.java

https://github.com/pphnl/discovery-server
Java | 217 lines | 174 code | 36 blank | 7 comment | 0 complexity | 64072d4dd44fd17a0b6da557a634b9bf MD5 | raw file
  1. package com.proofpoint.discovery;
  2. import com.google.common.collect.ImmutableMap;
  3. import com.google.common.collect.ImmutableSet;
  4. import com.google.inject.Guice;
  5. import com.google.inject.Injector;
  6. import com.ning.http.client.AsyncHttpClient;
  7. import com.ning.http.client.Response;
  8. import com.proofpoint.cassandra.testing.CassandraServerSetup;
  9. import com.proofpoint.cassandra.testing.TestingCassandraModule;
  10. import com.proofpoint.configuration.ConfigurationFactory;
  11. import com.proofpoint.configuration.ConfigurationModule;
  12. import com.proofpoint.discovery.client.DiscoveryClient;
  13. import com.proofpoint.discovery.client.ServiceAnnouncement;
  14. import com.proofpoint.discovery.client.ServiceDescriptor;
  15. import com.proofpoint.discovery.client.ServiceSelector;
  16. import com.proofpoint.discovery.client.ServiceSelectorConfig;
  17. import com.proofpoint.discovery.client.testing.SimpleServiceSelector;
  18. import com.proofpoint.json.JsonCodec;
  19. import com.proofpoint.json.JsonModule;
  20. import com.proofpoint.http.server.testing.TestingHttpServer;
  21. import com.proofpoint.http.server.testing.TestingHttpServerModule;
  22. import com.proofpoint.jaxrs.JaxrsModule;
  23. import com.proofpoint.node.NodeInfo;
  24. import com.proofpoint.node.NodeModule;
  25. import org.apache.cassandra.config.ConfigurationException;
  26. import org.apache.thrift.transport.TTransportException;
  27. import org.testng.annotations.AfterMethod;
  28. import org.testng.annotations.AfterSuite;
  29. import org.testng.annotations.BeforeMethod;
  30. import org.testng.annotations.BeforeSuite;
  31. import org.testng.annotations.Test;
  32. import java.io.IOException;
  33. import java.util.List;
  34. import java.util.Map;
  35. import static com.proofpoint.json.JsonCodec.mapJsonCodec;
  36. import static javax.ws.rs.core.Response.*;
  37. import static org.testng.Assert.assertEquals;
  38. import static org.testng.Assert.assertNotNull;
  39. import static org.testng.Assert.assertNull;
  40. import static org.testng.Assert.assertTrue;
  41. public class TestDiscoveryServer
  42. {
  43. private TestingHttpServer server;
  44. private CassandraDynamicStore dynamicStore;
  45. private CassandraStaticStore staticStore;
  46. @BeforeSuite
  47. public void setupCassandra()
  48. throws IOException, TTransportException, ConfigurationException, InterruptedException
  49. {
  50. CassandraServerSetup.tryInitialize();
  51. }
  52. @AfterSuite
  53. public void teardownCassandra()
  54. throws IOException
  55. {
  56. CassandraServerSetup.tryShutdown();
  57. }
  58. @BeforeMethod
  59. public void setup()
  60. throws Exception
  61. {
  62. // start server
  63. Map<String, String> serverProperties = ImmutableMap.<String, String>builder()
  64. .put("node.environment", "testing")
  65. .build();
  66. Injector serverInjector = Guice.createInjector(
  67. new NodeModule(),
  68. new TestingHttpServerModule(),
  69. new JsonModule(),
  70. new JaxrsModule(),
  71. new DiscoveryModule(),
  72. new TestingCassandraModule(),
  73. new ConfigurationModule(new ConfigurationFactory(serverProperties)));
  74. // TODO: wrap this in a testing bootstrap that handles PostConstruct & PreDestroy
  75. dynamicStore = serverInjector.getInstance(CassandraDynamicStore.class);
  76. dynamicStore.initialize();
  77. staticStore = serverInjector.getInstance(CassandraStaticStore.class);
  78. staticStore.initialize();
  79. server = serverInjector.getInstance(TestingHttpServer.class);
  80. server.start();
  81. }
  82. @AfterMethod
  83. public void teardown()
  84. throws Exception
  85. {
  86. server.stop();
  87. }
  88. @Test
  89. public void testDynamicAnnouncement()
  90. throws Exception
  91. {
  92. // publish announcement
  93. Map<String, String> announcerProperties = ImmutableMap.<String, String>builder()
  94. .put("node.environment", "testing")
  95. .put("node.pool", "red")
  96. .put("discovery.uri", server.getBaseUrl().toString())
  97. .build();
  98. Injector announcerInjector = Guice.createInjector(
  99. new NodeModule(),
  100. new JsonModule(),
  101. new ConfigurationModule(new ConfigurationFactory(announcerProperties)),
  102. new com.proofpoint.discovery.client.DiscoveryModule()
  103. );
  104. ServiceAnnouncement announcement = ServiceAnnouncement.serviceAnnouncement("apple")
  105. .addProperties(ImmutableMap.of("key", "value"))
  106. .build();
  107. DiscoveryClient client = announcerInjector.getInstance(DiscoveryClient.class);
  108. client.announce(ImmutableSet.of(announcement)).get();
  109. dynamicStore.reload();
  110. NodeInfo announcerNodeInfo = announcerInjector.getInstance(NodeInfo.class);
  111. List<ServiceDescriptor> services = selectorFor("apple", "red").selectAllServices();
  112. assertEquals(services.size(), 1);
  113. ServiceDescriptor service = services.get(0);
  114. assertNotNull(service.getId());
  115. assertEquals(service.getNodeId(), announcerNodeInfo.getNodeId());
  116. assertEquals(service.getLocation(), announcerNodeInfo.getLocation());
  117. assertEquals(service.getPool(), announcerNodeInfo.getPool());
  118. assertEquals(service.getProperties(), announcement.getProperties());
  119. // ensure that service is no longer visible
  120. client.unannounce().get();
  121. dynamicStore.reload();
  122. assertTrue(selectorFor("apple", "red").selectAllServices().isEmpty());
  123. }
  124. @Test
  125. public void testStaticAnnouncement()
  126. throws Exception
  127. {
  128. // create static announcement
  129. Map<String, Object> announcement = ImmutableMap.<String, Object>builder()
  130. .put("environment", "testing")
  131. .put("type", "apple")
  132. .put("pool", "red")
  133. .put("location", "/a/b/c")
  134. .put("properties", ImmutableMap.of("http", "http://host"))
  135. .build();
  136. AsyncHttpClient httpClient = new AsyncHttpClient();
  137. Response response = httpClient.preparePost(server.getBaseUrl().resolve("/v1/announcement/static").toString())
  138. .addHeader("Content-Type", "application/json")
  139. .setBody(JsonCodec.jsonCodec(Object.class).toJson(announcement))
  140. .execute()
  141. .get();
  142. assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode());
  143. String id = mapJsonCodec(String.class, Object.class)
  144. .fromJson(response.getResponseBody())
  145. .get("id")
  146. .toString();
  147. staticStore.reload();
  148. List<ServiceDescriptor> services = selectorFor("apple", "red").selectAllServices();
  149. assertEquals(services.size(), 1);
  150. ServiceDescriptor service = services.get(0);
  151. assertEquals(service.getId().toString(), id);
  152. assertNull(service.getNodeId());
  153. assertEquals(service.getLocation(), announcement.get("location"));
  154. assertEquals(service.getPool(), announcement.get("pool"));
  155. assertEquals(service.getProperties(), announcement.get("properties"));
  156. // remove announcement
  157. response = httpClient.prepareDelete(server.getBaseUrl().resolve("/v1/announcement/static/" + id).toString())
  158. .execute()
  159. .get();
  160. assertEquals(response.getStatusCode(), Status.NO_CONTENT.getStatusCode());
  161. // ensure announcement is gone
  162. staticStore.reload();
  163. assertTrue(selectorFor("apple", "red").selectAllServices().isEmpty());
  164. }
  165. private ServiceSelector selectorFor(String type, String pool)
  166. {
  167. Map<String, String> clientProperties = ImmutableMap.<String, String>builder()
  168. .put("node.environment", "testing")
  169. .put("discovery.uri", server.getBaseUrl().toString())
  170. .put("discovery.apple.pool", "red")
  171. .build();
  172. Injector clientInjector = Guice.createInjector(
  173. new NodeModule(),
  174. new JsonModule(),
  175. new ConfigurationModule(new ConfigurationFactory(clientProperties)),
  176. new com.proofpoint.discovery.client.DiscoveryModule()
  177. );
  178. DiscoveryClient client = clientInjector.getInstance(DiscoveryClient.class);
  179. return new SimpleServiceSelector(type, new ServiceSelectorConfig().setPool(pool), client);
  180. }
  181. }