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

import be.iminds.ilabt.jfed.rspec.model.controller.ExperimentCommand;
import be.iminds.ilabt.jfed.rspec.model.controller.Timestamp;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXModelRspec;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecFactory;
import be.iminds.ilabt.jfed.rspec.model.javafx_impl.FXRspecNode;
import be.iminds.ilabt.jfed.rspec.parser.TestRspecReconstructable;
import be.iminds.ilabt.jfed.rspec.parser.TestRspecsLibrary;
import be.iminds.ilabt.jfed.rspec.rspec_source.RequestRspecSource;
import be.iminds.ilabt.jfed.util.JavaFXTest;
import org.testng.annotations.Test;

import static org.testng.Assert.*;

/**
 * TestRspecParseFX
 *
 * Test if rspecs are parsed, and if the model is what is expected when these are parsed.
 */
public class TestRspecParseFX extends JavaFXTest {
    private static RspecFactory rspecFactory = FXRspecFactory.getInstance();

    @Test
    public void testRequestWithCommandsAndBarriersStaxErrorFormat() throws Throwable {
        String rspecString = TestRspecsLibrary.requestWithCommandsAndBarriersStaxErrorFormat;
        TestRspecReconstructable.testXmlStaxReconstructable(rspecString, rspecFactory);

        RequestRspecSource source = new RequestRspecSource(rspecString, ModelRspecType.FX);
        FXModelRspec fxModelRspec = (FXModelRspec) source.getModelRspec(ModelRspecType.FX);
        assertFalse(source.isLosingData(ModelRspecType.FX), "losing date. Diffs: "+source.getLosingDataDifferences(ModelRspecType.FX));

        String n1UniqueId = "node0@urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm";
        FXRspecNode n1 = fxModelRspec.getNodeByUniqueId(n1UniqueId);
        assertNotNull(n1);
        assertEquals(n1.getCommands().size(), 3);
        ExperimentCommand c1 = n1.getCommands().get(0);
        assertNotNull(c1);
        assertEquals(c1.getBarrierSegmentOrderNumber(), 0);
        assertEquals(c1.getCommand(), "echo test1");
        assertEquals(c1.getNodeUniqueID(), n1UniqueId);
        assertEquals(c1.getTag(), "Command 1");
        assertEquals(c1.getDuration(), new Timestamp(1000.0));
        assertEquals(c1.getStartingTime(), new Timestamp(21000.0));
    }

    @Test
    public void testRequestWithCommandsAndBarriersPreStaxFormat() throws Throwable {
        String rspecString = TestRspecsLibrary.requestWithCommandsAndBarriersPreStaxFormat;
        TestRspecReconstructable.testXmlStaxReconstructable(rspecString, rspecFactory);

        RequestRspecSource source = new RequestRspecSource(rspecString, ModelRspecType.FX);
        FXModelRspec fxModelRspec = (FXModelRspec) source.getModelRspec(ModelRspecType.FX);
        assertFalse(source.isLosingData(ModelRspecType.FX));

        String n1UniqueId = "node0@urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm";
        FXRspecNode n1 = fxModelRspec.getNodeByUniqueId(n1UniqueId);
        assertNotNull(n1);
        assertEquals(n1.getCommands().size(), 1);
        ExperimentCommand c1 = n1.getCommands().get(0);
        assertNotNull(c1);
        assertEquals(c1.getBarrierSegmentOrderNumber(), 0);
        assertEquals(c1.getCommand(), "sudo echo test");
        assertEquals(c1.getNodeUniqueID(), n1UniqueId);
        assertEquals(c1.getTag(), "server installation");
        assertEquals(c1.getDuration(), new Timestamp(37316.0));
        assertEquals(c1.getStartingTime(), new Timestamp(1.0));
    }
}
