package com.otognan.driverpete.logic;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;

import org.hamcrest.collection.IsIterableContainingInOrder;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={AWSConfiguration.class})
public class TrajectoryReaderTest {
    
    @Autowired
    AWSCredentials awsCredentials;

//    @Configuration
//    static class TestPropertiesConfiguration {
//        @Bean
//        PropertyPlaceholderConfigurer propConfig() {
//            PropertyPlaceholderConfigurer ppc =  new PropertyPlaceholderConfigurer();
//            ppc.setLocation(new ClassPathResource("security.properties"));
//            ppc.setIgnoreResourceNotFound(true);
//            return ppc;
//        }
//    }

    @Test
    public void testTrajectoryReader() throws IOException, ParseException {
        
        AmazonS3 s3client = new AmazonS3Client(awsCredentials);
        S3Object object = s3client.getObject(
                new GetObjectRequest("driverpete-storage", "_testing/testing_merged_0"));
        InputStream objectData = object.getObjectContent();
        
        List<Location> locations = TrajectoryReader.readTrajectory(objectData);
        
        //Process the objectData stream.
        objectData.close();
        
        assertEquals(2423, locations.size());
    }
    
    @Test
    public void testTrajectoryWriter() throws Exception {
        
        AmazonS3 s3client = new AmazonS3Client(awsCredentials);
        S3Object object = s3client.getObject(
                new GetObjectRequest("driverpete-storage", "_testing/testing_raw_0"));
        InputStream objectData = object.getObjectContent();
        
        List<Location> locations = TrajectoryReader.readTrajectory(objectData);

        //Process the objectData stream.
        objectData.close();
        
        byte[] compressedBytes = TrajectoryReader.writeTrajectory(locations);
        List<Location> locationsCopy = TrajectoryReader.readTrajectory(new ByteArrayInputStream(compressedBytes));

        Assert.assertThat(locationsCopy, 
                IsIterableContainingInOrder.contains(locations.toArray()));
    }
   
}

@@ -1137,16 +1137,81 @@
 on.class
+,%0A        TrajectoryReaderTest.TestPropertiesConfiguration.class%0A
 %7D)%0Apubli
@@ -1297,18 +1297,16 @@
 tials;%0A%0A
-//
     @Con
@@ -1316,18 +1316,16 @@
 uration%0A
-//
     stat
@@ -1363,18 +1363,16 @@
 ation %7B%0A
-//
         
@@ -1377,18 +1377,16 @@
   @Bean%0A
-//
         
@@ -1430,18 +1430,16 @@
 fig() %7B%0A
-//
         
@@ -1516,18 +1516,16 @@
 urer();%0A
-//
         
@@ -1591,18 +1591,16 @@
 ies%22));%0A
-//
         
@@ -1640,18 +1640,16 @@
 (true);%0A
-//
         
@@ -1664,18 +1664,16 @@
 rn ppc;%0A
-//
         
@@ -1674,18 +1674,16 @@
       %7D%0A
-//
     %7D%0A%0A 

add context configuration back