5690b0d26168fd6acce8481df02b0a485632871a
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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.CmDataJobSubscriptionPersistenceService
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(CmDataJobSubscriptionPersistenceService)
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 new 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         and: 'the persistence service confirms subscription id is new (not used for other subscription)'
70             mockCmSubscriptionPersistenceService.isNewSubscriptionId('test-id') >> true
71         and: 'relevant details is extracted from the event'
72             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
73             def predicates = testEventConsumed.getData().getPredicates()
74         and: 'the cache handler returns for relevant subscription id'
75             1 * mockDmiCacheHandler.get("test-id") >> testDmiSubscriptionsPerDmi
76         and: 'the delta predicates is returned'
77             1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> testListOfDeltaPredicates
78         and: 'the DMI in event mapper returns cm notification subscription event'
79             def testDmiInEvent = new DmiInEvent()
80             1 * mockDmiInEventMapper.toDmiInEvent(testListOfDeltaPredicates) >> testDmiInEvent
81         when: 'the valid and unique event is consumed'
82             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
83         then: 'the subscription cache handler is called once'
84             1 * mockDmiCacheHandler.add('test-id', _)
85         and: 'the events handler method to send DMI event is called correct number of times with the correct parameters'
86             testDmiSubscriptionsPerDmi.size() * mockDmiInEventProducer.sendDmiInEvent(
87                 "test-id", "dmi-1", "subscriptionCreateRequest", testDmiInEvent)
88         and: 'we schedule to send the response after configured time from the cache'
89             1 * mockNcmpOutEventProducer.sendNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
90     }
91
92     def 'Consume valid and Overlapping Cm Notification Subscription NcmpIn Event'() {
93         given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
94             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
95             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
96             def noDeltaPredicates = []
97         and: 'the persistence service confirms subscription id is new (not used for other subscription)'
98             mockCmSubscriptionPersistenceService.isNewSubscriptionId('test-id') >> true
99         and: 'the cache handler returns for relevant subscription id'
100             1 * mockDmiCacheHandler.get('test-id') >> testDmiSubscriptionsPerDmi
101         and: 'the delta predicates is returned'
102             1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> noDeltaPredicates
103         when: 'the valid and unique event is consumed'
104             objectUnderTest.processSubscriptionCreateRequest('test-id', noDeltaPredicates)
105         then: 'the subscription cache handler is called once'
106             1 * mockDmiCacheHandler.add('test-id', _)
107         and: 'the subscription details are updated in the cache'
108             1 * mockDmiCacheHandler.updateDmiSubscriptionStatus('test-id', _, ACCEPTED)
109         and: 'we schedule to send the response after configured time from the cache'
110             1 * mockNcmpOutEventProducer.sendNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
111     }
112
113     def 'Consume valid and but non-unique CmNotificationSubscription create message'() {
114         given: 'a cmNotificationSubscriptionNcmp in event'
115             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
116             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
117         and: 'the persistence service confirms subscription id is not new (already used subscription)'
118             mockCmSubscriptionPersistenceService.isNewSubscriptionId('test-id') >> false
119         and: 'relevant details is extracted from the event'
120             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
121             def predicates = testEventConsumed.getData().getPredicates()
122         and: 'the NCMP out in event mapper returns an event for rejected request'
123             def testNcmpOutEvent = new NcmpOutEvent()
124             1 * mockNcmpOutEventMapper.toNcmpOutEventForRejectedRequest(
125                 "test-id", _) >> testNcmpOutEvent
126         when: 'the valid but non-unique event is consumed'
127             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
128         then: 'the events handler method to send DMI event is never called'
129             0 * mockDmiInEventProducer.sendDmiInEvent(_, _, _, _)
130         and: 'the events handler method to send NCMP out event is called once'
131             1 * mockNcmpOutEventProducer.sendNcmpOutEvent('test-id', 'subscriptionCreateResponse', testNcmpOutEvent, false)
132     }
133
134     def 'Consume valid CmNotificationSubscriptionNcmpInEvent delete message'() {
135         given: 'a test subscription id'
136             def subscriptionId = 'test-id'
137         and: 'the persistence service returns datanodes'
138             1 * mockCmSubscriptionPersistenceService.getAffectedDataNodes(subscriptionId) >>
139                 [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']]),
140                  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']])]
141         and: 'the inventory persistence returns yang model cm handles'
142             1 * mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(dmiServiceName: 'dmi-1')
143             1 * mockInventoryPersistence.getYangModelCmHandle('ch-2') >> new YangModelCmHandle(dmiServiceName: 'dmi-2')
144         when: 'the subscription delete request is processed'
145             objectUnderTest.processSubscriptionDeleteRequest(subscriptionId)
146         then: 'the method to send a dmi event is called with correct parameters'
147             1 * mockDmiInEventProducer.sendDmiInEvent(subscriptionId,'dmi-1','subscriptionDeleteRequest',_)
148             1 * mockDmiInEventProducer.sendDmiInEvent(subscriptionId,'dmi-2','subscriptionDeleteRequest',_)
149         and: 'the method to send nmcp out event is called with correct parameters'
150             1 * mockNcmpOutEventProducer.sendNcmpOutEvent(subscriptionId, 'subscriptionDeleteResponse', null, true)
151     }
152
153     def 'Delete a subscriber for fully overlapping subscriptions'() {
154         given: 'a test subscription id'
155             def subscriptionId = 'test-id'
156         and: 'the persistence service returns datanodes with multiple subscribers'
157             1 * mockCmSubscriptionPersistenceService.getAffectedDataNodes(subscriptionId) >>
158                 [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']]),
159                  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']])]
160         and: 'the inventory persistence returns yang model cm handles'
161             1 * mockInventoryPersistence.getYangModelCmHandle('ch-1') >> new YangModelCmHandle(dmiServiceName: 'dmi-1')
162             1 * mockInventoryPersistence.getYangModelCmHandle('ch-2') >> new YangModelCmHandle(dmiServiceName: 'dmi-2')
163         and: 'the cache handler returns the relevant maps whenever called'
164             2 * mockDmiCacheHandler.get(subscriptionId) >> ['dmi-1':[:],'dmi-2':[:]]
165         when: 'the subscription delete request is processed'
166             objectUnderTest.processSubscriptionDeleteRequest(subscriptionId)
167         then: 'the method to send a dmi event is never called'
168             0 * mockDmiInEventProducer.sendDmiInEvent(_,_,_,_)
169         and: 'the cache handler is called to remove subscriber from database per dmi'
170             1 * mockDmiCacheHandler.removeFromDatabase('test-id', 'dmi-1')
171             1 * mockDmiCacheHandler.removeFromDatabase('test-id', 'dmi-2')
172         and: 'the method to send ncmp out event is called with correct parameters'
173             1 * mockNcmpOutEventProducer.sendNcmpOutEvent(subscriptionId, 'subscriptionDeleteResponse', null, false)
174     }
175 }