Commit b533fb61 authored by Jason Song's avatar Jason Song Committed by GitHub

Merge pull request #1403 from nobodyiam/refactor-mock-server

refactor a little bit
parents d7621107 71fca12e
...@@ -11,27 +11,38 @@ ...@@ -11,27 +11,38 @@
<artifactId>apollo-mockserver</artifactId> <artifactId>apollo-mockserver</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId> <artifactId>apollo-client</artifactId>
<version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.squareup.okhttp3</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId> <artifactId>mockwebserver</artifactId>
<version>3.11.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.squareup.okhttp3</groupId> <groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>3.11.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
<version>1.3.8.RELEASE</version> <scope>test</scope>
<scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
......
package com.ctrip.framework.apollo.mockserver;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.spi.MetaServerProvider;
/**
* Create by zhangzheng on 8/23/18
* Email:zhangzheng@youzan.com
*/
public class MockedMetaServerProvider implements MetaServerProvider{
private static String address;
public static void setAddress(String addr){
address = addr;
}
@Override
public String getMetaServerAddress(Env targetEnv) {
return address;
}
@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;
}
}
package com.ctrip.framework.apollo.mockserver;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.google.common.util.concurrent.SettableFuture;
import java.util.concurrent.TimeUnit;
import org.junit.ClassRule;
import org.junit.Test;
public class ApolloMockServerApiTest {
private static final String otherNamespace = "otherNamespace";
@ClassRule
public static EmbeddedApollo embeddedApollo = new EmbeddedApollo();
@Test
public void testGetProperty() throws Exception {
Config applicationConfig = ConfigService.getAppConfig();
assertEquals("value1", applicationConfig.getProperty("key1", null));
assertEquals("value2", applicationConfig.getProperty("key2", null));
}
@Test
public void testUpdateProperties() throws Exception {
String someNewValue = "someNewValue";
Config otherConfig = ConfigService.getConfig(otherNamespace);
SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
otherConfig.addChangeListener(future::set);
assertEquals("otherValue1", otherConfig.getProperty("key1", null));
assertEquals("otherValue2", otherConfig.getProperty("key2", null));
embeddedApollo.addOrModifyProperty(otherNamespace, "key1", someNewValue);
ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
assertEquals(someNewValue, otherConfig.getProperty("key1", null));
assertEquals("otherValue2", otherConfig.getProperty("key2", null));
assertTrue(changeEvent.isChanged("key1"));
}
}
package com.ctrip.framework.apollo.mockserver; package com.ctrip.framework.apollo.mockserver;
import static org.junit.Assert.assertEquals;
import com.ctrip.framework.apollo.enums.PropertyChangeType; import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.mockserver.SpringIntegrationTest.TestConfiguration; import com.ctrip.framework.apollo.mockserver.ApolloMockServerSpringIntegrationTest.TestConfiguration;
import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener; import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import static org.junit.Assert.*;
import com.google.common.util.concurrent.SettableFuture; import com.google.common.util.concurrent.SettableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -23,21 +23,23 @@ import org.springframework.test.annotation.DirtiesContext; ...@@ -23,21 +23,23 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** /**
* Create by zhangzheng on 8/16/18 * Create by zhangzheng on 8/16/18 Email:zhangzheng@youzan.com
* Email:zhangzheng@youzan.com
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class) @SpringApplicationConfiguration(classes = TestConfiguration.class)
public class SpringIntegrationTest { public class ApolloMockServerSpringIntegrationTest {
@Autowired
TestBean testBean; private static final String otherNamespace = "otherNamespace";
@ClassRule @ClassRule
public static EmbeddedApollo embeddedApollo = new EmbeddedApollo(); public static EmbeddedApollo embeddedApollo = new EmbeddedApollo();
@Autowired
private TestBean testBean;
@Test @Test
@DirtiesContext @DirtiesContext
public void testPropertyInject(){ public void testPropertyInject() {
assertEquals("value1", testBean.key1); assertEquals("value1", testBean.key1);
assertEquals("value2", testBean.key2); assertEquals("value2", testBean.key2);
} }
...@@ -45,8 +47,7 @@ public class SpringIntegrationTest { ...@@ -45,8 +47,7 @@ public class SpringIntegrationTest {
@Test @Test
@DirtiesContext @DirtiesContext
public void testListenerTriggeredByAdd() throws InterruptedException, ExecutionException, TimeoutException { public void testListenerTriggeredByAdd() throws InterruptedException, ExecutionException, TimeoutException {
String otherNamespace = "othernamespace"; embeddedApollo.addOrModifyProperty(otherNamespace, "someKey", "someValue");
embeddedApollo.addOrModifyPropery(otherNamespace,"someKey","someValue");
ConfigChangeEvent changeEvent = testBean.futureData.get(5000, TimeUnit.MILLISECONDS); ConfigChangeEvent changeEvent = testBean.futureData.get(5000, TimeUnit.MILLISECONDS);
assertEquals(otherNamespace, changeEvent.getNamespace()); assertEquals(otherNamespace, changeEvent.getNamespace());
assertEquals("someValue", changeEvent.getChange("someKey").getNewValue()); assertEquals("someValue", changeEvent.getChange("someKey").getNewValue());
...@@ -56,42 +57,34 @@ public class SpringIntegrationTest { ...@@ -56,42 +57,34 @@ public class SpringIntegrationTest {
@DirtiesContext @DirtiesContext
public void testListenerTriggeredByDel() public void testListenerTriggeredByDel()
throws InterruptedException, ExecutionException, TimeoutException { throws InterruptedException, ExecutionException, TimeoutException {
String otherNamespace = "othernamespace"; embeddedApollo.deleteProperty(otherNamespace, "key1");
embeddedApollo.delete(otherNamespace, "key1");
ConfigChangeEvent changeEvent = testBean.futureData.get(5000, TimeUnit.MILLISECONDS); ConfigChangeEvent changeEvent = testBean.futureData.get(5000, TimeUnit.MILLISECONDS);
assertEquals(otherNamespace, changeEvent.getNamespace()); assertEquals(otherNamespace, changeEvent.getNamespace());
assertEquals(PropertyChangeType.DELETED, changeEvent.getChange("key1").getChangeType()); assertEquals(PropertyChangeType.DELETED, changeEvent.getChange("key1").getChangeType());
} }
@EnableApolloConfig
@EnableApolloConfig("application")
@Configuration @Configuration
static class TestConfiguration{ static class TestConfiguration {
@Bean @Bean
public TestBean testBean(){ public TestBean testBean() {
return new TestBean(); return new TestBean();
} }
} }
static class TestBean{ private static class TestBean {
@Value("${key1:default}") @Value("${key1:default}")
String key1; private String key1;
@Value("${key2:default}") @Value("${key2:default}")
String key2; private String key2;
SettableFuture<ConfigChangeEvent> futureData = SettableFuture.create(); private SettableFuture<ConfigChangeEvent> futureData = SettableFuture.create();
@ApolloConfigChangeListener("othernamespace") @ApolloConfigChangeListener(otherNamespace)
private void onChange(ConfigChangeEvent changeEvent) { private void onChange(ConfigChangeEvent changeEvent) {
futureData.set(changeEvent); futureData.set(changeEvent);
} }
} }
} }
...@@ -113,6 +113,11 @@ ...@@ -113,6 +113,11 @@
<artifactId>apollo-core</artifactId> <artifactId>apollo-core</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency> <dependency>
<groupId>com.ctrip.framework.apollo</groupId> <groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-common</artifactId> <artifactId>apollo-common</artifactId>
......
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