CompositeStateBuilder added for building the compositeState
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.inventory
22
23 import org.onap.cps.spi.model.DataNode
24 import org.onap.cps.spi.model.DataNodeBuilder
25 import spock.lang.Specification
26
27 import java.time.OffsetDateTime
28 import java.time.ZoneOffset
29 import java.time.format.DateTimeFormatter
30
31 class CompositeStateBuilderSpec extends Specification {
32
33     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
34         .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
35
36     def static cmHandleId = 'myHandle1'
37     def static cmHandleXpath = "/dmi-registry/cm-handles[@id='${cmHandleId}/state']"
38     def static stateDataNodes = [new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/lock-reason")
39                                          .withLeaves(['reason': 'lock reason', 'details': 'lock details']).build(),
40                                  new DataNodeBuilder().withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores")
41                                             .withChildDataNodes(Arrays.asList(new DataNodeBuilder()
42                                                     .withXpath("/dmi-registry/cm-handles[@id='${cmHandleId}']/state/datastores/operational")
43                                                     .withLeaves(['sync-state': 'UNSYNCHRONIZED']).build())).build()]
44     def static cmHandleDataNode = new DataNode(xpath: cmHandleXpath, childDataNodes: stateDataNodes, leaves: ['cm-handle-state': 'ADVISED'])
45
46     def "Composite State Specification"() {
47         when: 'using composite state builder '
48             def compositeState = new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED)
49                     .withLockReason("lock-reason","").withOperationalDataStores("UNSYNCHRONIZED",
50                     formattedDateAndTime.toString()).withLastUpdatedTime(formattedDateAndTime).build();
51         then: 'it matches expected cm handle state and data store sync state'
52             assert compositeState.getCmhandleState() == CmHandleState.ADVISED
53             assert compositeState.dataStores.operationalDataStore.syncState == 'UNSYNCHRONIZED'
54     }
55
56     def "Build composite state from DataNode "() {
57         given: "a Data Node "
58             def dataNode = new DataNode(leaves: ['cm-handle-state': 'ADVISED'])
59         when: 'build from data node function is invoked'
60             def compositeState = new CompositeStateBuilder().fromDataNode(cmHandleDataNode).build()
61         then: 'it matches expected state model as JSON'
62             assert compositeState.cmhandleState == CmHandleState.ADVISED
63     }
64
65 }