|
| 1 | +package org.springframework.cloud.client.discovery.simple; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 7 | +import org.springframework.boot.test.context.SpringBootTest; |
| 8 | +import org.springframework.cloud.client.ServiceInstance; |
| 9 | +import org.springframework.cloud.client.discovery.DiscoveryClient; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.test.context.junit4.SpringRunner; |
| 12 | + |
| 13 | +import java.net.URI; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests for mapping properties to instances in {@link SimpleDiscoveryClient} |
| 19 | + * |
| 20 | + * @author Biju Kunjummen |
| 21 | + */ |
| 22 | + |
| 23 | +@RunWith(SpringRunner.class) |
| 24 | +@SpringBootTest(properties = { |
| 25 | + "spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s1-1:8080", |
| 26 | + "spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s1-2:8443", |
| 27 | + "spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s2-1:8080", |
| 28 | + "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s2-2:443" }) |
| 29 | +public class SimpleDiscoveryClientPropertiesMappingTests { |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private SimpleDiscoveryProperties props; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private DiscoveryClient discoveryClient; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void propsShouldGetCleanlyMapped() { |
| 39 | + assertThat(props.getInstances().size()).isEqualTo(2); |
| 40 | + assertThat(props.getInstances().get("service1").size()).isEqualTo(2); |
| 41 | + assertThat(props.getInstances().get("service1").get(0).getHost()) |
| 42 | + .isEqualTo("s1-1"); |
| 43 | + assertThat(props.getInstances().get("service1").get(0).getPort()).isEqualTo(8080); |
| 44 | + assertThat(props.getInstances().get("service1").get(0).getUri()) |
| 45 | + .isEqualTo(URI.create("http://s1-1:8080")); |
| 46 | + assertThat(props.getInstances().get("service1").get(0).isSecure()) |
| 47 | + .isEqualTo(false); |
| 48 | + |
| 49 | + assertThat(props.getInstances().get("service2").size()).isEqualTo(2); |
| 50 | + assertThat(props.getInstances().get("service2").get(0).getHost()) |
| 51 | + .isEqualTo("s2-1"); |
| 52 | + assertThat(props.getInstances().get("service2").get(0).getPort()).isEqualTo(8080); |
| 53 | + assertThat(props.getInstances().get("service2").get(0).getUri()) |
| 54 | + .isEqualTo(URI.create("https://s2-1:8080")); |
| 55 | + assertThat(props.getInstances().get("service2").get(0).isSecure()) |
| 56 | + .isEqualTo(true); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testDiscoveryClientShouldResolveSimpleValues() { |
| 61 | + assertThat(this.discoveryClient.description()) |
| 62 | + .isEqualTo("Simple Discovery Client"); |
| 63 | + assertThat(this.discoveryClient.getInstances("service1")).hasSize(2); |
| 64 | + |
| 65 | + ServiceInstance s1 = this.discoveryClient.getInstances("service1").get(0); |
| 66 | + assertThat(s1.getHost()).isEqualTo("s1-1"); |
| 67 | + assertThat(s1.getPort()).isEqualTo(8080); |
| 68 | + assertThat(s1.getUri()).isEqualTo(URI.create("http://s1-1:8080")); |
| 69 | + assertThat(s1.isSecure()).isEqualTo(false); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGetServices() { |
| 74 | + assertThat(this.discoveryClient.getServices()) |
| 75 | + .containsExactlyInAnyOrder("service1", "service2"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testGetANonExistentServiceShouldReturnAnEmptyList() { |
| 80 | + assertThat(this.discoveryClient.getInstances("nonexistent")).isNotNull(); |
| 81 | + assertThat(this.discoveryClient.getInstances("nonexistent")).isEmpty(); |
| 82 | + } |
| 83 | + |
| 84 | + @Configuration |
| 85 | + @EnableAutoConfiguration |
| 86 | + public static class SampleConfig { |
| 87 | + } |
| 88 | +} |
0 commit comments