package com.demo.qa.utils;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Class that read data from XSSF sheet * */public class DataReader { protected static final Logger logger = LoggerFactory.getLogger(DataReader.class); private HashMap map = new HashMap (); private Boolean byColumnName = false; private Boolean byRowKey = false; private List headers = new ArrayList (); private Integer size = 0; public DataReader() { } /** * Primary constructor. Uses Apache POI XSSF to pull data from given excel workbook sheet. Data is stored in a * structure depending on the options from other parameters. * * @param sheet Given excel sheet. * @param has_headers Boolean used to specify if the data has a header or not. The headers will be used as field keys. * @param has_key_column Boolean used to specify if the data has a column that should be used for record keys. * @param key_column Integer used to specify the key column for record keys. */ public DataReader(XSSFSheet sheet, Boolean has_headers, Boolean has_key_column, Integer key_column) { XSSFRow myRow = null; HashMap myList; size = 0; this.byColumnName = has_headers; this.byRowKey = has_key_column; try { if(byColumnName) { myRow = sheet.getRow(0); for(Cell cell: myRow) { headers.add(cell.getStringCellValue()); } size = 1; } for(; (myRow = sheet.getRow(size)) != null; size++ ) { myList = new HashMap (); if(byColumnName) { for(int col = 0; col < headers.size(); col++ ) { if(col < myRow.getLastCellNum()) { myList.put(headers.get(col), getSheetCellValue(myRow.getCell(col))); // myRow.getCell(col).getStringCellValue()); } else { myList.put(headers.get(col), ""); } } } else { for(int col = 0; col < myRow.getLastCellNum(); col++ ) { myList.put(Integer.toString(col), getSheetCellValue(myRow.getCell(col))); } } if(byRowKey) { if(myList.size() == 2 && key_column == 0) { map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(1))); } else if(myList.size() == 2 && key_column == 1) { map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(0))); } else { map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList)); } } else { map.put(Integer.toString(size), new RecordHandler(myList)); } } } catch (Exception e) { logger.error("Exception while loading data from Excel sheet:"+e.getMessage()); } } /** * Utility method used for getting an excel cell value. Cell's type is switched to String before accessing. * * @param cell Given excel cell. */ private String getSheetCellValue(XSSFCell cell) { String value = ""; try { cell.setCellType(Cell.CELL_TYPE_STRING); value = cell.getStringCellValue(); } catch(NullPointerException npe) { return ""; } return value; } /** * Returns entire HashMap containing this class's data. * * @return HashMap , map of ID-Record data. */ public HashMap get_map() { return map; } /** * Gets an entire record. * * @param record String key value for record to be returned. * @return HashMap of key-value pairs representing the specified record. */ public RecordHandler get_record(String record) { RecordHandler result = new RecordHandler(); if(map.containsKey(record)) { result = map.get(record); } return result; }}
HTTPReqGen
package com.demo.qa.utils;import static com.jayway.restassured.RestAssured.given;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import org.apache.commons.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.jayway.restassured.response.Response;import com.jayway.restassured.specification.RequestSpecification;/** * Wrapper for RestAssured. Uses an HTTP request template and a single record housed in a RecordHandler object to * generate and perform an HTTP requests. * */public class HTTPReqGen { protected static final Logger logger = LoggerFactory.getLogger(HTTPReqGen.class); private RequestSpecification reqSpec; private String call_host = ""; private String call_suffix = ""; private String call_string = ""; private String call_type = ""; private String body = ""; private Map headers = new HashMap (); private HashMap cookie_list = new HashMap (); public Map getHeaders() { return headers; } public String getCallString() { return call_string; } /** * Constructor. Initializes the RequestSpecification (relaxedHTTPSValidation avoids certificate errors). * */ public HTTPReqGen() { reqSpec = given().relaxedHTTPSValidation(); } public HTTPReqGen(String proxy) { reqSpec = given().relaxedHTTPSValidation().proxy(proxy); } /** * Pulls HashMap from given RecordHandler and calls primary generate_request method with it. * * @param template String, should contain the full template. * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template. * @return this Reference to this class, primarily to allow request generation and performance in one line. * @throws Exception */ public HTTPReqGen generate_request(String template, RecordHandler record) throws Exception { return generate_request(template, (HashMap ) record.get_map()); } /** * Generates request data, using input record to fill in the template and then parse out relevant data. To fill in the * template, identifies tags surrounded by << and >> and uses the text from the corresponding fields in the * RecordHandler to replace them. The replacement is recursive, so tags may also exist in the fields of the * RecordHandler so long as they are also represented by the RecordHandler and do not form an endless loop. * After filling in the template, parses the resulting string in preparation for performing the HTTP request. Expects the * the string to be in the following format: * * < > < > * Host: < > * < >:< > * ... * < >: < > * * < > * * < > must be GET, PUT, POST, or DELETE. < > must be a string with no spaces. It is appended to * < > to form the complete call string. After a single blank line is encountered, the rest of the file * is used as the body of text for PUT and POST calls. This function also expects the Record Handler to include a field * named "VPID" containing a unique record identifier for debugging purposes. * * @param template String, should contain the full template. * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template. * @return this Reference to this class, primarily to allow request generation and performance in one line. * @throws Exception */ public HTTPReqGen generate_request(String template, HashMap record) throws Exception { String filled_template = ""; Boolean found_replacement = true; headers.clear(); try { // Splits template into tokens, separating out the replacement strings // like < > String[] tokens = tokenize_template(template); // Repeatedly perform replacements with data from record until no // replacements are found // If a replacement's result is an empty string, it will not throw an // error (but will throw one if there is no column for that result) while(found_replacement) { found_replacement = false; filled_template = ""; for(String item: tokens) { if(item.startsWith("<<") && item.endsWith(">>")) { found_replacement = true; item = item.substring(2, item.length() - 2); if( !record.containsKey(item)) { logger.error("Template contained replacement string whose value did not exist in input record:[" + item + "]"); } item = record.get(item); } filled_template += item; } tokens = tokenize_template(filled_template); } } catch (Exception e) { logger.error("Problem performing replacements from template: ", e); } try { // Feed filled template into BufferedReader so that we can read it line // by line. InputStream stream = IOUtils.toInputStream(filled_template, "UTF-8"); BufferedReader in = new BufferedReader(new InputStreamReader(stream)); String line = ""; String[] line_tokens; // First line should always be call type followed by call suffix line = in.readLine(); line_tokens = line.split(" "); call_type = line_tokens[0]; call_suffix = line_tokens[1]; // Second line should contain the host as it's second token line = in.readLine(); line_tokens = line.split(" "); call_host = line_tokens[1]; // Full call string for RestAssured will be concatenation of call // host and call suffix call_string = call_host + call_suffix; // Remaining lines will contain headers, until the read line is // empty line = in.readLine(); while(line != null && !line.equals("")) { String lineP1 = line.substring(0, line.indexOf(":")).trim(); String lineP2 = line.substring(line.indexOf(" "), line.length()).trim(); headers.put(lineP1, lineP2); line = in.readLine(); } // If read line is empty, but next line(s) have data, create body // from them if(line != null && line.equals("")) { body = ""; while( (line = in.readLine()) != null && !line.equals("")) { body += line; } } } catch(Exception e) { logger.error("Problem setting request values from template: ", e); } return this; } /** * Performs the request using the stored request data and then returns the response. * * @return response Response, will contain entire response (response string and status code). */ public Response perform_request() throws Exception { Response response = null; try { for(Map.Entry entry: headers.entrySet()) { reqSpec.header(entry.getKey(), entry.getValue()); } for(Map.Entry entry: cookie_list.entrySet()) { reqSpec.cookie(entry.getKey(), entry.getValue()); } switch(call_type) { case "GET": { response = reqSpec.get(call_string); break; } case "POST": { response = reqSpec.body(body).post(call_string); break; } case "PUT": { response = reqSpec.body(body).put(call_string); break; } case "DELETE": { response = reqSpec.delete(call_string); break; } default: { logger.error("Unknown call type: [" + call_type + "]"); } } } catch (Exception e) { logger.error("Problem performing request: ", e); } return response; } /** * Splits a template string into tokens, separating out tokens that look like "< >" * * @param template String, the template to be tokenized. * @return list String[], contains the tokens from the template. */ private String[] tokenize_template(String template) { return template.split("(?=[<]{2})|(?<=[>]{2})"); }}
RecordHandler
package com.demo.qa.utils;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class RecordHandler { private enum RecordType { VALUE, NAMED_MAP, INDEXED_LIST } private String single_value = ""; private HashMap named_value_map = new HashMap (); private List indexed_value_list = new ArrayList (); private RecordType myType; public RecordHandler() { this(""); } public RecordHandler(String value) { this.myType = RecordType.VALUE; this.single_value = value; } public RecordHandler(HashMap map) { this.myType = RecordType.NAMED_MAP; this.named_value_map = map; } public RecordHandler(List list) { this.myType = RecordType.INDEXED_LIST; this.indexed_value_list = list; } public HashMap get_map() { return named_value_map; } public int size() { int result = 0; if(myType.equals(RecordType.VALUE)) { result = 1; } else if(myType.equals(RecordType.NAMED_MAP)) { result = named_value_map.size(); } else if(myType.equals(RecordType.INDEXED_LIST)) { result = indexed_value_list.size(); } return result; } public String get() { String result = ""; if(myType.equals(RecordType.VALUE)) result = single_value; else { System.out.println("Called get() on wrong type:" + myType.toString()); } return result; } public String get(String key) { String result = ""; if(myType.equals(RecordType.NAMED_MAP)) result = named_value_map.get(key); return result; } public String get(Integer index) { String result = ""; if(myType.equals(RecordType.INDEXED_LIST)) result = indexed_value_list.get(index); return result; } public Boolean set(String value) { Boolean result = false; if(myType.equals(RecordType.VALUE)) { this.single_value = value; result = true; } else if(myType.equals(RecordType.INDEXED_LIST)) { this.indexed_value_list.add(value); result = true; } return result; } public Boolean set(String key, String value) { Boolean result = false; if(myType.equals(RecordType.NAMED_MAP)) { this.named_value_map.put(key, value); result = true; } return result; } public Boolean set(Integer index, String value) { Boolean result = false; if(myType.equals(RecordType.INDEXED_LIST)) { if(this.indexed_value_list.size() > index) this.indexed_value_list.set(index, value); result = true; } return result; } public Boolean has(String value) { Boolean result = false; if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) { result = true; } else if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) { result = true; } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) { result = true; } return result; } public Boolean remove(String value) { Boolean result = false; if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) { this.single_value = ""; result = true; } if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) { this.named_value_map.remove(value); result = true; } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) { this.indexed_value_list.remove(value); result = true; } return result; } public Boolean remove(Integer index) { Boolean result = false; if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(index)) { this.indexed_value_list.remove(index); result = true; } return result; }}
package com.demo.qa.utils;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Class that read data from XSSF sheet * */public class DataReader { protected static final Logger logger = LoggerFactory.getLogger(DataReader.class); private HashMap map = new HashMap (); private Boolean byColumnName = false; private Boolean byRowKey = false; private List headers = new ArrayList (); private Integer size = 0; public DataReader() { } /** * Primary constructor. Uses Apache POI XSSF to pull data from given excel workbook sheet. Data is stored in a * structure depending on the options from other parameters. * * @param sheet Given excel sheet. * @param has_headers Boolean used to specify if the data has a header or not. The headers will be used as field keys. * @param has_key_column Boolean used to specify if the data has a column that should be used for record keys. * @param key_column Integer used to specify the key column for record keys. */ public DataReader(XSSFSheet sheet, Boolean has_headers, Boolean has_key_column, Integer key_column) { XSSFRow myRow = null; HashMap myList; size = 0; this.byColumnName = has_headers; this.byRowKey = has_key_column; try { if(byColumnName) { myRow = sheet.getRow(0); for(Cell cell: myRow) { headers.add(cell.getStringCellValue()); } size = 1; } for(; (myRow = sheet.getRow(size)) != null; size++ ) { myList = new HashMap (); if(byColumnName) { for(int col = 0; col < headers.size(); col++ ) { if(col < myRow.getLastCellNum()) { myList.put(headers.get(col), getSheetCellValue(myRow.getCell(col))); // myRow.getCell(col).getStringCellValue()); } else { myList.put(headers.get(col), ""); } } } else { for(int col = 0; col < myRow.getLastCellNum(); col++ ) { myList.put(Integer.toString(col), getSheetCellValue(myRow.getCell(col))); } } if(byRowKey) { if(myList.size() == 2 && key_column == 0) { map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(1))); } else if(myList.size() == 2 && key_column == 1) { map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList.get(0))); } else { map.put(getSheetCellValue(myRow.getCell(key_column)), new RecordHandler(myList)); } } else { map.put(Integer.toString(size), new RecordHandler(myList)); } } } catch (Exception e) { logger.error("Exception while loading data from Excel sheet:"+e.getMessage()); } } /** * Utility method used for getting an excel cell value. Cell's type is switched to String before accessing. * * @param cell Given excel cell. */ private String getSheetCellValue(XSSFCell cell) { String value = ""; try { cell.setCellType(Cell.CELL_TYPE_STRING); value = cell.getStringCellValue(); } catch(NullPointerException npe) { return ""; } return value; } /** * Returns entire HashMap containing this class's data. * * @return HashMap , map of ID-Record data. */ public HashMap get_map() { return map; } /** * Gets an entire record. * * @param record String key value for record to be returned. * @return HashMap of key-value pairs representing the specified record. */ public RecordHandler get_record(String record) { RecordHandler result = new RecordHandler(); if(map.containsKey(record)) { result = map.get(record); } return result; }}
HTTPReqGen
package com.demo.qa.utils;import static com.jayway.restassured.RestAssured.given;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import org.apache.commons.io.IOUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.jayway.restassured.response.Response;import com.jayway.restassured.specification.RequestSpecification;/** * Wrapper for RestAssured. Uses an HTTP request template and a single record housed in a RecordHandler object to * generate and perform an HTTP requests. * */public class HTTPReqGen { protected static final Logger logger = LoggerFactory.getLogger(HTTPReqGen.class); private RequestSpecification reqSpec; private String call_host = ""; private String call_suffix = ""; private String call_string = ""; private String call_type = ""; private String body = ""; private Map headers = new HashMap (); private HashMap cookie_list = new HashMap (); public Map getHeaders() { return headers; } public String getCallString() { return call_string; } /** * Constructor. Initializes the RequestSpecification (relaxedHTTPSValidation avoids certificate errors). * */ public HTTPReqGen() { reqSpec = given().relaxedHTTPSValidation(); } public HTTPReqGen(String proxy) { reqSpec = given().relaxedHTTPSValidation().proxy(proxy); } /** * Pulls HashMap from given RecordHandler and calls primary generate_request method with it. * * @param template String, should contain the full template. * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template. * @return this Reference to this class, primarily to allow request generation and performance in one line. * @throws Exception */ public HTTPReqGen generate_request(String template, RecordHandler record) throws Exception { return generate_request(template, (HashMap ) record.get_map()); } /** * Generates request data, using input record to fill in the template and then parse out relevant data. To fill in the * template, identifies tags surrounded by << and >> and uses the text from the corresponding fields in the * RecordHandler to replace them. The replacement is recursive, so tags may also exist in the fields of the * RecordHandler so long as they are also represented by the RecordHandler and do not form an endless loop. * After filling in the template, parses the resulting string in preparation for performing the HTTP request. Expects the * the string to be in the following format: * * < > < > * Host: < > * < >:< > * ... * < >: < > * * < > * * < > must be GET, PUT, POST, or DELETE. < > must be a string with no spaces. It is appended to * < > to form the complete call string. After a single blank line is encountered, the rest of the file * is used as the body of text for PUT and POST calls. This function also expects the Record Handler to include a field * named "VPID" containing a unique record identifier for debugging purposes. * * @param template String, should contain the full template. * @param record RecordHandler, the input data used to fill in replacement tags that exist in the template. * @return this Reference to this class, primarily to allow request generation and performance in one line. * @throws Exception */ public HTTPReqGen generate_request(String template, HashMap record) throws Exception { String filled_template = ""; Boolean found_replacement = true; headers.clear(); try { // Splits template into tokens, separating out the replacement strings // like < > String[] tokens = tokenize_template(template); // Repeatedly perform replacements with data from record until no // replacements are found // If a replacement's result is an empty string, it will not throw an // error (but will throw one if there is no column for that result) while(found_replacement) { found_replacement = false; filled_template = ""; for(String item: tokens) { if(item.startsWith("<<") && item.endsWith(">>")) { found_replacement = true; item = item.substring(2, item.length() - 2); if( !record.containsKey(item)) { logger.error("Template contained replacement string whose value did not exist in input record:[" + item + "]"); } item = record.get(item); } filled_template += item; } tokens = tokenize_template(filled_template); } } catch (Exception e) { logger.error("Problem performing replacements from template: ", e); } try { // Feed filled template into BufferedReader so that we can read it line // by line. InputStream stream = IOUtils.toInputStream(filled_template, "UTF-8"); BufferedReader in = new BufferedReader(new InputStreamReader(stream)); String line = ""; String[] line_tokens; // First line should always be call type followed by call suffix line = in.readLine(); line_tokens = line.split(" "); call_type = line_tokens[0]; call_suffix = line_tokens[1]; // Second line should contain the host as it's second token line = in.readLine(); line_tokens = line.split(" "); call_host = line_tokens[1]; // Full call string for RestAssured will be concatenation of call // host and call suffix call_string = call_host + call_suffix; // Remaining lines will contain headers, until the read line is // empty line = in.readLine(); while(line != null && !line.equals("")) { String lineP1 = line.substring(0, line.indexOf(":")).trim(); String lineP2 = line.substring(line.indexOf(" "), line.length()).trim(); headers.put(lineP1, lineP2); line = in.readLine(); } // If read line is empty, but next line(s) have data, create body // from them if(line != null && line.equals("")) { body = ""; while( (line = in.readLine()) != null && !line.equals("")) { body += line; } } } catch(Exception e) { logger.error("Problem setting request values from template: ", e); } return this; } /** * Performs the request using the stored request data and then returns the response. * * @return response Response, will contain entire response (response string and status code). */ public Response perform_request() throws Exception { Response response = null; try { for(Map.Entry entry: headers.entrySet()) { reqSpec.header(entry.getKey(), entry.getValue()); } for(Map.Entry entry: cookie_list.entrySet()) { reqSpec.cookie(entry.getKey(), entry.getValue()); } switch(call_type) { case "GET": { response = reqSpec.get(call_string); break; } case "POST": { response = reqSpec.body(body).post(call_string); break; } case "PUT": { response = reqSpec.body(body).put(call_string); break; } case "DELETE": { response = reqSpec.delete(call_string); break; } default: { logger.error("Unknown call type: [" + call_type + "]"); } } } catch (Exception e) { logger.error("Problem performing request: ", e); } return response; } /** * Splits a template string into tokens, separating out tokens that look like "< >" * * @param template String, the template to be tokenized. * @return list String[], contains the tokens from the template. */ private String[] tokenize_template(String template) { return template.split("(?=[<]{2})|(?<=[>]{2})"); }}
RecordHandler
package com.demo.qa.utils;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class RecordHandler { private enum RecordType { VALUE, NAMED_MAP, INDEXED_LIST } private String single_value = ""; private HashMap named_value_map = new HashMap (); private List indexed_value_list = new ArrayList (); private RecordType myType; public RecordHandler() { this(""); } public RecordHandler(String value) { this.myType = RecordType.VALUE; this.single_value = value; } public RecordHandler(HashMap map) { this.myType = RecordType.NAMED_MAP; this.named_value_map = map; } public RecordHandler(List list) { this.myType = RecordType.INDEXED_LIST; this.indexed_value_list = list; } public HashMap get_map() { return named_value_map; } public int size() { int result = 0; if(myType.equals(RecordType.VALUE)) { result = 1; } else if(myType.equals(RecordType.NAMED_MAP)) { result = named_value_map.size(); } else if(myType.equals(RecordType.INDEXED_LIST)) { result = indexed_value_list.size(); } return result; } public String get() { String result = ""; if(myType.equals(RecordType.VALUE)) result = single_value; else { System.out.println("Called get() on wrong type:" + myType.toString()); } return result; } public String get(String key) { String result = ""; if(myType.equals(RecordType.NAMED_MAP)) result = named_value_map.get(key); return result; } public String get(Integer index) { String result = ""; if(myType.equals(RecordType.INDEXED_LIST)) result = indexed_value_list.get(index); return result; } public Boolean set(String value) { Boolean result = false; if(myType.equals(RecordType.VALUE)) { this.single_value = value; result = true; } else if(myType.equals(RecordType.INDEXED_LIST)) { this.indexed_value_list.add(value); result = true; } return result; } public Boolean set(String key, String value) { Boolean result = false; if(myType.equals(RecordType.NAMED_MAP)) { this.named_value_map.put(key, value); result = true; } return result; } public Boolean set(Integer index, String value) { Boolean result = false; if(myType.equals(RecordType.INDEXED_LIST)) { if(this.indexed_value_list.size() > index) this.indexed_value_list.set(index, value); result = true; } return result; } public Boolean has(String value) { Boolean result = false; if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) { result = true; } else if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) { result = true; } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) { result = true; } return result; } public Boolean remove(String value) { Boolean result = false; if(myType.equals(RecordType.VALUE) && this.single_value.equals(value)) { this.single_value = ""; result = true; } if(myType.equals(RecordType.NAMED_MAP) && this.named_value_map.containsKey(value)) { this.named_value_map.remove(value); result = true; } else if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(value)) { this.indexed_value_list.remove(value); result = true; } return result; } public Boolean remove(Integer index) { Boolean result = false; if(myType.equals(RecordType.INDEXED_LIST) && this.indexed_value_list.contains(index)) { this.indexed_value_list.remove(index); result = true; } return result; }}