/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.dnetlib.espas.sos.client;

import eu.dnetlib.espas.sos.client.utils.SOSRequestStatusListenerIF;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.logging.LogManager;
import static org.junit.Assert.*;
import org.junit.Test;

/**
 *
 * @author George Athanasopoulos <george.athanasopoulos at gmail.com, UoA | ARC>
 */
public class SOSProviderHandlerTest {
    
    public SOSProviderHandlerTest() throws IOException {
       LogManager.getLogManager().readConfiguration(ClassLoader.getSystemResourceAsStream("logging.properties"));
    }
    
    /**
     * Test of run method, of class SOSProviderHandler.
     */
//    @Test
    public void testRun() {
        try{
        System.out.println("run");
        String sosEndpoint = "http://194.177.192.229:8080/espas/sos";
        
        List<String> observationIdList = Arrays.asList("http://ontology.espas-fp7.eu/observedProperty/CriticalFrequency_F1-Layer","http://resources.espas-fp7.eu/acquisition/ucl/uclKEOPSobservatory-FPIred/1", "http://resources.espas-fp7.eu/acquisition/ucl/uclKEOPSobservatory-FPIgreen/1", "http://resources.espas-fp7.eu/acquisition/ucl/uclSodankylaobservatory-FPI/1", "http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardAdventdalenobservatory-FPIred/1", "http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardKHOobservatory-FPIred/1", "http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardKHOobservatory-FPIgreen/1");
        List<String> propertyIdList = Arrays.asList("http://ontology.espas-fp7.eu/observedProperty/CriticalFrequency_F1-Layer","http://ontology.espas-fp7.eu/observedProperty/CriticalFrequency_F2-Layer" ,"http://ontology.espas-fp7.eu/observedProperty/MinimumVirtualHeight_F2-Layer","http://ontology.espas-fp7.eu/observedProperty/MinimumFrequency_FirstHop_AllLayers" ,"http://ontology.espas-fp7.eu/observedProperty/NeutralWindVelocity_Horizontal" ,"http://ontology.espas-fp7.eu/observedProperty/PhotonFlux_Optical");
        


        
        List<String> offeringsList = Arrays.asList("http://resources.espas-fp7.eu/compositeProcess/noa/athDig-artist/1","http://resources.espas-fp7.eu/acquisition/ucl/uclKEOPSobservatory-FPIred/1","http://resources.espas-fp7.eu/acquisition/ucl/uclKEOPSobservatory-FPIgreen/1","http://resources.espas-fp7.eu/acquisition/ucl/uclSodankylaobservatory-FPI/1","http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardAdventdalenobservatory-FPIred/1","http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardKHOobservatory-FPIred/1","http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardKHOobservatory-FPIgreen/1");
        Map<String,Map<String,List<String>>> observationOfferingMap = new HashMap();
        for(int i=0;i<observationIdList.size();i++)
            observationOfferingMap.put(observationIdList.get(i), new HashMap<String,List<String>>());
        

        TimePeriodConstraint tCon = new TimePeriodConstraint();
        SimpleDateFormat dtFormater = new SimpleDateFormat("dd-mm-YYYY");
        tCon.setFromDate(dtFormater.parse("01-02-2014"));tCon.setToDate(dtFormater.parse("03-02-2014"));
        
        List<TimePeriodConstraint> timeConstraints = Arrays.asList(tCon);
        SOSRequestInfo reqInfo = new SOSRequestInfo("Req1",observationIdList, propertyIdList, timeConstraints);
        reqInfo.setProviderId("LocalProvider");
        reqInfo.setObservationOfferingMappings(observationOfferingMap);
        
        SOSProviderHandler instance = new SOSProviderHandler(reqInfo, new URL(sosEndpoint),null);
        CountDownLatch providerSyncLatch = new CountDownLatch(1);
        instance.setLatch(providerSyncLatch);
        Thread providerThread = new Thread(instance);
        providerThread.start();
        providerSyncLatch.await();
        // TODO review the generated test code and remove the default call to fail.
        assert true;
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            assert false;
        }
    }


