Replacing ModelMapper with MapStruct
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / controller / NetworkCmProxyInventoryControllerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.DmiPluginRegistration
28 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration
29 import org.onap.cps.utils.JsonObjectMapper
30 import org.spockframework.spring.SpringBean
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.beans.factory.annotation.Value
33 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
34 import org.springframework.context.annotation.Import
35 import org.springframework.http.HttpStatus
36 import org.springframework.http.MediaType
37 import org.springframework.test.web.servlet.MockMvc
38 import spock.lang.Specification
39 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
40
41 @WebMvcTest(NetworkCmProxyInventoryController)
42 @Import(ObjectMapper)
43 class NetworkCmProxyInventoryControllerSpec extends Specification {
44
45     @Autowired
46     MockMvc mvc
47
48     @SpringBean
49     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
50
51     @SpringBean
52     NcmpRestInputMapper ncmpRestInputMapper = Mock()
53
54     DmiPluginRegistration mockDmiPluginRegistration = Mock()
55
56     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
57
58     @Value('${rest.api.ncmp-inventory-base-path}/v1')
59     def ncmpBasePathV1
60
61     def 'Dmi plugin registration #scenario' () {
62         given: 'a dmi plugin registration with #scenario'
63             def jsonData = TestUtils.getResourceFileContent(dmiRegistrationJson)
64         and: 'the expected rest input as an object'
65             def expectedRestDmiPluginRegistration = jsonObjectMapper.convertJsonString(jsonData, RestDmiPluginRegistration)
66         and: 'the converter returns a dmi registration (only for the expected input object)'
67             ncmpRestInputMapper.toDmiPluginRegistration(expectedRestDmiPluginRegistration) >> mockDmiPluginRegistration
68         when: 'post request is performed & registration is called with correct DMI plugin information'
69             def response = mvc.perform(
70                 post("$ncmpBasePathV1/ch")
71                     .contentType(MediaType.APPLICATION_JSON)
72                     .content(jsonData)
73             ).andReturn().response
74         then: 'the converted object is forwarded to the registration service'
75             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(mockDmiPluginRegistration)
76         and: 'response status is no content'
77             response.status ==  HttpStatus.NO_CONTENT.value()
78         where: 'the following registration json is used'
79             scenario                                                                       | dmiRegistrationJson
80             'multiple services, added, updated and removed cm handles and many properties' | 'dmi_registration_all_singing_and_dancing.json'
81             'updated cm handle with updated/new and removed properties'                    | 'dmi_registration_updates_only.json'
82             'without any properties'                                                       | 'dmi_registration_without_properties.json'
83     }
84
85     def 'Dmi plugin registration with invalid json' () {
86         given: 'a dmi plugin registration with #scenario'
87             def jsonDataWithUndefinedDataLabel = '{"notAdmiPlugin":""}'
88         when: 'post request is performed & registration is called with correct DMI plugin information'
89             def response = mvc.perform(
90                 post("$ncmpBasePathV1/ch")
91                     .contentType(MediaType.APPLICATION_JSON)
92                     .content(jsonDataWithUndefinedDataLabel)
93             ).andReturn().response
94         then: 'response status is bad request'
95             response.status == HttpStatus.BAD_REQUEST.value()
96     }
97
98 }