/**
 * MuleSoft Examples
 * Copyright 2014 MuleSoft, Inc.
 *
 * This product includes software developed at
 * MuleSoft, Inc. (http://www.mulesoft.com/).
 */

package org.mule.examples;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.mule.api.MuleMessage;
import org.mule.module.client.MuleClient;
import org.mule.tck.junit4.FunctionalTestCase;
import org.mule.tck.junit4.rule.DynamicPort;

public class UploadToFtpAfterConvertingJsonToXmlIT extends FunctionalTestCase
{
	private static final String PATH_TO_TEST_PROPERTIES = "./src/test/resources/mule.test.properties";
	private static final Logger log = LogManager.getLogger(UploadToFtpAfterConvertingJsonToXmlIT.class); 
	
	private static String FTP_PASSWORD;
	private static String FTP_USER;
	private static String FTP_HOST;
	private static String FTP_PORT;
	private static String FTP_HOME;
	private static String FTP_PATH;
	
	private static String MESSAGE = "";
	private static String REPLY = "";
	
	@Rule
	public DynamicPort port = new DynamicPort("http.port");
	
    @Override
    protected String getConfigResources()
    {
        return "upload-to-ftp.xml";
    }

    @BeforeClass
    public static void init() throws IOException{
    	final Properties props = new Properties();
    	try {
    	props.load(new FileInputStream(PATH_TO_TEST_PROPERTIES));
    	} catch (Exception e) {
    		log.error("Error occured while reading mule.test.properties", e);
    	}
    	try {
			MESSAGE = FileUtils.readFileToString(new File("./src/test/resources/message.json"));
			REPLY = FileUtils.readFileToString(new File("./src/test/resources/reply.xml"));
		} catch (IOException e) {
			e.printStackTrace();
		}    	    
    	FTP_USER = props.getProperty("ftp.user");
    	FTP_PASSWORD = props.getProperty("ftp.password");
    	FTP_PORT = props.getProperty("ftp.port");
    	FTP_HOME = props.getProperty("ftp.home");		
    	FTP_PATH = props.getProperty("ftp.path");
    	FTP_HOST = props.getProperty("ftp.host");
    	
    	System.setProperty("ftp.user", FTP_USER);
    	System.setProperty("ftp.password", FTP_PASSWORD);
    	System.setProperty("ftp.port", FTP_PORT);
    	System.setProperty("ftp.path", FTP_PATH);
    	System.setProperty("ftp.host", FTP_HOST);
		File dataDirectory = new File(FTP_HOME);
		if (dataDirectory.exists()) {
		    FileUtils.deleteDirectory(dataDirectory);
		}
		dataDirectory.mkdirs();    			
    }
    
    

    @Test
    public void testDataWeave() throws Exception
    {
        MuleClient client = new MuleClient(muleContext);
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("http.method", "POST");
        props.put("Content-Type", "application/json");
        MuleMessage result = client.send("http://0.0.0.0:" + port.getNumber() + "/", MESSAGE, props);
        assertEquals(REPLY.replaceAll("\\s",""), result.getPayloadAsString().replaceAll("\\s",""));
        
    }


	@Override
	protected Properties getStartUpProperties() {
		return new Properties(super.getStartUpProperties());
	}	    
}

@@ -224,16 +224,53 @@
 tEquals;
+%0Aimport static org.junit.Assert.fail;
 %0A%0Aimport
@@ -1073,16 +1073,64 @@
 lass); %0A
+%09private static final String EMPTY_STRING = %22%22;%0A
 %09%0A%09priva
@@ -1368,43 +1368,8 @@
 %22%22;%0A
-%09private static String REPLY = %22%22;%0A
 %09%0A%09@
@@ -1937,91 +1937,8 @@
 ));%0A
-%09%09%09REPLY = FileUtils.readFileToString(new File(%22./src/test/resources/reply.xml%22));%0A
 %09%09%7D 
@@ -2704,17 +2704,16 @@
 %09%09%0A    %7D
-%0A
     %0A   
@@ -3005,24 +3005,39 @@
 %22);%0A        
+try %7B%0A        %09
 MuleMessage 
@@ -3130,102 +3130,238 @@
     
-assertEquals(REPLY.replaceAll(%22%5C%5Cs%22,%22%22), result.getPayloadAsString().replaceAll(%22%5C%5Cs%22,%22%22)
+    //after upload, the payload should contain empty string%0A            assertEquals(EMPTY_STRING, result.getPayloadAsString());  %0A        %7D catch (Exception ex)%7B%0A        %09fail(%22The FTP upload flow was not successful.%22
 );%0A  
+ 
+     %7D
     

Fix tests for upload-to-ftp-after-converting-json-to-xml example (3.8.1).