package io.tracee.jaxws.container;

import io.tracee.NoopTraceeLoggerFactory;
import io.tracee.PermitAllTraceeFilterConfiguration;
import io.tracee.TraceeBackend;
import io.tracee.transport.SoapHeaderTransport;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import javax.xml.bind.JAXBException;
import javax.xml.soap.*;
import javax.xml.ws.handler.soap.SOAPMessageContext;

import java.util.Collections;

import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.refEq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class TraceeServerHandlerTest {

	private final TraceeBackend backend = mock(TraceeBackend.class);
	{
		when(backend.getLoggerFactory()).thenReturn(new NoopTraceeLoggerFactory());
	}
	private final SoapHeaderTransport soapHeaderTransport = mock(SoapHeaderTransport.class);
	private final TraceeServerHandler unit = new TraceeServerHandler(backend, soapHeaderTransport);
	private final SOAPMessageContext soapMessageContext = mock(SOAPMessageContext.class);
	private final SOAPMessage soapMessage = mock(SOAPMessage.class);
	private final SOAPPart soapPart = mock(SOAPPart.class);
	private final SOAPEnvelope soapEnvelope = mock(SOAPEnvelope.class);
	private final SOAPHeader soapHeader = mock(SOAPHeader.class);

	@Before
	public void setUp() throws SOAPException {
		when(backend.getConfiguration()).thenReturn(new PermitAllTraceeFilterConfiguration());
		when(soapMessageContext.getMessage()).thenReturn(soapMessage);
		when(soapMessage.getSOAPPart()).thenReturn(soapPart);
		when(soapPart.getEnvelope()).thenReturn(soapEnvelope);
		when(soapEnvelope.getHeader()).thenReturn(soapHeader);
	}

	@Test
	public void testHandleIncoming() throws JAXBException {
		when(soapHeaderTransport.parseSoapHeader(eq(soapHeader))).thenReturn(Collections.singletonMap("FOO","BAR"));
		unit.handleIncoming(soapMessageContext);
		verify(backend).putAll(Collections.singletonMap("FOO", "BAR"));
	}

	@Test
	public void testHandleOutgoing() throws SOAPException, JAXBException {
		unit.handleOutgoing(soapMessageContext);
		verify(soapHeaderTransport).renderSoapHeader(eq(backend), eq(soapHeader));
	}

	@Test
	public void testHandleOutgoingHasCalledClearInTheEnd() throws SOAPException {
		unit.handleOutgoing(soapMessageContext);
		verify(backend).clear();
	}
}

@@ -50,84 +50,27 @@
 cee.
-NoopTraceeLoggerFactory;%0Aimport io.tracee.PermitAllTraceeFilterConfiguration
+SimpleTraceeBackend
 ;%0Aim
@@ -198,36 +198,8 @@
 est;
-%0Aimport org.mockito.Mockito;
 %0A%0Aim
@@ -259,9 +259,158 @@
 oap.
-*
+SOAPEnvelope;%0Aimport javax.xml.soap.SOAPException;%0Aimport javax.xml.soap.SOAPHeader;%0Aimport javax.xml.soap.SOAPMessage;%0Aimport javax.xml.soap.SOAPPart
 ;%0Aim
@@ -460,17 +460,16 @@
 ontext;%0A
-%0A
 import j
@@ -534,50 +534,8 @@
 eq;%0A
-import static org.mockito.Matchers.refEq;%0A
 impo
@@ -570,16 +570,55 @@
 o.mock;%0A
+import static org.mockito.Mockito.spy;%0A
 import s
@@ -771,21 +771,26 @@
 ckend = 
-mock(
+spy(Simple
 TraceeBa
@@ -800,99 +800,48 @@
 nd.c
-lass);%0A%09%7B%0A%09%09when(backend.getLoggerFactory()).thenReturn(new NoopTraceeLoggerFactory
+reateNonLoggingAllPermittingBackend
 ());%0A
-%09%7D%0A
 %09pri
@@ -1375,16 +1375,16 @@
 @Before%0A
+
 %09public 
@@ -1423,97 +1423,8 @@
 n %7B%0A
-%09%09when(backend.getConfiguration()).thenReturn(new PermitAllTraceeFilterConfiguration());%0A
 %09%09wh

Test refactoring