Composite State to handle dmi-reg YANG updates
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / CompositeStateSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Nordix Foundation
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 com.fasterxml.jackson.databind.ObjectMapper
24 import spock.lang.Specification
25
26 import java.time.OffsetDateTime
27 import java.time.ZoneOffset
28 import java.time.format.DateTimeFormatter
29
30 import static CompositeState.DataStores
31 import static CompositeState.LockReason
32 import static CompositeState.Operational
33 import static CompositeState.Running
34 import static org.onap.cps.ncmp.utils.TestUtils.getResourceFileContent
35 import static org.springframework.util.StringUtils.trimAllWhitespace
36
37 class CompositeStateSpec extends Specification {
38
39     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(OffsetDateTime.of(2022, 1, 1, 1, 1, 1, 1, ZoneOffset.MIN))
40     def objectMapper = new ObjectMapper()
41
42     def "Composite State Specification"() {
43         given: "a Composite State"
44             def compositeState = new CompositeState(cmhandleState: CmHandleState.ADVISED,
45                 lockReason: LockReason.builder().reason('lock-reason').details("lock-misbehaving-details").build(),
46                 lastUpdateTime: formattedDateAndTime.toString(),
47                 dataSyncEnabled: false,
48                 dataStores: dataStores())
49         when: 'it is represented as JSON'
50             def jsonStateModelAsString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(compositeState)
51         then: 'it matches expected state model as JSON'
52             def expectedStateModelAsjson = getResourceFileContent('expectedStateModel.json')
53             assert trimAllWhitespace(expectedStateModelAsjson) == trimAllWhitespace(jsonStateModelAsString)
54     }
55
56     def dataStores() {
57         DataStores.builder().operationalDataStore(Operational.builder()
58             .syncState('NONE_REQUESTED')
59             .lastSyncTime(formattedDateAndTime.toString()).build()).runningDataStore(Running.builder()
60             .syncState('NONE_REQUESTED')
61             .lastSyncTime(formattedDateAndTime.toString()).build())
62             .build()
63     }
64 }