package nl.hsac.fitnesse.fixture.util;

import fit.exception.FitFailureException;
import nl.hsac.fitnesse.fixture.Environment;
import nl.hsac.fitnesse.fixture.leanapps.LalPolicyXPaths;
import org.junit.Test;

import javax.xml.namespace.NamespaceContext;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Tests XPathHelper.
 */
public class XPathHelperTest {
    private static final NamespaceContext NS_CONTEXT = Environment.getInstance().getNamespaceContext();
    private XPathHelper xPathHelper = new XPathHelper();

    @Test
    public void testXPathWithNamespace() {
        LalPolicyXPaths.registerNamespace();
        String responseString = FileUtil.loadFile("leanapps/getPolicyCheckResponse.xml");
        assertEquals("OK", xPathHelper.getXPath(NS_CONTEXT, responseString, "//lal:status/lal:status"));
    }

    @Test
    public void testBadXPath() {
        String responseString = FileUtil.loadFile("leanapps/getPolicyCheckResponse.xml");
        assertEquals("", xPathHelper.getXPath(null, responseString, "//status"));
        try {
            xPathHelper.getXPath(null, responseString, "\\status");
            fail("expected exception");
        } catch (FitFailureException e) {
            assertTrue("Bad message start: " + e.getMessage(), e.getMessage().startsWith("Unable to compile xpath: \\status\n"));
            assertTrue("Bad message end: " + e.getMessage(), e.getMessage().endsWith("A location path was expected, but the following token was encountered:  \\"));
        }
    }

    @Test
    public void testBadXml() {
        try {
            xPathHelper.getXPath(null, "bla", "//status");
            fail("expected exception");
        } catch (FitFailureException e) {
            assertEquals("Cannot perform XPATH on non-xml: bla", e.getMessage());
        }
    }

    @Test
    public void testBadXml2() {
        try {
            xPathHelper.getXPath(null, "<bla", "//status");
            fail("expected exception");
        } catch (FitFailureException e) {
            assertEquals("Unable to evaluate xpath: //status\n" +
                                "XML document structures must start and end within the same entity.",
                            e.getMessage());
        }
    }

    @Test
    public void testAllXmlNoText() {
        LalPolicyXPaths.registerNamespace();
        String responseString = FileUtil.loadFile("leanapps/getPolicyCheckResponse.xml");
        List<String> all = xPathHelper.getAllXPath(NS_CONTEXT, responseString, "//*/@xsi:type");

        assertEquals(13, all.size());
        assertEquals("ns:PostalAddress", all.get(1));
    }

    @Test
    public void testAllXmlWithText() {
        LalPolicyXPaths.registerNamespace();
        String responseString = FileUtil.loadFile("leanapps/getPolicyCheckResponse.xml");
        List<String> all = xPathHelper.getAllXPath(NS_CONTEXT, responseString, "//lal:key/text()");

        assertEquals(17, all.size());
        assertEquals("20000541", all.get(0));
    }
}

@@ -1291,32 +1291,77 @@
 eException e) %7B%0A
+            String message = e.getMessage();%0A
             asse
@@ -1395,38 +1395,24 @@
 %22 + 
-e.getMessage(), e.getM
+message, m
 essage
-()
 .sta
@@ -1509,38 +1509,24 @@
 %22 + 
-e.getMessage(), e.getM
+message, m
 essage
-()
 .end
@@ -2133,28 +2133,123 @@
         
-assertEquals
+String message = e.getMessage();%0A            assertTrue(%22Bad message start: %22 + message, message.startsWith
 (%22Unable
@@ -2283,13 +2283,12 @@
 s%5Cn%22
- +%0A  
+));%0A
     
@@ -2295,34 +2295,75 @@
         
-                  
+assertTrue(%22Bad message end: %22 + message, message.endsWith(
 %22XML doc
@@ -2426,51 +2426,8 @@
 ty.%22
-,%0A                            e.getMessage(
 ));%0A

Make unit test more robust to exact xpath engine error messages