Merge "Sending workflow data from UI to SO"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / CsvServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.services;
22
23 import org.apache.commons.io.IOUtils;
24 import org.json.JSONObject;
25 import org.skyscreamer.jsonassert.JSONAssert;
26 import org.skyscreamer.jsonassert.JSONCompareMode;
27 import org.testng.Assert;
28 import org.testng.annotations.DataProvider;
29 import org.testng.annotations.Test;
30
31 import javax.ws.rs.BadRequestException;
32 import java.io.IOException;
33 import java.lang.reflect.Method;
34 import java.net.URL;
35 import java.net.URLDecoder;
36 import java.util.List;
37
38 public class CsvServiceTest {
39
40     private CsvServiceImpl csvService = new CsvServiceImpl();
41     private final static String CSV_FOLDER = "csv_files/{CSV_FILE}";
42     private final static String VALID_CSV = "csv_to_json.csv";
43     private final static String ONE_LINE_CSV = "one_line.csv";
44     private final static String EMPTY_CSV = "empty_file.csv";
45     private final static String MISSING_CONTENT_CSV = "missing_content.csv";
46     private final static String MISSING_VALUES_CSV = "missing_values.csv";
47
48
49
50     @Test
51     public void parseValidCsv() throws IllegalAccessException, IOException, InstantiationException {
52         String expectedJson = getExpectation("vnfConfigUpdatePayload.json");
53         readAndParse(VALID_CSV,16,expectedJson);
54
55     }
56
57     private String getExpectation(String modelFileName) throws IOException {
58         // load expected result
59         final URL resource = this.getClass().getResource("/" + modelFileName);
60         String expected = IOUtils.toString(resource, "UTF-8");
61         return expected;
62     }
63
64     @Test
65     public void parseOneLineCsv() throws IllegalAccessException, IOException, InstantiationException {
66         String expectedJson ="{\"payload\":{\"request-parameters\":{\"vnf-name\":\"ibcx0099v\"}}}";
67         readAndParse(ONE_LINE_CSV,1,expectedJson);
68     }
69
70     @DataProvider
71     public static Object[][] invalidFiles(Method test) {
72         return new Object[][]{
73                 {MISSING_CONTENT_CSV}, {MISSING_VALUES_CSV}
74         };
75     }
76
77     @Test(dataProvider = "invalidFiles", expectedExceptions = {BadRequestException.class}, expectedExceptionsMessageRegExp = "Invalid csv file")
78     public void parseMissingContentCsv(String invalidFile) throws IllegalAccessException, IOException, InstantiationException {
79         readAndParse(invalidFile, 2, null);
80     }
81
82
83     @Test
84     public void parseEmptyCsv() throws IllegalAccessException, IOException, InstantiationException {
85         String expectedJson ="{}";
86         readAndParse(EMPTY_CSV,0,expectedJson);
87     }
88
89     private void readAndParse(String fileName, int expectedNumRows, String expectedJson) throws IllegalAccessException, IOException, InstantiationException {
90         final URL resource = CsvServiceTest.class.getClassLoader().getResource(CSV_FOLDER.replaceFirst("\\{CSV_FILE\\}", fileName));
91         Assert.assertNotNull(resource, "The csv file was not found");
92         //using URLDecoder.decode to convert special characters from %XX to real character
93         //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
94         List<String[]> content = csvService.readCsv(URLDecoder.decode(resource.getPath(), "UTF-8"));
95         Assert.assertEquals(content.size(), expectedNumRows, "The number of non-empty lines in file is wrong");
96         JSONObject json = csvService.convertCsvToJson (content);
97         JSONAssert.assertEquals(expectedJson, json, JSONCompareMode.STRICT);
98
99     }
100
101
102 }