Java Code Examples for com.netflix.discovery.shared.Application#addInstance()

The following examples show how to use com.netflix.discovery.shared.Application#addInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CachedServicesServiceTest.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testGetAService() {
    CachedServicesService cachedServicesService = new CachedServicesService();
    cachedServicesService.clearAllServices();
    Applications applications = cachedServicesService.getAllCachedServices();
    Assert.assertNull(applications);

    InstanceInfo instance = getStandardInstance("service", InstanceInfo.InstanceStatus.DOWN, null);
    Application application = new Application("service");
    application.addInstance(instance);
    cachedServicesService.updateService("service", application);

    Application service = cachedServicesService.getService("service");
    Assert.assertNotNull(service);
    Assert.assertEquals("service", service.getName());
}
 
Example 2
Source File: EurekaHostsSupplierTest.java    From dyno with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetHosts() {
    String zone = "us-east-1c";
    String hostname = "1.2.3.4";
    Application app = new Application();
    AmazonInfo amazonInfo = AmazonInfo.Builder.newBuilder()
            .addMetadata(MetaDataKey.availabilityZone, zone).build();
    InstanceInfo instance = InstanceInfo.Builder.newBuilder()
            .setInstanceId(UUID.randomUUID().toString())
            .setDataCenterInfo(amazonInfo)
            .setHostName(hostname)
            .setAppName(APPLICATION_NAME)
            .build();
    app.addInstance(instance);
    when(eurekaClient.getApplication(APPLICATION_NAME)).thenReturn(app);
    List<Host> hosts = hostsSupplier.getHosts();
    assertFalse(hosts.isEmpty());
    assertEquals(hostname, hosts.get(0).getHostName());
    assertEquals(zone, hosts.get(0).getRack());
}
 
Example 3
Source File: ApiCatalogControllerTests.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void whenGetAllContainers_givenNothing_thenReturnContainersWithState() {
    Application service1 = new Application("service-1");
    service1.addInstance(getStandardInstance("service1", InstanceInfo.InstanceStatus.UP));

    Application service2 = new Application("service-2");
    service1.addInstance(getStandardInstance("service2", InstanceInfo.InstanceStatus.DOWN));

    given(this.cachedServicesService.getService("service1")).willReturn(service1);
    given(this.cachedServicesService.getService("service2")).willReturn(service2);
    given(this.cachedProductFamilyService.getAllContainers()).willReturn(createContainers());

    RestAssuredMockMvc.standaloneSetup(apiCatalogController);
    RestAssuredMockMvc.given().
        when().
        get("/containers").
        then().
        statusCode(200);
}
 
Example 4
Source File: ApiCatalogControllerTests.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void whenGetSingleContainer_thenPopulateApiDocForServices() throws ContainerStatusRetrievalException {
    Application service1 = new Application("service-1");
    service1.addInstance(getStandardInstance("service1", InstanceInfo.InstanceStatus.UP));

    Application service2 = new Application("service-2");
    service1.addInstance(getStandardInstance("service2", InstanceInfo.InstanceStatus.DOWN));

    given(this.cachedServicesService.getService("service1")).willReturn(service1);
    given(this.cachedServicesService.getService("service2")).willReturn(service2);
    given(this.cachedProductFamilyService.getContainerById("api-one")).willReturn(createContainers().get(0));
    given(this.cachedApiDocService.getApiDocForService("service1", "v1")).willReturn("service1");
    given(this.cachedApiDocService.getApiDocForService("service2", "v1")).willReturn("service2");
    ResponseEntity<List<APIContainer>> containers = this.apiCatalogController.getAPIContainerById("api-one");
    Assert.assertNotNull(containers.getBody());
    Assert.assertEquals(1, containers.getBody().size());
    containers.getBody().forEach(apiContainer ->
        apiContainer.getServices().forEach(apiService ->
            Assert.assertEquals(apiService.getServiceId(), apiService.getApiDoc())));
}
 
