f394f919383c061c86adf1fa87fa0dc2d5b52483
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / mapper / CmHandleStateMapperSpec.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.rest.mapper
22
23 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.LOCKED_MISBEHAVING
24 import static org.onap.cps.ncmp.api.impl.inventory.LockReasonCategory.MODULE_SYNC_FAILED
25
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder
29 import org.onap.cps.ncmp.rest.model.CmHandleCompositeState
30 import org.onap.cps.ncmp.api.impl.inventory.DataStoreSyncState
31 import spock.lang.Ignore
32 import spock.lang.Specification
33
34 import java.time.OffsetDateTime
35 import java.time.ZoneOffset
36 import java.time.format.DateTimeFormatter
37
38 class CmHandleStateMapperSpec 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 objectUnderTest = Mappers.getMapper(CmHandleStateMapper)
43
44     def 'Composite State to CmHandleCompositeState'() {
45         given: 'a composite state model'
46             def compositeState = new CompositeStateBuilder()
47                 .withCmHandleState(CmHandleState.ADVISED)
48                 .withLastUpdatedTime(formattedDateAndTime.toString())
49                 .withLockReason(MODULE_SYNC_FAILED, 'locked details')
50                 .withOperationalDataStores(DataStoreSyncState.SYNCHRONIZED, formattedDateAndTime).build()
51         compositeState.setDataSyncEnabled(false)
52         when: 'mapper is called'
53             def result = objectUnderTest.toCmHandleCompositeStateExternalLockReason(compositeState)
54         then: 'result is of the correct type'
55             assert result.class == CmHandleCompositeState.class
56         and: 'mapped result should have correct values'
57             assert !result.dataSyncEnabled
58             assert result.lastUpdateTime == formattedDateAndTime
59             assert result.lockReason.reason == MODULE_SYNC_FAILED.name()
60             assert result.lockReason.details == 'locked details'
61             assert result.cmHandleState == 'ADVISED'
62             assert result.dataSyncState.operational.getSyncState() != null
63     }
64
65     @Ignore
66     def 'Handling null state.'() {
67         expect: 'converting null returns null'
68             objectUnderTest.toDataStores(null) == null
69     }
70
71     def 'Internal to External Lock Reason Mapping of #scenario'() {
72         given: 'a LOCKED composite state with locked reason of #scenario'
73         def compositeState = new CompositeStateBuilder()
74                 .withCmHandleState(CmHandleState.LOCKED)
75                 .withLockReason(lockReason, '').build()
76         when: 'the composite state is mapped to a CMHandle composite state'
77         def result = objectUnderTest.toCmHandleCompositeStateExternalLockReason(compositeState)
78         then: 'the composite state contains the expected lock Reason and details'
79         result.getLockReason().getReason() == (expectedExternalLockReason as String)
80         where:
81         scenario             | lockReason         || expectedExternalLockReason
82         'MODULE_SYNC_FAILED' | MODULE_SYNC_FAILED || MODULE_SYNC_FAILED
83         'null value'         | null               || LOCKED_MISBEHAVING
84     }
85
86 }