3c603ed4254c4987bec7fdb0f7fcc690b8b47004
[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.CmHandle
28 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
29 import org.spockframework.spring.SpringBean
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.beans.factory.annotation.Value
32 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
33 import org.springframework.context.annotation.Import
34 import org.springframework.http.HttpStatus
35 import org.springframework.http.MediaType
36 import org.springframework.test.web.servlet.MockMvc
37 import spock.lang.Specification
38 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
39
40 @WebMvcTest(NetworkCmProxyInventoryController)
41 @Import(ObjectMapper)
42 class NetworkCmProxyInventoryControllerSpec extends Specification {
43
44     @Autowired
45     MockMvc mvc
46
47     @SpringBean
48     NetworkCmProxyDataService mockNetworkCmProxyDataService = Mock()
49
50     @Value('${rest.api.ncmp-inventory-base-path}/v1')
51     def ncmpBasePathV1
52
53     def 'Register CM Handle Event' () {
54         given: 'jsonData'
55             def jsonData = TestUtils.getResourceFileContent('dmi-registration.json')
56         when: 'post request is performed'
57             def response = mvc.perform(
58                 post("$ncmpBasePathV1/ch")
59                 .contentType(MediaType.APPLICATION_JSON)
60                 .content(jsonData)
61             ).andReturn().response
62         then: 'the cm handles are registered with the service'
63             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule(_)
64         and: 'response status is No Content'
65             response.status == HttpStatus.NO_CONTENT.value()
66     }
67
68     def 'Dmi plugin registration with #scenario' () {
69         given: 'jsonData, cmHandle, & DmiPluginRegistration'
70             def jsonData = TestUtils.getResourceFileContent('dmi_registration_combined_valid.json' )
71             def cmHandle = new CmHandle(cmHandleID : 'example-name')
72             def expectedDmiPluginRegistration = new DmiPluginRegistration(
73                 dmiPlugin: 'service1',
74                 dmiDataPlugin: '',
75                 dmiModelPlugin: '',
76                 createdCmHandles: [cmHandle])
77         when: 'post request is performed & registration is called with correct DMI plugin information'
78             def response = mvc.perform(
79                 post("$ncmpBasePathV1/ch")
80                     .contentType(MediaType.APPLICATION_JSON)
81                     .content(jsonData)
82             ).andReturn().response
83         then: 'no NcmpException is thrown & updateDmiRegistrationAndSyncModule is called with correct parameters'
84             1 * mockNetworkCmProxyDataService.updateDmiRegistrationAndSyncModule({
85                 it.getDmiPlugin() == expectedDmiPluginRegistration.getDmiPlugin()
86                 it.getDmiDataPlugin() == expectedDmiPluginRegistration.getDmiDataPlugin()
87                 it.getDmiModelPlugin() == expectedDmiPluginRegistration.getDmiModelPlugin()
88                 it.getCreatedCmHandles().get(0).getCmHandleID() == expectedDmiPluginRegistration.getCreatedCmHandles().get(0).getCmHandleID()
89             })
90     }
91 }
92