package cz.moneta.test.system.messaging;

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.support.util.Template;

@TestScenario(name = "Send MQ message")
public class SendMQMessage {

	@TestCase(name = "Send JSON message")
	public void sendJsonMessage(Harness harness) {

		harness.withMessaging().to("payment-notifications").asJson()
				.withPayload("{\"paymentId\": \"PAY-456\", \"result\": \"OK\"}").send();

		harness.withMessaging().from("payment-notifications").asJson()
				.receiveWhere(msg -> msg.extract("paymentId").equals("PAY-456")).withTimeout(5, TimeUnit.SECONDS)
				.andAssertFieldValue("result", "OK");
	}

	@TestCase(name = "Send XML message")
	public void sendXmlMessage(Harness harness) {

		harness.withMessaging().to("mainframe-requests").asXml()
				.withPayload("<request><accountId>12345</accountId><balance>50000</balance></request>").send();

		harness.withMessaging().to("mainframe-requests").asXml()
				.withPayloadFromTemplate(Template.fromFilepath("messaging/mf-request.xml").set("accountId", "12345"))
				.send();

		harness.withMessaging().from("mainframe-requests").asXml()
				.receiveWhere(msg -> msg.extract("/request/accountId").equals("12345"))
				.withTimeout(5, TimeUnit.SECONDS).andAssertFieldValue("/request/balance", "50000");
	}

	@TestCase(name = "Send UTF8 message")
	public void sendUtf8Message(Harness harness) {

		harness.withMessaging().to("mainframe-utf8-queue").asUtf8().withPayload("DATA|12345|ÚČET|CZK").send();

		harness.withMessaging().from("mainframe-utf8-queue").asUtf8()
				.receiveWhere(msg -> msg.getBody().contains("12345")).withTimeout(5, TimeUnit.SECONDS);
	}
	
	@TestCase(name = "Send EBCDIC message")
	public void sendEbcdicMessage(Harness harness) {

		harness.withMessaging().to("mainframe-ebcdic-queue").asEbcdic().withPayload("DATA|12345|ÚČET|CZK").send();

		harness.withMessaging().from("mainframe-ebcdic-queue").asEbcdic()
				.receiveWhere(msg -> msg.getBody().contains("12345")).withTimeout(5, TimeUnit.SECONDS);
	}

}
