3c409e867d44ec12a894287bcbe0baeaa9a95088
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.dg.common.impl;
24
25 import ch.qos.logback.core.Appender;
26
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.openecomp.appc.dg.common.impl.JsonDgUtilImpl;
30 import org.openecomp.appc.exceptions.APPCException;
31 import org.openecomp.sdnc.sli.SvcLogicContext;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
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("error-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
146
147     @Test
148     public void testGenerateOutputPayloadFromContext() throws Exception {
149
150         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
151         String key = "output.payload";
152         String key1 = "output.payload.test-key[0]";
153         String key2 = "output.payload.test-key[1]";
154         String testValueKey1 = "value1";
155         String testValueKey2 = "value2";
156
157         String key3 = "output.payload.test-key3";
158         String testValueKey3 = "value3";
159
160         SvcLogicContext ctx = new SvcLogicContext();
161         Map<String, String> params = new HashMap<>();
162         ctx.setAttribute(key1, testValueKey1);
163         ctx.setAttribute(key2, testValueKey2);
164         ctx.setAttribute(key3, testValueKey3);
165         jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
166
167         Assert.assertNotNull(ctx.getAttribute(key));
168
169     }
170 }