|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.client.discovery.simple.reactive; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import reactor.core.publisher.Flux; |
| 24 | +import reactor.test.StepVerifier; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 28 | +import org.springframework.boot.test.context.SpringBootTest; |
| 29 | +import org.springframework.cloud.client.ServiceInstance; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.test.context.junit4.SpringRunner; |
| 32 | + |
| 33 | +import static org.assertj.core.api.BDDAssertions.then; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for mapping properties to instances in {@link SimpleReactiveDiscoveryClient} |
| 37 | + * |
| 38 | + * @author Daniel Gerber |
| 39 | + */ |
| 40 | +@RunWith(SpringRunner.class) |
| 41 | +@SpringBootTest(properties = { "spring.application.name=service0", |
| 42 | + "spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s11:8080", |
| 43 | + "spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s12:8443", |
| 44 | + "spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s21:8080", |
| 45 | + "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s22:443" }) |
| 46 | +public class SimpleReactiveDiscoveryClientPropertiesMappingTests { |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private SimpleReactiveDiscoveryProperties props; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private SimpleReactiveDiscoveryClient discoveryClient; |
| 53 | + |
| 54 | + @Test |
| 55 | + public void propsShouldGetCleanlyMapped() { |
| 56 | + then(props.getInstances().size()).isEqualTo(2); |
| 57 | + then(props.getInstances().get("service1").size()).isEqualTo(2); |
| 58 | + then(props.getInstances().get("service1").get(0).getHost()).isEqualTo("s11"); |
| 59 | + then(props.getInstances().get("service1").get(0).getPort()).isEqualTo(8080); |
| 60 | + then(props.getInstances().get("service1").get(0).getUri()).isEqualTo(URI.create("http://s11:8080")); |
| 61 | + then(props.getInstances().get("service1").get(0).isSecure()).isEqualTo(false); |
| 62 | + |
| 63 | + then(props.getInstances().get("service2").size()).isEqualTo(2); |
| 64 | + then(props.getInstances().get("service2").get(0).getHost()).isEqualTo("s21"); |
| 65 | + then(props.getInstances().get("service2").get(0).getPort()).isEqualTo(8080); |
| 66 | + then(props.getInstances().get("service2").get(0).getUri()).isEqualTo(URI.create("https://s21:8080")); |
| 67 | + then(props.getInstances().get("service2").get(0).isSecure()).isEqualTo(true); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testDiscoveryClientShouldResolveSimpleValues() { |
| 72 | + then(discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client"); |
| 73 | + |
| 74 | + Flux<ServiceInstance> services = discoveryClient.getInstances("service1"); |
| 75 | + StepVerifier.create(services) |
| 76 | + .expectNextMatches(inst -> inst.getHost().equals("s11") && inst.getPort() == 8080 |
| 77 | + && inst.getUri().toString().equals("http://s11:8080") && !inst.isSecure() |
| 78 | + && inst.getScheme().equals("http")) |
| 79 | + .expectNextMatches(inst -> inst.getHost().equals("s12") && inst.getPort() == 8443 |
| 80 | + && inst.getUri().toString().equals("https://s12:8443") && inst.isSecure() |
| 81 | + && inst.getScheme().equals("https")) |
| 82 | + .expectComplete().verify(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testGetANonExistentServiceShouldReturnAnEmptyList() { |
| 87 | + then(discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client"); |
| 88 | + |
| 89 | + Flux<ServiceInstance> services = discoveryClient.getInstances("nonexistent"); |
| 90 | + |
| 91 | + StepVerifier.create(services).expectNextCount(0).expectComplete().verify(); |
| 92 | + } |
| 93 | + |
| 94 | + @Configuration(proxyBeanMethods = false) |
| 95 | + @EnableAutoConfiguration |
| 96 | + public static class SampleConfig { |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments