Skip to content

Commit

Permalink
Merge pull request #26 from mercyblitz/dev-1.x
Browse files Browse the repository at this point in the history
Polish #24 : Merged the latest code
  • Loading branch information
mercyblitz authored Jan 15, 2025
2 parents 096c170 + e7b398c commit 644fed9
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 113 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,91 @@
package io.microsphere.spring.cloud.openfeign;

import io.microsphere.spring.cloud.openfeign.autoconfigure.EnableFeignAutoRefresh;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.test.context.TestPropertySource;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* @author <a href="mailto:[email protected]">韩超</a>
* @since 0.0.1
*/
@SpringBootTest(classes = {BaseTest.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableAutoConfiguration
@TestPropertySource(properties = {
"spring.main.allow-bean-definition-overriding=true",
"feign.client.config.default.encoder=io.microsphere.spring.cloud.openfeign.encoder.AEncoder",
"feign.client.config.default.request-interceptors[0]=io.microsphere.spring.cloud.openfeign.requestInterceptor.ARequestInterceptor",
"feign.client.config.default.default-request-headers.app=my-app",
"feign.client.config.default.default-query-parameters.sign=my-sign",
})
@ComponentScan(basePackages = "io.microsphere.spring.cloud.openfeign")
@TestPropertySource(
properties = {
"spring.main.allow-bean-definition-overriding=true",
"feign.client.config.default.encoder=io.microsphere.spring.cloud.openfeign.encoder.AEncoder",
"feign.client.config.default.request-interceptors[0]=io.microsphere.spring.cloud.openfeign.requestInterceptor.ARequestInterceptor"
}
)
@EnableFeignClients(clients = BaseClient.class)
@EnableFeignAutoRefresh
@AutoConfigureAfter(ConfigurationPropertiesRebinderAutoConfiguration.class)
public class BaseTest {
public abstract class BaseTest<T> {

private static final Logger log = LoggerFactory.getLogger(BaseTest.class);
@Autowired
private ApplicationEventPublisher publisher;
@Autowired
private Environment environment;
@Autowired
private BaseClient client;

protected abstract String afterTestComponentConfigKey();
protected abstract Class<? extends T> afterTestComponent();

public void replaceConfig() {
final String key = afterTestComponentConfigKey();
Set<String> keys = Collections.singleton(key);
final Class<?> className = afterTestComponent();
MutablePropertySources propertySources = ((ConfigurableEnvironment)this.environment).getPropertySources();
Map<String, Object> map = new HashMap<>();
log.trace("replacing config key {} with value {}", key, className.getName());
map.put(key, className);
propertySources.addFirst(new MapPropertySource("after", map));

EnvironmentChangeEvent event = new EnvironmentChangeEvent(keys);

triggerRefreshEvent();

this.publisher.publishEvent(event);
}

@Test
public void testInternal() {
try {
this.client.echo("hello", "1.0");

} catch (Exception ignored) {

}
replaceConfig();
try {
this.client.echo("world", "1.0");
} catch (Exception ignored) {

}


}

protected void triggerRefreshEvent() {
this.publisher.publishEvent(new RefreshEvent(new Object(), new Object(), "test"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,25 @@
package io.microsphere.spring.cloud.openfeign.encoder;

import io.microsphere.spring.cloud.openfeign.BaseClient;
import feign.codec.Encoder;
import io.microsphere.spring.cloud.openfeign.BaseTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @author <a href="mailto:[email protected]">韩超</a>
* @since 0.0.1
*/
@SpringBootTest(classes = EncoderChangedTest.class)
@EnableAutoConfiguration
public class EncoderChangedTest extends BaseTest {

@Autowired
private Environment environment;
@Autowired
private ApplicationEventPublisher eventPublisher;

@Autowired
private BaseClient baseClient;

@Test
public void testEncoderChange() {
try {
baseClient.echo("aaa");

} catch (Exception ignored) {
}
applyEnvironmentChange();
try {
baseClient.echo("bbb");
} catch (Exception ignored) {
public class EncoderChangedTest extends BaseTest<Encoder> {

}
@Override
protected String afterTestComponentConfigKey() {
return "feign.client.config.my-client.encoder";
}

private void applyEnvironmentChange() {
Set<String> keys = Collections.singleton("feign.client.config.aaa.encoder");
MutablePropertySources propertySources = ((ConfigurableEnvironment)this.environment).getPropertySources();
Map<String, Object> map = new HashMap<>();
System.out.println("替换encoder: BEncoder");
map.put("feign.client.config.aaa.encoder", BEncoder.class);
propertySources.addFirst(new MapPropertySource("addition", map));

EnvironmentChangeEvent event = new EnvironmentChangeEvent(keys);

triggerRefreshEvent();

this.eventPublisher.publishEvent(event);
@Override
protected Class<? extends Encoder> afterTestComponent() {
return BEncoder.class;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import feign.codec.ErrorDecoder;
import io.microsphere.spring.cloud.openfeign.BaseTest;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @author <a href="mailto:[email protected]">韩超</a>
* @since 1.0
*/
@SpringBootTest(classes = ErrorDecoderChangedTest.class)
@EnableAutoConfiguration
public class ErrorDecoderChangedTest extends BaseTest<ErrorDecoder> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import feign.QueryMapEncoder;
import io.microsphere.spring.cloud.openfeign.BaseTest;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @author <a href="mailto:[email protected]">韩超</a>
* @since 1.0
*/
@SpringBootTest(classes = QueryMapEncoderChangedTest.class)
@EnableAutoConfiguration
public class QueryMapEncoderChangedTest extends BaseTest<QueryMapEncoder> {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,29 @@
package io.microsphere.spring.cloud.openfeign.requestInterceptor;

import io.microsphere.spring.cloud.openfeign.BaseClient;
import feign.RequestInterceptor;
import io.microsphere.spring.cloud.openfeign.BaseTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

/**
* @author <a href="mailto:[email protected]">韩超</a>
* @since 0.0.1
*/
@SpringBootTest(classes = RequestInterceptorChangedTest.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@EnableAutoConfiguration
public class RequestInterceptorChangedTest extends BaseTest {

@Autowired
private Environment environment;
@Autowired
private ApplicationEventPublisher eventPublisher;

@Autowired
private BaseClient baseClient;

@Test
public void testEncoderChange() {
try {
baseClient.echo("aaa");

} catch (Exception ignored) {
}
applyEnvironmentChange();
try {
baseClient.echo("bbb");
} catch (Exception ignored) {

}
public class RequestInterceptorChangedTest extends BaseTest<RequestInterceptor> {

@Override
protected String afterTestComponentConfigKey() {
return "feign.client.config.my-client.request-interceptors[0]";
}

private void applyEnvironmentChange() {
Set<String> keys = Collections.singleton("feign.client.config.aaa.request-interceptors[0]");
MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
Map<String, Object> map = new HashMap<>();
System.out.println("替换requestInterceptor: BRequestInterceptor");
map.put("feign.client.config.aaa.request-interceptors[0]", BRequestInterceptor.class.getName());
propertySources.addFirst(new MapPropertySource("addition", map));

EnvironmentChangeEvent event = new EnvironmentChangeEvent(keys);

triggerRefreshEvent();
this.eventPublisher.publishEvent(event);
@Override
protected Class<? extends RequestInterceptor> afterTestComponent() {
return BRequestInterceptor.class;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import feign.Retryer;
import io.microsphere.spring.cloud.openfeign.BaseTest;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;

/**
* @author <a href="mailto:[email protected]">韩超</a>
* @since 1.0
*/
@SpringBootTest(classes = RetryerChangedTest.class)
@EnableAutoConfiguration
public class RetryerChangedTest extends BaseTest<Retryer> {

@Override
Expand Down

0 comments on commit 644fed9

Please sign in to comment.