support Reactor-based CF Java Client (#22)
* reactive Cloud Foundry Java Client - new support for auto-configuring the reactor-based CF java client v3.3. - reworkd the `DiscoveryClient` to depend on the standalone support. - made the tests run conditionally based on presence of certain env vars. fixes gh-21
This commit is contained in:
@@ -16,58 +16,22 @@
|
||||
|
||||
package org.springframework.cloud.cloudfoundry.discovery;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.cloudfoundry.client.lib.CloudCredentials;
|
||||
import org.cloudfoundry.client.lib.CloudFoundryClient;
|
||||
import org.cloudfoundry.client.lib.domain.CloudApplication;
|
||||
import org.cloudfoundry.client.lib.domain.InstanceInfo;
|
||||
import org.cloudfoundry.client.lib.domain.InstanceState;
|
||||
import org.cloudfoundry.client.lib.domain.InstancesInfo;
|
||||
import org.cloudfoundry.operations.CloudFoundryOperations;
|
||||
import org.cloudfoundry.operations.applications.ApplicationDetail;
|
||||
import org.cloudfoundry.operations.applications.ApplicationSummary;
|
||||
import org.cloudfoundry.operations.applications.InstanceDetail;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A Cloud Foundry v2 API-aware implementation of the {@link DiscoveryClient discovery
|
||||
* client} SPI. Cloud Foundry already retains a registry of running applications which we
|
||||
* expose here as services. Newer versions of Cloud Foundry support instance-specific
|
||||
* networking, but as the Cloud Foundry client API doesn't yet support that, this
|
||||
* {@link DiscoveryClient implementation doesn't either}.
|
||||
* <p/>
|
||||
* You need to provide an instance of the {@link CloudFoundryClient}. A workable
|
||||
* configuration looks like this:
|
||||
* <p/>
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Bean
|
||||
* CloudFoundryClient cloudFoundryClient(
|
||||
* @Value("${MY_CUSTOM_CF_API:https://api.run.pivotal.io}") String api,
|
||||
* CloudCredentials cc) throws MalformedURLException {
|
||||
* CloudFoundryClient cloudFoundryClient = new CloudFoundryClient(cc,
|
||||
* URI.create(api).toURL());
|
||||
* cloudFoundryClient.login();
|
||||
* return cloudFoundryClient;
|
||||
* }
|
||||
* </pre>
|
||||
* <p/>
|
||||
* You can configure all sorts of other things including which Cloud Foundry cloud
|
||||
* controller URI to use, how and whether to use an HTTP proxy, and more using alternative
|
||||
* constructors. As configured above, the client will talk to all services and
|
||||
* applications deployed in <EM>all</EM> spaces and organizations. Use one of the
|
||||
* {@link CloudFoundryClient#CloudFoundryClient(CloudCredentials, URL, String, String)}
|
||||
* variants to specify which space and organization to use.
|
||||
* <p/>
|
||||
* Cloud Foundry maintains a registry of running applications which we expose here as CloudFoundryService instances.
|
||||
*
|
||||
* @author Josh Long
|
||||
* @author Spencer Gibb
|
||||
@@ -75,119 +39,60 @@ import org.springframework.core.env.Environment;
|
||||
*/
|
||||
public class CloudFoundryDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
private static final String DESCRIPTION = "Cloud Foundry "
|
||||
+ DiscoveryClient.class.getName() + " implementation";
|
||||
private final CloudFoundryService cloudFoundryService;
|
||||
private final CloudFoundryOperations cloudFoundryOperations;
|
||||
|
||||
private static final Log log = LogFactory.getLog(CloudFoundryDiscoveryClient.class);
|
||||
|
||||
private final CloudFoundryClient cloudFoundryClient;
|
||||
private final String description = "Cloud Foundry " + DiscoveryClient.class.getName() + " implementation";
|
||||
|
||||
@Value("${vcap.application.name:${spring.application.name:application}}")
|
||||
private String vcapApplicationName = "application";
|
||||
|
||||
public CloudFoundryDiscoveryClient(CloudFoundryClient cloudFoundryClient,
|
||||
Environment environment) {
|
||||
this.cloudFoundryClient = cloudFoundryClient;
|
||||
|
||||
CloudFoundryDiscoveryClient(CloudFoundryOperations cloudFoundryOperations,
|
||||
CloudFoundryService svc) {
|
||||
this.cloudFoundryService = svc;
|
||||
this.cloudFoundryOperations = cloudFoundryOperations;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
|
||||
public ServiceInstance getLocalServiceInstance() {
|
||||
List<ServiceInstance> serviceInstances = null;
|
||||
try {
|
||||
CloudApplication application = this.cloudFoundryClient
|
||||
.getApplication(this.vcapApplicationName);
|
||||
serviceInstances = this.createServiceInstancesFromCloudApplications(
|
||||
Collections.singletonList(application));
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("Could not determine local service instance: " + e.getClass() + " ("
|
||||
+ e.getMessage() + ")");
|
||||
}
|
||||
return serviceInstances != null && serviceInstances.size() > 0
|
||||
? serviceInstances.iterator().next() : null;
|
||||
return this.description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(String s) {
|
||||
try {
|
||||
CloudApplication applications = this.cloudFoundryClient.getApplication(s);
|
||||
return this.createServiceInstancesFromCloudApplications(
|
||||
Collections.singletonList(applications));
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("Could not get service instances: " + e.getClass() + " ("
|
||||
+ e.getMessage() + ")");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
public List<ServiceInstance> getInstances(String serviceId) {
|
||||
return cloudFoundryService
|
||||
.getApplicationInstances(serviceId)
|
||||
.map(tuple -> {
|
||||
ApplicationDetail applicationDetail = tuple.getT1();
|
||||
InstanceDetail instanceDetail = tuple.getT2();
|
||||
|
||||
private boolean isRunning(CloudApplication ca) {
|
||||
InstancesInfo ii = this.cloudFoundryClient.getApplicationInstances(ca);
|
||||
List<InstanceInfo> instances;
|
||||
if (ii != null && (instances = ii.getInstances()) != null) {
|
||||
for (InstanceInfo resolved : instances) {
|
||||
InstanceState state = resolved.getState();
|
||||
if (state != null && state.equals(InstanceState.RUNNING)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
String applicationId = applicationDetail.getId();
|
||||
String applicationIndex = instanceDetail.getIndex();
|
||||
String name = applicationDetail.getName();
|
||||
String url = applicationDetail.getUrls().size() > 0 ? applicationDetail.getUrls().get(0) : null;
|
||||
boolean secure = (url + "").toLowerCase().startsWith("https");
|
||||
|
||||
HashMap<String, String> metadata = new HashMap<>();
|
||||
metadata.put("applicationId", applicationId);
|
||||
metadata.put("instanceId", applicationIndex);
|
||||
|
||||
return (ServiceInstance) new DefaultServiceInstance(name, url, 80, secure, metadata);
|
||||
})
|
||||
.collectList()
|
||||
.blockOptional()
|
||||
.orElse(new ArrayList<>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getServices() {
|
||||
List<String> services = new ArrayList<>();
|
||||
List<CloudApplication> applications;
|
||||
try {
|
||||
applications = this.cloudFoundryClient.getApplications();
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("Could not get applications: " + e.getClass() + " ("
|
||||
+ e.getMessage() + ")");
|
||||
applications = Collections.emptyList();
|
||||
}
|
||||
Set<String> serviceIds = new HashSet<>();
|
||||
for (CloudApplication ca : applications) {
|
||||
if (isRunning(ca)) {
|
||||
serviceIds.add(ca.getName());
|
||||
}
|
||||
}
|
||||
services.addAll(serviceIds);
|
||||
return services;
|
||||
}
|
||||
|
||||
protected List<ServiceInstance> createServiceInstancesFromCloudApplications(
|
||||
Collection<CloudApplication> cloudApplications) {
|
||||
Set<ServiceInstance> serviceInstances = new HashSet<>();
|
||||
for (CloudApplication ca : cloudApplications) {
|
||||
if (isRunning(ca)) {
|
||||
serviceInstances.add(new CloudFoundryServiceInstance(ca));
|
||||
}
|
||||
}
|
||||
List<ServiceInstance> instances = new ArrayList<>();
|
||||
instances.addAll(serviceInstances);
|
||||
return instances;
|
||||
}
|
||||
|
||||
public static class CloudFoundryServiceInstance extends DefaultServiceInstance {
|
||||
|
||||
private final CloudApplication cloudApplication;
|
||||
|
||||
public CloudApplication getCloudApplication() {
|
||||
return this.cloudApplication;
|
||||
}
|
||||
|
||||
public CloudFoundryServiceInstance(CloudApplication ca) {
|
||||
super(ca.getName(),
|
||||
ca.getUris().isEmpty() ? "localhost" : ca.getUris().iterator().next(),
|
||||
80, false);
|
||||
|
||||
this.cloudApplication = ca;
|
||||
}
|
||||
return this
|
||||
.cloudFoundryOperations
|
||||
.applications()
|
||||
.list()
|
||||
.map(ApplicationSummary::getName)
|
||||
.collectList()
|
||||
.blockOptional()
|
||||
.orElse(new ArrayList<>());
|
||||
}
|
||||
}
|
||||
@@ -16,26 +16,22 @@
|
||||
|
||||
package org.springframework.cloud.cloudfoundry.discovery;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
|
||||
import org.cloudfoundry.client.lib.CloudCredentials;
|
||||
import org.cloudfoundry.client.lib.CloudFoundryClient;
|
||||
import org.cloudfoundry.operations.CloudFoundryOperations;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Josh Long
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(CloudFoundryClient.class)
|
||||
@ConditionalOnClass(CloudFoundryOperations.class)
|
||||
@ConditionalOnProperty(value = "spring.cloud.cloudfoundry.discovery.enabled", matchIfMissing = true)
|
||||
@EnableConfigurationProperties(CloudFoundryDiscoveryProperties.class)
|
||||
public class CloudFoundryDiscoveryClientConfiguration {
|
||||
@@ -43,41 +39,15 @@ public class CloudFoundryDiscoveryClientConfiguration {
|
||||
@Autowired
|
||||
private CloudFoundryDiscoveryProperties discovery;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CloudCredentials.class)
|
||||
public CloudCredentials cloudCredentials() {
|
||||
return new CloudCredentials(this.discovery.getUsername(),
|
||||
this.discovery.getPassword());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CloudFoundryClient.class)
|
||||
public CloudFoundryClient cloudFoundryClient(CloudCredentials cc)
|
||||
throws MalformedURLException {
|
||||
CloudFoundryClient cloudFoundryClient;
|
||||
if (StringUtils.hasText(this.discovery.getOrg()) && StringUtils.hasText(this.discovery.getSpace())) {
|
||||
cloudFoundryClient = new CloudFoundryClient(cc,
|
||||
URI.create(this.discovery.getUrl()).toURL(), this.discovery.getOrg(),
|
||||
this.discovery.getSpace());
|
||||
}
|
||||
else {
|
||||
cloudFoundryClient = new CloudFoundryClient(cc,
|
||||
URI.create(this.discovery.getUrl()).toURL());
|
||||
}
|
||||
cloudFoundryClient.login();
|
||||
return cloudFoundryClient;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(CloudFoundryDiscoveryClient.class)
|
||||
public CloudFoundryDiscoveryClient cloudFoundryDiscoveryClient(
|
||||
CloudFoundryClient cloudFoundryClient, Environment environment) {
|
||||
return new CloudFoundryDiscoveryClient(cloudFoundryClient, environment);
|
||||
CloudFoundryOperations cf, CloudFoundryService svc) {
|
||||
return new CloudFoundryDiscoveryClient(cf, svc);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CloudFoundryHeartbeatSender cloudFoundryHeartbeatSender(CloudFoundryDiscoveryClient client) {
|
||||
return new CloudFoundryHeartbeatSender(client);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,32 +25,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@ConfigurationProperties(prefix = "spring.cloud.cloudfoundry.discovery")
|
||||
public class CloudFoundryDiscoveryProperties {
|
||||
|
||||
/**
|
||||
* URL of Cloud Foundry API (Cloud Controller).
|
||||
*/
|
||||
private String url = "https://api.run.pivotal.io";
|
||||
|
||||
/**
|
||||
* Username to authenticate (usually an email address).
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* Password for user to authenticate and obtain token.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* Organization name to authenticate with (default to user's default).
|
||||
*/
|
||||
private String org;
|
||||
|
||||
/**
|
||||
* Space name to authenticate with (default to user's default).
|
||||
*/
|
||||
@Value("${vcap.application.space_name:}")
|
||||
private String space;
|
||||
|
||||
/**
|
||||
* Flag to indicate that discovery is enabled.
|
||||
*/
|
||||
@@ -63,53 +37,13 @@ public class CloudFoundryDiscoveryProperties {
|
||||
private long heartbeatFrequency = 5000;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return this.enabled;
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public void setUrl(String cloudControllerUrl) {
|
||||
this.url = cloudControllerUrl;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return this.username;
|
||||
}
|
||||
|
||||
public void setUsername(String email) {
|
||||
this.username = email;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return this.password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getOrg() {
|
||||
return this.org;
|
||||
}
|
||||
|
||||
public void setOrg(String org) {
|
||||
this.org = org;
|
||||
}
|
||||
|
||||
public String getSpace() {
|
||||
return this.space;
|
||||
}
|
||||
|
||||
public void setSpace(String space) {
|
||||
this.space = space;
|
||||
}
|
||||
|
||||
public long getHeartbeatFrequency() {
|
||||
return this.heartbeatFrequency;
|
||||
}
|
||||
|
||||
@@ -16,20 +16,19 @@
|
||||
|
||||
package org.springframework.cloud.cloudfoundry.discovery;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.cloudfoundry.client.lib.CloudFoundryClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.netflix.client.config.CommonClientConfigKey;
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.config.ConfigurationManager;
|
||||
import com.netflix.config.DynamicPropertyFactory;
|
||||
import com.netflix.config.DynamicStringProperty;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* @author Josh Long
|
||||
@@ -52,10 +51,9 @@ public class CloudFoundryRibbonClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ServerList<?> ribbonServerList(CloudFoundryClient cloudFoundryClient,
|
||||
IClientConfig config) {
|
||||
CloudFoundryServerList cloudFoundryServerList = new CloudFoundryServerList(
|
||||
cloudFoundryClient);
|
||||
public ServerList<?> ribbonServerList(CloudFoundryService svc,
|
||||
IClientConfig config) {
|
||||
CloudFoundryServerList cloudFoundryServerList = new CloudFoundryServerList(svc);
|
||||
cloudFoundryServerList.initWithNiwsConfig(config);
|
||||
return cloudFoundryServerList;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.cloudfoundry.discovery;
|
||||
|
||||
import org.cloudfoundry.client.lib.domain.CloudApplication;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
|
||||
/**
|
||||
@@ -27,14 +25,12 @@ public class CloudFoundryServer extends Server {
|
||||
|
||||
private final MetaInfo metaInfo;
|
||||
|
||||
public CloudFoundryServer(final CloudApplication cloudApplication) {
|
||||
|
||||
super(cloudApplication.getUris().iterator().next(), 80);
|
||||
|
||||
public CloudFoundryServer(String appName, String uri, int port) {
|
||||
super(uri, port);
|
||||
this.metaInfo = new MetaInfo() {
|
||||
@Override
|
||||
public String getAppName() {
|
||||
return cloudApplication.getName();
|
||||
return appName;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,12 +40,12 @@ public class CloudFoundryServer extends Server {
|
||||
|
||||
@Override
|
||||
public String getServiceIdForDiscovery() {
|
||||
return cloudApplication.getName();
|
||||
return appName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return cloudApplication.getName();
|
||||
return appName;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,30 +16,24 @@
|
||||
|
||||
package org.springframework.cloud.cloudfoundry.discovery;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.cloudfoundry.client.lib.CloudFoundryClient;
|
||||
import org.cloudfoundry.client.lib.domain.CloudApplication;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractServerList;
|
||||
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Josh Long
|
||||
*/
|
||||
public class CloudFoundryServerList extends AbstractServerList<CloudFoundryServer> {
|
||||
|
||||
private static final Log log = LogFactory.getLog(CloudFoundryServerList.class);
|
||||
private String serviceId;
|
||||
|
||||
protected String serviceId;
|
||||
private final CloudFoundryService cloudFoundryService;
|
||||
|
||||
private final CloudFoundryClient cloudFoundryClient;
|
||||
|
||||
public CloudFoundryServerList(CloudFoundryClient cloudFoundryClient) {
|
||||
this.cloudFoundryClient = cloudFoundryClient;
|
||||
CloudFoundryServerList(CloudFoundryService svc) {
|
||||
this.cloudFoundryService = svc;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,15 +51,12 @@ public class CloudFoundryServerList extends AbstractServerList<CloudFoundryServe
|
||||
return this.cloudFoundryServers();
|
||||
}
|
||||
|
||||
protected List<CloudFoundryServer> cloudFoundryServers() {
|
||||
try {
|
||||
CloudApplication cloudApplications = this.cloudFoundryClient
|
||||
.getApplication(this.serviceId);
|
||||
return Collections.singletonList(new CloudFoundryServer(cloudApplications));
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("Cannot determine server list for " + this.serviceId + ": " + e.getClass() + "(" + e.getMessage() + ")");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
private List<CloudFoundryServer> cloudFoundryServers() {
|
||||
return cloudFoundryService
|
||||
.getApplicationInstances(this.serviceId)
|
||||
.map(tpl -> new CloudFoundryServer(tpl.getT1().getName(), tpl.getT1().getUrls().get(0), 80))
|
||||
.collectList()
|
||||
.blockOptional()
|
||||
.orElse(new ArrayList<>());
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,7 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({ CloudFoundryServerListTest.class, CloudFoundryAutoConfigurationTest.class,
|
||||
CloudFoundryServerTest.class, CloudFoundryDiscoveryClientTest.class })
|
||||
@SuiteClasses({CloudFoundryServerListTest.class})
|
||||
@Ignore
|
||||
public class AdhocTestSuite {
|
||||
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2015 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
|
||||
*
|
||||
* http://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.cloudfoundry.discovery;
|
||||
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.cloudfoundry.client.lib.CloudCredentials;
|
||||
import org.cloudfoundry.client.lib.CloudFoundryClient;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author <A href= "josh@joshlong.com">Josh Long</A>
|
||||
*/
|
||||
public class CloudFoundryAutoConfigurationTest {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
String hiServiceServiceId = "foo-service";
|
||||
|
||||
Object vcapAppl = "{\"limits\":{\"mem\":1024,\"disk\":1024,\"fds\":16384},\"application_version\":"
|
||||
+ "\"36eff082-96d6-498f-8214-508fda72ba65\",\"application_name\":\""
|
||||
+ hiServiceServiceId
|
||||
+ "\",\"application_uris\""
|
||||
+ ":[\""
|
||||
+ hiServiceServiceId
|
||||
+ ".cfapps.io\"],\"version\":\"36eff082-96d6-498f-8214-508fda72ba65\",\"name\":"
|
||||
+ "\"hi-service\",\"space_name\":\"joshlong\",\"space_id\":\"e0cd969c-3461-41ae-abde-4e11bb5acbd1\","
|
||||
+ "\"uris\":[\"hi-service.cfapps.io\"],\"users\":null,\"application_id\":\"af350f7c-88c4-4e35-a04e-698a1dbc7354\","
|
||||
+ "\"instance_id\":\"e4843ca23bd947b28e6d4cb3f9b92cbb\",\"instance_index\":0,\"host\":\"0.0.0.0\",\"port\":61590,"
|
||||
+ "\"started_at\":\"2015-05-07 20:00:10 +0000\",\"started_at_timestamp\":1431028810,\"start\":\"2015-05-07 20:00:10 +0000\","
|
||||
+ "\"state_timestamp\":1431028810}";
|
||||
|
||||
this.context = new SpringApplicationBuilder()
|
||||
.properties("VCAP_APPLICATION:"+vcapAppl, "server.port=0")
|
||||
.sources(SimpleConfiguration.class).run();
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() throws Throwable {
|
||||
synchronized (this) {
|
||||
if (null != this.context)
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableDiscoveryClient
|
||||
@EnableFeignClients
|
||||
@EnableAutoConfiguration
|
||||
public static class SimpleConfiguration {
|
||||
|
||||
@Bean
|
||||
CloudCredentials cloudCredentials() {
|
||||
return Mockito.mock(CloudCredentials.class);
|
||||
}
|
||||
|
||||
@Bean
|
||||
CloudFoundryClient cloudFoundryClient() {
|
||||
return Mockito.mock(CloudFoundryClient.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextLoaded() {
|
||||
LogFactory.getLog(getClass()).debug("contextLoad()");
|
||||
Assert.assertTrue(this.context.getBeansOfType(CloudFoundryDiscoveryClient.class)
|
||||
.size() > 0);
|
||||
Assert.assertTrue(this.context.getBeansOfType(
|
||||
CloudFoundryDiscoveryProperties.class).size() > 0);
|
||||
Assert.assertTrue(this.context.getBeansOfType(CloudFoundryClient.class).size() > 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,31 +16,28 @@
|
||||
|
||||
package org.springframework.cloud.cloudfoundry.discovery;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.cloudfoundry.client.lib.CloudFoundryClient;
|
||||
import org.cloudfoundry.client.lib.CloudFoundryException;
|
||||
import org.cloudfoundry.client.lib.domain.CloudApplication;
|
||||
import org.cloudfoundry.client.lib.domain.InstanceInfo;
|
||||
import org.cloudfoundry.client.lib.domain.InstanceState;
|
||||
import org.cloudfoundry.client.lib.domain.InstancesInfo;
|
||||
import org.cloudfoundry.operations.CloudFoundryOperations;
|
||||
import org.cloudfoundry.operations.applications.ApplicationDetail;
|
||||
import org.cloudfoundry.operations.applications.ApplicationSummary;
|
||||
import org.cloudfoundry.operations.applications.Applications;
|
||||
import org.cloudfoundry.operations.applications.InstanceDetail;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.util.function.Tuple2;
|
||||
import reactor.util.function.Tuples;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* @author <A href="mailto:josh@Joshlong.com">Josh Long</A>
|
||||
@@ -48,144 +45,59 @@ import org.springframework.http.HttpStatus;
|
||||
public class CloudFoundryDiscoveryClientTest {
|
||||
|
||||
private final Log log = LogFactory.getLog(getClass());
|
||||
|
||||
private CloudFoundryDiscoveryClient cloudFoundryDiscoveryClient;
|
||||
|
||||
private CloudApplication cloudApplication;
|
||||
|
||||
private String hiServiceServiceId = "hi-service";
|
||||
|
||||
private CloudFoundryClient cloudFoundryClient;
|
||||
|
||||
private CloudApplication fakeCloudApplication(String name, String... uri) {
|
||||
CloudApplication cloudApplication = mock(CloudApplication.class);
|
||||
given(cloudApplication.getName()).willReturn(name);
|
||||
given(cloudApplication.getUris()).willReturn(Arrays.asList(uri));
|
||||
return cloudApplication;
|
||||
}
|
||||
private CloudFoundryOperations ops;
|
||||
private CloudFoundryService svc;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.cloudFoundryClient = mock(CloudFoundryClient.class);
|
||||
Environment environment = mock(Environment.class);
|
||||
|
||||
given(environment.getProperty("VCAP_APPLICATION"))
|
||||
.willReturn(
|
||||
"{\"limits\":{\"mem\":1024,\"disk\":1024,\"fds\":16384},\"application_version\":"
|
||||
+ "\"36eff082-96d6-498f-8214-508fda72ba65\",\"application_name\":\""
|
||||
+ this.hiServiceServiceId
|
||||
+ "\",\"application_uris\""
|
||||
+ ":[\""
|
||||
+ this.hiServiceServiceId
|
||||
+ ".cfapps.io\"],\"version\":\"36eff082-96d6-498f-8214-508fda72ba65\",\"name\":"
|
||||
+ "\"hi-service\",\"space_name\":\"joshlong\",\"space_id\":\"e0cd969c-3461-41ae-abde-4e11bb5acbd1\","
|
||||
+ "\"uris\":[\"hi-service.cfapps.io\"],\"users\":null,\"application_id\":\"af350f7c-88c4-4e35-a04e-698a1dbc7354\","
|
||||
+ "\"instance_id\":\"e4843ca23bd947b28e6d4cb3f9b92cbb\",\"instance_index\":0,\"host\":\"0.0.0.0\",\"port\":61590,"
|
||||
+ "\"started_at\":\"2015-05-07 20:00:10 +0000\",\"started_at_timestamp\":1431028810,\"start\":\"2015-05-07 20:00:10 +0000\","
|
||||
+ "\"state_timestamp\":1431028810}");
|
||||
|
||||
List<CloudApplication> cloudApplications = new ArrayList<>();
|
||||
cloudApplications.add(fakeCloudApplication(this.hiServiceServiceId,
|
||||
"hi-service.cfapps.io", "hi-service-1.cfapps.io"));
|
||||
cloudApplications.add(fakeCloudApplication("config-service",
|
||||
"conf-service.cfapps.io", "conf-service-1.cfapps.io"));
|
||||
|
||||
given(this.cloudFoundryClient.getApplications()).willReturn(cloudApplications);
|
||||
|
||||
this.cloudApplication = cloudApplications.get(0);
|
||||
given(this.cloudFoundryClient.getApplication(this.hiServiceServiceId))
|
||||
.willReturn(this.cloudApplication);
|
||||
|
||||
given(this.cloudFoundryClient.getApplication(this.hiServiceServiceId))
|
||||
.willReturn(this.cloudApplication);
|
||||
|
||||
InstanceInfo instanceInfo = mock(InstanceInfo.class);
|
||||
InstancesInfo instancesInfo = mock(InstancesInfo.class);
|
||||
given(instancesInfo.getInstances()).willReturn(
|
||||
Collections.singletonList(instanceInfo));
|
||||
given(instanceInfo.getState()).willReturn(InstanceState.RUNNING);
|
||||
|
||||
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
|
||||
.willReturn(instancesInfo);
|
||||
|
||||
this.cloudFoundryDiscoveryClient = new CloudFoundryDiscoveryClient(
|
||||
this.cloudFoundryClient, environment);
|
||||
this.ops = mock(CloudFoundryOperations.class);
|
||||
this.svc = mock(CloudFoundryService.class);
|
||||
this.cloudFoundryDiscoveryClient = new CloudFoundryDiscoveryClient(this.ops, this.svc);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceResolution() {
|
||||
Applications apps = mock(Applications.class);
|
||||
ApplicationSummary s = ApplicationSummary.builder()
|
||||
.id(UUID.randomUUID().toString())
|
||||
.instances(2)
|
||||
.memoryLimit(1024)
|
||||
.requestedState("requestedState")
|
||||
.diskQuota(1024)
|
||||
.name(this.hiServiceServiceId)
|
||||
.runningInstances(2)
|
||||
.build();
|
||||
Mockito.when(apps.list()).thenReturn(Flux.just(s));
|
||||
Mockito.when(this.ops.applications()).thenReturn(apps);
|
||||
List<String> serviceNames = this.cloudFoundryDiscoveryClient.getServices();
|
||||
|
||||
Assert.assertTrue("there should be one registered service.",
|
||||
serviceNames.contains(this.hiServiceServiceId));
|
||||
|
||||
for (String serviceName : serviceNames) {
|
||||
this.log.debug("\t discovered serviceName: " + serviceName);
|
||||
}
|
||||
Assert.assertTrue("there should be one registered service.", serviceNames.contains(this.hiServiceServiceId));
|
||||
serviceNames.forEach(serviceName -> this.log.debug("\t discovered serviceName: " + serviceName));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstances() {
|
||||
ApplicationDetail applicationDetail = ApplicationDetail
|
||||
.builder()
|
||||
.instances(2)
|
||||
.name("my-app")
|
||||
.stack("stack")
|
||||
.memoryLimit(1024)
|
||||
.id("id")
|
||||
.requestedState("requestedState")
|
||||
.runningInstances(2)
|
||||
.url("http://my-app.cfapps.io")
|
||||
.diskQuota(20)
|
||||
.build();
|
||||
InstanceDetail instanceDetail = InstanceDetail
|
||||
.builder()
|
||||
.index("0")
|
||||
.build();
|
||||
Tuple2<ApplicationDetail, InstanceDetail> tuple2 = Tuples.of(applicationDetail, instanceDetail);
|
||||
Mockito.when(svc.getApplicationInstances(this.hiServiceServiceId)).thenReturn(Flux.just(tuple2));
|
||||
List<ServiceInstance> instances = this.cloudFoundryDiscoveryClient
|
||||
.getInstances(this.hiServiceServiceId);
|
||||
assertEquals("Wrong instances: " + instances, 1, instances.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstancesNotAvailable() {
|
||||
given(this.cloudFoundryClient.getApplication(this.hiServiceServiceId)).willThrow(new RuntimeException("Planned"));
|
||||
List<ServiceInstance> instances = this.cloudFoundryDiscoveryClient
|
||||
.getInstances(this.hiServiceServiceId);
|
||||
assertEquals("Wrong instances: " + instances, 0, instances.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalServiceInstanceRunning() {
|
||||
|
||||
given(this.cloudFoundryClient.getApplication("application"))
|
||||
.willReturn(this.cloudApplication);
|
||||
InstanceInfo instanceInfo = mock(InstanceInfo.class);
|
||||
InstancesInfo instancesInfo = mock(InstancesInfo.class);
|
||||
given(instancesInfo.getInstances()).willReturn(
|
||||
Collections.singletonList(instanceInfo));
|
||||
given(instanceInfo.getState()).willReturn(InstanceState.RUNNING);
|
||||
|
||||
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
|
||||
.willReturn(instancesInfo);
|
||||
|
||||
ServiceInstance localServiceInstance = this.cloudFoundryDiscoveryClient
|
||||
.getLocalServiceInstance();
|
||||
assertTrue(localServiceInstance.getHost().contains("hi-service.cfapps.io"));
|
||||
assertTrue(localServiceInstance.getServiceId().equals(this.hiServiceServiceId));
|
||||
assertEquals(localServiceInstance.getPort(), 80);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalServiceInstanceNotRunning() {
|
||||
|
||||
InstanceInfo instanceInfo = mock(InstanceInfo.class);
|
||||
InstancesInfo instancesInfo = mock(InstancesInfo.class);
|
||||
given(instancesInfo.getInstances()).willReturn(
|
||||
Collections.singletonList(instanceInfo));
|
||||
given(instanceInfo.getState()).willReturn(InstanceState.CRASHED);
|
||||
|
||||
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
|
||||
.willReturn(instancesInfo);
|
||||
|
||||
ServiceInstance localServiceInstance = this.cloudFoundryDiscoveryClient
|
||||
.getLocalServiceInstance();
|
||||
assertNull(localServiceInstance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalServiceInstanceNotFound() {
|
||||
|
||||
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
|
||||
.willThrow(new CloudFoundryException(HttpStatus.NOT_FOUND));
|
||||
|
||||
ServiceInstance localServiceInstance = this.cloudFoundryDiscoveryClient
|
||||
.getLocalServiceInstance();
|
||||
assertNull(localServiceInstance);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,22 +17,24 @@
|
||||
package org.springframework.cloud.cloudfoundry.discovery;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
|
||||
import org.cloudfoundry.client.lib.CloudFoundryClient;
|
||||
import org.cloudfoundry.client.lib.CloudFoundryException;
|
||||
import org.cloudfoundry.client.lib.domain.CloudApplication;
|
||||
import org.cloudfoundry.operations.applications.ApplicationDetail;
|
||||
import org.cloudfoundry.operations.applications.InstanceDetail;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.util.function.Tuple2;
|
||||
import reactor.util.function.Tuples;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author <A href="mailto:josh@Joshlong.com">Josh Long</A>
|
||||
@@ -41,52 +43,53 @@ public class CloudFoundryServerListTest {
|
||||
|
||||
private CloudFoundryServerList cloudFoundryServerList;
|
||||
private String serviceId = "foo-service";
|
||||
private CloudFoundryClient cloudFoundryClient;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
CloudApplication cloudApplication = mock(CloudApplication.class);
|
||||
given(cloudApplication.getUris()).will(new Answer<List<String>>() {
|
||||
@Override
|
||||
public List<String> answer(InvocationOnMock invocationOnMock)
|
||||
throws Throwable {
|
||||
return Arrays.asList("a-url.com", "b-url.com");
|
||||
}
|
||||
});
|
||||
|
||||
cloudFoundryClient = mock(CloudFoundryClient.class);
|
||||
given(cloudFoundryClient.getApplication(this.serviceId)).willReturn(
|
||||
cloudApplication);
|
||||
|
||||
IClientConfig iClientConfig = mock(IClientConfig.class);
|
||||
given(iClientConfig.getClientName()).willReturn(this.serviceId);
|
||||
when(iClientConfig.getClientName()).thenReturn(this.serviceId);
|
||||
|
||||
this.cloudFoundryServerList = new CloudFoundryServerList(cloudFoundryClient);
|
||||
CloudFoundryService cfs = mock(CloudFoundryService.class);
|
||||
ApplicationDetail applicationDetail = ApplicationDetail
|
||||
.builder()
|
||||
.instances(2)
|
||||
.name("my-app")
|
||||
.stack("stack")
|
||||
.memoryLimit(1024)
|
||||
.id("id")
|
||||
.requestedState("requestedState")
|
||||
.runningInstances(2)
|
||||
.url("http://my-app.cfapps.io")
|
||||
.diskQuota(20)
|
||||
.build();
|
||||
|
||||
InstanceDetail instanceDetail = InstanceDetail
|
||||
.builder()
|
||||
.index("0")
|
||||
.build();
|
||||
|
||||
Tuple2<ApplicationDetail, InstanceDetail> tuple2 = Tuples.of(applicationDetail, instanceDetail);
|
||||
Mockito.when(cfs.getApplicationInstances(this.serviceId)).thenReturn(Flux.just(tuple2));
|
||||
|
||||
this.cloudFoundryServerList = new CloudFoundryServerList(cfs);
|
||||
this.cloudFoundryServerList.initWithNiwsConfig(iClientConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListOfServers() {
|
||||
List<CloudFoundryServer> initialListOfServers = this.cloudFoundryServerList
|
||||
.getInitialListOfServers();
|
||||
List<CloudFoundryServer> updatedListOfServers = this.cloudFoundryServerList
|
||||
.getUpdatedListOfServers();
|
||||
List<CloudFoundryServer> initialListOfServers = this.cloudFoundryServerList.getInitialListOfServers();
|
||||
List<CloudFoundryServer> updatedListOfServers = this.cloudFoundryServerList.getUpdatedListOfServers();
|
||||
Assert.assertEquals(updatedListOfServers, initialListOfServers);
|
||||
Assert.assertTrue(initialListOfServers.size() == 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListOfServersFails() {
|
||||
given(cloudFoundryClient.getApplication(this.serviceId)).willThrow(
|
||||
new CloudFoundryException(HttpStatus.NOT_FOUND));
|
||||
List<CloudFoundryServer> initialListOfServers = this.cloudFoundryServerList
|
||||
.getInitialListOfServers();
|
||||
Assert.assertTrue(initialListOfServers.size() == 0);
|
||||
public void testInit() throws Exception {
|
||||
Field field = ReflectionUtils.findField(this.cloudFoundryServerList.getClass(), "serviceId");
|
||||
assert field != null;
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
Assert.assertEquals(String.class.cast(field.get(this.cloudFoundryServerList)), this.serviceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit() {
|
||||
Assert.assertEquals(this.cloudFoundryServerList.serviceId, this.serviceId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2015 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
|
||||
*
|
||||
* http://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.cloudfoundry.discovery;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import org.cloudfoundry.client.lib.domain.CloudApplication;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
|
||||
/**
|
||||
* @author <A href="mailto:josh@Joshlong.com">Josh Long</A>
|
||||
*/
|
||||
public class CloudFoundryServerTest {
|
||||
|
||||
private CloudFoundryServer cloudFoundryServer;
|
||||
private List<String> urls = Arrays.asList("a-url.com", "b-url.com");
|
||||
private String serverName = "server-name";
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
CloudApplication cloudApplication = mock(CloudApplication.class);
|
||||
given(cloudApplication.getUris()).willReturn(this.urls);
|
||||
given(cloudApplication.getName()).willReturn(this.serverName);
|
||||
given(cloudApplication.getRunningInstances()).willReturn(1);
|
||||
this.cloudFoundryServer = new CloudFoundryServer(cloudApplication);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProperConstruction() {
|
||||
Server.MetaInfo metaInfo = this.cloudFoundryServer.getMetaInfo();
|
||||
|
||||
Assert.assertEquals(metaInfo.getAppName(), this.serverName);
|
||||
Assert.assertEquals(metaInfo.getServiceIdForDiscovery(), this.serverName);
|
||||
Assert.assertEquals(metaInfo.getInstanceId(), this.serverName);
|
||||
Assert.assertEquals(this.cloudFoundryServer.getHost(), this.urls.get(0));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user