Add debug port to ncmp in docker-compose
[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-2023 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 'CmHandle search endpoint test #scenario with blank cmHandleQueryParameters.'() {
133         given: 'a query object'
134             def cmHandleQueryParameters = "{}"
135         and: 'the mapper service returns a converted object'
136             ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
137         and: 'the service returns the desired results'
138             mockNetworkCmProxyDataService.executeCmHandleIdSearchForInventory(cmHandleQueryServiceParameters) >> serviceMockResponse
139         when: 'post request is performed & search is called with the given request parameters'
140             def response = mvc.perform(
141                     post("$ncmpBasePathV1/ch/searches")
142                             .contentType(MediaType.APPLICATION_JSON)
143                             .content(cmHandleQueryParameters)
144             ).andReturn().response
145         then: 'response status is OK'
146             assert response.status == HttpStatus.OK.value()
147         and: 'the response data matches the service response.'
148             jsonObjectMapper.convertJsonString(response.getContentAsString(), List) == serviceMockResponse
149         where: 'the service respond with'
150             scenario             | serviceMockResponse
151             'empty response'     | []
152             'populates response' | ['cmHandle1', 'cmHandle2']
153     }
154
155     def 'CmHandle search endpoint Error Handling.'() {
156         given: 'the mapper service returns a converted object'
157             ncmpRestInputMapper.toCmHandleQueryServiceParameters(_) >> cmHandleQueryServiceParameters
158         and: 'the service returns the desired results'
159             mockNetworkCmProxyDataService.executeCmHandleIdSearchForInventory(cmHandleQueryServiceParameters) >> []
160         when: 'post request is performed & search is called with the given request parameters'
161             def response = mvc.perform(
162                     post("$ncmpBasePathV1/ch/searches")
163                             .contentType(MediaType.APPLICATION_JSON)
164                             .content(cmHandleQueryParameters)
165             ).andReturn().response
166         then: 'response status is BAD_REQUEST'
167             assert response.status == HttpStatus.BAD_REQUEST.value()
168         where: 'the cmHandleQueryParameters are'
169             scenario          | cmHandleQueryParameters
170             'empty string'    | ""
171             'non-json string' | "this is a test"
172     }
173
174     def 'DMI Registration: All cm-handles operations processed successfully.'() {
175         given: 'a dmi plugin registration'
176             def dmiRegistrationRequest = '{}'
177         and: 'service can register cm-handles successfully'
178             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
179                 createdCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1')],
180                 updatedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-2')],
181                 removedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-3')]
182             )
183             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
184         when: 'registration endpoint is invoked'
185             def response = mvc.perform(
186                 post("$ncmpBasePathV1/ch")
187                     .contentType(MediaType.APPLICATION_JSON)
188                     .content(dmiRegistrationRequest)
189             ).andReturn().response
190         then: 'response status is ok'
191             response.status == HttpStatus.OK.value()
192         and: 'the response body is empty'
193             response.getContentAsString() == ''
194
195     }
196
197     def 'DMI Registration Error Handling: #scenario.'() {
198         given: 'a dmi plugin registration'
199             def dmiRegistrationRequest = '{}'
200         and: '#scenario: service failed to register few cm-handle'
201             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
202                 createdCmHandles: [createCmHandleResponse],
203                 updatedCmHandles: [updateCmHandleResponse],
204                 removedCmHandles: [removeCmHandleResponse]
205             )
206             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
207         when: 'registration endpoint is invoked'
208             def response = mvc.perform(
209                 post("$ncmpBasePathV1/ch")
210                     .contentType(MediaType.APPLICATION_JSON)
211                     .content(dmiRegistrationRequest)
212             ).andReturn().response
213         then: 'request status is internal server error'
214             response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
215         and: 'the response body is in the expected format'
216             def responseBody = jsonObjectMapper.convertJsonString(response.getContentAsString(), DmiPluginRegistrationErrorResponse)
217         and: 'contains only the failure responses'
218             responseBody.getFailedCreatedCmHandles() == expectedFailedCreatedCmHandle
219             responseBody.getFailedUpdatedCmHandles() == expectedFailedUpdateCmHandle
220             responseBody.getFailedRemovedCmHandles() == expectedFailedRemovedCmHandle
221         where:
222             scenario               | createCmHandleResponse         | updateCmHandleResponse         | removeCmHandleResponse         || expectedFailedCreatedCmHandle       | expectedFailedUpdateCmHandle        | expectedFailedRemovedCmHandle
223             'only create failed'   | failedResponse('cm-handle-1')  | successResponse('cm-handle-2') | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | []                                  | []
224             'only update failed'   | successResponse('cm-handle-1') | failedResponse('cm-handle-2')  | successResponse('cm-handle-3') || []                                  | [failedRestResponse('cm-handle-2')] | []
225             'only delete failed'   | successResponse('cm-handle-1') | successResponse('cm-handle-2') | failedResponse('cm-handle-3')  || []                                  | []                                  | [failedRestResponse('cm-handle-3')]
226             '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')]
227             'create update failed' | failedResponse('cm-handle-1')  | failedResponse('cm-handle-2')  | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | [failedRestResponse('cm-handle-2')] | []
228             'create delete failed' | failedResponse('cm-handle-1')  | successResponse('cm-handle-2') | failedResponse('cm-handle-3')  || [failedRestResponse('cm-handle-1')] | []                                  | [failedRestResponse('cm-handle-3')]
229             'update delete failed' | successResponse('cm-handle-1') | failedResponse('cm-handle-2')  | failedResponse('cm-handle-3')  || []                                  | [failedRestResponse('cm-handle-2')] | [failedRestResponse('cm-handle-3')]
230     }
231
232     def 'Get all cm handle IDs by DMI plugin identifier.'() {
233         given: 'an endpoint for returning cm handle IDs for a registered dmi plugin'
234             def getUrl = "$ncmpBasePathV1/ch/cmHandles?dmi-plugin-identifier=some-dmi-plugin-identifier"
235         and: 'a collection of cm handle IDs are returned'
236             1 * mockNetworkCmProxyDataService.getAllCmHandleIdsByDmiPluginIdentifier('some-dmi-plugin-identifier')
237                     >> ['cm-handle-id-1','cm-handle-id-2']
238         when: 'the endpoint is invoked'
239             def response = mvc.perform(
240                     get(getUrl)
241                             .contentType(MediaType.APPLICATION_JSON)
242                             .accept(MediaType.APPLICATION_JSON_VALUE)
243             ).andReturn().response
244         then: 'the response matches the result returned by the service layer'
245             assert response.contentAsString.contains('cm-handle-id-1')
246             assert response.contentAsString.contains('cm-handle-id-2')
247     }
248
249     def failedRestResponse(cmHandle) {
250         return new CmHandlerRegistrationErrorResponse('cmHandle': cmHandle, 'errorCode': '00', 'errorText': 'Failed')
251     }
252
253     def failedResponse(cmHandle) {
254         return CmHandleRegistrationResponse.createFailureResponse(cmHandle, new RuntimeException('Failed'))
255     }
256
257     def successResponse(cmHandle) {
258         return CmHandleRegistrationResponse.createSuccessResponse(cmHandle)
259     }
260
261 }