/*
 * Copyright (c) 2004-2019, University of Oslo
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the HISP project nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.hisp.dhis.android.core.datavalue.internal;

import org.hisp.dhis.android.core.BaseRealIntegrationTest;
import org.hisp.dhis.android.core.D2;
import org.hisp.dhis.android.core.D2Factory;
import org.hisp.dhis.android.core.common.State;
import org.hisp.dhis.android.core.datavalue.DataValue;
import org.junit.Before;

import java.io.IOException;

import static com.google.common.truth.Truth.assertThat;

public class DataValuePostCallRealIntegrationShould extends BaseRealIntegrationTest {

    private D2 d2;

    private DataValueStore dataValueStore;

    @Before
    public void setUp() throws IOException {

        super.setUp();

        d2 = D2Factory.forNewDatabase();

        dataValueStore = DataValueStore.create(databaseAdapter());
    }

    // commented out since it is a flaky test that works against a real server.
    //@Test
    public void dataValuesWithToPostState_shouldBeUploaded() throws Exception {
        d2.userModule().logIn(username, password, url).blockingGet();

        d2.metadataModule().blockingDownload();
        d2.aggregatedModule().data().download().subscribe();

        DataValue dataValue = getTestDataValueWith(State.TO_POST, 1);

        assertThat(insertToPostDataValue(dataValue)).isTrue();

        d2.dataValueModule().dataValues().blockingUpload();

        /*int importCountTotal = dataValueImportSummary.importCount().imported() +
                dataValueImportSummary.importCount().updated() +
                dataValueImportSummary.importCount().ignored();

        assertThat(importCountTotal == 1).isTrue();*/
    }

    // commented out since it is a flaky test that works against a real server.
    //@Test
    public void dataValuesWithToUpdateState_shouldBeUploaded() throws Exception {

        d2.metadataModule().blockingDownload();
        d2.aggregatedModule().data().download().subscribe();

        DataValue dataValue = getTestDataValueWith(State.TO_UPDATE,2);

        assertThat(insertToPostDataValue(dataValue)).isTrue();

        d2.dataValueModule().dataValues().blockingUpload();

        /*int importCountTotal = dataValueImportSummary.importCount().updated() +
                dataValueImportSummary.importCount().ignored();

        assertThat(importCountTotal == 1).isTrue();*/
    }


    private boolean insertToPostDataValue(DataValue dataValue){

        return (dataValueStore.insert(dataValue) > 0);
    }

    private DataValue getTestDataValueWith(State state, int value) {

        DataValue dataValue =
                DataValue.builder()
                        .dataElement("Qb790K82yqZ")
                        .categoryOptionCombo("Gmbgme7z9BF")
                        .attributeOptionCombo("RgrNGmlMOAJ")
                        .period("2018")
                        .organisationUnit("s91COhdBQ23")
                        .value(String.valueOf(value))
                        .state(state)
                        .build();

        return dataValue;
    }
}

@@ -2286,16 +2286,19 @@
 .create(
+d2.
 database
@@ -3894,17 +3894,16 @@
 Value)%7B%0A
-%0A
         
@@ -4025,25 +4025,24 @@
 alue) %7B%0A
-%0A
         
 DataValu
@@ -4033,37 +4033,101 @@
         
-DataValue dataValue =
+String cocUid = d2.categoryModule().categoryOptionCombos().one().blockingGet().uid();
 %0A       
@@ -4127,23 +4127,22 @@
         
-       
+return
  DataVal
@@ -4195,21 +4195,71 @@
 ent(
-%22Qb790K82yqZ%22
+d2.dataElementModule().dataElements().one().blockingGet().uid()
 )%0A  
@@ -4305,21 +4305,14 @@
 mbo(
-%22Gmbgme7z9BF%22
+cocUid
 )%0A  
@@ -4359,21 +4359,14 @@
 mbo(
-%22RgrNGmlMOAJ%22
+cocUid
 )%0A  
@@ -4399,71 +4399,183 @@
 iod(
-%222018%22)%0A                        .organisationUnit(%22s91COhdBQ23%22
+d2.periodModule().periods().one().blockingGet().periodId())%0A                        .organisationUnit(d2.organisationUnitModule().organisationUnits().one().blockingGet().uid()
 )%0A  
@@ -4664,16 +4664,16 @@
 (state)%0A
+
         
@@ -4701,35 +4701,8 @@
 d();
-%0A%0A        return dataValue;
 %0A   

Fix DataValuePostCallRealIntegrationShould