-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dubbo-1689]: Enhance the test coverage part-10 : dubbo-plugin module (…
…#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
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...bbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/LocalHostPermitHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...in/dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/QosProcessHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...dubbo-qos/src/test/java/org/apache/dubbo/qos/server/handler/TelnetProcessHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>")); | ||
} | ||
} |