Example 5
Source File: ApiCatalogControllerTests.java    From api-layer with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void whenGetSingleContainer_thenPopulateApiDocForServicesExceptOneWhichFails() throws ContainerStatusRetrievalException {
    Application service1 = new Application("service-1");
    service1.addInstance(getStandardInstance("service1", InstanceInfo.InstanceStatus.UP));

    Application service2 = new Application("service-2");
    service1.addInstance(getStandardInstance("service2", InstanceInfo.InstanceStatus.DOWN));

    given(this.cachedServicesService.getService("service1")).willReturn(service1);
    given(this.cachedServicesService.getService("service2")).willReturn(service2);
    given(this.cachedProductFamilyService.getContainerById("api-one")).willReturn(createContainers().get(0));
    given(this.cachedApiDocService.getApiDocForService("service1", "v1")).willReturn("service1");
    given(this.cachedApiDocService.getApiDocForService("service2", "v1")).willThrow(new RuntimeException());
    ResponseEntity<List<APIContainer>> containers = this.apiCatalogController.getAPIContainerById("api-one");
    Assert.assertNotNull(containers.getBody());
    Assert.assertEquals(1, containers.getBody().size());
    containers.getBody().forEach(apiContainer ->
        apiContainer.getServices().forEach(apiService -> {
            if (apiService.getServiceId().equals("service1")) {
                Assert.assertEquals(apiService.getServiceId(), apiService.getApiDoc());
            }
            if (apiService.getServiceId().equals("service2")) {
                Assert.assertNull(apiService.getApiDoc());
            }
        }));
}
 
Example 6
Source File: ServiceControllerTest.java    From eureka-consul-adapter with MIT License 6 votes vote down vote up
@Test
public void service_sampleService_jsonObject_preferHostName() throws Exception {

    Applications applications = mock2Applications();
    Mockito.when(registry.getApplications()).thenReturn(applications);
    Application ms1 = applications.getRegisteredApplications().get(0);

    InstanceInfo instance1 = mock1Instance();
    ms1.addInstance(instance1);

    InstanceInfo instance2 = mock1Instance("2","1.2.3.5", "2.ms1.com", 81, true);
    ms1.addInstance(instance2);

    Mockito.when(registry.getApplication("ms1")).thenReturn(ms1);

    instanceInfoMapper.setPreferHostName(true);

    performAsync("/v1/catalog/service/ms1?wait=1ms")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Address", Matchers.is("ms1.com")))
            .andExpect(jsonPath("$[0].ServiceAddress", Matchers.is("ms1.com")))

            .andExpect(jsonPath("$[1].Address", Matchers.is("2.ms1.com")))
            .andExpect(jsonPath("$[1].ServiceAddress", Matchers.is("2.ms1.com")));
}
 
Example 7
Source File: InstanceRefreshService.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Go ahead and retrieve this instances API doc and update the cache
 *
 * @param containersUpdated containers, which were updated
 * @param instance          the instance
 * @param application       the service
 */
private void processInstance(Set<String> containersUpdated, InstanceInfo instance, Application application) {
    application.addInstance(instance);

    if (!InstanceInfo.InstanceStatus.DOWN.equals(instance.getStatus())) {
        // update any containers which contain this service
        updateContainer(containersUpdated, instance);
    }

    // Update the service cache
    updateService(instance.getAppName(), application);
}
 
Example 8
Source File: EurekaReactiveDiscoveryClientTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnFluxOfServices() {
	Applications applications = new Applications();
	Application app = new Application("my-service");
	app.addInstance(new InstanceInfo("instance", "my-service", "", "127.0.0.1", "",
			null, null, "", "", "", "", "", "", 0, null, "", null, null, null, null,
			null, null, null, null, null, null));
	applications.addApplication(app);
	when(eurekaClient.getApplications()).thenReturn(applications);
	Flux<String> services = this.client.getServices();
	StepVerifier.create(services).expectNext("my-service").expectComplete().verify();
}
 
Example 9
Source File: EurekaControllerTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
	PeerEurekaNodes peerEurekaNodes = mock(PeerEurekaNodes.class);
	when(peerEurekaNodes.getPeerNodesView())
			.thenReturn(Collections.<PeerEurekaNode>emptyList());

	InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder().setAppName("test")
			.setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn))
			.build();

	this.infoManager = mock(ApplicationInfoManager.class);
	this.original = ApplicationInfoManager.getInstance();
	setInstance(this.infoManager);
	when(this.infoManager.getInfo()).thenReturn(instanceInfo);

	Application myapp = new Application("myapp");
	myapp.addInstance(InstanceInfo.Builder.newBuilder().setAppName("myapp")
			.setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn))
			.setInstanceId("myapp:1").build());

	ArrayList<Application> applications = new ArrayList<>();
	applications.add(myapp);

	PeerAwareInstanceRegistry registry = mock(PeerAwareInstanceRegistry.class);
	when(registry.getSortedApplications()).thenReturn(applications);

	EurekaServerContext serverContext = mock(EurekaServerContext.class);
	EurekaServerContextHolder.initialize(serverContext);
	when(serverContext.getRegistry()).thenReturn(registry);
	when(serverContext.getPeerEurekaNodes()).thenReturn(peerEurekaNodes);
	when(serverContext.getApplicationInfoManager()).thenReturn(this.infoManager);

}
 
