package org.datavec.spark.transform;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.ObjectMapper;
import com.mashape.unirest.http.Unirest;
import org.apache.commons.io.FileUtils;
import org.datavec.api.transform.TransformProcess;
import org.datavec.api.transform.schema.Schema;
import org.datavec.spark.transform.model.Base64NDArrayBody;
import org.datavec.spark.transform.model.BatchRecord;
import org.datavec.spark.transform.model.CSVRecord;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull;

/**
 * Created by agibsonccc on 1/22/17.
 */
public class CSVSparkTransformServerNoJsonTest {

    private static CSVSparkTransformServer server;
    private static Schema schema = new Schema.Builder().addColumnDouble("1.0").addColumnDouble("2.0").build();
    private static TransformProcess transformProcess =
                    new TransformProcess.Builder(schema).convertToString("1.0").convertToString("2.0").build();
    private static File fileSave = new File(UUID.randomUUID().toString() + ".json");

    @BeforeClass
    public static void before() throws Exception {
        server = new CSVSparkTransformServer();
        FileUtils.write(fileSave, transformProcess.toJson());
        // Only one time
        Unirest.setObjectMapper(new ObjectMapper() {
            private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper =
                            new com.fasterxml.jackson.databind.ObjectMapper();

            public <T> T readValue(String value, Class<T> valueType) {
                try {
                    return jacksonObjectMapper.readValue(value, valueType);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            public String writeValue(Object value) {
                try {
                    return jacksonObjectMapper.writeValueAsString(value);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        server.runMain(new String[] {"-dp", "9050"});
    }

    @AfterClass
    public static void after() throws Exception {
        fileSave.delete();
        server.stop();

    }




    @Test
    public void testServer() throws Exception {
       assertTrue(server.getTransform() == null);
        JsonNode jsonStatus = Unirest.post("http://localhost:9050/transformprocess")
                .header("accept", "application/json")
                .header("Content-Type", "application/json").body(transformProcess.toJson())
                .asJson().getBody();
        assumeNotNull(server.getTransform());

        String[] values = new String[] {"1.0", "2.0"};
        CSVRecord record = new CSVRecord(values);
        JsonNode jsonNode = Unirest.post("http://localhost:9050/transform").header("accept", "application/json")
                        .header("Content-Type", "application/json").body(record).asJson().getBody();
        CSVRecord csvRecord = Unirest.post("http://localhost:9050/transform").header("accept", "application/json")
                        .header("Content-Type", "application/json").body(record).asObject(CSVRecord.class).getBody();

        BatchRecord batchRecord = new BatchRecord();
        for (int i = 0; i < 3; i++)
            batchRecord.add(csvRecord);
        BatchRecord batchRecord1 = Unirest.post("http://localhost:9050/transformbatch")
                        .header("accept", "application/json").header("Content-Type", "application/json")
                        .body(batchRecord).asObject(BatchRecord.class).getBody();

        Base64NDArrayBody array = Unirest.post("http://localhost:9050/transformedarray")
                        .header("accept", "application/json").header("Content-Type", "application/json").body(record)
                        .asObject(Base64NDArrayBody.class).getBody();

        Base64NDArrayBody batchArray1 = Unirest.post("http://localhost:9050/transformedbatcharray")
                        .header("accept", "application/json").header("Content-Type", "application/json")
                        .body(batchRecord).asObject(Base64NDArrayBody.class).getBody();



    }

}

@@ -3076,32 +3076,43 @@
 t:9050/transform
+incremental
 %22).header(%22accep
@@ -3127,32 +3127,32 @@
 lication/json%22)%0A
-
                 
@@ -3311,16 +3311,27 @@
 ransform
+incremental
 %22).heade
@@ -3690,21 +3690,16 @@
 ransform
-batch
 %22)%0A     
@@ -3960,18 +3960,27 @@
 ransform
-ed
+incremental
 array%22)%0A
@@ -4160,32 +4160,32 @@
 ss).getBody();%0A%0A
+
         Base64ND
@@ -4257,15 +4257,8 @@
 form
-edbatch
 arra

modify endpoints (removed `ed` postfix)