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 com.fasterxml.jackson.core.JsonProcessingException
 
  24 import com.fasterxml.jackson.databind.ObjectMapper
 
  25 import org.onap.cps.api.CpsAdminService
 
  26 import org.onap.cps.api.CpsDataService
 
  27 import org.onap.cps.api.CpsModuleService
 
  28 import org.onap.cps.ncmp.api.impl.exception.DmiRequestException
 
  29 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
 
  30 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
 
  31 import org.onap.cps.ncmp.api.models.CmHandle
 
  32 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
 
  33 import org.onap.cps.spi.exceptions.DataNodeNotFoundException
 
  34 import org.onap.cps.spi.exceptions.DataValidationException
 
  35 import org.onap.cps.utils.JsonObjectMapper
 
  36 import spock.lang.Shared
 
  37 import spock.lang.Specification
 
  39 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED
 
  41 class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification {
 
  44     def persistenceCmHandle = new CmHandle()
 
  47     def cmHandlesArray = ['cmHandle001']
 
  49     def mockCpsDataService = Mock(CpsDataService)
 
  50     def mockCpsModuleService = Mock(CpsModuleService)
 
  51     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
 
  52     def mockCpsAdminService = Mock(CpsAdminService)
 
  53     def mockDmiModelOperations = Mock(DmiModelOperations)
 
  54     def mockDmiDataOperations = Mock(DmiDataOperations)
 
  55     def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler)
 
  57     def noTimestamp = null
 
  59     def 'Register or re-register a DMI Plugin for the given cm-handle(s) with #scenario process.'() {
 
  60         given: 'a registration'
 
  61             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
  62             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'my-server')
 
  63             persistenceCmHandle.cmHandleID = '123'
 
  64             persistenceCmHandle.dmiProperties = [dmiProp1: 'dmiValue1', dmiProp2: 'dmiValue2']
 
  65             persistenceCmHandle.publicProperties = [publicProp1: 'publicValue1', publicProp2: 'publicValue2']
 
  66             dmiPluginRegistration.createdCmHandles = createdCmHandles
 
  67             dmiPluginRegistration.updatedCmHandles = updatedCmHandles
 
  68             dmiPluginRegistration.removedCmHandles = removedCmHandles
 
  69             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,' +
 
  70                 '"additional-properties":[{"name":"dmiProp1","value":"dmiValue1"},{"name":"dmiProp2","value":"dmiValue2"}],' +
 
  71                 '"public-properties":[{"name":"publicProp1","value":"publicValue1"},{"name":"publicProp2","value":"publicValue2"}]' +
 
  73         when: 'registration is updated and modules are synced'
 
  74             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
 
  75         then: 'save list elements is invoked with the expected parameters'
 
  76             expectedCallsToSaveNode * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
 
  77                 '/dmi-registry', expectedJsonData, noTimestamp)
 
  78         and: 'update data node leaves is called with correct parameters'
 
  79             expectedCallsToUpdateCmHandleProperty * mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(updatedCmHandles)
 
  80         and: 'delete schema set is invoked with the correct parameters'
 
  81             expectedCallsToDeleteSchemaSetAndListElement * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'cmHandle001', CASCADE_DELETE_ALLOWED)
 
  82         and: 'delete list or list element is invoked with the correct parameters'
 
  83             expectedCallsToDeleteSchemaSetAndListElement * mockCpsDataService.deleteListOrListElement('NCMP-Admin',
 
  84                 'ncmp-dmi-registry', "/dmi-registry/cm-handles[@id='cmHandle001']", noTimestamp)
 
  86             scenario                    | createdCmHandles      | updatedCmHandles      | removedCmHandles || expectedCallsToSaveNode | expectedCallsToDeleteSchemaSetAndListElement | expectedCallsToUpdateCmHandleProperty
 
  87             'create'                    | [persistenceCmHandle] | []                    | []               || 1                       | 0                                            | 0
 
  88             'update'                    | []                    | [persistenceCmHandle] | []               || 0                       | 0                                            | 1
 
  89             'delete'                    | []                    | []                    | cmHandlesArray   || 0                       | 1                                            | 0
 
  90             'create, update and delete' | [persistenceCmHandle] | [persistenceCmHandle] | cmHandlesArray   || 1                       | 1                                            | 1
 
  91             'no valid data'             | []                    | []                    | []               || 0                       | 0                                            | 0
 
  94     def 'Register a DMI Plugin for the given cm-handle(s) without DMI properties.'() {
 
  95         given: 'a registration without cm-handle properties'
 
  96             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
  97             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
 
  98             persistenceCmHandle.cmHandleID = '123'
 
  99             persistenceCmHandle.dmiProperties = Collections.emptyMap()
 
 100             persistenceCmHandle.publicProperties = Collections.emptyMap()
 
 101             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
 
 102             def expectedJsonData = '{"cm-handles":[{"id":"123","dmi-service-name":"my-server","dmi-data-service-name":null,"dmi-model-service-name":null,"additional-properties":[],"public-properties":[]}]}'
 
 103         when: 'registration is updated'
 
 104             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
 
 105         then: 'save list elements is invoked with the expected parameters'
 
 106             1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
 
 107                     '/dmi-registry', expectedJsonData, noTimestamp)
 
 110     def 'Register a DMI Plugin for a given cm-handle(s) with JSON processing errors during process.'() {
 
 111         given: 'a registration without cm-handle properties '
 
 112             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
 113             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
 
 114             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
 
 115         and: 'an json processing exception occurs'
 
 116             spiedJsonObjectMapper.asJsonString(_) >> { throw (new JsonProcessingException('')) }
 
 117         when: 'registration is updated and modules are synced'
 
 118             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
 
 119         then: 'a data validation exception is thrown'
 
 120             thrown(DataValidationException)
 
 123     def 'Register a DMI Plugin for the given cm-handle(s) with no data found during delete process.'() {
 
 124         given: 'a registration without cm-handle properties '
 
 125             NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
 126             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'some-plugin')
 
 127             dmiPluginRegistration.removedCmHandles = ['some cm handle']
 
 128         and: 'an json processing exception occurs during delete process'
 
 129             mockCpsDataService.deleteListOrListElement(*_) >>  { throw (new DataNodeNotFoundException('','')) }
 
 130         when: 'registration is updated and modules are synced'
 
 131             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
 
 132         then: 'no exception is thrown'
 
 136     def 'Register a DMI Plugin for the given cm-handle(s) with no schema set found during delete process.'() {
 
 137         given: 'a registration'
 
 138             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
 139             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:'my-server')
 
 140             dmiPluginRegistration.removedCmHandles = cmHandlesArray
 
 141         and: 'an exception occurs during delete schema set process'
 
 142             mockCpsModuleService.deleteSchemaSet(_,_,_) >>  { throw (new Exception('')) }
 
 143         when: 'registration is updated and modules are synced'
 
 144             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
 
 145         then: 'delete list or list element is still called'
 
 146             1 * mockCpsDataService.deleteListOrListElement(_,_,_,_)
 
 149     def 'Dmi plugin registration with #scenario'() {
 
 150         given: 'a registration '
 
 151             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
 152             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
 
 153                     dmiDataPlugin:dmiDataPlugin)
 
 154             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
 
 155         when: 'update registration and sync module is called with correct DMI plugin information'
 
 156             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
 
 157         then: 'create cm handles registration and sync modules is called with the correct plugin information'
 
 158             1 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
 
 160             scenario                          | dmiPlugin  | dmiModelPlugin | dmiDataPlugin
 
 161             'combined DMI plugin'             | 'service1' | ''             | ''
 
 162             'data & model DMI plugins'        | ''         | 'service1'     | 'service2'
 
 163             'data & model using same service' | ''         | 'service1'     | 'service1'
 
 166     def 'Invalid DMI plugin registration with #scenario'() {
 
 167         given: 'a registration '
 
 168             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
 169             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin:dmiPlugin, dmiModelPlugin:dmiModelPlugin,
 
 170                     dmiDataPlugin:dmiDataPlugin)
 
 171             dmiPluginRegistration.createdCmHandles = [persistenceCmHandle]
 
 172         when: 'registration is called with incorrect DMI plugin information'
 
 173             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginRegistration)
 
 174         then: 'a DMI Request Exception is thrown with correct message details'
 
 175             def exceptionThrown = thrown(DmiRequestException.class)
 
 176             assert exceptionThrown.getMessage().contains(expectedMessageDetails)
 
 177         and: 'registration is not called'
 
 178             0 * objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(dmiPluginRegistration)
 
 180             scenario                        | dmiPlugin  | dmiModelPlugin | dmiDataPlugin || expectedMessageDetails
 
 181             'empty DMI plugins'             | ''         | ''             | ''            || 'No DMI plugin service names'
 
 182             'blank DMI plugins'             | ' '        | ' '            | ' '           || 'No DMI plugin service names'
 
 183             'null DMI plugins'              | null       | null           | null          || 'No DMI plugin service names'
 
 184             'all DMI plugins'               | 'service1' | 'service2'     | 'service3'    || 'Cannot register combined plugin service name and other service names'
 
 185             '(combined)DMI and Data Plugin' | 'service1' | ''             | 'service2'    || 'Cannot register combined plugin service name and other service names'
 
 186             '(combined)DMI and model Plugin'| 'service1' | 'service2'     | ''            || 'Cannot register combined plugin service name and other service names'
 
 187             'only model DMI plugin'         | ''         | 'service1'     | ''            || 'Cannot register just a Data or Model plugin service name'
 
 188             'only data DMI plugin'          | ''         | ''             | 'service1'    || 'Cannot register just a Data or Model plugin service name'
 
 191     def 'Exception thrown on CM-Handle registration update request'() {
 
 192         given: 'a CM-handle registration'
 
 193             def objectUnderTest = getObjectUnderTestWithModelSyncDisabled()
 
 194         and: 'dmi plugin registration input update request'
 
 195             def dmiPluginReg = new DmiPluginRegistration();
 
 196             dmiPluginReg.dmiPlugin = 'onap.dmap.plugin';
 
 197             dmiPluginReg.updatedCmHandles = [new CmHandle(cmHandleID: 'unknownHandle')]
 
 198         and: 'update data node leaves is unable to find data node'
 
 199             mockNetworkCmProxyDataServicePropertyHandler.updateCmHandleProperties(*_) >> { throw new DataNodeNotFoundException('NCMP-Admin', 'ncmp-dmi-registry') }
 
 200         when: 'update dmi registration is called'
 
 201             objectUnderTest.updateDmiRegistrationAndSyncModule(dmiPluginReg)
 
 202         then: 'data validation exception is thrown'
 
 203             def exceptionThrown = thrown(DataValidationException.class)
 
 204             assert exceptionThrown.getDetails().contains('DataNode not found')
 
 207     def getObjectUnderTestWithModelSyncDisabled() {
 
 208         def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
 
 209             mockCpsModuleService, mockCpsAdminService, mockNetworkCmProxyDataServicePropertyHandler))
 
 210         objectUnderTest.syncModulesAndCreateAnchor(*_) >> null
 
 211         return objectUnderTest