Skip to content

Commit

Permalink
add integration test for place order
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnykaka committed Jun 3, 2016
1 parent 73805fb commit 810c6ec
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.akkafun.common.event.EventRegistry;
import com.akkafun.common.scheduler.config.SchedulerConfiguration;
import com.akkafun.common.spring.BaseApplication;
import com.akkafun.common.spring.ServiceClientApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -14,7 +15,7 @@
* Created by liubin on 2016/3/28.
*/
@SpringBootApplication
@Import({BaseApplication.class, SchedulerConfiguration.class})
@Import({BaseApplication.class, SchedulerConfiguration.class, ServiceClientApplication.class})
public class AccountApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
@EnableJpaRepositories("com.akkafun.**.dao")
@EnableJpaAuditing
@ComponentScan({"com.akkafun.**.service", "com.akkafun.**.web"})
@EnableDiscoveryClient
@Import({EventConfiguration.class})
public class BaseApplication {

Expand Down Expand Up @@ -76,15 +75,4 @@ public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter()
}


@Bean(destroyMethod = "shutdown")
@org.springframework.cloud.context.config.annotation.RefreshScope
public EurekaClient eurekaClient(ApplicationInfoManager manager,
EurekaClientConfig config,
DiscoveryClient.DiscoveryClientOptionalArgs optionalArgs,
ApplicationContext context) {
manager.getInfo(); // force initialization
return new CloudEurekaClient(manager, config, optionalArgs, context);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.akkafun.common.spring;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.EurekaClientConfig;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.CloudEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

/**
* Created by liubin on 2016/5/30.
*/
@EnableDiscoveryClient
public class ServiceClientApplication {

@Bean(destroyMethod = "shutdown")
@org.springframework.cloud.context.config.annotation.RefreshScope
public EurekaClient eurekaClient(ApplicationInfoManager manager,
EurekaClientConfig config,
DiscoveryClient.DiscoveryClientOptionalArgs optionalArgs,
ApplicationContext context) {
manager.getInfo(); // force initialization
return new CloudEurekaClient(manager, config, optionalArgs, context);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.akkafun.common.spring.utils;

import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.env.Environment;

/**
* 内部类也能查询出来
* Created by liubin on 2016/5/31.
*/
public class InnerClassPathScanningCandidateComponentProvider extends ClassPathScanningCandidateComponentProvider {

public InnerClassPathScanningCandidateComponentProvider(boolean useDefaultFilters) {
super(useDefaultFilters);
}

public InnerClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment) {
super(useDefaultFilters, environment);
}

@Override
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
return beanDefinition.getMetadata().isConcrete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.akkafun.common.event.EventRegistry;
import com.akkafun.common.scheduler.config.SchedulerConfiguration;
import com.akkafun.common.spring.BaseApplication;
import com.akkafun.common.spring.ServiceClientApplication;
import com.akkafun.coupon.event.EventHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -14,7 +15,7 @@
* Created by liubin on 2016/3/28.
*/
@SpringBootApplication
@Import({BaseApplication.class, SchedulerConfiguration.class})
@Import({BaseApplication.class, SchedulerConfiguration.class, ServiceClientApplication.class})
public class CouponApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.akkafun.common.event.EventInit;
import com.akkafun.common.event.EventRegistry;
import com.akkafun.common.spring.BaseApplication;
import com.akkafun.common.spring.ServiceClientApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
Expand All @@ -11,7 +12,7 @@
* Created by liubin on 2016/3/28.
*/
@SpringBootApplication
@Import(BaseApplication.class)
@Import({BaseApplication.class, ServiceClientApplication.class})
public class IntegrationTestApplication {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.akkafun.common.event.EventRegistry;
import com.akkafun.common.scheduler.config.SchedulerConfiguration;
import com.akkafun.common.spring.BaseApplication;
import com.akkafun.common.spring.ServiceClientApplication;
import com.akkafun.order.event.EventHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -14,7 +15,7 @@
* Created by liubin on 2016/3/28.
*/
@SpringBootApplication
@Import({BaseApplication.class, SchedulerConfiguration.class})
@Import({BaseApplication.class, SchedulerConfiguration.class, ServiceClientApplication.class})
public class OrderApplication {

public static void main(String[] args) {
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<compilerArgument>-parameters</compilerArgument>
<testCompilerArgument>-parameters</testCompilerArgument>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.akkafun.common.event.EventRegistry;
import com.akkafun.common.scheduler.config.SchedulerConfiguration;
import com.akkafun.common.spring.BaseApplication;
import com.akkafun.common.spring.ServiceClientApplication;
import com.akkafun.product.event.EventHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -14,7 +15,7 @@
* Created by liubin on 2016/3/28.
*/
@SpringBootApplication
@Import({BaseApplication.class, SchedulerConfiguration.class})
@Import({BaseApplication.class, SchedulerConfiguration.class, ServiceClientApplication.class})
public class ProductApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.akkafun.common.event.EventRegistry;
import com.akkafun.common.scheduler.config.SchedulerConfiguration;
import com.akkafun.common.spring.BaseApplication;
import com.akkafun.common.spring.ServiceClientApplication;
import com.akkafun.common.spring.WebApplication;
import com.akkafun.user.event.EventHandler;
import org.springframework.boot.SpringApplication;
Expand All @@ -15,7 +16,7 @@
* Created by liubin on 2016/3/28.
*/
@SpringBootApplication
@Import({BaseApplication.class, WebApplication.class, SchedulerConfiguration.class})
@Import({BaseApplication.class, WebApplication.class, SchedulerConfiguration.class, ServiceClientApplication.class})
public class UserApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.akkafun.common.event.load;

import org.apache.commons.lang.RandomStringUtils;

/**
* Created by liubin on 2016/5/30.
*/
public class FakeAskCouponUse extends FakeAskEvent {

private String id;

public FakeAskCouponUse() {
this.id = RandomStringUtils.randomAlphanumeric(8);
}

public String getId() {
return id;
}

@Override
public String toString() {
return "FakeAskCouponUse{" +
"id='" + id + '\'' +
"} " + super.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.akkafun.common.event.load;

import org.apache.commons.lang.RandomStringUtils;

/**
* Created by liubin on 2016/5/30.
*/
public class FakeAskDeductBalance extends FakeAskEvent {

private String id;

public FakeAskDeductBalance() {
this.id = RandomStringUtils.randomAlphanumeric(8);
}

public String getId() {
return id;
}

@Override
public String toString() {
return "FakeAskDeductBalance{" +
"id='" + id + '\'' +
"} " + super.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.akkafun.common.event.load;

/**
* Created by liubin on 2016/5/30.
*/
public class FakeAskEvent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.akkafun.common.event.load;

/**
* Created by liubin on 2016/5/30.
*/
public class FakeEventBus {

public void askUnited(FakeUnitedAskEventCallback callback, FakeAskEvent firstAskEvent, FakeAskEvent secondAskEvent,
FakeAskEvent... remainAskEvents) {
System.out.println(callback.getClass());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.akkafun.common.event.load;

/**
* Created by liubin on 2016/5/30.
*/
public interface FakeUnitedAskEventCallback {

void onSuccess(FakeAskEvent[] askEvents);

void onFailure(FakeAskEvent[] askEvents);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.akkafun.common.event.load;

import com.akkafun.common.spring.utils.InnerClassPathScanningCandidateComponentProvider;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

/**
* Created by liubin on 2016/5/30.
*/
public class LoadEventClassTest {

@Test
@Ignore
public void test() throws Exception {

ClassPathScanningCandidateComponentProvider provider = new InnerClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AssignableTypeFilter(FakeUnitedAskEventCallback.class));

Set<BeanDefinition> componentSet = provider.findCandidateComponents("com/akkafun");

assertThat(componentSet, not(empty()));
System.out.println(String.format("FakeUnitedAskEventCallback subclass count: %d, content: %s",
componentSet.size(), componentSet.toString()));

ArrayList<BeanDefinition> components = new ArrayList<>(componentSet);
BeanDefinition beanDefinition = components.get(0);
Class<? extends FakeUnitedAskEventCallback> callbackClass =
(Class<? extends FakeUnitedAskEventCallback>) Class.forName(beanDefinition.getBeanClassName());
Method onSuccessMethod = callbackClass.getMethod("onSuccess", FakeAskEvent[].class);
assertThat(onSuccessMethod, notNullValue());
Constructor[] ctors = callbackClass.getDeclaredConstructors();
for (Constructor cc : ctors)
{
System.out.println("my ctor is " + cc.toString());
}

// onSuccessMethod.invoke(callbackClass.newInstance(), new FakeAskCouponUse(), new FakeAskDeductBalance());


FakeEventBus eventBus = new FakeEventBus();

final Integer a = new Integer(2);

eventBus.askUnited(new FakeUnitedAskEventCallback() {

@Override
public void onSuccess(FakeAskEvent[] askEvents) {
System.out.println(String.format("onSuccess, params: %s, a: %d", Arrays.toString(askEvents), a));
}

@Override
public void onFailure(FakeAskEvent[] askEvents) {
System.out.println("onFailure: " + Arrays.toString(askEvents));
}

}, new FakeAskCouponUse(), new FakeAskDeductBalance());

}


}

0 comments on commit 810c6ec

Please sign in to comment.