6ca3105500ca9b17824627a4d76de2381cb090e1
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / DmiServiceUrlBuilderSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.utils
22
23 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService
24 import org.onap.cps.spi.utils.CpsValidator
25
26 import static org.onap.cps.ncmp.api.impl.operations.DataStoreEnum.PASSTHROUGH_RUNNING
27
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
30 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
31 import spock.lang.Shared
32 import spock.lang.Specification
33
34 class DmiServiceUrlBuilderSpec extends Specification {
35
36     @Shared
37     YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('dmiServiceName',
38         'dmiDataServiceName', 'dmiModuleServiceName', new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id'))
39
40     NcmpConfiguration.DmiProperties dmiProperties = new NcmpConfiguration.DmiProperties()
41
42     def mockCpsValidator = Mock(CpsValidator)
43
44     def objectUnderTest = new DmiServiceUrlBuilder(dmiProperties, mockCpsValidator)
45
46     def 'Create the dmi service url with #scenario.'() {
47         given: 'uri variables'
48             dmiProperties.dmiBasePath = 'dmi'
49             def uriVars = objectUnderTest.populateUriVariables(PASSTHROUGH_RUNNING.value, yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA),
50                     "cmHandle")
51         and: 'query params'
52                             def uriQueries = objectUnderTest.populateQueryParams(resourceId,
53                     'optionsParamInQuery', topic)
54         when: 'a dmi datastore service url is generated'
55             def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars)
56         then: 'service url is generated as expected'
57             assert dmiServiceUrl == expectedDmiServiceUrl
58         where: 'the following parameters are used'
59             scenario                       | topic               | resourceId   || expectedDmiServiceUrl
60             'With valid resourceId'        | 'topicParamInQuery' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery'
61             'With Empty resourceId'        | 'topicParamInQuery' | ''           || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?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'
64     }
65
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.value, yangModelCmHandle.resolveDmiServiceName(RequiredDmiService.DATA),
70                     "cmHandle")
71         and: 'null query params'
72             def uriQueries = objectUnderTest.populateQueryParams(null,
73                     null, null)
74         when: 'a dmi datastore service url is generated'
75             def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars)
76         then: 'the created dmi service url matches the expected'
77             assert dmiServiceUrl == expectedDmiServiceUrl
78         where: 'the following parameters are used'
79             scenario               | decription                                | dmiBasePath || expectedDmiServiceUrl
80             'with base path  / '   | 'Invalid base path as it starts with /'   | '/dmi'      || 'dmiServiceName//dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running'
81             'without base path / ' | 'Valid path as it does not starts with /' | 'dmi'       || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running'
82     }
83 }