add dubbo input unit test.
This commit is contained in:
10
pom.xml
10
pom.xml
@@ -222,10 +222,14 @@
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
@@ -233,6 +237,12 @@
|
||||
<artifactId>dubbo</artifactId>
|
||||
<version>${apache.dubbo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!--<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
|
||||
@@ -69,6 +69,10 @@ public class Step {
|
||||
return this.getWeakPipeline() != null ? this.getWeakPipeline().get().getApplicationContext(): null;
|
||||
}
|
||||
|
||||
public ConfigurableApplicationContext getCurrentApplicationContext() {
|
||||
return this.getWeakPipeline() != null ? this.getWeakPipeline().get().getApplicationContext(): null;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
public Step read(Map<String, Object> config, SoftReference<Pipeline> weakPipeline) {
|
||||
Step step = new Step();
|
||||
|
||||
@@ -35,10 +35,18 @@ public class DubboInput extends RPCInput {
|
||||
declaration.setMethod(config.getMethod());
|
||||
declaration.setParameterTypes(config.getParameterTypes());
|
||||
declaration.setTimeout(timeout);
|
||||
|
||||
HashMap<String, String> contextAttachment = new HashMap<String, String>(attachments);
|
||||
HashMap<String, String> contextAttachment = null;
|
||||
if (attachments == null){
|
||||
contextAttachment = new HashMap<String, String>();
|
||||
} else {
|
||||
contextAttachment = new HashMap<String, String>(attachments);
|
||||
}
|
||||
if (inputContext.getStepContext() != null && inputContext.getStepContext().getTraceId() != null){
|
||||
contextAttachment.put(CommonConstants.HEADER_TRACE_ID, inputContext.getStepContext().getTraceId());
|
||||
return proxy.send(body, declaration, contextAttachment).flatMap(cr->{
|
||||
}
|
||||
|
||||
Mono<Object> proxyResponse = proxy.send(body, declaration, contextAttachment);
|
||||
return proxyResponse.flatMap(cr->{
|
||||
DubboRPCResponse response = new DubboRPCResponse();
|
||||
String responseStr = JSON.toJSONString(cr);
|
||||
response.setBodyMono(Mono.just(responseStr));
|
||||
|
||||
87
src/test/java/we/fizz/input/DubboInputTests.java
Normal file
87
src/test/java/we/fizz/input/DubboInputTests.java
Normal file
@@ -0,0 +1,87 @@
|
||||
package we.fizz.input;
|
||||
|
||||
import org.apache.dubbo.config.ReferenceConfig;
|
||||
import org.apache.dubbo.rpc.service.GenericService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import reactor.core.publisher.Mono;
|
||||
import we.fizz.Step;
|
||||
import we.fizz.StepContext;
|
||||
import we.fizz.StepResponse;
|
||||
import we.fizz.input.extension.dubbo.DubboInput;
|
||||
import we.fizz.input.extension.dubbo.DubboInputConfig;
|
||||
import we.fizz.input.proxy.dubbo.ApacheDubboGenericProxyTests;
|
||||
import we.proxy.dubbo.ApacheDubboGenericProxy;
|
||||
import we.proxy.dubbo.DubboInterfaceDeclaration;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DubboInputTests {
|
||||
private static final String SERVICE_NAME = "com.fizzgate.test";
|
||||
private static final String METHOD_NAME = "method";
|
||||
private static final String[] LEFT = new String[]{};
|
||||
|
||||
private static final Object[] RIGHT = new Object[]{};
|
||||
|
||||
private ApacheDubboGenericProxy proxy;
|
||||
// @Before
|
||||
// public void setup(){
|
||||
// ApacheDubboGenericProxyTests test = new ApacheDubboGenericProxyTests();
|
||||
// proxy = test.getMockApachDubbo();
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
DubboInterfaceDeclaration declaration = mock(DubboInterfaceDeclaration.class);
|
||||
declaration.setServiceName(SERVICE_NAME);
|
||||
declaration.setMethod(METHOD_NAME);
|
||||
declaration.setParameterTypes("java.lang.String, java.lang.String");
|
||||
declaration.setTimeout(3000);
|
||||
|
||||
ReferenceConfig referenceConfig = mock(ReferenceConfig.class);
|
||||
GenericService genericService = mock(GenericService.class);
|
||||
when(referenceConfig.get()).thenReturn(genericService);
|
||||
when(referenceConfig.getInterface()).thenReturn(SERVICE_NAME);
|
||||
CompletableFuture<Object> future = new CompletableFuture<>();
|
||||
when(genericService.$invokeAsync(any(), any(), any())).thenReturn(future);
|
||||
ApacheDubboGenericProxy apacheDubboProxyService = new ApacheDubboGenericProxy();
|
||||
ApacheDubboGenericProxy proxy = spy(apacheDubboProxyService);
|
||||
when(proxy.createReferenceConfig(SERVICE_NAME)).thenReturn(referenceConfig);
|
||||
|
||||
ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class);
|
||||
when(applicationContext.getBean(ApacheDubboGenericProxy.class)).thenReturn(proxy);
|
||||
|
||||
Step step = mock(Step.class);
|
||||
when(step.getCurrentApplicationContext()).thenReturn(applicationContext);
|
||||
|
||||
StepResponse stepResponse = new StepResponse(step, null, new HashMap<String, Map<String, Object>>());
|
||||
DubboInputConfig config = mock(DubboInputConfig.class);
|
||||
when(config.getServiceName()).thenReturn(SERVICE_NAME);
|
||||
InputFactory.registerInput(InputType.DUBBO, DubboInput.class);
|
||||
DubboInput dubboInput = (DubboInput)InputFactory.createInput(InputType.DUBBO.toString());
|
||||
|
||||
dubboInput.setName("hello");
|
||||
dubboInput.setWeakStep(new SoftReference<>(step));
|
||||
dubboInput.setStepResponse(stepResponse);
|
||||
dubboInput.setConfig(config);
|
||||
StepContext stepContext = mock(StepContext.class);
|
||||
stepContext.put("step1", stepResponse);
|
||||
InputContext context = new InputContext(stepContext, null);
|
||||
dubboInput.beforeRun(context);
|
||||
|
||||
dubboInput.run();
|
||||
|
||||
future.complete("success");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package we.fizz.input.proxy.dubbo;
|
||||
|
||||
import org.apache.dubbo.config.ReferenceConfig;
|
||||
import org.apache.dubbo.rpc.service.GenericService;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import we.proxy.dubbo.ApacheDubboGenericProxy;
|
||||
import we.proxy.dubbo.DubboInterfaceDeclaration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ApacheDubboGenericProxyTests {
|
||||
private static final String SERVICE_NAME = "com.fizzgate.test";
|
||||
private static final String METHOD_NAME = "method";
|
||||
private static final String[] LEFT = new String[]{};
|
||||
|
||||
private static final Object[] RIGHT = new Object[]{};
|
||||
@Before
|
||||
public void setup(){
|
||||
|
||||
}
|
||||
|
||||
public ApacheDubboGenericProxy getMockApachDubbo(){
|
||||
ReferenceConfig referenceConfig = mock(ReferenceConfig.class);
|
||||
GenericService genericService = mock(GenericService.class);
|
||||
when(referenceConfig.get()).thenReturn(genericService);
|
||||
when(referenceConfig.getInterface()).thenReturn(SERVICE_NAME);
|
||||
ApacheDubboGenericProxy apacheDubboProxyService = mock(ApacheDubboGenericProxy.class);
|
||||
when(apacheDubboProxyService.createReferenceConfig(SERVICE_NAME)).thenReturn(referenceConfig);
|
||||
CompletableFuture<Object> future = new CompletableFuture<>();
|
||||
when(genericService.$invokeAsync(METHOD_NAME, LEFT, RIGHT)).thenReturn(future);
|
||||
future.complete("success");
|
||||
return apacheDubboProxyService;
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
HashMap<String, String> attachments = mock(HashMap.class);
|
||||
DubboInterfaceDeclaration declaration = mock(DubboInterfaceDeclaration.class);
|
||||
declaration.setServiceName(SERVICE_NAME);
|
||||
declaration.setMethod(METHOD_NAME);
|
||||
declaration.setParameterTypes("java.lang.String, java.lang.String");
|
||||
declaration.setTimeout(3000);
|
||||
ApacheDubboGenericProxyTests test = new ApacheDubboGenericProxyTests();
|
||||
ApacheDubboGenericProxy apacheDubboProxyService = test.getMockApachDubbo();
|
||||
apacheDubboProxyService.send("", declaration, attachments);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user