### Question:
MessageIdHeaderInterceptor extends AbstractPhaseInterceptor<Message> { @Override public void handleMessage(Message message) { List<SoapHeader> headers = (ArrayList<SoapHeader>) message.get(SOAP_HEADER_KEY); if(!messageIdHeaderExists(headers) && !headers.isEmpty()) { Element existingHeaderElement = (Element) headers.get(0).getObject(); Element element = existingHeaderElement.getOwnerDocument().createElementNS(NAMESPACE_URI, QUALIFIED_NAME); element.appendChild(existingHeaderElement.getOwnerDocument().createTextNode("uuid:" + UUID.randomUUID().toString())); QName qname = new QName(NAMESPACE_URI, LOCAL_NAME); SoapHeader header = new SoapHeader(qname, element); header.setDirection(Header.Direction.DIRECTION_IN); headers.add(header); } } MessageIdHeaderInterceptor(); @Override void handleMessage(Message message);  }### Answer:
@Test public void handleMessageWithoutHeaders() { when(message.get(anyString())).thenReturn(new ArrayList<SOAPHeader>()); interceptor.handleMessage(message); }
@Test public void handleMessageWithoutHeadersVerifyHeadersNotAltered() { ArrayList<SoapHeader> soapHeaders = new ArrayList<>(); when(message.get(anyString())).thenReturn(soapHeaders); interceptor.handleMessage(message); assertEquals(0, soapHeaders.size()); }
@Test public void handleMessageWithMessageIdHeaderVerifyNoNewHeaderAdded() { ArrayList<SoapHeader> soapHeaders = new ArrayList<>(); SoapHeader soapHeader = mock(SoapHeader.class); when(soapHeader.getName()).thenReturn(new QName(MESSAGE_ID_LOCAL_NAME)); soapHeaders.add(soapHeader); when(message.get(anyString())).thenReturn(soapHeaders); interceptor.handleMessage(message); assertEquals(1, soapHeaders.size()); }
@Test public void handleMessageWithoutMessageIdHeaderVerifyNewHeaderAdded() { SoapHeader soapHeader = mock(SoapHeader.class); when(soapHeader.getName()).thenReturn(new QName(OTHER_HEADER_LOCAL_NAME)); Element element = mock(Element.class); try { when(element.getOwnerDocument()).thenReturn(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); } catch (ParserConfigurationException e) { fail(); } when(soapHeader.getObject()).thenReturn(element); ArrayList<SoapHeader> soapHeaders = new ArrayList<>(); soapHeaders.add(soapHeader); when(message.get(anyString())).thenReturn(soapHeaders); interceptor.handleMessage(message); assertEquals(2, soapHeaders.size()); SoapHeader messageIdHeader = soapHeaders.get(1); assertEquals(Header.Direction.DIRECTION_IN, messageIdHeader.getDirection()); assertEquals(MESSAGE_ID_LOCAL_NAME, ((Element) messageIdHeader.getObject()).getLocalName()); assertNotNull(((Element) messageIdHeader.getObject()).getFirstChild().getNodeValue()); }