Merge "CM Data Subscriptions PoC/Performance test fixes"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / CompositeStateSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-2023 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 org.onap.cps.ncmp.api.impl.inventory.CmHandleState
25 import org.onap.cps.ncmp.api.impl.inventory.CompositeState
26 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
27 import org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory
28 import spock.lang.Specification
29 import java.time.OffsetDateTime
30 import java.time.ZoneOffset
31 import java.time.format.DateTimeFormatter
32
33 import static org.onap.cps.ncmp.api.impl.inventory.CompositeState.DataStores
34 import static org.onap.cps.ncmp.api.impl.inventory.CompositeState.Operational
35 import static org.onap.cps.ncmp.utils.TestUtils.getResourceFileContent
36 import static org.springframework.util.StringUtils.trimAllWhitespace
37
38 class CompositeStateSpec extends Specification {
39
40     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
41         .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
42     def objectMapper = new ObjectMapper()
43
44     def "Composite State Specification"() {
45         given: "a Composite State"
46             def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED,
47                 lockReason: CompositeState.LockReason.builder().lockReasonCategory(LockReasonCategory.MODULE_SYNC_FAILED).details("lock details").build(),
48                 lastUpdateTime: formattedDateAndTime.toString(),
49                 dataSyncEnabled: false,
50                 dataStores: dataStores())
51         when: 'it is represented as JSON'
52             def resultJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(compositeState)
53         then: 'it matches expected state model as JSON'
54             def expectedJson = getResourceFileContent('expectedStateModel.json')
55             assert trimAllWhitespace(expectedJson) == trimAllWhitespace(resultJson)
56     }
57
58     def dataStores() {
59         DataStores.builder().operationalDataStore(Operational.builder()
60             .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
61             .lastSyncTime(formattedDateAndTime.toString()).build())
62             .build()
63     }
64 }