tests for all ServiceInfoCreators

This commit is contained in:
Christopher Smith
2014-07-11 16:28:09 -05:00
parent 7cf18cce1b
commit 14272c8b10
10 changed files with 183 additions and 4 deletions

View File

@@ -79,6 +79,9 @@ public class LocalConfigConnector extends AbstractCloudConnector<KeyValuePair> {
@Override
protected List<KeyValuePair> getServicesData() {
if(fileProperties == null)
throw new IllegalStateException("isInMatchingCloud() must be called first to initialize connector");
LinkedHashMap<String, Properties> propertySources = new LinkedHashMap<String, Properties>();
propertySources.put("programmatic properties", programmaticProperties);

View File

@@ -23,7 +23,11 @@ public final class LocalConfigUtil {
// iterate over the property sources in order, extracting matching properties
for (Map.Entry<String, Properties> propertySource : propertySources.entrySet()) {
logger.info("reading services from " + propertySource.getValue());
if(propertySource.getValue().isEmpty()) {
logger.info("no " + propertySource.getKey());
continue;
}
logger.info("reading services from " + propertySource.getKey());
Map<String, String> services = readServices(propertySource.getValue());
// add each of the found services to the list, warning about duplicates

View File

@@ -0,0 +1,54 @@
package org.springframework.cloud.localconfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.UriBasedServiceInfo;
public class AbstractLocalConfigConnectorTest {
public static final String PROPERTIES_FILE = "localconfig.testuris.properties";
protected LocalConfigConnector connector = new LocalConfigConnector();
protected static final String HOSTNAME = "10.20.30.40";
protected static final int PORT = 1234;
protected static final String USERNAME = "myuser";
protected static final String PASSWORD = "mypass";
@Before
public void init() throws IOException {
InputStream propertiesFile = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
LocalConfigConnector.supplyProperties(propertiesFile);
assertTrue(connector.isInMatchingCloud());
}
@After
public void clearProperties() {
LocalConfigConnector.programmaticProperties = new Properties();
}
protected static ServiceInfo getServiceInfo(List<ServiceInfo> serviceInfos, String serviceId) {
for (ServiceInfo serviceInfo : serviceInfos) {
if (serviceInfo.getId().equals(serviceId)) {
return serviceInfo;
}
}
return null;
}
protected static void assertUriParameters(UriBasedServiceInfo serviceInfo) {
assertEquals(HOSTNAME, serviceInfo.getHost());
assertEquals(PORT, serviceInfo.getPort());
assertEquals(USERNAME, serviceInfo.getUserName());
assertEquals(PASSWORD, serviceInfo.getPassword());
}
}

View File

@@ -0,0 +1,23 @@
package org.springframework.cloud.localconfig;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.AmqpServiceInfo;
public class LocalConfigConnectorAmqpServiceTest extends AbstractLocalConfigConnectorTest {
@Test
public void serviceCreation() {
List<ServiceInfo> services = connector.getServiceInfos();
ServiceInfo service = getServiceInfo(services, "rabbit");
assertNotNull(service);
assertTrue(service instanceof AmqpServiceInfo);
assertUriParameters((AmqpServiceInfo) service);
}
}

View File

@@ -0,0 +1,23 @@
package org.springframework.cloud.localconfig;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.MongoServiceInfo;
public class LocalConfigConnectorMongoServiceTest extends AbstractLocalConfigConnectorTest {
@Test
public void serviceCreation() {
List<ServiceInfo> services = connector.getServiceInfos();
ServiceInfo service = getServiceInfo(services, "candygram");
assertNotNull(service);
assertTrue(service instanceof MongoServiceInfo);
assertUriParameters((MongoServiceInfo) service);
}
}

View File

@@ -0,0 +1,23 @@
package org.springframework.cloud.localconfig;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.MysqlServiceInfo;
public class LocalConfigConnectorMysqlServiceTest extends AbstractLocalConfigConnectorTest {
@Test
public void serviceCreation() {
List<ServiceInfo> services = connector.getServiceInfos();
ServiceInfo service = getServiceInfo(services, "maria");
assertNotNull(service);
assertTrue(service instanceof MysqlServiceInfo);
assertUriParameters((MysqlServiceInfo) service);
}
}

View File

@@ -0,0 +1,23 @@
package org.springframework.cloud.localconfig;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.PostgresqlServiceInfo;
public class LocalConfigConnectorPostgresqlServiceTest extends AbstractLocalConfigConnectorTest {
@Test
public void serviceCreation() {
List<ServiceInfo> services = connector.getServiceInfos();
ServiceInfo service = getServiceInfo(services, "ingres");
assertNotNull(service);
assertTrue(service instanceof PostgresqlServiceInfo);
assertUriParameters((PostgresqlServiceInfo) service);
}
}

View File

@@ -0,0 +1,23 @@
package org.springframework.cloud.localconfig;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.Test;
import org.springframework.cloud.service.ServiceInfo;
import org.springframework.cloud.service.common.RedisServiceInfo;
public class LocalConfigConnectorRedisServiceTest extends AbstractLocalConfigConnectorTest {
@Test
public void serviceCreation() {
List<ServiceInfo> services = connector.getServiceInfos();
ServiceInfo service = getServiceInfo(services, "blue");
assertNotNull(service);
assertTrue(service instanceof RedisServiceInfo);
assertUriParameters((RedisServiceInfo) service);
}
}

View File

@@ -1,3 +0,0 @@
spring.cloud.appId: testApp
spring.cloud.foo: bar
spring.cloud.baz: quux

View File

@@ -0,0 +1,6 @@
spring.cloud.appId: testAppWithUris
spring.cloud.rabbit: amqp://myuser:mypass@10.20.30.40:1234/queue
spring.cloud.maria: mysql://myuser:mypass@10.20.30.40:1234/dbname
spring.cloud.candygram: mongodb://myuser:mypass@10.20.30.40:1234/dbname
spring.cloud.ingres: postgres://myuser:mypass@10.20.30.40:1234/dbname
spring.cloud.blue: redis://myuser:mypass@10.20.30.40:1234/dbname