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