package gov.nih.nci.integration.caaers;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import gov.nih.nci.integration.caaers.invoker.CaAERSServiceInvocationStrategyFactory;
import gov.nih.nci.integration.domain.IHubMessage;
import gov.nih.nci.integration.domain.ServiceInvocationMessage;
import gov.nih.nci.integration.domain.StrategyIdentifier;
import gov.nih.nci.integration.invoker.ServiceInvocationResult;
import gov.nih.nci.integration.invoker.ServiceInvocationStrategy;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Test class for CaAERSServiceInvocationStrategyFactory
 * 
 * @author Vinodh
 * 
 */
public class CaAERSServiceInvocationStrategyFactoryTest {

    private static final Logger LOG = LoggerFactory.getLogger(CaAERSServiceInvocationStrategyFactoryTest.class);

    @Test
    public void createCaAERSServiceInvocationStrategy() {
        final File customLibLoc = new File("..\\caaers-client\\build\\caaers-client-lib\\");
        final File distLoc = new File("..\\caaers-client\\build\\dist\\");
        final ServiceInvocationStrategy sris = CaAERSServiceInvocationStrategyFactory
                .createCaAERSRegistrationServiceInvocationStrategy(new String[] { customLibLoc.getAbsolutePath(),
                        distLoc.getAbsolutePath() }, "classpath*:applicationContext-caaers-client-test.xml");
        final ServiceInvocationStrategy suris = CaAERSServiceInvocationStrategyFactory
                .createCaAERSUpdateRegistrationServiceInvocationStrategy(new String[] { customLibLoc.getAbsolutePath(),
                        distLoc.getAbsolutePath() }, "classpath*:applicationContext-caaers-client-test.xml");

        assertNotNull(sris);
        assertNotNull(suris);

        final ServiceInvocationMessage msg = new ServiceInvocationMessage();
        msg.setStrategyIdentifier(StrategyIdentifier.CAEERS_CREATE_REGISTRATION);
        final IHubMessage iHubMessage = new IHubMessage();
        iHubMessage.setRequest(getPStr());
        final Date stTime = new Date(new java.util.Date().getTime());
        iHubMessage.setStartTime(stTime);
        msg.setMessage(iHubMessage);

        final ServiceInvocationResult res = sris.invoke(msg);
        assertNotNull(res);
        assertTrue(res.isFault());
        assertNotNull(res.getInvocationException());
    }

    /**
     * Testcase for createCaAERSAdverseEventServiceInvocationStrategy
     */
    @Test
    public void createCaAERSAdverseEventServiceInvocationStrategy() {
        final File customLibLoc = new File("..\\caaers-client\\build\\caaers-client-lib\\");
        final File distLoc = new File("..\\caaers-client\\build\\dist\\");
        final ServiceInvocationStrategy saeis = CaAERSServiceInvocationStrategyFactory
                .createCaAERSAdverseEventServiceInvocationStrategy(new String[] { customLibLoc.getAbsolutePath(),
                        distLoc.getAbsolutePath() }, "classpath*:applicationContext-caaers-client-test.xml");
        assertNotNull(saeis);
    }

    /**
     * Testcase for createCaAERSAdverseEventServiceInvocationStrategy
     */
    @Test
    public void createCaAERSAdverseEventServiceInvocationStrategyFailure() {
        final File customLibLoc = new File("..\\caaers-client\\caaers-client-lib\\");// This Path doesn't exist
        final File distLoc = new File("..\\caaers-client\\build\\dist\\");
        CaAERSServiceInvocationStrategyFactory.setInitStatus(null);
        final ServiceInvocationStrategy saeis = CaAERSServiceInvocationStrategyFactory
                .createCaAERSAdverseEventServiceInvocationStrategy(new String[] { customLibLoc.getAbsolutePath(),
                        distLoc.getAbsolutePath() }, "classpath*:applicationContext-caaers-client-test.xml");
        assertNull(saeis);
    }

    private String getPStr() {
        return getXMLString("Registration_MBC.xml");
    }

    private String getXMLString(String fileName) {
        String contents = null;
        final InputStream is = CaAERSServiceInvocationStrategyFactoryTest.class.getClassLoader().getResourceAsStream(
                "payloads/" + fileName);
        try {
            contents = org.apache.cxf.helpers.IOUtils.toString(is);
        } catch (IOException e) {
            LOG.error("Error while reading contents of file : " + fileName + ". " + e);
        }
        return contents;
    }
}

@@ -1140,34 +1140,33 @@
 c = new File(%22..
-%5C%5C
+/
 caaers-client%5C%5Cb
@@ -1158,33 +1158,25 @@
 aaers-client
-%5C%5Cbuild%5C%5C
+/
 caaers-clien
@@ -1172,34 +1172,33 @@
 aaers-client-lib
-%5C%5C
+/
 %22);%0D%0A        fin
@@ -1219,34 +1219,33 @@
 c = new File(%22..
-%5C%5C
+/
 caaers-client%5C%5Cb
@@ -1237,39 +1237,36 @@
 aaers-client
-%5C%5C
+/
 build
-%5C%5C
+/
 dist
-%5C%5C
+/
 %22);%0D%0A       
@@ -2775,34 +2775,33 @@
 c = new File(%22..
-%5C%5C
+/
 caaers-client%5C%5Cb
@@ -2797,25 +2797,17 @@
 s-client
-%5C%5Cbuild%5C%5C
+/
 caaers-c
@@ -2815,18 +2815,17 @@
 ient-lib
-%5C%5C
+/
 %22);%0D%0A   
@@ -2854,34 +2854,33 @@
 c = new File(%22..
-%5C%5C
+/
 caaers-client%5C%5Cb
@@ -2872,39 +2872,36 @@
 aaers-client
-%5C%5C
+/
 build
-%5C%5C
+/
 dist
-%5C%5C
+/
 %22);%0D%0A       
@@ -3463,34 +3463,33 @@
 c = new File(%22..
-%5C%5C
+/
 caaers-client%5C%5Cc
@@ -3485,18 +3485,17 @@
 s-client
-%5C%5C
+/
 caaers-c
@@ -3503,18 +3503,21 @@
 ient-lib
-%5C%5C
+-123/
 %22);// Th
@@ -3580,18 +3580,17 @@
 File(%22..
-%5C%5C
+/
 caaers-c
@@ -3598,23 +3598,20 @@
 ient
-%5C%5C
+/
 build
-%5C%5C
+/
 dist
-%5C%5C
+/
 %22);%0D

Changing the "\\" to "/"