Created
January 22, 2017 14:02
-
-
Save rjsmith/7cdbcedee63a7a4bb7dcda9df48af4dd to your computer and use it in GitHub Desktop.
Proof of concept jUnit test to trigger NetKernel module XUnit tests and assert test status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| dependencies { | |
| compile group: 'urn.org.netkernel', name: 'mod.hds', version: '[1.1.1,)' | |
| testCompile ( | |
| [group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'], | |
| [group: 'junit', name: 'junit', version: '4.7+'], | |
| [group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'] | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package; | |
| import static org.hamcrest.CoreMatchers.equalTo; | |
| import java.io.IOException; | |
| import java.io.StringReader; | |
| import java.util.Optional; | |
| import javax.xml.parsers.DocumentBuilder; | |
| import javax.xml.parsers.DocumentBuilderFactory; | |
| import javax.xml.parsers.ParserConfigurationException; | |
| import javax.xml.xpath.XPath; | |
| import javax.xml.xpath.XPathConstants; | |
| import javax.xml.xpath.XPathExpressionException; | |
| import javax.xml.xpath.XPathFactory; | |
| import org.apache.http.HttpEntity; | |
| import org.apache.http.HttpResponse; | |
| import org.apache.http.client.ClientProtocolException; | |
| import org.apache.http.client.ResponseHandler; | |
| import org.apache.http.client.methods.HttpGet; | |
| import org.apache.http.impl.client.CloseableHttpClient; | |
| import org.apache.http.impl.client.HttpClients; | |
| import org.apache.http.util.EntityUtils; | |
| import org.junit.Rule; | |
| import org.junit.Test; | |
| import org.junit.rules.ErrorCollector; | |
| import org.w3c.dom.Document; | |
| import org.w3c.dom.Node; | |
| import org.w3c.dom.NodeList; | |
| import org.xml.sax.InputSource; | |
| import org.xml.sax.SAXException; | |
| public class NetKernelRemoteXUnitTests { | |
| @Rule | |
| public ErrorCollector collector = new ErrorCollector(); | |
| /** | |
| * Example jUnit test that runs all NetKernel xUnit tests configured for a given Module. | |
| * | |
| * The url passed to the runNetKernelXUnitTests method has to point to a running NetKernel instance accessible to the jUnit executable. | |
| * Should be of the form: <NK Backend fulcrum address>/test/exec/xml/test:<module uri> | |
| * | |
| * @throws Exception | |
| * | |
| */ | |
| @Test | |
| public void runXUnitTests() throws Exception { | |
| // SETUP | |
| // EXECUTE | |
| // Code from: http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.java | |
| String response = runNetKernelXUnitTests("http://localhost:1060/test/exec/xml/test:urn:mymodule", false) | |
| .orElseThrow(IllegalStateException::new); | |
| // VERIFY | |
| assertTestsHavePassed(response); | |
| } | |
| protected Optional<String> runNetKernelXUnitTests(String url, boolean suppressConsoleOutput) { | |
| String responseBody = null; | |
| CloseableHttpClient httpclient = HttpClients.createDefault(); | |
| try { | |
| HttpGet httpget = new HttpGet(url); | |
| if (!suppressConsoleOutput) System.out.println("Executing request " + httpget.getRequestLine()); | |
| // Create a custom response handler | |
| ResponseHandler<String> responseHandler = new ResponseHandler<String>() { | |
| @Override | |
| public String handleResponse( | |
| final HttpResponse response) throws ClientProtocolException, IOException { | |
| int status = response.getStatusLine().getStatusCode(); | |
| if (status >= 200 && status < 300) { | |
| HttpEntity entity = response.getEntity(); | |
| return entity != null ? EntityUtils.toString(entity) : null; | |
| } else { | |
| throw new ClientProtocolException("Unexpected response status: " + status); | |
| } | |
| } | |
| }; | |
| responseBody = httpclient.execute(httpget, responseHandler); | |
| if (!suppressConsoleOutput) { | |
| System.out.println("----------------------------------------"); | |
| System.out.println(responseBody); | |
| } | |
| } catch (Exception e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } finally { | |
| try { | |
| httpclient.close(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| return Optional.of(responseBody); | |
| } | |
| protected void assertTestsHavePassed(String responseBody) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { | |
| DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); | |
| Document document = builder.parse(new InputSource(new StringReader(responseBody))); | |
| //see: https://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html | |
| XPath xpath = XPathFactory.newInstance().newXPath(); | |
| String expression = "/testlist/test"; | |
| NodeList testNodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET); | |
| for (int i = 0; i < testNodes.getLength(); i++) { | |
| Node testNode = testNodes.item(i); | |
| String testStatus = testNode.getAttributes().getNamedItem("testStatus").getNodeValue(); | |
| Node testNameNode = testNode.getAttributes().getNamedItem("name"); | |
| String failureMessage = "Test FAILED: " + ((testNameNode != null) ? testNameNode.getNodeValue() : "#" + (i+1)); | |
| collector.checkThat(failureMessage, testStatus, equalTo("success")); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment