2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2021-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.api.impl
 
  23 import org.onap.cps.api.CpsAdminService
 
  24 import org.onap.cps.api.CpsModuleService
 
  25 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
 
  26 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
 
  27 import org.onap.cps.ncmp.api.models.CmHandle
 
  28 import org.onap.cps.ncmp.api.models.PersistenceCmHandle
 
  29 import org.onap.cps.spi.model.ModuleReference
 
  30 import org.onap.cps.utils.JsonObjectMapper
 
  31 import spock.lang.Specification
 
  33 class NetworkCmProxyDataServiceImplModelSyncSpec extends Specification {
 
  35     def nullCpsDataService = null
 
  36     def mockJsonObjectMapper = Mock(JsonObjectMapper)
 
  37     def mockCpsModuleService = Mock(CpsModuleService)
 
  38     def mockCpsAdminService = Mock(CpsAdminService)
 
  39     def mockDmiModelOperations = Mock(DmiModelOperations)
 
  40     def mockDmiDataOperations = Mock(DmiDataOperations)
 
  41     def nullNetworkCmProxyDataServicePropertyHandler = null
 
  43     def objectUnderTest = new NetworkCmProxyDataServiceImpl(nullCpsDataService, mockJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
 
  44         mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler)
 
  46     def expectedDataspaceName = 'NFP-Operational'
 
  48     def 'Sync model for a (new) cm handle with #scenario'() {
 
  49         given: 'persistence cm handle is given'
 
  50             def cmHandle = new CmHandle()
 
  51             def dmiServiceName = 'some service name'
 
  52             cmHandle.cmHandleID = 'cm handle id 1'
 
  53             cmHandle.dmiProperties = dmiProperties
 
  54             def persistenceCmHandle = PersistenceCmHandle.toPersistenceCmHandle(dmiServiceName, '' , '', cmHandle)
 
  55         and: 'DMI operations returns some module references'
 
  56             def moduleReferences =  [ new ModuleReference(moduleName:'module1',revision:'1'),
 
  57                                                             new ModuleReference(moduleName:'module2',revision:'2') ]
 
  58             mockDmiModelOperations.getModuleReferences(persistenceCmHandle) >> moduleReferences
 
  59         and: 'CPS-Core returns list of existing module resources'
 
  60             mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> existingModuleResourcesInCps
 
  61         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
 
  62             mockDmiModelOperations.getNewYangResourcesFromDmi(persistenceCmHandle, [new ModuleReference('module1', '1')]) >> yangResourceToContentMap
 
  63         when: 'module sync is triggered'
 
  64             objectUnderTest.syncModulesAndCreateAnchor(persistenceCmHandle)
 
  65         then: 'the CPS module service is called once with the correct parameters'
 
  66             1 * mockCpsModuleService.createSchemaSetFromModules(expectedDataspaceName, persistenceCmHandle.getId(), yangResourceToContentMap, expectedKnownModules)
 
  67         and: 'admin service create anchor method has been called with correct parameters'
 
  68             1 * mockCpsAdminService.createAnchor(expectedDataspaceName, persistenceCmHandle.getId(), persistenceCmHandle.getId())
 
  69         where: 'the following parameters are used'
 
  70             scenario                        | dmiProperties        | existingModuleResourcesInCps                                               | yangResourceToContentMap      || expectedKnownModules
 
  71             'one unknown module'            | ['name1': 'value1']  | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | [module1: 'some yang source'] || [new ModuleReference('module2', '2')]
 
  72             'no add. properties'            | [:]                  | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | [module1: 'some yang source'] || [new ModuleReference('module2', '2')]
 
  73             'no unknown module'             | [:]                  | [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')] | [:]                           || [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')]