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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory
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
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
38 class CompositeStateSpec extends Specification {
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()
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)
59 DataStores.builder().operationalDataStore(Operational.builder()
60 .dataStoreSyncState(DataStoreSyncState.NONE_REQUESTED)
61 .lastSyncTime(formattedDateAndTime.toString()).build())