package org.dasein.cloud.aws;

import org.dasein.cloud.CloudException;
import org.junit.Test;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import static org.junit.Assert.assertEquals;

/**
 * @author Eugene Yaroslavtsev
 * @since 11.09.2014
 */
public class AWSCloudTests {

    @Test
    public void testGetTimestampValue() throws ParserConfigurationException, IOException, SAXException, CloudException {
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        Element node =  documentBuilder
                .parse(new ByteArrayInputStream("<node>2014-07-01T15:08:28Z</node>".getBytes()))
                .getDocumentElement();

        Element nodeWithMs =  documentBuilder
                .parse(new ByteArrayInputStream("<node>2014-07-01T15:08:28.001Z</node>".getBytes()))
                .getDocumentElement();

        Element brokenNode =  documentBuilder
                .parse(new ByteArrayInputStream("<node>2014-07-01T15:08:28.001.0Z</node>".getBytes()))
                .getDocumentElement();

        assertEquals(1404227308000L, AWSCloud.getTimestampValue(node));
        assertEquals(1404227308001L, AWSCloud.getTimestampValue(nodeWithMs));

        try {
            AWSCloud.getTimestampValue(brokenNode);
            throw new AssertionError();
        } catch( CloudException ex ) {
            //expected exception
        }
    }
}

@@ -130,28 +130,20 @@
 org.
-xml.sax.SAXException
+w3c.dom.Node
 ;%0A%0Ai
@@ -327,36 +327,8 @@
 eam;
-%0Aimport java.io.IOException;
 %0A%0Aim
@@ -464,241 +464,347 @@
 s %7B%0A
-%0A    @Test%0A    public void 
+    priva
 te
+ 
 st
-GetTimestampValue() throws ParserConfigurationException, IOException, SAXException, CloudException %7B%0A        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();%0A
+atic DocumentBuilder documentBuilder;%0A%0A    static %7B%0A        try %7B%0A            documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();%0A        %7D catch( ParserConfigurationException e ) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testGetTimestampValue() throws Exception %7B
 %0A   
@@ -1176,236 +1176,628 @@
     
-Element brokenNode =  documentBuilder%0A                .parse(new ByteArrayInputStream(%22%3Cnode%3E2014-07-01T15:08:28.001.0Z%3C/node%3E%22.getBytes()))%0A                .getDocumentElement();%0A%0A        assertEquals(1404227308000L, AWSCloud.g
+assertEquals(1404227308000L, AWSCloud.getTimestampValue(node));%0A        assertEquals(1404227308001L, AWSCloud.getTimestampValue(nodeWithMs));%0A    %7D%0A%0A    @Test%0A    public void negativeTestGetTimestampValue() throws Exception %7B%0A        Element brokenNode =  documentBuilder%0A                .parse(new ByteArrayInputStream(%22%3Cnode%3E2014-07-01T15:08:28.001.0Z%3C/node%3E%22.getBytes()))%0A                .getDocumentElement();%0A%0A        Element secondBrokenNode =  documentBuilder%0A                .parse(new ByteArrayInputStream(%22%3Cnode%3E2014-07-01T%3C/node%3E%22.getBytes()))%0A                .getDocumentElement();%0A%0A        waitExceptionForG
 etTi
@@ -1801,38 +1801,43 @@
 tTimestampValue(
-n
+brokenN
 ode)
-)
 ;%0A        assert
@@ -1834,47 +1834,109 @@
     
-assertEquals(1404227308001L, AWSCloud.g
+waitExceptionForGetTimestampValue(secondBrokenNode);%0A    %7D%0A%0A    private static void waitExceptionForG
 etTi
@@ -1948,30 +1948,30 @@
 mpValue(
-nodeWithMs));%0A
+ Node node ) %7B
 %0A       
@@ -2008,39 +2008,33 @@
 tTimestampValue(
-brokenN
+n
 ode);%0A          

add negative test and separate negative tests (#1119)