package cz.moneta.test.system.messaging.kafka;

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.endpoints.cds.CdsEndpoint;
import org.jooq.Record;
import org.jooq.Result;
import org.junit.jupiter.api.Assertions;

import java.util.UUID;

@TestScenario(name = "Test Kafka pruducent and select from cds database")
public class KafkaTestsDb {

    @TestCase(name = "Send message to topic with additional header")
    public void sendToKafkaReadFromDatabase(Harness harness) throws InterruptedException {
        String activityId = UUID.randomUUID().toString();

//      key -> card number
        harness.withKafka()
                .toTopic("TP_CVM_CARD-AUTHORIZATION-NOTIFICATION_STREAM_APP")
                .withKey("0001234567890123456")
                .withPayloadFromFile("system/kafka/test-with-db.json")
                .withHeader("requestId", "c3e2d840d4c4d6f14040404040404040e254359aa23fad11")
                .withHeader("activityId", activityId)
                .withHeader("traceparent", "00-c3e2d840d4c4d6f14040404040404040-e254359aa23fad11-01")
                .withHeader("sourceCodebookId", "KOD_ODS")
                .send();

        Thread.sleep(1000);

        String sql = "SELECT * FROM SAS_RTDM_OWNER.SASRT_CDSS_TRIGGER_LOG WHERE ACTIVITY_ID = '"+ activityId + "'";
        Result<Record> resultList = harness.getEndpoint(CdsEndpoint.class).executeSql(sql);
        Assertions.assertEquals(activityId, resultList.getValue(0, "ACTIVITY_ID"));
    }
}