Example 10
Source File: EurekaControllerTest.java    From didi-eureka-server with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	PeerEurekaNodes peerEurekaNodes = mock(PeerEurekaNodes.class);
	when(peerEurekaNodes.getPeerNodesView()).thenReturn(Collections.<PeerEurekaNode>emptyList());

	InstanceInfo instanceInfo = InstanceInfo.Builder.newBuilder()
			.setAppName("test")
			.setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn))
			.build();

	this.infoManager = mock(ApplicationInfoManager.class);
	this.original = ApplicationInfoManager.getInstance();
	setInstance(this.infoManager);
	when(this.infoManager.getInfo()).thenReturn(instanceInfo);

	Application myapp = new Application("myapp");
	myapp.addInstance(InstanceInfo.Builder.newBuilder()
			.setAppName("myapp")
			.setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn))
			.setInstanceId("myapp:1")
			.build());

	ArrayList<Application> applications = new ArrayList<>();
	applications.add(myapp);

	PeerAwareInstanceRegistry registry = mock(PeerAwareInstanceRegistry.class);
	when(registry.getSortedApplications()).thenReturn(applications);

	EurekaServerContext serverContext = mock(EurekaServerContext.class);
	EurekaServerContextHolder.initialize(serverContext);
	when(serverContext.getRegistry()).thenReturn(registry);
	when(serverContext.getPeerEurekaNodes()).thenReturn(peerEurekaNodes);
	when(serverContext.getApplicationInfoManager()).thenReturn(this.infoManager);

}
 
Example 11
Source File: ServiceControllerTest.java    From eureka-consul-adapter with MIT License 5 votes vote down vote up
@Test(timeout = 3000)
public void service_eventInterruptsRequestError_isResolved() throws Exception {

    Applications applications = mock2Applications();
    Mockito.when(registry.getApplications()).thenReturn(applications);

    Application ms1 = applications.getRegisteredApplications().get(0);
    InstanceInfo instance1 = mock1Instance();
    ms1.addInstance(instance1);
    Mockito.when(registry.getApplication("ms1")).thenReturn(ms1);

    startThread(() -> {
        sleepFor(500);
        serviceChangeDetector.publish("ms2", 1);
        sleepFor(500);
        serviceChangeDetector.publish("ms2", 2);
        sleepFor(500);
        serviceChangeDetector.publish("ms2", 3);
        sleepFor(500);
        serviceChangeDetector.publish("ms2", 4);
        sleepFor(500);
        serviceChangeDetector.publish("ms2", 5);
    });

    long t1 = System.currentTimeMillis();
    performAsync("/v1/catalog/service/ms1?wait=2s&index=1")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(header().string("X-Consul-Index", "1"))
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Address", Matchers.is("1.2.3.4")));

    Assert.assertThat(System.currentTimeMillis() - t1, Matchers.is(Matchers.greaterThan(2000L)));

}
 
Example 12
Source File: CachedProductFamilyTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCalculationOfContainerTotalsWithSomeServicesDown() {
    CachedServicesService cachedServicesService = Mockito.mock(CachedServicesService.class);

    InstanceInfo instance1 = createApp("service1", "demoapp", InstanceInfo.InstanceStatus.UP);
    InstanceInfo instance2 = createApp("service2", "demoapp", InstanceInfo.InstanceStatus.DOWN);
    Application application1 = new Application();
    application1.addInstance(instance1);
    Application application2 = new Application();
    application2.addInstance(instance2);

    when(cachedServicesService.getService("service1")).thenReturn(application1);
    when(cachedServicesService.getService("service2")).thenReturn(application2);
    service = new CachedProductFamilyService(
        cachedServicesService,
        null,
        cacheRefreshUpdateThresholdInMillis);

    service.getContainer("demoapp", instance1);
    service.addServiceToContainer("demoapp", instance2);

    APIContainer container = service.retrieveContainer("demoapp");
    Assert.assertNotNull(container);

    service.calculateContainerServiceTotals(container);
    assertEquals("WARNING", container.getStatus());
    assertEquals(2, container.getTotalServices().intValue());
    assertEquals(1, container.getActiveServices().intValue());
}
 
