Replace RestTemplate with WebClient in synchronous DMI calls
[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 org.onap.cps.integration.base.CpsIntegrationSpecBase
24 import org.springframework.http.MediaType
25 import spock.lang.Ignore
26 import spock.util.concurrent.PollingConditions
27 import static org.hamcrest.Matchers.containsInAnyOrder
28 import static org.hamcrest.Matchers.hasSize
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
31 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
32 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
33
34 @Ignore
35 class NcmpRestApiSpec extends CpsIntegrationSpecBase {
36
37     static final MODULE_REFERENCES_RESPONSE_A = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_Response.json')
38     static final MODULE_RESOURCES_RESPONSE_A = readResourceDataFile('mock-dmi-responses/bookStoreAWithModules_M1_M2_ResourcesResponse.json')
39     static final MODULE_REFERENCES_RESPONSE_B = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_Response.json')
40     static final MODULE_RESOURCES_RESPONSE_B = readResourceDataFile('mock-dmi-responses/bookStoreBWithModules_M1_M3_ResourcesResponse.json')
41
42     def setup() {
43         mockDmiWillRespondToHealthChecks(DMI_URL)
44     }
45
46     def 'Register CM Handles using REST API.'() {
47         given: 'DMI will return modules'
48             mockDmiResponsesForModuleSync(DMI_URL, 'ch-1', MODULE_REFERENCES_RESPONSE_A, MODULE_RESOURCES_RESPONSE_A)
49             mockDmiResponsesForModuleSync(DMI_URL, 'ch-2', MODULE_REFERENCES_RESPONSE_A, MODULE_RESOURCES_RESPONSE_A)
50             mockDmiResponsesForModuleSync(DMI_URL, 'ch-3', MODULE_REFERENCES_RESPONSE_B, MODULE_RESOURCES_RESPONSE_B)
51         and: 'a POST request is made to register the CM Handles'
52             def requestBody = '{"dmiPlugin":"'+DMI_URL+'","createdCmHandles":[{"cmHandle":"ch-1"},{"cmHandle":"ch-2"},{"cmHandle":"ch-3"}]}'
53             mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody))
54                     .andExpect(status().is2xxSuccessful())
55         when: 'module sync runs'
56             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
57         then: 'CM-handles go to READY state'
58             new PollingConditions(timeout: 3, delay: 0.5).eventually {
59                 mvc.perform(get('/ncmp/v1/ch/ch-1'))
60                         .andExpect(status().isOk())
61                         .andExpect(jsonPath('$.state.cmHandleState').value('READY'))
62             }
63     }
64
65     def 'Search for CM Handles by module using REST API.'() {
66         given: 'a JSON request body containing search parameter'
67             def requestBodyWithModuleCondition = """{
68                     "cmHandleQueryParameters": [
69                             {
70                                 "conditionName": "hasAllModules",
71                                 "conditionParameters": [ {"moduleName": "%s"} ]
72                             }
73                     ]
74                 }""".formatted(moduleName)
75         expect: "a search for module ${moduleName} returns expected CM handles"
76             mvc.perform(post(DMI_URL+'/ncmp/v1/ch/id-searches').contentType(MediaType.APPLICATION_JSON).content(requestBodyWithModuleCondition))
77                     .andExpect(status().is2xxSuccessful())
78                     .andExpect(jsonPath('$[*]', containsInAnyOrder(expectedCmHandles.toArray())))
79                     .andExpect(jsonPath('$', hasSize(expectedCmHandles.size())));
80         where:
81             moduleName || expectedCmHandles
82             'M1'       || ['ch-1', 'ch-2', 'ch-3']
83             'M2'       || ['ch-1', 'ch-2']
84             'M3'       || ['ch-3']
85     }
86
87     def 'De-register CM handles using REST API.'() {
88         when: 'a POST request is made to deregister the CM Handle'
89             def requestBody = '{"dmiPlugin":"'+DMI_URL+'", "removedCmHandles": ["ch-1", "ch-2", "ch-3"]}'
90             mvc.perform(post('/ncmpInventory/v1/ch').contentType(MediaType.APPLICATION_JSON).content(requestBody))
91                     .andExpect(status().is2xxSuccessful())
92         then: 'the CM handles are not found using GET'
93             (1..3).each {
94                 mvc.perform(get('/ncmp/v1/ch/ch-'+it)).andExpect(status().is4xxClientError())
95             }
96     }
97 }