Skip to content

File parsing steps

jose.fernandez edited this page Apr 11, 2021 · 5 revisions

Check these examples:

For a complete list of steps definitions related to files manipulation, please check the corresponding class in the javadoc here


@Given("^I parse the file located at '(.+?)' using the template defined in '(.+?)'$")

Parse the given file according to the rules described in the XML configuration file. The operation returns a list of 'records' that is stored internally for further operations

Text based files are parsed using the third party library 'Utah parser' (more information about the library here). So, for parsing and decoding files, two basic elements are needed: First, the file that contains the text we would like to analize, and Second, and XML definition file that contains the rules for parsing and decoding the first file. This XML file contains a series of tags that define what elements of the first file we want to retrieve and in what format.

Example

We would like to extract all the products from a WARTM file and make operations on then. For that, we will have to define a XML definition file with name "WARTMConfig.xml" such as the following:

<!--This config file was created to extract only the products lines in a WARTM file.-->

<!--Header line (starting by FWARTM) is ignored-->

<config>

<!-- One record per line -->

<delim  per-line="true"  />

<!--Everything before this is considered hedaer-->

<header-delim><![CDATA[(^FWARTM)]]></header-delim>

<values>

<!--Definition of field in WARTM file based on position and length-->

<value  id="REC_TYPE"><![CDATA[((?<=^.{0})(.{0}).)]]></value>

<value  id="CAMP_ID"><![CDATA[((?<=^.{1})(.{19}).)]]></value>

<value  id="CAMP_NAME"><![CDATA[((?<=^.{21})(.{19}).)]]></value>

<value  id="LBRAND_ID"><![CDATA[((?<=^.{41})(.{19}).)]]></value>

<value  id="LBRAND_NAME"><![CDATA[((?<=^.{61})(.{19}).)]]></value>

<value  id="ART_ID"><![CDATA[((?<=^.{81})(.{19}).)]]></value>

<value  id="REF_LOG"><![CDATA[((?<=^.{101})(.{19}).)]]></value>

<value  id="REF_COM"><![CDATA[((?<=^.{121})(.{99}).)]]></value>

<value  id="EAN_CODE"><![CDATA[((?<=^.{221})(.{19}).)]]></value>

<value  id="ART_NAME"><![CDATA[((?<=^.{241})(.{69}).)]]></value>

<value  id="ART_SIZE"><![CDATA[((?<=^.{311})(.{9}).)]]></value>

<value  id="REF_LOG_B"><![CDATA[((?<=^.{321})(.{69}).)]]></value>

<value  id="ART_TYPE"><![CDATA[((?<=^.{391})(.{9}).)]]></value>

<value  id="SEASON"><![CDATA[((?<=^.{401})(.{19}).)]]></value>

<value  id="UNIT_COST"><![CDATA[((?<=^.{421})(.{9}).)]]></value>

<value  id="TAX_RATE"><![CDATA[((?<=^.{431})(.{4}).)]]></value>

<value  id="DIM_X"><![CDATA[((?<=^.{436})(.{9}).)]]></value>

<value  id="DIM_Y"><![CDATA[((?<=^.{446})(.{9}).)]]></value>

<value  id="DIM_Z"><![CDATA[((?<=^.{456})(.{9}).)]]></value>

<value  id="VOLUME"><![CDATA[((?<=^.{466})(.{13}).)]]></value>

<value  id="WEIGHT"><![CDATA[((?<=^.{480})(.{9}).)]]></value>

<value  id="ABC"><![CDATA[((?<=^.{490})(.{0}).)]]></value>

<value  id="ART_CLASS"><![CDATA[((?<=^.{491})(.{9}).)]]></value>

<value  id="CAMPAIGN_SHARED"><![CDATA[((?<=^.{501})(.{9}).)]]></value>

<value  id="UNIT_CONS"><![CDATA[((?<=^.{511})(.{2}).)]]></value>

<value  id="UNIT_SERV"><![CDATA[((?<=^.{514})(.{2}).)]]></value>

<value  id="NUM_CONS"><![CDATA[((?<=^.{517})(.{5}).)]]></value>

<value  id="ALTER_REF"><![CDATA[((?<=^.{523})(.{19}).)]]></value>

<value  id="ALTER_ORDER"><![CDATA[((?<=^.{543})(.{1}).)]]></value>

<value  id="PACK_CODE"><![CDATA[((?<=^.{545})(.{19}).)]]></value>

</values>

</config>

Using this file, we can now parse and decode a WARTM file like this:

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'

This XML configuration file contains the specific rules for extracting the products from a WARTM file, so, is not suitable for any other type of file. Other types of files would require their specific XML config file with its own set of rules

@Then("^the result contains( at least)? '(\d+)' records$")

Verifies that the stored result from the last operation contains the specified amount of records, or if the amount of records is greater that or equal

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
Then the result contains '20' records
Then the result contains at least '15' records

@And("^the total of the column '(.+?)' is '(.+?)'$")

Calculates the sum of the given column. The column must contain numeric values

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
And the total of the column 'DIM_X' is '20.0'

@And("^there are '(\d+)' records with column '(.+?)' equal to '(.+?)'$")

Calculates the ammount of records in the last operation where the column matches the expected value

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
And there are '5' records with column 'ART_NAME' equal to 'SACO SILLA-SACO SILLA'

@And("^the record at position '(\d+)' at column '(.+?)' has the value '(.+?)'$")

Verifies that for the record located at the given position, the column contains the expected value

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
Then the result contains '20' records
And the record at position '1' at column 'LBRAND_NAME' has the value 'POLAR'
And the record at position '18' at column 'ART_NAME' has the value 'SILLAS DE AUTO-PROTECTOR'

@And("^I get the first record with column '(.+?)' equal to '(.+?)'$")

Returns the first record in the set in which the column has the given value. The record can be later used in other steps such as @And("^the selected record matches the following cases:$")

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
Then the result contains '20' records
And I get the first record with column 'ART_NAME' equal to 'SILLAS DE AUTO-PROTECTOR FIX'

@Then("^I get the record at position '(\d+)'$")

Returns the record at the specified position in the set. The record can be later used in other steps such as @And("^the selected record matches the following cases:$")

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
Then the result contains '20' records
Then I get the record at position '1'

@And("^the selected record matches the following cases:$")

Verifies if the selected record matches the given cases by the datatable.

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
Then the result contains '20' records
And I get the first record with column 'ART_NAME' equal to 'SILLAS DE AUTO-PROTECTOR FIX'
And the selected record matches the following cases:
| LBRAND_NAME | equal | POLAR |
| ART_NAME | contains | SILLA |
| ART_NAME | does not contain | SILLASS |

@And("^I select all records that match the following cases:$")

From the previous operation, filter all the records from the set that match the conditions given in the datatable. The resulting set will be stored internally and will be available to perform further filter operations

Given I parse the file located at 'schemas/WARTM.txt' using the template defined in 'schemas/WARTMConfig.xml'
Then the result contains '20' records
And I select all records that match the following cases:
| LBRAND_NAME | equal | POLAR |
| ART_NAME | contains | AUTO |
Then the result contains '12' records
And I select all records that match the following cases:
| LBRAND_NAME | equal | POLAR |
| ART_NAME | contains | PROTECTOR |
Then the result contains '3' records
And I get the first record with column 'REF_LOG_B' equal to '11416837-'
And the selected record matches the following cases:
| ART_ID | contains | PR171597761 |
Clone this wiki locally