### Question:
TraceeEndpointInterceptor extends AbstractTraceeInterceptor implements EndpointInterceptor { @Override public boolean handleRequest(MessageContext messageContext, Object o) { parseContextFromSoapHeader(messageContext.getRequest(), IncomingRequest); Utilities.generateInvocationIdIfNecessary(backend); return true; } TraceeEndpointInterceptor();  TraceeEndpointInterceptor(final String profile);  TraceeEndpointInterceptor(final TraceeBackend backend, final String profile); @Override boolean handleRequest(MessageContext messageContext, Object o); @Override boolean handleResponse(MessageContext messageContext, Object o); @Override boolean handleFault(MessageContext messageContext, Object o); @Override void afterCompletion(MessageContext messageContext, Object o, Exception e);  }### Answer:
@Test public void skipIncomingHeaderProcessingIfNoSoapHeaderIsPresentAndGenerateRequestId() throws Exception { when(((SoapMessage) messageContext.getRequest()).getSoapHeader()).thenReturn(null); unit.handleRequest(messageContext, new Object()); assertThat(backend.copyToMap(), hasKey(INVOCATION_ID_KEY)); assertThat(backend.size(), is(1)); }
@Test public void skipIncomingHeaderProcessingIfNoTpicHeaderIsPresentAndGenerateRequestId() throws Exception { final SoapHeader soapHeader = mock(SoapHeader.class); when(((SoapMessage) messageContext.getRequest()).getSoapHeader()).thenReturn(soapHeader); when(soapHeader.examineHeaderElements(eq(SOAP_HEADER_QNAME))).thenReturn(Collections.<SoapHeaderElement>emptyList().iterator()); unit.handleRequest(messageContext, new Object()); assertThat(backend.copyToMap(), hasKey(INVOCATION_ID_KEY)); assertThat(backend.size(), is(1)); }
@Test public void parseTpicHeaderFromRequestToTraceeBackend() throws Exception { final Map<String, String> context = new HashMap<>(); context.put("our key", "is our value"); final StringResult result = new StringResult(); new SoapHeaderTransport().renderSoapHeader(context, result); final Source source = new StringSource(result.toString()); final SoapHeader soapHeader = mock(SoapHeader.class); when(((SoapMessage) messageContext.getRequest()).getSoapHeader()).thenReturn(soapHeader); final SoapHeaderElement element = mock(SoapHeaderElement.class); when(element.getSource()).thenReturn(source); when(soapHeader.examineHeaderElements(eq(SOAP_HEADER_QNAME))).thenReturn(singletonList(element).iterator()); unit.handleRequest(messageContext, new Object()); assertThat(backend.size(), is(2)); assertThat(backend.copyToMap(), hasKey(INVOCATION_ID_KEY)); assertThat(backend.copyToMap(), hasEntry("our key", "is our value")); }
@Test public void handleSoapHeaderExceptionGraceful() { final SoapHeader soapHeader = mock(SoapHeader.class); when(soapHeader.examineHeaderElements(eq(SOAP_HEADER_QNAME))).thenThrow(new SoapHeaderException("test exception")); when(((SoapMessage) messageContext.getRequest()).getSoapHeader()).thenReturn(soapHeader); unit.handleRequest(messageContext, new Object()); assertThat(backend.size(), is(1)); assertThat(backend.copyToMap(), hasKey(INVOCATION_ID_KEY)); }
@Test public void doNotAddTpicHeaderToResponseIfBackendIsEmpty() throws Exception { unit.handleRequest(messageContext, new Object()); verify(((SoapMessage) messageContext.getResponse()).getSoapHeader(), never()).addHeaderElement(eq(SOAP_HEADER_QNAME)); }
@Test public void skipProcessingWithWrongMessageTypeOnRequest() throws Exception { when(messageContext.getRequest()).thenReturn(mock(WebServiceMessage.class)); unit.handleRequest(messageContext, new Object()); assertThat(backend.copyToMap(), hasKey(TraceeConstants.INVOCATION_ID_KEY)); }