// Path: src/main/java/org/jboss/elasticsearch/tools/content/RESTCallPreprocessor.java
// public static enum HttpMethodType {
//     GET, POST;
// }
// 
// Path: src/main/java/org/jboss/elasticsearch/tools/content/RESTCallPreprocessor.java
// public static final class HttpResponseContent {
//     public String contentType;
//     public byte[] content;
// 
//     public HttpResponseContent(String contentType, byte[] content) {
//         super();
//         this.contentType = contentType;
//         this.content = content;
//     }
// 
//     @Override
//     public String toString() {
//         return "HttpResponseContent [contentType=" + contentType + ", content=" + content != null ? new String(content) : "" + "]";
//     }
// 
// }
// 
// Path: src/test/java/org/jboss/elasticsearch/tools/content/testtools/TestUtils.java
// public abstract class TestUtils {
// 
// 	/**
// 	 * Assert passed string is same as contnt of given file loaded from classpath.
// 	 * 
// 	 * @param expectedFilePath path to file inside classpath
// 	 * @param actual content to assert
// 	 * @throws IOException
// 	 */
// 	public static void assertStringFromClasspathFile(String expectedFilePath, String actual) throws IOException {
// 		Assert.assertEquals(readStringFromClasspathFile(expectedFilePath), actual);
// 	}
// 
// 	/**
// 	 * Read file from classpath into String. UTF-8 encoding expected.
// 	 * 
// 	 * @param filePath in classpath to read data from.
// 	 * @return file content.
// 	 * @throws IOException
// 	 */
// 	public static String readStringFromClasspathFile(String filePath) throws IOException {
// 		StringWriter stringWriter = new StringWriter();
// 		IOUtils.copy(TestUtils.class.getResourceAsStream(filePath), stringWriter, "UTF-8");
// 		return stringWriter.toString();
// 	}
// 
// 	/**
// 	 * Read JSON file from classpath into Map of Map structure.
// 	 * 
// 	 * @param filePath path in classpath pointing to JSON file to read
// 	 * @return parsed JSON file
// 	 * @throws SettingsException
// 	 */
// 	public static Map<String, Object> loadJSONFromClasspathFile(String filePath) throws SettingsException {
// 		XContentParser parser = null;
// 		try {
// 			parser = XContentFactory.xContent(XContentType.JSON).createParser(TestUtils.class.getResourceAsStream(filePath));
// 			return parser.mapAndClose();
// 		} catch (IOException e) {
// 			throw new SettingsException(e.getMessage(), e);
// 		} finally {
// 			if (parser != null)
// 				parser.close();
// 		}
// 	}
// 
// }


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.jboss.elasticsearch.tools.content.RESTCallPreprocessor.HttpMethodType;
import org.jboss.elasticsearch.tools.content.RESTCallPreprocessor.HttpResponseContent;
import org.jboss.elasticsearch.tools.content.testtools.TestUtils;
import org.junit.Test;
import junit.framework.Assert;
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2017 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.jboss.elasticsearch.tools.content;




/**
 * Unit test for {@link RESTCallPreprocessor}. It also contains <code>main</code> method for integration testing.
 *
 * @author Vlastimil Elias (velias at redhat dot com)
 */
public class RESTCallPreprocessorTest {
    
    //private static final String INT_TEST_CONFIG = "/RESTCallPreprocessor_integrationTest_GET.json";
    private static final String INT_TEST_CONFIG = "/RESTCallPreprocessor_integrationTest_POST.json";
    
    
    public static void main(String[] args) {
        RESTCallPreprocessor tested = getTested();
        Map<String, Object> settings = TestUtils.loadJSONFromClasspathFile(INT_TEST_CONFIG);
        tested.init(settings);
        
        Map<String, Object> data = new HashMap<>();
        data.put("id", "JBEAP-2377");
        data.put("type", "p&2\"aa");
        
        tested.preprocessData(data, new PreprocessChainContext() {
            
            @Override
            public void addDataWarning(String preprocessorName, String warningMessage) throws IllegalArgumentException {
                System.out.println(warningMessage);
            }
        } );
        
        tested.logger.info("Data after integration test {}", data);
    }
    
