|
1 | 1 | package com.baeldung.netty; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertEquals; |
4 | | -import static org.junit.Assert.assertTrue; |
5 | | - |
6 | 3 | import java.nio.charset.Charset; |
7 | 4 |
|
8 | | -import org.junit.Assert; |
| 5 | +import static org.assertj.core.api.Assertions.*; |
| 6 | + |
| 7 | +import org.assertj.core.api.Assertions; |
9 | 8 | import org.junit.Test; |
10 | 9 |
|
11 | 10 | import io.netty.channel.embedded.EmbeddedChannel; |
|
17 | 16 | import io.netty.handler.codec.http.HttpVersion; |
18 | 17 |
|
19 | 18 | public class EmbeddedChannelUnitTest { |
20 | | - |
| 19 | + |
21 | 20 | @Test |
22 | 21 | 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"); |
26 | 22 |
|
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"); |
33 | 26 |
|
| 27 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpMessageHandler(), new CalculatorOperationHandler()); |
34 | 28 |
|
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()); |
37 | 30 |
|
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(); |
40 | 33 |
|
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); |
44 | 36 |
|
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"); |
48 | 44 | } |
49 | | - |
| 45 | + |
50 | 46 | @Test |
51 | 47 | 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()); |
84 | 48 |
|
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"); |
87 | 54 |
|
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 | + }); |
92 | 61 |
|
93 | | - } catch (Exception ex) { |
| 62 | + assertThat(thrownException) |
| 63 | + .isInstanceOf(UnsupportedOperationException.class) |
| 64 | + .hasMessage("HTTP method not supported"); |
94 | 65 |
|
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 | + } |
98 | 71 |
|
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); |
105 | 95 | } |
106 | 96 |
|
107 | 97 | } |
0 commit comments