ConsulAdvertiser { public boolean register( final String applicationScheme, final int applicationPort, final int adminPort) { return register(applicationScheme, applicationPort, adminPort, null); } ConsulAdvertiser(
      final Environment environment,
      final ConsulFactory configuration,
      final Consul consul,
      final String serviceId); String getServiceId(); boolean register(
      final String applicationScheme, final int applicationPort, final int adminPort); boolean register(
      final String applicationScheme,
      final int applicationPort,
      final int adminPort,
      Collection<String> ipAddresses); void deregister();  }
@Test public void testRegister() { when(agent.isRegistered(serviceId)).thenReturn(false); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testRegisterWithSubnet() { when(agent.isRegistered(serviceId)).thenReturn(false); advertiser.register( "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, SECOND_SUBNET_IP, THIRD_SUBNET_IP)); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .address(SECOND_SUBNET_IP) .meta(ImmutableMap.of("scheme", "http")) .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testRegisterWithSubnetNoEligibleIps() { when(agent.isRegistered(serviceId)).thenReturn(false); advertiser.register( "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, "192.168.7.23", THIRD_SUBNET_IP)); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testRegisterWithSupplier() { when(agent.isRegistered(serviceId)).thenReturn(false); when(supplierMock.get()).thenReturn("192.168.8.99"); advertiser.register( "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, "192.168.7.23", THIRD_SUBNET_IP)); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .address("192.168.8.99") .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testRegisterWithSupplierException() { when(agent.isRegistered(serviceId)).thenReturn(false); when(supplierMock.get()).thenThrow(new IllegalArgumentException()); advertiser.register( "http", 8080, 8081, Arrays.asList(FIRST_SUBNET_IP, "192.168.7.23", THIRD_SUBNET_IP)); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testRegisterWithHttps() { when(agent.isRegistered(serviceId)).thenReturn(false); advertiser.register("https", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .port(8080) .check( ImmutableRegCheck.builder() .http("https: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "https")) .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testRegisterAlreadyRegistered() { when(agent.isRegistered(anyString())).thenReturn(true); advertiser.register("http", 8080, 8081); verify(agent, never()) .register(anyInt(), anyString(), anyLong(), anyString(), anyString(), anyList(), anyMap()); }
@Test public void testHostFromConfig() { factory.setServicePort(8888); factory.setServiceAddress("127.0.0.1"); when(agent.isRegistered(anyString())).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .id(serviceId) .port(8888) .address("127.0.0.1") .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .build(); verify(agent).register(registration); }
@Test public void testTagsFromConfig() { final List<String> tags = Arrays.asList("test", "second-test"); factory.setTags(tags); when(agent.isRegistered(serviceId)).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .tags(tags) .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .meta(ImmutableMap.of("scheme", "http")) .port(8080) .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testAclTokenFromConfig() { String aclToken = "acl-token"; factory.setAclToken(aclToken); when(agent.isRegistered(serviceId)).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .id(serviceId) .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .port(8080) .meta(ImmutableMap.of("scheme", "http")) .id(serviceId) .build(); verify(agent).register(registration); }
@Test public void testServiceMetaFromConfig() { final Map<String, String> serviceMeta = new HashMap<>(); serviceMeta.put("meta1-key", "meta1-value"); serviceMeta.put("meta2-key", "meta2-value"); factory.setServiceMeta(serviceMeta); when(agent.isRegistered(serviceId)).thenReturn(false); final ConsulAdvertiser advertiser = new ConsulAdvertiser(environment, factory, consul, serviceId); advertiser.register("http", 8080, 8081); final ImmutableRegistration registration = ImmutableRegistration.builder() .meta(serviceMeta) .putMeta("scheme", "http") .check( ImmutableRegCheck.builder() .http("http: .interval("1s") .deregisterCriticalServiceAfter("1m") .build()) .name("test") .port(8080) .id(serviceId) .build(); verify(agent).register(registration); }