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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.rest.controller
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
44 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_ALREADY_EXIST
45 import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_DOES_NOT_EXIST
46 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
48 @WebMvcTest(NetworkCmProxyInventoryController)
50 class NetworkCmProxyInventoryControllerSpec extends Specification {
56 NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
59 NcmpRestInputMapper ncmpRestInputMapper = Mock()
61 DmiPluginRegistration mockDmiPluginRegistration = Mock()
63 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
65 @Value('${rest.api.ncmp-inventory-base-path}/v1')
68 def 'Dmi plugin registration #scenario'() {
69 given: 'a dmi plugin registration with #scenario'
70 def jsonData = TestUtils.getResourceFileContent(dmiRegistrationJson)
71 and: 'the expected rest input as an object'
72 def expectedRestDmiPluginRegistration = jsonObjectMapper.convertJsonString(jsonData, RestDmiPluginRegistration)
73 and: 'the converter returns a dmi registration (only for the expected input object)'
74 ncmpRestInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration
75 when: 'post request is performed & registration is called with correct DMI plugin information'
76 def response = mvc.perform(
77 post("$ncmpBasePathV1/ch")
78 .contentType(MediaType.APPLICATION_JSON)
80 ).andReturn().response
81 then: 'the converted object is forwarded to the registration service'
82 1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(mockDmiPluginRegistration) >> new DmiPluginRegistrationResponse()
83 and: 'response status is no content'
84 response.status == HttpStatus.OK.value()
85 where: 'the following registration json is used'
86 scenario | dmiRegistrationJson
87 'multiple services, added, updated and removed cm handles and many properties' | 'dmi_registration_all_singing_and_dancing.json'
88 'updated cm handle with updated/new and removed properties' | 'dmi_registration_updates_only.json'
89 'without any properties' | 'dmi_registration_without_properties.json'
92 def 'Dmi plugin registration with invalid json'() {
93 given: 'a dmi plugin registration with #scenario'
94 def jsonDataWithUndefinedDataLabel = '{"notAdmiPlugin":""}'
95 when: 'post request is performed & registration is called with correct DMI plugin information'
96 def response = mvc.perform(
97 post("$ncmpBasePathV1/ch")
98 .contentType(MediaType.APPLICATION_JSON)
99 .content(jsonDataWithUndefinedDataLabel)
100 ).andReturn().response
101 then: 'response status is bad request'
102 response.status == HttpStatus.BAD_REQUEST.value()
105 def 'DMI Registration: All cm-handles operations processed successfully.'() {
106 given: 'a dmi plugin registration'
107 def dmiRegistrationRequest = '{}'
108 and: 'service can register cm-handles successfully'
109 def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
110 createdCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-1')],
111 updatedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-2')],
112 removedCmHandles: [CmHandleRegistrationResponse.createSuccessResponse('cm-handle-3')]
114 mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
115 when: 'registration endpoint is invoked'
116 def response = mvc.perform(
117 post("$ncmpBasePathV1/ch")
118 .contentType(MediaType.APPLICATION_JSON)
119 .content(dmiRegistrationRequest)
120 ).andReturn().response
121 then: 'response status is ok'
122 response.status == HttpStatus.OK.value()
123 and: 'the response body is empty'
124 response.getContentAsString() == ''
128 def 'DMI Registration Error Handling: #scenario.'() {
129 given: 'a dmi plugin registration'
130 def dmiRegistrationRequest = '{}'
131 and: '#scenario: service failed to register few cm-handle'
132 def dmiRegistrationResponse = new DmiPluginRegistrationResponse(
133 createdCmHandles: [createCmHandleResponse],
134 updatedCmHandles: [updateCmHandleResponse],
135 removedCmHandles: [removeCmHandleResponse]
137 mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(*_) >> dmiRegistrationResponse
138 when: 'registration endpoint is invoked'
139 def response = mvc.perform(
140 post("$ncmpBasePathV1/ch")
141 .contentType(MediaType.APPLICATION_JSON)
142 .content(dmiRegistrationRequest)
143 ).andReturn().response
144 then: 'request status is internal server error'
145 response.status == HttpStatus.INTERNAL_SERVER_ERROR.value()
146 and: 'the response body is in the expected format'
147 def responseBody = jsonObjectMapper.convertJsonString(response.getContentAsString(), DmiPluginRegistrationErrorResponse)
148 and: 'contains only the failure responses'
149 responseBody.getFailedCreatedCmHandles() == expectedFailedCreatedCmHandle
150 responseBody.getFailedUpdatedCmHandles() == expectedFailedUpdateCmHandle
151 responseBody.getFailedRemovedCmHandles() == expectedFailedRemovedCmHandle
153 scenario | createCmHandleResponse | updateCmHandleResponse | removeCmHandleResponse || expectedFailedCreatedCmHandle | expectedFailedUpdateCmHandle | expectedFailedRemovedCmHandle
154 'only create failed' | failedResponse('cm-handle-1') | successResponse('cm-handle-2') | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | [] | []
155 'only update failed' | successResponse('cm-handle-1') | failedResponse('cm-handle-2') | successResponse('cm-handle-3') || [] | [failedRestResponse('cm-handle-2')] | []
156 'only delete failed' | successResponse('cm-handle-1') | successResponse('cm-handle-2') | failedResponse('cm-handle-3') || [] | [] | [failedRestResponse('cm-handle-3')]
157 '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')]
158 'create update failed' | failedResponse('cm-handle-1') | failedResponse('cm-handle-2') | successResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | [failedRestResponse('cm-handle-2')] | []
159 'create delete failed' | failedResponse('cm-handle-1') | successResponse('cm-handle-2') | failedResponse('cm-handle-3') || [failedRestResponse('cm-handle-1')] | [] | [failedRestResponse('cm-handle-3')]
160 'update delete failed' | successResponse('cm-handle-1') | failedResponse('cm-handle-2') | failedResponse('cm-handle-3') || [] | [failedRestResponse('cm-handle-2')] | [failedRestResponse('cm-handle-3')]
163 def failedRestResponse(cmHandle) {
164 return new CmHandlerRegistrationErrorResponse('cmHandle': cmHandle, 'errorCode': '00', 'errorText': 'Failed')
167 def failedResponse(cmHandle) {
168 return CmHandleRegistrationResponse.createFailureResponse(cmHandle, new RuntimeException("Failed"))
171 def successResponse(cmHandle) {
172 return CmHandleRegistrationResponse.createSuccessResponse(cmHandle)