Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions apollo-client-config-data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@
<artifactId>spring-webflux</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<!-- <optional>true</optional>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webclient</artifactId>
<!-- <version>4.0.1</version>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
<!-- <optional>true</optional>-->
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.ctrip.framework.apollo.config.data.extension.initialize;

import com.ctrip.framework.apollo.config.data.extension.properties.ApolloClientProperties;
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties;
import org.springframework.boot.security.oauth2.client.autoconfigure.OAuth2ClientProperties;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.boot.webclient.WebClientCustomizer;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.client.WebClient;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private <T> HttpResponse<T> doGetInternal(HttpRequest httpRequest, Type response
if (HttpStatus.NOT_MODIFIED.equals(clientResponse.statusCode())) {
return Mono.just(new HttpResponse<T>(HttpStatus.NOT_MODIFIED.value(), null));
}
return Mono.error(new ApolloConfigStatusCodeException(clientResponse.rawStatusCode(),
return Mono.error(new ApolloConfigStatusCodeException(clientResponse.statusCode().value(),
String.format("Get operation failed for %s", httpRequest.getUrl())));
}).block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.commons.logging.Log;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.web.reactive.function.client.WebClientCustomizer;
import org.springframework.boot.webclient.WebClientCustomizer;
import org.springframework.lang.Nullable;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;

Expand All @@ -31,19 +31,19 @@
*/
public class ApolloClientPropertiesFactoryTest {

@Test
public void testCreateApolloClientProperties() throws IOException {
Map<String, String> map = new LinkedHashMap<>();
map.put("apollo.client.extension.enabled", "true");
map.put("apollo.client.extension.messaging-type", "long_polling");
MapConfigurationPropertySource propertySource = new MapConfigurationPropertySource(map);
Binder binder = new Binder(propertySource);
ApolloClientPropertiesFactory factory = new ApolloClientPropertiesFactory();
ApolloClientProperties apolloClientProperties = factory
.createApolloClientProperties(binder, null);
@Test
public void testCreateApolloClientProperties() throws IOException {
Map<String, String> map = new LinkedHashMap<>();
map.put("apollo.client.extension.enabled", "true");
map.put("apollo.client.extension.messaging-type", "long_polling");
MapConfigurationPropertySource propertySource = new MapConfigurationPropertySource(map);
Binder binder = new Binder(propertySource);
ApolloClientPropertiesFactory factory = new ApolloClientPropertiesFactory();
ApolloClientProperties apolloClientProperties = factory
.createApolloClientProperties(binder, null);

Assert.assertEquals(apolloClientProperties.getExtension().getEnabled(), true);
Assert.assertEquals(apolloClientProperties.getExtension().getMessagingType(),
ApolloClientMessagingType.LONG_POLLING);
}
Assertions.assertEquals(apolloClientProperties.getExtension().getEnabled(), true);
Assertions.assertEquals(apolloClientProperties.getExtension().getMessagingType(),
ApolloClientMessagingType.LONG_POLLING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,73 +23,74 @@
import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.DefaultConfigFactory;
import com.github.stefanbirkner.systemlambda.SystemLambda;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* @author vdisk <vdisk@foxmail.com>
*/
public class PureApolloConfigTest {

@Before
public void before() {
System.setProperty("env", "local");
}
@BeforeEach
public void before() {
System.setProperty("env", "local");
}

@After
public void after() {
System.clearProperty("spring.profiles.active");
System.clearProperty("env");
ApolloMockInjectorCustomizer.clear();
}
@AfterEach
public void after() {
System.clearProperty("spring.profiles.active");
System.clearProperty("env");
ApolloMockInjectorCustomizer.clear();
}

@Test
public void testDefaultConfigWithSystemProperties() {
System.setProperty("spring.profiles.active", "test");
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
DefaultConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assert.assertEquals("test", config.getProperty("spring.profiles.active", null));
}
@Test
public void testDefaultConfigWithSystemProperties() {
System.setProperty("spring.profiles.active", "test");
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
DefaultConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assertions.assertEquals("test", config.getProperty("spring.profiles.active", null));
}

@Test
public void testPureApolloConfigWithSystemProperties() {
System.setProperty("spring.profiles.active", "test");
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
PureApolloConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assert.assertNull(config.getProperty("spring.profiles.active", null));
}
@Test
public void testPureApolloConfigWithSystemProperties() {
System.setProperty("spring.profiles.active", "test");
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
PureApolloConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assertions.assertNull(config.getProperty("spring.profiles.active", null));
}

@Test
public void testDefaultConfigWithEnvironmentVariables() throws Exception {
SystemLambda.withEnvironmentVariable(
"SPRING_PROFILES_ACTIVE",
"test-env")
.execute(() -> {
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
DefaultConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assert.assertEquals("test-env", config.getProperty("SPRING_PROFILES_ACTIVE", null));
});
}
@Test
public void testDefaultConfigWithEnvironmentVariables() throws Exception {
SystemLambda.withEnvironmentVariable(
"SPRING_PROFILES_ACTIVE",
"test-env")
.execute(() -> {
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
DefaultConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assertions.assertEquals("test-env",
config.getProperty("SPRING_PROFILES_ACTIVE", null));
});
}

@Test
public void testPureApolloConfigWithEnvironmentVariables() throws Exception {
SystemLambda.withEnvironmentVariable(
"SPRING_PROFILES_ACTIVE",
"test-env")
.execute(() -> {
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
PureApolloConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assert.assertNull(config.getProperty("SPRING_PROFILES_ACTIVE", null));
});
}
@Test
public void testPureApolloConfigWithEnvironmentVariables() throws Exception {
SystemLambda.withEnvironmentVariable(
"SPRING_PROFILES_ACTIVE",
"test-env")
.execute(() -> {
ApolloMockInjectorCustomizer.register(ConfigFactory.class,
PureApolloConfigFactory::new);
ConfigFactory configFactory = ApolloInjector.getInstance(ConfigFactory.class);
Config config = configFactory.create("application");
Assertions.assertNull(config.getProperty("SPRING_PROFILES_ACTIVE", null));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,42 @@

import com.ctrip.framework.apollo.core.ApolloClientSystemConsts;
import com.ctrip.framework.apollo.spring.boot.ApolloApplicationContextInitializer;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/**
* @author vdisk <vdisk@foxmail.com>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = ApolloClientPropertyCompatibleTestConfiguration.class,
webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ActiveProfiles("test-compatible")
public class ApolloClientApplicationPropertiesCompatibleTest {

@Autowired
private ConfigurableEnvironment environment;
@Autowired
private ConfigurableEnvironment environment;

@Test
public void testApplicationPropertiesCompatible() {
Assert.assertEquals("test-1/cacheDir",
this.environment.getProperty(ApolloClientSystemConsts.APOLLO_CACHE_DIR));
Assert.assertEquals("test-1-secret",
this.environment.getProperty(ApolloClientSystemConsts.APOLLO_ACCESS_KEY_SECRET));
Assert.assertEquals("https://test-1-config-service",
this.environment.getProperty(ApolloClientSystemConsts.APOLLO_CONFIG_SERVICE));
}
@Test
public void testApplicationPropertiesCompatible() {
Assertions.assertEquals("test-1/cacheDir",
this.environment.getProperty(ApolloClientSystemConsts.APOLLO_CACHE_DIR));
Assertions.assertEquals("test-1-secret",
this.environment.getProperty(ApolloClientSystemConsts.APOLLO_ACCESS_KEY_SECRET));
Assertions.assertEquals("https://test-1-config-service",
this.environment.getProperty(ApolloClientSystemConsts.APOLLO_CONFIG_SERVICE));
}

@After
public void clearProperty() {
for (String propertyName : ApolloApplicationContextInitializer.APOLLO_SYSTEM_PROPERTIES) {
System.clearProperty(propertyName);
@AfterEach
public void clearProperty() {
for (String propertyName : ApolloApplicationContextInitializer.APOLLO_SYSTEM_PROPERTIES) {
System.clearProperty(propertyName);
}
}
}
}
Loading
Loading