be27dfad5d4e1272914f93156f4072713bdfb702
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / JsonUtilsSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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 package org.onap.cps.ncmp.api.impl
21
22 import spock.lang.Specification
23
24 class JsonUtilsSpec extends Specification  {
25     def 'Remove redundant escape characters.'() {
26         expect: 'removing redundant escape characters returns the correct output for #scenario'
27             JsonUtils.removeRedundantEscapeCharacters(input) == expectedOutput
28         where: 'the following input is used'
29             scenario                               | input                                      || expectedOutput
30             'two lines'                            | 'line1\\nline2'                            || 'line1\nline2'
31             'a string inside quotes'               | 'a \\"word in quotes\\"'                   || 'a "word in quotes"'
32             'quotes inside quotes (double escape)' | '\\"quotes \\\\\\"inside\\\\\\" quotes\\"' || '"quotes \\"inside\\" quotes"'  // human readable:  "quotes \"inside\" quotes"
33     }
34     def 'Remove wrapping tokens.'() {
35         expect: 'removing wrapping tokens returns the correct output for #scenario'
36             JsonUtils.removeWrappingTokens(input) == expectedOutput
37         where: 'the following input is used'
38             scenario                           | input    || expectedOutput
39             'a string in quotes'               | '"abc"'  || 'abc'
40             'a string in apostrophes'          | "'abc'"  || 'abc'
41             'a string inside any other tokens' | 'abcde'  || 'bcd'
42     }
43
44     def 'Cannot use constructor.'() {
45         when: 'attempt to construct object'
46             new JsonUtils()
47         then: 'an exception is thrown'
48             thrown(IllegalStateException)
49     }
50 }
51