Skip to content

Commit

Permalink
[dubbo-1689]: Enhance the test coverage part-10 : dubbo-plugin module (
Browse files Browse the repository at this point in the history
…#2001)

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module

* fix unit test failure

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module

*     #1689: Enhance the test coverage part-10 : dubbo-plugin module
  • Loading branch information
beiwei30 authored Jun 28, 2018
1 parent 534fd24 commit a1b301d
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.apache.dubbo.qos.server.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.net.InetAddress;
import java.net.InetSocketAddress;

import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class LocalHostPermitHandlerTest {
@Test
public void testHandlerAdded() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
Channel channel = mock(Channel.class);
when(context.channel()).thenReturn(channel);
InetAddress addr = mock(InetAddress.class);
when(addr.isLoopbackAddress()).thenReturn(false);
InetSocketAddress address = new InetSocketAddress(addr, 12345);
when(channel.remoteAddress()).thenReturn(address);
ChannelFuture future = mock(ChannelFuture.class);
when(context.writeAndFlush(any(ByteBuf.class))).thenReturn(future);
LocalHostPermitHandler handler = new LocalHostPermitHandler(false);
handler.handlerAdded(context);
ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
verify(context).writeAndFlush(captor.capture());
assertThat(new String(captor.getValue().array()), containsString("Foreign Ip Not Permitted"));
verify(future).addListener(ChannelFutureListener.CLOSE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.apache.dubbo.qos.server.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;

public class QosProcessHandlerTest {
@Test
public void testDecodeHttp() throws Exception {
ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'G'});
ChannelHandlerContext context = Mockito.mock(ChannelHandlerContext.class);
ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class);
Mockito.when(context.pipeline()).thenReturn(pipeline);
QosProcessHandler handler = new QosProcessHandler("welcome", false);
handler.decode(context, buf, Collections.emptyList());
verify(pipeline).addLast(any(HttpServerCodec.class));
verify(pipeline).addLast(any(HttpObjectAggregator.class));
verify(pipeline).addLast(any(HttpProcessHandler.class));
verify(pipeline).remove(handler);
}

@Test
public void testDecodeTelnet() throws Exception {
ByteBuf buf = Unpooled.wrappedBuffer(new byte[] {'A'});
ChannelHandlerContext context = Mockito.mock(ChannelHandlerContext.class);
ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class);
Mockito.when(context.pipeline()).thenReturn(pipeline);
QosProcessHandler handler = new QosProcessHandler("welcome", false);
handler.decode(context, buf, Collections.emptyList());
verify(pipeline).addLast(any(LineBasedFrameDecoder.class));
verify(pipeline).addLast(any(StringDecoder.class));
verify(pipeline).addLast(any(StringEncoder.class));
verify(pipeline).addLast(any(TelnetProcessHandler.class));
verify(pipeline).remove(handler);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.apache.dubbo.qos.server.handler;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class TelnetProcessHandlerTest {
@Test
public void testPrompt() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
TelnetProcessHandler handler = new TelnetProcessHandler();
handler.channelRead0(context, "");
verify(context).writeAndFlush(QosProcessHandler.prompt);
}

@Test
public void testBye() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
TelnetProcessHandler handler = new TelnetProcessHandler();
ChannelFuture future = mock(ChannelFuture.class);
when(context.writeAndFlush("BYE!\n")).thenReturn(future);
handler.channelRead0(context, "quit");
verify(future).addListener(ChannelFutureListener.CLOSE);
}

@Test
public void testUnknownCommand() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
TelnetProcessHandler handler = new TelnetProcessHandler();
handler.channelRead0(context, "unknown");
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(context, Mockito.atLeastOnce()).writeAndFlush(captor.capture());
assertThat(captor.getAllValues(), contains("unknown :no such command", "\r\ndubbo>"));
}

@Test
public void testGreeting() throws Exception {
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
TelnetProcessHandler handler = new TelnetProcessHandler();
handler.channelRead0(context, "greeting");
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(context).writeAndFlush(captor.capture());
assertThat(captor.getValue(), containsString("greeting"));
assertThat(captor.getValue(), containsString("dubbo>"));
}
}

0 comments on commit a1b301d

Please sign in to comment.