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

import be.iminds.ilabt.jfed.rspec.model.ModelRspec;
import be.iminds.ilabt.jfed.rspec.model.RspecFactory;
import be.iminds.ilabt.jfed.rspec.model.StringRspec;
import be.iminds.ilabt.jfed.rspec.model.impl.BasicRspecFactory;
import be.iminds.ilabt.jfed.rspec.parser.RspecCompare;
import be.iminds.ilabt.jfed.rspec.parser.TestRspecsLibrary;
import be.iminds.ilabt.jfed.rspec.rspec_source.ManifestRspecSource;
import be.iminds.ilabt.jfed.rspec.rspec_source.RequestRspecSource;
import org.testng.Assert;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;


public class RspecConverterTest {
    protected final RspecFactory rspecFactory;
    protected RspecConverterTest(RspecFactory rspecFactory) {
        this.rspecFactory = rspecFactory;
    }
    public RspecConverterTest() {
        this(BasicRspecFactory.getInstance());
    }

    public void testConvertManifestToRequestHelper(String manifestString, String expectedRequestString, boolean keepNodeBindings) throws Throwable {
        ModelRspec manifest =  new ManifestRspecSource(manifestString, rspecFactory.getModelRspecType()).getModelRspec(rspecFactory.getModelRspecType());
        ModelRspec request = new RspecConverter().convertManifestToRequest(manifest, keepNodeBindings, rspecFactory);

        ModelRspec expectedRequest = new RequestRspecSource(expectedRequestString, rspecFactory.getModelRspecType()).getModelRspec(rspecFactory.getModelRspecType());

        assertThat(request.getModelRspecType(), equalTo(rspecFactory.getModelRspecType()));
        assertThat(manifest.getModelRspecType(), equalTo(rspecFactory.getModelRspecType()));
        assertThat(expectedRequest.getModelRspecType(), equalTo(rspecFactory.getModelRspecType()));

        String convertedRequestString = request.toGeni3Rspec();
        //System.out.println("Converted Request:"+convertedRequestString);

        RspecCompare compare = new RspecCompare(convertedRequestString, expectedRequestString, "Converted Request", "Expected Request");
        Assert.assertTrue(compare.isEqual(), compare.getDifferences().toString());

        assertThat(StringRspec.isSimilarXml(convertedRequestString, expectedRequestString), is(true)); //extra attribute in first node
    }

    @Test
    public void testConvertManifestToRequestWithNodeBinding() throws Throwable {
        testConvertManifestToRequestHelper(TestRspecsLibrary.stitchedManifestB, TestRspecsLibrary.stitchedManifestBToRequestWithCompId, true);
    }
    @Test
    public void testConvertManifestToRequestNoBodeBinding() throws Throwable {
        testConvertManifestToRequestHelper(TestRspecsLibrary.stitchedManifestB, TestRspecsLibrary.stitchedManifestBToRequestNoCompiId, false);
    }
}