2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.ncmp.api.impl.event
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
25 import org.onap.cps.ncmp.api.inventory.CompositeState
26 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
27 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
28 import org.onap.cps.utils.JsonObjectMapper
29 import spock.lang.Specification
31 import static org.onap.cps.ncmp.api.inventory.CmHandleState.ADVISED
32 import static org.onap.cps.ncmp.api.inventory.CmHandleState.LOCKED
33 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY
34 import static org.onap.cps.ncmp.api.inventory.LockReasonCategory.LOCKED_MODULE_SYNC_FAILED
36 class NcmpEventsCmHandleStateHandlerImplSpec extends Specification {
38 def mockInventoryPersistence = Mock(InventoryPersistence)
39 def mockNcmpEventsCreator = Mock(NcmpEventsCreator)
40 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
41 def mockNcmpEventsService = Mock(NcmpEventsService)
43 def objectUnderTest = new NcmpEventsCmHandleStateHandlerImpl(mockInventoryPersistence, mockNcmpEventsCreator, spiedJsonObjectMapper, mockNcmpEventsService)
45 def 'Update and Publish Events on State Change #stateChange'() {
46 given: 'Cm Handle represented as YangModelCmHandle'
47 def cmHandleId = 'cmhandle-id-1'
48 def compositeState = new CompositeState(cmHandleState: fromCmHandleState)
49 def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
50 when: 'update state is invoked'
51 objectUnderTest.updateCmHandleState(yangModelCmHandle, toCmHandleState)
52 then: 'state is saved using inventory persistence'
53 expectedCallsToInventoryPersistence * mockInventoryPersistence.saveCmHandleState(cmHandleId, _)
54 and: 'event service is called to publish event'
55 expectedCallsToEventService * mockNcmpEventsService.publishNcmpEvent(cmHandleId, _)
56 where: 'state change parameters are provided'
57 stateChange | fromCmHandleState | toCmHandleState || expectedCallsToInventoryPersistence | expectedCallsToEventService
58 'ADVISED to READY' | ADVISED | READY || 1 | 1
59 'READY to LOCKED' | READY | LOCKED || 1 | 1
60 'ADVISED to ADVISED' | ADVISED | ADVISED || 0 | 0
61 'READY to READY' | READY | READY || 0 | 0
62 'LOCKED to LOCKED' | LOCKED | LOCKED || 0 | 0
66 def 'Update and Publish Events on State Change from NO_EXISTING state to ADVISED'() {
67 given: 'Cm Handle represented as YangModelCmHandle in READY state'
68 def cmHandleId = 'cmhandle-id-1'
69 def compositeState = new CompositeState()
70 def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
71 when: 'update state is invoked'
72 objectUnderTest.updateCmHandleState(yangModelCmHandle, ADVISED)
73 then: 'state is saved using inventory persistence'
74 1 * mockInventoryPersistence.saveListElements(_)
75 and: 'event service is called to publish event'
76 1 * mockNcmpEventsService.publishNcmpEvent(cmHandleId, _)
79 def 'Update and Publish Events on State Change from LOCKED to ADVISED'() {
80 given: 'Cm Handle represented as YangModelCmHandle in LOCKED state'
81 def cmHandleId = 'cmhandle-id-1'
82 def compositeState = new CompositeState(cmHandleState: LOCKED,
83 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LOCKED_MODULE_SYNC_FAILED).details('some lock details').build())
84 def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
85 when: 'update state is invoked'
86 objectUnderTest.updateCmHandleState(yangModelCmHandle, ADVISED)
87 then: 'state is saved using inventory persistence and old lock reason details are retained'
88 1 * mockInventoryPersistence.saveCmHandleState(cmHandleId, _) >> {
90 assert (args[1] as CompositeState).lockReason.details == 'some lock details'
93 and: 'event service is called to publish event'
94 1 * mockNcmpEventsService.publishNcmpEvent(cmHandleId, _)
97 def 'Update and Publish Events on State Change to READY with #scenario'() {
98 given: 'Cm Handle represented as YangModelCmHandle'
99 def cmHandleId = 'cmhandle-id-1'
100 def compositeState = new CompositeState(cmHandleState: ADVISED)
101 def yangModelCmHandle = new YangModelCmHandle(id: cmHandleId, dmiProperties: [], publicProperties: [], compositeState: compositeState)
102 and: 'global sync flag is set'
103 objectUnderTest.isGlobalDataSyncCacheEnabled = dataSyncCacheEnabled
104 when: 'update cmhandle state is invoked'
105 objectUnderTest.updateCmHandleState(yangModelCmHandle, READY)
106 then: 'state is saved using inventory persistence with expected dataSyncState'
107 1 * mockInventoryPersistence.saveCmHandleState(cmHandleId, _) >> {
109 assert (args[1] as CompositeState).dataSyncEnabled == dataSyncCacheEnabled
110 assert (args[1] as CompositeState).dataStores.operationalDataStore.dataStoreSyncState == expectedDataStoreSyncState
114 and: 'event service is called to publish event'
115 1 * mockNcmpEventsService.publishNcmpEvent(cmHandleId, _)
117 scenario | dataSyncCacheEnabled || expectedDataStoreSyncState
118 'data sync cache enabled' | true || DataStoreSyncState.UNSYNCHRONIZED
119 'data sync cache is not enabled' | false || DataStoreSyncState.NONE_REQUESTED