ClusterModuleConsulProvider extends ModuleProvider { @Override public void prepare() throws ServiceNotProvidedException, ModuleStartException { try { List<Address> addressList = ConnectUtils.parse(config.getHostPort()); List<HostAndPort> hostAndPorts = new ArrayList<>(); for (Address address : addressList) { hostAndPorts.add(HostAndPort.fromParts(address.getHost(), address.getPort())); } Consul.Builder consulBuilder = Consul.builder() .withConnectTimeoutMillis(3000); if (StringUtils.isNotEmpty(config.getAclToken())) { consulBuilder.withAclToken(config.getAclToken()); } if (hostAndPorts.size() > 1) { client = consulBuilder.withMultipleHostAndPort(hostAndPorts, 5000).build(); } else { client = consulBuilder.withHostAndPort(hostAndPorts.get(0)).build(); } } catch (ConnectStringParseException | ConsulException e) { throw new ModuleStartException(e.getMessage(), e); } ConsulCoordinator coordinator = new ConsulCoordinator(config, client); this.registerServiceImplementation(ClusterRegister.class, coordinator); this.registerServiceImplementation(ClusterNodesQuery.class, coordinator); } ClusterModuleConsulProvider(); @Override String name(); @Override Class module(); @Override ModuleConfig createConfigBeanIfAbsent(); @Override void prepare(); @Override void start(); @Override void notifyAfterCompleted(); @Override String[] requiredModules();  }
@Test(expected = ModuleStartException.class) public void prepareWithNonHost() throws Exception { provider.prepare(); }
@Test @SuppressWarnings("unchecked") public void prepare() throws Exception { ClusterModuleConsulConfig consulConfig = new ClusterModuleConsulConfig(); consulConfig.setHostPort("10.0.0.1:1000,10.0.0.2:1001"); Whitebox.setInternalState(provider, "config", consulConfig); Consul consulClient = mock(Consul.class); Consul.Builder builder = mock(Consul.Builder.class); when(builder.build()).thenReturn(consulClient); PowerMockito.mockStatic(Consul.class); when(Consul.builder()).thenReturn(builder); when(builder.withConnectTimeoutMillis(anyLong())).thenReturn(builder); when(builder.withMultipleHostAndPort(anyCollection(), anyLong())).thenReturn(builder); provider.prepare(); ArgumentCaptor<Collection> addressCaptor = ArgumentCaptor.forClass(Collection.class); ArgumentCaptor<Long> timeCaptor = ArgumentCaptor.forClass(long.class); verify(builder).withMultipleHostAndPort(addressCaptor.capture(), timeCaptor.capture()); List<HostAndPort> address = (List<HostAndPort>) addressCaptor.getValue(); assertEquals(2, address.size()); assertEquals(Lists.newArrayList(HostAndPort.fromParts("10.0.0.1", 1000), HostAndPort.fromParts("10.0.0.2", 1001)), address); }
@Test public void prepareSingle() throws Exception { ClusterModuleConsulConfig consulConfig = new ClusterModuleConsulConfig(); consulConfig.setHostPort("10.0.0.1:1000"); Whitebox.setInternalState(provider, "config", consulConfig); Consul consulClient = mock(Consul.class); Consul.Builder builder = mock(Consul.Builder.class); when(builder.build()).thenReturn(consulClient); PowerMockito.mockStatic(Consul.class); when(Consul.builder()).thenReturn(builder); when(builder.withConnectTimeoutMillis(anyLong())).thenCallRealMethod(); when(builder.withHostAndPort(any())).thenReturn(builder); provider.prepare(); ArgumentCaptor<HostAndPort> hostAndPortArgumentCaptor = ArgumentCaptor.forClass(HostAndPort.class); verify(builder).withHostAndPort(hostAndPortArgumentCaptor.capture()); HostAndPort address = hostAndPortArgumentCaptor.getValue(); assertEquals(HostAndPort.fromParts("10.0.0.1", 1000), address); }