Moving all files to root directory
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / openecomp / appc / dg / common / impl / JsonDgUtilImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.dg.common.impl;
23
24 import ch.qos.logback.core.Appender;
25
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.openecomp.appc.dg.common.impl.JsonDgUtilImpl;
29 import org.openecomp.appc.exceptions.APPCException;
30 import org.openecomp.sdnc.sli.SvcLogicContext;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import static org.junit.Assert.*;
36 import static org.mockito.Mockito.mock;
37
38 public class JsonDgUtilImplTest {
39
40     private final Appender appender = mock(Appender.class);
41
42
43     @Test
44     public void testFlatAndAddToContext() throws Exception {
45         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
46         String key = "payload";
47         String testValueKey = "test-key";
48         String testValueValue = "test-value";
49         String testValueKey2 = "test-key2";
50         String testValueValue2 = "test-value2";
51         String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
52
53         SvcLogicContext ctx = new SvcLogicContext();
54         Map<String, String> params = new HashMap<>();
55         params.put(key, payload);
56         jsonDgUtil.flatAndAddToContext(params, ctx);
57
58
59             Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
60             Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
61
62
63
64     }
65
66
67     @Test
68     public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
69         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
70         String key = "payload";
71         String testValueKey = "test-key";
72         String testValueValue = "test-value";
73         String testValueKey2 = "test-key2";
74         String testValueValue2 = "test-value2";
75         String payload = "{{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
76
77         SvcLogicContext ctx = new SvcLogicContext();
78         Map<String, String> params = new HashMap<>();
79         params.put(key, payload);
80         try {
81             jsonDgUtil.flatAndAddToContext(params, ctx);
82
83         } catch (APPCException e) {
84             Assert.assertNull(ctx.getAttribute(testValueKey));
85             Assert.assertNull(ctx.getAttribute(testValueKey2));
86             Assert.assertNotNull(ctx.getAttribute("output.status.message"));
87         }
88
89
90     }
91
92
93     @Test
94     public void testFlatAndAddToContextPayloadFromContext() throws Exception {
95         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
96         String key = "payload";
97         String testValueKey = "test-key";
98         String testValueValue = "test-value";
99         String testValueKey2 = "test-key2";
100         String testValueValue2 = "test-value2";
101         String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
102
103         SvcLogicContext ctx = new SvcLogicContext();
104         Map<String, String> params = new HashMap<>();
105         params.put(key, "");
106         ctx.setAttribute("input.payload", payload);
107         jsonDgUtil.flatAndAddToContext(params, ctx);
108
109
110         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
111         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
112     }
113
114     @Test
115     public void testFlatAndAddToContextNegativeNullPayload() throws Exception {
116         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
117         String testValueKey = "test-key";
118         String testValueKey2 = "test-key2";
119         SvcLogicContext ctx = new SvcLogicContext();
120         Map<String, String> params = new HashMap<>();
121         jsonDgUtil.flatAndAddToContext(params, ctx);
122
123
124         Assert.assertNull(ctx.getAttribute(testValueKey));
125         Assert.assertNull(ctx.getAttribute(testValueKey2));
126     }
127
128
129     @Test
130     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
131
132         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
133         String key = "payload";
134         String testValueKey = "test-key";
135         String testValueKey2 = "test-key2";
136
137         SvcLogicContext ctx = new SvcLogicContext();
138         Map<String, String> params = new HashMap<>();
139         params.put(key, "");
140         jsonDgUtil.flatAndAddToContext(params, ctx);
141
142         Assert.assertNull(ctx.getAttribute(testValueKey));
143         Assert.assertNull(ctx.getAttribute(testValueKey2));
144     }
145 }