blob: 88fa7a472b3af7499acecfa27ae840f610466a28 [file] [log] [blame]
Ted Humphreyb56cb112020-06-12 03:17:35 -04001/*-
2 * ============LICENSE_START=======================================================
3 * ONAP CLAMP
4 * ================================================================================
5 * Copyright (C) 2019 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 */
23
24import CsvToJson from './CsvToJson'
25
26describe('Verify CsvToJson', () => {
27
28 const hdrNames= [
29 "Element Short Name",
30 "Element Name",
31 "Element Description",
32 "Element Type",
33 "Sub-Dictionary"
34 ];
35
36 const jsonKeyNames = [
37 "shortName",
38 "name",
39 "description",
40 "type",
41 "subDictionary"
42 ];
43
44 const mandatory = [ true, true, true, true, false ];
45
46 it('Test CsvToJson No Error Case, Quoted Columns', () => {
47
48 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
49 rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"';
50
51 let expectedResult = {
52 errorMessages: '',
53 jsonObjArray: [
54 {
55 description: "Type of Alert",
56 name: "Alert Type",
57 shortName: "alertType",
58 subDictionary: "",
59 type: "string"
60 }
61 ]
62 };
63
64 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
65 });
66
67 it('Test CsvToJson No Error Case, Unquoted Columns', () => {
68
69 let rawCsv = 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary\n';
70 rawCsv += 'alertType,Alert Type,Type of Alert,string,,admin,2020-06-11T13:56:14.927437Z';
71
72 let expectedResult = {
73 errorMessages: '',
74 jsonObjArray: [
75 {
76 description: "Type of Alert",
77 name: "Alert Type",
78 shortName: "alertType",
79 subDictionary: "",
80 type: "string"
81 }
82 ]
83 };
84
85 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
86 });
87
88 it('Test CsvToJson Properly Escaped Double Quote and Delimiter', () => {
89
90 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
91 rawCsv += '"alertType","Alert ""Type""","Type of Alert, Varies","string","","admin","2020-06-11T13:56:14.927437Z"';
92
93 let errorMessage = '';
94
95 let expectedResult = {
96 errorMessages: errorMessage,
97 jsonObjArray: [
98 {
99 description: "Type of Alert, Varies",
100 name: 'Alert "Type"',
101 shortName: 'alertType',
102 subDictionary: "",
103 type: "string",
104 }
105
106 ]
107 };
108
109 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
110 });
111
112
113 it('Test CsvToJson Error Header Mismatch Error Case', () => {
114
115 let rawCsv = '"Element Short Names","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
116 rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"';
117
118 let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n';
119 errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary';
120
121 let expectedResult = {
122 errorMessages: errorMessage,
123 jsonObjArray: []
124 };
125
126 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
127 });
128
129 it('Test CsvToJson Error Mismatched Double Quotes in Column', () => {
130
131 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
132 rawCsv += '"alert"Type","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"';
133
134 let errorMessage = '\nRow #2 is badly formatted at column #1. Perhaps an unescaped double quote.'
135
136 let expectedResult = {
137 errorMessages: errorMessage,
138 jsonObjArray: []
139 };
140
141 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
142 });
143
144 it('Test CsvToJson Error Illegal Whitespace', () => {
145
146 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
147 rawCsv += 'alertType , "Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"';
148
149 let errorMessage = '\nMismatched double quotes or illegal whitespace around delimiter at row #2 near column #2';
150
151 let expectedResult = {
152 errorMessages: errorMessage,
153 jsonObjArray: []
154 };
155
156 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
157 });
158
159 it('Test CsvToJson Error Too Few Data Columns', () => {
160
161 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
162 rawCsv += '"alertType","Alert Type","Type of Alert"';
163
164 let errorMessage = '\nNot enough columns (5) at row #2';
165
166 let expectedResult = {
167 errorMessages: errorMessage,
168 jsonObjArray: []
169 };
170
171 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
172 });
173
174 it('Test CsvToJson Error Wrong Header Column Order', () => {
175
176 let rawCsv = '"Element Name","Element Short Name","Element Description","Element Type","Sub-Dictionary"\n';
177 rawCsv += '"alertType","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"';
178
179 let errorMessage = 'Row 1 header key at column #1 is a mismatch. Expected row header must contain at least:\n';
180 errorMessage += 'Element Short Name,Element Name,Element Description,Element Type,Sub-Dictionary';
181
182 let expectedResult = {
183 errorMessages: errorMessage,
184 jsonObjArray: []
185 };
186
187 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
188 });
189
190 it('Test CsvToJson Error Not Enough Rows', () => {
191
192 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
193
194 let errorMessage = '\nNot enough row data found in import file. Need at least a header row and one row of data';
195
196 let expectedResult = {
197 errorMessages: errorMessage,
198 jsonObjArray: []
199 };
200
201 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
202 });
203
204 it('Test CsvToJson Error Mandatory Field Is Empty', () => {
205
206 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
207 rawCsv += '"","Alert Type","Type of Alert","string","","admin","2020-06-11T13:56:14.927437Z"';
208
209 let expectedResult = {
210 errorMessages: '\nElement Short Name at row #2 is empty but requires a value.',
211 jsonObjArray: []
212 };
213
214 expect(CsvToJson(rawCsv, ',', '|', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
215 });
216
217 it('Test CsvToJson Error Mismatched Double Quotes At End', () => {
218
219 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
220 rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z';
221
222 let expectedResult = {
223 errorMessages: '\nMismatched double quotes at row #2',
224 jsonObjArray: []
225 };
226
227 expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
228 });
229
230 it('Test CsvToJson Error Mismatched Mandatory Array Parameters', () => {
231
232 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
233 rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z';
234
235 let expectedResult = {
236 errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays parameters are not the same length',
237 jsonObjArray: []
238 };
239
240 expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, [ true ])).toEqual(expectedResult);
241 });
242
243 it('Test CsvToJson Error Empty Mandatory Array Parameters', () => {
244
245 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
246 rawCsv += '"alertType","Alert Type","Alert Type Description","string","admin","2020-06-11T13:56:14.927437Z';
247
248 let expectedResult = {
249 errorMessages: 'interanl error: csvHeaderNames, jsonKeyNames, and mandatory arrays have no entries',
250 jsonObjArray: []
251 };
252
253 expect(CsvToJson(rawCsv, ',', '||', [], [], [])).toEqual(expectedResult);
254 });
255
256 it('Test CsvToJson Error Illegal Data Contains Internal Delimiter', () => {
257
258 let rawCsv = '"Element Short Name","Element Name","Element Description","Element Type","Sub-Dictionary"\n';
259 rawCsv += '"alertType","Alert Type","Alert Type||Description","string","admin","2020-06-11T13:56:14.927437Z';
260
261 let expectedResult = {
262 errorMessages: '\nRow #1 contains illegal sequence of characters (||)',
263 jsonObjArray: []
264 };
265
266 expect(CsvToJson(rawCsv, ',', '||', hdrNames, jsonKeyNames, mandatory)).toEqual(expectedResult);
267 });
268})