Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug with DefaultServiceInstance: getScheme() was not implemented #926

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -82,7 +82,7 @@ public DefaultServiceInstance() {
* @return URI of the form (secure)?https:http + "host:port".
*/
public static URI getUri(ServiceInstance instance) {
String scheme = (instance.isSecure()) ? "https" : "http";
String scheme = instance.getScheme();
String uri = String.format("%s://%s:%s", scheme, instance.getHost(), instance.getPort());
return URI.create(uri);
}
Expand Down Expand Up @@ -173,4 +173,9 @@ public int hashCode() {
return Objects.hash(instanceId, serviceId, host, port, secure, metadata);
}

@Override
public String getScheme() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reuse this method in getUri(ServiceInstance instance).

return isSecure() ? "https" : "http";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public void testDiscoveryClientShouldResolveSimpleValues() {
then(s1.getPort()).isEqualTo(8080);
then(s1.getUri()).isEqualTo(URI.create("http://s11:8080"));
then(s1.isSecure()).isEqualTo(false);
then(s1.getScheme()).isEqualTo("http");

ServiceInstance s2 = this.discoveryClient.getInstances("service1").get(1);
then(s2.getHost()).isEqualTo("s12");
then(s2.getPort()).isEqualTo(8443);
then(s2.getUri()).isEqualTo(URI.create("https://s12:8443"));
then(s2.isSecure()).isEqualTo(true);
then(s2.getScheme()).isEqualTo("https");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.client.discovery.simple.reactive;

import java.net.URI;

import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.BDDAssertions.then;

/**
* Tests for mapping properties to instances in {@link SimpleReactiveDiscoveryClient}
*
* @author Daniel Gerber
*/
@RunWith(SpringRunner.class)
@SpringBootTest(properties = { "spring.application.name=service0",
"spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s11:8080",
"spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s12:8443",
"spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s21:8080",
"spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s22:443" })
public class SimpleReactiveDiscoveryClientPropertiesMappingTests {

@Autowired
private SimpleReactiveDiscoveryProperties props;

@Autowired
private SimpleReactiveDiscoveryClient discoveryClient;

@Test
public void propsShouldGetCleanlyMapped() {
then(props.getInstances().size()).isEqualTo(2);
then(props.getInstances().get("service1").size()).isEqualTo(2);
then(props.getInstances().get("service1").get(0).getHost()).isEqualTo("s11");
then(props.getInstances().get("service1").get(0).getPort()).isEqualTo(8080);
then(props.getInstances().get("service1").get(0).getUri()).isEqualTo(URI.create("http://s11:8080"));
then(props.getInstances().get("service1").get(0).isSecure()).isEqualTo(false);

then(props.getInstances().get("service2").size()).isEqualTo(2);
then(props.getInstances().get("service2").get(0).getHost()).isEqualTo("s21");
then(props.getInstances().get("service2").get(0).getPort()).isEqualTo(8080);
then(props.getInstances().get("service2").get(0).getUri()).isEqualTo(URI.create("https://s21:8080"));
then(props.getInstances().get("service2").get(0).isSecure()).isEqualTo(true);
}

@Test
public void testDiscoveryClientShouldResolveSimpleValues() {
then(discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client");

Flux<ServiceInstance> services = discoveryClient.getInstances("service1");
StepVerifier.create(services)
.expectNextMatches(inst -> inst.getHost().equals("s11") && inst.getPort() == 8080
&& inst.getUri().toString().equals("http://s11:8080") && !inst.isSecure()
&& inst.getScheme().equals("http"))
.expectNextMatches(inst -> inst.getHost().equals("s12") && inst.getPort() == 8443
&& inst.getUri().toString().equals("https://s12:8443") && inst.isSecure()
&& inst.getScheme().equals("https"))
.expectComplete().verify();
}

@Test
public void testGetANonExistentServiceShouldReturnAnEmptyList() {
then(discoveryClient.description()).isEqualTo("Simple Reactive Discovery Client");

Flux<ServiceInstance> services = discoveryClient.getInstances("nonexistent");

StepVerifier.create(services).expectNextCount(0).expectComplete().verify();
}

@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration
public static class SampleConfig {

}

}