Replace sleep with PollingConditions
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / async / SerializationIntegrationSpec.groovy
index 212c673..ee89333 100644 (file)
@@ -24,7 +24,6 @@ import com.fasterxml.jackson.databind.ObjectMapper
 import io.cloudevents.core.builder.CloudEventBuilder
 import org.onap.cps.events.EventsPublisher
 import org.onap.cps.ncmp.api.impl.config.kafka.KafkaConfig
-
 import org.onap.cps.ncmp.api.kafka.ConsumerBaseSpec
 import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent
 import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent
@@ -38,8 +37,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration
 import org.springframework.boot.test.context.SpringBootTest
 import org.springframework.test.annotation.DirtiesContext
 import org.testcontainers.spock.Testcontainers
-
-import java.util.concurrent.TimeUnit
+import spock.util.concurrent.PollingConditions
 
 @SpringBootTest(classes =[DataOperationEventConsumer, AsyncRestRequestResponseEventConsumer, RecordFilterStrategies, KafkaConfig])
 @DirtiesContext
@@ -59,33 +57,38 @@ class SerializationIntegrationSpec extends ConsumerBaseSpec {
     @Value('${app.ncmp.async-m2m.topic}')
     def topic
 
-    def capturedForwardedEvent
-
     def 'Forwarding DataOperation Event Data.'() {
         given: 'a data operation cloud event'
             def cloudEvent = createCloudEvent()
+        and: 'a flag to track the publish cloud event call'
+            def publishCloudEventMethodCalled = false
+        and: 'the (mocked) events publisher will use the flag to indicate if it is called and will capture the cloud event'
+            mockEventsPublisher.publishCloudEvent('some client topic', 'some-correlation-id', cloudEvent) >> {
+                publishCloudEventMethodCalled = true
+            }
         when: 'send the event'
             cloudEventKafkaTemplate.send(topic, cloudEvent)
-        and: 'wait a little for async processing of message'
-            TimeUnit.MILLISECONDS.sleep(300)
         then: 'the event has been forwarded'
-            1 * mockEventsPublisher.publishCloudEvent('some client topic', 'some-correlation-id', _) >> { args -> { capturedForwardedEvent = args[2] } }
-        and: 'the forwarded event is identical to the event that was sent'
-            assert capturedForwardedEvent == cloudEvent
+            new PollingConditions().within(1) {
+                assert publishCloudEventMethodCalled == true
+            }
     }
 
     def 'Forwarding AsyncRestRequestResponse Event Data.'() {
         given: 'async request response legacy event'
             def dmiAsyncRequestResponseEvent = new DmiAsyncRequestResponseEvent(eventId: 'my-event-id',eventTarget: 'some client topic')
+        and: 'a flag to track the publish event call'
+            def publishEventMethodCalled = false
+        and: 'the (mocked) events publisher will use the flag to indicate if it is called and will capture the event'
+            mockEventsPublisher.publishEvent(*_) >> {
+                publishEventMethodCalled = true
+            }
         when: 'send the event'
             legacyEventKafkaTemplate.send(topic, dmiAsyncRequestResponseEvent)
-        and: 'wait a little for async processing of message'
-            TimeUnit.MILLISECONDS.sleep(300)
         then: 'the event has been forwarded'
-            1 * mockEventsPublisher.publishEvent('some client topic', 'my-event-id', _) >> { args -> { capturedForwardedEvent = args[2] } }
-        and: 'the captured id and target of the forwarded event is same as the one that was sent'
-            assert capturedForwardedEvent.eventId == dmiAsyncRequestResponseEvent.eventId
-            assert capturedForwardedEvent.eventTarget == dmiAsyncRequestResponseEvent.eventTarget
+            new PollingConditions().within(1) {
+                assert publishEventMethodCalled == true
+            }
     }
 
     def createCloudEvent() {