Skip to content
This repository has been archived by the owner on Dec 4, 2019. It is now read-only.

Commit

Permalink
Open Interface automation.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszJerzewskiKainos committed Jun 28, 2018
1 parent 58f207f commit 3bae733
Show file tree
Hide file tree
Showing 15 changed files with 792 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

/.project
/.settings/
/bin/
/.classpath
/target/
/poc-new-selenium.iml
/build/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.dvsa.mot.framework.WebDriverWrapper;
import uk.gov.dvsa.mot.framework.xml.XmlDocument;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -37,6 +38,17 @@ public DocumentStepDefinitions(WebDriverWrapper driverWrapper) {

And("^I click \"([^\"]+)\" and check the CSV contains:$",
(String link, DataTable table) -> assertTrue(csvContainsData(link, table)));

And("^I click \"([^\"]+)\" and check the XML contains:$",
(String link, DataTable table) -> {
try {
XmlDocument xmlDocument = driverWrapper.createXmlDocument(link);

assertTrue(xmlDocument.contains(getList(table)));
} catch (Exception exception) {
logger.error("Unable to load XML document: %s", exception);
}
});
}

/**
Expand Down Expand Up @@ -82,4 +94,14 @@ private String getStringValue(String text) {
}
return text;
}

private List<String> getList(DataTable dataTable) {
List<String> processedDataRows = new ArrayList<>();

for (String dataItem : dataTable.asList(String.class)) {
processedDataRows.add(getStringValue(dataItem));
}

return processedDataRows;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ public MothStepDefintions(WebDriverWrapper driverWrapper) {

When("^I close extra tabs$", () -> driverWrapper.closeTabs());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package uk.gov.dvsa.mot.fixtures.openinterface;

import static org.junit.Assert.assertTrue;

import cucumber.api.DataTable;
import cucumber.api.java8.En;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import uk.gov.dvsa.mot.framework.WebDriverWrapper;
import uk.gov.dvsa.mot.framework.xml.XmlDocument;
import uk.gov.dvsa.mot.reporting.OpenInterfaceReportBuilder;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;

public class OpenInterfaceStepDefinitions implements En {

/** The logger to use. */
private static final Logger logger = LoggerFactory.getLogger(OpenInterfaceStepDefinitions.class);

/** The driverwrapper to use. */
private final WebDriverWrapper driverWrapper;

/** The configuration to use by the testsuite. */
private final Environment env;

/**
* Create a new instance.
* @param driverWrapper The web driver wrapper to be used
*/
@Inject
public OpenInterfaceStepDefinitions(WebDriverWrapper driverWrapper, Environment env) {
this.driverWrapper = driverWrapper;
this.env = env;

Then("^I check if a \"([^\\}]+)\", with \\{([^\\}]+)\\} vrm, contains xpath values:$",
(String type, String registration, DataTable table) -> {
String response = getResponse(registration);
XmlDocument document = getXmlDocument(response);

Map<String, String> values = expandDataTableXpath(table);
values = processXpathValues(values);
Map<String,String> responseValues =
getResponseValues(document, new ArrayList<>(values.keySet()));

writeHtmlxPath(type, driverWrapper.getData(registration), response, values, responseValues);
});
}

private HashMap<String, String> getResponseValues(XmlDocument xmlDocument, List<String> xpathList) {
HashMap<String, String> responseValues = new HashMap<>();

for (String xpathString : xpathList) {
try {
responseValues.put(xpathString, xmlDocument.getxPath(xpathString));
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage());
}
}

return responseValues;
}

private String getResponse(String registration) {
try {
ProcessBuilder processBuilder = new ProcessBuilder( "curl", "-v",
"--header \"Accept: application/xml\"", "--tlsv1.2", "-E ../openif-test-cert/dvlaclienttest.pem",
env.getProperty("openInterfaceUrl") + "dvla/servlet/ECSODDispatcher?VRM=" + registration);

logger.debug(processBuilder.toString());

processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();

StringBuilder rawXml = new StringBuilder();
String content;

InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream());
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

while ((content = bufferedReader.readLine()) != null) {
rawXml.append(content);
}

logger.debug("Raw xml: " + rawXml);

return rawXml.toString();
} catch (Exception ex) {
throw new RuntimeException(String.format("Failed to check the mot history, %s", ex.getStackTrace()));
}
}

private XmlDocument getXmlDocument(String rawXml) {
try {
return new XmlDocument(rawXml);
} catch (Exception ex) {
throw new RuntimeException(String.format("Failed to create new XmlDocument, %s", ex.getStackTrace()));
}
}

private Map<String,String> expandDataTableXpath(DataTable table) {
return table.asMap(String.class, String.class);
}

private List<String> expandDataTable(DataTable table) {
return table.asList(String.class);
}

private boolean valueMatchesDataKey(String value) {
return driverWrapper.getAllDataKeys().contains(value);
}

