Package com.privalia.qa.utils
Class FileParserUtils
- java.lang.Object
-
- com.privalia.qa.utils.FileParserUtils
-
public class FileParserUtils extends Object
Base class for parsing text files based on XML definitions This class makes use of Utah Parser: A Java library for parsing semi-structured text files https://github.com/sonalake/utah-parser/tree/master/src/test/java/com/sonalake/utahThis is a low level class for most basic functions regarding records
- Author:
- Jose Fernandez
-
-
Constructor Summary
Constructors Constructor Description FileParserUtils()
Generic constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
elementsWhereEqual(List<Map<String,String>> records, String columnName, String value)
Returns the amount of records in which the specified column has the given valueint
elementWhereNotEqual(List<Map<String,String>> records, String columnName, String value)
Returns tue amount of records where the value of the specified column is different to the given valueList<Map<String,String>>
filterRecordThatMatches(List<Map<String,String>> initialSet, String key, String value, String condition)
Returns a list of records that matches the expected condition.Map<String,String>
getFirstRecordThatMatches(List<Map<String,String>> records, String key, String value)
Returns the first record that matches the given conditionMap<String,String>
getRecordAtPosition(List<Map<String,String>> records, int rowNumber)
Returns a single record at the specified positionString
getValueofColumnAtPosition(List<Map<String,String>> records, String columnName, int rowNumber)
Get the value of the given column at the given index (starting at 0)List<Map<String,String>>
parseFile(com.sonalake.utah.config.Config XMLDefinitionFile, Reader fileToParse)
Parses the file passed as a Reader object using the definition in the XMLDefinitionFile as aConfig
objectString
sumColumn(List<Map<String,String>> records, String columnName)
Sum al lthe values in the given colum.boolean
validateRecordValues(Map<String,String> record, String key, String expectedValue)
Validates the value of a given key in a record
-
-
-
Method Detail
-
parseFile
public List<Map<String,String>> parseFile(com.sonalake.utah.config.Config XMLDefinitionFile, Reader fileToParse) throws IOException, JAXBException
Parses the file passed as a Reader object using the definition in the XMLDefinitionFile as aConfig
object- Parameters:
XMLDefinitionFile
- File containing the rules for decodingfileToParse
- File to extract the values from- Returns:
- a list of map objects with the result
- Throws:
IOException
- the io exceptionJAXBException
- the jaxb exception
-
sumColumn
public String sumColumn(List<Map<String,String>> records, String columnName) throws ParseException
Sum al lthe values in the given colum. This functions will try to cast all values to longs with dot (".") as decimal separator. The result will be the string representation of the resulting float number- Parameters:
records
- Initial set of records to perform the operationcolumnName
- Column name to sum- Returns:
- String representation of the sum
- Throws:
ParseException
- the parse exception
-
elementsWhereEqual
public int elementsWhereEqual(List<Map<String,String>> records, String columnName, String value)
Returns the amount of records in which the specified column has the given value- Parameters:
records
- Initial set of records in which to perform the operationcolumnName
- Column name to look forvalue
- Value the column should have to be considered- Returns:
- Amount of records in which column matches the given value
-
elementWhereNotEqual
public int elementWhereNotEqual(List<Map<String,String>> records, String columnName, String value)
Returns tue amount of records where the value of the specified column is different to the given value- Parameters:
records
- Initial set of records in which to perform the operationcolumnName
- Column name to look forvalue
- Value the column should not have to be considered- Returns:
- Amount of records in which column dont matche the given value
-
getValueofColumnAtPosition
public String getValueofColumnAtPosition(List<Map<String,String>> records, String columnName, int rowNumber)
Get the value of the given column at the given index (starting at 0)- Parameters:
records
- Initial set of records in which to perform the operationcolumnName
- Column namerowNumber
- Index (row)- Returns:
- Value as String
-
getRecordAtPosition
public Map<String,String> getRecordAtPosition(List<Map<String,String>> records, int rowNumber)
Returns a single record at the specified position- Parameters:
records
- Initial set of records in which to perform the operationrowNumber
- Record number- Returns:
- A single Map object representing a record
-
validateRecordValues
public boolean validateRecordValues(Map<String,String> record, String key, String expectedValue)
Validates the value of a given key in a record- Parameters:
record
- recordkey
- Key to look for in the recordexpectedValue
- Expected value for the key- Returns:
- True if the value is the expected, false otherwise
-
getFirstRecordThatMatches
public Map<String,String> getFirstRecordThatMatches(List<Map<String,String>> records, String key, String value)
Returns the first record that matches the given condition- Parameters:
records
- Initial set of records in which to perform the operationkey
- Key to llok for in the recordvalue
- Value for the given key- Returns:
- Fisrt record that matches the expected condition
-
filterRecordThatMatches
public List<Map<String,String>> filterRecordThatMatches(List<Map<String,String>> initialSet, String key, String value, String condition)
Returns a list of records that matches the expected condition. The resulted list of records is stored internally and can be used to perform new searches on it- Parameters:
initialSet
- Records list to perform operationkey
- key to look for in each recordvalue
- Expected value to match the conditioncondition
- Condition to apply (equal, not equal, contains, does not contains, length)- Returns:
- List of records where the value for the key match the expected condition
-
-