Add method to escape json strings
[appc.git] / appc-config / appc-config-generator / provider / src / test / java / org / onap / sdnc / config / generator / tool / TestJSONTool.java
1 package org.onap.sdnc.config.generator.tool;
2
3 import org.codehaus.jettison.json.JSONException;
4 import org.junit.Assert;
5 import org.junit.Test;
6
7 public class TestJSONTool {
8
9     @Test
10     public void testEscapeInternalJson() {
11         String testData = "{\"test1\":\"value1\",\"test2\":\"{\"key1\":\"value\"}\"}";
12         String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
13         try {
14             Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
15         } catch (JSONException e) {
16             Assert.fail();
17         }
18     }
19
20     @Test
21     public void testEscapeInternalJson_alreadyEscaped() {
22         String testData = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
23         String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"{\\\"key1\\\":\\\"value\\\"}\"}";
24         try {
25             Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
26         } catch (JSONException e) {
27             Assert.fail();
28         }
29     }
30
31     @Test
32     public void testEscapeInternalJson_withNewLines() {
33         String testData = "{\"test1\":\"value1\",\"test2\":\"\n{\"key1\":\"value\"\n}\"}";
34         String expectedOutput = "{\"test1\":\"value1\",\"test2\":\"\n{\\\"key1\\\":\\\"value\\\"\n}\"}";
35         try {
36             Assert.assertEquals(expectedOutput, JSONTool.escapeInternalJson(testData));
37         } catch (JSONException e) {
38             Assert.fail();
39         }
40     }
41
42 }