4560ae48186a1b48cde1a4bf6e07be43fc23eb8e
[cps.git] / cps-ncmp-rest / src / test / groovy / org / onap / cps / ncmp / rest / mapper / RestOutputCmHandleStateMapperTest.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.rest.mapper
22
23 import org.mapstruct.factory.Mappers
24 import org.onap.cps.ncmp.api.inventory.CmHandleState
25 import org.onap.cps.ncmp.api.inventory.CompositeState
26 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleState
27 import spock.lang.Specification
28
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.inventory.CompositeState.DataStores
34 import static org.onap.cps.ncmp.api.inventory.CompositeState.LockReason
35 import static org.onap.cps.ncmp.api.inventory.CompositeState.Operational
36
37 class RestOutputCmHandleStateMapperTest extends Specification {
38
39     def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
40         .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
41     def objectUnderTest = Mappers.getMapper(RestOutputCmHandleStateMapper)
42
43     def 'Composite State to Rest Output CmHandleState'() {
44         given: 'a composite state model'
45             def compositeState = new CompositeState(cmhandleState: CmHandleState.ADVISED,
46                 lockReason: LockReason.builder().reason('LOCKED_OTHER').details('locked-other-details').build(),
47                 lastUpdateTime: formattedDateAndTime.toString(),
48                 dataSyncEnabled: false,
49                 dataStores: dataStores())
50         when: 'mapper is called'
51             def result = objectUnderTest.toRestOutputCmHandleState(compositeState)
52         then: 'result is of the correct type'
53             assert result.class == RestOutputCmHandleState.class
54         and: 'mapped result should have correct values'
55             assert !result.dataSyncEnabled
56             assert result.lastUpdateTime == formattedDateAndTime
57             assert result.lockReason.reason == 'LOCKED_OTHER'
58             assert result.lockReason.details == 'locked-other-details'
59             assert result.cmHandleState == CmHandleState.ADVISED.name()
60             assert result.dataSyncState.operational != null
61             assert result.dataSyncState.running != null
62     }
63
64     def dataStores() {
65
66         return DataStores.builder()
67             .operationalDataStore(Operational.builder()
68                 .syncState('NONE_REQUESTED')
69                 .lastSyncTime(formattedDateAndTime.toString()).build())
70             .runningDataStore(CompositeState.Running.builder()
71                 .syncState('NONE_REQUESTED')
72                 .lastSyncTime(formattedDateAndTime.toString()).build())
73             .build()
74     }
75 }