package cz.moneta.test.regression.cvm.api.cvm;

import com.fasterxml.jackson.databind.JsonNode;
import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;

import java.util.List;

import static cz.moneta.test.harness.annotations.Environment.PPE;
import static cz.moneta.test.harness.annotations.Environment.TST1;

@TestScenario(name = "Call API RTRD_ODS_CONSENTS_CHANGE to WSO2 and assert response parameters")
public class ApiRtrdOdsConsentsChange {

    @TestCase(name = "Call API RTRD_ODS_CONSENTS_CHANGE to WSO2", environments = {TST1, PPE})
    public void postRtrdOdsConsentsChange(Harness harness) throws InterruptedException {
        RawRestRequest.Response response = harness.tasks().wso2().restRequest().prepareRequestFromCvm(
                "cvm.cluster/RTRD_ODS_CONSENTS_CHANGE/001", false,
                "{\n" +
                        "  \"uniPtKeyList\": [\n" +
                        "    \"" + harness.getConfig("cvm.client.odsconsentschange.uniptkey") + "\"\n" +
                        "  ]\n" +
                        "}")
                .postExtended()
                .andAssertStatus(200);

        JsonNode body = response.extract("");
        List<Object> activityIds = response.getHeader("activityId");

        Assertions.assertNotNull(body, "Not found output in response");
        Assertions.assertEquals(1, activityIds.size(), "API should return activityId header");

        Thread.sleep(6000); // Delay to finish async code

        Result<Record> onlineDbRecords = harness.tasks().cvm().onlineDbGetData()
                .getOnlineData("ODS_GET_EVDCNST_DATA", activityIds.get(0).toString());
        Assertions.assertTrue(onlineDbRecords.size() > 10, "Should create at least 10 records in OnlineDB, but created only " + onlineDbRecords.size());
        Assertions.assertEquals(activityIds.get(0).toString() + ".1", onlineDbRecords.get(0).getValue("MSG_ID"), "First record should contain activityId.1 as message id");
        Assertions.assertEquals("UNI_PT_KEY", onlineDbRecords.get(0).getValue("NAME"), "First record should contain UNI_PT_KEY attribute name");
        Assertions.assertEquals(harness.getConfig("cvm.client.odsconsentschange.uniptkey"), onlineDbRecords.get(0).getValue("VAL"), "First record should contain requested client uni pt key as attribute value");
    }
}