package net.krecan.springws.test.generator;

import static net.krecan.springws.test.util.XmlUtil.loadDocument;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Collections;

import net.krecan.springws.test.AbstractMessageTest;

import org.custommonkey.xmlunit.Diff;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.WebServiceMessage;
import org.springframework.xml.transform.ResourceSource;
import org.springframework.xml.xpath.XPathExpression;
import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Document;


public class PathBasedResponseGeneratorTest extends AbstractMessageTest{
	
	private PathBasedResponseGenerator generator;
	public PathBasedResponseGeneratorTest() {
		generator = new PathBasedResponseGenerator();
		XPathExpression resourceXpathExpression = XPathExpressionFactory.createXPathExpression("concat('mock-responses/test/',//ns:text,'-response.xml')", Collections.singletonMap("ns", "http://www.example.org/schema"));
		generator.setResourceXPathExpression(resourceXpathExpression);
	}
	@Test
	public void testDefaultResponse() throws IOException
	{
		WebServiceMessage response = generator.generateResponse(null, messageFactory, createMessage("xml/valid-message.xml"));
		assertNotNull(response);
		
		Document controlDocument = loadDocument(new ResourceSource(new ClassPathResource("mock-responses/test/default-response.xml")));
		Diff diff = new Diff(controlDocument, loadDocument(response));
		assertTrue(diff.toString(), diff.similar());
	}
	@Test
	public void testDifferentResponse() throws IOException
	{
		WebServiceMessage response = generator.generateResponse(null, messageFactory, createMessage("xml/valid-message2.xml"));
		assertNotNull(response);
		
		Document controlDocument = loadDocument(new ResourceSource(new ClassPathResource("mock-responses/test/different-response.xml")));
		Diff diff = new Diff(controlDocument, loadDocument(response));
		assertTrue(diff.toString(), diff.similar());
	}
}

@@ -239,27 +239,45 @@
 va.util.
-Collections
+HashMap;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -905,16 +905,219 @@
 ator();%0A
+%09%09Map%3CString, String%3E namespaceMap = new HashMap%3CString, String%3E();%0A%09%09namespaceMap.put(%22ns%22, %22http://www.example.org/schema%22);%0A%09%09namespaceMap.put(%22soapenv%22, %22http://schemas.xmlsoap.org/soap/envelope/%22);%0A
 %09%09XPathE
@@ -1221,20 +1221,45 @@
 sponses/
-test
+',name(//soapenv:Body/*%5B1%5D),'
 /',//ns:
@@ -1286,71 +1286,20 @@
 )%22, 
-Collections.singletonMap(%22ns%22, %22http://www.example.org/schema%22)
+namespaceMap
 );%0A%09

Test enhanced