Fixes get applications with regions.

Polishes tests.
parent ab87e735
......@@ -56,6 +56,9 @@ public class RestTemplateEurekaHttpClient implements EurekaHttpClient {
public RestTemplateEurekaHttpClient(RestTemplate restTemplate, String serviceUrl) {
this.restTemplate = restTemplate;
this.serviceUrl = serviceUrl;
if (!serviceUrl.endsWith("/")) {
this.serviceUrl = this.serviceUrl+"/";
}
}
@Override
......@@ -67,7 +70,7 @@ public class RestTemplateEurekaHttpClient implements EurekaHttpClient {
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
ResponseEntity<Void> response = restTemplate.exchange(urlPath, HttpMethod.POST,
new HttpEntity<InstanceInfo>(info, headers), Void.class);
new HttpEntity<>(info, headers), Void.class);
return anEurekaHttpResponse(response.getStatusCodeValue())
.headers(headersOf(response)).build();
......@@ -141,9 +144,10 @@ public class RestTemplateEurekaHttpClient implements EurekaHttpClient {
String[] regions) {
String url = serviceUrl + urlPath;
if (regions != null && regions.length > 0)
urlPath = (urlPath.contains("?") ? "&" : "?") + "regions="
if (regions != null && regions.length > 0) {
url = url + (urlPath.contains("?") ? "&" : "?") + "regions="
+ StringUtil.join(regions);
}
ResponseEntity<EurekaApplications> response = restTemplate.exchange(url,
HttpMethod.GET, null, EurekaApplications.class);
......
......@@ -16,6 +16,15 @@
package org.springframework.cloud.netflix.eureka.http;
import java.util.Collections;
import com.netflix.appinfo.DataCenterInfo;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.LeaseInfo;
import com.netflix.appinfo.MyDataCenterInfo;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
......@@ -38,10 +47,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import static com.netflix.appinfo.InstanceInfo.DEFAULT_PORT;
import static com.netflix.appinfo.InstanceInfo.DEFAULT_SECURE_PORT;
import static org.springframework.util.Assert.isTrue;
......@@ -55,9 +60,46 @@ import static org.springframework.util.Assert.isTrue;
@RestController
@SpringBootApplication
public class EurekaServerMockApplication {
private static final InstanceInfo INFO = new InstanceInfo(null, null, null, null,
null, null, null, null, null, null, null, null, null, 0, null, null, null,
null, null, null, null, 0l, 0l, null, null);
private static final InstanceInfo INFO = InstanceInfo.Builder.newBuilder()
.setInstanceId("app1instance1")
.setAppName("app1")
.setAppNameForDeser("app1fordeser")
.setAppGroupName("app1group")
.setAppGroupNameForDeser("app1group1fordeser")
.setHostName("app1host1")
.setStatus(InstanceInfo.InstanceStatus.UP)
.setOverriddenStatus(InstanceInfo.InstanceStatus.DOWN)
.setIPAddr("127.0.0.1")
.setSID("app1sid")
.setPort(8080)
.setSecurePort(4443)
.enablePort(InstanceInfo.PortType.UNSECURE, true)
.setHomePageUrl("/", "http://localhost/")
.setHomePageUrlForDeser("http://localhost/")
.setStatusPageUrl("/status", "http://localhost/info")
.setStatusPageUrlForDeser("http://localhost/status")
.setHealthCheckUrls("/ping", "http://localhost/ping", null)
.setHealthCheckUrlsForDeser("http://localhost/ping", null)
.setVIPAddress("localhost:8080")
.setVIPAddressDeser("localhost:8080")
.setSecureVIPAddress("localhost:4443")
.setSecureVIPAddressDeser("localhost:4443")
.setDataCenterInfo(new MyDataCenterInfo(DataCenterInfo.Name.MyOwn))
.setLeaseInfo(LeaseInfo.Builder.newBuilder()
.setDurationInSecs(30)
.setRenewalIntervalInSecs(30)
.setEvictionTimestamp(System.currentTimeMillis()+30000)
.setRenewalTimestamp(System.currentTimeMillis()-1000)
.setRegistrationTimestamp(System.currentTimeMillis()-2000)
.build())
.add("metadatakey1", "metadatavalue1")
.setASGName("asg1")
.setIsCoordinatingDiscoveryServer(false)
.setLastUpdatedTimestamp(System.currentTimeMillis())
.setLastDirtyTimestamp(System.currentTimeMillis())
.setActionType(InstanceInfo.ActionType.ADDED)
.setNamespace("namespace1")
.build();
/**
* Simulates Eureka Server own's serialization.
......@@ -116,7 +158,9 @@ public class EurekaServerMockApplication {
@GetMapping(value = { "/apps", "/apps/delta", "/vips/{address}", "/svips/{address}" })
public Applications getApplications(@PathVariable(required = false) String address,
@RequestParam(required = false) String regions) {
return new Applications();
Applications applications = new Applications();
applications.addApplication(new Application("app1", Collections.singletonList(INFO)));
return applications;
}
@GetMapping(value = "/apps/{appName}")
......
......@@ -16,6 +16,7 @@
package org.springframework.cloud.netflix.eureka.http;
import com.netflix.discovery.shared.Applications;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
......@@ -105,7 +106,8 @@ public class RestTemplateEurekaHttpClientTest {
@Test
public void testGetApplications() {
Assert.assertNotNull(eurekaHttpClient.getApplications().getEntity());
Applications entity = eurekaHttpClient.getApplications().getEntity();
Assert.assertNotNull(entity);
Assert.assertNotNull(eurekaHttpClient.getApplications("us", "eu").getEntity());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment