fde7e182d094b91c3ad866820266b428ca18ef52
[cps.git] /
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.impl.cmnotificationsubscription.ncmp
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import org.onap.cps.events.EventsPublisher
26 import org.onap.cps.ncmp.config.CpsApplicationContext
27 import org.onap.cps.ncmp.impl.cmnotificationsubscription.cache.DmiCacheHandler
28 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.Data
29 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent
30 import org.onap.cps.ncmp.utils.events.CloudEventMapper
31 import org.onap.cps.utils.JsonObjectMapper
32 import org.springframework.boot.test.context.SpringBootTest
33 import org.springframework.test.context.ContextConfiguration
34 import spock.lang.Specification
35
36 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
37 @ContextConfiguration(classes = [CpsApplicationContext])
38 class NcmpOutEventProducerSpec extends Specification {
39
40     def mockEventsPublisher = Mock(EventsPublisher)
41     def mockNcmpOutEventMapper = Mock(NcmpOutEventMapper)
42     def mockDmiCacheHandler = Mock(DmiCacheHandler)
43
44     def objectUnderTest = new NcmpOutEventProducer(mockEventsPublisher, mockNcmpOutEventMapper, mockDmiCacheHandler)
45
46     def 'Create and #scenario Cm Notification Subscription NCMP out event'() {
47         given: 'a cm subscription response for the client'
48             def subscriptionId = 'test-subscription-id-2'
49             def eventType = 'subscriptionCreateResponse'
50             def ncmpOutEvent = new NcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-2', acceptedTargets: ['ch-1', 'ch-2']))
51         and: 'also we have target topic for publishing to client'
52             objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
53         and: 'a deadline to an event'
54             objectUnderTest.dmiOutEventTimeoutInMs = 1000
55         when: 'the event is published'
56             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, eventPublishingTaskToBeScheduled)
57         then: 'we conditionally wait for a while'
58             Thread.sleep(delayInMs)
59         then: 'the event contains the required attributes'
60             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
61                 args ->
62                     {
63                         assert args[0] == 'client-test-topic'
64                         assert args[1] == subscriptionId
65                         def ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
66                         assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
67                         assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
68                         assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
69                         assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
70                     }
71             }
72         where: 'following scenarios are considered'
73             scenario                                          | delayInMs | eventPublishingTaskToBeScheduled
74             'publish event now'                               | 0         | false
75             'schedule and publish after the configured time ' | 1500      | true
76     }
77
78     def 'Schedule Cm Notification Subscription NCMP out event but later publish it on demand'() {
79         given: 'a cm subscription response for the client'
80             def subscriptionId = 'test-subscription-id-3'
81             def eventType = 'subscriptionCreateResponse'
82             def ncmpOutEvent = new NcmpOutEvent(data: new Data(subscriptionId: 'test-subscription-id-3', acceptedTargets: ['ch-2', 'ch-3']))
83         and: 'also we have target topic for publishing to client'
84             objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
85         and: 'a deadline to an event'
86             objectUnderTest.dmiOutEventTimeoutInMs = 1000
87         when: 'the event is scheduled to be published'
88             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, true)
89         then: 'we wait for 10ms and then we receive response from DMI'
90             Thread.sleep(10)
91         and: 'we receive response from DMI so we publish the message on demand'
92             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, false)
93         then: 'the event contains the required attributes'
94             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
95                 args ->
96                     {
97                         assert args[0] == 'client-test-topic'
98                         assert args[1] == subscriptionId
99                         def ncmpOutEventAsCloudEvent = (args[2] as CloudEvent)
100                         assert ncmpOutEventAsCloudEvent.getExtension('correlationid') == subscriptionId
101                         assert ncmpOutEventAsCloudEvent.type == 'subscriptionCreateResponse'
102                         assert ncmpOutEventAsCloudEvent.source.toString() == 'NCMP'
103                         assert CloudEventMapper.toTargetEvent(ncmpOutEventAsCloudEvent, NcmpOutEvent) == ncmpOutEvent
104                     }
105             }
106         then: 'the cache handler is called once to remove accepted and rejected entries in cache'
107             1 * mockDmiCacheHandler.removeAcceptedAndRejectedDmiSubscriptionEntries(subscriptionId)
108     }
109
110     def 'No event published when NCMP out event is null'() {
111         given: 'a cm subscription response for the client'
112             def subscriptionId = 'test-subscription-id-3'
113             def eventType = 'subscriptionCreateResponse'
114             def ncmpOutEvent = null
115         and: 'also we have target topic for publishing to client'
116             objectUnderTest.ncmpOutEventTopic = 'client-test-topic'
117         and: 'a deadline to an event'
118             objectUnderTest.dmiOutEventTimeoutInMs = 1000
119         when: 'the event is scheduled to be published'
120             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, true)
121         then: 'we wait for 10ms and then we receive response from DMI'
122             Thread.sleep(10)
123         and: 'we receive NO response from DMI so we publish the message on demand'
124             objectUnderTest.publishNcmpOutEvent(subscriptionId, eventType, ncmpOutEvent, false)
125         and: 'no event published'
126             0 * mockEventsPublisher.publishCloudEvent(*_)
127     }
128
129 }