Addressed an adaptability issue
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / NcmpRestApiSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the 'License');
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.integration.functional
22
23 import static org.hamcrest.Matchers.containsInAnyOrder
24 import static org.hamcrest.Matchers.hasSize
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
28 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
29
30 import org.onap.cps.integration.base.CpsIntegrationSpecBase
31 import org.springframework.http.MediaType
32 import spock.util.concurrent.PollingConditions
33
34 class NcmpRestApiSpec extends CpsIntegrationSpecBase {
35
36     def 'Register CM Handles using REST API.'() {
37         given: 'DMI will return modules'
38             dmiDispatcher.moduleNamesPerCmHandleId = [
39                 'ch-1': ['M1', 'M2'],
40                 'ch-2': ['M1', 'M2'],
41                 'ch-3': ['M1', 'M3']
42             ]
43         and: 'a POST request is made to register the CM Handles'
44             def requestBody = '{"dmiPlugin":"'+DMI_URL+'","createdCmHandles":[{"cmHandle":"ch-1"},{"cmHandle":"ch-2"},{"cmHandle":"ch-3"}]}'
45             mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody))
46                     .andExpect(status().is2xxSuccessful())
47         when: 'module sync runs'
48             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
49         then: 'CM-handles go to READY state'
50             new PollingConditions().eventually {
51                 (1..3).each {
52                     mvc.perform(get('/ncmp/v1/ch/ch-'+it))
53                             .andExpect(status().isOk())
54                             .andExpect(jsonPath('$.state.cmHandleState').value('READY'))
55                 }
56             }
57     }
58
59     def 'Search for CM Handles by module using REST API.'() {
60         given: 'a JSON request body containing search parameter'
61             def requestBodyWithModuleCondition = """{
62                     "cmHandleQueryParameters": [
63                             {
64                                 "conditionName": "hasAllModules",
65                                 "conditionParameters": [ {"moduleName": "%s"} ]
66                             }
67                     ]
68                 }""".formatted(moduleName)
69         expect: "a search for module ${moduleName} returns expected CM handles"
70             mvc.perform(post('/ncmp/v1/ch/id-searches').contentType(MediaType.APPLICATION_JSON).content(requestBodyWithModuleCondition))
71                     .andExpect(status().is2xxSuccessful())
72                     .andExpect(jsonPath('$[*]', containsInAnyOrder(expectedCmHandles.toArray())))
73                     .andExpect(jsonPath('$', hasSize(expectedCmHandles.size())));
74         where:
75             moduleName || expectedCmHandles
76             'M1'       || ['ch-1', 'ch-2', 'ch-3']
77             'M2'       || ['ch-1', 'ch-2']
78             'M3'       || ['ch-3']
79     }
80
81     def 'De-register CM handles using REST API.'() {
82         when: 'a POST request is made to deregister the CM Handle'
83             def requestBody = '{"dmiPlugin":"'+DMI_URL+'", "removedCmHandles": ["ch-1", "ch-2", "ch-3"]}'
84             mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody))
85                     .andExpect(status().is2xxSuccessful())
86         then: 'the CM handles are not found using GET'
87             (1..3).each {
88                 mvc.perform(get('/ncmp/v1/ch/ch-'+it)).andExpect(status().is4xxClientError())
89             }
90     }
91 }