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
9 * 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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.integration.functional
23 import org.onap.cps.integration.base.CpsIntegrationSpecBase
24 import org.springframework.http.MediaType
25 import spock.util.concurrent.PollingConditions
26 import static org.hamcrest.Matchers.containsInAnyOrder
27 import static org.hamcrest.Matchers.hasSize
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
33 class NcmpRestApiSpec extends CpsIntegrationSpecBase {
35 def 'Register CM Handles using REST API.'() {
36 given: 'DMI will return modules'
37 dmiDispatcher.moduleNamesPerCmHandleId = [
42 and: 'a POST request is made to register the CM Handles'
43 def requestBody = '{"dmiPlugin":"'+DMI_URL+'","createdCmHandles":[{"cmHandle":"ch-1"},{"cmHandle":"ch-2"},{"cmHandle":"ch-3"}]}'
44 mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody))
45 .andExpect(status().is2xxSuccessful())
46 when: 'module sync runs'
47 moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
48 then: 'CM-handles go to READY state'
49 new PollingConditions().eventually {
51 mvc.perform(get('/ncmp/v1/ch/ch-'+it))
52 .andExpect(status().isOk())
53 .andExpect(jsonPath('$.state.cmHandleState').value('READY'))
58 def 'Search for CM Handles by module using REST API.'() {
59 given: 'a JSON request body containing search parameter'
60 def requestBodyWithModuleCondition = """{
61 "cmHandleQueryParameters": [
63 "conditionName": "hasAllModules",
64 "conditionParameters": [ {"moduleName": "%s"} ]
67 }""".formatted(moduleName)
68 expect: "a search for module ${moduleName} returns expected CM handles"
69 mvc.perform(post('/ncmp/v1/ch/id-searches').contentType(MediaType.APPLICATION_JSON).content(requestBodyWithModuleCondition))
70 .andExpect(status().is2xxSuccessful())
71 .andExpect(jsonPath('$[*]', containsInAnyOrder(expectedCmHandles.toArray())))
72 .andExpect(jsonPath('$', hasSize(expectedCmHandles.size())));
74 moduleName || expectedCmHandles
75 'M1' || ['ch-1', 'ch-2', 'ch-3']
76 'M2' || ['ch-1', 'ch-2']
80 def 'De-register CM handles using REST API.'() {
81 when: 'a POST request is made to deregister the CM Handle'
82 def requestBody = '{"dmiPlugin":"'+DMI_URL+'", "removedCmHandles": ["ch-1", "ch-2", "ch-3"]}'
83 mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody))
84 .andExpect(status().is2xxSuccessful())
85 then: 'the CM handles are not found using GET'
87 mvc.perform(get('/ncmp/v1/ch/ch-'+it)).andExpect(status().is4xxClientError())