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

[dubbo-1689]: Enhance the test coverage part-10 : dubbo-plugin module #1982

Merged
merged 4 commits into from
Jun 25, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void startQosServer(URL url) {

int port = Integer.parseInt(url.getParameter(QOS_PORT,"22222"));
boolean acceptForeignIp = Boolean.parseBoolean(url.getParameter(ACCEPT_FOREIGN_IP,"true"));
Server server = org.apache.dubbo.qos.server.Server.getInstance();
Server server = Server.getInstance();
server.setPort(port);
server.setAcceptForeignIp(acceptForeignIp);
server.start();
Expand All @@ -92,4 +92,11 @@ private void startQosServer(URL url) {
//throw new RpcException("fail to start qos server", throwable);
}
}

/*package*/ void stopServer() {
if (hasStarted.compareAndSet(true, false)) {
Server server = Server.getInstance();
server.stop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private Server() {

private String welcome;

private AtomicBoolean hasStarted = new AtomicBoolean();
private AtomicBoolean started = new AtomicBoolean();

/**
* welcome message
Expand All @@ -78,7 +78,7 @@ public int getPort() {
* start server, bind port
*/
public void start() throws Throwable {
if (!hasStarted.compareAndSet(false, true)) {
if (!started.compareAndSet(false, true)) {
return;
}
boss = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-boss", true));
Expand Down Expand Up @@ -132,4 +132,8 @@ public void setAcceptForeignIp(boolean acceptForeignIp) {
public String getWelcome() {
return welcome;
}

public boolean isStarted() {
return started.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.apache.dubbo.qos.command;

import org.apache.dubbo.qos.command.annotation.Cmd;


@Cmd(name = "greeting", summary = "greeting message", example = {"greeting dubbo",})
public class GreetingCommand implements BaseCommand {
@Override
public String execute(CommandContext commandContext, String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.apache.dubbo.qos.command.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import org.apache.dubbo.qos.command.CommandContext;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class HttpCommandDecoderTest {
@Test
public void decodeGet() throws Exception {
HttpRequest request = mock(HttpRequest.class);
when(request.getUri()).thenReturn("localhost:80/test");
when(request.getMethod()).thenReturn(HttpMethod.GET);
CommandContext context = HttpCommandDecoder.decode(request);
assertThat(context.getCommandName(), equalTo("test"));
assertThat(context.isHttp(), is(true));
when(request.getUri()).thenReturn("localhost:80/test?a=b&c=d");
context = HttpCommandDecoder.decode(request);
assertThat(context.getArgs(), arrayContaining("b", "d"));
}

@Test
public void decodePost() throws Exception {
FullHttpRequest request = mock(FullHttpRequest.class);
when(request.getUri()).thenReturn("localhost:80/test");
when(request.getMethod()).thenReturn(HttpMethod.POST);
when(request.headers()).thenReturn(HttpHeaders.EMPTY_HEADERS);
ByteBuf buf = Unpooled.copiedBuffer("a=b&c=d", StandardCharsets.UTF_8);
when(request.content()).thenReturn(buf);
CommandContext context = HttpCommandDecoder.decode(request);
assertThat(context.getCommandName(), equalTo("test"));
assertThat(context.isHttp(), is(true));
assertThat(context.getArgs(), arrayContaining("b", "d"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.apache.dubbo.qos.command.decoder;

import org.apache.dubbo.qos.command.CommandContext;
import org.junit.Test;

import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class TelnetCommandDecoderTest {
@Test
public void testDecode() throws Exception {
CommandContext context = TelnetCommandDecoder.decode("test a b");
assertThat(context.getCommandName(), equalTo("test"));
assertThat(context.isHttp(), is(false));
assertThat(context.getArgs(), arrayContaining("a", "b"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.apache.dubbo.qos.command.impl;

import org.apache.dubbo.qos.command.CommandContext;
import org.junit.Test;
import org.mockito.Mockito;

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

public class HelpTest {
@Test
public void testMainHelp() throws Exception {
Help help = new Help();
String output = help.execute(Mockito.mock(CommandContext.class), null);
assertThat(output, containsString("greeting"));
assertThat(output, containsString("help"));
assertThat(output, containsString("ls"));
assertThat(output, containsString("online"));
assertThat(output, containsString("offline"));
assertThat(output, containsString("quit"));
}

@Test
public void testGreeting() throws Exception {
Help help = new Help();
String output = help.execute(Mockito.mock(CommandContext.class), new String[]{"greeting"});
assertThat(output, containsString("COMMAND NAME"));
assertThat(output, containsString("greeting"));
assertThat(output, containsString("EXAMPLE"));
assertThat(output, containsString("greeting dubbo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.apache.dubbo.qos.command.impl;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.model.ApplicationModel;
import org.apache.dubbo.config.model.ConsumerModel;
import org.apache.dubbo.config.model.ProviderModel;
import org.apache.dubbo.qos.command.CommandContext;
import org.apache.dubbo.registry.integration.RegistryDirectory;
import org.apache.dubbo.registry.support.ProviderConsumerRegTable;
import org.apache.dubbo.registry.support.ProviderInvokerWrapper;
import org.apache.dubbo.rpc.Invoker;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.Map;

import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class LsTest {
@Test
public void testExecute() throws Exception {
ConsumerModel consumerModel = mock(ConsumerModel.class);
when(consumerModel.getServiceName()).thenReturn("org.apache.dubbo.FooService");
ProviderModel providerModel = mock(ProviderModel.class);
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
ApplicationModel.initConsumerModel("org.apache.dubbo.FooService", consumerModel);
ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel);

Invoker providerInvoker = mock(Invoker.class);
URL registryUrl = mock(URL.class);
when(registryUrl.toFullString()).thenReturn("test://localhost:8080");
URL providerUrl = mock(URL.class);
when(providerUrl.getServiceKey()).thenReturn("org.apache.dubbo.BarService");
when(providerUrl.toFullString()).thenReturn("dubbo://localhost:8888/org.apache.dubbo.BarService");
when(providerInvoker.getUrl()).thenReturn(providerUrl);
ProviderConsumerRegTable.registerProvider(providerInvoker, registryUrl, providerUrl);
for (ProviderInvokerWrapper wrapper : getProviderInvoker("org.apache.dubbo.BarService")) {
wrapper.setReg(true);
}

Invoker consumerInvoker = mock(Invoker.class);
URL consumerUrl = mock(URL.class);
when(consumerUrl.getServiceKey()).thenReturn("org.apache.dubbo.FooService");
when(consumerUrl.toFullString()).thenReturn("dubbo://localhost:8888/org.apache.dubbo.FooService");
when(consumerInvoker.getUrl()).thenReturn(consumerUrl);
RegistryDirectory directory = mock(RegistryDirectory.class);
Map invokers = Mockito.mock(Map.class);
when(invokers.size()).thenReturn(100);
when(directory.getUrlInvokerMap()).thenReturn(invokers);
ProviderConsumerRegTable.registerConsumer(consumerInvoker, registryUrl, consumerUrl, directory);

Ls ls = new Ls();
String output = ls.execute(mock(CommandContext.class), null);
assertThat(output, containsString("org.apache.dubbo.FooService|100"));
assertThat(output, containsString("org.apache.dubbo.BarService| Y"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.apache.dubbo.qos.command.impl;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.model.ApplicationModel;
import org.apache.dubbo.config.model.ProviderModel;
import org.apache.dubbo.qos.command.CommandContext;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.support.ProviderConsumerRegTable;
import org.apache.dubbo.registry.support.ProviderInvokerWrapper;
import org.apache.dubbo.rpc.Invoker;
import org.junit.Test;
import org.mockito.Mockito;

import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OfflineTest {
@Test
public void testExecute() throws Exception {
ProviderModel providerModel = mock(ProviderModel.class);
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel);

Invoker providerInvoker = mock(Invoker.class);
URL registryUrl = mock(URL.class);
when(registryUrl.toFullString()).thenReturn("test://localhost:8080");
URL providerUrl = mock(URL.class);
when(providerUrl.getServiceKey()).thenReturn("org.apache.dubbo.BarService");
when(providerUrl.toFullString()).thenReturn("dubbo://localhost:8888/org.apache.dubbo.BarService");
when(providerInvoker.getUrl()).thenReturn(providerUrl);
ProviderConsumerRegTable.registerProvider(providerInvoker, registryUrl, providerUrl);
for (ProviderInvokerWrapper wrapper : getProviderInvoker("org.apache.dubbo.BarService")) {
wrapper.setReg(true);
}

Registry registry = mock(Registry.class);
TestRegistryFactory.registry = registry;

Offline offline = new Offline();
String output = offline.execute(mock(CommandContext.class), new String[]{"org.apache.dubbo.BarService"});
assertThat(output, containsString("OK"));
Mockito.verify(registry).unregister(providerUrl);
for (ProviderInvokerWrapper wrapper : getProviderInvoker("org.apache.dubbo.BarService")) {
assertThat(wrapper.isReg(), is(false));
}

output = offline.execute(mock(CommandContext.class), new String[]{"org.apache.dubbo.FooService"});
assertThat(output, containsString("service not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.apache.dubbo.qos.command.impl;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.model.ApplicationModel;
import org.apache.dubbo.config.model.ProviderModel;
import org.apache.dubbo.qos.command.CommandContext;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.support.ProviderConsumerRegTable;
import org.apache.dubbo.registry.support.ProviderInvokerWrapper;
import org.apache.dubbo.rpc.Invoker;
import org.junit.Test;

import static org.apache.dubbo.registry.support.ProviderConsumerRegTable.getProviderInvoker;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OnlineTest {
@Test
public void testExecute() throws Exception {
ProviderModel providerModel = mock(ProviderModel.class);
when(providerModel.getServiceName()).thenReturn("org.apache.dubbo.BarService");
ApplicationModel.initProviderModel("org.apache.dubbo.BarService", providerModel);

Invoker providerInvoker = mock(Invoker.class);
URL registryUrl = mock(URL.class);
when(registryUrl.toFullString()).thenReturn("test://localhost:8080");
URL providerUrl = mock(URL.class);
when(providerUrl.getServiceKey()).thenReturn("org.apache.dubbo.BarService");
when(providerUrl.toFullString()).thenReturn("dubbo://localhost:8888/org.apache.dubbo.BarService");
when(providerInvoker.getUrl()).thenReturn(providerUrl);
ProviderConsumerRegTable.registerProvider(providerInvoker, registryUrl, providerUrl);

Registry registry = mock(Registry.class);
TestRegistryFactory.registry = registry;

Online online = new Online();
String output = online.execute(mock(CommandContext.class), new String[]{"org.apache.dubbo.BarService"});
assertThat(output, equalTo("OK"));
for (ProviderInvokerWrapper wrapper : getProviderInvoker("org.apache.dubbo.BarService")) {
assertTrue(wrapper.isReg());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.apache.dubbo.qos.command.impl;

import org.apache.dubbo.qos.command.CommandContext;
import org.apache.dubbo.qos.common.QosConstants;
import org.junit.Test;
import org.mockito.Mockito;

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

public class QuitTest {
@Test
public void testExecute() throws Exception {
Quit quit = new Quit();
String output = quit.execute(Mockito.mock(CommandContext.class), null);
assertThat(output, equalTo(QosConstants.CLOSE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.apache.dubbo.qos.command.impl;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;

public class TestRegistryFactory implements RegistryFactory {
static Registry registry;

@Override
public Registry getRegistry(URL url) {
return registry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.apache.dubbo.qos.command.util;

import org.apache.dubbo.qos.command.GreetingCommand;
import org.apache.dubbo.qos.command.impl.Help;
import org.apache.dubbo.qos.command.impl.Ls;
import org.apache.dubbo.qos.command.impl.Offline;
import org.apache.dubbo.qos.command.impl.Online;
import org.apache.dubbo.qos.command.impl.Quit;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class CommandHelperTest {
@Test
public void testHasCommand() throws Exception {
assertTrue(CommandHelper.hasCommand("greeting"));
assertFalse(CommandHelper.hasCommand("not-exiting"));
}

@Test
public void testGetAllCommandClass() throws Exception {
List<Class<?>> classes = CommandHelper.getAllCommandClass();
assertThat(classes, containsInAnyOrder(GreetingCommand.class, Help.class, Ls.class, Offline.class, Online.class, Quit.class));
}

@Test
public void testGetCommandClass() throws Exception {
assertThat(CommandHelper.getCommandClass("greeting"), equalTo(GreetingCommand.class));
assertNull(CommandHelper.getCommandClass("not-exiting"));
}
}
Loading