Get cm-handle public properties endpoint
[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.CmHandlerRegistrationErrorResponse
31 import org.onap.cps.ncmp.rest.model.DmiPluginRegistrationErrorResponse
32 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.spockframework.spring.SpringBean
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.beans.factory.annotation.Value
37 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
38 import org.springframework.context.annotation.Import
39 import org.springframework.http.HttpStatus
40 import org.springframework.http.MediaType
41 import org.springframework.test.web.servlet.MockMvc
42 import spock.lang.Specification
43
44 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
45
46 @WebMvcTest(NetworkCmProxyInventoryController)
47 @Import(ObjectMapper)
48 class NetworkCmProxyInventoryControllerSpec extends Specification {
49
50     @Autowired
51     MockMvc mvc
52
53     @SpringBean
54     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
55
56     @SpringBean
57     NcmpRestInputMapper ncmpRestInputMapper = Mock()
58
59     DmiPluginRegistration mockDmiPluginRegistration = Mock()
60
61     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
62
63     @Value('${rest.api.ncmp-inventory-base-path}/v1')
64     def ncmpBasePathV1
65
66     def 'Dmi plugin registration #scenario'() {
67         given: 'a dmi plugin registration with #scenario'
68             def jsonData = TestUtils.getResourceFileContent(dmiRegistrationJson)
69         and: 'the expected rest input as an object'
70             def expectedRestDmiPluginRegistration = jsonObjectMapper.convertJsonString(jsonData, RestDmiPluginRegistration)
71         and: 'the converter returns a dmi registration (only for the expected input object)'
72             ncmpRestInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration
73         when: 'post request is performed & registration is called with correct DMI plugin information'
74             def response = mvc.perform(
75                 post("$ncmpBasePathV1/ch")
76                     .contentType(MediaType.APPLICATION_JSON)
77                     .content(jsonData)
78             ).andReturn().response
79         then: 'the converted object is forwarded to the registration service'
80             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(mockDmiPluginRegistration) >> new DmiPluginRegistrationResponse()
81         and: 'response status is no content'
82             response.status == HttpStatus.OK.value()
83         where: 'the following registration json is used'
84             scenario                                                                       | dmiRegistrationJson
85             'multiple services, added, updated and removed cm handles and many properties' | 'dmi_registration_all_singing_and_dancing.json'
86             'updated cm handle with updated/new and removed properties'                    | 'dmi_registration_updates_only.json'
87             'without any properties'                                                       | 'dmi_registration_without_properties.json'
88     }
89
90     def 'Dmi plugin registration with invalid json'() {
91         given: 'a dmi plugin registration with #scenario'
92             def jsonDataWithUndefinedDataLabel = '{"notAdmiPlugin":""}'
93         when: 'post request is performed & registration is called with correct DMI plugin information'
94             def response = mvc.perform(
95                 post("$ncmpBasePathV1/ch")
96                     .contentType(MediaType.APPLICATION_JSON)
97                     .content(jsonDataWithUndefinedDataLabel)
98             ).andReturn().response
99         then: 'response status is bad request'
100             response.status == HttpStatus.BAD_REQUEST.value()
101     }
102
103     def 'DMI Registration: All cm-handles operations processed successfully.'() {
104         given: 'a dmi plugin registration'
105             def dmiRegistrationRequest = '{}'
106         and: 'service can register cm-handles successfully'
107             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
108                 createdCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1')],
109                 updatedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-2')],
110                 removedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-3')]
111             )
112             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
113         when: 'registration endpoint is invoked'
114             def response = mvc.perform(
115                 post("$ncmpBasePathV1/ch")
116                     .contentType(MediaType.APPLICATION_JSON)
117                     .content(dmiRegistrationRequest)
118             ).andReturn().response
119         then: 'response status is ok'
120             response.status == HttpStatus.OK.value()
121         and: 'the response body is empty'
122             response.getContentAsString() == ''
123
124     }
125
126     def 'DMI Registration Error Handling: #scenario.'() {
127         given: 'a dmi plugin registration'
128             def dmiRegistrationRequest = '{}'
129         and: '#scenario: service failed to register few cm-handle'
130             def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
131                 createdCmHandles: [createCmHandleResponse],
132                 updatedCmHandles: [updateCmHandleResponse],
133                 removedCmHandles: [removeCmHandleResponse]
134             )
135             mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
136         when: 'registration endpoint is invoked'
137             def response = mvc.perform(
138                 post("$ncmpBasePathV1/ch")
139                     .contentType(MediaType.APPLICATION_JSON)
140                     .content(dmiRegistrationRequest)
141             ).andReturn().response
142         then: 'request status is internal server error'
143             response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
144         and: 'the response body is in the expected format'
145             def responseBody = jsonObjectMapper.convertJsonString(response.getContentAsString(), DmiPluginRegistrationErrorResponse)
146         and: 'contains only the failure responses'
147             responseBody.getFailedCreatedCmHandles() == expectedFailedCreatedCmHandle
148             responseBody.getFailedUpdatedCmHandles() == expectedFailedUpdateCmHandle
149             responseBody.getFailedRemovedCmHandles() == expectedFailedRemovedCmHandle
150         where:
151             scenario               | createCmHandleResponse         | updateCmHandleResponse         | removeCmHandleResponse         || expectedFailedCreatedCmHandle       | expectedFailedUpdateCmHandle        | expectedFailedRemovedCmHandle
152             'only create failed'   | failedResponse('cm-handle-1')  | successResponse('cm-handle-2') | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | []                                  | []
153             'only update failed'   | successResponse('cm-handle-1') | failedResponse('cm-handle-2')  | successResponse('cm-handle-3') || []                                  | [failedRestResponse('cm-handle-2')] | []
154             'only delete failed'   | successResponse('cm-handle-1') | successResponse('cm-handle-2') | failedResponse('cm-handle-3')  || []                                  | []                                  | [failedRestResponse('cm-handle-3')]
155             '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')]
156             'create update failed' | failedResponse('cm-handle-1')  | failedResponse('cm-handle-2')  | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | [failedRestResponse('cm-handle-2')] | []
157             'create delete failed' | failedResponse('cm-handle-1')  | successResponse('cm-handle-2') | failedResponse('cm-handle-3')  || [failedRestResponse('cm-handle-1')] | []                                  | [failedRestResponse('cm-handle-3')]
158             'update delete failed' | successResponse('cm-handle-1') | failedResponse('cm-handle-2')  | failedResponse('cm-handle-3')  || []                                  | [failedRestResponse('cm-handle-2')] | [failedRestResponse('cm-handle-3')]
159     }
160
161     def failedRestResponse(cmHandle) {
162         return new CmHandlerRegistrationErrorResponse('cmHandle': cmHandle, 'errorCode': '00', 'errorText': 'Failed')
163     }
164
165     def failedResponse(cmHandle) {
166         return CmHandleRegistrationResponse.createFailureResponse(cmHandle, new RuntimeException("Failed"))
167     }
168
169     def successResponse(cmHandle) {
170         return CmHandleRegistrationResponse.createSuccessResponse(cmHandle)
171     }
172
173 }