Example 13
Source File: CachedProductFamilyTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCalculationOfContainerTotalsWithAllServicesDown() {
    CachedServicesService cachedServicesService = Mockito.mock(CachedServicesService.class);

    InstanceInfo instance1 = createApp("service1", "demoapp", InstanceInfo.InstanceStatus.DOWN);
    InstanceInfo instance2 = createApp("service2", "demoapp", InstanceInfo.InstanceStatus.DOWN);
    Application application1 = new Application();
    application1.addInstance(instance1);
    Application application2 = new Application();
    application2.addInstance(instance2);

    when(cachedServicesService.getService("service1")).thenReturn(application1);
    when(cachedServicesService.getService("service2")).thenReturn(application2);
    service = new CachedProductFamilyService(
        cachedServicesService,
        null,
        cacheRefreshUpdateThresholdInMillis);

    service.getContainer("demoapp", instance1);
    service.addServiceToContainer("demoapp", instance2);

    APIContainer container = service.retrieveContainer("demoapp");
    Assert.assertNotNull(container);

    service.calculateContainerServiceTotals(container);
    assertEquals("DOWN", container.getStatus());
    assertEquals(2, container.getTotalServices().intValue());
    assertEquals(0, container.getActiveServices().intValue());
}
 
Example 14
Source File: CachedProductFamilyTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCalculationOfContainerTotalsWithAllServicesUp() {
    CachedServicesService cachedServicesService = Mockito.mock(CachedServicesService.class);

    InstanceInfo instance1 = createApp("service", "demoapp");
    InstanceInfo instance2 = createApp("service", "demoapp");
    Application application = new Application();
    application.addInstance(instance1);
    application.addInstance(instance2);

    when(cachedServicesService.getService("service")).thenReturn(application);
    service = new CachedProductFamilyService(
        cachedServicesService,
        null,
        cacheRefreshUpdateThresholdInMillis);

    service.getContainer("demoapp", instance1);
    service.addServiceToContainer("demoapp", instance2);

    List<APIContainer> containersForService = service.getContainersForService("service");
    assertEquals(1, service.getContainerCount());

    APIContainer container = containersForService.get(0);
    service.calculateContainerServiceTotals(container);
    assertEquals("UP", container.getStatus());
    assertEquals(1, container.getTotalServices().intValue());
    assertEquals(1, container.getActiveServices().intValue());
}
 
Example 15
Source File: CachedServicesServiceTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReturningOneService() {
    CachedServicesService cachedServicesService = new CachedServicesService();
    cachedServicesService.clearAllServices();
    Applications applications = cachedServicesService.getAllCachedServices();
    Assert.assertNull(applications);

    InstanceInfo instance = getStandardInstance("service", InstanceInfo.InstanceStatus.DOWN, null);
    Application application = new Application("service");
    application.addInstance(instance);
    cachedServicesService.updateService("service", application);

    applications = cachedServicesService.getAllCachedServices();
    Assert.assertNotNull(applications.getRegisteredApplications());
}
 
Example 16
Source File: ApplicationRegistry.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Add new route to a service.
 *
 * @param service    Details of the service to be registered in the Gateway
 * @param addTimeout Whether the custom metadata should be provided for given service.
 */
public void addApplication(Service service, boolean addTimeout, boolean corsEnabled) {
    String id = service.getId();
    String locationPattern = service.getLocationPattern();
    String serviceRoute = service.getServiceRoute();

    Applications applications = new Applications();
    Application withMetadata = new Application(id);

    Map<String, String> metadata = createMetadata(addTimeout, corsEnabled);

    withMetadata.addInstance(getStandardInstance(metadata, id));
    applications.addApplication(withMetadata);
    ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute(locationPattern, service.getId());
    zuulRouteLinkedHashMap.put(locationPattern, route);

    applicationsToReturn.put(id, applications);

    RoutedServices routedServices = new RoutedServices();
    routedServices.addRoutedService(new RoutedService("test", serviceRoute, ""));
    if (this.routedServicesUsers != null) {
        for (RoutedServicesUser routedServicesUser : routedServicesUsers) {
            routedServicesUser.addRoutedServices(id, routedServices);
        }
    } else {
        servicesToAdd.add(new Services(id, routedServices));
    }
}
 
