RTD change to document migration to Spring Boot 3.0
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / CmSubscriptionDmiOutEventConsumerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (c) 2023 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.api.impl.events.cmsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import com.hazelcast.map.IMap
25 import io.cloudevents.CloudEvent
26 import io.cloudevents.core.builder.CloudEventBuilder
27 import org.apache.kafka.clients.consumer.ConsumerRecord
28 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistenceImpl
29 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
30 import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.spi.model.DataNodeBuilder
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
38 class CmSubscriptionDmiOutEventConsumerSpec extends MessagingBaseSpec {
39
40     @Autowired
41     JsonObjectMapper jsonObjectMapper
42
43     @Autowired
44     ObjectMapper objectMapper
45
46     IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
47     def mockSubscriptionPersistence = Mock(SubscriptionPersistenceImpl)
48     def mockSubscriptionEventResponseMapper  = Mock(CmSubscriptionDmiOutEventToYangModelSubscriptionEventMapper)
49     def mockCmSubscriptionNcmpOutEventPublisher = Mock(CmSubscriptionNcmpOutEventPublisher)
50
51     def objectUnderTest = new CmSubscriptionDmiOutEventConsumer(mockForwardedSubscriptionEventCache,
52         mockSubscriptionPersistence, mockSubscriptionEventResponseMapper, mockCmSubscriptionNcmpOutEventPublisher)
53
54     def 'Consume dmi out event where all DMIs have responded'() {
55         given: 'a consumer record including cloud event having dmi out event'
56             def dmiOutConsumerRecord = getDmiOutConsumerRecord()
57         and: 'notifications are enabled'
58             objectUnderTest.notificationFeatureEnabled = notificationEnabled
59         and: 'subscription model loader is enabled'
60             objectUnderTest.subscriptionModelLoaderEnabled = modelLoaderEnabled
61         and: 'subscription persistence service returns data node includes no pending cm handle'
62             mockSubscriptionPersistence.getCmHandlesForSubscriptionEvent(*_) >> [getDataNode()]
63         when: 'the valid event is consumed'
64             objectUnderTest.consumeDmiOutEvent(dmiOutConsumerRecord)
65         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
66             1 * mockForwardedSubscriptionEventCache.containsKey('SCO-9989752cm-subscription-001') >> true
67             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['some-dmi-name'] as Set)
68         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
69             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> ([] as Set)
70         and: 'the response event is map to yang model'
71             numberOfTimeToPersist * mockSubscriptionEventResponseMapper.toYangModelSubscriptionEvent(_)
72         and: 'the response event is persisted into the db'
73             numberOfTimeToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(_)
74         and: 'the subscription event is removed from the map'
75             numberOfTimeToRemove * mockForwardedSubscriptionEventCache.remove('SCO-9989752cm-subscription-001')
76         and: 'a response outcome has been created'
77             numberOfTimeToResponse * mockCmSubscriptionNcmpOutEventPublisher.sendResponse(_, 'subscriptionCreated')
78         where: 'the following values are used'
79             scenario                                              | modelLoaderEnabled  |   notificationEnabled  ||  numberOfTimeToPersist  ||  numberOfTimeToRemove  || numberOfTimeToResponse
80             'Both model loader and notification are enabled'      |    true             |     true               ||   1                     ||      1                 ||       1
81             'Both model loader and notification are disabled'     |    false            |     false              ||   0                     ||      0                 ||       0
82             'Model loader enabled and notification  disabled'     |    true             |     false              ||   1                     ||      0                 ||       0
83             'Model loader disabled and notification enabled'      |    false            |     true               ||   0                     ||      1                 ||       1
84     }
85
86     def 'Consume dmi out event where another DMI has not yet responded'() {
87         given: 'a subscription event response and notifications are enabled'
88             objectUnderTest.notificationFeatureEnabled = notificationEnabled
89         and: 'subscription model loader is enabled'
90             objectUnderTest.subscriptionModelLoaderEnabled = modelLoaderEnabled
91         when: 'the valid event is consumed'
92             objectUnderTest.consumeDmiOutEvent(getDmiOutConsumerRecord())
93         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
94             1 * mockForwardedSubscriptionEventCache.containsKey('SCO-9989752cm-subscription-001') >> true
95             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['responded-dmi', 'non-responded-dmi'] as Set)
96         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
97             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['non-responded-dmi'] as Set)
98         and: 'the response event is map to yang model'
99             numberOfTimeToPersist * mockSubscriptionEventResponseMapper.toYangModelSubscriptionEvent(_)
100         and: 'the response event is persisted into the db'
101             numberOfTimeToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(_)
102         and: 'the subscription event is removed from the map'
103         and: 'the subscription event is not removed from the map'
104             0 * mockForwardedSubscriptionEventCache.remove(_)
105         and: 'a response outcome has not been created'
106             0 * mockCmSubscriptionNcmpOutEventPublisher.sendResponse(*_)
107         where: 'the following values are used'
108             scenario                                              | modelLoaderEnabled  |   notificationEnabled  ||  numberOfTimeToPersist
109             'Both model loader and notification are enabled'      |    true             |     true               ||   1
110             'Both model loader and notification are disabled'     |    false            |     false              ||   0
111             'Model loader enabled and notification  disabled'     |    true             |     false              ||   1
112             'Model loader disabled and notification enabled'      |    false            |     true               ||   0
113     }
114
115     def getDmiOutEvent() {
116         def cmSubscriptionDmiOutEventJsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
117         return jsonObjectMapper.convertJsonString(cmSubscriptionDmiOutEventJsonData, CmSubscriptionDmiOutEvent.class)
118     }
119
120     def getCloudEvent() {
121         return CloudEventBuilder.v1()
122             .withData(objectMapper.writeValueAsBytes(getDmiOutEvent()))
123             .withId('some-id')
124             .withType('subscriptionCreated')
125             .withSource(URI.create('NCMP')).build()
126     }
127
128     def getDmiOutConsumerRecord() {
129         return new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', getCloudEvent())
130     }
131
132     def getDataNode() {
133         def leaves = [status:'ACCEPTED', cmHandleId:'cmhandle1'] as Map
134         return new DataNodeBuilder().withDataspace('NCMP-Admin')
135             .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription')
136             .withLeaves(leaves).build()
137     }
138 }