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 spock.lang.Specification
25 class DmiServiceUrlBuilderSpec extends Specification {
27 def objectUnderTest = new DmiServiceUrlBuilder()
29 def 'Build URI with (variable) path segments and parameters.'() {
30 given: 'the URI details are given to the builder'
31 objectUnderTest.pathSegment(segment1)
32 objectUnderTest.variablePathSegment('myVariableSegment','someValue')
33 objectUnderTest.pathSegment(segment2)
34 objectUnderTest.queryParameter('param1', paramValue1)
35 objectUnderTest.queryParameter('param2', paramValue2)
36 objectUnderTest.queryParameter('param3', null)
37 objectUnderTest.queryParameter('param4', '')
38 when: 'the URI (string) is build'
39 def result = objectUnderTest.build('myDmiServer', 'myBasePath')
40 then: 'the URI is correct (segments are in correct order) '
41 assert result == expectedUri
42 where: 'following URI details are used'
43 segment1 | segment2 | paramValue1 | paramValue2 || expectedUri
44 'segment1' | 'segment2' | '123' | 'abc' || 'myDmiServer/myBasePath/v1/segment1/someValue/segment2?param1=123¶m2=abc'
45 'segment2' | 'segment1' | 'abc' | '123' || 'myDmiServer/myBasePath/v1/segment2/someValue/segment1?param1=abc¶m2=123'
48 def 'Build URI with special characters in path segments.'() {
49 given: 'the path segments are given to the builder'
50 objectUnderTest.pathSegment(segment)
51 objectUnderTest.variablePathSegment('myVariableSegment', variableSegmentValue)
52 when: 'the URI (string) is build'
53 def result = objectUnderTest.build('myDmiServer', 'myBasePath')
54 then: 'Only teh characters that cause issues in path segments issues are encoded'
55 assert result == expectedUri
56 where: 'following variable path segments are used'
57 segment | variableSegmentValue || expectedUri
58 'some/special?characters=are\\encoded' | 'my/variable/segment' || 'myDmiServer/myBasePath/v1/some%2Fspecial%3Fcharacters=are%5Cencoded/my%2Fvariable%2Fsegment'
59 'but=some&are:not-!' | 'my&variable:segment' || 'myDmiServer/myBasePath/v1/but=some&are:not-!/my&variable:segment'
62 def 'Build URI with special characters in query parameters.'() {
63 given: 'the query parameter is given to the builder'
64 objectUnderTest.queryParameter(paramName, value)
65 when: 'the URI (string) is build'
66 def result = objectUnderTest.build('myDmiServer', 'myBasePath')
67 then: 'Only the characters (in the name and value) that cause issues in query parameters are encoded'
68 assert result == expectedUri
69 where: 'the following query parameters are used'
70 paramName | value || expectedUri
71 'my¶m' | 'some?special&characters=are\\encoded' || 'myDmiServer/myBasePath/v1?my%26param=some?special%26characters%3Dare%5Cencoded'
72 'my-param' | 'but/some:are-not-!' || 'myDmiServer/myBasePath/v1?my-param=but/some:are-not-!'
75 def 'Build URI with empty query parameters.'() {
76 when: 'the query parameter is given to the builder'
77 objectUnderTest.queryParameter('param', value)
78 and: 'the URI (string) is build'
79 def result = objectUnderTest.build('myDmiServer', 'myBasePath')
80 then: 'no parameter gets added'
81 assert result == 'myDmiServer/myBasePath/v1'
82 where: 'the following parameter values are used'
83 value << [ null, '', ' ' ]