Example 17
Source File: ServiceControllerTest.java    From eureka-consul-adapter with MIT License 4 votes vote down vote up
@Test
public void service_sampleService_jsonObject_nodeMetadataMapper() throws Exception{

    Applications applications = mock2Applications();
    Mockito.when(registry.getApplications()).thenReturn(applications);
    Application ms1 = applications.getRegisteredApplications().get(0);

    InstanceInfo instance = mock1Instance();
    ms1.addInstance(instance);

    Mockito.when(registry.getApplication("ms1")).thenReturn(ms1);

    String nodeMetaPrefix = "nodeMeta_";
    MetadataMapper metadataMapper = new NodeMetadataMapper(nodeMetaPrefix);
    instanceInfoMapper.setMetadataMapper(metadataMapper);

    Map<String, String> md = new HashMap<>();
    md.put("k1", "v1");
    md.put("k2", "v2");
    md.put("nodeMeta_k1", "nv1");
    md.put("nodeMeta_k2", "nv2");
    Mockito.when(instance.getMetadata()).thenReturn(md);

    performAsync("/v1/catalog/service/ms1?wait=1ms")
        .andExpect(content().contentType("application/json;charset=UTF-8"))
        .andExpect(jsonPath("$[0].Address", Matchers.is("1.2.3.4")))
        .andExpect(jsonPath("$[0].Node", Matchers.is("ms1")))
        .andExpect(jsonPath("$[0].ServiceAddress", Matchers.is("1.2.3.4")))
        .andExpect(jsonPath("$[0].ServiceName", Matchers.is("ms1")))
        .andExpect(jsonPath("$[0].ServiceID", Matchers.is("1")))
        .andExpect(jsonPath("$[0].ServicePort", Matchers.is(80)))
        .andExpect(jsonPath("$[0].NodeMeta.k1", Matchers.is("nv1")))
        .andExpect(jsonPath("$[0].NodeMeta.k2", Matchers.is("nv2")))
        .andExpect(jsonPath("$[0].ServiceMeta.k1", Matchers.is("v1")))
        .andExpect(jsonPath("$[0].ServiceMeta.k2", Matchers.is("v2")))
        .andExpect(jsonPath("$[0].ServiceTags", Matchers.is(new JSONArray())));

    nodeMetaPrefix = "";
    metadataMapper = new NodeMetadataMapper(nodeMetaPrefix);
    instanceInfoMapper.setMetadataMapper(metadataMapper);

    performAsync("/v1/catalog/service/ms1?wait=1ms")
        .andExpect(content().contentType("application/json;charset=UTF-8"))
        .andExpect(jsonPath("$[0].Address", Matchers.is("1.2.3.4")))
        .andExpect(jsonPath("$[0].Node", Matchers.is("ms1")))
        .andExpect(jsonPath("$[0].ServiceAddress", Matchers.is("1.2.3.4")))
        .andExpect(jsonPath("$[0].ServiceName", Matchers.is("ms1")))
        .andExpect(jsonPath("$[0].ServiceID", Matchers.is("1")))
        .andExpect(jsonPath("$[0].ServicePort", Matchers.is(80)))
        .andExpect(jsonPath("$[0].NodeMeta.k1", Matchers.is("v1")))
        .andExpect(jsonPath("$[0].NodeMeta.k2", Matchers.is("v2")))
        .andExpect(jsonPath("$[0].NodeMeta.nodeMeta_k1", Matchers.is("nv1")))
        .andExpect(jsonPath("$[0].NodeMeta.nodeMeta_k2", Matchers.is("nv2")))
        .andExpect(jsonPath("$[0].ServiceMeta").isEmpty())
        .andExpect(jsonPath("$[0].ServiceTags", Matchers.is(new JSONArray())));
}
 
