aboutsummaryrefslogtreecommitdiffstats
path: root/appc-config/appc-config-generator/provider/src/test/java/org/onap/sdnc/config/generator/tool/TestJSONTool.java
blob: fcc0c7c27ff7e6bd4cee311c79239038d15c68d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.onap.sdnc.config.generator.tool;

import org.codehaus.jettison.json.JSONException;
import org.junit.Assert;
import org.junit.Test;

public class TestJSONTool {

    @Test
    public void testEscapeInternalJson() {
        String testData = "{\"test1\":\"value1\",\"test2\":\"{\"key1\":\"value\"}\"}";
        String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
        try {
            Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
        } catch (JSONException e) {
            Assert.fail();
        }
    }

    @Test
    public void testEscapeInternalJson_alreadyEscaped() {
        String testData = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
        String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
        try {
            Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
        } catch (JSONException e) {
            Assert.fail();
        }
    }

    @Test
    public void testEscapeInternalJson_withNewLines() {
        String testData = "{\"test1\":\"value1\",\"test2\":\"\n{\"key1\":\"value\"\n}\"}";
        String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"\n{\\\"key1\\\":\\\"value\\\"\n}\"}";
        try {
            Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
        } catch (JSONException e) {
            Assert.fail();
        }
    }

}