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