This commit is contained in:
@@ -115,6 +115,53 @@ public class Cloud {
|
||||
return matchingServiceInfos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all {@link ServiceInfo}s for the given service info type.
|
||||
*
|
||||
* <p>
|
||||
* Unlike {@link #getServiceInfos(Class)} which checks if the service info
|
||||
* can be mapped to the given service connector type, this method only
|
||||
* checks the type of the service info.
|
||||
*
|
||||
* @param <T> the class of service info to return
|
||||
* @param serviceInfoType
|
||||
* service info type
|
||||
* @return a list of service info of the given type
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ServiceInfo> List<T> getServiceInfosByType(Class<T> serviceInfoType) {
|
||||
List<ServiceInfo> allServiceInfos = getServiceInfos();
|
||||
|
||||
List<T> matchingServiceInfos = new ArrayList<T>();
|
||||
for (ServiceInfo serviceInfo : allServiceInfos) {
|
||||
if (serviceInfoType.isAssignableFrom(serviceInfo.getClass())) {
|
||||
matchingServiceInfos.add((T) serviceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
return matchingServiceInfos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the singleton {@link ServiceInfo} for the given service info type.
|
||||
*
|
||||
* @param <T> the class of service info to return
|
||||
* @param serviceInfoType
|
||||
* service info type
|
||||
* @return the single service info of the given type
|
||||
* @throws CloudException
|
||||
* if there are either 0 or more than 1 service info of the
|
||||
* given type.
|
||||
*/
|
||||
public <T extends ServiceInfo> T getSingletonServiceInfoByType(Class<T> serviceInfoType) {
|
||||
List<T> serviceInfos = getServiceInfosByType(serviceInfoType);
|
||||
if (serviceInfos.size() != 1) {
|
||||
throw new CloudException(
|
||||
"No unique service info " + serviceInfoType + " found. Expected 1, found " + serviceInfos.size());
|
||||
}
|
||||
return serviceInfos.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a service connector for the given service id, the connector type, configured with the given config
|
||||
*
|
||||
|
||||
@@ -16,6 +16,8 @@ import org.springframework.cloud.CloudTestUtil.StubApplicationInstanceInfo;
|
||||
import org.springframework.cloud.CloudTestUtil.StubCloudConnector;
|
||||
import org.springframework.cloud.CloudTestUtil.StubCompositeServiceInfo;
|
||||
import org.springframework.cloud.CloudTestUtil.StubServiceInfo;
|
||||
import org.springframework.cloud.CloudTestUtil.TestServiceInfoTypeA;
|
||||
import org.springframework.cloud.CloudTestUtil.TestServiceInfoTypeB;
|
||||
import org.springframework.cloud.service.BaseServiceInfo;
|
||||
import org.springframework.cloud.service.ServiceConnectorConfig;
|
||||
import org.springframework.cloud.service.ServiceConnectorCreator;
|
||||
@@ -217,7 +219,48 @@ public class CloudTest {
|
||||
assertNotNull(testCloud.getServiceInfo("test-id-2a"));
|
||||
assertNotNull(testCloud.getServiceInfo("test-id-2b"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getServiceInfosByType() {
|
||||
StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
|
||||
TestServiceInfoTypeA testServiceInfoTypeA1 = new TestServiceInfoTypeA("test-id-a1");
|
||||
TestServiceInfoTypeA testServiceInfoTypeA2 = new TestServiceInfoTypeA("test-id-a2");
|
||||
TestServiceInfoTypeB testServiceInfoTypeB = new TestServiceInfoTypeB("test-id-b");
|
||||
StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo, testServiceInfoTypeA1, testServiceInfoTypeA2, testServiceInfoTypeB);
|
||||
Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
|
||||
|
||||
List<TestServiceInfoTypeA> actualServiceInfoTypeA = testCloud.getServiceInfosByType(TestServiceInfoTypeA.class);
|
||||
assertEquals(2, actualServiceInfoTypeA.size());
|
||||
assertEquals(1, testCloud.getServiceInfosByType(TestServiceInfoTypeB.class).size());
|
||||
}
|
||||
|
||||
@Test(expected=CloudException.class)
|
||||
public void getSingletonServiceInfoByTypeNoService() {
|
||||
StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector();
|
||||
Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
|
||||
|
||||
testCloud.getSingletonServiceInfoByType(StubServiceInfo.class);
|
||||
}
|
||||
|
||||
@Test(expected=CloudException.class)
|
||||
public void getSingletonServiceInfoByTypeMultipleServices() {
|
||||
StubServiceInfo testServiceInfo1 = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
|
||||
StubServiceInfo testServiceInfo2 = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
|
||||
StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo1, testServiceInfo2);
|
||||
Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
|
||||
|
||||
testCloud.getSingletonServiceInfoByType(StubServiceInfo.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSingletonServiceInfoByTypeSingleService() {
|
||||
StubServiceInfo testServiceInfo = new StubServiceInfo("test-id", "test-host", 1000, "test-username", "test-password");
|
||||
StubCloudConnector stubCloudConnector = CloudTestUtil.getTestCloudConnector(testServiceInfo);
|
||||
Cloud testCloud = new Cloud(stubCloudConnector, serviceConnectorCreators);
|
||||
|
||||
assertNotNull(testCloud.getSingletonServiceInfoByType(StubServiceInfo.class));
|
||||
}
|
||||
|
||||
private void assertStubServiceProp(String leadKey, StubServiceInfo serviceInfo, Properties cloudProperties) {
|
||||
CloudTestUtil.assertBasicProps(leadKey, serviceInfo, cloudProperties);
|
||||
|
||||
|
||||
@@ -9,12 +9,12 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.cloud.CloudConnector;
|
||||
import org.springframework.cloud.app.ApplicationInstanceInfo;
|
||||
import org.springframework.cloud.service.BaseServiceInfo;
|
||||
import org.springframework.cloud.service.CompositeServiceInfo;
|
||||
import org.springframework.cloud.service.UriBasedServiceInfo;
|
||||
import org.springframework.cloud.service.ServiceInfo;
|
||||
import org.springframework.cloud.service.ServiceInfo.ServiceLabel;
|
||||
import org.springframework.cloud.service.UriBasedServiceInfo;
|
||||
|
||||
/**
|
||||
* Class to support testing.
|
||||
@@ -98,6 +98,18 @@ public class CloudTestUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestServiceInfoTypeA extends BaseServiceInfo {
|
||||
public TestServiceInfoTypeA(String id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TestServiceInfoTypeB extends BaseServiceInfo {
|
||||
public TestServiceInfoTypeB(String id) {
|
||||
super(id);
|
||||
}
|
||||
}
|
||||
|
||||
public static class StubCompositeServiceInfo implements CompositeServiceInfo {
|
||||
private String id;
|
||||
private List<ServiceInfo> constituents;
|
||||
|
||||
Reference in New Issue
Block a user