diff --git a/build.sbt b/build.sbt index 2a1dc18b..47482d9e 100644 --- a/build.sbt +++ b/build.sbt @@ -406,7 +406,7 @@ lazy val modulePulsar = (project in file("modules/pulsar")) ) lazy val moduleRabbitmq = (project in file("modules/rabbitmq")) - .dependsOn(core % "compile->compile;test->test;provided->provided") + .dependsOn(core % "compile->compile;test->test;provided->provided", scalatest % "test->test") .settings(commonSettings: _*) .settings( name := "testcontainers-scala-rabbitmq", diff --git a/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala b/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala index 95311fb7..2e55d2c6 100644 --- a/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala +++ b/modules/rabbitmq/src/main/scala/com/dimafeng/testcontainers/RabbitMQContainer.scala @@ -30,6 +30,15 @@ case class RabbitMQContainer( c.withAdminPassword(adminPassword) + vhosts.foreach { + case RabbitMQContainer.VHost(name, Some(tracing)) => c.withVhost(name, tracing) + case RabbitMQContainer.VHost(name, None) => c.withVhost(name) + } + + vhostsLimits.foreach { x => + c.withVhostLimit(x.vhost, x.name, x.value) + } + queues.foreach { x => c.withQueue(x.name, x.autoDelete, x.durable, toJavaArguments(x.arguments)) } @@ -51,15 +60,6 @@ case class RabbitMQContainer( if (x.tags.isEmpty) c.withUser(x.name, x.password) else c.withUser(x.name, x.password, x.tags.asJava) } - vhosts.foreach { - case RabbitMQContainer.VHost(name, Some(tracing)) => c.withVhost(name, tracing) - case RabbitMQContainer.VHost(name, None) => c.withVhost(name) - } - - vhostsLimits.foreach { x => - c.withVhostLimit(x.vhost, x.name, x.value) - } - operatorPolicies.foreach { x => c.withOperatorPolicy(x.name, x.pattern, toJavaArguments(x.definition), x.priority, x.applyTo) } diff --git a/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala new file mode 100644 index 00000000..5943d2bf --- /dev/null +++ b/modules/rabbitmq/src/test/scala/com/dimafeng/testcontainers/RabbitMQSpec.scala @@ -0,0 +1,110 @@ +package com.dimafeng.testcontainers + +import com.dimafeng.testcontainers.RabbitMQContainer.{Exchange, Permission, User, VHost} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import org.testcontainers.utility.DockerImageName +import sttp.client3.{HttpURLConnectionBackend, UriContext, basicRequest} + +import scala.util.Either + +class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers { + import RabbitMQSpec._ + + override val container: Container = MultipleContainers( + defaultRabbitContainer, customRabbitContainer + ) + + "Default Rabbit container" should "start" in { + val baseUri = defaultRabbitContainer.httpUrl + val request = + basicRequest + .auth.basic(testUsername, testPassword) + .get(uri"$baseUri/") + + val eitherContainerIsOnline = + request.send(httpClientBackend).body match { + case Right(_) => Right(true) + case e@Left(_) => e + } + + assertResult(Right(true))(eitherContainerIsOnline) + } + + + "Custom Rabbit container" should "start and load exchanges config" in { + val baseUri = customRabbitContainer.httpUrl + val request = + basicRequest + .auth.basic(testUsername, testPassword) + .get(uri"$baseUri/api/exchanges") + + val eitherExchangeWasLoaded = + request.send(httpClientBackend).body match { + case Right(v) => Right(v.contains(testExchange)) + case e@Left(_) => e + } + + assertResult(Right(true))(eitherExchangeWasLoaded) + } + + "Custom Rabbit container" should "start and load users config" in { + val baseUri = customRabbitContainer.httpUrl + val request = + basicRequest + .auth.basic(testUsername, testPassword) + .get(uri"$baseUri/api/users") + + val eitherUserWasLoaded = + request.send(httpClientBackend).body match { + case Right(v) => Right(v.contains(testUsername)) + case e@Left(_) => e + } + + assertResult(Right(true))(eitherUserWasLoaded) + } +} + +object RabbitMQSpec { + val testExchange = "test-exchange" + val testUsername = "test-user" + val testPassword = "test-password" + val httpClientBackend = HttpURLConnectionBackend() + + val defaultRabbitContainer: RabbitMQContainer = RabbitMQContainer() + val customRabbitContainer: RabbitMQContainer = RabbitMQContainer( + dockerImageName = DockerImageName.parse(RabbitMQContainer.defaultDockerImageName), + adminPassword = RabbitMQContainer.defaultAdminPassword, + queues = Seq.empty, + exchanges = Seq( + Exchange( + name = testExchange, + exchangeType = "direct", + arguments = Map.empty, + vhost = Some("test-vhost") + ) + ), + bindings = Seq.empty, + users = Seq( + User( + name = testUsername, + password = testPassword, + tags = Set("administrator") + ) + ), + vhosts = Seq(VHost(name = "test-vhost")), + vhostsLimits = Seq.empty, + operatorPolicies = Seq.empty, + policies = Seq.empty, + parameters = Seq.empty, + permissions = Seq( + Permission( + vhost = "test-vhost", + user = testUsername, + configure = ".*", + write = ".*", + read = ".*" + ) + ) + ) +} \ No newline at end of file diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 907f4fdd..8c2df629 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -31,6 +31,7 @@ object Dependencies { private val restAssuredVersion = "4.0.0" private val awsV1Version = "1.11.479" private val awsV2Version = "2.15.7" + private val sttpVersion = "3.3.13" val allOld = Def.setting( PROVIDED( @@ -234,6 +235,10 @@ object Dependencies { val moduleRabbitmq = Def.setting( COMPILE( "org.testcontainers" % "rabbitmq" % testcontainersVersion + ) ++ TEST( + "org.scalatest" %% "scalatest" % scalaTestVersion + ) ++ PROVIDED( + "com.softwaremill.sttp.client3" %% "core" % sttpVersion ) )