Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sofa test #512

Merged
merged 4 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions soul-plugin/soul-plugin-sofa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
<artifactId>sofa-rpc-all</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add blank line

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.soul.plugin.sofa;

import org.dromara.soul.common.constant.Constants;
import org.dromara.soul.common.dto.MetaData;
import org.dromara.soul.common.dto.RuleData;
import org.dromara.soul.common.dto.SelectorData;
import org.dromara.soul.common.enums.RpcTypeEnum;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.dromara.soul.plugin.api.context.SoulContext;
import org.dromara.soul.plugin.sofa.proxy.SofaProxyService;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

/**
* SofaPluginTest.
*
* @author tydhot
*/
@RunWith(MockitoJUnitRunner.class)
public class SofaPluginTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add final


private SofaPlugin sofaPlugin;

private MetaData metaData;

private ServerWebExchange exchange;

@Mock
private SoulPluginChain chain;

@Before
public void setUp() {
exchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost").build());
metaData = new MetaData();
metaData.setId("1332017966661636096");
metaData.setAppName("sofa");
metaData.setPath("/sofa/findAll");
metaData.setServiceName("org.dromara.soul.test.dubbo.api.service.DubboTestService");
metaData.setMethodName("findAll");
metaData.setRpcType(RpcTypeEnum.SOFA.getName());
SofaProxyService sofaProxyService = mock(SofaProxyService.class);
when(sofaProxyService.genericInvoker(null, metaData, exchange)).thenReturn(Mono.empty());
sofaPlugin = new SofaPlugin(sofaProxyService);
}

