afa2e9874e5fb1052ecb30c0146d1599546edfe2
[cps.git] /
1 package org.onap.cps.ncmp.impl.cmnotificationsubscription.ncmp
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.impl.cmnotificationsubscription.cache.DmiCacheHandler
7 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.Data
8 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent
9 import org.onap.cps.ncmp.utils.events.CloudEventMapper
10 import org.onap.cps.utils.JsonObjectMapper
11 import spock.lang.Specification
12
13 class NcmpOutEventProducerSpec extends Specification {
14
15     def mockEventsPublisher = Mock(EventsPublisher)
16     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
17     def mockNcmpOutEventMapper = Mock(NcmpOutEventMapper)
18     def mockDmiCacheHandler = Mock(DmiCacheHandler)
19
20     def objectUnderTest = new NcmpOutEventProducer(mockEventsPublisher, jsonObjectMapper, mockNcmpOutEventMapper, mockDmiCacheHandler)
21
22     def 'Create and #scenario Cm Notification Subscription NCMP out event'() {
23         given: 'a cm subscription response for the client'
24             def subscriptionId = 'test-subscription-id-2'
25             def eventType = 'subscriptionCreateResponse'
26             def ncmpOutEvent = new NcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-2', acceptedTargets: ['ch-1', 'ch-2']))
27         and: 'also we have target topic for publishing to client'
28             objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
29         and: 'a deadline to an event'
30             objectUnderTest.dmiOutEventTimeoutInMs = 1000
31         when: 'the event is published'
32             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, eventPublishingTaskToBeScheduled)
33         then: 'we conditionally wait for a while'
34             Thread.sleep(delayInMs)
35         then: 'the event contains the required attributes'
36             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
37                 args ->
38                     {
39                         assert args[0] == 'client-test-topic'
40                         assert args[1] == subscriptionId
41                         def ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
42                         assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
43                         assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
44                         assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
45                         assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
46                     }
47             }
48         where: 'following scenarios are considered'
49             scenario                                          | delayInMs | eventPublishingTaskToBeScheduled
50             'publish event now'                               | 0         | false
51             'schedule and publish after the configured time ' | 1500      | true
52     }
53
54     def 'Schedule Cm Notification Subscription NCMP out event but later publish it on demand'() {
55         given: 'a cm subscription response for the client'
56             def subscriptionId = 'test-subscription-id-3'
57             def eventType = 'subscriptionCreateResponse'
58             def ncmpOutEvent = new NcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-3', acceptedTargets: ['ch-2', 'ch-3']))
59         and: 'also we have target topic for publishing to client'
60             objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
61         and: 'a deadline to an event'
62             objectUnderTest.dmiOutEventTimeoutInMs = 1000
63         when: 'the event is scheduled to be published'
64             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, true)
65         then: 'we wait for 10ms and then we receive response from DMI'
66             Thread.sleep(10)
67         and: 'we receive response from DMI so we publish the message on demand'
68             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, false)
69         then: 'the event contains the required attributes'
70             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
71                 args ->
72                     {
73                         assert args[0] == 'client-test-topic'
74                         assert args[1] == subscriptionId
75                         def ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
76                         assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
77                         assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
78                         assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
79                         assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
80                     }
81             }
82         then: 'the cache handler is called once to remove accepted and rejected entries in cache'
83             1 * mockDmiCacheHandler.removeAcceptedAndRejectedDmiSubscriptionEntries(subscriptionId)
84     }
85
86     def 'No event published when NCMP out event is null'() {
87         given: 'a cm subscription response for the client'
88             def subscriptionId = 'test-subscription-id-3'
89             def eventType = 'subscriptionCreateResponse'
90             def ncmpOutEvent = null
91         and: 'also we have target topic for publishing to client'
92             objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
93         and: 'a deadline to an event'
94             objectUnderTest.dmiOutEventTimeoutInMs = 1000
95         when: 'the event is scheduled to be published'
96             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, true)
97         then: 'we wait for 10ms and then we receive response from DMI'
98             Thread.sleep(10)
99         and: 'we receive NO response from DMI so we publish the message on demand'
100             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, false)
101         and: 'no event published'
102             0 * mockEventsPublisher.publishCloudEvent(*_)
103     }
104
105
106 }