package org.waterforpeople.mapping.app.web;

import com.gallatinsystems.surveyal.dao.SurveyedLocaleDao;
import com.gallatinsystems.surveyal.domain.SurveyedLocale;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import org.akvo.flow.api.app.DataStoreTestUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.waterforpeople.mapping.app.web.dto.RawDataImportRequest;
import org.waterforpeople.mapping.domain.SurveyInstance;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RawDataRestServletTest {
    private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
    private DataStoreTestUtil dataStoreTestUtil;

    @BeforeEach
    public void setUp() {
        helper.setUp();
        dataStoreTestUtil = new DataStoreTestUtil();
    }

    @AfterEach
    public void tearDown() {
        helper.tearDown();
    }

    @Test
    void testFormInstanceMissingDataPoint() {
        Long surveyId = dataStoreTestUtil.randomId();

        List<SurveyedLocale> dataPoints = dataStoreTestUtil.createDataPoints(surveyId, 1);
        List<SurveyInstance> formInstances = dataStoreTestUtil.createFormInstances(dataPoints, 1);

        SurveyInstance instance = formInstances.get(0);
        new SurveyedLocaleDao().delete(dataPoints.get(0));

        RawDataImportRequest importRequest = new RawDataImportRequest();
        importRequest.setSurveyInstanceId(instance.getKey().getId());

        RawDataRestServlet servlet = new RawDataRestServlet();
        Map<Long, String> validateMissingDataPoint = servlet.validateImportRequest(importRequest);
        assertEquals("Associated datapoint is missing [ datapoint id = " + instance.getSurveyedLocaleId() + "]", validateMissingDataPoint.get(instance.getKey().getId()));
    }
}

@@ -717,16 +717,75 @@
 tEquals;
+%0Aimport static org.junit.jupiter.api.Assertions.assertTrue;
 %0A%0Apublic
@@ -2087,11 +2087,727 @@
 );%0A    %7D
+%0A%0A    @Test%0A    void testFormInstanceValidationOk() %7B%0A        Long surveyId = dataStoreTestUtil.randomId();%0A%0A        List%3CSurveyedLocale%3E dataPoints = dataStoreTestUtil.createDataPoints(surveyId, 1);%0A        List%3CSurveyInstance%3E formInstances = dataStoreTestUtil.createFormInstances(dataPoints, 1);%0A%0A        SurveyInstance instance = formInstances.get(0);%0A%0A        RawDataImportRequest importRequest = new RawDataImportRequest();%0A        importRequest.setSurveyInstanceId(instance.getKey().getId());%0A%0A        RawDataRestServlet servlet = new RawDataRestServlet();%0A        Map%3CLong, String%3E validateImportErrors = servlet.validateImportRequest(importRequest);%0A        assertTrue(validateImportErrors.isEmpty());%0A    %7D
 %0A%7D%0A

Add test for scenario where the imported instance has no errors