2b91065592b9486343a990c40b4559f682ad9ca8
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2024 Nordix Foundation.
4  * Modifications Copyright (C) 2024 TechMahindra Ltd.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an 'AS IS' BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.impl.cmnotificationsubscription.utils
23
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.api.CpsDataService
26 import org.onap.cps.api.CpsQueryService
27 import org.onap.cps.spi.model.DataNode
28 import org.onap.cps.utils.ContentType
29 import org.onap.cps.utils.JsonObjectMapper
30 import spock.lang.Specification
31
32 import static CmSubscriptionPersistenceService.CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_FILTERS_WITH_DATASTORE_AND_CMHANDLE
33 import static CmSubscriptionPersistenceService.CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_DATASTORE_CMHANDLE_AND_XPATH
34 import static CmSubscriptionPersistenceService.CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_ID
35 import static org.onap.cps.ncmp.api.data.models.DatastoreType.PASSTHROUGH_OPERATIONAL
36 import static org.onap.cps.ncmp.api.data.models.DatastoreType.PASSTHROUGH_RUNNING
37 import static org.onap.cps.spi.FetchDescendantsOption.DIRECT_CHILDREN_ONLY
38 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS
39
40 class CmSubscriptionPersistenceServiceSpec extends Specification {
41
42     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
43     def mockCpsQueryService = Mock(CpsQueryService)
44     def mockCpsDataService = Mock(CpsDataService)
45
46     def objectUnderTest = new CmSubscriptionPersistenceService(jsonObjectMapper, mockCpsQueryService, mockCpsDataService)
47
48     def 'Check ongoing cm subscription #scenario'() {
49         given: 'a valid cm subscription query'
50             def cpsPathQuery = "/datastores/datastore[@name='ncmp-datastore:passthrough-running']/cm-handles/cm-handle[@id='ch-1']/filters/filter[@xpath='/cps/path']"
51         and: 'datanodes optionally returned'
52             1 * mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
53                 cpsPathQuery, OMIT_DESCENDANTS) >> dataNode
54         when: 'we check for an ongoing cm subscription'
55             def response = objectUnderTest.isOngoingCmSubscription(PASSTHROUGH_RUNNING, 'ch-1', '/cps/path')
56         then: 'we get expected response'
57             assert response == isOngoingCmSubscription
58         where: 'following scenarios are used'
59             scenario                  | dataNode                                                                            || isOngoingCmSubscription
60             'valid datanodes present' | [new DataNode(xpath: '/cps/path', leaves: ['subscriptionIds': ['sub-1', 'sub-2']])] || true
61             'no datanodes present'    | []                                                                                  || false
62     }
63
64     def 'Checking uniqueness of incoming subscription ID'() {
65         given: 'a cps path with a subscription ID for querying'
66             def cpsPathQuery = CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_ID.formatted('some-sub')
67         and: 'relevant datanodes are returned'
68             1 * mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions', cpsPathQuery, OMIT_DESCENDANTS) >>
69                 dataNodes
70         when: 'a subscription ID is tested for uniqueness'
71             def result = objectUnderTest.isUniqueSubscriptionId('some-sub')
72         then: 'result is as expected'
73             assert result == isValidSubscriptionId
74         where: 'following scenarios are used'
75             scenario               | dataNodes        || isValidSubscriptionId
76             'datanodes present'    | [new DataNode()] || false
77             'no datanodes present' | []               || true
78     }
79
80     def 'Add new subscriber to an ongoing cm notification subscription'() {
81         given: 'a valid cm subscription path query'
82             def cpsPathQuery = CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_DATASTORE_CMHANDLE_AND_XPATH.formatted('ncmp-datastore:passthrough-running', 'ch-1', '/x/y')
83         and: 'a dataNode exists for the given cps path query'
84             mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
85                 cpsPathQuery, OMIT_DESCENDANTS) >> [new DataNode(xpath: cpsPathQuery, leaves: ['xpath': '/x/y', 'subscriptionIds': ['sub-1']])]
86         when: 'the method to add/update cm notification subscription is called'
87             objectUnderTest.addCmSubscription(PASSTHROUGH_RUNNING, 'ch-1', '/x/y', 'newSubId')
88         then: 'data service method to update list of subscribers is called once'
89             1 * mockCpsDataService.updateNodeLeaves(
90                 'NCMP-Admin',
91                 'cm-data-subscriptions',
92                 '/datastores/datastore[@name=\'ncmp-datastore:passthrough-running\']/cm-handles/cm-handle[@id=\'ch-1\']/filters',
93                 objectUnderTest.getSubscriptionDetailsAsJson('/x/y', ['sub-1', 'newSubId']), _, ContentType.JSON)
94     }
95
96     def 'Add new cm notification subscription for #datastoreType'() {
97         given: 'a valid cm subscription path query'
98             def cmSubscriptionCpsPathQuery = CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_DATASTORE_CMHANDLE_AND_XPATH.formatted(datastoreName, 'ch-1', '/x/y')
99             def cmHandleForSubscriptionPathQuery = CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_FILTERS_WITH_DATASTORE_AND_CMHANDLE.formatted(datastoreName, 'ch-1')
100         and: 'a parent node xpath for the cm subscription path above'
101             def parentNodeXpath = '/datastores/datastore[@name=\'%s\']/cm-handles'
102         and: 'a datanode does not exist for cm subscription path query'
103             mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
104                 cmSubscriptionCpsPathQuery,
105                 OMIT_DESCENDANTS) >> []
106         and: 'a datanode does not exist for the given cm handle subscription path query'
107             mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
108                 cmHandleForSubscriptionPathQuery, OMIT_DESCENDANTS) >> []
109         and: 'subscription is mapped as JSON'
110             def subscriptionAsJson = '{"cm-handle":[{"id":"ch-1","filters":' +
111                 objectUnderTest.getSubscriptionDetailsAsJson('/x/y', ['newSubId']) + '}]}'
112         when: 'the method to add/update cm notification subscription is called'
113             objectUnderTest.addCmSubscription(datastoreType, 'ch-1', '/x/y', 'newSubId')
114         then: 'data service method to create new subscription for given subscriber is called once with the correct parameters'
115             1 * mockCpsDataService.saveData(
116                 'NCMP-Admin',
117                 'cm-data-subscriptions',
118                 parentNodeXpath.formatted(datastoreName),
119                 subscriptionAsJson, _, ContentType.JSON)
120         where:
121             scenario                  | datastoreType           || datastoreName
122             'passthrough_running'     | PASSTHROUGH_RUNNING     || 'ncmp-datastore:passthrough-running'
123             'passthrough_operational' | PASSTHROUGH_OPERATIONAL || 'ncmp-datastore:passthrough-operational'
124     }
125
126     def 'Add new cm notification subscription when xpath does not exist for existing subscription cm handle'() {
127         given: 'a valid cm subscription path query'
128             def cmSubscriptionCpsPathQuery = CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_DATASTORE_CMHANDLE_AND_XPATH.formatted(datastoreName, 'ch-1', '/x/y')
129             def cmHandleForSubscriptionPathQuery = CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_FILTERS_WITH_DATASTORE_AND_CMHANDLE.formatted(datastoreName, 'ch-1')
130         and: 'a parent node xpath for given cm handle for subscription path above'
131             def parentNodeXpath = '/datastores/datastore[@name=\'%s\']/cm-handles/cm-handle[@id=\'%s\']/filters'
132         and: 'a datanode does not exist for cm subscription path query'
133             mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
134                 cmSubscriptionCpsPathQuery, OMIT_DESCENDANTS) >> []
135         and: 'a datanode exists for the given cm handle subscription path query'
136             mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
137                 cmHandleForSubscriptionPathQuery, OMIT_DESCENDANTS) >> [new DataNode()]
138         when: 'the method to add/update cm notification subscription is called'
139             objectUnderTest.addCmSubscription(datastoreType, 'ch-1', '/x/y', 'newSubId')
140         then: 'data service method to create new subscription for given subscriber is called once with the correct parameters'
141             1 * mockCpsDataService.saveListElements(
142                 'NCMP-Admin',
143                 'cm-data-subscriptions',
144                 parentNodeXpath.formatted(datastoreName, 'ch-1'),
145                 objectUnderTest.getSubscriptionDetailsAsJson('/x/y', ['newSubId']), _, ContentType.JSON)
146         where:
147             scenario                  | datastoreType           || datastoreName
148             'passthrough_running'     | PASSTHROUGH_RUNNING     || 'ncmp-datastore:passthrough-running'
149             'passthrough_operational' | PASSTHROUGH_OPERATIONAL || 'ncmp-datastore:passthrough-operational'
150     }
151
152     def 'Remove subscriber from a list of an ongoing cm notification subscription'() {
153         given: 'a subscription exists when queried'
154             def cpsPathQuery = CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_DATASTORE_CMHANDLE_AND_XPATH.formatted('ncmp-datastore:passthrough-running', 'ch-1', '/x/y')
155             mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
156                 cpsPathQuery, OMIT_DESCENDANTS) >> [new DataNode(xpath: cpsPathQuery, leaves: ['xpath': '/x/y', 'subscriptionIds': ['sub-1', 'sub-2']])]
157         when: 'the subscriber is removed'
158             objectUnderTest.removeCmSubscription(PASSTHROUGH_RUNNING, 'ch-1', '/x/y', 'sub-1')
159         then: 'the list of subscribers is updated'
160             1 * mockCpsDataService.updateNodeLeaves('NCMP-Admin', 'cm-data-subscriptions',
161                 '/datastores/datastore[@name=\'ncmp-datastore:passthrough-running\']/cm-handles/cm-handle[@id=\'ch-1\']/filters',
162                 objectUnderTest.getSubscriptionDetailsAsJson('/x/y', ['sub-2']), _, ContentType.JSON)
163     }
164
165     def 'Removing last ongoing subscription for datastore and cmhandle and xpath'() {
166         given: 'a subscription exists when queried but has only 1 subscriber'
167             mockCpsQueryService.queryDataNodes(
168                 'NCMP-Admin', 'cm-data-subscriptions',
169                 CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_WITH_DATASTORE_CMHANDLE_AND_XPATH.formatted('ncmp-datastore:passthrough-running', 'ch-1', '/x/y'),
170                 OMIT_DESCENDANTS) >> [new DataNode(leaves: ['xpath': '/x/y', 'subscriptionIds': ['sub-1']])]
171         and: 'the #scenario'
172             mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions',
173                 CPS_PATH_QUERY_FOR_CM_SUBSCRIPTION_FILTERS_WITH_DATASTORE_AND_CMHANDLE.formatted('ncmp-datastore:passthrough-running', 'ch-1'),
174                 DIRECT_CHILDREN_ONLY) >> [new DataNode(childDataNodes: listOfChildNodes)]
175         when: 'that last ongoing subscription is removed'
176             objectUnderTest.removeCmSubscription(PASSTHROUGH_RUNNING, 'ch-1', '/x/y', 'sub-1')
177         then: 'the subscription with empty subscriber list is removed'
178             1 * mockCpsDataService.deleteDataNode('NCMP-Admin', 'cm-data-subscriptions',
179                 '/datastores/datastore[@name=\'ncmp-datastore:passthrough-running\']/cm-handles/cm-handle[@id=\'ch-1\']/filters/filter[@xpath=\'/x/y\']',
180                 _)
181         and: 'method call to delete the cm handle is called the correct number of times'
182             numberOfCallsToDeleteCmHandle * mockCpsDataService.deleteDataNode('NCMP-Admin', 'cm-data-subscriptions',
183                 '/datastores/datastore[@name=\'ncmp-datastore:passthrough-running\']/cm-handles/cm-handle[@id=\'ch-1\']',
184                 _)
185         where:
186             scenario                                                          | listOfChildNodes || numberOfCallsToDeleteCmHandle
187             'cm handle in same datastore is used for other subscriptions'     | [new DataNode()] || 0
188             'cm handle in same datastore is NOT used for other subscriptions' | []               || 1
189     }
190
191     def 'Get all nodes for subscription id'() {
192         given: 'the query service returns nodes for subscription id'
193             def expectedDataNode = new DataNode(xpath: '/some/xpath')
194             def queryServiceResponse = [expectedDataNode].asCollection()
195             1 * mockCpsQueryService.queryDataNodes('NCMP-Admin', 'cm-data-subscriptions', '//filter/subscriptionIds[text()=\'some-id\']', OMIT_DESCENDANTS) >> queryServiceResponse
196         when: 'retrieving all nodes for subscription id'
197             def result = objectUnderTest.getAllNodesForSubscriptionId('some-id')
198         then: 'the result returns correct number of datanodes'
199             assert result.size() == 1
200         and: 'the attribute of the datanode is as expected'
201             assert result.iterator().next().xpath == expectedDataNode.xpath
202     }
203
204 }