Unable to change state from LOCKED to ADVISED
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 Bell Canada
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.ncmp.api.impl
24
25 import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.inventory.CompositeState
29 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
30 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters
31 import org.onap.cps.ncmp.api.models.ConditionApiProperties
32 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
33 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
34 import org.onap.cps.spi.exceptions.DataValidationException
35 import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService
36 import org.onap.cps.spi.model.CmHandleQueryServiceParameters
37 import org.onap.cps.spi.model.ConditionProperties
38 import spock.lang.Shared
39
40 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
41 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
42 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
43 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
44
45 import org.onap.cps.utils.JsonObjectMapper
46 import com.fasterxml.jackson.databind.ObjectMapper
47 import org.onap.cps.api.CpsAdminService
48 import org.onap.cps.api.CpsDataService
49 import org.onap.cps.api.CpsModuleService
50 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
51 import org.onap.cps.spi.FetchDescendantsOption
52 import org.onap.cps.spi.model.DataNode
53 import org.springframework.http.HttpStatus
54 import org.springframework.http.ResponseEntity
55 import spock.lang.Specification
56
57 class NetworkCmProxyDataServiceImplSpec extends Specification {
58
59     def mockCpsDataService = Mock(CpsDataService)
60     def mockCpsModuleService = Mock(CpsModuleService)
61     def mockCpsAdminService = Mock(CpsAdminService)
62     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
63     def mockDmiDataOperations = Mock(DmiDataOperations)
64     def nullNetworkCmProxyDataServicePropertyHandler = null
65     def mockInventoryPersistence = Mock(InventoryPersistence)
66     def mockModuleSyncService = Mock(ModuleSyncService)
67     def mockDmiPluginRegistration = Mock(DmiPluginRegistration)
68     def mockCpsCmHandlerQueryService = Mock(NetworkCmProxyCmHandlerQueryService)
69
70     def NO_TOPIC = null
71     def NO_REQUEST_ID = null
72     @Shared
73     def OPTIONS_PARAM = '(a=1,b=2)'
74     @Shared
75     def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
76
77     def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations,
78         mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence,
79         mockModuleSyncService, mockCpsCmHandlerQueryService)
80
81     def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
82
83     def dataNode = new DataNode(leaves: ['id': 'some-cm-handle', 'dmi-service-name': 'testDmiService'])
84
85     def 'Write resource data for pass-through running from DMI using POST.'() {
86         given: 'cpsDataService returns valid datanode'
87             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
88                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
89         when: 'write resource data is called'
90             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
91                 'testResourceId', CREATE,
92                 '{some-json}', 'application/json')
93         then: 'DMI called with correct data'
94             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
95                 CREATE, '{some-json}', 'application/json')
96                 >> { new ResponseEntity<>(HttpStatus.CREATED) }
97     }
98
99     def 'Write resource data for pass-through running from DMI using an invalid id.'() {
100         when: 'write resource data is called'
101             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('invalid cm handle name',
102                 'testResourceId', CREATE,
103                 '{some-json}', 'application/json')
104         then: 'exception is thrown'
105             thrown(DataValidationException.class)
106         and: 'DMI is not invoked'
107             0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _)
108     }
109
110     def 'Get resource data for pass-through operational from DMI.'() {
111         given: 'get data node is called'
112             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
113                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
114         and: 'get resource data from DMI is called'
115             mockDmiDataOperations.getResourceDataFromDmi(
116                 'testCmHandle',
117                 'testResourceId',
118                 OPTIONS_PARAM,
119                 PASSTHROUGH_OPERATIONAL,
120                 NO_REQUEST_ID,
121                 NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
122         when: 'get resource data operational for cm-handle is called'
123             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
124                 'testResourceId',
125                 OPTIONS_PARAM,
126                 NO_TOPIC,
127                 NO_REQUEST_ID)
128         then: 'DMI returns a json response'
129             response == 'dmi-response'
130     }
131
132     def 'Get resource data for pass-through running from DMI.'() {
133         given: 'cpsDataService returns valid data node'
134             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
135                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
136         and: 'DMI returns valid response and data'
137             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
138                 'testResourceId',
139                 OPTIONS_PARAM,
140                 PASSTHROUGH_RUNNING,
141                 NO_REQUEST_ID,
142                 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
143         when: 'get resource data is called'
144             def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
145                 'testResourceId',
146                 OPTIONS_PARAM,
147                 NO_TOPIC,
148                 NO_REQUEST_ID)
149         then: 'get resource data returns expected response'
150             response == '{dmi-response}'
151     }
152
153     def 'Getting Yang Resources.'() {
154         when: 'yang resources is called'
155             objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
156         then: 'CPS module services is invoked for the correct dataspace and cm handle'
157             1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
158     }
159
160     def 'Getting Yang Resources with an invalid #scenario.'() {
161         when: 'yang resources is called'
162             objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces')
163         then: 'a data validation exception is thrown'
164             thrown(DataValidationException)
165         and: 'CPS module services is not invoked'
166             0 * mockCpsModuleService.getYangResourcesModuleReferences(*_)
167     }
168
169     def 'Get a cm handle.'() {
170         given: 'the system returns a yang modelled cm handle'
171             def dmiServiceName = 'some service name'
172             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
173             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
174             def compositeState = new CompositeState(cmHandleState: 'ADVISED')
175             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName,
176                 dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
177             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
178         when: 'getting cm handle details for a given cm handle id from ncmp service'
179             def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle')
180         then: 'the result returns the correct data'
181             result.cmHandleId == 'some-cm-handle'
182             result.dmiProperties ==[ Book:'Romance Novel' ]
183             result.publicProperties == [ "Public Book":'Public Romance Novel' ]
184             result.compositeState.cmHandleState == CmHandleState.ADVISED
185     }
186
187     def 'Get a cm handle with an invalid id.'() {
188         when: 'getting cm handle details for a given cm handle id with an invalid name'
189             objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces')
190         then: 'an exception is thrown'
191             thrown(DataValidationException)
192         and: 'the yang model cm handle retriever is not invoked'
193             0 * mockInventoryPersistence.getYangModelCmHandle(*_)
194     }
195
196     def 'Get cm handle public properties'() {
197         given: 'a yang modelled cm handle'
198             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
199             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
200             def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
201         and: 'the system returns this yang modelled cm handle'
202             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
203         when: 'getting cm handle public properties for a given cm handle id from ncmp service'
204             def result = objectUnderTest.getCmHandlePublicProperties('some-cm-handle')
205         then: 'the result returns the correct data'
206             result == [ 'public prop' : 'some public prop' ]
207     }
208
209     def 'Get cm handle public properties with an invalid id.'() {
210         when: 'getting cm handle details for a given cm handle id with an invalid name'
211             objectUnderTest.getCmHandlePublicProperties('invalid cm handle with spaces')
212         then: 'an exception is thrown'
213             thrown(DataValidationException)
214         and: 'the yang model cm handle retriever is not invoked'
215             0 * mockInventoryPersistence.getYangModelCmHandle(*_)
216     }
217
218     def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
219         given: 'cpsDataService returns valid datanode'
220             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
221                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
222         when: 'get resource data is called'
223             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
224                 'testResourceId', UPDATE,
225                 '{some-json}', 'application/json')
226         then: 'DMI called with correct data'
227             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
228                 UPDATE, '{some-json}', 'application/json')
229                 >> { new ResponseEntity<>(HttpStatus.OK) }
230     }
231
232     def 'Verify modules and create anchor params'() {
233         given: 'dmi plugin registration return created cm handles'
234             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1',
235                 dmiDataPlugin: 'service2')
236             dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
237             mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle]
238         when: 'parse and create cm handle in dmi registration then sync module'
239             objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration)
240         then: 'validate params for creating anchor and list elements'
241         1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', _, null) >> {
242             args -> {
243                 assert args[3].startsWith('{"cm-handles":[{"id":"some-cm-handle-id","state":{"cm-handle-state":"ADVISED","last-update-time":"20')
244             }
245         }
246     }
247
248     def 'Execute cm handle id search'() {
249         given: 'valid CmHandleQueryApiParameters input'
250             def cmHandleQueryApiParameters = new CmHandleQueryApiParameters()
251             def conditionApiProperties = new ConditionApiProperties()
252             conditionApiProperties.conditionName = 'hasAllModules'
253             conditionApiProperties.conditionParameters = [[moduleName: 'module-name-1']]
254             cmHandleQueryApiParameters.cmHandleQueryParameters = [conditionApiProperties]
255         and: 'valid CmHandleQueryParameters input'
256             def cmHandleQueryParameters = new CmHandleQueryServiceParameters()
257             def conditionProperties = new ConditionProperties()
258             conditionProperties.conditionName = 'hasAllModules'
259             conditionProperties.conditionParameters = [[moduleName: 'module-name-1']]
260             cmHandleQueryParameters.cmHandleQueryParameters = [conditionProperties]
261         and: 'query cm handle method return with a data node list'
262             mockCpsCmHandlerQueryService.queryCmHandles(cmHandleQueryParameters) >> [new DataNode(leaves: [id: 'cm-handle-id-1'])]
263         when: 'execute cm handle search is called'
264             def result = objectUnderTest.executeCmHandleIdSearch(cmHandleQueryApiParameters)
265         then: 'result is the same collection as returned by the CPS Data Service'
266             assert result == ['cm-handle-id-1'] as Set
267     }
268 }