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.url.builder
 
  23 import spock.lang.Specification
 
  25 class DmiServiceUrlTemplateBuilderSpec extends Specification {
 
  27     def objectUnderTest = new DmiServiceUrlTemplateBuilder()
 
  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}¶m2={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']
 
  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¶m', '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¶m': 'special&characters=are?not\\encoded']
 
  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, '', ' ' ]