PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/discovery/src/test/java/io/airlift/discovery/client/TestHttpAnnouncementBinder.java

https://gitlab.com/CORP-RESELLER/airlift
Java | 189 lines | 150 code | 24 blank | 15 comment | 0 complexity | b767d179f4125f6ed4625e81bfbc74c2 MD5 | raw file
  1. /*
  2. * Copyright 2010 Proofpoint, Inc.
  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 io.airlift.discovery.client;
  17. import com.google.common.collect.Iterables;
  18. import com.google.inject.Binder;
  19. import com.google.inject.Guice;
  20. import com.google.inject.Injector;
  21. import com.google.inject.Key;
  22. import com.google.inject.Module;
  23. import com.google.inject.TypeLiteral;
  24. import io.airlift.discovery.client.testing.TestingDiscoveryModule;
  25. import org.testng.Assert;
  26. import org.testng.annotations.Test;
  27. import java.net.URI;
  28. import java.util.Set;
  29. import static io.airlift.discovery.client.ServiceAnnouncement.serviceAnnouncement;
  30. public class TestHttpAnnouncementBinder
  31. {
  32. @Test
  33. public void testHttpAnnouncement()
  34. {
  35. final StaticAnnouncementHttpServerInfoImpl httpServerInfo = new StaticAnnouncementHttpServerInfoImpl(
  36. URI.create("http://127.0.0.1:4444"),
  37. URI.create("http://example.com:4444"),
  38. null,
  39. null
  40. );
  41. Injector injector = Guice.createInjector(
  42. new TestingDiscoveryModule(),
  43. new Module()
  44. {
  45. @Override
  46. public void configure(Binder binder)
  47. {
  48. binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo);
  49. DiscoveryBinder.discoveryBinder(binder).bindHttpAnnouncement("apple");
  50. }
  51. }
  52. );
  53. ServiceAnnouncement announcement = serviceAnnouncement("apple")
  54. .addProperty("http", httpServerInfo.getHttpUri().toASCIIString())
  55. .addProperty("http-external", httpServerInfo.getHttpExternalUri().toASCIIString())
  56. .build();
  57. Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>()
  58. {
  59. }));
  60. assertAnnouncement(announcements, announcement);
  61. }
  62. @Test
  63. public void testHttpsAnnouncement()
  64. {
  65. final StaticAnnouncementHttpServerInfoImpl httpServerInfo = new StaticAnnouncementHttpServerInfoImpl(
  66. null,
  67. null,
  68. URI.create("https://127.0.0.1:4444"),
  69. URI.create("https://example.com:4444")
  70. );
  71. Injector injector = Guice.createInjector(
  72. new TestingDiscoveryModule(),
  73. new Module()
  74. {
  75. @Override
  76. public void configure(Binder binder)
  77. {
  78. binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo);
  79. DiscoveryBinder.discoveryBinder(binder).bindHttpAnnouncement("apple");
  80. }
  81. }
  82. );
  83. ServiceAnnouncement announcement = serviceAnnouncement("apple")
  84. .addProperty("https", httpServerInfo.getHttpsUri().toASCIIString())
  85. .addProperty("https-external", httpServerInfo.getHttpsExternalUri().toASCIIString())
  86. .build();
  87. Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>()
  88. {
  89. }));
  90. assertAnnouncement(announcements, announcement);
  91. }
  92. @Test
  93. public void testHttpAnnouncementWithPool()
  94. {
  95. final StaticAnnouncementHttpServerInfoImpl httpServerInfo = new StaticAnnouncementHttpServerInfoImpl(
  96. URI.create("http://127.0.0.1:4444"),
  97. URI.create("http://example.com:4444"),
  98. URI.create("https://127.0.0.1:4444"),
  99. URI.create("https://example.com:4444")
  100. );
  101. Injector injector = Guice.createInjector(
  102. new TestingDiscoveryModule(),
  103. new Module()
  104. {
  105. @Override
  106. public void configure(Binder binder)
  107. {
  108. binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo);
  109. DiscoveryBinder.discoveryBinder(binder).bindHttpAnnouncement("apple");
  110. }
  111. }
  112. );
  113. ServiceAnnouncement announcement = serviceAnnouncement("apple")
  114. .addProperty("http", httpServerInfo.getHttpUri().toASCIIString())
  115. .addProperty("http-external", httpServerInfo.getHttpExternalUri().toASCIIString())
  116. .addProperty("https", httpServerInfo.getHttpsUri().toASCIIString())
  117. .addProperty("https-external", httpServerInfo.getHttpsExternalUri().toASCIIString())
  118. .build();
  119. Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>()
  120. {
  121. }));
  122. assertAnnouncement(announcements, announcement);
  123. }
  124. @Test
  125. public void testHttpAnnouncementWithCustomProperties()
  126. {
  127. final StaticAnnouncementHttpServerInfoImpl httpServerInfo = new StaticAnnouncementHttpServerInfoImpl(
  128. URI.create("http://127.0.0.1:4444"),
  129. URI.create("http://example.com:4444"),
  130. URI.create("https://127.0.0.1:4444"),
  131. URI.create("https://example.com:4444")
  132. );
  133. Injector injector = Guice.createInjector(
  134. new TestingDiscoveryModule(),
  135. new Module()
  136. {
  137. @Override
  138. public void configure(Binder binder)
  139. {
  140. binder.bind(AnnouncementHttpServerInfo.class).toInstance(httpServerInfo);
  141. DiscoveryBinder.discoveryBinder(binder).bindHttpAnnouncement("apple").addProperty("a", "apple");
  142. }
  143. }
  144. );
  145. ServiceAnnouncement announcement = serviceAnnouncement("apple")
  146. .addProperty("a", "apple")
  147. .addProperty("http", httpServerInfo.getHttpUri().toASCIIString())
  148. .addProperty("http-external", httpServerInfo.getHttpExternalUri().toASCIIString())
  149. .addProperty("https", httpServerInfo.getHttpsUri().toASCIIString())
  150. .addProperty("https-external", httpServerInfo.getHttpsExternalUri().toASCIIString())
  151. .build();
  152. Set<ServiceAnnouncement> announcements = injector.getInstance(Key.get(new TypeLiteral<Set<ServiceAnnouncement>>()
  153. {
  154. }));
  155. assertAnnouncement(announcements, announcement);
  156. }
  157. private void assertAnnouncement(Set<ServiceAnnouncement> actualAnnouncements, ServiceAnnouncement expected)
  158. {
  159. Assert.assertNotNull(actualAnnouncements);
  160. Assert.assertEquals(actualAnnouncements.size(), 1);
  161. ServiceAnnouncement announcement = Iterables.getOnlyElement(actualAnnouncements);
  162. Assert.assertEquals(announcement.getType(), expected.getType());
  163. Assert.assertEquals(announcement.getProperties(), expected.getProperties());
  164. }
  165. }