4d42e62025f9624e4095de7b5e8e38e46b860bb1
[cps.git] /
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.models
23
24
25 import org.onap.cps.ncmp.api.inventory.DataStoreSyncState
26 import org.onap.cps.api.model.DataNode
27 import org.onap.cps.api.model.DataNodeBuilder
28 import spock.lang.Specification
29
30 import java.time.OffsetDateTime
31 import java.time.ZoneOffset
32 import java.time.format.DateTimeFormatter
33
34 class CompositeStateBuilderSpec extends Specification {
35
36     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
37         .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
38
39     def static cmHandleId = 'myHandle1'
40     def static cmHandleXpath = "/dmi-registry/cm-handles[@id='${cmHandleId}/state']"
41     def static stateDataNodes = [new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/lock-reason")
42                                          .withLeaves(['reason': 'MODULE_SYNC_FAILED', 'details': 'lock details']).build(),
43                                  new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores")
44                                             .withChildDataNodes(Arrays.asList(new DataNodeBuilder()
45                                                     .withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores/operational")
46                                                     .withLeaves(['sync-state': 'UNSYNCHRONIZED']).build())).build()]
47     def static cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: stateDataNodes, leaves: ['cm-handle-state': 'ADVISED'])
48
49     def "Composite State Specification"() {
50         when: 'using composite state builder '
51             def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED)
52                     .withLockReason(LockReasonCategory.MODULE_SYNC_FAILED,"").withOperationalDataStores(DataStoreSyncState.UNSYNCHRONIZED,
53                     formattedDateAndTime.toString()).withLastUpdatedTime(formattedDateAndTime).build()
54         then: 'it matches expected cm handle state and data store sync state'
55             assert compositeState.cmHandleState == CmHandleState.ADVISED
56             assert compositeState.dataStores.operationalDataStore.dataStoreSyncState == DataStoreSyncState.UNSYNCHRONIZED
57     }
58
59     def "Build composite state from DataNode "() {
60         given: "a Data Node "
61             new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
62         when: 'build from data node function is invoked'
63             def compositeState = new CompositeStateBuilder().fromDataNode(cmHandleDataNode).build()
64         then: 'it matches expected state model as JSON'
65             assert compositeState.cmHandleState == CmHandleState.ADVISED
66     }
67
68     def 'CompositeStateBuilder build'() {
69         given: 'A CompositeStateBuilder with all private fields set'
70             def finalCompositeStateBuilder = new CompositeStateBuilder()
71                 .withCmHandleState(CmHandleState.ADVISED)
72                 .withLastUpdatedTime(formattedDateAndTime.toString())
73                 .withLockReason(LockReasonCategory.MODULE_SYNC_FAILED, 'locked details')
74                 .withOperationalDataStores(DataStoreSyncState.SYNCHRONIZED, formattedDateAndTime)
75         when: 'build is called'
76             def result = finalCompositeStateBuilder.build()
77         then: 'result is of the correct type'
78             assert result.class == CompositeState.class
79         and: 'built result should have correct values'
80             assert !result.getDataSyncEnabled()
81             assert result.getLastUpdateTime() == formattedDateAndTime
82             assert result.getLockReason().getLockReasonCategory() == LockReasonCategory.MODULE_SYNC_FAILED
83             assert result.getLockReason().getDetails() == 'locked details'
84             assert result.getCmHandleState() == CmHandleState.ADVISED
85             assert result.getDataStores().getOperationalDataStore().getDataStoreSyncState() == DataStoreSyncState.SYNCHRONIZED
86             assert result.getDataStores().getOperationalDataStore().getLastSyncTime() == formattedDateAndTime
87     }
88
89 }