f96f3df786ff7848d0a02072d23c96fc2acef513
[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.MappersFacade
7 import org.onap.cps.ncmp.impl.cmnotificationsubscription.cache.DmiCacheHandler
8 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.Data
9 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent
10 import org.onap.cps.ncmp.utils.events.CloudEventMapper
11 import org.onap.cps.utils.JsonObjectMapper
12 import spock.lang.Specification
13
14 class NcmpOutEventProducerSpec extends Specification {
15
16     def mockEventsPublisher = Mock(EventsPublisher)
17     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
18     def mockCmNotificationSubscriptionMappersHandler = Mock(MappersFacade)
19     def mockDmiCmNotificationSubscriptionCacheHandler = Mock(DmiCacheHandler)
20
21     def objectUnderTest = new NcmpOutEventProducer(mockEventsPublisher, jsonObjectMapper,
22         mockCmNotificationSubscriptionMappersHandler, 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 ncmpOutEvent = new NcmpOutEvent(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.ncmpOutEventTopic = 'client-test-topic'
31         and: 'a deadline to an event'
32             objectUnderTest.dmiOutEventTimeoutInMs = 1000
33         when: 'the event is published'
34             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, 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 ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
44                         assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
45                         assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
46                         assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
47                         assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
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 ncmpOutEvent = new NcmpOutEvent(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.ncmpOutEventTopic = 'client-test-topic'
63         and: 'a deadline to an event'
64             objectUnderTest.dmiOutEventTimeoutInMs = 1000
65         when: 'the event is scheduled to be published'
66             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, 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.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, 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 ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
78                         assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
79                         assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
80                         assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
81                         assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
82                     }
83             }
84         then: 'the cache handler is called once to remove accepted and rejected entries in cache'
85             1 * mockDmiCmNotificationSubscriptionCacheHandler.removeAcceptedAndRejectedDmiSubscriptionEntries(subscriptionId)
86     }
87
88
89 }