Make NCMP integration tests use MockWebServer
[cps.git] / integration-test / src / test / groovy / org / onap / cps / integration / functional / NcmpCmHandleUpgradeSpec.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.onap.cps.ncmp.api.NetworkCmProxyDataService
25 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
26 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
27 import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse
28 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
29 import org.onap.cps.ncmp.api.models.UpgradedCmHandles
30 import spock.util.concurrent.PollingConditions
31
32 class NcmpCmHandleUpgradeSpec extends CpsIntegrationSpecBase {
33
34     NetworkCmProxyDataService objectUnderTest
35
36     static final CM_HANDLE_ID = 'ch-1'
37     static final CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG = 'ch-2'
38
39     def setup() {
40         objectUnderTest = networkCmProxyDataService
41     }
42
43     def 'Upgrade CM-handle with new moduleSetTag or no moduleSetTag.'() {
44         given: 'a CM-handle is created with expected initial modules: M1 and M2'
45             dmiDispatcher.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
46             registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag)
47             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
48
49         when: "the CM-handle is upgraded with given moduleSetTag '${updatedModuleSetTag}'"
50             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
51             def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
52                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
53
54         then: 'registration gives successful response'
55             assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
56
57         and: 'CM-handle is in LOCKED state due to MODULE_UPGRADE'
58             def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
59             assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
60             assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE
61             assert cmHandleCompositeState.lockReason.details == "Upgrade to ModuleSetTag: ${updatedModuleSetTag}"
62
63         when: 'DMI will return different modules for upgrade: M1 and M3'
64             dmiDispatcher.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M3']
65         and: 'module sync runs'
66             moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
67             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
68
69         then: 'CM-handle goes to READY state'
70             new PollingConditions().eventually {
71                 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
72             }
73
74         and: 'the CM-handle has expected moduleSetTag'
75             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag
76
77         and: 'CM-handle has expected updated modules: M1 and M3'
78             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
79
80         cleanup: 'deregister CM-handle'
81             deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
82
83         where:
84             initialModuleSetTag | updatedModuleSetTag
85             NO_MODULE_SET_TAG   | NO_MODULE_SET_TAG
86             NO_MODULE_SET_TAG   | 'new'
87             'initial'           | NO_MODULE_SET_TAG
88             'initial'           | 'new'
89     }
90
91     def 'Upgrade CM-handle with existing moduleSetTag.'() {
92         given: 'DMI will return modules for registration'
93             dmiDispatcher.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
94             dmiDispatcher.moduleNamesPerCmHandleId[CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG] = ['M1', 'M3']
95         and: "an existing CM-handle handle with moduleSetTag '${updatedModuleSetTag}'"
96             registerCmHandle(DMI_URL, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG, updatedModuleSetTag)
97             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG).moduleName.sort()
98         and: "a CM-handle with moduleSetTag '${initialModuleSetTag}' which will be upgraded"
99             registerCmHandle(DMI_URL, CM_HANDLE_ID, initialModuleSetTag)
100             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
101
102         when: "CM-handle is upgraded to moduleSetTag '${updatedModuleSetTag}'"
103             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: updatedModuleSetTag)
104             def dmiPluginRegistrationResponse = networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
105                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
106
107         then: 'registration gives successful response'
108             assert dmiPluginRegistrationResponse.upgradedCmHandles == [CmHandleRegistrationResponse.createSuccessResponse(CM_HANDLE_ID)]
109
110         when: 'module sync runs'
111             moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
112             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
113
114         then: 'CM-handle goes to READY state'
115             new PollingConditions().eventually {
116                 assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
117             }
118
119         and: 'the CM-handle has expected moduleSetTag'
120             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == updatedModuleSetTag
121
122         and: 'CM-handle has expected updated modules: M1 and M3'
123             assert ['M1', 'M3'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
124
125         cleanup: 'deregister CM-handle'
126             deregisterCmHandles(DMI_URL, [CM_HANDLE_ID, CM_HANDLE_ID_WITH_EXISTING_MODULE_SET_TAG])
127
128         where:
129             initialModuleSetTag | updatedModuleSetTag
130             NO_MODULE_SET_TAG   | 'moduleSet2'
131             'moduleSet1'        | 'moduleSet2'
132     }
133
134     def 'Skip upgrade of CM-handle with same moduleSetTag as before.'() {
135         given: 'an existing CM-handle with expected initial modules: M1 and M2'
136             dmiDispatcher.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
137             registerCmHandle(DMI_URL, CM_HANDLE_ID, 'same')
138             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
139
140         when: 'CM-handle is upgraded with the same moduleSetTag'
141             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'same')
142             networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
143                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
144
145         then: 'CM-handle remains in READY state'
146             assert CmHandleState.READY == objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID).cmHandleState
147
148         and: 'the CM-handle has same moduleSetTag as before'
149             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'same'
150
151         then: 'CM-handle has same modules as before: M1 and M2'
152             assert ['M1', 'M2'] == objectUnderTest.getYangResourcesModuleReferences(CM_HANDLE_ID).moduleName.sort()
153
154         cleanup: 'deregister CM-handle'
155             deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
156     }
157
158     def 'Upgrade of CM-handle fails due to DMI error.'() {
159         given: 'a CM-handle exists'
160             dmiDispatcher.moduleNamesPerCmHandleId[CM_HANDLE_ID] = ['M1', 'M2']
161             registerCmHandle(DMI_URL, CM_HANDLE_ID, 'oldTag')
162         and: 'DMI is not available for upgrade'
163             dmiDispatcher.isAvailable = false
164
165         when: 'the CM-handle is upgraded'
166             def cmHandlesToUpgrade = new UpgradedCmHandles(cmHandles: [CM_HANDLE_ID], moduleSetTag: 'newTag')
167             networkCmProxyDataService.updateDmiRegistrationAndSyncModule(
168                     new DmiPluginRegistration(dmiPlugin: DMI_URL, upgradedCmHandles: cmHandlesToUpgrade))
169
170         and: 'module sync runs'
171             moduleSyncWatchdog.resetPreviouslyFailedCmHandles()
172             moduleSyncWatchdog.moduleSyncAdvisedCmHandles()
173
174         then: 'CM-handle goes to LOCKED state with reason MODULE_UPGRADE_FAILED'
175             new PollingConditions(timeout: 3).eventually {
176                 def cmHandleCompositeState = objectUnderTest.getCmHandleCompositeState(CM_HANDLE_ID)
177                 assert cmHandleCompositeState.cmHandleState == CmHandleState.LOCKED
178                 assert cmHandleCompositeState.lockReason.lockReasonCategory == LockReasonCategory.MODULE_UPGRADE_FAILED
179             }
180
181         and: 'the CM-handle has same moduleSetTag as before'
182             assert objectUnderTest.getNcmpServiceCmHandle(CM_HANDLE_ID).moduleSetTag == 'oldTag'
183
184         cleanup: 'deregister CM-handle'
185             deregisterCmHandle(DMI_URL, CM_HANDLE_ID)
186     }
187
188 }