Updated unit test to use assertj (#4025)

This commit is contained in:
tamasradu 2018-04-16 17:38:24 +03:00 committed by maibin
parent 6f887d29c6
commit 72b5ac265c
1 changed files with 56 additions and 66 deletions

View File

@ -1,11 +1,10 @@
package com.baeldung.netty; package com.baeldung.netty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import org.junit.Assert; import static org.assertj.core.api.Assertions.*;
import org.assertj.core.api.Assertions;
import org.junit.Test; import org.junit.Test;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
@ -17,91 +16,82 @@ import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.codec.http.HttpVersion;
public class EmbeddedChannelUnitTest { public class EmbeddedChannelUnitTest {
@Test @Test
public void givenTwoChannelHandlers_testPipeline() { public void givenTwoChannelHandlers_testPipeline() {
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5");
httpRequest.headers().add("Operator", "Add");
EmbeddedChannel channel = new EmbeddedChannel( final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
new HttpMessageHandler(), new CalculatorOperationHandler()); "/calculate?a=10&b=5");
httpRequest.headers().add("Operator", "Add");
channel.pipeline()
.addFirst(new HttpMessageHandler())
.addLast(new CalculatorOperationHandler());
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
// send HTTP request to server and check that the message is on the inbound pipeline channel.pipeline().addFirst(new HttpMessageHandler()).addLast(new CalculatorOperationHandler());
assertTrue(channel.writeInbound(httpRequest));
long inboundChannelResponse = channel.readInbound(); // send HTTP request to server and check that the message is on the inbound pipeline
assertEquals(15, inboundChannelResponse); assertThat(channel.writeInbound(httpRequest)).isTrue();
// we should have an outbound message in the form of a HTTP response long inboundChannelResponse = channel.readInbound();
assertEquals(1, channel.outboundMessages().size()); assertThat(inboundChannelResponse).isEqualTo(15);
// Object response = channel.readOutbound();
FullHttpResponse httpResponse = channel.readOutbound(); // we should have an outbound message in the form of a HTTP response
String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset()); assertThat(channel.outboundMessages().size()).isEqualTo(1);
assertTrue("15".equalsIgnoreCase(httpResponseContent)); // Object response = channel.readOutbound();
FullHttpResponse httpResponse = channel.readOutbound();
String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset());
assertThat(httpResponseContent).isEqualTo("15");
} }
@Test @Test
public void givenTwoChannelHandlers_testExceptionHandlingInHttpMessageHandler() { public void givenTwoChannelHandlers_testExceptionHandlingInHttpMessageHandler() {
EmbeddedChannel channel = new EmbeddedChannel(
new HttpMessageHandler(), new CalculatorOperationHandler());
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/calculate?a=10&b=5"); EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
wrongHttpRequest.headers().add("Operator", "Add");
try { final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
// send invalid HTTP request to server and expect and error "/calculate?a=10&b=5");
channel.pipeline().fireChannelRead(wrongHttpRequest); wrongHttpRequest.headers().add("Operator", "Add");
channel.checkException();
// channel.writeInbound(wrongHttpRequest);
Assert.fail();
} catch (Exception ex) { Throwable thrownException = catchThrowable(() -> {
// send invalid HTTP request to server and expect and error
channel.pipeline().fireChannelRead(wrongHttpRequest);
channel.checkException();
Assertions.failBecauseExceptionWasNotThrown(UnsupportedOperationException.class);
});
// the HttpMessageHandler does not handle the exception and throws it down the pipeline assertThat(thrownException)
assertTrue(ex instanceof UnsupportedOperationException); .isInstanceOf(UnsupportedOperationException.class)
assertTrue(ex.getMessage().equalsIgnoreCase("HTTP method not supported")); .hasMessage("HTTP method not supported");
FullHttpResponse errorHttpResponse = channel.readOutbound(); FullHttpResponse errorHttpResponse = channel.readOutbound();
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset()); String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent)); assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status()); assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
}
} }
@Test @Test
public void givenTwoChannelHandlers_testExceptionHandlingInCalculatorOperationHandler() { public void givenTwoChannelHandlers_testExceptionHandlingInCalculatorOperationHandler() {
EmbeddedChannel channel = new EmbeddedChannel( EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
new HttpMessageHandler(), new CalculatorOperationHandler());
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5"); final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
wrongHttpRequest.headers().add("Operator", "Invalid_operation"); "/calculate?a=10&b=5");
wrongHttpRequest.headers().add("Operator", "Invalid_operation");
try { Throwable thrownException = catchThrowable(() -> {
// send invalid HTTP request to server and expect and error // send invalid HTTP request to server and expect and error
channel.writeInbound(wrongHttpRequest); channel.writeInbound(wrongHttpRequest);
Assert.fail(); Assertions.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
});
} catch (Exception ex) { // the HttpMessageHandler does not handle the exception and throws it down the
// pipeline
assertThat(thrownException).isInstanceOf(IllegalArgumentException.class).hasMessage("Operation not defined");
// the HttpMessageHandler does not handle the exception and throws it down the pipeline // the outbound message is a HTTP response with the status code 500
assertTrue(ex instanceof IllegalArgumentException); FullHttpResponse errorHttpResponse = channel.readOutbound();
assertTrue(ex.getMessage().equalsIgnoreCase("Operation not defined")); String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
// the outbound message is a HTTP response with the status code 500 assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
FullHttpResponse errorHttpResponse = channel.readOutbound();
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent));
assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status());
}
} }
} }