Updates for boot 2.0 compatibility

This commit is contained in:
Spencer Gibb
2017-11-27 14:16:21 -05:00
parent e1348be717
commit 4c3ef400ef
5 changed files with 26 additions and 10 deletions

View File

@@ -17,8 +17,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.zookeeper.discovery.test.CommonTestConfig;
import org.springframework.cloud.zookeeper.discovery.test.TestRibbonClient;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.cloud.zookeeper.serviceregistry.ServiceInstanceRegistration;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperServiceRegistry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -30,6 +30,7 @@ import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.zookeeper.discovery.test.TestRibbonClient.BASE_PATH;
/**
* @author Marcin Grzejszczak
@@ -50,7 +51,7 @@ public class ZookeeperDiscoveryHealthIndicatorWithNestedStructureTests {
@Test public void should_return_a_response_that_app_is_in_a_healthy_state_when_nested_folders_in_zookeeper_are_present()
throws Exception {
// when:
String response = this.testRibbonClient.callService("me", "application/health");
String response = this.testRibbonClient.callService("me", BASE_PATH + "/health");
// then:
log.info("Received response [" + response + "]");
then(this.curatorFramework.getChildren().forPath("/services/me")).isNotEmpty();

View File

@@ -27,6 +27,7 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.jayway.awaitility.Awaitility;
@@ -34,6 +35,7 @@ import com.toomuchcoding.jsonassert.JsonPath;
import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.cloud.zookeeper.discovery.test.TestRibbonClient.BASE_PATH;
/**
* @author Marcin Grzejszczak
@@ -90,7 +92,7 @@ public class ZookeeperDiscoveryTests {
final IdUsingFeignClient idUsingFeignClient = this.idUsingFeignClient;
//expect:
Awaitility.await().until(() -> {
then(idUsingFeignClient.getBeans()).isNotEmpty();
then(idUsingFeignClient.hi()).isNotEmpty();
return true;
});
}
@@ -100,7 +102,7 @@ public class ZookeeperDiscoveryTests {
}
private String registeredServiceStatus(ServiceInstance instance) {
return JsonPath.builder(this.testRibbonClient.callOnUrl(instance.getHost()+":"+instance.getPort(), "application/health")).field("status").read(String.class);
return JsonPath.builder(this.testRibbonClient.callOnUrl(instance.getHost()+":"+instance.getPort(), BASE_PATH + "/health")).field("status").read(String.class);
}
@Test public void should_properly_find_local_instance() {
@@ -110,9 +112,9 @@ public class ZookeeperDiscoveryTests {
@FeignClient("ribbonApp")
public static interface IdUsingFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/application/beans")
String getBeans();
public interface IdUsingFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/hi")
String hi();
}
@Configuration
@@ -120,12 +122,18 @@ public class ZookeeperDiscoveryTests {
@Import(CommonTestConfig.class)
@EnableFeignClients(clients = { IdUsingFeignClient.class })
@Profile("ribbon")
@RestController
static class Config {
@Bean TestRibbonClient testRibbonClient(@LoadBalanced RestTemplate restTemplate,
@Value("${spring.application.name}") String springAppName) {
return new TestRibbonClient(restTemplate, springAppName);
}
@RequestMapping("/hi")
public String hi() {
return "hi";
}
}
@Controller

View File

@@ -1,5 +1,6 @@
package org.springframework.cloud.zookeeper.discovery.test;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.web.client.RestTemplate;
/**
@@ -7,6 +8,8 @@ import org.springframework.web.client.RestTemplate;
*/
public class TestRibbonClient extends TestServiceRestClient {
public static final String BASE_PATH = new WebEndpointProperties().getBasePath();
private final String thisAppName;
public TestRibbonClient(RestTemplate restTemplate) {
@@ -21,7 +24,7 @@ public class TestRibbonClient extends TestServiceRestClient {
public String thisHealthCheck() {
return this.restTemplate
.getForObject("http://" + this.thisAppName + "/application/health", String.class);
.getForObject("http://" + this.thisAppName + BASE_PATH + "/health", String.class);
}
public Integer thisPort() {

View File

@@ -27,6 +27,10 @@ public class TestServiceRestClient {
}
public String callOnUrl(String url, String endpoint) {
return new RestTemplate().getForObject("http://" + url + "/" + endpoint, String.class);
if (!endpoint.startsWith("/")) {
endpoint = "/" + endpoint;
}
return new RestTemplate().getForObject("http://" + url + endpoint, String.class);
}
}

View File

@@ -40,7 +40,7 @@ public class SampleApplicationTests {
"--management.endpoints.web.expose=*",
"--spring.cloud.zookeeper.connect-string=localhost:" + zkPort);
ResponseEntity<String> response = new TestRestTemplate().getForEntity("http://localhost:"+port+"/application/health", String.class);
ResponseEntity<String> response = new TestRestTemplate().getForEntity("http://localhost:"+port+"/hi", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
context.close();