package fi.nls.paikkatietoikkuna.coordtransform;

import static org.junit.Assert.assertEquals;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.vividsolutions.jts.geom.Coordinate;
import fi.nls.oskari.control.ActionException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.Test;

public class CoordinateTransformationActionHandlerTest {

    @Test
    public void testParseInputCoordinates() throws IOException, ActionException {
        int n = 100;
        int dimension = 3;
        List<Coordinate> expected = getRandomCoordinates(n, 0.0, 1000.0);
        byte[] jsonBytes = createJsonArrayOfArrays(expected, dimension);
        CoordinateTransformationActionHandler handler = new CoordinateTransformationActionHandler();
        List<Coordinate> actual = handler.parseInputCoordinates(new ByteArrayInputStream(jsonBytes), dimension);
        for (int i = 0; i < n; i++) {
            Coordinate e = expected.get(i);
            Coordinate a = actual.get(i);
            assertEquals(e.x, a.x, 0.00001);
            assertEquals(e.y, a.y, 0.00001);
            assertEquals(e.z, a.z, 0.00001);
        }
    }

    private List<Coordinate> getRandomCoordinates(int n, double min, double max) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        List<Coordinate> coordinates = new ArrayList<>(n);
        for (int i = 0; i < n; i++) {
            Coordinate coord = new Coordinate(
                    random.nextDouble(min, max),
                    random.nextDouble(min, max),
                    random.nextDouble(min, max));
            coordinates.add(coord);
        }
        return coordinates;
    }

    private byte[] createJsonArrayOfArrays(List<Coordinate> coords, int dimension) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JsonGenerator json = new JsonFactory().createGenerator(baos);
        json.writeStartArray();
        for (Coordinate coord : coords) {
            json.writeStartArray();
            json.writeNumber(coord.x);
            json.writeNumber(coord.y);
            if (dimension == 3) {
                json.writeNumber(coord.z);
            }
            json.writeEndArray();
        }
        json.writeEndArray();
        json.close();
        return baos.toByteArray();
    }

}

@@ -185,16 +185,68 @@
 erator;%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0A
 import c
@@ -284,16 +284,16 @@
 dinate;%0A
-
 import f
@@ -531,16 +531,17 @@
 Random;%0A
+%0A
 import o
@@ -1104,32 +1104,39 @@
 ytes), dimension
+, false
 );%0A        for (
@@ -1390,32 +1390,1484 @@
        %7D%0A    %7D%0A%0A
+    @Test%0A    public void testCreateFileSettings ()%7B%0A        CoordTransFile file = getFileSettings();%0A        assertEquals(%22test.txt%22, file.getFileName());%0A        assertEquals('.', file.getDecimalSeparator());%0A        assertEquals(%22tab%22, file.getCoordinateSeparator());%0A        assertEquals(2, file.getHeaderLineCount());%0A        assertEquals(5, file.getDecimalCount());%0A        assertEquals(%22degree%22, file.getUnit());%0A        assertEquals(true, file.isAxisFlip());%0A        assertEquals(true, file.isPrefixId());%0A        assertEquals(false, file.isWriteCardinals());%0A        assertEquals(true, file.isWriteLineEndings());%0A        assertEquals(true, file.isWriteHeader());%0A    %7D%0A    private CoordTransFile getFileSettings ()%7B%0A        ObjectMapper mapper = new ObjectMapper();%0A        String json = %22%7B%5C%22fileName%5C%22:%5C%22test.txt%5C%22,%22%0A                + %22%5C%22unit%5C%22:%5C%22degree%5C%22,%22%0A                + %22%5C%22decimalSeparator%5C%22:%5C%22.%5C%22,%22%0A                + %22%5C%22coordinateSeparator%5C%22:%5C%22tab%5C%22,%22%0A                + %22%5C%22prefixId%5C%22:true,%22%0A                + %22%5C%22writeHeader%5C%22:true,%22%0A                + %22%5C%22axisFlip%5C%22:true,%22%0A                + %22%5C%22writeCardinals%5C%22:false,%22%0A                + %22%5C%22writeLineEndings%5C%22:true,%22%0A                + %22%5C%22decimalCount%5C%22:5,%22%0A                + %22%5C%22headerLineCount%5C%22:2%7D%22;%0A        try %7B%0A             return mapper.readValue(json, CoordTransFile.class);%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A        return null;%0A    %7D%0A%0A
     private List
@@ -4020,17 +4020,16 @@
 rray();%0A
+
     %7D%0A
-%0A
 %7D%0A

test file settings mapping