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

import java.util.concurrent.TimeUnit;

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.imq.ImqFirstVisionQueue;

/**
 * Unit tests for IBM MQ First Vision DSL methods.
 */
@TestScenario(name = "IBM MQ First Vision DSL Tests")
public class ImqFirstVisionTest {

	@TestCase(name = "Send and receive JSON message")
	public void sendJsonMessage(Harness harness) {
		harness.withImqFirstVision().toQueue(ImqFirstVisionQueue.PAYMENT_NOTIFICATIONS).asJson()
				.withPayload("{\"paymentId\": \"PAY-456\", \"result\": \"OK\"}").send();

		harness.withImqFirstVision().fromQueue(ImqFirstVisionQueue.PAYMENT_NOTIFICATIONS).asJson()
				.receiveWhere(msg -> msg.extract("paymentId").equals("PAY-456")).withTimeout(10, TimeUnit.SECONDS)
				.andAssertFieldValue("result", "OK");

	}

	@TestCase(name = "Send and receive XML message")
	public void sendXmlMessage(Harness harness) {
		harness.withImqFirstVision().toQueue(ImqFirstVisionQueue.MF_REQUESTS).asXml()
				.withPayload("<request><accountId>12345</accountId><balance>50000</balance></request>").send();
		harness.withImqFirstVision().fromQueue(ImqFirstVisionQueue.MF_RESPONSES).asXml()
				.receiveWhere(msg -> msg.extract("/request/accountId").equals("12345"))
				.withTimeout(15, TimeUnit.SECONDS).andAssertFieldValue("/request/balance", "50000");

	}

	@TestCase(name = "Send and receive UTF-8 message as bytes")
	public void sendUtf8BytesMessage(Harness harness) {
		harness.withImqFirstVision().toQueue(ImqFirstVisionQueue.MF_UTF8).asUtf8().withPayload("DATA|12345|ÚČET|CZK")
				.send();
		harness.withImqFirstVision().fromQueue(ImqFirstVisionQueue.MF_UTF8).asUtf8()
				.receiveWhere(msg -> msg.getBody().contains("12345")).withTimeout(15, TimeUnit.SECONDS);
	}

	@TestCase(name = "Send and receive EBCDIC message as bytes")
	public void sendEbcdicBytesMessage(Harness harness) {
		harness.withImqFirstVision().toQueue(ImqFirstVisionQueue.MF_EBCDIC).asEbcdic()
				.withPayload("PŘIKAZ|12345|ZŮSTATEK|CZK").send();
		harness.withImqFirstVision().fromQueue(ImqFirstVisionQueue.MF_EBCDIC).asEbcdic()
				.receiveWhere(msg -> msg.getBody().contains("12345")).withTimeout(15, TimeUnit.SECONDS);
	}
}
