package cz.moneta.test.regression.risk.refinanso.wso2;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.refinanso.util.data.Calculation;
import cz.moneta.test.dsl.refinanso.util.data.Client;
import cz.moneta.test.dsl.refinanso.util.data.model.NasApprovalResponse;
import cz.moneta.test.dsl.refinanso.util.data.model.RsRsn;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.harness.annotations.Environment;
import cz.moneta.test.harness.annotations.TestCase;
import cz.moneta.test.harness.annotations.TestScenario;
import cz.moneta.test.harness.support.rest.RawRestRequest;
import cz.moneta.test.harness.support.util.Level;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;

import java.util.List;

@TestScenario(name = "Create new contract in Refinanso via WSO2 APIs")
public class CreateNewContractTest {
    private RawRestRequest.Response response;
    private String jsonBody, tokenMortgages;
    private Calculation calculation;
    private int delayTimeout = 10000;
    private int maxRequestLoops = 12;

    @BeforeAll
    public void setup(Harness harness) {
        tokenMortgages = harness.tasks().wso2().authorization().getToken("REFINANSO_HYP");

        Client mainClient = Client.builder()
                .firstName("Pavel")
                .lastName("Pavelka")
                .pin("8304032649")
                .nationality(52)
                .maritalStatus("unmarried")
                .instalmentAmountOther(0)
                .income(120000)
                .isStable(true)
                .noCollDependent(true)
                .noForeignCurrencyntIsStable(true)
                .build();

        calculation = Calculation.builder()
                .mainClient(mainClient)
                .callContext("standard")
                .creditWorthinessVariant("simple")
                .channel("refinanso")
                .fixation(7)
                .bankName("Hypoteční banka, a.s.")
                .lifeInsuranceVariant("1")
                .offsetType("reduction")
                .product(8)
                .purposesID(87)
                .refinancingDate(DateUtils.getFormattedTodayDate("yyyy-MM-dd"))
                .installmentAmount(7200)
                .loanAmount(1000001)
                .maturity(15)
                .isCollApplicabilityClient(true)
                .isCollApplicabilityProcess(true)
                .pledgeReferenceNumber("V-3132/2013-704")
                .personsWithIncome(1)
                .personsWithoutIncome(0)
                .build();
    }

    @TestCase(name = "Create new contract", environments = {Environment.TST1, Environment.PPE})
    public void createNewContract(Harness harness) {
        String contractId, queueId, dmsId;
        String orderId = null;

        //1. SAVE-CALC
        jsonBody = "{\n" +
                "    \"author\": { \"name\": \"" + calculation.getMainClient().getFirstName() + " " + calculation.getMainClient().getLastName() + "\", \"role\": \"client\"},\n" +
                "    \"calc\": {\n" +
                "        \"callContext\": \"" + calculation.getCallContext() + "\",\n" +
                "        \"channel\": \"" + calculation.getChannel() + "\",\n" +
                "        \"creditWorthinessVariant\": \"" + calculation.getCreditWorthinessVariant() + "\",\n" +
                "        \"fixation\": " + calculation.getFixation() + ",\n" +
                "        \"households\": [\n" +
                "            {\n" +
                "                \"persons\": [{\n" +
                "                        \"addresses\":[],\n" +
                "                        \"cellphone\":\"420721559695\",\n" +
                "                        \"email\": \"jan.danhel@moneta.cz\",\n" +
                "                        \"expenditures\": [{\n" +
                "                            \"correction\": \"refunding\",\n" +
                "                            \"installmentAmount\": " + calculation.getInstallmentAmount() + ",\n" +
                "                            \"type\": \"loan_mortgage\"\n" +
                "                        },\n" +
                "                        {\"installmentAmount\": " + calculation.getMainClient().getInstalmentAmountOther() + ", \"type\": \"other\"}],\n" +
                "                        \"firstName\": \"" + calculation.getMainClient().getFirstName() + "\",\n" +
                "                        \"lastName\": \"" + calculation.getMainClient().getLastName() + "\",\n" +
                "                        \"identities\": [],\n" +
                "                        \"incomes\":{\n" +
                "                            \"other\":[{\n" +
                "                                \"amount\": " + calculation.getMainClient().getIncome() + ",\n" +
                "                                \"type\": \"unspecified\"\n" +
                "                            }]\n" +
                "                        },\n" +
                "                        \"lifeInsuranceVariant\": \"" + calculation.getLifeInsuranceVariant() + "\",\n" +
                "                        \"pin\": \"" + calculation.getMainClient().getPin() + "\"\n" +
                "                }],\n" +
                "                \"personsWithIncome\": " + calculation.getPersonsWithIncome() + ",\n" +
                "                \"personsWithoutIncome\": " + calculation.getPersonsWithoutIncome() + "\n" +
                "            }\n" +
                "        ],\n" +
                "        \"loanAmount\": " + calculation.getLoanAmount() + ",\n" +
                "        \"maturity\": " + calculation.getMaturity() + ",\n" +
                "        \"offsetType\": \"" + calculation.getOffsetType() + "\",\n" +
                "        \"product\": " + calculation.getProduct() + ",\n" +
                "        \"purposes\": [\n" +
                "            {\n" +
                "                \"amount\": " + calculation.getLoanAmount() + ",\n" +
                "                \"id\": " + calculation.getPurposesID() + "\n" +
                "            }\n" +
                "        ],\n" +
                "        \"realties\": [\n" +
                "            {\n" +
                "                \"contractRelationType\": \"pledge_loan_object\",\n" +
                "                \"price\": 1428730,\n" +
                "                \"realtyType\": \"not_available\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"source\": \"web_online\"\n" +
                "    }\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/save-calc/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true")
                .andAssertPresent("contract");
        harness.log(Level.DEBUG, response.extract("").toString());
        contractId = response.extract("contract").asText();
        harness.log("contractId: " + contractId);

