Commit 933b532b by Jason Song

refactor config client - add config repository

parent b78fadf0
......@@ -5,6 +5,8 @@ import java.util.Properties;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public interface ConfigLoader {
public interface ConfigRepository {
public Properties loadConfig();
public void setFallback(ConfigRepository fallbackConfigRepository);
}
......@@ -2,36 +2,36 @@ package com.ctrip.apollo.internals;
import com.ctrip.apollo.Config;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.apollo.util.ConfigUtil;
import com.dianping.cat.Cat;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class DefaultConfig implements Config, ConfigLoader {
public class DefaultConfig implements Config {
private final String m_namespace;
private final File m_baseDir;
private Properties m_resourceProperties;
private Properties m_fileProperties;
private ConfigLoader m_fallbackLoader;
private ConfigUtil m_configUtil;
private Properties m_configProperties;
private ConfigRepository m_configRepository;
public DefaultConfig(File baseDir, String namespace, ConfigLoader fallbackLoader, ConfigUtil configUtil) {
public DefaultConfig(String namespace, ConfigRepository configRepository) {
m_namespace = namespace;
m_baseDir = baseDir;
m_resourceProperties = loadFromResource(m_namespace);
m_fallbackLoader = fallbackLoader;
m_configUtil = configUtil;
this.initLocalConfig();
m_configRepository = configRepository;
initialize();
}
private void initialize() {
try {
m_configProperties = m_configRepository.loadConfig();
} catch (Throwable ex) {
throw new RuntimeException(
String.format("Init Apollo Local Config failed - namespace: %s",
m_namespace), ex);
}
}
@Override
......@@ -41,7 +41,7 @@ public class DefaultConfig implements Config, ConfigLoader {
// step 2: check local cached properties file
if (value == null) {
value = m_fileProperties.getProperty(key);
value = m_configProperties.getProperty(key);
}
/**
......@@ -87,97 +87,5 @@ public class DefaultConfig implements Config, ConfigLoader {
return properties;
}
void initLocalConfig() {
m_fileProperties = this.loadFromLocalCacheFile(m_baseDir, m_namespace);
//TODO check if local file is expired
if (m_fileProperties != null) {
return;
}
if (m_fallbackLoader != null) {
m_fileProperties = m_fallbackLoader.loadConfig();
}
if (m_fileProperties == null) {
throw new RuntimeException(
String.format("Init Apollo Local Config failed - namespace: %s",
m_namespace));
}
persistLocalCacheFile(m_baseDir, m_namespace);
}
private Properties loadFromLocalCacheFile(File baseDir, String namespace) {
if (baseDir == null) {
return null;
}
File file = assembleLocalCacheFile(baseDir, namespace);
Properties properties = null;
if (file.isFile() && file.canRead()) {
InputStream in = null;
try {
in = new FileInputStream(file);
properties = new Properties();
properties.load(in);
} catch (IOException e) {
Cat.logError(e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
// ignore
}
}
} else {
//TODO error handling
}
return properties;
}
void persistLocalCacheFile(File baseDir, String namespace) {
if (baseDir == null) {
return;
}
File file = assembleLocalCacheFile(baseDir, namespace);
OutputStream out = null;
try {
out = new FileOutputStream(file);
m_fileProperties.store(out, "Persisted by DefaultConfig");
} catch (FileNotFoundException ex) {
Cat.logError(ex);
} catch (IOException ex) {
Cat.logError(ex);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
//ignore
}
}
}
}
File assembleLocalCacheFile(File baseDir, String namespace) {
String fileName = String.format("%s-%s-%s.properties", m_configUtil.getAppId(),
m_configUtil.getCluster(), namespace);
return new File(baseDir, fileName);
}
@Override
public Properties loadConfig() {
Properties result = new Properties();
result.putAll(m_fileProperties);
return result;
}
public ConfigLoader getFallbackLoader() {
return m_fallbackLoader;
}
}
package com.ctrip.apollo.internals;
import com.google.common.base.Preconditions;
import com.ctrip.apollo.util.ConfigUtil;
import com.dianping.cat.Cat;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class LocalFileConfigRepository implements ConfigRepository {
private final String m_namespace;
private final File m_baseDir;
private final ConfigUtil m_configUtil;
private Properties m_fileProperties;
private ConfigRepository m_fallback;
public LocalFileConfigRepository(File baseDir, String namespace, ConfigUtil configUtil) {
m_baseDir = baseDir;
m_namespace = namespace;
m_configUtil = configUtil;
}
@Override
public Properties loadConfig() {
if (m_fileProperties == null) {
initLocalConfig();
}
Properties result = new Properties();
result.putAll(m_fileProperties);
return result;
}
@Override
public void setFallback(ConfigRepository fallbackConfigRepository) {
m_fallback = fallbackConfigRepository;
}
void initLocalConfig() {
if (m_fallback != null) {
try {
m_fileProperties = m_fallback.loadConfig();
//TODO register change listener
persistLocalCacheFile(m_baseDir, m_namespace);
return;
} catch (Throwable ex) {
Cat.logError(ex);
}
}
try {
m_fileProperties = this.loadFromLocalCacheFile(m_baseDir, m_namespace);
} catch (IOException ex) {
throw new RuntimeException("Loading config from local cache file failed", ex);
}
}
private Properties loadFromLocalCacheFile(File baseDir, String namespace) throws IOException {
Preconditions.checkNotNull(baseDir, "Basedir cannot be null");
File file = assembleLocalCacheFile(baseDir, namespace);
Properties properties = null;
if (file.isFile() && file.canRead()) {
InputStream in = null;
try {
in = new FileInputStream(file);
properties = new Properties();
properties.load(in);
} catch (IOException e) {
Cat.logError(e);
throw e;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
// ignore
}
}
} else {
//TODO error handling
}
return properties;
}
void persistLocalCacheFile(File baseDir, String namespace) {
if (baseDir == null) {
return;
}
File file = assembleLocalCacheFile(baseDir, namespace);
OutputStream out = null;
try {
out = new FileOutputStream(file);
m_fileProperties.store(out, "Persisted by DefaultConfig");
} catch (FileNotFoundException ex) {
Cat.logError(ex);
} catch (IOException ex) {
Cat.logError(ex);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
//ignore
}
}
}
}
File assembleLocalCacheFile(File baseDir, String namespace) {
String fileName = String.format("%s-%s-%s.properties", m_configUtil.getAppId(),
m_configUtil.getCluster(), namespace);
return new File(baseDir, fileName);
}
}
......@@ -2,7 +2,6 @@ package com.ctrip.apollo.internals;
import com.google.common.collect.Maps;
import com.ctrip.apollo.Config;
import com.ctrip.apollo.core.dto.ApolloConfig;
import com.ctrip.apollo.core.dto.ServiceDTO;
import com.ctrip.apollo.util.ConfigUtil;
......@@ -23,44 +22,45 @@ import java.util.Properties;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class RemoteConfig implements Config, ConfigLoader {
private static final Logger logger = LoggerFactory.getLogger(RemoteConfig.class);
public class RemoteConfigRepository implements ConfigRepository {
private static final Logger logger = LoggerFactory.getLogger(RemoteConfigRepository.class);
private RestTemplate m_restTemplate;
private ConfigServiceLocator m_serviceLocator;
private String m_namespace;
private ConfigUtil m_configUtil;
private Properties m_remoteProperties;
private String m_namespace;
public RemoteConfig(RestTemplate restTemplate,
ConfigServiceLocator locator, String namespace, ConfigUtil configUtil) {
this.m_restTemplate = restTemplate;
this.m_serviceLocator = locator;
this.m_namespace = namespace;
this.m_configUtil = configUtil;
this.initialize();
}
@Override
public String getProperty(String key, String defaultValue) {
return this.m_remoteProperties.getProperty(key, defaultValue);
public RemoteConfigRepository(RestTemplate restTemplate,
ConfigServiceLocator serviceLocator,
ConfigUtil configUtil, String namespace) {
m_restTemplate = restTemplate;
m_serviceLocator = serviceLocator;
m_configUtil = configUtil;
m_namespace = namespace;
}
@Override
public Properties loadConfig() {
if (m_remoteProperties == null) {
return null;
initRemoteConfig();
}
Properties result = new Properties();
result.putAll(m_remoteProperties);
return result;
}
private void initialize() {
@Override
public void setFallback(ConfigRepository fallbackConfigRepository) {
//remote config doesn't need fallback
}
private void initRemoteConfig() {
ApolloConfig apolloConfig = this.loadApolloConfig();
m_remoteProperties = new Properties();
m_remoteProperties.putAll(apolloConfig.getConfigurations());
}
private ApolloConfig loadApolloConfig() {
String appId = m_configUtil.getAppId();
String cluster = m_configUtil.getCluster();
......@@ -82,9 +82,10 @@ public class RemoteConfig implements Config, ConfigLoader {
}
}
private ApolloConfig getRemoteConfig(RestTemplate restTemplate, String uri,
String appId, String cluster, String namespace,
ApolloConfig previousConfig) {
String appId, String cluster, String namespace,
ApolloConfig previousConfig) {
logger.info("Loading config from {}, appId={}, cluster={}, namespace={}", uri, appId, cluster,
namespace);
......@@ -136,5 +137,4 @@ public class RemoteConfig implements Config, ConfigLoader {
}
return services.get(0).getHomepageUrl();
}
}
package com.ctrip.apollo.internals;
import com.ctrip.apollo.Config;
import java.util.Properties;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class SimpleConfig implements Config {
private String m_namespace;
private ConfigRepository m_configRepository;
private Properties m_configProperties;
public SimpleConfig(String namespace, ConfigRepository configRepository) {
m_namespace = namespace;
m_configRepository = configRepository;
this.initialize();
}
private void initialize() {
try {
m_configProperties = m_configRepository.loadConfig();
} catch (Throwable ex) {
throw new RuntimeException(
String.format("Init Apollo Remote Config failed - namespace: %s",
m_namespace), ex);
}
}
@Override
public String getProperty(String key, String defaultValue) {
return this.m_configProperties.getProperty(key, defaultValue);
}
}
......@@ -2,9 +2,8 @@ package com.ctrip.apollo.spi;
import com.ctrip.apollo.Config;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.apollo.internals.ConfigServiceLocator;
import com.ctrip.apollo.internals.DefaultConfig;
import com.ctrip.apollo.internals.RemoteConfig;
import com.ctrip.apollo.internals.*;
import com.ctrip.apollo.internals.RemoteConfigRepository;
import com.ctrip.apollo.util.ConfigUtil;
import org.springframework.web.client.RestTemplate;
......@@ -29,14 +28,20 @@ public class DefaultConfigFactory implements ConfigFactory {
@Override
public Config create(String namespace) {
RemoteConfig remoteConfig = this.createRemoteConfig(namespace);
DefaultConfig defaultConfig =
new DefaultConfig(m_baseDir, namespace, remoteConfig, ConfigUtil.getInstance());
DefaultConfig defaultConfig = new DefaultConfig(namespace, createLocalConfigRepository(namespace));
return defaultConfig;
}
public RemoteConfig createRemoteConfig(String namespace) {
return new RemoteConfig(new RestTemplate(), new ConfigServiceLocator(), namespace,
ConfigUtil.getInstance());
LocalFileConfigRepository createLocalConfigRepository(String namespace) {
LocalFileConfigRepository
localFileConfigLoader =
new LocalFileConfigRepository(m_baseDir, namespace, ConfigUtil.getInstance());
localFileConfigLoader.setFallback(createRemoteConfigRepository(namespace));
return localFileConfigLoader;
}
RemoteConfigRepository createRemoteConfigRepository(String namespace) {
return new RemoteConfigRepository(new RestTemplate(), new ConfigServiceLocator(),
ConfigUtil.getInstance(), namespace);
}
}
......@@ -3,7 +3,9 @@ package com.ctrip.apollo;
import com.ctrip.apollo.internals.DefaultConfigManagerTest;
import com.ctrip.apollo.internals.DefaultConfigTest;
import com.ctrip.apollo.internals.RemoteConfigTest;
import com.ctrip.apollo.internals.LocalFileConfigRepositoryTest;
import com.ctrip.apollo.internals.RemoteConfigRepositoryTest;
import com.ctrip.apollo.internals.SimpleConfigTest;
import com.ctrip.apollo.spi.DefaultConfigFactoryManagerTest;
import com.ctrip.apollo.spi.DefaultConfigFactoryTest;
import com.ctrip.apollo.spi.DefaultConfigRegistryTest;
......@@ -15,8 +17,8 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
ConfigServiceTest.class, DefaultConfigRegistryTest.class, DefaultConfigFactoryManagerTest.class,
DefaultConfigFactoryTest.class, DefaultConfigManagerTest.class, DefaultConfigTest.class,
RemoteConfigTest.class
DefaultConfigManagerTest.class, DefaultConfigTest.class, LocalFileConfigRepositoryTest.class,
RemoteConfigRepositoryTest.class, SimpleConfigTest.class, DefaultConfigFactoryTest.class
})
public class AllTests {
......
......@@ -11,7 +11,6 @@ import org.unidal.lookup.ComponentTestCase;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* @author Jason Song(song_s@ctrip.com)
......
......@@ -4,59 +4,37 @@ import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.ctrip.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.apollo.util.ConfigUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import java.io.File;
import java.util.Properties;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Jason Song(song_s@ctrip.com)
*/
public class DefaultConfigTest extends ComponentTestCase {
private File someBaseDir;
public class DefaultConfigTest {
private File someResourceDir;
private String someNamespace;
private ConfigLoader fallbackLoader;
private ConfigRepository configRepository;
private Properties someProperties;
private ConfigUtil someConfigUtil;
@Before
public void setUp() throws Exception {
super.setUp();
someBaseDir = new File("src/test/resources/config-cache");
someBaseDir.mkdir();
someResourceDir = new File(ClassLoaderUtil.getClassPath() + "/META-INF/config");
someResourceDir.mkdir();
someNamespace = "someName";
someProperties = new Properties();
someProperties.setProperty("defaultKey", "defaultValue");
fallbackLoader = mock(RemoteConfig.class);
when(fallbackLoader.loadConfig()).thenReturn(someProperties);
String someAppId = "someApp";
String someCluster = "someCluster";
someConfigUtil = mock(ConfigUtil.class);
when(someConfigUtil.getAppId()).thenReturn(someAppId);
when(someConfigUtil.getCluster()).thenReturn(someCluster);
configRepository = mock(ConfigRepository.class);
}
@Override
@After
public void tearDown() throws Exception {
recursiveDelete(someBaseDir);
recursiveDelete(someResourceDir);
}
......@@ -73,47 +51,6 @@ public class DefaultConfigTest extends ComponentTestCase {
file.delete();
}
private String assembleLocalCacheFileName() {
return String.format("%s-%s-%s.properties", someConfigUtil.getAppId(),
someConfigUtil.getCluster(), someNamespace);
}
@Test
public void testGetPropertyWithLocalFile() throws Exception {
File file = new File(someBaseDir, assembleLocalCacheFileName());
String someKey = "someKey";
String someValue = "someValue";
Files.write(someKey + "=" + someValue, file, Charsets.UTF_8);
DefaultConfig defaultConfig = new DefaultConfig(someBaseDir, someNamespace, fallbackLoader, someConfigUtil);
file.delete();
assertEquals(someValue, defaultConfig.getProperty(someKey, null));
}
@Test
public void testGetPropertyWithLocalResource() throws Exception {
File file = new File(someResourceDir, someNamespace + ".properties");
file.deleteOnExit();
Files.createParentDirs(file);
String someKey = "someKey";
String someValue = "someValue";
Files.write(someKey + "=" + someValue, file, Charsets.UTF_8);
DefaultConfig
defaultConfig =
new DefaultConfig(someBaseDir, someNamespace, fallbackLoader, someConfigUtil);
file.delete();
assertEquals(someValue, defaultConfig.getProperty(someKey, null));
}
@Test
public void testGetPropertyWithAllPropertyHierarchy() throws Exception {
String someKey = "someKey";
......@@ -128,11 +65,11 @@ public class DefaultConfigTest extends ComponentTestCase {
//set up system property
System.setProperty(someKey, someSystemPropertyValue);
//set up local file
File localCacheFile = new File(someBaseDir, assembleLocalCacheFileName());
Files.write(someKey + "=" + someLocalFileValue, localCacheFile, Charsets.UTF_8);
Files.append(System.getProperty("line.separator"), localCacheFile, Charsets.UTF_8);
Files.append(anotherKey + "=" + someLocalFileValue, localCacheFile, Charsets.UTF_8);
//set up config repo
someProperties = new Properties();
someProperties.setProperty(someKey, someLocalFileValue);
someProperties.setProperty(anotherKey, someLocalFileValue);
when(configRepository.loadConfig()).thenReturn(someProperties);
//set up resource file
File resourceFile = new File(someResourceDir, someNamespace + ".properties");
......@@ -143,52 +80,19 @@ public class DefaultConfigTest extends ComponentTestCase {
Files.append(lastKey + "=" + someResourceValue, resourceFile, Charsets.UTF_8);
DefaultConfig
defaultConfig =
new DefaultConfig(someBaseDir, someNamespace, fallbackLoader, someConfigUtil);
defaultConfig =
new DefaultConfig(someNamespace, configRepository);
String someKeyValue = defaultConfig.getProperty(someKey, null);
String anotherKeyValue = defaultConfig.getProperty(anotherKey, null);
String lastKeyValue = defaultConfig.getProperty(lastKey, null);
localCacheFile.delete();
resourceFile.delete();
//clean up
System.clearProperty(someKey);
assertEquals(someSystemPropertyValue, someKeyValue);
assertEquals(someLocalFileValue, anotherKeyValue);
assertEquals(someResourceValue, lastKeyValue);
}
@Test
public void testInitLocalConfigWithNoLocalFile() throws Exception {
DefaultConfig
defaultConfig =
new DefaultConfig(someBaseDir, someNamespace, fallbackLoader, someConfigUtil);
Properties result = defaultConfig.loadConfig();
assertThat(
"Default config's properties should be the same as fallback loader's when there is no local cache",
result.entrySet(), equalTo(someProperties.entrySet()));
}
@Test
public void testInitLocalConfigWithNoLocalFileMultipleTimes() throws Exception {
Properties anotherProperties = new Properties();
anotherProperties.setProperty("anotherKey", "anotherValue");
ConfigLoader anotherLoader = mock(RemoteConfig.class);
when(anotherLoader.loadConfig()).thenReturn(anotherProperties);
DefaultConfig
defaultConfig =
new DefaultConfig(someBaseDir, someNamespace, fallbackLoader, someConfigUtil);
DefaultConfig
anotherConfig =
new DefaultConfig(someBaseDir, someNamespace, anotherLoader, someConfigUtil);
assertThat(
"Default config should persist local cache files and return that afterwards",
defaultConfig.loadConfig().entrySet(), equalTo(anotherConfig.loadConfig().entrySet()));
}
}
package com.ctrip.apollo.internals;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.ctrip.apollo.util.ConfigUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.Properties;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Created by Jason on 4/9/16.
*/
public class LocalFileConfigRepositoryTest {
private File someBaseDir;
private String someNamespace;
private ConfigRepository fallbackRepo;
private Properties someProperties;
private ConfigUtil someConfigUtil;
@Before
public void setUp() throws Exception {
someBaseDir = new File("src/test/resources/config-cache");
someBaseDir.mkdir();
someNamespace = "someName";
someProperties = new Properties();
someProperties.setProperty("defaultKey", "defaultValue");
fallbackRepo = mock(ConfigRepository.class);
when(fallbackRepo.loadConfig()).thenReturn(someProperties);
String someAppId = "someApp";
String someCluster = "someCluster";
someConfigUtil = mock(ConfigUtil.class);
when(someConfigUtil.getAppId()).thenReturn(someAppId);
when(someConfigUtil.getCluster()).thenReturn(someCluster);
}
@After
public void tearDown() throws Exception {
recursiveDelete(someBaseDir);
}
//helper method to clean created files
private void recursiveDelete(File file) {
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
for (File f : file.listFiles()) {
recursiveDelete(f);
}
}
file.delete();
}
private String assembleLocalCacheFileName() {
return String.format("%s-%s-%s.properties", someConfigUtil.getAppId(),
someConfigUtil.getCluster(), someNamespace);
}
@Test
public void testLoadConfig() throws Exception {
}
@Test
public void testLoadConfigWithLocalFile() throws Exception {
File file = new File(someBaseDir, assembleLocalCacheFileName());
String someKey = "someKey";
String someValue = "someValue";
Files.write(someKey + "=" + someValue, file, Charsets.UTF_8);
LocalFileConfigRepository localRepo = new LocalFileConfigRepository(someBaseDir, someNamespace, someConfigUtil);
Properties properties = localRepo.loadConfig();
assertEquals(someValue, properties.getProperty(someKey));
}
@Test
public void testLoadConfigWithNoLocalFile() throws Exception {
LocalFileConfigRepository
localFileConfigRepository =
new LocalFileConfigRepository(someBaseDir, someNamespace, someConfigUtil);
localFileConfigRepository.setFallback(fallbackRepo);
Properties result = localFileConfigRepository.loadConfig();
assertThat(
"LocalFileConfigRepository's properties should be the same as fallback repo's when there is no local cache",
result.entrySet(), equalTo(someProperties.entrySet()));
}
@Test
public void testLoadConfigWithNoLocalFileMultipleTimes() throws Exception {
LocalFileConfigRepository localRepo =
new LocalFileConfigRepository(someBaseDir, someNamespace, someConfigUtil);
localRepo.setFallback(fallbackRepo);
Properties someProperties = localRepo.loadConfig();
LocalFileConfigRepository
anotherLocalRepoWithNoFallback =
new LocalFileConfigRepository(someBaseDir, someNamespace, someConfigUtil);
Properties anotherProperties = anotherLocalRepoWithNoFallback.loadConfig();
assertThat(
"LocalFileConfigRepository should persist local cache files and return that afterwards",
someProperties.entrySet(), equalTo(anotherProperties.entrySet()));
}
}
\ No newline at end of file
......@@ -22,18 +22,18 @@ import java.util.Map;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyMap;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Jason Song(song_s@ctrip.com)
* Created by Jason on 4/9/16.
*/
@RunWith(MockitoJUnitRunner.class)
public class RemoteConfigTest {
public class RemoteConfigRepositoryTest {
@Mock
private RestTemplate restTemplate;
@Mock
......@@ -47,7 +47,6 @@ public class RemoteConfigTest {
@Before
public void setUp() throws Exception {
someNamespace = "someName";
String someServerUrl = "http://someServer";
mockConfigServiceLocator(someServerUrl);
......@@ -58,11 +57,9 @@ public class RemoteConfigTest {
}
@Test
public void testGetProperty() throws Exception {
public void testLoadConfig() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
String someKeyNotExisted = "key-not-existed";
String someDefaultValue = "someDefault";
Map<String, String> configurations = Maps.newHashMap();
configurations.put(someKey, someValue);
ApolloConfig someApolloConfig = assembleApolloConfig(configurations);
......@@ -70,12 +67,13 @@ public class RemoteConfigTest {
when(someResponse.getStatusCode()).thenReturn(HttpStatus.OK);
when(someResponse.getBody()).thenReturn(someApolloConfig);
when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), any(HttpEntity.class),
eq(ApolloConfig.class), anyMap())).thenReturn(someResponse);
eq(ApolloConfig.class), anyMap())).thenReturn(someResponse);
RemoteConfig remoteConfig = new RemoteConfig(restTemplate, configServiceLocator, someNamespace, someConfigUtil);
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(restTemplate, configServiceLocator, someConfigUtil, someNamespace);
Properties config = remoteConfigRepository.loadConfig();
assertEquals(someValue, remoteConfig.getProperty(someKey, null));
assertEquals(someDefaultValue, remoteConfig.getProperty(someKeyNotExisted, someDefaultValue));
assertEquals(configurations, config);
}
@Test(expected = RuntimeException.class)
......@@ -83,30 +81,10 @@ public class RemoteConfigTest {
when(someResponse.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), any(HttpEntity.class),
eq(ApolloConfig.class), anyMap())).thenReturn(someResponse);
new RemoteConfig(restTemplate, configServiceLocator, someNamespace, someConfigUtil);
}
@Test
public void testLoadConfig() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
Map<String, String> configurations = Maps.newHashMap();
configurations.put(someKey, someValue);
ApolloConfig someApolloConfig = assembleApolloConfig(configurations);
when(someResponse.getStatusCode()).thenReturn(HttpStatus.OK);
when(someResponse.getBody()).thenReturn(someApolloConfig);
when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), any(HttpEntity.class),
eq(ApolloConfig.class), anyMap())).thenReturn(someResponse);
RemoteConfig remoteConfig = new RemoteConfig(restTemplate, configServiceLocator, someNamespace, someConfigUtil);
eq(ApolloConfig.class), anyMap())).thenReturn(someResponse);
Properties config = remoteConfig.loadConfig();
assertEquals(configurations, config);
RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(restTemplate, configServiceLocator, someConfigUtil, someNamespace);
remoteConfigRepository.loadConfig();
}
private ApolloConfig assembleApolloConfig(Map<String, String> configurations) {
......@@ -114,7 +92,7 @@ public class RemoteConfigTest {
String someClusterName = "cluster";
long someReleaseId = 1;
ApolloConfig apolloConfig =
new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseId);
new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseId);
apolloConfig.setConfigurations(configurations);
......
package com.ctrip.apollo.internals;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Properties;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
/**
* @author Jason Song(song_s@ctrip.com)
*/
@RunWith(MockitoJUnitRunner.class)
public class SimpleConfigTest {
private String someNamespace;
@Mock
private ConfigRepository configRepository;
@Before
public void setUp() throws Exception {
someNamespace = "someName";
}
@Test
public void testGetProperty() throws Exception {
Properties someProperties = new Properties();
String someKey = "someKey";
String someValue = "someValue";
someProperties.setProperty(someKey, someValue);
when(configRepository.loadConfig()).thenReturn(someProperties);
SimpleConfig config = new SimpleConfig(someNamespace, configRepository);
assertEquals(someValue, config.getProperty(someKey, null));
}
@Test(expected = RuntimeException.class)
public void testLoadConfigFromConfigRepositoryError() throws Exception {
when(configRepository.loadConfig()).thenThrow(Throwable.class);
new SimpleConfig(someNamespace, configRepository);
}
}
......@@ -10,7 +10,6 @@ import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* @author Jason Song(song_s@ctrip.com)
......
......@@ -2,7 +2,7 @@ package com.ctrip.apollo.spi;
import com.ctrip.apollo.Config;
import com.ctrip.apollo.internals.DefaultConfig;
import com.ctrip.apollo.internals.RemoteConfig;
import com.ctrip.apollo.internals.LocalFileConfigRepository;
import org.junit.Before;
import org.junit.Test;
......@@ -12,6 +12,7 @@ import java.util.Properties;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
......@@ -38,17 +39,16 @@ public class DefaultConfigFactoryTest extends ComponentTestCase {
String someValue = "someValue";
someProperties.setProperty(someKey, someValue);
RemoteConfig someRemoteConfig = mock(RemoteConfig.class);
when(someRemoteConfig.loadConfig()).thenReturn(someProperties);
LocalFileConfigRepository someLocalConfigRepo = mock(LocalFileConfigRepository.class);
when(someLocalConfigRepo.loadConfig()).thenReturn(someProperties);
doReturn(someRemoteConfig).when(defaultConfigFactory).createRemoteConfig(someNamespace);
doReturn(someLocalConfigRepo).when(defaultConfigFactory).createLocalConfigRepository(someNamespace);
Config result = defaultConfigFactory.create(someNamespace);
assertThat("DefaultConfigFactory should create DefaultConfig", result,
is(instanceOf(DefaultConfig.class)));
assertThat("DefaultConfigFactory should set remote config as the fallback loader",
((DefaultConfig) result).getFallbackLoader(), instanceOf(RemoteConfig.class));
assertEquals(someValue, result.getProperty(someKey, null));
}
}
......@@ -4,11 +4,11 @@ import com.ctrip.apollo.Config;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.unidal.lookup.ComponentTestCase;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
/**
* @author Jason Song(song_s@ctrip.com)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment