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
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.rest.mapper
23 import org.mapstruct.factory.Mappers
24 import org.onap.cps.ncmp.api.inventory.CmHandleState
25 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
26 import org.onap.cps.ncmp.api.inventory.LockReasonCategory
27 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleState
28 import spock.lang.Specification
30 import java.time.OffsetDateTime
31 import java.time.ZoneOffset
32 import java.time.format.DateTimeFormatter
34 class RestOutputCmHandleStateMapperTest extends Specification {
36 def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
37 .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC))
38 def objectUnderTest = Mappers.getMapper(RestOutputCmHandleStateMapper)
40 def 'Composite State to Rest Output CmHandleState'() {
41 given: 'a composite state model'
42 def compositeState = new CompositeStateBuilder()
43 .withCmHandleState(CmHandleState.ADVISED)
44 .withLastUpdatedTime(formattedDateAndTime.toString())
45 .withLockReason(LockReasonCategory.LOCKED_MISBEHAVING, 'locked other details')
46 .withOperationalDataStores('SYNCHRONIZED', formattedDateAndTime).build()
47 compositeState.setDataSyncEnabled(false)
48 when: 'mapper is called'
49 def result = objectUnderTest.toRestOutputCmHandleState(compositeState)
50 then: 'result is of the correct type'
51 assert result.class == RestOutputCmHandleState.class
52 and: 'mapped result should have correct values'
53 assert !result.dataSyncEnabled
54 assert result.lastUpdateTime == formattedDateAndTime
55 assert result.lockReason.reason == 'LOCKED_MISBEHAVING'
56 assert result.lockReason.details == 'locked other details'
57 assert result.cmHandleState == CmHandleState.ADVISED.name()
58 assert result.dataSyncState.operational.getState() != null