Refactoring persistence classes
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / CompositeStateBuilderSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Bell Canada
4  * Modifications Copyright (C) 2022-2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory
23
24 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
25 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
26 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder
27 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
28 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
29 import org.onap.cps.spi.model.DataNode
30 import org.onap.cps.spi.model.DataNodeBuilder
31 import spock.lang.Specification
32
33 import java.time.OffsetDateTime
34 import java.time.ZoneOffset
35 import java.time.format.DateTimeFormatter
36
37 class CompositeStateBuilderSpec extends Specification {
38
39     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
40         .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
41
42     def static cmHandleId = 'myHandle1'
43     def static cmHandleXpath = "/dmi-registry/cm-handles[@id='${cmHandleId}/state']"
44     def static stateDataNodes = [new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/lock-reason")
45                                          .withLeaves(['reason': 'MODULE_SYNC_FAILED', 'details': 'lock details']).build(),
46                                  new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores")
47                                             .withChildDataNodes(Arrays.asList(new DataNodeBuilder()
48                                                     .withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores/operational")
49                                                     .withLeaves(['sync-state': 'UNSYNCHRONIZED']).build())).build()]
50     def static cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: stateDataNodes, leaves: ['cm-handle-state': 'ADVISED'])
51
52     def "Composite State Specification"() {
53         when: 'using composite state builder '
54             def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED)
55                     .withLockReason(LockReasonCategory.MODULE_SYNC_FAILED,"").withOperationalDataStores(DataStoreSyncState.UNSYNCHRONIZED,
56                     formattedDateAndTime.toString()).withLastUpdatedTime(formattedDateAndTime).build()
57         then: 'it matches expected cm handle state and data store sync state'
58             assert compositeState.cmHandleState == CmHandleState.ADVISED
59             assert compositeState.dataStores.operationalDataStore.dataStoreSyncState == DataStoreSyncState.UNSYNCHRONIZED
60     }
61
62     def "Build composite state from DataNode "() {
63         given: "a Data Node "
64             new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
65         when: 'build from data node function is invoked'
66             def compositeState = new CompositeStateBuilder().fromDataNode(cmHandleDataNode).build()
67         then: 'it matches expected state model as JSON'
68             assert compositeState.cmHandleState == CmHandleState.ADVISED
69     }
70
71     def 'CompositeStateBuilder build'() {
72         given: 'A CompositeStateBuilder with all private fields set'
73             def finalCompositeStateBuilder = new CompositeStateBuilder()
74                 .withCmHandleState(CmHandleState.ADVISED)
75                 .withLastUpdatedTime(formattedDateAndTime.toString())
76                 .withLockReason(LockReasonCategory.MODULE_SYNC_FAILED, 'locked details')
77                 .withOperationalDataStores(DataStoreSyncState.SYNCHRONIZED, formattedDateAndTime)
78         when: 'build is called'
79             def result = finalCompositeStateBuilder.build()
80         then: 'result is of the correct type'
81             assert result.class == CompositeState.class
82         and: 'built result should have correct values'
83             assert !result.getDataSyncEnabled()
84             assert result.getLastUpdateTime() == formattedDateAndTime
85             assert result.getLockReason().getLockReasonCategory() == LockReasonCategory.MODULE_SYNC_FAILED
86             assert result.getLockReason().getDetails() == 'locked details'
87             assert result.getCmHandleState() == CmHandleState.ADVISED
88             assert result.getDataStores().getOperationalDataStore().getDataStoreSyncState() == DataStoreSyncState.SYNCHRONIZED
89             assert result.getDataStores().getOperationalDataStore().getLastSyncTime() == formattedDateAndTime
90     }
91
92 }