1 package org.onap.vid.services;
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;
11 import javax.ws.rs.BadRequestException;
12 import java.io.IOException;
13 import java.lang.reflect.Method;
15 import java.net.URLDecoder;
16 import java.util.List;
18 public class CsvServiceTest {
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";
31 public void parseValidCsv() throws IllegalAccessException, IOException, InstantiationException {
32 String expectedJson = getExpectation("vnfConfigUpdatePayload.json");
33 readAndParse(VALID_CSV,16,expectedJson);
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");
45 public void parseOneLineCsv() throws IllegalAccessException, IOException, InstantiationException {
46 String expectedJson ="{\"payload\":{\"request-parameters\":{\"vnf-name\":\"ibcx0099v\"}}}";
47 readAndParse(ONE_LINE_CSV,1,expectedJson);
51 public static Object[][] invalidFiles(Method test) {
52 return new Object[][]{
53 {MISSING_CONTENT_CSV}, {MISSING_VALUES_CSV}
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);
64 public void parseEmptyCsv() throws IllegalAccessException, IOException, InstantiationException {
65 String expectedJson ="{}";
66 readAndParse(EMPTY_CSV,0,expectedJson);
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);