Example 18
Source File: ServiceControllerTest.java    From eureka-consul-adapter with MIT License 4 votes vote down vote up
@Test
public void service_sampleService_jsonObject() throws Exception {

    Applications applications = mock2Applications();
    Mockito.when(registry.getApplications()).thenReturn(applications);
    Application ms1 = applications.getRegisteredApplications().get(0);

    InstanceInfo instance1 = mock1Instance();
    ms1.addInstance(instance1);

    Mockito.when(registry.getApplication("ms1")).thenReturn(ms1);

    MetadataMapper metadataMapper = new ServiceMetadataMapper();
    instanceInfoMapper.setMetadataMapper(metadataMapper);

    performAsync("/v1/catalog/service/ms1?wait=1ms")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Address", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].ServiceAddress", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].ServiceName", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].ServiceID", Matchers.is("1")))
            .andExpect(jsonPath("$[0].ServicePort", Matchers.is(80)))
            .andExpect(jsonPath("$[0].NodeMeta").isEmpty())
            .andExpect(jsonPath("$[0].ServiceMeta").isEmpty())
            .andExpect(jsonPath("$[0].ServiceTags", Matchers.is(new JSONArray())));

    InstanceInfo instance2 = mock1Instance("2","1.2.3.5", "ms2.com", 81, true);

    Map<String, String> md = new HashMap<>();
    md.put("k1", "v1");
    md.put("k2", "v2");
    Mockito.when(instance2.getMetadata()).thenReturn(md);

    ms1.addInstance(instance2);

    performAsync("/v1/catalog/service/ms1?wait=1ms")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Address", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].ServiceAddress", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].ServiceName", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].ServiceID", Matchers.is("1")))
            .andExpect(jsonPath("$[0].ServicePort", Matchers.is(80)))
            .andExpect(jsonPath("$[0].NodeMeta").isEmpty())
            .andExpect(jsonPath("$[0].ServiceMeta").isEmpty())
            .andExpect(jsonPath("$[0].ServiceTags", Matchers.is(new JSONArray())))

            .andExpect(jsonPath("$[1].Address", Matchers.is("1.2.3.5")))
            .andExpect(jsonPath("$[1].Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[1].ServiceAddress", Matchers.is("1.2.3.5")))
            .andExpect(jsonPath("$[1].ServiceName", Matchers.is("ms1")))
            .andExpect(jsonPath("$[1].ServiceID", Matchers.is("2")))
            .andExpect(jsonPath("$[1].ServicePort", Matchers.is(443)))
            .andExpect(jsonPath("$[1].NodeMeta").isEmpty())
            .andExpect(jsonPath("$[1].ServiceMeta.k1", Matchers.is("v1")))
            .andExpect(jsonPath("$[1].ServiceMeta.k2", Matchers.is("v2")))
            .andExpect(jsonPath("$[1].ServiceTags", Matchers.is(new JSONArray())));
}
 
