55import com .rabbitmq .client .Channel ;
66import com .rabbitmq .client .Connection ;
77import com .rabbitmq .client .ConnectionFactory ;
8+ import com .rabbitmq .client .DeliverCallback ;
89import org .junit .Test ;
910import org .testcontainers .containers .RabbitMQContainer .SslVerification ;
1011import org .testcontainers .utility .MountableFile ;
1112
1213import java .io .File ;
13- import java .io .FileInputStream ;
1414import java .io .IOException ;
15+ import java .nio .charset .StandardCharsets ;
16+ import java .nio .file .Files ;
1517import java .security .KeyManagementException ;
1618import java .security .KeyStore ;
1719import java .security .KeyStoreException ;
1820import java .security .NoSuchAlgorithmException ;
1921import java .security .UnrecoverableKeyException ;
2022import java .security .cert .CertificateException ;
2123import java .util .Collections ;
24+ import java .util .concurrent .TimeoutException ;
2225
2326import javax .net .ssl .KeyManagerFactory ;
2427import javax .net .ssl .SSLContext ;
@@ -40,32 +43,19 @@ public class RabbitMQContainerTest {
4043 @ Test
4144 public void shouldCreateRabbitMQContainer () {
4245 try (RabbitMQContainer container = new RabbitMQContainer (RabbitMQTestImages .RABBITMQ_IMAGE )) {
46+ container .start ();
47+
4348 assertThat (container .getAdminPassword ()).isEqualTo ("guest" );
4449 assertThat (container .getAdminUsername ()).isEqualTo ("guest" );
4550
46- container .start ();
47-
4851 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 ()));
5253 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 ()));
5655 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 ()));
6057 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 ()));
6959
7060 assertThat (container .getLivenessCheckPortNumbers ())
7161 .containsExactlyInAnyOrder (
@@ -74,6 +64,24 @@ public void shouldCreateRabbitMQContainer() {
7464 container .getMappedPort (DEFAULT_HTTP_PORT ),
7565 container .getMappedPort (DEFAULT_HTTPS_PORT )
7666 );
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 );
7785 }
7886 }
7987
@@ -283,15 +291,15 @@ private SSLContext createSslContext(
283291
284292 KeyStore ks = KeyStore .getInstance ("PKCS12" );
285293 ks .load (
286- new FileInputStream (new File (classLoader .getResource (keystoreFile ).getFile ())),
294+ Files . newInputStream (new File (classLoader .getResource (keystoreFile ).getFile ()). toPath ( )),
287295 keystorePassword .toCharArray ()
288296 );
289297 KeyManagerFactory kmf = KeyManagerFactory .getInstance ("SunX509" );
290298 kmf .init (ks , "password" .toCharArray ());
291299
292300 KeyStore trustStore = KeyStore .getInstance ("PKCS12" );
293301 trustStore .load (
294- new FileInputStream (new File (classLoader .getResource (truststoreFile ).getFile ())),
302+ Files . newInputStream (new File (classLoader .getResource (truststoreFile ).getFile ()). toPath ( )),
295303 truststorePassword .toCharArray ()
296304 );
297305 TrustManagerFactory tmf = TrustManagerFactory .getInstance ("SunX509" );
@@ -301,4 +309,27 @@ private SSLContext createSslContext(
301309 c .init (kmf .getKeyManagers (), tmf .getTrustManagers (), null );
302310 return c ;
303311 }
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+ }
304335}
0 commit comments