1 package org.onap.cps.ncmp.impl.cmnotificationsubscription.ncmp
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
13 class NcmpOutEventProducerSpec extends Specification {
15 def mockEventsPublisher = Mock(EventsPublisher)
16 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
17 def mockNcmpOutEventMapper = Mock(NcmpOutEventMapper)
18 def mockDmiCacheHandler = Mock(DmiCacheHandler)
20 def objectUnderTest = new NcmpOutEventProducer(mockEventsPublisher, jsonObjectMapper, mockNcmpOutEventMapper, mockDmiCacheHandler)
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(_, _, _) >> {
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
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
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'
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(_, _, _) >> {
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
82 then: 'the cache handler is called once to remove accepted and rejected entries in cache'
83 1 * mockDmiCacheHandler.removeAcceptedAndRejectedDmiSubscriptionEntries(subscriptionId)