Skip to content

Commit 72b5ac2

Browse files
tamasradumaibin
authored andcommitted
Updated unit test to use assertj (eugenp#4025)
1 parent 6f887d2 commit 72b5ac2

1 file changed

Lines changed: 63 additions & 73 deletions

File tree

Lines changed: 63 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.baeldung.netty;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertTrue;
5-
63
import java.nio.charset.Charset;
74

8-
import org.junit.Assert;
5+
import static org.assertj.core.api.Assertions.*;
6+
7+
import org.assertj.core.api.Assertions;
98
import org.junit.Test;
109

1110
import io.netty.channel.embedded.EmbeddedChannel;
@@ -17,91 +16,82 @@
1716
import io.netty.handler.codec.http.HttpVersion;
1817

1918
public class EmbeddedChannelUnitTest {
20-
19+
2120
@Test
2221
public void givenTwoChannelHandlers_testPipeline() {
23-
24-
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5");
25-
httpRequest.headers().add("Operator", "Add");
2622

27-
EmbeddedChannel channel = new EmbeddedChannel(
28-
new HttpMessageHandler(), new CalculatorOperationHandler());
29-
30-
channel.pipeline()
31-
.addFirst(new HttpMessageHandler())
32-
.addLast(new CalculatorOperationHandler());
23+
final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
24+
"/calculate?a=10&b=5");
25+
httpRequest.headers().add("Operator", "Add");
3326

27+
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
3428

35-
// send HTTP request to server and check that the message is on the inbound pipeline
36-
assertTrue(channel.writeInbound(httpRequest));
29+
channel.pipeline().addFirst(new HttpMessageHandler()).addLast(new CalculatorOperationHandler());
3730

38-
long inboundChannelResponse = channel.readInbound();
39-
assertEquals(15, inboundChannelResponse);
31+
// send HTTP request to server and check that the message is on the inbound pipeline
32+
assertThat(channel.writeInbound(httpRequest)).isTrue();
4033

41-
// we should have an outbound message in the form of a HTTP response
42-
assertEquals(1, channel.outboundMessages().size());
43-
// Object response = channel.readOutbound();
34+
long inboundChannelResponse = channel.readInbound();
35+
assertThat(inboundChannelResponse).isEqualTo(15);
4436

45-
FullHttpResponse httpResponse = channel.readOutbound();
46-
String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset());
47-
assertTrue("15".equalsIgnoreCase(httpResponseContent));
37+
// we should have an outbound message in the form of a HTTP response
38+
assertThat(channel.outboundMessages().size()).isEqualTo(1);
39+
// Object response = channel.readOutbound();
40+
41+
FullHttpResponse httpResponse = channel.readOutbound();
42+
String httpResponseContent = httpResponse.content().toString(Charset.defaultCharset());
43+
assertThat(httpResponseContent).isEqualTo("15");
4844
}
49-
45+
5046
@Test
5147
public void givenTwoChannelHandlers_testExceptionHandlingInHttpMessageHandler() {
52-
53-
EmbeddedChannel channel = new EmbeddedChannel(
54-
new HttpMessageHandler(), new CalculatorOperationHandler());
55-
56-
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/calculate?a=10&b=5");
57-
wrongHttpRequest.headers().add("Operator", "Add");
58-
59-
try {
60-
// send invalid HTTP request to server and expect and error
61-
channel.pipeline().fireChannelRead(wrongHttpRequest);
62-
channel.checkException();
63-
// channel.writeInbound(wrongHttpRequest);
64-
Assert.fail();
65-
66-
} catch (Exception ex) {
67-
68-
// the HttpMessageHandler does not handle the exception and throws it down the pipeline
69-
assertTrue(ex instanceof UnsupportedOperationException);
70-
assertTrue(ex.getMessage().equalsIgnoreCase("HTTP method not supported"));
71-
72-
FullHttpResponse errorHttpResponse = channel.readOutbound();
73-
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
74-
assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent));
75-
assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status());
76-
}
77-
}
78-
79-
80-
@Test
81-
public void givenTwoChannelHandlers_testExceptionHandlingInCalculatorOperationHandler() {
82-
EmbeddedChannel channel = new EmbeddedChannel(
83-
new HttpMessageHandler(), new CalculatorOperationHandler());
8448

85-
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/calculate?a=10&b=5");
86-
wrongHttpRequest.headers().add("Operator", "Invalid_operation");
49+
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
50+
51+
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
52+
"/calculate?a=10&b=5");
53+
wrongHttpRequest.headers().add("Operator", "Add");
8754

88-
try {
89-
// send invalid HTTP request to server and expect and error
90-
channel.writeInbound(wrongHttpRequest);
91-
Assert.fail();
55+
Throwable thrownException = catchThrowable(() -> {
56+
// send invalid HTTP request to server and expect and error
57+
channel.pipeline().fireChannelRead(wrongHttpRequest);
58+
channel.checkException();
59+
Assertions.failBecauseExceptionWasNotThrown(UnsupportedOperationException.class);
60+
});
9261

93-
} catch (Exception ex) {
62+
assertThat(thrownException)
63+
.isInstanceOf(UnsupportedOperationException.class)
64+
.hasMessage("HTTP method not supported");
9465

95-
// the HttpMessageHandler does not handle the exception and throws it down the pipeline
96-
assertTrue(ex instanceof IllegalArgumentException);
97-
assertTrue(ex.getMessage().equalsIgnoreCase("Operation not defined"));
66+
FullHttpResponse errorHttpResponse = channel.readOutbound();
67+
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
68+
assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
69+
assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
70+
}
9871

99-
// the outbound message is a HTTP response with the status code 500
100-
FullHttpResponse errorHttpResponse = channel.readOutbound();
101-
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
102-
assertTrue("Operation not defined".equalsIgnoreCase(errorHttpResponseContent));
103-
assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorHttpResponse.status());
104-
}
72+
@Test
73+
public void givenTwoChannelHandlers_testExceptionHandlingInCalculatorOperationHandler() {
74+
EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler());
75+
76+
final FullHttpRequest wrongHttpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
77+
"/calculate?a=10&b=5");
78+
wrongHttpRequest.headers().add("Operator", "Invalid_operation");
79+
80+
Throwable thrownException = catchThrowable(() -> {
81+
// send invalid HTTP request to server and expect and error
82+
channel.writeInbound(wrongHttpRequest);
83+
Assertions.failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
84+
});
85+
86+
// the HttpMessageHandler does not handle the exception and throws it down the
87+
// pipeline
88+
assertThat(thrownException).isInstanceOf(IllegalArgumentException.class).hasMessage("Operation not defined");
89+
90+
// the outbound message is a HTTP response with the status code 500
91+
FullHttpResponse errorHttpResponse = channel.readOutbound();
92+
String errorHttpResponseContent = errorHttpResponse.content().toString(Charset.defaultCharset());
93+
assertThat(errorHttpResponseContent).isEqualToIgnoringCase("Operation not defined");
94+
assertThat(errorHttpResponse.status()).isEqualTo(HttpResponseStatus.INTERNAL_SERVER_ERROR);
10595
}
10696

10797
}

0 commit comments

Comments
 (0)