package org.oskari.map.userlayer.input;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.referencing.CRS;
import org.junit.Test;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.oskari.map.userlayer.input.SHPParser;

import fi.nls.oskari.service.ServiceException;

public class SHPParserTest {

    @Test
    public void testParse() throws ServiceException, URISyntaxException, NoSuchAuthorityCodeException, FactoryException {
        SimpleFeatureCollection fc = parse("SHP2017.shp");
        List<SimpleFeature> features = collectToList(fc);
        assertEquals("There are 21 Features in the file", 21, features.size());
        CoordinateReferenceSystem crs = fc.getSchema().getGeometryDescriptor().getCoordinateReferenceSystem();
        assertEquals("Projection is correctly determined from .prj file", "EPSG:3067", CRS.toSRS(crs));
    }

    private SimpleFeatureCollection parse(String resourcePath) throws URISyntaxException, ServiceException {
        SHPParser parser = new SHPParser();
        File file = new File(getClass().getResource(resourcePath).toURI());
        return parser.parse(file, null, null);
    }

    private List<SimpleFeature> collectToList(SimpleFeatureCollection fc) {
        List<SimpleFeature> features = new ArrayList<>();
        try (SimpleFeatureIterator it = fc.features()) {
            while (it.hasNext()) {
                features.add(it.next());
            }
        }
        return features;
    }

}

@@ -78,16 +78,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Aimport
@@ -629,55 +629,247 @@
 em;%0A
+%0A
 import 
-org.oskari.map.userlayer.input.SHPParser
+com.vividsolutions.jts.geom.Coordinate;%0Aimport com.vividsolutions.jts.geom.CoordinateSequence;%0Aimport com.vividsolutions.jts.geom.Geometry;%0Aimport com.vividsolutions.jts.geom.MultiPolygon;%0Aimport com.vividsolutions.jts.geom.Polygon
 ;%0A%0Ai
@@ -1069,32 +1069,705 @@
 toryException %7B%0A
+        SimpleFeature shp16 = getSHP16();%0A        Geometry geom = (Geometry) shp16.getDefaultGeometry();%0A        assertTrue(geom instanceof MultiPolygon);%0A        MultiPolygon mp = (MultiPolygon) geom;%0A        assertEquals(14, mp.getNumGeometries());%0A        Polygon p = (Polygon) mp.getGeometryN(0);%0A        CoordinateSequence exterior = p.getExteriorRing().getCoordinateSequence();%0A        Coordinate first = exterior.getCoordinate(0);%0A        assertEquals(194831.89, first.x, 1e6);%0A        assertEquals(6947066.665, first.y, 1e6);%0A        assertTrue(Double.isNaN(first.z));%0A    %7D%0A%0A    public static SimpleFeature getSHP16() throws URISyntaxException, ServiceException %7B%0A
         SimpleFe
@@ -2158,24 +2158,235 @@
 oSRS(crs));%0A
+        return features.stream()%0A                .filter(f -%3E ((String) f.getAttribute(%22Sairaanhoi%22)).equals(%2216%22))%0A                .findAny()%0A                .orElseThrow(() -%3E new IllegalArgumentException());%0A
     %7D%0A%0A    p
@@ -2392,16 +2392,23 @@
 private 
+static 
 SimpleFe
@@ -2573,18 +2573,27 @@
 ile(
-getC
+SHPParserTest.c
 lass
-()
 .get
@@ -2691,16 +2691,23 @@
 private 
+static 
 List%3CSim

Improve SHPParserTest