1a54deea6a622b01107a8a4c4d489673166678e9
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2024 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 org.onap.cps.ncmp.impl.cmnotificationsubscription.cache.DmiCacheHandler
25 import org.onap.cps.ncmp.impl.cmnotificationsubscription.dmi.DmiCmSubscriptionDetailsPerDmiMapper
26 import org.onap.cps.ncmp.impl.cmnotificationsubscription.dmi.DmiInEventMapper
27 import org.onap.cps.ncmp.impl.cmnotificationsubscription.dmi.DmiInEventProducer
28 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionDetails
29 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionPredicate
30 import org.onap.cps.ncmp.impl.cmnotificationsubscription.utils.CmSubscriptionPersistenceService
31 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.client_to_ncmp.NcmpInEvent
32 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent
33 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_dmi.DmiInEvent
34 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence
35 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
36 import org.onap.cps.ncmp.utils.TestUtils
37 import org.onap.cps.api.model.DataNode
38 import org.onap.cps.utils.JsonObjectMapper
39 import spock.lang.Specification
40
41 import static org.onap.cps.ncmp.api.data.models.DatastoreType.PASSTHROUGH_OPERATIONAL
42 import static org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus.ACCEPTED
43 import static org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus.PENDING
44
45 class CmSubscriptionHandlerImplSpec extends Specification {
46
47     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
48     def mockCmSubscriptionPersistenceService = Mock(CmSubscriptionPersistenceService)
49     def mockCmSubscriptionComparator = Mock(CmSubscriptionComparator)
50     def mockNcmpOutEventMapper = Mock(NcmpOutEventMapper)
51     def mockDmiInEventMapper = Mock(DmiInEventMapper)
52     def dmiCmSubscriptionDetailsPerDmiMapper = new DmiCmSubscriptionDetailsPerDmiMapper()
53     def mockNcmpOutEventProducer = Mock(NcmpOutEventProducer)
54     def mockDmiInEventProducer = Mock(DmiInEventProducer)
55     def mockDmiCacheHandler = Mock(DmiCacheHandler)
56     def mockInventoryPersistence = Mock(InventoryPersistence)
57
58     def objectUnderTest = new CmSubscriptionHandlerImpl(mockCmSubscriptionPersistenceService,
59         mockCmSubscriptionComparator, mockNcmpOutEventMapper, mockDmiInEventMapper, dmiCmSubscriptionDetailsPerDmiMapper,
60         mockNcmpOutEventProducer, mockDmiInEventProducer, mockDmiCacheHandler, mockInventoryPersistence)
61
62     def testDmiSubscriptionsPerDmi = ["dmi-1": new DmiCmSubscriptionDetails([], PENDING)]
63
64     def 'Consume valid and unique CmNotificationSubscriptionNcmpInEvent create message'() {
65         given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
66             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
67             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
68             def testListOfDeltaPredicates = [new DmiCmSubscriptionPredicate(['ch1'].toSet(), PASSTHROUGH_OPERATIONAL, ['/a/b'].toSet())]
69             mockCmSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
70         and: 'relevant details is extracted from the event'
71             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
72             def predicates = testEventConsumed.getData().getPredicates()
73         and: 'the cache handler returns for relevant subscription id'
74             1 * mockDmiCacheHandler.get("test-id") >> testDmiSubscriptionsPerDmi
75         and: 'the delta predicates is returned'
76             1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> testListOfDeltaPredicates
77         and: 'the DMI in event mapper returns cm notification subscription event'
78             def testDmiInEvent = new DmiInEvent()
79             1 * mockDmiInEventMapper.toDmiInEvent(testListOfDeltaPredicates) >> testDmiInEvent
80         when: 'the valid and unique event is consumed'
81             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
82         then: 'the subscription cache handler is called once'
83             1 * mockDmiCacheHandler.add('test-id', _)
84         and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
85             testDmiSubscriptionsPerDmi.size() * mockDmiInEventProducer.publishDmiInEvent(
86                 "test-id", "dmi-1", "subscriptionCreateRequest", testDmiInEvent)
87         and: 'we schedule to send the response after configured time from the cache'
88             1 * mockNcmpOutEventProducer.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
89     }
90
91     def 'Consume valid and Overlapping Cm Notification Subscription NcmpIn Event'() {
92         given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
93             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
94             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
95             def noDeltaPredicates = []
96             mockCmSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
97         and: 'the cache handler returns for relevant subscription id'
98             1 * mockDmiCacheHandler.get('test-id') >> testDmiSubscriptionsPerDmi
99         and: 'the delta predicates is returned'
100             1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> noDeltaPredicates
101         when: 'the valid and unique event is consumed'
102             objectUnderTest.processSubscriptionCreateRequest('test-id', noDeltaPredicates)
103         then: 'the subscription cache handler is called once'
104             1 * mockDmiCacheHandler.add('test-id', _)
105         and: 'the subscription details are updated in the cache'
106             1 * mockDmiCacheHandler.updateDmiSubscriptionStatus('test-id', _, ACCEPTED)
107         and: 'we schedule to send the response after configured time from the cache'
108             1 * mockNcmpOutEventProducer.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
109     }
110
111     def 'Consume valid and but non-unique CmNotificationSubscription create message'() {
112         given: 'a cmNotificationSubscriptionNcmp in event'
113             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
114             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
115             mockCmSubscriptionPersistenceService.isUniqueSubscriptionId('test-id') >> false
116         and: 'relevant details is extracted from the event'
117             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
118             def predicates = testEventConsumed.getData().getPredicates()
119         and: 'the NCMP out in event mapper returns an event for rejected request'
120             def testNcmpOutEvent = new NcmpOutEvent()
121             1 * mockNcmpOutEventMapper.toNcmpOutEventForRejectedRequest(
122                 "test-id", _) >> testNcmpOutEvent
123         when: 'the valid but non-unique event is consumed'
124             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
125         then: 'the events handler method to publish DMI event is never called'
126             0 * mockDmiInEventProducer.publishDmiInEvent(_, _, _, _)
127         and: 'the events handler method to publish NCMP out event is called once'
128             1 * mockNcmpOutEventProducer.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', testNcmpOutEvent, false)
129     }
130
131     def 'Consume valid CmNotificationSubscriptionNcmpInEvent delete message'() {
132         given: 'a test subscription id'
133             def subscriptionId = 'test-id'
134         and: 'the persistence service returns datanodes'
135             1 * mockCmSubscriptionPersistenceService.getAllNodesForSubscriptionId(subscriptionId) >>
136                 [new DataNode(xpath: "/datastores/datastore[@name='ncmp-datastore:passthrough-running']/cm-handles/cm-handle[@id='ch-1']/filters/filter[@xpath='x/y']", leaves: ['xpath': 'x/y', 'subscriptionIds': ['test-id']]),
137                 new DataNode(xpath: "/datastores/datastore[@name='ncmp-datastore:passthrough-running']/cm-handles/cm-handle[@id='ch-2']/filters/filter[@xpath='y/z']", leaves: ['xpath': 'y/z', 'subscriptionIds': ['test-id']])]
138         and: 'the inventory persistence returns yang model cm handles'
139             1 * mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(dmiServiceName: 'dmi-1')
140             1 * mockInventoryPersistence.getYangModelCmHandle('ch-2') >> new YangModelCmHandle(dmiServiceName: 'dmi-2')
141         when: 'the subscription delete request is processed'
142             objectUnderTest.processSubscriptionDeleteRequest(subscriptionId)
143         then: 'the method to publish a dmi event is called with correct parameters'
144             1 * mockDmiInEventProducer.publishDmiInEvent(subscriptionId,'dmi-1','subscriptionDeleteRequest',_)
145             1 * mockDmiInEventProducer.publishDmiInEvent(subscriptionId,'dmi-2','subscriptionDeleteRequest',_)
146         and: 'the method to publish nmcp out event is called with correct parameters'
147             1 * mockNcmpOutEventProducer.publishNcmpOutEvent(subscriptionId, 'subscriptionDeleteResponse', null, true)
148     }
149
150     def 'Delete a subscriber for fully overlapping subscriptions'() {
151         given: 'a test subscription id'
152             def subscriptionId = 'test-id'
153         and: 'the persistence service returns datanodes with multiple subscribers'
154             1 * mockCmSubscriptionPersistenceService.getAllNodesForSubscriptionId(subscriptionId) >>
155                 [new DataNode(xpath: "/datastores/datastore[@name='ncmp-datastore:passthrough-running']/cm-handles/cm-handle[@id='ch-1']/filters/filter[@xpath='x/y']", leaves: ['xpath': 'x/y', 'subscriptionIds': ['test-id','other-id']]),
156                  new DataNode(xpath: "/datastores/datastore[@name='ncmp-datastore:passthrough-running']/cm-handles/cm-handle[@id='ch-2']/filters/filter[@xpath='y/z']", leaves: ['xpath': 'y/z', 'subscriptionIds': ['test-id','other-id']])]
157         and: 'the inventory persistence returns yang model cm handles'
158             1 * mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(dmiServiceName: 'dmi-1')
159             1 * mockInventoryPersistence.getYangModelCmHandle('ch-2') >> new YangModelCmHandle(dmiServiceName: 'dmi-2')
160         and: 'the cache handler returns the relevant maps whenever called'
161             2 * mockDmiCacheHandler.get(subscriptionId) >> ['dmi-1':[:],'dmi-2':[:]]
162         when: 'the subscription delete request is processed'
163             objectUnderTest.processSubscriptionDeleteRequest(subscriptionId)
164         then: 'the method to publish a dmi event is never called'
165             0 * mockDmiInEventProducer.publishDmiInEvent(_,_,_,_)
166         and: 'the cache handler is called to remove subscriber from database per dmi'
167             1 * mockDmiCacheHandler.removeFromDatabase('test-id', 'dmi-1')
168             1 * mockDmiCacheHandler.removeFromDatabase('test-id', 'dmi-2')
169         and: 'the method to publish nmcp out event is called with correct parameters'
170             1 * mockNcmpOutEventProducer.publishNcmpOutEvent(subscriptionId, 'subscriptionDeleteResponse', null, false)
171     }
172 }