package org.oskari.statistics.plugins.unsd;

import fi.nls.oskari.control.statistics.data.IndicatorValue;
import fi.nls.oskari.control.statistics.data.StatisticalIndicator;
import fi.nls.oskari.control.statistics.data.StatisticalIndicatorDataModel;
import fi.nls.oskari.util.JSONHelper;
import fi.nls.test.util.ResourceHelper;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import java.util.List;
import java.util.Map;

;import static org.junit.Assert.*;

public class UnsdDataParserTest {

    private static String targetResponse = ResourceHelper.readStringResource(
            "TargetList.json", UnsdDataParserTest.class);
    private static String dimensionsResponse = ResourceHelper.readStringResource(
            "Dimensions.json", UnsdDataParserTest.class);
    private static JSONObject dataResponseUnlimited = JSONHelper.createJSONObject(ResourceHelper.readStringResource(
            "IndicatorData.json", UnsdDataParserTest.class));
    private static JSONObject dataResponse2015AreaLimited = JSONHelper.createJSONObject(ResourceHelper.readStringResource(
            "IndicatorData2015.json", UnsdDataParserTest.class));

    @Test
    public void testIndicatorMetadata() throws JSONException {
        List<StatisticalIndicator> indicators = UnsdIndicatorParser.parseIndicators(targetResponse);
        assertFalse("Did not parse any indicator codes from target response.", indicators.isEmpty());

        StatisticalIndicatorDataModel model = UnsdIndicatorParser.parseDimensions(dimensionsResponse);
        assertNotEquals("Indicator doesn't have any dimensions.", model.getDimensions().size(), 0);

        Map<String, String> sources = UnsdIndicatorParser.parseSource(dataResponseUnlimited);
        assertNotNull("Indicator doesn't have source.", sources.get("en"));
    }

    @Test
    public void testIndicatorData() throws JSONException {
        Map<String, IndicatorValue> valuesByRegion = UnsdDataParser.parseIndicatorData(dataResponseUnlimited);
        assertFalse("Did not parse any data from response.", valuesByRegion.isEmpty());
    }

    @Test
    public void testIndicatorDataPages() throws JSONException {
        Boolean lastPage = UnsdDataParser.isLastPage(dataResponse2015AreaLimited);
        assertNotNull("Could not parse page info", lastPage);
        assertTrue("The response wasn't the last page.", lastPage);

        lastPage = UnsdDataParser.isLastPage(dataResponseUnlimited);
        assertNotNull("Could not parse page info", lastPage);
        assertFalse("The response was the last page.", lastPage);
    }
}


@@ -38,16 +38,73 @@
 .unsd;%0A%0A
+import fi.nls.oskari.control.statistics.data.IdNamePair;%0A
 import f
@@ -223,16 +223,96 @@
 icator;%0A
+import fi.nls.oskari.control.statistics.data.StatisticalIndicatorDataDimension;%0A
 import f
@@ -540,16 +540,44 @@
 .Test;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -614,17 +614,16 @@
 l.Map;%0A%0A
-;
 import s
@@ -824,148 +824,8 @@
 s);%0A
-    private static String dimensionsResponse = ResourceHelper.readStringResource(%0A            %22Dimensions.json%22, UnsdDataParserTest.class);%0A
     
@@ -1185,24 +1185,541 @@
 t.class));%0A%0A
+    private static final int EXPECTED_DIMENSIONS_COUNT = 1;%0A    private static final String EXPECTED_DIMENSION_ID = %22Reporting Type%22;%0A    private static final List%3CIdNamePair%3E EXPECTED_DIMENSION_ALLOWED_VALUES = new ArrayList%3C%3E();%0A%0A    static %7B%0A        // equals method in IdNamePair class only compares keys so empty strings are%0A        // used here as values.%0A        EXPECTED_DIMENSION_ALLOWED_VALUES.add(new IdNamePair(%22N%22, %22%22));%0A        EXPECTED_DIMENSION_ALLOWED_VALUES.add(new IdNamePair(%22G%22, %22%22));%0A    %7D%0A    %0A
     @Test%0A  
@@ -1749,16 +1749,9 @@
 ator
-Metadata
+s
 () t
@@ -1979,212 +1979,86 @@
 ));%0A
-%0A
     
-    StatisticalIndicatorDataModel model = UnsdIndicatorParser.parseDimensions(dimensionsResponse);%0A        assertNotEquals(%22Indicator doesn't have any dimensions.%22, model.getDimensions().size(), 0);%0A
+%7D%0A %0A    @Test%0A    public void testIndicatorSource() throws JSONException %7B
 %0A   
@@ -2997,11 +2997,902 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void testIndicatorDimensions() %7B%0A        StatisticalIndicatorDataModel m = UnsdIndicatorParser.parseDimensions(dataResponseUnlimited);%0A        assertNotNull(m);%0A        assertNotNull(m.getDimensions());%0A        assertEquals(EXPECTED_DIMENSIONS_COUNT, m.getDimensions().size());%0A        StatisticalIndicatorDataDimension d = m.getDimensions().get(0);%0A        assertEquals(EXPECTED_DIMENSION_ID, d.getId());%0A        List%3CIdNamePair%3E dimensionsAllowedValues = d.getAllowedValues();%0A        assertNotNull(dimensionsAllowedValues);%0A        assertEquals(EXPECTED_DIMENSION_ALLOWED_VALUES.size(), dimensionsAllowedValues.size());%0A        EXPECTED_DIMENSION_ALLOWED_VALUES.forEach(v -%3E assertTrue(%0A                String.format(%22Expected key %5B %25s %5D not found from dimensions parsed allowed values%22, v.getKey()),%0A                dimensionsAllowedValues.contains(v)));%0A    %7D%0A
 %7D%0A%0A

Refactor testIndicatorMetadata test to multiple tests and add new test to verify parsing of indicator spesific dimensions