package be.iminds.ilabt.jfed.rspec.parser;

import org.testng.Assert;
import org.testng.annotations.Test;

import javax.xml.stream.XMLStreamException;

/**
 * TestRspecCompare
 */
public class TestRspecCompare {
    @Test
    public void testIsEqualRspec0() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspeclinkA);
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec1() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAsameA);
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());  //different order, missing generated and added generated_by
    }

    @Test
    public void testIsEqualRspec2() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAsameB);
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());  //has extra data in jfed namespace (which should be ignored)
    }

    @Test
    public void testIsEqualRspec3() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAdiffA);
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString()); //extra attribute in first node
    }

    @Test
    public void testIsEqualRspec4() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAdiffB);
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString()); //extra attribute in rspec
    }

    @Test
    public void testIsEqualRspec5() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAdiffC);
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString()); //missing attribute type in rspec
    }

    @Test
    public void testIsEqualRspec6() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAdiffD);
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString()); //missing sliver_type in 2nd node
    }

    @Test
    public void testIsEqualRspec7() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAdiffE);
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString()); //type manifest instead of request
    }

    @Test
    public void testIsEqualRspec8() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.testSamerspeclinkA, TestRspecsLibrary.testSamerspecAdiffF);
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString()); //missing link
    }

    @Test
    public void testIsEqualRspecStitchedManifestA() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.stitchedManifestA, TestRspecsLibrary.stitchedManifestA);
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());
    }
    @Test
        public void testIsEqualRspecStitchedManifestB() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.stitchedManifestB, TestRspecsLibrary.stitchedManifestB);
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());
    }

    @Test
    public void testIsEqualDifferentOrder() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(TestRspecsLibrary.differentOrderA, TestRspecsLibrary.differentOrderB);
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());
    }


    private static String rspecElementStart = "<rspec type=\"request\" " +
            "xsi:schemaLocation=\"http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/request.xsd \" " +
            "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
            "xmlns=\"http://www.geni.net/resources/rspec/3\">\n";

    @Test
    public void testIsEqualRspec10() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a>test1</a>\n"+
                        "<b>test2</b>\n"+
                        "<c>test3</c>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<b>test2</b>\n"+
                        "<c>test3</c>\n"+
                        "<a>test1</a>\n"+
                        "</rspec>");
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec11() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<c bar=\"baz\"></c>\n"+
                        "<a>test1</a>\n"+
                        "<c foo=\"bar\"></c>\n"+
                        "<b><d/></b>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<b><d/></b>\n"+
                        "<c foo=\"bar\"></c>\n"+
                        "<c bar=\"baz\"></c>\n"+
                        "<a>test1</a>\n"+
                        "</rspec>");
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec12() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a><d/></a>\n"+
                        "<a><e/></a>\n"+
                        "<c>test3</c>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<c>test3</c>\n"+
                        "<a><e/></a>\n"+
                        "<a><d/></a>\n"+
                        "</rspec>");
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec13() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a><e foo=\"bar\"/></a>\n"+
                        "<c>test3</c>\n"+
                        "<a><e bar=\"baz\"/></a>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<c>test3</c>\n"+
                        "<a><e bar=\"baz\"/></a>\n"+
                        "<a><e foo=\"bar\"/></a>\n"+
                        "</rspec>");
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }

    @Test
    public void testIsEqualRspec20() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a>test1</a>\n"+
                        "<b>test2</b>\n"+
                        "<c>test3</c>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<b>test2</b>\n"+
                        "<c>test3</c>\n"+
                        "<a>test1</a>\n"+
                        "<a>test1</a>\n"+
                        "</rspec>");
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec20b() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a>test1</a>\n"+
                        "<a>test1</a>\n"+
                        "<b>test2</b>\n"+
                        "<c>test3</c>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<b>test2</b>\n"+
                        "<c>test3</c>\n"+
                        "<a>test1</a>\n"+
                        "</rspec>");
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec21() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<c bar=\"baz\"></c>\n"+
                        "<a>test1</a>\n"+
                        "<c foo=\"bar\"></c>\n"+
                        "<b><d/></b>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<b><d/></b>\n"+
                        "<c foo=\"baz\"></c>\n"+
                        "<c bar=\"baz\"></c>\n"+
                        "<a>test1</a>\n"+
                        "</rspec>");
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec22() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a><d/></a>\n"+
                        "<a><e/></a>\n"+
                        "<c>test3</c>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<c>test3</c>\n"+
                        "<a><e/><d/></a>\n"+
                        "<a><d/></a>\n"+
                        "</rspec>");
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec22b() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a><d/></a>\n"+
                        "<a><e/></a>\n"+
                        "<c>test3</c>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<c>test3</c>\n"+
                        "<a><e/><e/></a>\n"+
                        "<a><d/></a>\n"+
                        "</rspec>");
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
    @Test
    public void testIsEqualRspec23() throws XMLStreamException, RspecParseException {
        RspecCompare compare = new RspecCompare(
                rspecElementStart+
                        "<a><e foo=\"bar\"/></a>\n"+
                        "<c>test3</c>\n"+
                        "<a><e bar=\"baz\"/></a>\n"+
                        "</rspec>",
                rspecElementStart+
                        "<c>test3</c>\n"+
                        "<a><e bar=\"baz\"/></a>\n"+
                        "<a foo=\"bar\"><e foo=\"bar\"/></a>\n"+
                        "</rspec>");
        Assert.assertFalse(compare.isEqual(), compare.getDifferences().toString());  //exactly same
    }
}
