NcmpCloudEventBuilder refactoring
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / CmNotificationSubscriptionNcmpOutEventProducerSpec.groovy
1 package org.onap.cps.ncmp.api.impl.events.cmsubscription
2
3 import com.fasterxml.jackson.databind.ObjectMapper
4 import io.cloudevents.CloudEvent
5 import org.onap.cps.events.EventsPublisher
6 import org.onap.cps.ncmp.api.impl.events.cmsubscription.mapper.CmNotificationSubscriptionNcmpOutEventMapper
7 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails
8 import org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper
9 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent
10 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.Data
11 import org.onap.cps.utils.JsonObjectMapper
12 import spock.lang.Specification
13
14 class CmNotificationSubscriptionNcmpOutEventProducerSpec extends Specification {
15
16     def mockEventsPublisher = Mock(EventsPublisher)
17     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
18     def mockCmNotificationSubscriptionCache = Mock(Map<String, Map<String, DmiCmNotificationSubscriptionDetails>>)
19     def mockCmNotificationSubscriptionNcmpOutEventMapper = Mock(CmNotificationSubscriptionNcmpOutEventMapper)
20
21     def objectUnderTest = new CmNotificationSubscriptionNcmpOutEventProducer(mockEventsPublisher, jsonObjectMapper, mockCmNotificationSubscriptionCache, mockCmNotificationSubscriptionNcmpOutEventMapper)
22
23     def 'Create and #scenario Cm Notification Subscription NCMP out event'() {
24         given: 'a cm subscription response for the client'
25             def subscriptionId = 'test-subscription-id-2'
26             def eventType = 'subscriptionCreateResponse'
27             def cmNotificationSubscriptionNcmpOutEvent = new CmNotificationSubscriptionNcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-2', acceptedTargets: ['ch-1', 'ch-2']))
28         and: 'also we have target topic for publishing to client'
29             objectUnderTest.cmNotificationSubscriptionNcmpOutEventTopic = 'client-test-topic'
30         and: 'a deadline to an event'
31             objectUnderTest.cmNotificationSubscriptionDmiOutEventTimeoutInMs = 1000
32         when: 'the event is published'
33             objectUnderTest.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId, eventType, cmNotificationSubscriptionNcmpOutEvent, eventPublishingTaskToBeScheduled)
34         then: 'we conditionally wait for a while'
35             Thread.sleep(delayInMs)
36         then: 'the event contains the required attributes'
37             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
38                 args ->
39                     {
40                         assert args[0] == 'client-test-topic'
41                         assert args[1] == subscriptionId
42                         def cmNotificationSubscriptionNcmpOutEventAsCloudEvent = (args[2] as CloudEvent)
43                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
44                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
45                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.source.toString() == 'NCMP'
46                         assert CloudEventMapper.toTargetEvent(cmNotificationSubscriptionNcmpOutEventAsCloudEvent, CmNotificationSubscriptionNcmpOutEvent) == cmNotificationSubscriptionNcmpOutEvent
47                     }
48             }
49         where: 'following scenarios are considered'
50             scenario                                          | delayInMs | eventPublishingTaskToBeScheduled
51             'publish event now'                               | 0         | false
52             'schedule and publish after the configured time ' | 1500      | true
53     }
54
55     def 'Schedule Cm Notification Subscription NCMP out event but later publish it on demand'() {
56         given: 'a cm subscription response for the client'
57             def subscriptionId = 'test-subscription-id-3'
58             def eventType = 'subscriptionCreateResponse'
59             def cmNotificationSubscriptionNcmpOutEvent = new CmNotificationSubscriptionNcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-3', acceptedTargets: ['ch-2', 'ch-3']))
60         and: 'also we have target topic for publishing to client'
61             objectUnderTest.cmNotificationSubscriptionNcmpOutEventTopic = 'client-test-topic'
62         and: 'a deadline to an event'
63             objectUnderTest.cmNotificationSubscriptionDmiOutEventTimeoutInMs = 1000
64         when: 'the event is scheduled to be published'
65             objectUnderTest.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId, eventType, cmNotificationSubscriptionNcmpOutEvent, true)
66         then: 'we wait for 10ms and then we receive response from DMI'
67             Thread.sleep(10)
68         and: 'we receive response from DMI so we publish the message on demand'
69             objectUnderTest.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId, eventType, cmNotificationSubscriptionNcmpOutEvent, false)
70         then: 'the event contains the required attributes'
71             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
72                 args ->
73                     {
74                         assert args[0] == 'client-test-topic'
75                         assert args[1] == subscriptionId
76                         def cmNotificationSubscriptionNcmpOutEventAsCloudEvent = (args[2] as CloudEvent)
77                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
78                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
79                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.source.toString() == 'NCMP'
80                         assert CloudEventMapper.toTargetEvent(cmNotificationSubscriptionNcmpOutEventAsCloudEvent, CmNotificationSubscriptionNcmpOutEvent) == cmNotificationSubscriptionNcmpOutEvent
81                     }
82             }
83     }
84
85
86 }