@Test
public void test01SofaPlugin() {
RuleData data = mock(RuleData.class);
SoulContext context = mock(SoulContext.class);
exchange.getAttributes().put(Constants.CONTEXT, context);
exchange.getAttributes().put(Constants.META_DATA, metaData);
when(chain.execute(exchange)).thenReturn(Mono.empty());
SelectorData selectorData = mock(SelectorData.class);
StepVerifier.create(sofaPlugin.doExecute(exchange, chain, selectorData, data)).expectSubscription().verifyComplete();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.soul.plugin.sofa.handler;

import org.dromara.soul.common.config.SofaRegisterConfig;
import org.dromara.soul.common.dto.MetaData;
import org.dromara.soul.common.dto.PluginData;
import org.dromara.soul.plugin.base.utils.Singleton;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

/**
* SofaPluginDataHandlerTest.
*
* @author tydhot
*/
@RunWith(MockitoJUnitRunner.class)
public class SofaPluginDataHandlerTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add final


private SofaPluginDataHandler sofaPluginDataHandler;

private MetaData metaData;

private String registryConfig = "{\"protocol\":\"zookeeper\",\"register\":\"127.0.0.1:2181\"}";

@Before
public void setUp() {
sofaPluginDataHandler = new SofaPluginDataHandler();
metaData = new MetaData();
metaData.setId("1332017966661636096");
metaData.setAppName("sofa");
metaData.setPath("/sofa/findAll");
metaData.setServiceName("org.dromara.soul.test.dubbo.api.service.DubboTestService");
metaData.setMethodName("findAll");
}

@Test
public void test02PluginEnable() {
PluginData pluginData = new PluginData("", "", registryConfig, 1, true);
sofaPluginDataHandler.handlerPlugin(pluginData);
Assert.assertEquals(Singleton.INST.get(SofaRegisterConfig.class).getRegister(), "127.0.0.1:2181");
}

@Test
public void test01PluginDisable() {
PluginData pluginData = new PluginData("", "", registryConfig, 1, false);
sofaPluginDataHandler.handlerPlugin(pluginData);
Assert.assertNull(Singleton.INST.get(SofaRegisterConfig.class));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.soul.plugin.sofa.param;

import org.dromara.soul.common.constant.Constants;
import org.dromara.soul.common.enums.RpcTypeEnum;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.dromara.soul.plugin.api.context.SoulContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

/**
* BodyParamPluginTest.
*
* @author tydhot
*/
@RunWith(MockitoJUnitRunner.class)
public class BodyParamPluginTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add final


@Mock
private SoulPluginChain chain;

private ServerWebExchange simpleExchange;

private ServerWebExchange exchange;

private ServerWebExchange formExchange;

private BodyParamPlugin bodyParamPlugin;

@Before
public void setup() {
exchange = MockServerWebExchange.from(MockServerHttpRequest.post("localhost").contentType(MediaType.APPLICATION_JSON).body("{}"));
formExchange = MockServerWebExchange.from(MockServerHttpRequest.post("localhost").contentType(MediaType.APPLICATION_FORM_URLENCODED).body("{}"));
simpleExchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost").build());
}

@Test
public void test01NoBody() {
Mockito.when(chain.execute(exchange)).thenReturn(Mono.empty());
bodyParamPlugin = new BodyParamPlugin();
StepVerifier.create(bodyParamPlugin.execute(exchange, chain)).expectSubscription().verifyComplete();
}

@Test
public void test02SimpleBody() {
Mockito.when(chain.execute(simpleExchange)).thenReturn(Mono.empty());
bodyParamPlugin = new BodyParamPlugin();
SoulContext context = new SoulContext();
context.setRpcType(RpcTypeEnum.SOFA.getName());
simpleExchange.getAttributes().put(Constants.CONTEXT, context);
StepVerifier.create(bodyParamPlugin.execute(simpleExchange, chain)).expectSubscription().verifyComplete();
}

@Test
public void test03JsonBody() {
Mockito.when(chain.execute(exchange)).thenReturn(Mono.empty());
bodyParamPlugin = new BodyParamPlugin();
SoulContext context = new SoulContext();
context.setRpcType(RpcTypeEnum.SOFA.getName());
exchange.getAttributes().put(Constants.CONTEXT, context);
StepVerifier.create(bodyParamPlugin.execute(exchange, chain)).expectSubscription().verifyComplete();
}

@Test
public void test04FormBody() {
Mockito.when(chain.execute(formExchange)).thenReturn(Mono.empty());
bodyParamPlugin = new BodyParamPlugin();
SoulContext context = new SoulContext();
context.setRpcType(RpcTypeEnum.SOFA.getName());
formExchange.getAttributes().put(Constants.CONTEXT, context);
StepVerifier.create(bodyParamPlugin.execute(formExchange, chain)).expectSubscription().verifyComplete();
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.soul.plugin.sofa.response;

import org.dromara.soul.common.constant.Constants;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.mock.web.server.MockServerWebExchange;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

/**
* SofaResponsePluginTest.
*
* @author tydhot
*/
@RunWith(MockitoJUnitRunner.class)
public class SofaResponsePluginTest {

@Mock
private SoulPluginChain chain;

private ServerWebExchange exchange;

private SofaResponsePlugin sofaResponsePlugin;

private String response = "{}";

@Before
public void setup() {
exchange = MockServerWebExchange.from(MockServerHttpRequest.get("localhost").build());
}

@Test
public void test01NoResult() {
Mockito.when(chain.execute(exchange)).thenReturn(Mono.empty());
sofaResponsePlugin = new SofaResponsePlugin();
StepVerifier.create(sofaResponsePlugin.execute(exchange, chain)).expectSubscription().verifyError();
}

@Test
public void test02GetResult() {
Mockito.when(chain.execute(exchange)).thenReturn(Mono.empty());
sofaResponsePlugin = new SofaResponsePlugin();
exchange.getAttributes().put(Constants.SOFA_RPC_RESULT, response);
StepVerifier.create(sofaResponsePlugin.execute(exchange, chain)).expectSubscription().verifyError(NullPointerException.class);
}

@Test
public void test03GetResult() {
Mockito.when(chain.execute(exchange)).thenReturn(Mono.empty());
sofaResponsePlugin = new SofaResponsePlugin();
exchange.getAttributes().put(Constants.SOFA_RPC_RESULT, response);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

StepVerifier.create(sofaResponsePlugin.execute(exchange, chain)).expectSubscription().verifyError(NullPointerException.class);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line

}
Loading