package fi.nls.oskari.map.layer.formatters;

import fi.nls.oskari.domain.map.OskariLayer;
import fi.nls.oskari.domain.map.userlayer.UserLayer;
import fi.nls.oskari.domain.map.wfs.WFSLayerAttributes;
import fi.nls.oskari.util.PropertyUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class LayerJSONFormatterUSERLAYERTest {
    private final static LayerJSONFormatterUSERLAYER FORMATTER_USERLAYER = new LayerJSONFormatterUSERLAYER();
    private final static String SRS = "EPSG:4326";
    private final static String LANG = PropertyUtil.getDefaultLanguage();
    private final static String NAMESPACE = "http://www.oskari.org";

    private static final String FIELDS = "[{" +
            "\"name\": \"the_geom\"," +
            "\"type\": \"MultiPolygon\"" +
        "}, {" +
            "\"name\": \"NAME\"," +
            "\"type\": \"String\"," +
            "\"locales\": {" +
                "\"en\": \"Name\"," +
                "\"fi\": \"Nimi\"" +
            "}" +
        "}, {" +
            "\"name\": \"CODE\"," +
            "\"type\": \"Long\","+
            "\"locales\": {" +
                "\"en\": \"Code\"," +
                "\"fi\": \"Koodi\"" +
        "}" +
    "}]";

    @Test
    public void parseFields() throws JSONException {
        OskariLayer baseLayer = new OskariLayer();
        baseLayer.getAttributes().put("namespaceURL", NAMESPACE);
        UserLayer ulayer = new UserLayer();
        ulayer.setFields(new JSONArray(FIELDS));
        JSONObject layerJson = FORMATTER_USERLAYER.getJSON(baseLayer, ulayer, SRS, LANG);
        WFSLayerAttributes attr = new WFSLayerAttributes(layerJson.getJSONObject("attributes"));
        Assert.assertEquals( "parseFields shouldn't override baselayer attributes", NAMESPACE, attr.getNamespaceURL());

        List<String> selected = attr.getSelectedAttributes();
        Assert.assertEquals("2 attributes selected", 2, selected.size());
        Assert.assertEquals("NAME should be first", "NAME", selected.get(0));
        Assert.assertEquals( "CODE should be second","CODE", selected.get(1));

        JSONObject en = attr.getLocalization().get();
        Assert.assertEquals(2, en.length());
        Assert.assertEquals("Name", en.getString("NAME"));
        JSONObject fi = attr.getLocalization("fi").get();
        Assert.assertEquals(2, fi.length());
        Assert.assertEquals("Nimi", fi.getString("NAME"));

        JSONObject data = attr.getAttributes().getJSONObject("data");
        JSONObject types = data.optJSONObject("types");
        Assert.assertEquals("string", types.getString("NAME"));
        Assert.assertEquals("number", types.getString("CODE"));

        Assert.assertEquals("the_geom", data.getString("geometryName"));
        Assert.assertEquals("MultiPolygon", data.getString("geometryType"));
    }
}

@@ -2915,11 +2915,962 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void parseAttributes() throws JSONException %7B%0A        OskariLayer baseLayer = new OskariLayer();%0A        baseLayer.getAttributes().put(%22namespaceURL%22, NAMESPACE);%0A        UserLayer ulayer = new UserLayer();%0A        JSONArray fields = new JSONArray(FIELDS);%0A        ulayer.setFields(fields);%0A        JSONObject json = FORMATTER_USERLAYER.getJSON(baseLayer, ulayer, SRS, LANG);%0A        fields.remove(2);%0A        JSONObject anotherJson = FORMATTER_USERLAYER.getJSON(baseLayer, ulayer, SRS, LANG);%0A%0A        WFSLayerAttributes attr = new WFSLayerAttributes(json.getJSONObject(%22attributes%22));%0A        WFSLayerAttributes anotherAttr = new WFSLayerAttributes(anotherJson.getJSONObject(%22attributes%22));%0A%0A        Assert.assertEquals(attr.getNamespaceURL(), anotherAttr.getNamespaceURL());%0A        Assert.assertEquals(2, attr.getSelectedAttributes().size());%0A        Assert.assertEquals(1, anotherAttr.getSelectedAttributes().size());%0A    %7D
 %0A%7D%0A

add test for parsing another userlayer json