5
5
import com .rabbitmq .client .Channel ;
6
6
import com .rabbitmq .client .Connection ;
7
7
import com .rabbitmq .client .ConnectionFactory ;
8
+ import com .rabbitmq .client .DeliverCallback ;
8
9
import org .junit .Test ;
9
10
import org .testcontainers .containers .RabbitMQContainer .SslVerification ;
10
11
import org .testcontainers .utility .MountableFile ;
11
12
12
13
import java .io .File ;
13
- import java .io .FileInputStream ;
14
14
import java .io .IOException ;
15
+ import java .nio .charset .StandardCharsets ;
16
+ import java .nio .file .Files ;
15
17
import java .security .KeyManagementException ;
16
18
import java .security .KeyStore ;
17
19
import java .security .KeyStoreException ;
18
20
import java .security .NoSuchAlgorithmException ;
19
21
import java .security .UnrecoverableKeyException ;
20
22
import java .security .cert .CertificateException ;
21
23
import java .util .Collections ;
24
+ import java .util .concurrent .TimeoutException ;
22
25
23
26
import javax .net .ssl .KeyManagerFactory ;
24
27
import javax .net .ssl .SSLContext ;
@@ -40,32 +43,19 @@ public class RabbitMQContainerTest {
40
43
@ Test
41
44
public void shouldCreateRabbitMQContainer () {
42
45
try (RabbitMQContainer container = new RabbitMQContainer (RabbitMQTestImages .RABBITMQ_IMAGE )) {
46
+ container .start ();
47
+
43
48
assertThat (container .getAdminPassword ()).isEqualTo ("guest" );
44
49
assertThat (container .getAdminUsername ()).isEqualTo ("guest" );
45
50
46
- container .start ();
47
-
48
51
assertThat (container .getAmqpsUrl ())
49
- .isEqualTo (
50
- String .format ("amqps://%s:%d" , container .getHost (), container .getMappedPort (DEFAULT_AMQPS_PORT ))
51
- );
52
+ .isEqualTo (String .format ("amqps://%s:%d" , container .getHost (), container .getAmqpsPort ()));
52
53
assertThat (container .getAmqpUrl ())
53
- .isEqualTo (
54
- String .format ("amqp://%s:%d" , container .getHost (), container .getMappedPort (DEFAULT_AMQP_PORT ))
55
- );
54
+ .isEqualTo (String .format ("amqp://%s:%d" , container .getHost (), container .getAmqpPort ()));
56
55
assertThat (container .getHttpsUrl ())
57
- .isEqualTo (
58
- String .format ("https://%s:%d" , container .getHost (), container .getMappedPort (DEFAULT_HTTPS_PORT ))
59
- );
56
+ .isEqualTo (String .format ("https://%s:%d" , container .getHost (), container .getHttpsPort ()));
60
57
assertThat (container .getHttpUrl ())
61
- .isEqualTo (
62
- String .format ("http://%s:%d" , container .getHost (), container .getMappedPort (DEFAULT_HTTP_PORT ))
63
- );
64
-
65
- assertThat (container .getHttpsPort ()).isEqualTo (container .getMappedPort (DEFAULT_HTTPS_PORT ));
66
- assertThat (container .getHttpPort ()).isEqualTo (container .getMappedPort (DEFAULT_HTTP_PORT ));
67
- assertThat (container .getAmqpsPort ()).isEqualTo (container .getMappedPort (DEFAULT_AMQPS_PORT ));
68
- assertThat (container .getAmqpPort ()).isEqualTo (container .getMappedPort (DEFAULT_AMQP_PORT ));
58
+ .isEqualTo (String .format ("http://%s:%d" , container .getHost (), container .getHttpPort ()));
69
59
70
60
assertThat (container .getLivenessCheckPortNumbers ())
71
61
.containsExactlyInAnyOrder (
@@ -74,6 +64,24 @@ public void shouldCreateRabbitMQContainer() {
74
64
container .getMappedPort (DEFAULT_HTTP_PORT ),
75
65
container .getMappedPort (DEFAULT_HTTPS_PORT )
76
66
);
67
+
68
+ assertFunctionality (container );
69
+ }
70
+ }
71
+
72
+ @ Test
73
+ public void shouldCreateRabbitMQContainerWithCustomCredentials () {
74
+ try (
75
+ RabbitMQContainer container = new RabbitMQContainer (RabbitMQTestImages .RABBITMQ_IMAGE )
76
+ .withAdminUser ("admin" )
77
+ .withAdminPassword ("admin" )
78
+ ) {
79
+ container .start ();
80
+
81
+ assertThat (container .getAdminPassword ()).isEqualTo ("admin" );
82
+ assertThat (container .getAdminUsername ()).isEqualTo ("admin" );
83
+
84
+ assertFunctionality (container );
77
85
}
78
86
}
79
87
@@ -283,15 +291,15 @@ private SSLContext createSslContext(
283
291
284
292
KeyStore ks = KeyStore .getInstance ("PKCS12" );
285
293
ks .load (
286
- new FileInputStream (new File (classLoader .getResource (keystoreFile ).getFile ())),
294
+ Files . newInputStream (new File (classLoader .getResource (keystoreFile ).getFile ()). toPath ( )),
287
295
keystorePassword .toCharArray ()
288
296
);
289
297
KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
290
298
kmf .init (ks , "password" .toCharArray ());
291
299
292
300
KeyStore trustStore = KeyStore .getInstance ("PKCS12" );
293
301
trustStore .load (
294
- new FileInputStream (new File (classLoader .getResource (truststoreFile ).getFile ())),
302
+ Files . newInputStream (new File (classLoader .getResource (truststoreFile ).getFile ()). toPath ( )),
295
303
truststorePassword .toCharArray ()
296
304
);
297
305
TrustManagerFactory tmf = TrustManagerFactory .getInstance ("SunX509" );
@@ -301,4 +309,27 @@ private SSLContext createSslContext(
301
309
c .init (kmf .getKeyManagers (), tmf .getTrustManagers (), null );
302
310
return c ;
303
311
}
312
+
313
+ private void assertFunctionality (RabbitMQContainer container ) {
314
+ String queueName = "test-queue" ;
315
+ String text = "Hello World!" ;
316
+
317
+ ConnectionFactory factory = new ConnectionFactory ();
318
+ factory .setHost (container .getHost ());
319
+ factory .setPort (container .getAmqpPort ());
320
+ factory .setUsername (container .getAdminUsername ());
321
+ factory .setPassword (container .getAdminPassword ());
322
+ try (Connection connection = factory .newConnection (); Channel channel = connection .createChannel ()) {
323
+ channel .queueDeclare (queueName , false , false , false , null );
324
+ channel .basicPublish ("" , queueName , null , text .getBytes ());
325
+
326
+ DeliverCallback deliverCallback = (consumerTag , delivery ) -> {
327
+ String message = new String (delivery .getBody (), StandardCharsets .UTF_8 );
328
+ assertThat (message ).isEqualTo (text );
329
+ };
330
+ channel .basicConsume (queueName , true , deliverCallback , consumerTag -> {});
331
+ } catch (IOException | TimeoutException e ) {
332
+ throw new RuntimeException (e );
333
+ }
334
+ }
304
335
}
0 commit comments