package be.iminds.jfed.gts_highlevel.model;

import be.iminds.jfed.gts_highlevel.model.helpers.GtsResources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.xml.bind.*;
import java.io.StringReader;

/**
 * User: twalcari
 * Date: 9/18/2015
 * Time: 17:57
 */
public class GtsXmlResourceTest {
    private static final Logger LOG = LoggerFactory.getLogger(GtsXmlResourceTest.class);

    @Test
    public void unmarshalGtsResource() throws JAXBException {
        final String input = "<composite>\n" +
                "    <capabilities>\n" +
                "        <entry>\n" +
                "            <key>id</key>\n" +
                "            <value>248103</value>\n" +
                "        </entry>\n" +
                "        <entry>\n" +
                "            <key>ports</key>\n" +
                "            <value>{}</value>\n" +
                "        </entry>\n" +
                "        <entry>\n" +
                "            <key>status</key>\n" +
                "            <value>ACTIVE</value>\n" +
                "        </entry>\n" +
                "        <entry>\n" +
                "            <key>providerId</key>\n" +
                "            <value>66</value>\n" +
                "        </entry>\n" +
                "        <entry>\n" +
                "            <key>type</key>\n" +
                "            <value>Testbed</value>\n" +
                "        </entry>\n" +
                "    </capabilities>\n" +
                "    <failOnActivate>false</failOnActivate>\n" +
                "    <failOnDeactivate>false</failOnDeactivate>\n" +
                "    <id>248103</id>\n" +
                "    <ports/>\n" +
                "    <providerId>66</providerId>\n" +
                "    <reservations>\n" +
                "        <children>\n" +
                "            <id>58</id>\n" +
                "            <endTime>17548354799000</endTime>\n" +
                "            <resourceProviderId>Host-1442504304395</resourceProviderId>\n" +
                "            <status>ACTIVE</status>\n" +
                "            <startTime>1442504280000</startTime>\n" +
                "        </children>\n" +
                "        <id>17</id>\n" +
                "        <endTime>17548354799000</endTime>\n" +
                "        <resourceProviderId>66</resourceProviderId>\n" +
                "        <status>ACTIVE</status>\n" +
                "        <startTime>1442504280000</startTime>\n" +
                "    </reservations>\n" +
                "    <status>ACTIVE</status>\n" +
                "    <type>Testbed</type>\n" +
                "    <name>JFEDTEST</name>\n" +
                "</composite>";

        JAXBContext context = JAXBContext.newInstance(GtsXmlResource.class, GtsXmlComposite.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        GtsXmlResource reservation = (GtsXmlResource) unmarshaller.unmarshal(new StringReader(input));

        Assert.assertEquals(GtsXmlReservation.Status.ACTIVE, reservation.getStatus());



    }

    @Test
    public void unmarshalResources() throws JAXBException {
        String input = "<resourceBases>\n" +
                "    <resourceBase>\n" +
                "        <capabilities>\n" +
                "            <entry>\n" +
                "                <key>id</key>\n" +
                "                <value>host0</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>ports</key>\n" +
                "                <value>{}</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>cpuCores</key>\n" +
                "                <value>-1</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>status</key>\n" +
                "                <value>ACTIVATING</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>location</key>\n" +
                "                <value>MIL</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>portNum</key>\n" +
                "                <value>0</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>providerId</key>\n" +
                "                <value>Host-1453821772502</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>type</key>\n" +
                "                <value>Host</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>memory</key>\n" +
                "                <value>-1</value>\n" +
                "            </entry>\n" +
                "        </capabilities>\n" +
                "        <failOnActivate>false</failOnActivate>\n" +
                "        <failOnDeactivate>false</failOnDeactivate>\n" +
                "        <id>host0</id>\n" +
                "        <ports/>\n" +
                "        <providerId>Host-1453821772502</providerId>\n" +
                "        <reservations>\n" +
                "            <id>3336</id>\n" +
                "            <endTime>17548358399000</endTime>\n" +
                "            <resourceProviderId>Host-1453821772502</resourceProviderId>\n" +
                "            <status>ACTIVATING</status>\n" +
                "            <startTime>1453821720000</startTime>\n" +
                "        </reservations>\n" +
                "        <status>ACTIVATING</status>\n" +
                "        <type>Host</type>\n" +
                "    </resourceBase>\n" +
                "    <composite>\n" +
                "        <capabilities>\n" +
                "            <entry>\n" +
                "                <key>id</key>\n" +
                "                <value>150527637</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>ports</key>\n" +
                "                <value>{}</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>providerId</key>\n" +
                "                <value>3267</value>\n" +
                "            </entry>\n" +
                "            <entry>\n" +
                "                <key>type</key>\n" +
                "                <value>Testbed</value>\n" +
                "            </entry>\n" +
                "        </capabilities>\n" +
                "        <failOnActivate>false</failOnActivate>\n" +
                "        <failOnDeactivate>false</failOnDeactivate>\n" +
                "        <id>150527637</id>\n" +
                "        <ports/>\n" +
                "        <providerId>3267</providerId>\n" +
                "        <reservations>\n" +
                "            <children>\n" +
                "                <id>3336</id>\n" +
                "                <endTime>17548358399000</endTime>\n" +
                "                <resourceProviderId>Host-1453821772502</resourceProviderId>\n" +
                "                <status>ACTIVATING</status>\n" +
                "                <startTime>1453821720000</startTime>\n" +
                "            </children>\n" +
                "            <id>3267</id>\n" +
                "            <endTime>17548358399000</endTime>\n" +
                "            <resourceProviderId>3267</resourceProviderId>\n" +
                "            <status>ACTIVATING</status>\n" +
                "            <startTime>1453821720000</startTime>\n" +
                "        </reservations>\n" +
                "        <status>ACTIVATING</status>\n" +
                "        <type>Testbed</type>\n" +
                "        <name>UntitledGts</name>\n" +
                "    </composite>\n" +
                "</resourceBases>\n";

        JAXBContext context = JAXBContext.newInstance(GtsXmlResource.class, GtsXmlComposite.class, GtsResources.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();


        unmarshaller.setEventHandler(new ValidationEventHandler() {
            @Override
            public boolean handleEvent(ValidationEvent event) {
                if (event.getSeverity() == ValidationEvent.FATAL_ERROR) {
                    LOG.error("FATAL GTS-JAXB[{}]: {}", event.getLocator(), event.getMessage());
                    return false;
                }
                if (event.getSeverity() == ValidationEvent.ERROR)
                    LOG.error("GTS-JAXB[{}]: {}", event.getLocator(), event.getMessage());
                if (event.getSeverity() == ValidationEvent.WARNING)
                    LOG.warn("GTS-JAXB[{}]: {}", event.getLocator(), event.getMessage());

                return true;
            }
        });

        GtsResources resources = (GtsResources) unmarshaller.unmarshal(new StringReader(input));

        Assert.assertEquals(2, resources.getResources().size());



    }
}