    /**
     * Test of setStatusListener method, of class SOSProviderHandler.
     */
    public void testSetStatusListener() {
        System.out.println("setStatusListener");
        SOSRequestStatusListenerIF statusListener = null;
        SOSProviderHandler instance = null;
        instance.setStatusListener(statusListener);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }

//    Test for interaction with daniel's client
//    @Test
        public void testDHCClient() {
        try{
        System.out.println("DHC Client");
        String sosEndpoint = "http://espas.spaceweatherservices.com/SOSService.php";
        
        List<String> observationIdList = Arrays.asList("http://resources.espas-fp7.eu/observation/iasb/whisper4-data-20110323T123839Z/1",
//                "http://ontology.espas-fp7.eu/observedProperty/CriticalFrequency_F1-Layer",
                "http://resources.espas-fp7.eu/observation/ucl/fpi-g05b29/1" 
//                "http://resources.espas-fp7.eu/acquisition/ucl/uclKEOPSobservatory-FPIgreen/1", 
//                "http://resources.espas-fp7.eu/acquisition/ucl/uclSodankylaobservatory-FPI/1", 
//                "http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardAdventdalenobservatory-FPIred/1", 
//                "http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardKHOobservatory-FPIred/1", 
//                "http://resources.espas-fp7.eu/acquisition/ucl/uclSvalbardKHOobservatory-FPIgreen/1"
        );
        
        List<String> propertyIdList = Arrays.asList("http://ontology.espas-fp7.eu/observedProperty/ElectronDensity",
//                "http://ontology.espas-fp7.eu/observedProperty/CriticalFrequency_F1-Layer",
//                "http://ontology.espas-fp7.eu/observedProperty/CriticalFrequency_F2-Layer" ,
//                "http://ontology.espas-fp7.eu/observedProperty/MinimumVirtualHeight_F2-Layer",
//                "http://ontology.espas-fp7.eu/observedProperty/MinimumFrequency_FirstHop_AllLayers" ,
                "http://ontology.espas-fp7.eu/observedProperty/NeutralWindVelocity_Horizontal" ,
                "http://ontology.espas-fp7.eu/observedProperty/PhotonFlux_Optical");
        


        
        List<String> offeringsList = Arrays.asList(
                "http://resources.espas-fp7.eu/acquisition/iasb/whisper4/1",
                "http://resources.espas-fp7.eu/acquisition/ucl/Keops_red630/1");
        Map<String,Map<String,List<String>>> observationOfferingMap = new HashMap();
        for(int i=0;i<observationIdList.size();i++)
            observationOfferingMap.put(observationIdList.get(i), new HashMap<String,List<String>>()); //Arrays.asList(offeringsList.get(i)));
        

        TimePeriodConstraint tCon = new TimePeriodConstraint();
        SimpleDateFormat dtFormater = new SimpleDateFormat("dd-mm-YYYY");
        tCon.setFromDate(dtFormater.parse("01-02-2006"));
        tCon.setToDate(dtFormater.parse("03-02-2006"));
        
        List<TimePeriodConstraint> timeConstraints = Arrays.asList(tCon);
        SOSRequestInfo reqInfo = new SOSRequestInfo("Req1",observationIdList, propertyIdList, timeConstraints);
        reqInfo.setProviderId("LocalProvider");
        reqInfo.setObservationOfferingMappings(observationOfferingMap);
        
        SOSProviderHandler instance = new SOSProviderHandler(reqInfo, new URL(sosEndpoint),null);
        CountDownLatch providerSyncLatch = new CountDownLatch(1);
        instance.setLatch(providerSyncLatch);
        Thread providerThread = new Thread(instance);
        providerThread.start();
        providerSyncLatch.await();
        // TODO review the generated test code and remove the default call to fail.
        assert true;
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            assert false;
        }
    }

}
