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

import org.testng.annotations.Test;

import static org.testng.Assert.*;

/**
 * SliverTypeTest
 */
public class SliverTypeTest {
    public SliverType makeSimpleSliverType() {
        return new SliverTypeBuilder("vm").build();
    }
    public SliverType makeComplexSliverType() {
        return new SliverTypeBuilder("vm").addDiskImage("dummyImage").build();
    }
    public SliverType makeXenSliverType() {
        return new SliverTypeBuilder("emulab-xen").xenCores(1).xenDisk(16).xenRam(1024).build();
    }

    @Test
    public void testBuilderSimple() {
        SliverType base = makeSimpleSliverType();
        assertEquals(base.getName(), "vm");
        assertNull(base.getXenCores());
        assertNull(base.getXenRam());
        assertNull(base.getXenDisk());
        assertNotNull(base.getDiskImages());
        assertEquals(base.getDiskImages().size(), 0);
    }
    @Test
    public void testBuilderComplex() {
        SliverType base = makeComplexSliverType();
        assertEquals(base.getName(), "vm");
        assertNull(base.getXenCores());
        assertNull(base.getXenRam());
        assertNull(base.getXenDisk());
        assertNotNull(base.getDiskImages());
        assertEquals(base.getDiskImages().size(), 1);
        assertEquals(base.getDiskImages().get(0).getName(), "dummyImage");
    }
    @Test
    public void testBuilderXen() {
        SliverType base = makeXenSliverType();
        assertEquals(base.getName(), "emulab-xen");
        assertEquals(base.getXenCores(), new Integer(1));
        assertEquals(base.getXenDisk(), new Integer(16));
        assertEquals(base.getXenRam(), new Integer(1024));
        assertNotNull(base.getDiskImages());
        assertEquals(base.getDiskImages().size(), 0);
    }

    @Test
    public void testBuilderModifyNameSimple() {
        SliverType base = makeSimpleSliverType();
        assertEquals(base.getName(), "vm");

        SliverType modified = new SliverTypeBuilder(base).name("pc").build();
        assertEquals(modified.getName(), "pc");
        assertNull(modified.getXenCores());
        assertNull(modified.getXenRam());
        assertNull(modified.getXenDisk());
        assertNotNull(modified.getDiskImages());
        assertEquals(modified.getDiskImages().size(), 0);
    }

    @Test
    public void testBuilderModifyNameComplex() {
        SliverType base = makeComplexSliverType();
        assertEquals(base.getName(), "vm");

        SliverType modified = new SliverTypeBuilder(base).name("pc").build();
        assertEquals(modified.getName(), "pc");
        assertNull(modified.getXenCores());
        assertNull(modified.getXenRam());
        assertNull(modified.getXenDisk());
        assertNotNull(modified.getDiskImages());
        assertEquals(modified.getDiskImages().size(), 1);
        assertEquals(modified.getDiskImages().get(0).getName(), "dummyImage");
    }

    @Test
    public void testBuilderModifyNameXen() {
        SliverType base = makeXenSliverType();
        assertEquals(base.getName(), "emulab-xen");

        SliverType modified = new SliverTypeBuilder(base).name("pc").build();
        assertEquals(modified.getName(), "pc");
        assertEquals(modified.getXenCores(), new Integer(1));
        assertEquals(modified.getXenDisk(), new Integer(16));
        assertEquals(modified.getXenRam(), new Integer(1024));
        assertNotNull(modified.getDiskImages());
        assertEquals(modified.getDiskImages().size(), 0);
    }

    @Test
    public void testBuilderModifyXenSimple() {
        SliverType base = makeSimpleSliverType();

        SliverType modified = new SliverTypeBuilder(base).xenCores(2).xenRam(2048).build();
        assertEquals(modified.getName(), "vm");
        assertEquals(modified.getXenCores(), new Integer(2));
        assertNull(modified.getXenDisk());
        assertEquals(modified.getXenRam(), new Integer(2048));
        assertNotNull(modified.getDiskImages());
        assertEquals(modified.getDiskImages().size(), 0);
    }

    @Test
    public void testBuilderModifyXenComplex() {
        SliverType base = makeComplexSliverType();

        SliverType modified = new SliverTypeBuilder(base).xenCores(2).xenRam(2048).build();
        assertEquals(modified.getName(), "vm");
        assertEquals(modified.getXenCores(), new Integer(2));
        assertNull(modified.getXenDisk());
        assertEquals(modified.getXenRam(), new Integer(2048));
        assertNotNull(modified.getDiskImages());
        assertEquals(modified.getDiskImages().size(), 1);
        assertEquals(modified.getDiskImages().get(0).getName(), "dummyImage");
    }

    @Test
    public void testBuilderModifyXenXen() {
        SliverType base = makeXenSliverType();
        assertEquals(base.getName(), "emulab-xen");

        SliverType modified = new SliverTypeBuilder(base).xenCores(2).xenRam(2048).build();
        assertEquals(modified.getName(), "emulab-xen");
        assertEquals(modified.getXenCores(), new Integer(2));
        assertEquals(modified.getXenDisk(), new Integer(16));
        assertEquals(modified.getXenRam(), new Integer(2048));
        assertNotNull(modified.getDiskImages());
        assertEquals(modified.getDiskImages().size(), 0);

        modified = new SliverTypeBuilder(modified).xenCores(4).xenRam(null).build();
        assertEquals(modified.getName(), "emulab-xen");
        assertEquals(modified.getXenCores(), new Integer(4));
        assertEquals(modified.getXenDisk(), new Integer(16));
        assertNull(modified.getXenRam());
        assertNotNull(modified.getDiskImages());
        assertEquals(modified.getDiskImages().size(), 0);
    }
}