        //2.STORE_CONSENTS
        jsonBody = "{\n" +
                "  \"consents\": [\n" +
                "    {\n" +
                "      \"consentType\": \"mandatory\",\n" +
                "      \"pin\": \"" + calculation.getMainClient().getPin() + "\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"contract\": " + contractId + "\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/store_consents/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //3.CREATE_CONTRACT
        jsonBody = "{\n" +
                "  \"contract\": " + contractId + "\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/create_contract/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //GET_HYP_APPROVAL_RSLT_PRESCORING
        for (int i = 0; i <= maxRequestLoops; i++) {
            jsonBody = "{\n" +
                    "  \"HYPOS_ID\": " + contractId + "\n" +
                    "}";
            response = harness.withWso2()
                    .prepareRequest()
                    .withPath("/t/risk.cluster/NAS/GET_HYP_APPROVAL_RSLT_004/001")
                    .withHeader("Authorization", tokenMortgages)
                    .withHeader("Accept", "application/json")
                    .withHeader("USERID_L2", "HYPREFI")
                    .withPayload(jsonBody)
                    .post()
                    .andAssertStatus(200);
            harness.log(Level.DEBUG, response.extract("").toString());

            if (response.extract("APPL_STATUS").asInt() == 8 && response.extract("tifResponseStatus.resultCode").asInt() == 0) {
                if (containsNasResponseError(response)) {
                    harness.log(Level.ERROR, "NAS response: " + response.extract("RS_RSN"));
                    Assertions.fail("Response contained error message. Stopping test execution.");
                    break;
                } else {
                    harness.log("Prescoring finished successfully. Continuing with test.");
                    break;
                }
            } else {
                try {
                    if (i == maxRequestLoops) {
                        harness.log(Level.ERROR, String.format("Prescoring result was not set after %s calls. Stopping test execution.", maxRequestLoops));
                        Assertions.fail();
                    }
                    harness.log(String.format("Does not have prescoring result yet. Waiting for next request in %ss.", delayTimeout / 1000));
                    Thread.sleep(delayTimeout);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        //4.CONTRACT-STATE-SIMPLE
        for (int i = 0; i <= maxRequestLoops; i++) {
            jsonBody = "{\n" +
                    "  \"contract\": " + contractId + "\n" +
                    "}";
            response = harness.withWso2()
                    .prepareRequest()
                    .withPath("/t/mortgages.cluster/contract-state-simple/001")
                    .withHeader("Authorization", tokenMortgages)
                    .withHeader("Accept", "application/json")
                    .withPayload(jsonBody)
                    .post()
                    .andAssertStatus(200);
            harness.log(Level.DEBUG, response.extract("").toString());
            harness.log("Contract state: " + response.extract("state").asText());
            if (response.extract("state").asText().equals("stt_obchod_zalozen_kontrola_ok")) {
                harness.log("Contract state is stt_obchod_zalozen_kontrola_ok. Continuing with test.");
                break;
            } else {
                try {
                    if (i == maxRequestLoops) {
                        harness.log(Level.ERROR, String.format("Scoring result was not set after %s calls. Stopping test execution.", maxRequestLoops));
                        Assertions.fail();
                    }
                    harness.log(String.format("Does not have scoring result yet. Waiting for next request in %ss.", delayTimeout / 1000));
                    Thread.sleep(delayTimeout);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        //5.SAVE-CALC
        jsonBody = "{\n" +
                "    \"author\": { \"name\": \"" + calculation.getMainClient().getFirstName() + " " + calculation.getMainClient().getLastName() + "\", \"role\": \"client\"},\n" +
                "    \"calc\": {\n" +
                "        \"callContext\": \"" + calculation.getCallContext() + "\",\n" +
                "        \"channel\": \"" + calculation.getChannel() + "\",\n" +
                "        \"creditWorthinessVariant\": \"" + calculation.getCreditWorthinessVariant() + "\",\n" +
                "        \"fixation\": " + calculation.getFixation() + ",\n" +
                "        \"households\": [\n" +
                "            {\n" +
                "                \"persons\": [{\n" +
                "                        \"addresses\":[],\n" +
                "                        \"cellphone\":\"420721559695\",\n" +
                "                        \"email\": \"jan.danhel@moneta.cz\",\n" +
                "                        \"expenditures\": [{\n" +
                "                            \"correction\": \"refunding\",\n" +
                "                            \"installmentAmount\": " + calculation.getInstallmentAmount() + ",\n" +
                "                            \"type\": \"loan_mortgage\"\n" +
                "                        },\n" +
                "                        {\"installmentAmount\": " + calculation.getMainClient().getInstalmentAmountOther() + ", \"type\": \"other\"}],\n" +
                "                        \"firstName\": \"" + calculation.getMainClient().getFirstName() + "\",\n" +
                "                        \"lastName\": \"" + calculation.getMainClient().getLastName() + "\",\n" +
                "                        \"identities\": [],\n" +
                "                        \"incomes\":{\n" +
                "                            \"other\":[{\n" +
                "                                \"amount\": " + calculation.getMainClient().getIncome() + ",\n" +
                "                                \"type\": \"unspecified\"\n" +
                "                            }]\n" +
                "                        },\n" +
                "                        \"lifeInsuranceVariant\": \"" + calculation.getLifeInsuranceVariant() + "\",\n" +
                "                        \"pin\": \"" + calculation.getMainClient().getPin() + "\"\n" +
                "                }],\n" +
                "                \"personsWithIncome\": " + calculation.getPersonsWithIncome() + ",\n" +
                "                \"personsWithoutIncome\": " + calculation.getPersonsWithoutIncome() + "\n" +
                "            }\n" +
                "        ],\n" +
                "        \"loanAmount\": " + calculation.getLoanAmount() + ",\n" +
                "        \"maturity\": " + calculation.getMaturity() + ",\n" +
                "        \"offsetType\": \"" + calculation.getOffsetType() + "\",\n" +
                "        \"product\": " + calculation.getProduct() + ",\n" +
                "        \"purposes\": [\n" +
                "            {\n" +
                "                \"amount\": " + calculation.getLoanAmount() + ",\n" +
                "                \"id\": " + calculation.getPurposesID() + "\n" +
                "            }\n" +
                "        ],\n" +
                "        \"realties\": [\n" +
                "            {\n" +
                "                \"contractRelationType\": \"pledge_loan_object\",\n" +
                "                \"price\": 1428730,\n" +
                "                \"realtyType\": \"not_available\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"source\": \"web_online\"\n" +
                "    },\n" +
                "    \"collApplicabilityClient\": " + calculation.isCollApplicabilityClient() + ",\n" +
                "    \"collApplicabilityProcess\": " + calculation.isCollApplicabilityProcess() + ",\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"pledgeReferenceNumber\": \"" + calculation.getPledgeReferenceNumber() + "\"\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/save_calc/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //6.STORE_CONSENTS
        jsonBody = "{\n" +
                "  \"consents\": [\n" +
                "    {\n" +
                "      \"consentType\": \"secret_by_unsecured\",\n" +
                "      \"pin\": \"" + calculation.getMainClient().getPin() + "\"\n" +
                "    }\n" +
                "  ],\n" +
                "  \"contract\": " + contractId + "\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/store_consents/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //7.STORE-HEALTH-FORM-NEW
        jsonBody = "{\n" +
                "    \"channel\": \"" + calculation.getChannel() + "\",\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"pin\": \"" + calculation.getMainClient().getPin() + "\",\n" +
                "    \"practitionerContact\": \"Franta Zdravý, Praha 2\",\n" +
                "    \"questions\": [\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"aids\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"cancer\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"diabetes\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"disability\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"drugs\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"heart\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"liver_kidney\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"lungs\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"sclerosis\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"answer\": false,\n" +
                "            \"question\": \"sick_leave\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/store-health-form/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //8.INIT_ONLINE_VALUATION TST-NEW
        jsonBody = "{\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"redocs\": [\n" +
                "        {\n" +
                "            \"flats\": [\n" +
                "                {\n" +
                "                    \"isknId\": \"34376704\",\n" +
                "                    \"mainUnit\": true,\n" +
                "                    \"metadata\": [\n" +
                "                        {\n" +
                "                            \"name\": \"building_no\",\n" +
                "                            \"value\": \"2340\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"building_town\",\n" +
                "                            \"value\": \"Břeclav\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"building_street\",\n" +
                "                            \"value\": \"Fintajslova\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"building_post_code\",\n" +
                "                            \"value\": \"69002\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"lease_property\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"contact_person_name\",\n" +
                "                            \"value\": \"" + calculation.getMainClient().getFirstName() + "\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"building_construction_material\",\n" +
                "                            \"value\": \"brick\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"flag_control\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"floor_area_other\",\n" +
                "                            \"value\": \"0.00\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"land_area\",\n" +
                "                            \"value\": \"0.00\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"upstairs_floor\",\n" +
                "                            \"value\": \"7\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"flat_floor_area\",\n" +
                "                            \"value\": \"80.00\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"contact_person_surname\",\n" +
                "                            \"value\": \"" + calculation.getMainClient().getLastName() + "\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"construction_year_building\",\n" +
                "                            \"value\": \"2011\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"contact_person_role\",\n" +
                "                            \"value\": \"loan_applicant\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"contact_person_telephone\",\n" +
                "                            \"value\": \"+420718091553\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"flat_type\",\n" +
                "                            \"value\": \"flat\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"flat_placing\",\n" +
                "                            \"value\": \"other\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"garage_flat\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"parking_place_outside\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"parking_place_inside\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"floor_area\",\n" +
                "                            \"value\": \"80.00\"\n" +
                "                        }\n" +
                "                    ],\n" +
                "                    \"pricemapData\": [\n" +
                "                        {\n" +
                "                            \"name\": \"valuation_number\",\n" +
                "                            \"value\": \"MONETA-REFI-TST-2019-000441\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"number_of_prices\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"floorspace_from_cm\",\n" +
                "                            \"value\": \"57.70\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"number_of_parking_cm\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"segment\",\n" +
                "                            \"value\": \"Ostatni vystavba\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"building_type\",\n" +
                "                            \"value\": \"Panelovy dum\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"url_edit\",\n" +
                "                            \"value\": \"https://www.cenovamapa.org/valuator/?ValuatorID=91585&ValuatorMode=2\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"url_readonly\",\n" +
                "                            \"value\": \"CM2\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"allow_change\",\n" +
                "                            \"value\": \"1\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"is_error\",\n" +
                "                            \"value\": \"0\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"name\": \"valuator_id\",\n" +
                "                            \"value\": \"91585\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            ],\n" +
                "            \"lands\": [\n" +
                "                {\n" +
                "                    \"isknId\": \"2764717704\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"request\": 24988,\n" +
                "            \"shares\": [\n" +
                "                {\n" +
                "                    \"isknId\": \"1067590704\",\n" +
                "                    \"type\": \"community_property\"\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    ],\n" +
                "    \"valuation\": {\n" +
                "        \"automaticApprovalEligible\": 1,\n" +
                "        \"floodArea\": false,\n" +
                "        \"processType\": \"online_verify\",\n" +
                "        \"propertyValue\": 90000000,\n" +
                "        \"reproductionPropertyValue\": 80000000\n" +
                "    }\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/init_online_valuation/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());
        queueId = response.extract("queueId").asText();
        harness.log("queueId: " + queueId);

        //9.VALUATION-ORDER-STATE
        for (int i = 0; i <= maxRequestLoops; i++) {
            jsonBody = "{\n" +
                    "  \"contract\": " + contractId + ",\n" +
                    "  \"queueId\": " + queueId + "\n" +
                    "}";
            response = harness.withWso2()
                    .prepareRequest()
                    .withPath("/t/mortgages.cluster/valuation-order-state/v1")
                    .withHeader("Authorization", tokenMortgages)
                    .withHeader("Accept", "application/json")
                    .withHeader("USERID_L2", "HYPREFI")
                    .withPayload(jsonBody)
                    .post()
                    .andAssertStatus(200);
            harness.log(Level.DEBUG, response.extract("").toString());
            if (response.extract("queueState").asText().equals("done")) {
                orderId = response.andAssertPresent("order").extract("order.id").asText();
                break;
            } else if (response.extract("queueState").asText().equals("error")) {
                harness.log(Level.ERROR, "valuation-order-state returned an error: " + response.extract(""));
                Assertions.fail("Response contained error message. Stopping test execution.");
                break;
            } else {
                try {
                    if (i == maxRequestLoops) {
                        harness.log(Level.ERROR, String.format("Scoring result was not set after %s calls. Stopping test execution.", maxRequestLoops));
                        Assertions.fail();
                    }
                    harness.log(String.format("Does not have scoring result yet. Waiting for next request in %ss.", delayTimeout / 1000));
                    Thread.sleep(delayTimeout);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        //10.CreateItem
        jsonBody = "{\n" +
                "    \"binary_name\": \"nemovitost_foto_Loznice.jpg\",\n" +
                "    \"MIMEType\": \"image/jpeg\",\n" +
                "    \"originalFileName\": \"Loznice.jpg\",\n" +
                "    \"mtomRef\": \"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAPABQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9fP25/jB8cfgD/wAIv4y+GHgLw/8AE7wF4f8Atd18QvDcEk48ZX1n+4EL6CgItp7iJTczPbzkNcCFIYSskgIz/wDgnt+29qv/AAUA0TUviJomh6fa/BfVrR08Hawkztda3cWuv6/p11JIknlyxRvaWGk3KRvbxmM30sZeUxnZ2H7Yv7Lt5+2J4Eh8A6n4l/sf4Za9uh8a6bYwXUGr+IrMSQyLYwahDdRfY7eby5ILpTDM09tcSxI1ux82tD4T/sh+DfgP8ZfGHjPwXDqHhmT4iXcuq+KNGs7o/wBja1qjiBf7UNq4ZLe82QsryW3ki5MzvcrcSJC8QB6hRRRQB//Z\"\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/dms-hypos/1.0/CreateItem")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("createStatus", "true");
        harness.log(Level.DEBUG, response.extract("").toString());
        dmsId = response.extract("dmsId").asText();
        harness.log("dmsId: " + dmsId);

        //11.APPRAISEMENT-ORDER-ATTACH-FILE
        jsonBody = "{\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"dmsId\": " + dmsId + ",\n" +
                "    \"name\": \"nemovitost_foto_Loznice.jpg\",\n" +
                "    \"orderId\": " + orderId + "\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/appraisement-order-attach-file/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //12.APPRAISEMENT-ORDER-ENQUEUE
        jsonBody = "{\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"orderId\": " + orderId + "\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/appraisement-order-enqueue/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //13.SAVE_CALC
        jsonBody = "{\n" +
                "    \"author\": { \"name\": \"" + calculation.getMainClient().getFirstName() + " " + calculation.getMainClient().getLastName() + "\", \"role\": \"client\"},\n" +
                "    \"bankName\": \"" + calculation.getBankName() + "\",\n" +
                "    \"calc\": {\n" +
                "        \"callContext\": \"" + calculation.getCallContext() + "\",\n" +
                "        \"channel\": \"" + calculation.getChannel() + "\",\n" +
                "        \"creditWorthinessVariant\": \"" + calculation.getCreditWorthinessVariant() + "\",\n" +
                "        \"fixation\": " + calculation.getFixation() + ",\n" +
                "        \"households\": [\n" +
                "            {\n" +
                "                \"persons\": [{\n" +
                "                    \"addresses\":[{\n" +
                "                                \"descriptiveNumber\": \"661\",\n" +
                "                                \"orientationNumber\": \"42\",\n" +
                "                                \"postCode\": \"16900\",\n" +
                "                                \"street\": \"Hošťálkova\",\n" +
                "                                \"town\": \"Praha\",\n" +
                "                                \"townPart\": \"Břevnov\",\n" +
                "                                \"type\": \"permanent\"\n" +
                "                            },\n" +
                "                            {\n" +
                "                                \"descriptiveNumber\": \"661\",\n" +
                "                                \"orientationNumber\": \"42\",\n" +
                "                                \"postCode\": \"16900\",\n" +
                "                                \"street\": \"Hošťálkova\",\n" +
                "                                \"town\": \"Praha\",\n" +
                "                                \"townPart\": \"Břevnov\",\n" +
                "                                \"type\": \"temporary\"\n" +
                "                            },\n" +
                "                            {\n" +
                "                                \"descriptiveNumber\": \"661\",\n" +
                "                                \"orientationNumber\": \"42\",\n" +
                "                                \"postCode\": \"16900\",\n" +
                "                                \"street\": \"Hošťálkova\",\n" +
                "                                \"town\": \"Praha\",\n" +
                "                                \"townPart\": \"Břevnov\",\n" +
                "                                \"type\": \"real_stay\"\n" +
                "                            }],\n" +
                "                        \"cellphone\":\"420721559695\",\n" +
                "                        \"email\": \"jan.danhel@moneta.cz\",\n" +
                "                        \"expenditures\": [{\n" +
                "                            \"correction\": \"refunding\",\n" +
                "                            \"installmentAmount\": " + calculation.getInstallmentAmount() + ",\n" +
                "                            \"type\": \"loan_mortgage\"\n" +
                "                        },\n" +
                "                        {\"installmentAmount\": " + calculation.getMainClient().getInstalmentAmountOther() + ", \"type\": \"other\"}],\n" +
                "                        \"firstName\": \"" + calculation.getMainClient().getFirstName() + "\",\n" +
                "                        \"lastName\": \"" + calculation.getMainClient().getLastName() + "\",\n" +
                "                        \"identities\": [],\n" +
                "                        \"incomes\":{\n" +
                "                            \"other\":[{\n" +
                "                               \"amount\": " + calculation.getMainClient().getIncome() + ",\n" +
                "                               \"type\": \"unspecified\",\n" +
                "                               \"source\": \"main\"\n" +
                "                            }],\n" +
                "                            \"specification:{ \"isStable\": " + calculation.getMainClient().isStable() + ",\n" +
                "                                              \"noCollDependent\": " + calculation.getMainClient().isNoCollDependent() + ",\n" +
                "                                              \"noForeignCurrency\": " + calculation.getMainClient().isNoForeignCurrencyntIsStable() + "}\n" +
                "                         },\n" +
                "                        \"lifeInsuranceVariant\": \"" + calculation.getLifeInsuranceVariant() + "\",\n" +
                "                        \"maritalStatus\": \"" + calculation.getMainClient().getMaritalStatus() + "\",\n" +
                "                        \"nationality\": " + calculation.getMainClient().getNationality() + ",\n" +
                "                        \"pin\": \"" + calculation.getMainClient().getPin() + "\"\n" +
                "                }],\n" +
                "                \"personsWithIncome\": " + calculation.getPersonsWithIncome() + ",\n" +
                "                \"personsWithoutIncome\": " + calculation.getPersonsWithoutIncome() + "\n" +
                "            }\n" +
                "        ],\n" +
                "        \"loanAmount\": " + calculation.getLoanAmount() + ",\n" +
                "        \"maturity\": " + calculation.getMaturity() + ",\n" +
                "        \"offsetType\": \"" + calculation.getOffsetType() + "\",\n" +
                "        \"product\": " + calculation.getProduct() + ",\n" +
                "        \"purposes\": [\n" +
                "            {\n" +
                "                \"amount\": " + calculation.getLoanAmount() + ",\n" +
                "                \"id\": " + calculation.getPurposesID() + "\n" +
                "            }\n" +
                "        ],\n" +
                "        \"realties\": [\n" +
                "            {\n" +
                "                \"contractRelationType\": \"pledge_loan_object\",\n" +
                "                \"price\": 1428730,\n" +
                "                \"realtyType\": \"not_available\"\n" +
                "            }\n" +
                "        ],\n" +
                "       \"refinancingDate\": \"" + calculation.getRefinancingDate() + "\",\n" +
                "        \"source\": \"web_online\"\n" +
                "    },\n" +
                "    \"collApplicabilityClient\": " + calculation.isCollApplicabilityClient() + ",\n" +
                "    \"collApplicabilityProcess\": " + calculation.isCollApplicabilityProcess() + ",\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"pledgeReferenceNumber\": \"" + calculation.getPledgeReferenceNumber() + "\"\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/save-calc/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //14.SEND_TO_SCORING
        jsonBody = "{\n" +
                "  \"contract\": " + contractId + "\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/send_to_scoring/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //15.GET_HYP_APPROVAL_RSLT
        for (int i = 0; i <= maxRequestLoops; i++) {
            jsonBody = "{\n" +
                    "  \"HYPOS_ID\": " + contractId + "\n" +
                    "}";
            response = harness.withWso2()
                    .prepareRequest()
                    .withPath("/t/risk.cluster/NAS/GET_HYP_APPROVAL_RSLT_003/001")
                    .withHeader("Authorization", tokenMortgages)
                    .withHeader("Accept", "application/json")
                    .withHeader("USERID_L2", "HYPREFI")
                    .withPayload(jsonBody)
                    .post()
                    .andAssertStatus(200);
            harness.log(Level.DEBUG, response.extract("").toString());

            if (response.extract("APPL_STATUS").asInt() == 8 && response.extract("tifResponseStatus.resultCode").asInt() == 0) {
                if (containsNasResponseError(response)) {
                    harness.log(Level.ERROR, "NAS response: " + response.extract("RS_RSN"));
                    Assertions.fail("Response contained error message. Stopping test execution.");
                    break;
                } else {
                    harness.log("Scoring finished successfully. Continuing with test.");
                    break;
                }
            } else if (response.extract("APPL_STATUS").asInt() == 5 && response.extract("tifResponseStatus.resultCode").asInt() == 0) {
                if (containsNasResponseError(response)) {
                    harness.log(Level.ERROR, "NAS response: " + response.extract("RS_RSN"));
                    Assertions.fail("Response contained error message. Stopping test execution.");
                    break;
                } else {
                    harness.log("Scoring is missing client documents. Continuing with document import.");
                    importAndProcessDocuments(harness, contractId);
                }
            } else {
                try {
                    if (i == maxRequestLoops) {
                        harness.log(Level.ERROR, String.format("Scoring result was not set after %s calls. Stopping test execution.", maxRequestLoops));
                        Assertions.fail();
                    }
                    harness.log(String.format("Does not have scoring result yet. Waiting for next request in %ss.", delayTimeout / 1000));
                    Thread.sleep(delayTimeout);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        //21.GET_HYP_APPROVAL_RSLT
        for (int i = 0; i <= maxRequestLoops; i++) {
            jsonBody = "{\n" +
                    "  \"HYPOS_ID\": " + contractId + "\n" +
                    "}";
            response = harness.withWso2()
                    .prepareRequest()
                    .withPath("/t/risk.cluster/NAS/GET_HYP_APPROVAL_RSLT_003/001")
                    .withHeader("Authorization", tokenMortgages)
                    .withHeader("Accept", "application/json")
                    .withHeader("USERID_L2", "HYPREFI")
                    .withPayload(jsonBody)
                    .post()
                    .andAssertStatus(200);
            harness.log(Level.DEBUG, response.extract("").toString());

            if (response.extract("APPL_STATUS").asInt() == 8) {
                harness.log("Scoring finished successfully.");
                break;
            } else {
                try {
                    if (i == maxRequestLoops) {
                        harness.log(Level.ERROR, String.format("Scoring result was not set after %s calls. Stopping test execution.", maxRequestLoops));
                        Assertions.fail();
                    }
                    harness.log(String.format("Does not have scoring result yet. Waiting for next request in %ss.", delayTimeout / 1000));
                    Thread.sleep(delayTimeout);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void importAndProcessDocuments(Harness harness, String contractId) {
        //16.CreateItem
        jsonBody = "{\n" +
                "    \"H_kategorie_dok\": \"1\",\n" +
                "    \"H_typ_dok\": \"96\",\n" +
                "    \"IDHypo\": " + contractId + ",\n" +
                "    \"H_nazev_dok\": \"Výpis z BÚ - Příjem ze zaměstnání.jpg\",\n" +
                "    \"MIMEType\": \"image/jpeg\",\n" +
                "    \"originalFileName\": \"prijmy.jpg\",\n" +
                "    \"mtomRef\": \"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAPABQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9fP25/jB8cfgD/wAIv4y+GHgLw/8AE7wF4f8Atd18QvDcEk48ZX1n+4EL6CgItp7iJTczPbzkNcCFIYSskgIz/wDgnt+29qv/AAUA0TUviJomh6fa/BfVrR08Hawkztda3cWuv6/p11JIknlyxRvaWGk3KRvbxmM30sZeUxnZ2H7Yv7Lt5+2J4Eh8A6n4l/sf4Za9uh8a6bYwXUGr+IrMSQyLYwahDdRfY7eby5ILpTDM09tcSxI1ux82tD4T/sh+DfgP8ZfGHjPwXDqHhmT4iXcuq+KNGs7o/wBja1qjiBf7UNq4ZLe82QsryW3ki5MzvcrcSJC8QB6hRRRQB//Z\"\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/dms-hypos/1.0/CreateItem")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("createStatus", "true");
        harness.log(Level.DEBUG, response.extract("").toString());
        String dmsId1 = response.extract("dmsId").asText();

        //17.IMPORT_DOCUMENT
        jsonBody = "{\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"dmsId\": \"" + dmsId1 + "\",\n" +
                "    \"documentName\": \"Výpis z BÚ - Příjem ze zaměstnání.jpg\",\n" +
                "    \"scanType\": \"r_vypisy_z_uctu_vyplatni_pasky\",\n" +
                "    \"template\": \"digital_folder\",\n" +
                "    \"comment\": \"Komentář k souboru a nejaky text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text, text\",\n" +
                "    \"author\": { \"name\": \"" + calculation.getMainClient().getFirstName() + " " + calculation.getMainClient().getLastName() + "\", \"role\": \"client\"}\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/import_document/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //18.CreateItem 2
        jsonBody = "{\n" +
                "    \"H_kategorie_dok\": \"1\",\n" +
                "    \"H_typ_dok\": \"96\",\n" +
                "    \"IDHypo\": " + contractId + ",\n" +
                "    \"H_nazev_dok\": \"PoP.jpg\",\n" +
                "    \"MIMEType\": \"image/jpeg\",\n" +
                "    \"originalFileName\": \"PoP.jpg\",\n" +
                "    \"mtomRef\": \"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAPABQDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9fP25/jB8cfgD/wAIv4y+GHgLw/8AE7wF4f8Atd18QvDcEk48ZX1n+4EL6CgItp7iJTczPbzkNcCFIYSskgIz/wDgnt+29qv/AAUA0TUviJomh6fa/BfVrR08Hawkztda3cWuv6/p11JIknlyxRvaWGk3KRvbxmM30sZeUxnZ2H7Yv7Lt5+2J4Eh8A6n4l/sf4Za9uh8a6bYwXUGr+IrMSQyLYwahDdRfY7eby5ILpTDM09tcSxI1ux82tD4T/sh+DfgP8ZfGHjPwXDqHhmT4iXcuq+KNGs7o/wBja1qjiBf7UNq4ZLe82QsryW3ki5MzvcrcSJC8QB6hRRRQB//Z\"\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/dms-hypos/1.0/CreateItem")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("createStatus", "true");
        harness.log(Level.DEBUG, response.extract("").toString());
        String dmsId2 = response.extract("dmsId").asText();

        //19.IMPORT_DOCUMENT 2
        jsonBody = "{\n" +
                "    \"contract\": " + contractId + ",\n" +
                "    \"dmsId\": \"" + dmsId2 + "\",\n" +
                "    \"documentName\": \"PoP.jpg\",\n" +
                "    \"scanType\": \"r_vypisy_z_uctu_vyplatni_pasky\",\n" +
                "    \"template\": \"digital_folder\",\n" +
                "    \"comment\": \"Komentář k souboru 2\",\n" +
                "    \"author\": { \"name\": \"" + calculation.getMainClient().getFirstName() + " " + calculation.getMainClient().getLastName() + "\", \"role\": \"client\"}\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/import_document/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());

        //20.process-incomplete-docs
        jsonBody = "{\n" +
                "  \"clientsDocs\": [{docs: [" + dmsId1 + ", " + dmsId2 + "], \"pin\": \"" + calculation.getMainClient().getPin() + "\"}],\n" +
                "  \"contract\": " + contractId + "\n" +
                "}";
        response = harness.withWso2()
                .prepareRequest()
                .withPath("/t/mortgages.cluster/process-incomplete-docs/001")
                .withHeader("Authorization", tokenMortgages)
                .withHeader("Accept", "application/json")
                .withPayload(jsonBody)
                .post()
                .andAssertStatus(200)
                .andAssertFieldValue("result.status", "true");
        harness.log(Level.DEBUG, response.extract("").toString());
    }

    private Boolean containsNasResponseError(RawRestRequest.Response response) {
        boolean containsError = false;
        List<RsRsn> rsRsnList = response.mapResponseIntoObject(NasApprovalResponse.class).getRsRsnList();
        if (!rsRsnList.isEmpty()) {
            for (RsRsn rsRsn : rsRsnList) {
                if (rsRsn.getDetReason().equals("KO")
                        || rsRsn.getDetReason().equals("externalSystemsUnavailable")
                        || rsRsn.getDetReason().equals("bonityKO")) {
                    containsError = true;
                    break;
                }
            }
        }
        return containsError;
    }
}
