2  *  ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2023 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.utils
 
  23 import com.fasterxml.jackson.databind.ObjectMapper
 
  24 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
 
  25 import org.onap.cps.ncmp.api.inventory.CmHandleState
 
  26 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder
 
  27 import org.onap.cps.ncmp.api.models.ResourceDataBatchRequest
 
  28 import org.onap.cps.ncmp.utils.TestUtils
 
  29 import org.onap.cps.utils.JsonObjectMapper
 
  30 import org.spockframework.spring.SpringBean
 
  31 import spock.lang.Specification
 
  33 class ResourceDataBatchRequestUtilsSpec extends Specification {
 
  36     JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
 
  38     def 'Process per operation in batch request with #serviceName.'() {
 
  39         given: 'batch request with 3 operations'
 
  40             def resourceDataBatchRequestJsonData = TestUtils.getResourceFileContent('resourceDataBatchRequest.json')
 
  41             def resourceDataBatchRequest = jsonObjectMapper.convertJsonString(resourceDataBatchRequestJsonData, ResourceDataBatchRequest.class)
 
  42         and: '4 known cm handles: ch1-dmi1, ch2-dmi1, ch3-dmi2, ch4-dmi2'
 
  43             def yangModelCmHandles = getYangModelCmHandles()
 
  44         when: 'Operation in batch request is processed'
 
  45             def operationsOutPerDmiServiceName = ResourceDataBatchRequestUtils.processPerOperationInBatchRequest(resourceDataBatchRequest, yangModelCmHandles)
 
  46         and: 'converted to a json node'
 
  47             def dmiBatchRequestBody = jsonObjectMapper.asJsonString(operationsOutPerDmiServiceName.get(serviceName))
 
  48             def dmiBatchRequestBodyAsJsonNode = jsonObjectMapper.convertToJsonNode(dmiBatchRequestBody).get(operationIndex)
 
  49         then: 'it contains the correct operation details'
 
  50             assert dmiBatchRequestBodyAsJsonNode.get('operation').asText() == 'read'
 
  51             assert dmiBatchRequestBodyAsJsonNode.get('operationId').asText() == expectedOperationId
 
  52             assert dmiBatchRequestBodyAsJsonNode.get('datastore').asText() == expectedDatastore
 
  53         and: 'the correct cm handles (just for #serviceName)'
 
  54             assert dmiBatchRequestBodyAsJsonNode.get('cmHandles').size() == expectedCmHandleIds.size()
 
  55             expectedCmHandleIds.each {
 
  56                 dmiBatchRequestBodyAsJsonNode.get('cmHandles').toString().contains(it)
 
  58         where: 'the following dmi service and operations are checked'
 
  59             serviceName | operationIndex || expectedOperationId | expectedDatastore                        | expectedCmHandleIds
 
  60             'dmi1'      | 0              || 'operational-14'    | 'ncmp-datastore:passthrough-operational' | ['ch6-dmi1']
 
  61             'dmi1'      | 1              || 'running-12'        | 'ncmp-datastore:passthrough-running'     | ['ch1-dmi1', 'ch2-dmi1']
 
  62             'dmi1'      | 2              || 'operational-15'    | 'ncmp-datastore:passthrough-operational' | ['ch6-dmi1']
 
  63             'dmi2'      | 0              || 'operational-14'    | 'ncmp-datastore:passthrough-operational' | ['ch3-dmi2']
 
  64             'dmi2'      | 1              || 'running-12'        | 'ncmp-datastore:passthrough-running'     | ['ch7-dmi2']
 
  65             'dmi2'      | 2              || 'operational-15'    | 'ncmp-datastore:passthrough-operational' | ['ch4-dmi2']
 
  68     static def getYangModelCmHandles() {
 
  69         def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
 
  70         def readyState = new CompositeStateBuilder().withCmHandleState(CmHandleState.READY).withLastUpdatedTimeNow().build()
 
  71         return [new YangModelCmHandle(id: 'ch1-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
 
  72                 new YangModelCmHandle(id: 'ch2-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
 
  73                 new YangModelCmHandle(id: 'ch6-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
 
  74                 new YangModelCmHandle(id: 'ch8-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
 
  75                 new YangModelCmHandle(id: 'ch3-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
 
  76                 new YangModelCmHandle(id: 'ch4-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
 
  77                 new YangModelCmHandle(id: 'ch7-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),