Merge "Filter on private properties of CM Handles"
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / NetworkCmProxyInventoryControllerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Bell Canada
4  *  Modifications Copyright (C) 2021-2022 Nordix Foundation
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.rest.controller
23
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.TestUtils
26 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
27 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
28 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
29 import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse
30 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters
31 import org.onap.cps.ncmp.rest.model.CmHandlerRegistrationErrorResponse
32 import org.onap.cps.ncmp.rest.model.DmiPluginRegistrationErrorResponse
33 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration
34 import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters
35 import org.onap.cps.utils.JsonObjectMapper
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.beans.factory.annotation.Value
39 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
40 import org.springframework.context.annotation.Import
41 import org.springframework.http.HttpStatus
42 import org.springframework.http.MediaType
43 import org.springframework.test.web.servlet.MockMvc
44 import spock.lang.Specification
45
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
47 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
48
49 @WebMvcTest(NetworkCmProxyInventoryController)
50 @Import(ObjectMapper)
51 class NetworkCmProxyInventoryControllerSpec extends Specification {
52
53     @Autowired
54     MockMvc mvc
55
56     @SpringBean
57     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
58
59     @SpringBean
60     NcmpRestInputMapper ncmpRestInputMapper = Mock()
61
62     DmiPluginRegistration mockDmiPluginRegistration = Mock()
63
64     CmHandleQueryServiceParameters cmHandleQueryServiceParameters = Mock()
65
66     @SpringBean
67     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
68
69     @Value('${rest.api.ncmp-inventory-base-path}/v1')
70     def ncmpBasePathV1
71
72     def 'Dmi plugin registration #scenario'() {
73         given: 'a dmi plugin registration with #scenario'
74             def jsonData = TestUtils.getResourceFileContent(dmiRegistrationJson)
75         and: 'the expected rest input as an object'
76             def expectedRestDmiPluginRegistration = jsonObjectMapper.convertJsonString(jsonData, RestDmiPluginRegistration)
77         and: 'the converter returns a dmi registration (only for the expected input object)'
78             ncmpRestInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration
79         when: 'post request is performed & registration is called with correct DMI plugin information'
80             def response = mvc.perform(
81                 post("$ncmpBasePathV1/ch")
82                     .contentType(MediaType.APPLICATION_JSON)
83                     .content(jsonData)
84             ).andReturn().response
85         then: 'the converted object is forwarded to the registration service'
86             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(mockDmiPluginRegistration) >> new DmiPluginRegistrationResponse()
87         and: 'response status is no content'
88             response.status == HttpStatus.OK.value()
89         where: 'the following registration json is used'
90             scenario                                                                       | dmiRegistrationJson
91             'multiple services, added, updated and removed cm handles and many properties' | 'dmi_registration_all_singing_and_dancing.json'
92             'updated cm handle with updated/new and removed properties'                    | 'dmi_registration_updates_only.json'
93             'without any properties'                                                       | 'dmi_registration_without_properties.json'
94     }
95
96     def 'Dmi plugin registration with invalid json'() {
97         given: 'a dmi plugin registration with #scenario'
98             def jsonDataWithUndefinedDataLabel = '{"notAdmiPlugin":""}'
99         when: 'post request is performed & registration is called with correct DMI plugin information'
100             def response = mvc.perform(
101                 post("$ncmpBasePathV1/ch")
102                     .contentType(MediaType.APPLICATION_JSON)
103                     .content(jsonDataWithUndefinedDataLabel)
104             ).andReturn().response
105         then: 'response status is bad request'
106             response.status == HttpStatus.BAD_REQUEST.value()
107     }
108
109     def 'CmHandle search endpoint test #scenario.'() {
110         given: 'a query object'
111             def cmHandleQueryParameters = jsonObjectMapper.asJsonString(new CmHandleQueryParameters())
112         and: 'the mapper service returns a converted object'
113             ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
114         and: 'the service returns the desired results'
115             mockNetworkCmProxyDataService.executeCmHandleIdSearchForInventory(cmHandleQueryServiceParameters) >> serviceMockResponse
116         when: 'post request is performed & search is called with the given request parameters'
117             def response = mvc.perform(
118                     post("$ncmpBasePathV1/ch/searches")
119                             .contentType(MediaType.APPLICATION_JSON)
120                             .content(cmHandleQueryParameters)
121             ).andReturn().response
122         then: 'response status is OK'
123             assert response.status == HttpStatus.OK.value()
124         and: 'the response data matches the service response.'
125             jsonObjectMapper.convertJsonString(response.getContentAsString(), List) == serviceMockResponse
126         where: 'the service respond with'
127             scenario             | serviceMockResponse
128             'empty response'     | []
129             'populates response' | ['cmHandle1', 'cmHandle2']
130     }
131
132     def 'DMI Registration: All cm-handles operations processed successfully.'() {
133         given: 'a dmi plugin registration'
134             def dmiRegistrationRequest = '{}'
135         and: 'service can register cm-handles successfully'
136             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
137                 createdCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1')],
138                 updatedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-2')],
139                 removedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-3')]
140             )
141             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
142         when: 'registration endpoint is invoked'
143             def response = mvc.perform(
144                 post("$ncmpBasePathV1/ch")
145                     .contentType(MediaType.APPLICATION_JSON)
146                     .content(dmiRegistrationRequest)
147             ).andReturn().response
148         then: 'response status is ok'
149             response.status == HttpStatus.OK.value()
150         and: 'the response body is empty'
151             response.getContentAsString() == ''
152
153     }
154
155     def 'DMI Registration Error Handling: #scenario.'() {
156         given: 'a dmi plugin registration'
157             def dmiRegistrationRequest = '{}'
158         and: '#scenario: service failed to register few cm-handle'
159             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
160                 createdCmHandles: [createCmHandleResponse],
161                 updatedCmHandles: [updateCmHandleResponse],
162                 removedCmHandles: [removeCmHandleResponse]
163             )
164             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
165         when: 'registration endpoint is invoked'
166             def response = mvc.perform(
167                 post("$ncmpBasePathV1/ch")
168                     .contentType(MediaType.APPLICATION_JSON)
169                     .content(dmiRegistrationRequest)
170             ).andReturn().response
171         then: 'request status is internal server error'
172             response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
173         and: 'the response body is in the expected format'
174             def responseBody = jsonObjectMapper.convertJsonString(response.getContentAsString(), DmiPluginRegistrationErrorResponse)
175         and: 'contains only the failure responses'
176             responseBody.getFailedCreatedCmHandles() == expectedFailedCreatedCmHandle
177             responseBody.getFailedUpdatedCmHandles() == expectedFailedUpdateCmHandle
178             responseBody.getFailedRemovedCmHandles() == expectedFailedRemovedCmHandle
179         where:
180             scenario               | createCmHandleResponse         | updateCmHandleResponse         | removeCmHandleResponse         || expectedFailedCreatedCmHandle       | expectedFailedUpdateCmHandle        | expectedFailedRemovedCmHandle
181             'only create failed'   | failedResponse('cm-handle-1')  | successResponse('cm-handle-2') | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | []                                  | []
182             'only update failed'   | successResponse('cm-handle-1') | failedResponse('cm-handle-2')  | successResponse('cm-handle-3') || []                                  | [failedRestResponse('cm-handle-2')] | []
183             'only delete failed'   | successResponse('cm-handle-1') | successResponse('cm-handle-2') | failedResponse('cm-handle-3')  || []                                  | []                                  | [failedRestResponse('cm-handle-3')]
184             'all three failed'     | failedResponse('cm-handle-1')  | failedResponse('cm-handle-2')  | failedResponse('cm-handle-3')  || [failedRestResponse('cm-handle-1')] | [failedRestResponse('cm-handle-2')] | [failedRestResponse('cm-handle-3')]
185             'create update failed' | failedResponse('cm-handle-1')  | failedResponse('cm-handle-2')  | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | [failedRestResponse('cm-handle-2')] | []
186             'create delete failed' | failedResponse('cm-handle-1')  | successResponse('cm-handle-2') | failedResponse('cm-handle-3')  || [failedRestResponse('cm-handle-1')] | []                                  | [failedRestResponse('cm-handle-3')]
187             'update delete failed' | successResponse('cm-handle-1') | failedResponse('cm-handle-2')  | failedResponse('cm-handle-3')  || []                                  | [failedRestResponse('cm-handle-2')] | [failedRestResponse('cm-handle-3')]
188     }
189
190     def 'Get all cm handle IDs by DMI plugin identifier.'() {
191         given: 'an endpoint for returning cm handle IDs for a registered dmi plugin'
192             def getUrl = "$ncmpBasePathV1/ch/cmHandles?dmi-plugin-identifier=some-dmi-plugin-identifier"
193         and: 'a collection of cm handle IDs are returned'
194             1 * mockNetworkCmProxyDataService.getAllCmHandleIdsByDmiPluginIdentifier('some-dmi-plugin-identifier')
195                     >> ['cm-handle-id-1','cm-handle-id-2']
196         when: 'the endpoint is invoked'
197             def response = mvc.perform(
198                     get(getUrl)
199                             .contentType(MediaType.APPLICATION_JSON)
200                             .accept(MediaType.APPLICATION_JSON_VALUE)
201             ).andReturn().response
202         then: 'the response matches the result returned by the service layer'
203             assert response.contentAsString.contains('cm-handle-id-1')
204             assert response.contentAsString.contains('cm-handle-id-2')
205     }
206
207     def failedRestResponse(cmHandle) {
208         return new CmHandlerRegistrationErrorResponse('cmHandle': cmHandle, 'errorCode': '00', 'errorText': 'Failed')
209     }
210
211     def failedResponse(cmHandle) {
212         return CmHandleRegistrationResponse.createFailureResponse(cmHandle, new RuntimeException("Failed"))
213     }
214
215     def successResponse(cmHandle) {
216         return CmHandleRegistrationResponse.createSuccessResponse(cmHandle)
217     }
218
219 }