Watchdog-process that changes CM Handles state
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / SyncUtilsSpec.groovy
1 /*
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
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.ncmp.api.inventory.sync
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.api.CpsDataService
25 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.spi.CpsDataPersistenceService
28 import org.onap.cps.spi.FetchDescendantsOption
29 import org.onap.cps.spi.model.DataNode
30 import org.onap.cps.utils.JsonObjectMapper
31 import spock.lang.Shared
32 import spock.lang.Specification
33
34 import java.time.OffsetDateTime
35
36 class SyncUtilsSpec extends Specification{
37
38     def mockCpsDataService = Mock(CpsDataService)
39     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
40     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
41     def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
42
43     def objectUnderTest = new SyncUtils(mockCpsDataService, mockCpsDataPersistenceService, spiedJsonObjectMapper, mockYangModelCmHandleRetriever)
44
45     @Shared
46     def dataNode = new DataNode(leaves: ['id': 'cm-handle-123'])
47
48
49
50     def 'Get an advised Cm-Handle where ADVISED cm handle #scenario'() {
51         given: 'the cps (persistence service) returns a collection of data nodes'
52             mockCpsDataPersistenceService.queryDataNodes('NCMP-Admin',
53                 'ncmp-dmi-registry', '//cm-handles[@state=\"ADVISED\"]',
54                 FetchDescendantsOption.OMIT_DESCENDANTS) >> dataNodeCollection
55         when: 'get advised cm handle is called'
56             objectUnderTest.getAnAdvisedCmHandle()
57         then: 'the returned data node collection is the correct size'
58             dataNodeCollection.size() == expectedDataNodeSize
59         and: 'get yang model cm handles is invoked the correct number of times'
60            expectedCallsToGetYangModelCmHandle * mockYangModelCmHandleRetriever.getYangModelCmHandle('cm-handle-123')
61         where: 'the following scenarios are used'
62             scenario         | dataNodeCollection || expectedCallsToGetYangModelCmHandle | expectedDataNodeSize
63             'exists'         | [ dataNode ]       || 1                                   | 1
64             'does not exist' | [ ]                || 0                                   | 0
65
66     }
67
68     def 'Update cm handle state from Advised to Ready'() {
69         given: 'a yang model cm handle and the expected json data'
70             def yangModelCmHandle = new YangModelCmHandle('id': 'Some-Cm-Handle', 'cmHandleState': 'ADVISED')
71             def expectedJsonData = '{"cm-handles":[{"id":"Some-Cm-Handle","state":"READY"}]}'
72         when: 'update cm handle state is called'
73             objectUnderTest.updateCmHandleState(yangModelCmHandle, 'READY')
74         then: 'update data note leaves is invoked with the correct params'
75             1 * mockCpsDataService.updateNodeLeaves('NCMP-Admin', 'ncmp-dmi-registry', '/dmi-registry', expectedJsonData, _ as OffsetDateTime)
76     }
77
78 }