2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-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.api.impl.utils
23 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
25 import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration
26 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService
27 import org.onap.cps.spi.utils.CpsValidator
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
30 import spock.lang.Specification
32 class DmiServiceUrlBuilderSpec extends Specification {
34 static YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('dmiServiceName',
35 'dmiDataServiceName', 'dmiModuleServiceName', new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id'),'my-module-set-tag', 'my-alternate-id', 'my-data-producer-identifier')
37 DmiWebClientConfiguration.DmiProperties dmiProperties = new DmiWebClientConfiguration.DmiProperties()
39 def mockCpsValidator = Mock(CpsValidator)
41 def objectUnderTest = new DmiServiceUrlBuilder(dmiProperties, mockCpsValidator)
44 dmiProperties.dmiBasePath = 'dmi'
47 def 'Create the dmi service url with #scenario.'() {
48 given: 'uri variables'
49 def uriVars = objectUnderTest.populateUriVariables(PASSTHROUGH_RUNNING.datastoreName, yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA), 'cmHandle')
51 def uriQueries = objectUnderTest.populateQueryParams(resourceId, 'optionsParamInQuery', topic, moduleSetTag)
52 when: 'a dmi datastore service url is generated'
53 def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars)
54 then: 'service url is generated as expected'
55 assert dmiServiceUrl == expectedDmiServiceUrl
56 where: 'the following parameters are used'
57 scenario | topic | moduleSetTag | resourceId || expectedDmiServiceUrl
58 'With valid resourceId' | 'topicParamInQuery' | '' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery'
59 'With Empty resourceId' | 'topicParamInQuery' | '' | '' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?options=optionsParamInQuery&topic=topicParamInQuery'
60 'With valid moduleSetTag' | 'topicParamInQuery' | 'module-set-tag1' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery&moduleSetTag=module-set-tag1'
61 'With Empty moduleSetTag' | 'topicParamInQuery' | '' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery'
62 'With Empty dmi base path' | 'topicParamInQuery' | '' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery'
63 'With Empty topicParamInQuery' | '' | '' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery'
66 def 'Populate dmi data store url #scenario.'() {
67 given: 'uri variables are created'
68 dmiProperties.dmiBasePath = dmiBasePath
69 def uriVars = objectUnderTest.populateUriVariables(PASSTHROUGH_RUNNING.datastoreName, yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA), 'cmHandle')
70 and: 'null query params'
71 def uriQueries = objectUnderTest.populateQueryParams(null, null, null, null)
72 when: 'a dmi datastore service url is generated'
73 def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars)
74 then: 'the created dmi service url matches the expected'
75 assert dmiServiceUrl == expectedDmiServiceUrl
76 where: 'the following parameters are used'
77 scenario | decription | dmiBasePath || expectedDmiServiceUrl
78 'with base path / ' | 'Invalid base path as it starts with /' | '/dmi' || 'dmiServiceName//dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running'
79 'without base path / ' | 'Valid path as it does not starts with /' | 'dmi' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running'
82 def 'Bath request Url creation.'() {
83 given: 'the required path parameters'
84 def batchRequestUriVariables = [dmiServiceName: 'some-service', dmiBasePath: 'testBase', cmHandleId: '123']
85 and: 'the relevant query parameters'
86 def batchRequestQueryParams = objectUnderTest.getDataOperationRequestQueryParams('some topic', 'some id')
87 when: 'a URL is created'
88 def result = objectUnderTest.getDataOperationRequestUrl(batchRequestQueryParams, batchRequestUriVariables)
89 then: 'it is formed correctly'
90 assert result.toString() == 'some-service/testBase/v1/data?topic=some+topic&requestId=some+id'
93 def 'Populate batch uri variables.'() {
94 expect: 'Populate batch uri variables returns a map with given service name and base path from setup'
95 assert objectUnderTest.populateDataOperationRequestUriVariables('some service') == [dmiServiceName: 'some service', dmiBasePath: 'dmi' ]