private List<String> processValues(List<String> values) {
List<String> processedList = new ArrayList<>();

for (String value : values) {
if (valueMatchesDataKey(value)) {
processedList.add(driverWrapper.getData(value));
} else {
processedList.add(value);
}
}

return processedList;
}

private Map<String, String> processXpathValues(Map<String, String> values) {
Map<String, String> processedMap = new HashMap<>();

for (int i = 0; i < values.size(); ++i) {
String key = (String) values.keySet().toArray()[i];
String value = (String) values.values().toArray()[i];

if (valueMatchesDataKey(value)) {
processedMap.put(key, driverWrapper.getData(value));
} else {
processedMap.put(key, value);
}
}

return processedMap;
}

private void writeHtmlxPath(String type, String registration, String response,
Map<String, String> expectedValues, Map<String, String> responseValues) {
boolean scenarioResult = true;

Map<String, Boolean> results = new HashMap<>();

for (String key : expectedValues.keySet()) {
String expectedValue = expectedValues.get(key);
String responseValue = responseValues.get(key);

boolean comparisonResult = expectedValue.equals(responseValue);

results.put(key, comparisonResult);

if (!comparisonResult) {
scenarioResult = false;
}
}

OpenInterfaceReportBuilder.generateResponseReport(type, registration, response,
expectedValues, responseValues, results);

assertTrue(scenarioResult);
}
}
27 changes: 27 additions & 0 deletions src/main/java/uk/gov/dvsa/mot/framework/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.springframework.core.env.Environment;
import uk.gov.dvsa.mot.framework.csv.CsvDocument;
import uk.gov.dvsa.mot.framework.csv.CsvException;
import uk.gov.dvsa.mot.framework.xml.XmlDocument;
import uk.gov.dvsa.mot.framework.xml.XmlException;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -121,6 +123,31 @@ public CsvDocument getCsvDocument(String url) throws CsvException {
}
}

/**
* Get XML document.
* @param url of the file to load.
* @return xml document as a string.
*/
public XmlDocument getXmlDocument(String url) throws XmlException {
Cookie session = getCookie(DEFAULT_SESSION_COOKIE_NAME);
Cookie token = getCookie(DEFAULT_TOKEN_COOKIE_NAME);

Response serverResponse = with()
.cookie(session.getName(), session.getValue())
.cookie(token.getName(), token.getValue())
.get(url);

String document = new String(serverResponse.asByteArray());
try {
String filename = url.replaceFirst("https://", "").replaceAll("/", "-") + ".xml";
writeFile(filename, url);

return new XmlDocument(document);
} catch (Exception ex) {
throw new XmlException("Error parsing XML file", ex);
}
}

/**
* Used to save a copy of the document for auditing and verification purposes.
* @param filename The filename to save the document as
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/uk/gov/dvsa/mot/framework/WebDriverWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import uk.gov.dvsa.mot.framework.csv.CsvException;
import uk.gov.dvsa.mot.framework.pdf.PdfDocument;
import uk.gov.dvsa.mot.framework.pdf.PdfException;
import uk.gov.dvsa.mot.framework.xml.XmlDocument;
import uk.gov.dvsa.mot.framework.xml.XmlException;

import java.io.IOException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -1654,4 +1656,17 @@ public String getCurrentUrl() {
return this.webDriver.getCurrentUrl();
}

/**
* Creates a XML document from the URL provided.
* @param url of the target document
* @return parsed XML from URL
*/
public XmlDocument createXmlDocument(String url) throws XmlException {
try {
return requestHandler.getXmlDocument(url);
} catch (Exception ex) {
logger.error(String.format("Failed to load XML document from %s.", url), ex);
throw new XmlException("Error processing XML document", ex);
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/uk/gov/dvsa/mot/framework/html/HtmlWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.gov.dvsa.mot.framework.html;

import java.util.Map;

public class HtmlWriter {
/**
* Get a full html tag.
*
* @param tagName of the html tag
* @param attributes to include in the tag
* @param text the contents of the tag
* @return the html tag with it's contents
*/
public static String tag(String tagName, Map<String, String> attributes, String text) {
StringBuilder output = new StringBuilder();

output.append("<")
.append(tagName);

String collapsedAttributes = collapseAttributes(attributes);

if (collapsedAttributes != null) {
output.append(" ").append(collapsedAttributes);
}

output.append(">")
.append(text)
.append("</")
.append(tagName)
.append(">");

return output.toString();
}

private static String collapseAttributes(Map<String, String> attributes) {
if (attributes == null || attributes.size() == 0) {
return null;
}

StringBuilder output = new StringBuilder();

for (String key : attributes.keySet()) {
output.append(key)
.append("=\"")
.append(attributes.get(key))
.append("\" ");
}

return output.toString();
}
}
Loading

0 comments on commit 3bae733

Please sign in to comment.