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