Example 19
Source File: ServiceControllerTest.java    From eureka-consul-adapter with MIT License 4 votes vote down vote up
@Test
public void service_healthEndpoint_jsonObject() throws Exception {

    Applications applications = mock2Applications();
    Mockito.when(registry.getApplications()).thenReturn(applications);
    Application ms1 = applications.getRegisteredApplications().get(0);

    InstanceInfo instance1 = mock1Instance();
    ms1.addInstance(instance1);

    Mockito.when(registry.getApplication("ms1")).thenReturn(ms1);

    MetadataMapper metadataMapper = new ServiceMetadataMapper();
    instanceInfoMapper.setMetadataMapper(metadataMapper);

    performAsync("/v1/health/service/ms1?wait=1ms")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Node.Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].Node.Address", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].Node.Meta").isEmpty())
            .andExpect(jsonPath("$[0].Service.ID", Matchers.is("1")))
            .andExpect(jsonPath("$[0].Service.Service", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].Service.Address", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].Service.Port", Matchers.is(80)))
            .andExpect(jsonPath("$[0].Service.Meta").isEmpty())
            .andExpect(jsonPath("$[0].Service.Tags", Matchers.is(new JSONArray())))
            .andExpect(jsonPath("$[0].Checks[0].Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].Checks[0].CheckID", Matchers.is("service:1")))
            .andExpect(jsonPath("$[0].Checks[0].Name", Matchers.is("Service '1' check")))
            .andExpect(jsonPath("$[0].Checks[0].Status", Matchers.is("UP")));

    InstanceInfo instance2 = mock1Instance("2","1.2.3.5", "ms2.com", 81, true);

    Map<String, String> md = new HashMap<>();
    md.put("k1", "v1");
    md.put("k2", "v2");
    Mockito.when(instance2.getMetadata()).thenReturn(md);

    ms1.addInstance(instance2);

    performAsync("/v1/health/service/ms1?wait=1ms")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Node.Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].Node.Address", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].Node.Meta").isEmpty())
            .andExpect(jsonPath("$[0].Service.ID", Matchers.is("1")))
            .andExpect(jsonPath("$[0].Service.Service", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].Service.Address", Matchers.is("1.2.3.4")))
            .andExpect(jsonPath("$[0].Service.Port", Matchers.is(80)))
            .andExpect(jsonPath("$[0].Service.Meta").isEmpty())
            .andExpect(jsonPath("$[0].Service.Tags", Matchers.is(new JSONArray())))
            .andExpect(jsonPath("$[0].Checks[0].Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[0].Checks[0].CheckID", Matchers.is("service:1")))
            .andExpect(jsonPath("$[0].Checks[0].Name", Matchers.is("Service '1' check")))
            .andExpect(jsonPath("$[0].Checks[0].Status", Matchers.is("UP")))

            .andExpect(jsonPath("$[1].Node.Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[1].Node.Address", Matchers.is("1.2.3.5")))
            .andExpect(jsonPath("$[1].Node.Meta").isEmpty())
            .andExpect(jsonPath("$[1].Service.ID", Matchers.is("2")))
            .andExpect(jsonPath("$[1].Service.Service", Matchers.is("ms1")))
            .andExpect(jsonPath("$[1].Service.Address", Matchers.is("1.2.3.5")))
            .andExpect(jsonPath("$[1].Service.Port", Matchers.is(443)))
            .andExpect(jsonPath("$[1].Service.Meta.k1", Matchers.is("v1")))
            .andExpect(jsonPath("$[1].Service.Meta.k2", Matchers.is("v2")))
            .andExpect(jsonPath("$[1].Service.Tags", Matchers.is(new JSONArray())))
            .andExpect(jsonPath("$[1].Checks[0].Node", Matchers.is("ms1")))
            .andExpect(jsonPath("$[1].Checks[0].CheckID", Matchers.is("service:2")))
            .andExpect(jsonPath("$[1].Checks[0].Name", Matchers.is("Service '2' check")))
            .andExpect(jsonPath("$[1].Checks[0].Status", Matchers.is("UP")));
}
 
Example 20
Source File: ServiceControllerTest.java    From eureka-consul-adapter with MIT License 4 votes vote down vote up
@Test(timeout = 10000)
public void service_serviceChangesToOtherServices_interruptOnCorrectService() throws Exception {

    Applications applications = mock2Applications();
    Mockito.when(registry.getApplications()).thenReturn(applications);
    Application ms1 = applications.getRegisteredApplications().get(0);

    InstanceInfo instance1 = mock1Instance();
    ms1.addInstance(instance1);

    Mockito.when(registry.getApplication("ms1")).thenReturn(ms1);

    startThread(() -> {
        sleepFor(1000);
        serviceChangeDetector.publish("ms1", 2);
        sleepFor(500);
        serviceChangeDetector.publish("ms2", 1);
        serviceChangeDetector.publish("ms3", 1);
        serviceChangeDetector.publish("ms4", 1);
        sleepFor(500);
        Mockito.when(instance1.getIPAddr()).thenReturn("8.8.8.8");
        serviceChangeDetector.publish("ms1", 3);
    });

    performAsync("/v1/catalog/service/ms1?wait=30s")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(header().string("X-Consul-Index", "1"))
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Address", Matchers.is("1.2.3.4")));

    performAsync("/v1/catalog/service/ms1?wait=30s&index=1")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(header().string("X-Consul-Index", "2"))
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Address", Matchers.is("1.2.3.4")));


    performAsync("/v1/catalog/service/ms1?wait=30s&index=2")
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(header().string("X-Consul-Index", "3"))
            .andExpect(content().contentType("application/json;charset=UTF-8"))
            .andExpect(jsonPath("$[0].Address", Matchers.is("8.8.8.8")));

}