package com.tlswe.awsmock.ec2.util;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import com.tlswe.awsmock.common.exception.AwsMockException;
import com.tlswe.awsmock.common.util.Constants;
import com.tlswe.awsmock.common.util.PropertiesUtils;
import com.tlswe.awsmock.ec2.control.MockEC2QueryHandler;
import com.tlswe.awsmock.ec2.cxf_generated.RunInstancesResponseType;
import com.tlswe.awsmock.ec2.model.AbstractMockEc2Instance.InstanceType;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ MockEC2QueryHandler.class, PropertiesUtils.class })
public class JAXBUtilTest {

    @Test
    public void Test_marshall() throws Exception {

        String imageID = "ami-1";
        String instanceType = InstanceType.C1_MEDIUM.getName();
        int minCount = 1;
        int maxCount = 1;

        MockEC2QueryHandler handler = MockEC2QueryHandler.getInstance();
        RunInstancesResponseType runInstancesResponseType = Whitebox.invokeMethod(handler,
                "runInstances", imageID,
                instanceType, minCount, maxCount);

        String xml = JAXBUtil.marshall(runInstancesResponseType, "RunInstancesResponse",
                "2012-02-10");

        Assert.assertTrue(xml != null && !xml.isEmpty());
        Assert.assertTrue(xml.contains("<imageId>ami-1</imageId>"));
        Assert.assertTrue(xml.contains("<instanceType>c1.medium</instanceType>"));

    }

    @Test(expected = AwsMockException.class)
    public void Test_marshallFailed() throws Exception {

        // A class not made to be marshaled
        class Person {

            String name;
        }

        JAXBUtil.marshall(new Person(), "Person", "2012-02-10");
    }

    @Test
    public void Test_mashallElasticFoxTrueVersionNull() throws Exception {

        String imageID = "ami-1";
        String instanceType = InstanceType.C1_MEDIUM.getName();
        int minCount = 1;
        int maxCount = 1;

        MockEC2QueryHandler handler = MockEC2QueryHandler.getInstance();
        RunInstancesResponseType runInstancesResponseType = Whitebox.invokeMethod(handler,
                "runInstances", imageID,
                instanceType, minCount, maxCount);

        String xml = JAXBUtil.marshall(runInstancesResponseType, "RunInstancesResponse", null);

        Assert.assertTrue(xml != null && !xml.isEmpty());
        Assert.assertTrue(xml.contains("<imageId>ami-1</imageId>"));
        Assert.assertTrue(xml.contains("<instanceType>c1.medium</instanceType>"));
    }

    @Test
    public void Test_mashallNotElasticFox() throws Exception {

        PowerMockito.spy(PropertiesUtils.class);
        Mockito.when(PropertiesUtils.getProperty(Constants.PROP_NAME_ELASTICFOX_COMPATIBLE))
                .thenReturn("false");

        String imageID = "ami-1";
        String instanceType = InstanceType.C1_MEDIUM.getName();
        int minCount = 1;
        int maxCount = 1;

        MockEC2QueryHandler handler = MockEC2QueryHandler.getInstance();
        RunInstancesResponseType runInstancesResponseType = Whitebox.invokeMethod(handler,
                "runInstances", imageID,
                instanceType, minCount, maxCount);

        String xml = JAXBUtil.marshall(runInstancesResponseType, "RunInstancesResponse", null);

        Assert.assertTrue(xml != null && !xml.isEmpty());
        Assert.assertTrue(xml.contains("<imageId>ami-1</imageId>"));
        Assert.assertTrue(xml.contains("<instanceType>c1.medium</instanceType>"));
    }

    @Test
    public void Test_mashallReplaceVersionWithElasticFoxVersion() throws Exception {

        String imageID = "ami-1";
        String instanceType = InstanceType.C1_MEDIUM.getName();
        int minCount = 1;
        int maxCount = 1;

        MockEC2QueryHandler handler = MockEC2QueryHandler.getInstance();
        RunInstancesResponseType runInstancesResponseType = Whitebox.invokeMethod(handler,
                "runInstances", imageID,
                instanceType, minCount, maxCount);

        String xml = JAXBUtil.marshall(runInstancesResponseType, "RunInstancesResponse",
                PropertiesUtils
                        .getProperty(Constants.PROP_NAME_EC2_API_VERSION_ELASTICFOX));

        Assert.assertTrue(xml != null && !xml.isEmpty());
        Assert.assertTrue(xml.contains("xmlns:ns3=\"http://ec2.amazonaws.com/doc/" + PropertiesUtils
                .getProperty(Constants.PROP_NAME_EC2_API_VERSION_ELASTICFOX)));
        Assert.assertTrue(xml.contains("<imageId>ami-1</imageId>"));
        Assert.assertTrue(xml.contains("<instanceType>c1.medium</instanceType>"));
    }

}

@@ -30,16 +30,378 @@
 .util;%0A%0A
+import com.tlswe.awsmock.common.exception.AwsMockException;%0Aimport com.tlswe.awsmock.common.util.Constants;%0Aimport com.tlswe.awsmock.common.util.PropertiesUtils;%0Aimport com.tlswe.awsmock.ec2.control.MockEC2QueryHandler;%0Aimport com.tlswe.awsmock.ec2.cxf_generated.RunInstancesResponseType;%0Aimport com.tlswe.awsmock.ec2.model.AbstractMockEc2Instance.InstanceType;%0A
 import o
@@ -540,32 +540,99 @@
 o.PowerMockito;%0A
+import org.powermock.core.classloader.annotations.PowerMockIgnore;%0A
 import org.power
@@ -778,469 +778,285 @@
 x;%0A%0A
-import com.tlswe.awsmock.common.exception.AwsMockException;%0Aimport com.tlswe.awsmock.common.util.Constants;%0Aimport com.tlswe.awsmock.common.util.PropertiesUtils;%0Aimport com.tlswe.awsmock.ec2.control.MockEC2QueryHandler;%0Aimport com.tlswe.awsmock.ec2.cxf_generated.RunInstancesResponseType;%0Aimport com.tlswe.awsmock.ec2.model.AbstractMockEc2Instance.InstanceType;%0A%0A@RunWith(PowerMockRunner.class)%0A@PrepareForTest(%7B MockEC2QueryHandler.class, PropertiesUtils.class
+@RunWith(PowerMockRunner.class)%0A@PrepareForTest(%7B MockEC2QueryHandler.class, PropertiesUtils.class %7D)%0A@PowerMockIgnore(%7B %22javax.management.*%22, %22com.sun.org.apache.xerces.*%22, %22javax.xml.*%22,%0A        %22org.xml.*%22, %22org.w3c.dom.*%22, %22com.sun.org.apache.xalan.*%22, %22javax.activation.*%22
  %7D)%0A

Use PowerMock Ignore