/*
* Copyright 2020 webtau maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.testingisdocumenting.webtau.data;
import org.testingisdocumenting.webtau.data.table.TableData;
import org.testingisdocumenting.webtau.utils.CsvUtils;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.stream.Collectors.*;
import static org.testingisdocumenting.webtau.data.DataContentUtils.*;
public class DataCsv {
/**
* Use data.csv.table
to read data as {@link TableData} from CSV file.
*
* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.
* @param fileOrResourcePath relative file path, absolute file path or classpath resource path
* @return table data with CSV content
*/
public TableData table(String fileOrResourcePath) {
return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath),
(text) -> tableFromListOfMaps(CsvUtils.parse(text)));
}
/**
* Use data.csv.table
to read data as {@link TableData} from CSV file. It will convert values based on provided converter.
*
* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.
* @param fileOrResourcePath relative file path, absolute file path or classpath resource path
* @param valueConverter converter to convert values from String
* @return table data with CSV content
*/
public TableData table(String fileOrResourcePath, DataCsvValueConverter valueConverter) {
return parseCsvTextAsStep(DataPath.fromFileOrResourcePath(fileOrResourcePath),
(text) -> tableFromListOfMaps(parseAndCovertValues(valueConverter, text)));
}
/**
* Use data.csv.table
to read data as {@link TableData} from CSV file.
*
* Passed path is either relative based on working dir or absolute file path.
* @param filePath relative file path or absolute file path
* @return table data with CSV content
*/
public TableData table(Path filePath) {
return parseCsvTextAsStep(DataPath.fromFilePath(filePath),
(text) -> tableFromListOfMaps(CsvUtils.parse(text)));
}
/**
* Use data.csv.table
to read data as {@link TableData} from CSV file. It will convert values based on provided converter.
*
* Passed path is either relative based on working dir or absolute file path.
* @param filePath relative file path or absolute file path
* @param valueConverter converter to convert values from String
* @return table data with CSV content
*/
public TableData table(Path filePath, DataCsvValueConverter valueConverter) {
return parseCsvTextAsStep(DataPath.fromFilePath(filePath),
(text) -> tableFromListOfMaps(parseAndCovertValues(valueConverter, text)));
}
/**
* Use data.csv.tableAutoConverted
to read data as {@link TableData} from CSV file. Numeric values become values of Numeric type instead of String type.
*
* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.
* @param fileOrResourcePath relative file path, absolute file path or classpath resource path
* @return table data with CSV content
*/
public TableData tableAutoConverted(String fileOrResourcePath) {
return table(fileOrResourcePath, new DataCsvAutoNumberConversion());
}
/**
* Use data.csv.tableAutoConverted
to read data as {@link TableData} from CSV file. Numeric values become values of Numeric type instead of String type.
*
* Passed path is either relative based on working dir or absolute file path.
* @param filePath relative file path or absolute file path
* @return table data with CSV content
*/
public TableData tableAutoConverted(Path filePath) {
return table(filePath, new DataCsvAutoNumberConversion());
}
/**
* Use data.csv.listOfMaps
to read data as {@link java.util.List} of {@link java.util.Map} from CSV file.
*
* Passed path is either relative based on working dir or absolute file path. Or it can be a resource class path.
* @param fileOrResourcePath relative file path, absolute file path or classpath resource path
* @return list of maps
*/
public List