Module Sync Lock State implementation
[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  * 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 spock.lang.Specification
27
28 import java.time.OffsetDateTime
29 import java.time.ZoneOffset
30 import java.time.format.DateTimeFormatter
31
32 class CompositeStateBuilderSpec extends Specification {
33
34     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
35         .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
36
37     def static cmHandleId = 'myHandle1'
38     def static cmHandleXpath = "/dmi-registry/cm-handles[@id='${cmHandleId}/state']"
39     def static stateDataNodes = [new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/lock-reason")
40                                          .withLeaves(['reason': 'LOCKED_MISBEHAVING', 'details': 'lock details']).build(),
41                                  new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores")
42                                             .withChildDataNodes(Arrays.asList(new DataNodeBuilder()
43                                                     .withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores/operational")
44                                                     .withLeaves(['sync-state': 'UNSYNCHRONIZED']).build())).build()]
45     def static cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: stateDataNodes, leaves: ['cm-handle-state': 'ADVISED'])
46
47     def "Composite State Specification"() {
48         when: 'using composite state builder '
49             def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED)
50                     .withLockReason(LockReasonCategory.LOCKED_MISBEHAVING,"").withOperationalDataStores("UNSYNCHRONIZED",
51                     formattedDateAndTime.toString()).withLastUpdatedTime(formattedDateAndTime).build()
52         then: 'it matches expected cm handle state and data store sync state'
53             assert compositeState.cmHandleState == CmHandleState.ADVISED
54             assert compositeState.dataStores.operationalDataStore.syncState == 'UNSYNCHRONIZED'
55     }
56
57     def "Build composite state from DataNode "() {
58         given: "a Data Node "
59             new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
60         when: 'build from data node function is invoked'
61             def compositeState = new CompositeStateBuilder().fromDataNode(cmHandleDataNode).build()
62         then: 'it matches expected state model as JSON'
63             assert compositeState.cmHandleState == CmHandleState.ADVISED
64     }
65
66 }