Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / CsvServiceTest.java
1 package org.onap.vid.services;
2
3 import org.apache.commons.io.IOUtils;
4 import org.json.JSONObject;
5 import org.skyscreamer.jsonassert.JSONAssert;
6 import org.skyscreamer.jsonassert.JSONCompareMode;
7 import org.testng.Assert;
8 import org.testng.annotations.DataProvider;
9 import org.testng.annotations.Test;
10
11 import javax.ws.rs.BadRequestException;
12 import java.io.IOException;
13 import java.lang.reflect.Method;
14 import java.net.URL;
15 import java.net.URLDecoder;
16 import java.util.List;
17
18 public class CsvServiceTest {
19
20     private CsvServiceImpl csvService = new CsvServiceImpl();
21     private final static String CSV_FOLDER = "csv_files/{CSV_FILE}";
22     private final static String VALID_CSV = "csv_to_json.csv";
23     private final static String ONE_LINE_CSV = "one_line.csv";
24     private final static String EMPTY_CSV = "empty_file.csv";
25     private final static String MISSING_CONTENT_CSV = "missing_content.csv";
26     private final static String MISSING_VALUES_CSV = "missing_values.csv";
27
28
29
30     @Test
31     public void parseValidCsv() throws IllegalAccessException, IOException, InstantiationException {
32         String expectedJson = getExpectation("vnfConfigUpdatePayload.json");
33         readAndParse(VALID_CSV,16,expectedJson);
34
35     }
36
37     private String getExpectation(String modelFileName) throws IOException {
38         // load expected result
39         final URL resource = this.getClass().getResource("/" + modelFileName);
40         String expected = IOUtils.toString(resource, "UTF-8");
41         return expected;
42     }
43
44     @Test
45     public void parseOneLineCsv() throws IllegalAccessException, IOException, InstantiationException {
46         String expectedJson ="{\"payload\":{\"request-parameters\":{\"vnf-name\":\"ibcx0099v\"}}}";
47         readAndParse(ONE_LINE_CSV,1,expectedJson);
48     }
49
50     @DataProvider
51     public static Object[][] invalidFiles(Method test) {
52         return new Object[][]{
53                 {MISSING_CONTENT_CSV}, {MISSING_VALUES_CSV}
54         };
55     }
56
57     @Test(dataProvider = "invalidFiles", expectedExceptions = {BadRequestException.class}, expectedExceptionsMessageRegExp = "Invalid csv file")
58     public void parseMissingContentCsv(String invalidFile) throws IllegalAccessException, IOException, InstantiationException {
59         readAndParse(invalidFile, 2, null);
60     }
61
62
63     @Test
64     public void parseEmptyCsv() throws IllegalAccessException, IOException, InstantiationException {
65         String expectedJson ="{}";
66         readAndParse(EMPTY_CSV,0,expectedJson);
67     }
68
69     private void readAndParse(String fileName, int expectedNumRows, String expectedJson) throws IllegalAccessException, IOException, InstantiationException {
70         final URL resource = CsvServiceTest.class.getClassLoader().getResource(CSV_FOLDER.replaceFirst("\\{CSV_FILE\\}", fileName));
71         Assert.assertNotNull(resource, "The csv file was not found");
72         //using URLDecoder.decode to convert special characters from %XX to real character
73         //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
74         List<String[]> content = csvService.readCsv(URLDecoder.decode(resource.getPath(), "UTF-8"));
75         Assert.assertEquals(content.size(), expectedNumRows, "The number of non-empty lines in file is wrong");
76         JSONObject json = csvService.convertCsvToJson (content);
77         JSONAssert.assertEquals(expectedJson, json, JSONCompareMode.STRICT);
78
79     }
80
81
82 }