CM Subscription: REfactor classes of producers and consumers
[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.producer.CmNotificationSubscriptionNcmpOutEventProducer
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 mockCmNotificationSubscriptionNcmpOutEventMapper = Mock(CmNotificationSubscriptionNcmpOutEventMapper)
19     def mockDmiCmNotificationSubscriptionCacheHandler = Mock(DmiCmNotificationSubscriptionCacheHandler)
20
21     def objectUnderTest = new CmNotificationSubscriptionNcmpOutEventProducer(mockEventsPublisher, jsonObjectMapper,
22         mockCmNotificationSubscriptionNcmpOutEventMapper, mockDmiCmNotificationSubscriptionCacheHandler)
23
24     def 'Create and #scenario Cm Notification Subscription NCMP out event'() {
25         given: 'a cm subscription response for the client'
26             def subscriptionId = 'test-subscription-id-2'
27             def eventType = 'subscriptionCreateResponse'
28             def cmNotificationSubscriptionNcmpOutEvent = new CmNotificationSubscriptionNcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-2', acceptedTargets: ['ch-1', 'ch-2']))
29         and: 'also we have target topic for publishing to client'
30             objectUnderTest.cmNotificationSubscriptionNcmpOutEventTopic = 'client-test-topic'
31         and: 'a deadline to an event'
32             objectUnderTest.cmNotificationSubscriptionDmiOutEventTimeoutInMs = 1000
33         when: 'the event is published'
34             objectUnderTest.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId, eventType, cmNotificationSubscriptionNcmpOutEvent, eventPublishingTaskToBeScheduled)
35         then: 'we conditionally wait for a while'
36             Thread.sleep(delayInMs)
37         then: 'the event contains the required attributes'
38             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
39                 args ->
40                     {
41                         assert args[0] == 'client-test-topic'
42                         assert args[1] == subscriptionId
43                         def cmNotificationSubscriptionNcmpOutEventAsCloudEvent = (args[2] as CloudEvent)
44                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
45                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
46                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.source.toString() == 'NCMP'
47                         assert CloudEventMapper.toTargetEvent(cmNotificationSubscriptionNcmpOutEventAsCloudEvent, CmNotificationSubscriptionNcmpOutEvent) == cmNotificationSubscriptionNcmpOutEvent
48                     }
49             }
50         where: 'following scenarios are considered'
51             scenario                                          | delayInMs | eventPublishingTaskToBeScheduled
52             'publish event now'                               | 0         | false
53             'schedule and publish after the configured time ' | 1500      | true
54     }
55
56     def 'Schedule Cm Notification Subscription NCMP out event but later publish it on demand'() {
57         given: 'a cm subscription response for the client'
58             def subscriptionId = 'test-subscription-id-3'
59             def eventType = 'subscriptionCreateResponse'
60             def cmNotificationSubscriptionNcmpOutEvent = new CmNotificationSubscriptionNcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-3', acceptedTargets: ['ch-2', 'ch-3']))
61         and: 'also we have target topic for publishing to client'
62             objectUnderTest.cmNotificationSubscriptionNcmpOutEventTopic = 'client-test-topic'
63         and: 'a deadline to an event'
64             objectUnderTest.cmNotificationSubscriptionDmiOutEventTimeoutInMs = 1000
65         when: 'the event is scheduled to be published'
66             objectUnderTest.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId, eventType, cmNotificationSubscriptionNcmpOutEvent, true)
67         then: 'we wait for 10ms and then we receive response from DMI'
68             Thread.sleep(10)
69         and: 'we receive response from DMI so we publish the message on demand'
70             objectUnderTest.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId, eventType, cmNotificationSubscriptionNcmpOutEvent, false)
71         then: 'the event contains the required attributes'
72             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
73                 args ->
74                     {
75                         assert args[0] == 'client-test-topic'
76                         assert args[1] == subscriptionId
77                         def cmNotificationSubscriptionNcmpOutEventAsCloudEvent = (args[2] as CloudEvent)
78                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
79                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
80                         assert cmNotificationSubscriptionNcmpOutEventAsCloudEvent.source.toString() == 'NCMP'
81                         assert CloudEventMapper.toTargetEvent(cmNotificationSubscriptionNcmpOutEventAsCloudEvent, CmNotificationSubscriptionNcmpOutEvent) == cmNotificationSubscriptionNcmpOutEvent
82                     }
83             }
84         then: 'the cache handler is called once to remove accepted and rejected entries in cache'
85             1 * mockDmiCmNotificationSubscriptionCacheHandler.removeAcceptedAndRejectedDmiCmNotificationSubscriptionEntries(subscriptionId)
86     }
87
88
89 }