StubResponseGenerator { public byte[] generateResponse(final HttpServletRequest req, final HttpServletResponse resp) { String wilmaLoggerId = req.getHeader(WilmaHttpRequest.WILMA_LOGGER_ID); byte[] result = null; if (wilmaLoggerId != null) { ResponseDescriptorDTO responseDescriptorDTO = mapBasedResponseDescriptorAccess.getResponseDescriptor(wilmaLoggerId); Set<ResponseFormatterDescriptor> responseFormatterDescriptors = responseDescriptorDTO.getResponseDescriptor().getResponseFormatters(); WilmaHttpRequest wilmaRequest = httpServletRequestTransformer.transformToWilmaHttpRequest(wilmaLoggerId, req, responseDescriptorDTO); stubResponseHeaderConfigurer.addWilmaInfoToResponseHeader(req, resp, responseDescriptorDTO.getDialogDescriptorName()); result = generate(resp, responseDescriptorDTO, responseFormatterDescriptors, wilmaRequest); } return result; }  byte[] generateResponse(final HttpServletRequest req, final HttpServletResponse resp);  }
@Test public void testGenerateResponseShouldGetWilmaLoggerIdFromRequestHeaders() { underTest.generateResponse(request, response); verify(request).getHeader(WilmaHttpRequest.WILMA_LOGGER_ID); }
@Test public void testGenerateResponseShouldGetResponseDescriptorDTOFromResponseDescriptorAccess() { underTest.generateResponse(request, response); verify(responseDescriptorAccess).getResponseDescriptor(anyString()); }
@Test public void testGenerateResponseShouldResponseFormatterDescriptorsFromResponseDescriptorDTO() { underTest.generateResponse(request, response); verify(responseDescriptorDTO, times(2)).getResponseDescriptor(); verify(responseDescriptor).getResponseFormatters(); }
@Test public void testGenerateResponseShouldTransformWilmaHttpRequestFromHttpServletRequestAndResponseDescriptorDTO() { underTest.generateResponse(request, response); verify(requestTransformer).transformToWilmaHttpRequest(WILMA_LOGGER_ID, request, responseDescriptorDTO); }
@Test public void testGenerateResponseShouldAddWilmaInfoToWilmaHttpRequest() { String descriptorName = "dialog-descriptor-name"; given(responseDescriptorDTO.getDialogDescriptorName()).willReturn(descriptorName); underTest.generateResponse(request, response); verify(headerConfigurer).addWilmaInfoToResponseHeader(request, response, descriptorName); }
@Test public void testGenerateResponseShouldReturnTemplateResourceAndSetResponseHeadersWhenResponseFormatterDescriptorsArrayIsNull() throws InterruptedException { mockTemplateResource(); responseFormatterDescriptors = null; byte[] actual = underTest.generateResponse(request, response); verify(headerConfigurer).setResponseContentTypeAndStatus(response, responseDescriptorDTO); Assert.assertEquals(actual, templateResource); }
@Test public void testGenerateResponseShouldReturnTemplateResourceAndSetResponseHeadersWhenNoResponseFormatterDefined() { mockTemplateResource(); byte[] actual = underTest.generateResponse(request, response); verify(headerConfigurer).setResponseContentTypeAndStatus(response, responseDescriptorDTO); Assert.assertEquals(actual, templateResource); }
@Test public void testGenerateResponseShouldReturnTemplateResourceAndSetResponseHeadersWhenResponseFormatterDefined() throws Exception { ParameterList params = new ParameterList(); responseFormatterDescriptors.add(responseFormatterDescriptor); mockTemplateResource(); given(responseFormatterDescriptor.getParams()).willReturn(params); given(responseFormatterDescriptor.getResponseFormatter()).willReturn(responseFormatter); given(responseFormatter.formatResponse(wilmaRequest, response, templateResource, params)).willReturn(templateResource); byte[] actual = underTest.generateResponse(request, response); verify(headerConfigurer).setResponseContentTypeAndStatus(response, responseDescriptorDTO); Assert.assertEquals(actual, templateResource); }
@Test public void testGenerateResponseShouldReturnStackTraceAsResourceAndSetErrorResponseWhenResponseFormattingFailed() throws Exception { ParameterList params = new ParameterList(); responseFormatterDescriptors.add(responseFormatterDescriptor); mockTemplateResource(); given(responseFormatterDescriptor.getResponseFormatter()).willReturn(responseFormatter); given(responseFormatterDescriptor.getParams()).willReturn(params); given(responseFormatter.formatResponse(wilmaRequest, response, templateResource, params)).willThrow( new ResponseFormattingFailedException("Response formatting failed....", new ResponseFormattingFailedException(""))); underTest.generateResponse(request, response); verify(stackTraceConverter).getStackTraceAsString(Matchers.any(ResponseFormattingFailedException.class)); verify(headerConfigurer).setErrorResponseContentTypeAndStatus(response); }
@Test public void testGenerateResponseShouldReturnWithNullWhenNotFindWilmaLoggerIdInRequestHeader() { given(request.getHeader(WilmaHttpRequest.WILMA_LOGGER_ID)).willReturn(null); byte[] result = underTest.generateResponse(request, response); Assert.assertNull(result); }
@Test public void testWhenActualSequenceIsNullShouldNotWaitForSequenceResponses() throws InterruptedException { given(wilmaRequest.getHeader(WilmaHttpEntity.WILMA_SEQUENCE_ID)).willReturn("abcd"); String[] sequenceIds = new String[]{"id"}; given(headerCreator.resolveSequenceHeader("abcd")).willReturn(sequenceIds); given(matcher.matchSequenceKeyWithDescriptor(SEQUENCE_DESCRIPTOR_KEY, sequenceIds)).willReturn(null); underTest.generateResponse(request, response); verify(sequenceResponseGuard, never()).waitForResponses(wilmaRequest, null); }
@Test public void testWhenActualSequenceIsNotNullShouldWaitForSequenceResponses() throws InterruptedException { WilmaSequence sequence = Mockito.mock(WilmaSequence.class); given(wilmaRequest.getHeader(WilmaHttpEntity.WILMA_SEQUENCE_ID)).willReturn("abcd"); String[] sequenceIds = new String[]{"id"}; given(headerCreator.resolveSequenceHeader("abcd")).willReturn(sequenceIds); given(matcher.matchSequenceKeyWithDescriptor(SEQUENCE_DESCRIPTOR_KEY, sequenceIds)).willReturn(sequence); underTest.generateResponse(request, response); verify(sequenceResponseGuard).waitForResponses(wilmaRequest, sequence); }