1
+ package com .dimafeng .testcontainers
2
+
3
+ import com .dimafeng .testcontainers .RabbitMQContainer .{Exchange , Permission , User , VHost }
4
+ import org .scalatest .flatspec .AnyFlatSpec
5
+ import org .scalatest .matchers .should .Matchers
6
+ import org .testcontainers .utility .DockerImageName
7
+ import sttp .client3 .{HttpURLConnectionBackend , UriContext , basicRequest }
8
+
9
+ import scala .util .Either
10
+
11
+ class RabbitMQSpec extends AnyFlatSpec with ForAllTestContainer with Matchers {
12
+ import RabbitMQSpec ._
13
+
14
+ override val container : Container = MultipleContainers (
15
+ defaultRabbitContainer, customRabbitContainer
16
+ )
17
+
18
+ " Default Rabbit container" should " start" in {
19
+ val baseUri = defaultRabbitContainer.httpUrl
20
+ val request =
21
+ basicRequest
22
+ .auth.basic(testUsername, testPassword)
23
+ .get(uri " $baseUri/ " )
24
+
25
+ val eitherContainerIsOnline =
26
+ request.send(httpClientBackend).body match {
27
+ case Right (_) => Right (true )
28
+ case e@ Left (_) => e
29
+ }
30
+
31
+ assertResult(Right (true ))(eitherContainerIsOnline)
32
+ }
33
+
34
+
35
+ " Custom Rabbit container" should " start and load exchanges config" in {
36
+ val baseUri = customRabbitContainer.httpUrl
37
+ val request =
38
+ basicRequest
39
+ .auth.basic(testUsername, testPassword)
40
+ .get(uri " $baseUri/api/exchanges " )
41
+
42
+ val eitherExchangeWasLoaded =
43
+ request.send(httpClientBackend).body match {
44
+ case Right (v) => Right (v.contains(testExchange))
45
+ case e@ Left (_) => e
46
+ }
47
+
48
+ assertResult(Right (true ))(eitherExchangeWasLoaded)
49
+ }
50
+
51
+ " Custom Rabbit container" should " start and load users config" in {
52
+ val baseUri = customRabbitContainer.httpUrl
53
+ val request =
54
+ basicRequest
55
+ .auth.basic(testUsername, testPassword)
56
+ .get(uri " $baseUri/api/users " )
57
+
58
+ val eitherUserWasLoaded =
59
+ request.send(httpClientBackend).body match {
60
+ case Right (v) => Right (v.contains(testUsername))
61
+ case e@ Left (_) => e
62
+ }
63
+
64
+ assertResult(Right (true ))(eitherUserWasLoaded)
65
+ }
66
+ }
67
+
68
+ object RabbitMQSpec {
69
+ val testExchange = " test-exchange"
70
+ val testUsername = " test-user"
71
+ val testPassword = " test-password"
72
+ val httpClientBackend = HttpURLConnectionBackend ()
73
+
74
+ val defaultRabbitContainer : RabbitMQContainer = RabbitMQContainer ()
75
+ val customRabbitContainer : RabbitMQContainer = RabbitMQContainer (
76
+ dockerImageName = DockerImageName .parse(RabbitMQContainer .defaultDockerImageName),
77
+ adminPassword = RabbitMQContainer .defaultAdminPassword,
78
+ queues = Seq .empty,
79
+ exchanges = Seq (
80
+ Exchange (
81
+ name = testExchange,
82
+ exchangeType = " direct" ,
83
+ arguments = Map .empty,
84
+ vhost = Some (" test-vhost" )
85
+ )
86
+ ),
87
+ bindings = Seq .empty,
88
+ users = Seq (
89
+ User (
90
+ name = testUsername,
91
+ password = testPassword,
92
+ tags = Set (" administrator" )
93
+ )
94
+ ),
95
+ vhosts = Seq (VHost (name = " test-vhost" )),
96
+ vhostsLimits = Seq .empty,
97
+ operatorPolicies = Seq .empty,
98
+ policies = Seq .empty,
99
+ parameters = Seq .empty,
100
+ permissions = Seq (
101
+ Permission (
102
+ vhost = " test-vhost" ,
103
+ user = testUsername,
104
+ configure = " .*" ,
105
+ write = " .*" ,
106
+ read = " .*"
107
+ )
108
+ )
109
+ )
110
+ }
0 commit comments