Skip to content

Commit 0bcc303

Browse files
committed
Fixed bug with DefaultServiceInstance: getScheme() was not implemented
This fixes the problem, that a https URI could not be overriden in Unit tests with http (avoiding certificate problems on the build server)
1 parent cde355e commit 0bcc303

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

spring-cloud-commons/src/main/java/org/springframework/cloud/client/DefaultServiceInstance.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,9 @@ public int hashCode() {
173173
return Objects.hash(instanceId, serviceId, host, port, secure, metadata);
174174
}
175175

176+
@Override
177+
public String getScheme() {
178+
return this.isSecure() ? "https" : "http";
179+
}
180+
176181
}

spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/simple/SimpleDiscoveryClientPropertiesMappingTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public void testDiscoveryClientShouldResolveSimpleValues() {
7676
then(s1.getPort()).isEqualTo(8080);
7777
then(s1.getUri()).isEqualTo(URI.create("http://s11:8080"));
7878
then(s1.isSecure()).isEqualTo(false);
79+
then(s1.getScheme()).isEqualTo("http");
80+
81+
ServiceInstance s2 = this.discoveryClient.getInstances("service1").get(1);
82+
then(s2.getHost()).isEqualTo("s12");
83+
then(s2.getPort()).isEqualTo(8443);
84+
then(s2.getUri()).isEqualTo(URI.create("https://s12:8443"));
85+
then(s2.isSecure()).isEqualTo(true);
86+
then(s2.getScheme()).isEqualTo("https");
7987
}
8088

8189
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2012-2020 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.cloud.client.discovery.simple.SimpleDiscoveryClient;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.test.context.junit4.SpringRunner;
33+
34+
import static org.assertj.core.api.BDDAssertions.then;
35+
36+
/**
37+
* Tests for mapping properties to instances in {@link SimpleDiscoveryClient}
38+
*
39+
* @author Daniel Gerber
40+
*/
41+
42+
@RunWith(SpringRunner.class)
43+
@SpringBootTest(properties = { "spring.application.name=service0",
44+
"spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s11:8080",
45+
"spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s12:8443",
46+
"spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s21:8080",
47+
"spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s22:443" })
48+
public class SimpleReactiveDiscoveryClientPropertiesMappingTests {
49+
50+
@Autowired
51+
private SimpleReactiveDiscoveryProperties props;
52+
53+
@Autowired
54+
private SimpleReactiveDiscoveryClient discoveryClient;
55+
56+
@Test
57+
public void propsShouldGetCleanlyMapped() {
58+
then(this.props.getInstances().size()).isEqualTo(2);
59+
then(this.props.getInstances().get("service1").size()).isEqualTo(2);
60+
then(this.props.getInstances().get("service1").get(0).getHost()).isEqualTo("s11");
61+
then(this.props.getInstances().get("service1").get(0).getPort()).isEqualTo(8080);
62+
then(this.props.getInstances().get("service1").get(0).getUri()).isEqualTo(URI.create("http://s11:8080"));
63+
then(this.props.getInstances().get("service1").get(0).isSecure()).isEqualTo(false);
64+
65+
then(this.props.getInstances().get("service2").size()).isEqualTo(2);
66+
then(this.props.getInstances().get("service2").get(0).getHost()).isEqualTo("s21");
67+
then(this.props.getInstances().get("service2").get(0).getPort()).isEqualTo(8080);
68+
then(this.props.getInstances().get("service2").get(0).getUri()).isEqualTo(URI.create("https://s21:8080"));
69+
then(this.props.getInstances().get("service2").get(0).isSecure()).isEqualTo(true);
70+
}
71+
72+
@Test
73+
public void testDiscoveryClientShouldResolveSimpleValues() {
74+
then(this.discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client");
75+
76+
Flux<ServiceInstance> services = this.discoveryClient.getInstances("service1");
77+
StepVerifier.create(services)
78+
.expectNextMatches(inst -> inst.getHost().equals("s11") && inst.getPort() == 8080
79+
&& inst.getUri().toString().equals("http://s11:8080") && !inst.isSecure()
80+
&& inst.getScheme().equals("http"))
81+
.expectNextMatches(inst -> inst.getHost().equals("s12") && inst.getPort() == 8443
82+
&& inst.getUri().toString().equals("https://s12:8443") && inst.isSecure()
83+
&& inst.getScheme().equals("https"))
84+
.expectComplete().verify();
85+
}
86+
87+
@Test
88+
public void testGetANonExistentServiceShouldReturnAnEmptyList() {
89+
then(this.discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client");
90+
91+
Flux<ServiceInstance> services = this.discoveryClient.getInstances("nonexistent");
92+
93+
StepVerifier.create(services).expectNextCount(0).expectComplete().verify();
94+
}
95+
96+
@Configuration(proxyBeanMethods = false)
97+
@EnableAutoConfiguration
98+
public static class SampleConfig {
99+
100+
}
101+
102+
}

0 commit comments

Comments
 (0)