6d56f432d3800c83d8eb88b7b4bb2d60596fbf88
[cps.git] /
1 /*
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
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.url.builder
22
23 import spock.lang.Specification
24
25 class DmiServiceUrlTemplateBuilderSpec extends Specification {
26
27     def objectUnderTest = new DmiServiceUrlTemplateBuilder()
28
29     def 'Build URL template parameters with (variable) path segments and query parameters.'() {
30         given: 'the URL details are given to the builder'
31             objectUnderTest.fixedPathSegment('segment')
32             objectUnderTest.variablePathSegment('myVariableSegment','someValue')
33             objectUnderTest.fixedPathSegment('segment?with:special&characters')
34             objectUnderTest.queryParameter('param1', 'abc')
35             objectUnderTest.queryParameter('param2', 'value?with#special:characters')
36         when: 'the URL template parameters are created'
37             def result = objectUnderTest.createUrlTemplateParameters('myDmiServer', 'myBasePath')
38         then: 'the URL template contains variable names instead of value and un-encoded fixed segment'
39             assert result.urlTemplate == 'myDmiServer/myBasePath/v1/segment/{myVariableSegment}/segment?with:special&characters?param1={param1}&param2={param2}'
40         and: 'URL variables contains name and un-encoded value pairs'
41             assert result.urlVariables == ['myVariableSegment': 'someValue', 'param1': 'abc', 'param2': 'value?with#special:characters']
42     }
43
44     def 'Build URL template parameters with special characters in query parameters.'() {
45         given: 'the query parameter is given to the builder'
46            objectUnderTest.queryParameter('my&param', 'special&characters=are?not\\encoded')
47         when: 'the URL template parameters are created'
48             def result = objectUnderTest.createUrlTemplateParameters('myDmiServer', 'myBasePath')
49         then: 'Special characters are not encoded'
50             assert result.urlVariables == ['my&param': 'special&characters=are?not\\encoded']
51     }
52
53     def 'Build URL template parameters with empty query parameters.'() {
54         when: 'the query parameter is given to the builder'
55             objectUnderTest.queryParameter('param', value)
56         and: 'the URL template parameters are create'
57             def result = objectUnderTest.createUrlTemplateParameters('myDmiServer', 'myBasePath')
58         then: 'no parameter gets added'
59             assert result.urlVariables.isEmpty()
60         where: 'the following parameter values are used'
61             value << [ null, '', ' ' ]
62     }
63 }