From fa323be8046009d77deb35b9eec583bcfb313579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Sat, 16 Nov 2024 16:53:40 +0100 Subject: [PATCH] feature/Add testcontainers RabbitMQ --- obp-api/pom.xml | 10 ++- .../code/container/EmbeddedRabbitMQ.scala | 62 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 obp-api/src/test/scala/code/container/EmbeddedRabbitMQ.scala diff --git a/obp-api/pom.xml b/obp-api/pom.xml index 6383a47d4..17da5d4ad 100644 --- a/obp-api/pom.xml +++ b/obp-api/pom.xml @@ -499,7 +499,15 @@ amqp-client 5.22.0 - + + + + org.testcontainers + rabbitmq + 1.20.3 + test + + diff --git a/obp-api/src/test/scala/code/container/EmbeddedRabbitMQ.scala b/obp-api/src/test/scala/code/container/EmbeddedRabbitMQ.scala new file mode 100644 index 000000000..1c88cb578 --- /dev/null +++ b/obp-api/src/test/scala/code/container/EmbeddedRabbitMQ.scala @@ -0,0 +1,62 @@ +package code.container + +import code.api.v5_0_0.V500ServerSetup +import code.setup.DefaultUsers +import com.rabbitmq.client.{Channel, Connection, ConnectionFactory} +import org.scalatest.Ignore +import org.testcontainers.containers.RabbitMQContainer + +@Ignore +class EmbeddedRabbitMQ extends V500ServerSetup with DefaultUsers { + + val rabbitMQContainer = new RabbitMQContainer("rabbitmq:3.7.25-management-alpine") + + override def beforeAll(): Unit = { + super.beforeAll() + // Start RabbitMQ container + rabbitMQContainer.start() + } + + override def afterAll(): Unit = { + super.afterAll() + rabbitMQContainer.stop() + } + + feature(s"test EmbeddedRabbitMQ") { + scenario("Publish and Consume Message") { + + val rabbitHost = rabbitMQContainer.getHost + val rabbitPort = rabbitMQContainer.getAmqpPort + + // Set up RabbitMQ connection + val factory = new ConnectionFactory() + factory.setHost(rabbitHost) + factory.setPort(rabbitPort) + + val connection: Connection = factory.newConnection() + val channel: Channel = connection.createChannel() + + // Declare a queue + val queueName = "test-queue" + channel.queueDeclare(queueName, false, false, false, null) + + // Publish a message + val message = "Hello, RabbitMQ!" + channel.basicPublish("", queueName, null, message.getBytes) + println(s"Published message: $message") + + // Consume the message + val delivery = channel.basicGet(queueName, true) + val consumedMessage = new String(delivery.getBody) + + println(s"Consumed message: $consumedMessage") + consumedMessage shouldBe message + + // Clean up + channel.close() + connection.close() + + } + } + +} \ No newline at end of file