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