8d434e775897b60a9fbc393f647b311caad39dfb
[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  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.rest.controller
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.TestUtils
25 import org.onap.cps.ncmp.api.NetworkCmProxyDataService
26 import org.onap.cps.ncmp.api.models.CmHandle
27 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.beans.factory.annotation.Value
31 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
32 import org.springframework.context.annotation.Import
33 import org.springframework.http.HttpStatus
34 import org.springframework.http.MediaType
35 import org.springframework.test.web.servlet.MockMvc
36 import spock.lang.Specification
37 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
38
39 @WebMvcTest(NetworkCmProxyInventoryController)
40 @Import(ObjectMapper)
41 class NetworkCmProxyInventoryControllerSpec extends Specification {
42
43     @Autowired
44     MockMvc mvc
45
46     @SpringBean
47     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
48
49     @Value('${rest.api.ncmp-inventory-base-path}/v1')
50     def ncmpBasePathV1
51
52     def 'Register CM Handle Event' () {
53         given: 'jsonData'
54             def jsonData = TestUtils.getResourceFileContent('dmi-registration.json')
55         when: 'post request is performed'
56             def response = mvc.perform(
57                 post("$ncmpBasePathV1/ch")
58                 .contentType(MediaType.APPLICATION_JSON)
59                 .content(jsonData)
60             ).andReturn().response
61         then: 'the cm handles are registered with the service'
62             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(_)
63         and: 'response status is created'
64             response.status == HttpStatus.CREATED.value()
65     }
66
67     def 'Dmi plugin registration with #scenario' () {
68         given: 'jsonData, cmHandle, & DmiPluginRegistration'
69             def jsonData = TestUtils.getResourceFileContent('dmi_registration_combined_valid.json' )
70             def cmHandle = new CmHandle(cmHandleID : 'example-name')
71             def expectedDmiPluginRegistration = new DmiPluginRegistration(
72                 dmiPlugin: 'service1',
73                 dmiDataPlugin: '',
74                 dmiModelPlugin: '',
75                 createdCmHandles: [cmHandle])
76         when: 'post request is performed & registration is called with correct DMI plugin information'
77             def response = mvc.perform(
78                 post("$ncmpBasePathV1/ch")
79                     .contentType(MediaType.APPLICATION_JSON)
80                     .content(jsonData)
81             ).andReturn().response
82         then: 'no NcmpException is thrown & updateDmiRegistrationAndSyncModule is called with correct parameters'
83             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule({
84                 it.getDmiPlugin() == expectedDmiPluginRegistration.getDmiPlugin()
85                 it.getDmiDataPlugin() == expectedDmiPluginRegistration.getDmiDataPlugin()
86                 it.getDmiModelPlugin() == expectedDmiPluginRegistration.getDmiModelPlugin()
87                 it.getCreatedCmHandles().get(0).getCmHandleID() == expectedDmiPluginRegistration.getCreatedCmHandles().get(0).getCmHandleID()
88             })
89     }
90 }
91