package cz.moneta.test.sanity.transactional.cards.ods;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.JiraTestCase;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.sanity.transactional.SmokeData;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.SAME_THREAD)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestScenario(name = "get and use UniPtKey")
public class InstPtKey {
    private String rc;
    private String instPtKey;
    private String cif;
    private String firstName;
    private String lastName;

    @BeforeAll
    public void getInfo(Harness harness) {
        SmokeData smokeData = SmokeData.getInstance(harness);
        rc = smokeData.getCreditData().getRc();
        cif = smokeData.getCreditData().getCif();
        String name = smokeData.getCreditData().getName();
        String[] split = name.split(" ");
        lastName = split[split.length - 1];
        firstName = split[split.length - 2];
    }

    @Order(1)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3657")
    public void GET_INST_PT_KEY(Harness harness) {
        instPtKey = harness.withWso2()
                .prepareRequestXml()
                .withPath("/t/cards.cluster/test-ods-get-inst-pt-key")
                .withPayload("<GET_GET_INST_PT_KEY_REQ>" +
                        "<RC_NUM_IN>" + rc + "</RC_NUM_IN>" +
                        "<SRC_SYS_ID>ICB0</SRC_SYS_ID>" +
                        "</GET_GET_INST_PT_KEY_REQ>")
                .post()
                .andAssertStatus(200)
                .andAssertPresent("RS_1")
                .andAssertPresent("RS_1.rows")
                .andAssertPresent("RS_1.rows.row")
                .andAssertPresent("RS_1.rows.row.INST_PT_KEY")
                .andAssertFieldValue("RS_1.rows.row.CIF", cif)
                .andAssertFieldValue("RS_1.rows.row.FAMILY_NAME", lastName)
                .andAssertFieldValue("RS_1.rows.row.FIRST_NAME", firstName)
                .extract("RS_1.rows.row.INST_PT_KEY")
                .textValue();
    }

    @Order(2)
    @TestCase(environments = {Environment.TST3, Environment.FVE, Environment.PPE})
    @JiraTestCase(id = "FVP-T3658")
    public void ODS_GET_INSTPT_DATA(Harness harness) {
        harness.withWso2()
                .prepareRequestXml()
                .withPath("/t/cards.cluster/test-ods-get-instpt-data")
                .withPayload("<GET_INSTPT_DATA_REQ>" +
                        "<IPT_FLAG>1</IPT_FLAG>" +
                        "<IPHN_FLAG>1</IPHN_FLAG>" +
                        "<IPT_INST_PT_KEY>" + instPtKey + "</IPT_INST_PT_KEY>" +
                        "</GET_INSTPT_DATA_REQ>")
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("IPT_FIRST_NAME", firstName)
                .andAssertFieldValue("IPT_FAMILY_NAME", lastName)
                .andAssertFieldValue("IPT_CIF", cif)
                .andAssertFieldValue("IPT_RC_NUM", rc);
    }
}
