11package org .testcontainers .solace ;
22
33import com .solacesystems .jcsmp .BytesXMLMessage ;
4+ import com .solacesystems .jcsmp .ConsumerFlowProperties ;
5+ import com .solacesystems .jcsmp .EndpointProperties ;
46import com .solacesystems .jcsmp .JCSMPException ;
57import com .solacesystems .jcsmp .JCSMPFactory ;
68import com .solacesystems .jcsmp .JCSMPProperties ;
79import com .solacesystems .jcsmp .JCSMPSession ;
810import com .solacesystems .jcsmp .JCSMPStreamingPublishCorrelatingEventHandler ;
11+ import com .solacesystems .jcsmp .Queue ;
912import com .solacesystems .jcsmp .TextMessage ;
1013import com .solacesystems .jcsmp .Topic ;
1114import com .solacesystems .jcsmp .XMLMessageConsumer ;
@@ -30,40 +33,75 @@ public class SolaceContainerSMFTest {
3033
3134 private static final Topic TOPIC = JCSMPFactory .onlyInstance ().createTopic ("Topic/ActualTopic" );
3235
36+ private static final Queue QUEUE = JCSMPFactory .onlyInstance ().createQueue ("Queue" );
37+
3338 @ Test
3439 public void testSolaceContainerWithSimpleAuthentication () {
3540 try (
3641 // solaceContainerSetup {
37- SolaceContainer solaceContainer = new SolaceContainer ("solace/solace-pubsub-standard:10.2 " )
42+ SolaceContainer solaceContainer = new SolaceContainer ("solace/solace-pubsub-standard:10.25.0 " )
3843 .withCredentials ("user" , "pass" )
39- .withTopic ("Topic/ActualTopic" , Service .SMF )
44+ .withTopic (TOPIC . getName () , Service .SMF )
4045 .withVpn ("test_vpn" )
4146 // }
4247 ) {
4348 solaceContainer .start ();
4449 JCSMPSession session = createSessionWithBasicAuth (solaceContainer );
4550 assertThat (session ).isNotNull ();
46- assertThat ( consumeMessageFromSolace ( session )). isEqualTo ( MESSAGE );
51+ consumeMessageFromTopics ( session );
4752 session .closeSession ();
4853 }
4954 }
5055
56+ @ Test
57+ public void testSolaceContainerWithCreateFlow () {
58+ try (
59+ SolaceContainer solaceContainer = new SolaceContainer ("solace/solace-pubsub-standard:10.25.0" )
60+ .withCredentials ("user" , "pass" )
61+ .withTopic (TOPIC .getName (), Service .SMF )
62+ .withVpn ("test_vpn" )
63+ ) {
64+ solaceContainer .start ();
65+ JCSMPSession session = createSessionWithBasicAuth (solaceContainer );
66+ assertThat (session ).isNotNull ();
67+ testCreateFlow (session );
68+ session .closeSession ();
69+ }
70+ }
71+
72+ private static void testCreateFlow (JCSMPSession session ) {
73+ try {
74+ EndpointProperties endpointProperties = new EndpointProperties ();
75+ endpointProperties .setAccessType (EndpointProperties .ACCESSTYPE_NONEXCLUSIVE );
76+ endpointProperties .setQuota (1000 );
77+ session .provision (QUEUE , endpointProperties , JCSMPSession .FLAG_IGNORE_ALREADY_EXISTS );
78+ session .addSubscription (QUEUE , TOPIC , JCSMPSession .WAIT_FOR_CONFIRM );
79+ ConsumerFlowProperties flowProperties = new ConsumerFlowProperties ().setEndpoint (QUEUE );
80+ TestConsumer listener = new TestConsumer ();
81+ session .createFlow (listener , flowProperties ).start ();
82+ publishMessageToSolaceTopic (session );
83+ listener .waitForMessage ();
84+ } catch (Exception e ) {
85+ throw new RuntimeException ("Cannot process message using solace topic/queue: " + e .getMessage (), e );
86+ }
87+ }
88+
5189 @ Test
5290 public void testSolaceContainerWithCertificates () {
5391 try (
5492 // solaceContainerUsageSSL {
55- SolaceContainer solaceContainer = new SolaceContainer ("solace/solace-pubsub-standard:10.6 " )
93+ SolaceContainer solaceContainer = new SolaceContainer ("solace/solace-pubsub-standard:10.25.0 " )
5694 .withClientCert (
5795 MountableFile .forClasspathResource ("solace.pem" ),
5896 MountableFile .forClasspathResource ("rootCA.crt" )
5997 )
60- .withTopic ("Topic/ActualTopic" , Service .SMF_SSL )
98+ .withTopic (TOPIC . getName () , Service .SMF_SSL )
6199 // }
62100 ) {
63101 solaceContainer .start ();
64102 JCSMPSession session = createSessionWithCertificates (solaceContainer );
65103 assertThat (session ).isNotNull ();
66- assertThat ( consumeMessageFromSolace ( session )). isEqualTo ( MESSAGE );
104+ consumeMessageFromTopics ( session );
67105 session .closeSession ();
68106 }
69107 }
@@ -112,7 +150,7 @@ private static JCSMPSession createSession(JCSMPProperties properties) {
112150 }
113151 }
114152
115- private void publishMessageToSolace (JCSMPSession session ) throws JCSMPException {
153+ private static void publishMessageToSolaceTopic (JCSMPSession session ) throws JCSMPException {
116154 XMLMessageProducer producer = session .getMessageProducer (
117155 new JCSMPStreamingPublishCorrelatingEventHandler () {
118156 @ Override
@@ -131,37 +169,49 @@ public void handleErrorEx(Object o, JCSMPException e, long l) {
131169 producer .send (msg , TOPIC );
132170 }
133171
134- private String consumeMessageFromSolace (JCSMPSession session ) {
135- CountDownLatch latch = new CountDownLatch (1 );
172+ private static void consumeMessageFromTopics (JCSMPSession session ) {
136173 try {
137- String [] result = new String [1 ];
138- XMLMessageConsumer cons = session .getMessageConsumer (
139- new XMLMessageListener () {
140- @ Override
141- public void onReceive (BytesXMLMessage msg ) {
142- if (msg instanceof TextMessage ) {
143- TextMessage textMessage = (TextMessage ) msg ;
144- String message = textMessage .getText ();
145- result [0 ] = message ;
146- LOGGER .info ("TextMessage received: " + message );
147- }
148- latch .countDown ();
149- }
150-
151- @ Override
152- public void onException (JCSMPException e ) {
153- LOGGER .error ("Exception received: " + e .getMessage ());
154- latch .countDown ();
155- }
156- }
157- );
174+ TestConsumer listener = new TestConsumer ();
175+ XMLMessageConsumer cons = session .getMessageConsumer (listener );
158176 session .addSubscription (TOPIC );
159177 cons .start ();
160- publishMessageToSolace (session );
161- assertThat (latch .await (10L , TimeUnit .SECONDS )).isTrue ();
162- return result [0 ];
178+ publishMessageToSolaceTopic (session );
179+ listener .waitForMessage ();
163180 } catch (Exception e ) {
164- throw new RuntimeException ("Cannot receive message from solace" , e );
181+ throw new RuntimeException ("Cannot process message using solace: " + e .getMessage (), e );
182+ }
183+ }
184+
185+ static class TestConsumer implements XMLMessageListener {
186+
187+ private final CountDownLatch latch = new CountDownLatch (1 );
188+
189+ private String result ;
190+
191+ @ Override
192+ public void onReceive (BytesXMLMessage msg ) {
193+ if (msg instanceof TextMessage ) {
194+ TextMessage textMessage = (TextMessage ) msg ;
195+ String message = textMessage .getText ();
196+ result = message ;
197+ LOGGER .info ("Message received: " + message );
198+ }
199+ latch .countDown ();
200+ }
201+
202+ @ Override
203+ public void onException (JCSMPException e ) {
204+ LOGGER .error ("Exception received: " + e .getMessage ());
205+ latch .countDown ();
206+ }
207+
208+ private void waitForMessage () {
209+ try {
210+ assertThat (latch .await (10L , TimeUnit .SECONDS )).isTrue ();
211+ assertThat (result ).isEqualTo (MESSAGE );
212+ } catch (Exception e ) {
213+ throw new RuntimeException ("Cannot receive message from solace: " + e .getMessage (), e );
214+ }
165215 }
166216 }
167217}
0 commit comments