1 package org.onap.cps.ncmp.api.impl.events.cmsubscription
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.producer.CmNotificationSubscriptionNcmpOutEventProducer
7 import org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper
8 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent
9 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.Data
10 import org.onap.cps.utils.JsonObjectMapper
11 import spock.lang.Specification
13 class CmNotificationSubscriptionNcmpOutEventProducerSpec extends Specification {
15 def mockEventsPublisher = Mock(EventsPublisher)
16 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
17 def mockCmNotificationSubscriptionMappersHandler = Mock(CmNotificationSubscriptionMappersHandler)
18 def mockDmiCmNotificationSubscriptionCacheHandler = Mock(DmiCmNotificationSubscriptionCacheHandler)
20 def objectUnderTest = new CmNotificationSubscriptionNcmpOutEventProducer(mockEventsPublisher, jsonObjectMapper,
21 mockCmNotificationSubscriptionMappersHandler, mockDmiCmNotificationSubscriptionCacheHandler)
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(_, _, _) >> {
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
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
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'
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(_, _, _) >> {
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
83 then: 'the cache handler is called once to remove accepted and rejected entries in cache'
84 1 * mockDmiCmNotificationSubscriptionCacheHandler.removeAcceptedAndRejectedDmiCmNotificationSubscriptionEntries(subscriptionId)