beiwei30 closed pull request #1976: [dubbo-1689]: Enhance the test coverage 
part-10 : dubbo-plugin module
URL: https://github.com/apache/incubator-dubbo/pull/1976
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java
 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java
index 8c515ee4cb..ff49804b75 100644
--- 
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java
+++ 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/GreetingCommand.java
@@ -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) {
diff --git 
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java
 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java
new file mode 100644
index 0000000000..36d6650fb4
--- /dev/null
+++ 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/HttpCommandDecoderTest.java
@@ -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"));
+    }
+}
diff --git 
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java
 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java
new file mode 100644
index 0000000000..e9aed83fae
--- /dev/null
+++ 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/decoder/TelnetCommandDecoderTest.java
@@ -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"));
+    }
+}
diff --git 
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java
 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java
new file mode 100644
index 0000000000..180a144fcc
--- /dev/null
+++ 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/HelpTest.java
@@ -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"));
+    }
+}
diff --git 
a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java
 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java
new file mode 100644
index 0000000000..a4c239a1d5
--- /dev/null
+++ 
b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/impl/LsTest.java
@@ -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("registry://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"));
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to