2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-2024 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.impl.inventory.models
 
  23 import org.onap.cps.ncmp.api.inventory.models.CompositeState
 
  24 import org.onap.cps.ncmp.api.inventory.models.CompositeStateBuilder
 
  25 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle
 
  26 import org.onap.cps.ncmp.impl.inventory.DataStoreSyncState
 
  27 import spock.lang.Specification
 
  29 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.DATA
 
  30 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.MODEL
 
  32 class YangModelCmHandleSpec extends Specification {
 
  34     def 'Creating yang model cm handle from a service api cm handle.'() {
 
  35         given: 'a cm handle with properties'
 
  36             def ncmpServiceCmHandle = new NcmpServiceCmHandle()
 
  37             ncmpServiceCmHandle.cmHandleId = 'cm-handle-id01'
 
  38             ncmpServiceCmHandle.dmiProperties = [myDmiProperty:'value1']
 
  39             ncmpServiceCmHandle.publicProperties = [myPublicProperty:'value2']
 
  40         and: 'with a composite state'
 
  41             def compositeState = new CompositeStateBuilder()
 
  42                 .withCmHandleState(CmHandleState.LOCKED)
 
  43                 .withLastUpdatedTime('some-update-time')
 
  44                 .withLockReason(LockReasonCategory.MODULE_SYNC_FAILED, 'locked details')
 
  45                 .withOperationalDataStores(DataStoreSyncState.SYNCHRONIZED, 'some-sync-time').build()
 
  46             ncmpServiceCmHandle.setCompositeState(compositeState)
 
  47         when: 'it is converted to a yang model cm handle'
 
  48             def objectUnderTest = YangModelCmHandle.toYangModelCmHandle('', '', '', ncmpServiceCmHandle,'my-module-set-tag', 'my-alternate-id', 'my-data-producer-identifier')
 
  49         then: 'the result has the right size'
 
  50             assert objectUnderTest.dmiProperties.size() == 1
 
  51         and: 'the result has the correct values for module set tag, alternate ID, and data producer identifier'
 
  52             assert objectUnderTest.moduleSetTag == 'my-module-set-tag'
 
  53             assert objectUnderTest.alternateId == 'my-alternate-id'
 
  54             assert objectUnderTest.dataProducerIdentifier == 'my-data-producer-identifier'
 
  55         and: 'the DMI property in the result has the correct name and value'
 
  56             assert objectUnderTest.dmiProperties[0].name == 'myDmiProperty'
 
  57             assert objectUnderTest.dmiProperties[0].value == 'value1'
 
  58         and: 'the public property in the result has the correct name and value'
 
  59             assert objectUnderTest.publicProperties[0].name == 'myPublicProperty'
 
  60             assert objectUnderTest.publicProperties[0].value == 'value2'
 
  61         and: 'the composite state matches the composite state of the ncmpServiceCmHandle'
 
  62             objectUnderTest.getCompositeState().cmHandleState == CmHandleState.LOCKED
 
  63             objectUnderTest.getCompositeState() == ncmpServiceCmHandle.getCompositeState()
 
  66     def 'Resolve DMI service name: #scenario and #requiredService service require.'() {
 
  67         given: 'a yang model cm handle'
 
  68             def objectUnderTest = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, dmiDataServiceName,
 
  69                     dmiModelServiceName, new NcmpServiceCmHandle(cmHandleId: 'cm-handle-id-1'),'', '', '')
 
  71             assert objectUnderTest.resolveDmiServiceName(requiredService) == expectedService
 
  73             scenario                        | dmiServiceName     | dmiDataServiceName | dmiModelServiceName | requiredService || expectedService
 
  74             'common service registered'     | 'common service'   | 'does not matter'  | 'does not matter'   | DATA            || 'common service'
 
  75             'common service registered'     | 'common service'   | 'does not matter'  | 'does not matter'   | MODEL           || 'common service'
 
  76             'common service empty'          | ''                 | 'data service'     | 'does not matter'   | DATA            || 'data service'
 
  77             'common service empty'          | ''                 | 'does not matter'  | 'model service'     | MODEL           || 'model service'
 
  78             'common service blank'          | '   '              | 'data service'     | 'does not matter'   | DATA            || 'data service'
 
  79             'common service blank'          | '   '              | 'does not matter'  | 'model service'     | MODEL           || 'model service'
 
  80             'common service null '          | null               | 'data service'     | 'does not matter'   | DATA            || 'data service'
 
  81             'common service null'           | null               | 'does not matter'  | 'model service'     | MODEL           || 'model service'
 
  82             'only model service registered' | null               | null               | 'does not matter'   | DATA            || null
 
  83             'only data service registered'  | null               | 'does not matter'  | null                | MODEL           || null
 
  86     def 'Yang Model Cm Handle Deep Copy'() {
 
  87         given: 'a yang model cm handle'
 
  88             def currentYangModelCmHandle = new YangModelCmHandle(id: 'cmhandle',
 
  89                 publicProperties: [new YangModelCmHandle.Property('publicProperty1', 'value1')],
 
  90                 dmiProperties: [new YangModelCmHandle.Property('dmiProperty1', 'value1')],
 
  91                 compositeState: new CompositeState(cmHandleState: CmHandleState.ADVISED, dataSyncEnabled: false))
 
  92         when: 'a deep copy is created'
 
  93             def yangModelCmhandleDeepCopy = YangModelCmHandle.deepCopyOf(currentYangModelCmHandle)
 
  94         and: 'we try to mutate current yang model cm handle'
 
  95             currentYangModelCmHandle.id = 'cmhandle-changed'
 
  96             currentYangModelCmHandle.dmiProperties = [new YangModelCmHandle.Property('updatedPublicProperty1', 'value1')]
 
  97             currentYangModelCmHandle.publicProperties = [new YangModelCmHandle.Property('updatedDmiProperty1', 'value1')]
 
  98             currentYangModelCmHandle.compositeState.cmHandleState = CmHandleState.READY
 
  99             currentYangModelCmHandle.compositeState.dataSyncEnabled = true
 
 100         then: 'there is no change in the deep copied object'
 
 101             assert yangModelCmhandleDeepCopy.id == 'cmhandle'
 
 102             assert yangModelCmhandleDeepCopy.dmiProperties == [new YangModelCmHandle.Property('dmiProperty1', 'value1')]
 
 103             assert yangModelCmhandleDeepCopy.publicProperties == [new YangModelCmHandle.Property('publicProperty1', 'value1')]
 
 104             assert yangModelCmhandleDeepCopy.compositeState.cmHandleState == CmHandleState.ADVISED
 
 105             assert yangModelCmhandleDeepCopy.compositeState.dataSyncEnabled == false
 
 106         and: 'equality on reference and hashcode behave as expected'
 
 107             assert currentYangModelCmHandle.hashCode() != yangModelCmhandleDeepCopy.hashCode()
 
 108             assert currentYangModelCmHandle != yangModelCmhandleDeepCopy