@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) throws Exception { sendResponse(ctx, msg); return PipelineContinuationBehavior.CONTINUE; }
@Override public PipelineContinuationBehavior doExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { sendResponse(ctx, null); return PipelineContinuationBehavior.CONTINUE; }
@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) throws Exception { sendResponse(ctx, msg); return PipelineContinuationBehavior.CONTINUE; }
@Override public PipelineContinuationBehavior doExceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { sendResponse(ctx, null); return PipelineContinuationBehavior.CONTINUE; }
@Test public void doChannelRead_calls_sendResponse_and_returns_CONTINUE() throws Exception { // given Object msg = new Object(); // when PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg); // then verify(handlerSpy).sendResponse(ctxMock, msg); assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
@Test public void doExceptionCaught_calls_sendResponse_and_returns_CONTINUE() throws Exception { // when PipelineContinuationBehavior result = handlerSpy.doExceptionCaught(ctxMock, new Exception("intentional test exception")); // then verify(handlerSpy).sendResponse(ctxMock, null); assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE); }
@Test public void sendResponse_uses_dummy_RequestInfo_if_state_is_missing_requestInfo() throws JsonProcessingException { // given doReturn(null).when(stateMock).getRequestInfo(); // when handlerSpy.sendResponse(ctxMock, null); // then ArgumentCaptor<RequestInfo> requestInfoArgumentCaptor = ArgumentCaptor.forClass(RequestInfo.class); verify(responseSenderMock).sendFullResponse(eq(ctxMock), requestInfoArgumentCaptor.capture(), eq(responseInfo), eq(customSerializerMock)); RequestInfo requestInfoUsed = requestInfoArgumentCaptor.getValue(); assertThat(requestInfoUsed.getUri()).isEqualTo(RequestInfo.NONE_OR_UNKNOWN_TAG); }
@Test public void sendResponse_passes_null_serializer_if_endpoint_is_null() throws JsonProcessingException { // given RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); doReturn(requestInfo).when(stateMock).getRequestInfo(); doReturn(null).when(stateMock).getEndpointForExecution(); // when handlerSpy.sendResponse(ctxMock, null); // then verify(responseSenderMock).sendFullResponse(ctxMock, requestInfo, responseInfo, null); }
@Test public void sendResponse_passes_null_serializer_if_endpoint_has_no_custom_serializer() throws JsonProcessingException { // given RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); doReturn(requestInfo).when(stateMock).getRequestInfo(); doReturn(null).when(endpointExecutedMock).customResponseContentSerializer(any(RequestInfo.class)); // when handlerSpy.sendResponse(ctxMock, null); // then verify(responseSenderMock).sendFullResponse(ctxMock, requestInfo, responseInfo, null); }
@Test public void sendResponse_calls_responseSender_sendResponse_for_non_error_content() throws JsonProcessingException { // given RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); doReturn(requestInfo).when(stateMock).getRequestInfo(); Whitebox.setInternalState(responseInfo, "contentForFullResponse", UUID.randomUUID().toString()); // when handlerSpy.sendResponse(ctxMock, null); // then verify(responseSenderMock).sendFullResponse(ctxMock, requestInfo, responseInfo, customSerializerMock); }
@Test public void sendResponse_calls_responseSender_sendErrorResponse_for_error_content() throws JsonProcessingException { // given RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); doReturn(requestInfo).when(stateMock).getRequestInfo(); ErrorResponseBody errorContentMock = mock(ErrorResponseBody.class); Whitebox.setInternalState(responseInfo, "contentForFullResponse", errorContentMock); // when handlerSpy.sendResponse(ctxMock, null); // then verify(responseSenderMock).sendErrorResponse(ctxMock, requestInfo, (ResponseInfo<ErrorResponseBody>) responseInfo); }
@Test public void sendResponse_calls_responseSender_sendResponse_for_null_content() throws JsonProcessingException { // given RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests(); doReturn(requestInfo).when(stateMock).getRequestInfo(); Whitebox.setInternalState(responseInfo, "contentForFullResponse", null); // when handlerSpy.sendResponse(ctxMock, null); // then verify(responseSenderMock).sendFullResponse(ctxMock, requestInfo, responseInfo, customSerializerMock); }