2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.output;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.cli.fw.error.OnapCommandException;
29 import org.onap.cli.fw.input.OnapCommandParameterType;
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
34 public class OnapCommandResultTest {
38 public void commandResultObjTest() throws OnapCommandException { //NOSONAR
39 OnapCommandResult res = new OnapCommandResult();
40 res.setDebugInfo("debugInfo");
41 res.setIncludeSeparator(true);
42 res.setIncludeTitle(true);
43 res.setOutput("Output");
44 res.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
45 res.setRecords(new ArrayList<OnapCommandResultAttribute>());
46 res.setScope(OnapCommandResultAttributeScope.LONG);
47 res.setType(OnapCommandResultType.TABLE);
50 assertTrue("debugInfo".equals(res.getDebugInfo()) && res.isIncludeSeparator()
51 && "Output".equals(res.getOutput())
52 && OnapCommandPrintDirection.LANDSCAPE.equals(res.getPrintDirection())
53 && res.getRecords().isEmpty() && OnapCommandResultAttributeScope.LONG.equals(res.getScope())
54 && OnapCommandResultType.TABLE.equals(res.getType()));
56 String help = res.print();
61 public void commandResultPrintLandscapeTableTest() throws OnapCommandException { //NOSONAR
62 OnapCommandResult res = new OnapCommandResult();
63 res.setDebugInfo("debugInfo");
64 res.setIncludeSeparator(true);
65 res.setIncludeTitle(true);
66 res.setOutput("Output");
67 res.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
69 OnapCommandResultAttribute att = new OnapCommandResultAttribute();
71 att.setDescription("description");
72 att.setType(OnapCommandParameterType.STRING);
73 att.setValues(new ArrayList<String>(Arrays.asList(new String[] { "value" })));
74 List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
77 res.setScope(OnapCommandResultAttributeScope.LONG);
78 res.setType(OnapCommandResultType.TABLE);
80 String expRes = "+--------+\n|param |\n+--------+\n|value |\n+--------+\n";
81 String result = res.print();
83 assertEquals(expRes, result);
89 public void commandResultPrintLandscapeJsonTest() throws OnapCommandException { //NOSONAR
90 OnapCommandResult res = new OnapCommandResult();
91 res.setDebugInfo("debugInfo");
92 res.setIncludeSeparator(true);
93 res.setIncludeTitle(true);
94 res.setOutput("Output");
95 res.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
97 OnapCommandResultAttribute att = new OnapCommandResultAttribute();
99 att.setDescription("description");
100 att.setType(OnapCommandParameterType.JSON);
102 new ArrayList<String>(Arrays.asList(new String[] { "{\"id\": \"0001\",\"value\": \"result\"}" })));
103 List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
105 res.setRecords(list);
106 res.setScope(OnapCommandResultAttributeScope.LONG);
107 res.setType(OnapCommandResultType.JSON);
109 // Will be handled after the json print is implemented
110 String result = res.print();
111 assertEquals("[{\"param\":{\"id\":\"0001\",\"value\":\"result\"}}]",result);
112 // String expRes = "+--------+\n|param |\n+--------+\n|value
114 // assertEquals(expRes,result);
120 public void commandResultPrintLandscapeCsvTest() throws OnapCommandException { //NOSONAR
121 OnapCommandResult res = new OnapCommandResult();
122 res.setDebugInfo("debugInfo");
123 res.setIncludeSeparator(true);
124 res.setIncludeTitle(true);
125 res.setOutput("Output");
126 res.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
128 OnapCommandResultAttribute att = new OnapCommandResultAttribute();
129 att.setName("param");
130 att.setDescription("description");
131 att.setType(OnapCommandParameterType.STRING);
132 att.setValues(new ArrayList<String>(Arrays.asList(new String[] { "value" })));
133 List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
135 OnapCommandResultAttribute a1 = new OnapCommandResultAttribute();
136 a1.setName("param1");
137 a1.setDescription("description1");
138 a1.setType(OnapCommandParameterType.STRING);
139 a1.setValues(new ArrayList<String>(Arrays.asList(new String[] { "value1" })));
142 res.setRecords(list);
143 res.setScope(OnapCommandResultAttributeScope.LONG);
144 res.setType(OnapCommandResultType.CSV);
146 String expRes = "param,param1\r\n";
147 String result = res.print();
148 assertEquals(expRes, result);
154 public void commandResultPrintPortraitCsvTest() throws OnapCommandException { //NOSONAR
155 OnapCommandResult res = new OnapCommandResult();
156 res.setDebugInfo("debugInfo");
157 res.setIncludeSeparator(true);
158 res.setIncludeTitle(true);
159 res.setOutput("Output");
160 res.setPrintDirection(OnapCommandPrintDirection.PORTRAIT);
162 OnapCommandResultAttribute att = new OnapCommandResultAttribute();
163 att.setName("param");
164 att.setDescription("description");
165 att.setType(OnapCommandParameterType.STRING);
166 att.setValues(new ArrayList<String>(Arrays.asList(new String[] { "value" })));
167 List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
169 OnapCommandResultAttribute a1 = new OnapCommandResultAttribute();
170 a1.setName("param1");
171 a1.setDescription("description1");
172 a1.setType(OnapCommandParameterType.STRING);
173 a1.setValues(new ArrayList<String>(Arrays.asList(new String[] { "value1" })));
176 res.setRecords(list);
177 res.setScope(OnapCommandResultAttributeScope.LONG);
178 res.setType(OnapCommandResultType.CSV);
179 String expRes = "property,value\r\nparam,value\r\n";
180 String result = res.print();
181 assertEquals(expRes, result);
186 public void commandResultPrintPortraitTableTest() throws OnapCommandException { //NOSONAR
187 OnapCommandResult res = new OnapCommandResult();
188 res.setDebugInfo("debugInfo");
189 res.setIncludeSeparator(true);
190 res.setIncludeTitle(true);
191 res.setOutput("Output");
192 res.setPrintDirection(OnapCommandPrintDirection.PORTRAIT);
194 OnapCommandResultAttribute att = new OnapCommandResultAttribute();
195 att.setName("param");
196 att.setDescription("description");
197 att.setType(OnapCommandParameterType.STRING);
198 att.setValues(new ArrayList<String>(Arrays.asList(new String[] { "value" })));
200 List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
202 res.setRecords(list);
203 res.setScope(OnapCommandResultAttributeScope.LONG);
204 res.setType(OnapCommandResultType.TABLE);
205 String expRes = "+----------+--------+\n|property |value |\n+----------+--------+"
206 + "\n|param |value |\n+----------+--------+\n";
208 String result = res.print();
210 assertEquals(expRes, result);
214 public void commandResultPrintLandscapeJsonTestForGson() throws OnapCommandException {
215 OnapCommandResult res = new OnapCommandResult();
216 res.setDebugInfo("debugInfo");
217 res.setIncludeSeparator(true);
218 res.setIncludeTitle(true);
219 res.setOutput("Output");
220 res.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
222 OnapCommandResultAttribute att = new OnapCommandResultAttribute();
223 att.setName("param");
224 att.setDescription("description");
225 att.setType(OnapCommandParameterType.JSON);
227 new ArrayList<String>(Arrays.asList(new String[] { "{\"id\": \"0001\",\"value\": \"result\"}" })));
228 List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
230 res.setRecords(list);
231 res.setScope(OnapCommandResultAttributeScope.LONG);
232 res.setType(OnapCommandResultType.JSON);
233 String result = res.print();
234 String expRes="[{\"param\":{\"id\":\"0001\",\"value\":\"result\"}}]";
235 assertEquals(expRes,result);
240 public void printYamlTest() throws OnapCommandException {
241 OnapCommandResult res = new OnapCommandResult();
242 res.setDebugInfo("debugInfo");
243 res.setIncludeSeparator(true);
244 res.setIncludeTitle(true);
245 res.setOutput("Output");
246 res.setPrintDirection(OnapCommandPrintDirection.LANDSCAPE);
248 OnapCommandResultAttribute att = new OnapCommandResultAttribute();
249 att.setName("param");
250 att.setDescription("description");
251 att.setType(OnapCommandParameterType.YAML);
253 new ArrayList<String>(Arrays.asList(new String[] { "{\"id\": \"0001\",\"value\": \"result\"}" })));
254 List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
256 res.setRecords(list);
257 res.setScope(OnapCommandResultAttributeScope.LONG);
258 res.setType(OnapCommandResultType.YAML);
259 String result = res.print();
260 String expRes="---\n- param:\n id: \"0001\"\n value: \"result\"\n";
261 assertEquals(expRes,result);
264 new ArrayList<String>(Arrays.asList(new String[] { "{\"id\": \"0001\": \"value\": }" })));
265 list = new ArrayList<OnapCommandResultAttribute>();
267 res.setRecords(list);
268 result = res.print();
269 expRes="---\n- param: null\n";
270 assertEquals(expRes,result);