Fix @Ignore test
[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.Specification
32 import java.time.OffsetDateTime
33 import java.time.ZoneOffset
34 import java.time.format.DateTimeFormatter
35
36 class CmHandleStateMapperSpec extends Specification {
37
38     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
39         .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
40     def objectUnderTest = Mappers.getMapper(CmHandleStateMapper)
41
42     def 'Composite State to CmHandleCompositeState'() {
43         given: 'a composite state model'
44             def compositeState = new CompositeStateBuilder()
45                 .withCmHandleState(CmHandleState.ADVISED)
46                 .withLastUpdatedTime(formattedDateAndTime.toString())
47                 .withLockReason(MODULE_SYNC_FAILED, 'locked details')
48                 .withOperationalDataStores(DataStoreSyncState.SYNCHRONIZED, formattedDateAndTime).build()
49         compositeState.setDataSyncEnabled(false)
50         when: 'mapper is called'
51             def result = objectUnderTest.toCmHandleCompositeStateExternalLockReason(compositeState)
52         then: 'result is of the correct type'
53             assert result.class == CmHandleCompositeState.class
54         and: 'mapped result should have correct values'
55             assert !result.dataSyncEnabled
56             assert result.lastUpdateTime == formattedDateAndTime
57             assert result.lockReason.reason == MODULE_SYNC_FAILED.name()
58             assert result.lockReason.details == 'locked details'
59             assert result.cmHandleState == 'ADVISED'
60             assert result.dataSyncState.operational.getSyncState() != null
61     }
62
63     def 'Handling null state.'() {
64         expect: 'converting null returns null'
65             CmHandleStateMapper.toDataStores(null) == null
66     }
67
68     def 'Internal to External Lock Reason Mapping of #scenario'() {
69         given: 'a LOCKED composite state with locked reason of #scenario'
70         def compositeState = new CompositeStateBuilder()
71                 .withCmHandleState(CmHandleState.LOCKED)
72                 .withLockReason(lockReason, '').build()
73         when: 'the composite state is mapped to a CMHandle composite state'
74         def result = objectUnderTest.toCmHandleCompositeStateExternalLockReason(compositeState)
75         then: 'the composite state contains the expected lock Reason and details'
76         result.getLockReason().getReason() == (expectedExternalLockReason as String)
77         where:
78         scenario             | lockReason         || expectedExternalLockReason
79         'MODULE_SYNC_FAILED' | MODULE_SYNC_FAILED || MODULE_SYNC_FAILED
80         'null value'         | null               || LOCKED_MISBEHAVING
81     }
82
83 }