b4154d25bac348f10b819ad7f0f69ba97023526b
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2025 OpenInfra Foundation Europe. All rights reserved.
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.ncmp.inventory
22
23 import org.onap.cps.integration.base.CpsIntegrationSpecBase
24 import org.springframework.http.MediaType
25
26 import static org.hamcrest.Matchers.containsInAnyOrder
27 import static org.hamcrest.Matchers.hasSize
28 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
29 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
30 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
31
32 class CmHandleSearchesSouthBoundRestApiSpec extends CpsIntegrationSpecBase {
33
34     def 'Search for CM Handles by dmi-service using REST API.'() {
35         given: 'register some CM Handles for dmi service #DMI1_URL (has to be done in same test otherwise URL/Port changes)'
36             def requestBody = '{"dmiPlugin":"'+DMI1_URL+'","createdCmHandles":[{"cmHandle":"ch-1"},{"cmHandle":"ch-2"}]}'
37             mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody)).andExpect(status().is2xxSuccessful())
38         and: 'a cm-handle search on dmi service name request (body)'
39             def dmiPluginNameSearchValue = useCorrectDmiForSearch ? DMI1_URL : 'non existing dmi'
40             def requestBodyWithModuleCondition = """{
41                     "cmHandleQueryParameters": [ {
42                                 "conditionName": "cmHandleWithDmiPlugin",
43                                 "conditionParameters": [ {"dmiPluginName": "%s"} ]
44                             } ]
45                 }""".formatted(dmiPluginNameSearchValue)
46         when: 'the search is executed it returns expected CM handles'
47             def result = mvc.perform(post("/ncmpInventory/v1/ch/searchCmHandles?includeCmHandlePropertiesInQuery=${includeCmHandleProperties}").contentType(MediaType.APPLICATION_JSON).content(requestBodyWithModuleCondition))
48                     .andExpect(status().is2xxSuccessful())
49                     .andExpect(jsonPath('$', hasSize(expectedCmHandleReferences.size())))
50                     .andExpect(jsonPath('$[*].cmHandle', containsInAnyOrder(expectedCmHandleReferences.toArray())))
51                     .andReturn()
52         then: 'the result response only contains additional properties when requested'
53             assert result.response.contentAsString.contains('cmHandleProperties') == includeCmHandleProperties
54         cleanup: 'deregister the CM Handles'
55             requestBody = '{"dmiPlugin":"'+DMI1_URL+'", "removedCmHandles": ["ch-1", "ch-2"]}'
56             mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody)).andExpect(status().is2xxSuccessful())
57         where: 'the following parameters are used'
58             useCorrectDmiForSearch | includeCmHandleProperties || expectedCmHandleReferences
59             true                   | true                      || ['ch-1', 'ch-2']
60             true                   | false                     || ['ch-1', 'ch-2']
61             false                  | false                     || []
62     }
63
64 }