    @Test
    public void prepareContent(){
        RESTCallPreprocessor tested = getTested();
        tested.request_content_template = "{ \"test\" : \"$val$\", \"test2\":\"$val2.val$\"}";

        //always null for GET
        tested.request_method = HttpMethodType.GET;
        Assert.assertEquals(null, tested.prepareContent(null));
        
        
// Path: src/main/java/org/jboss/elasticsearch/tools/content/RESTCallPreprocessor.java
// public static enum HttpMethodType {
//     GET, POST;
// }
// 
// Path: src/main/java/org/jboss/elasticsearch/tools/content/RESTCallPreprocessor.java
// public static final class HttpResponseContent {
//     public String contentType;
//     public byte[] content;
// 
//     public HttpResponseContent(String contentType, byte[] content) {
//         super();
//         this.contentType = contentType;
//         this.content = content;
//     }
// 
//     @Override
//     public String toString() {
//         return "HttpResponseContent [contentType=" + contentType + ", content=" + content != null ? new String(content) : "" + "]";
//     }
// 
// }
// 
// Path: src/test/java/org/jboss/elasticsearch/tools/content/testtools/TestUtils.java
// public abstract class TestUtils {
// 
// 	/**
// 	 * Assert passed string is same as contnt of given file loaded from classpath.
// 	 * 
// 	 * @param expectedFilePath path to file inside classpath
// 	 * @param actual content to assert
// 	 * @throws IOException
// 	 */
// 	public static void assertStringFromClasspathFile(String expectedFilePath, String actual) throws IOException {
// 		Assert.assertEquals(readStringFromClasspathFile(expectedFilePath), actual);
// 	}
// 
// 	/**
// 	 * Read file from classpath into String. UTF-8 encoding expected.
// 	 * 
// 	 * @param filePath in classpath to read data from.
// 	 * @return file content.
// 	 * @throws IOException
// 	 */
// 	public static String readStringFromClasspathFile(String filePath) throws IOException {
// 		StringWriter stringWriter = new StringWriter();
// 		IOUtils.copy(TestUtils.class.getResourceAsStream(filePath), stringWriter, "UTF-8");
// 		return stringWriter.toString();
// 	}
// 
// 	/**
// 	 * Read JSON file from classpath into Map of Map structure.
// 	 * 
// 	 * @param filePath path in classpath pointing to JSON file to read
// 	 * @return parsed JSON file
// 	 * @throws SettingsException
// 	 */
// 	public static Map<String, Object> loadJSONFromClasspathFile(String filePath) throws SettingsException {
// 		XContentParser parser = null;
// 		try {
// 			parser = XContentFactory.xContent(XContentType.JSON).createParser(TestUtils.class.getResourceAsStream(filePath));
// 			return parser.mapAndClose();
// 		} catch (IOException e) {
// 			throw new SettingsException(e.getMessage(), e);
// 		} finally {
// 			if (parser != null)
// 				parser.close();
// 		}
// 	}
// 
// }


// Path: src/test/java/org/jboss/elasticsearch/tools/content/RESTCallPreprocessorTest.java
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.common.xcontent.support.XContentMapValues;
import org.jboss.elasticsearch.tools.content.RESTCallPreprocessor.HttpMethodType;
import org.jboss.elasticsearch.tools.content.RESTCallPreprocessor.HttpResponseContent;
import org.jboss.elasticsearch.tools.content.testtools.TestUtils;
import org.junit.Test;
import junit.framework.Assert;

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2017 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 */
package org.jboss.elasticsearch.tools.content;




/**
 * Unit test for {@link RESTCallPreprocessor}. It also contains <code>main</code> method for integration testing.
 *
 * @author Vlastimil Elias (velias at redhat dot com)
 */
public class RESTCallPreprocessorTest {
    
    //private static final String INT_TEST_CONFIG = "/RESTCallPreprocessor_integrationTest_GET.json";
    private static final String INT_TEST_CONFIG = "/RESTCallPreprocessor_integrationTest_POST.json";
    
    
    public static void main(String[] args) {
        RESTCallPreprocessor tested = getTested();
        Map<String, Object> settings = TestUtils.loadJSONFromClasspathFile(INT_TEST_CONFIG);
        tested.init(settings);
        
        Map<String, Object> data = new HashMap<>();
        data.put("id", "JBEAP-2377");
        data.put("type", "p&2\"aa");
        
        tested.preprocessData(data, new PreprocessChainContext() {
            
            @Override
            public void addDataWarning(String preprocessorName, String warningMessage) throws IllegalArgumentException {
                System.out.println(warningMessage);
            }
        } );
        
        tested.logger.info("Data after integration test {}", data);
    }
    
    @Test
    public void prepareContent(){
        RESTCallPreprocessor tested = getTested();
        tested.request_content_template = "{ \"test\" : \"$val$\", \"test2\":\"$val2.val$\"}";

        //always null for GET
        tested.request_method = HttpMethodType.GET;
        Assert.assertEquals(null, tested.prepareContent(null));
        
        
        tested.request_method = HttpMethodType.POST;