2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.impl.cmnotificationsubscription.ncmp
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.core.v1.CloudEventBuilder
26 import org.onap.cps.events.EventsPublisher
27 import org.onap.cps.ncmp.config.CpsApplicationContext
28 import org.onap.cps.ncmp.impl.cmnotificationsubscription.cache.DmiCacheHandler
29 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.Data
30 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent
31 import org.onap.cps.ncmp.utils.events.CloudEventMapper
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.test.context.ContextConfiguration
35 import spock.lang.Specification
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, CloudEventBuilder])
38 @ContextConfiguration(classes = [CpsApplicationContext])
39 class NcmpOutEventProducerSpec extends Specification {
41 def mockEventsPublisher = Mock(EventsPublisher)
42 def mockNcmpOutEventMapper = Mock(NcmpOutEventMapper)
43 def mockDmiCacheHandler = Mock(DmiCacheHandler)
45 def objectUnderTest = new NcmpOutEventProducer(mockEventsPublisher, mockNcmpOutEventMapper, mockDmiCacheHandler)
47 def 'Create and #scenario Cm Notification Subscription NCMP out event'() {
48 given: 'a cm subscription response for the client'
49 def subscriptionId = 'test-subscription-id-2'
50 def eventType = 'subscriptionCreateResponse'
51 def ncmpOutEvent = new NcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-2', acceptedTargets: ['ch-1', 'ch-2']))
52 and: 'also we have target topic for publishing to client'
53 objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
54 and: 'a deadline to an event'
55 objectUnderTest.dmiOutEventTimeoutInMs = 1000
56 when: 'the event is published'
57 objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, eventPublishingTaskToBeScheduled)
58 then: 'we conditionally wait for a while'
59 Thread.sleep(delayInMs)
60 then: 'the event contains the required attributes'
61 1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
64 assert args[0] == 'client-test-topic'
65 assert args[1] == subscriptionId
66 def ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
67 assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
68 assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
69 assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
70 assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
73 where: 'following scenarios are considered'
74 scenario | delayInMs | eventPublishingTaskToBeScheduled
75 'publish event now' | 0 | false
76 'schedule and publish after the configured time ' | 1500 | true
79 def 'Schedule Cm Notification Subscription NCMP out event but later publish it on demand'() {
80 given: 'a cm subscription response for the client'
81 def subscriptionId = 'test-subscription-id-3'
82 def eventType = 'subscriptionCreateResponse'
83 def ncmpOutEvent = new NcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-3', acceptedTargets: ['ch-2', 'ch-3']))
84 and: 'also we have target topic for publishing to client'
85 objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
86 and: 'a deadline to an event'
87 objectUnderTest.dmiOutEventTimeoutInMs = 1000
88 when: 'the event is scheduled to be published'
89 objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, true)
90 then: 'we wait for 10ms and then we receive response from DMI'
92 and: 'we receive response from DMI so we publish the message on demand'
93 objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, false)
94 then: 'the event contains the required attributes'
95 1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
98 assert args[0] == 'client-test-topic'
99 assert args[1] == subscriptionId
100 def ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
101 assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
102 assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
103 assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
104 assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
107 then: 'the cache handler is called once to remove accepted and rejected entries in cache'
108 1 * mockDmiCacheHandler.removeAcceptedAndRejectedDmiSubscriptionEntries(subscriptionId)
111 def 'No event published when NCMP out event is null'() {
112 given: 'a cm subscription response for the client'
113 def subscriptionId = 'test-subscription-id-3'
114 def eventType = 'subscriptionCreateResponse'
115 def ncmpOutEvent = null
116 and: 'also we have target topic for publishing to client'
117 objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
118 and: 'a deadline to an event'
119 objectUnderTest.dmiOutEventTimeoutInMs = 1000
120 when: 'the event is scheduled to be published'
121 objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, true)
122 then: 'we wait for 10ms and then we receive response from DMI'
124 and: 'we receive NO response from DMI so we publish the message on demand'
125 objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, false)
126 and: 'no event published'
127 0 * mockEventsPublisher.publishCloudEvent(*_)