printyaml method fix
[cli.git] / framework / src / test / java / org / onap / cli / fw / output / OnapCommandResultTest.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.cli.fw.output;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
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;
30
31 import com.google.gson.Gson;
32 import com.google.gson.GsonBuilder;
33
34 public class OnapCommandResultTest {
35
36     @Test
37     @Ignore
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);
48         res.setDebug(true);
49
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()));
55
56         String help = res.print();
57     }
58
59     @Test
60     @Ignore
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);
68
69         OnapCommandResultAttribute att = new OnapCommandResultAttribute();
70         att.setName("param");
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>();
75         list.add(att);
76         res.setRecords(list);
77         res.setScope(OnapCommandResultAttributeScope.LONG);
78         res.setType(OnapCommandResultType.TABLE);
79         res.getRecordsMap();
80         String expRes = "+--------+\n|param   |\n+--------+\n|value   |\n+--------+\n";
81         String result = res.print();
82
83         assertEquals(expRes, result);
84
85     }
86
87     @Test
88     @Ignore
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);
96
97         OnapCommandResultAttribute att = new OnapCommandResultAttribute();
98         att.setName("param");
99         att.setDescription("description");
100         att.setType(OnapCommandParameterType.JSON);
101         att.setValues(
102                 new ArrayList<String>(Arrays.asList(new String[] { "{\"id\": \"0001\",\"value\": \"result\"}" })));
103         List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
104         list.add(att);
105         res.setRecords(list);
106         res.setScope(OnapCommandResultAttributeScope.LONG);
107         res.setType(OnapCommandResultType.JSON);
108
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
113         // |\n+--------+\n";
114         // assertEquals(expRes,result);
115
116     }
117
118     @Test
119     @Ignore
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);
127
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>();
134         list.add(att);
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" })));
140
141         list.add(a1);
142         res.setRecords(list);
143         res.setScope(OnapCommandResultAttributeScope.LONG);
144         res.setType(OnapCommandResultType.CSV);
145
146         String expRes = "param,param1\r\n";
147         String result = res.print();
148         assertEquals(expRes, result);
149
150     }
151
152     @Test
153     @Ignore
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);
161
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>();
168         list.add(att);
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" })));
174
175         list.add(a1);
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);
182     }
183
184     @Test
185     @Ignore
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);
193
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" })));
199
200         List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
201         list.add(att);
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";
207
208         String result = res.print();
209
210         assertEquals(expRes, result);
211
212     }
213     @Test
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);
221
222         OnapCommandResultAttribute att = new OnapCommandResultAttribute();
223         att.setName("param");
224         att.setDescription("description");
225         att.setType(OnapCommandParameterType.JSON);
226         att.setValues(
227                 new ArrayList<String>(Arrays.asList(new String[] { "{\"id\": \"0001\",\"value\": \"result\"}" })));
228         List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
229         list.add(att);
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);
236
237     }
238
239     @Test
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);
247
248         OnapCommandResultAttribute att = new OnapCommandResultAttribute();
249         att.setName("param");
250         att.setDescription("description");
251         att.setType(OnapCommandParameterType.YAML);
252         att.setValues(
253                 new ArrayList<String>(Arrays.asList(new String[] {  "{\"id\": \"0001\",\"value\": \"result\"}"  })));
254         List<OnapCommandResultAttribute> list = new ArrayList<OnapCommandResultAttribute>();
255         list.add(att);
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);
262
263         att.setValues(
264                 new ArrayList<String>(Arrays.asList(new String[] {  "{\"id\": \"0001\": \"value\": }"  })));
265         list = new ArrayList<OnapCommandResultAttribute>();
266         list.add(att);
267         res.setRecords(list);
268         result = res.print();
269         expRes="---\n- param: null\n";
270         assertEquals(expRes,result);
271
272     }
273 }