Prerequisite
In order to Unit Test our services with jMoby, we will be using the following classes:
Additionally, you will need the location of a service’s description. In this tutorial, we will be using the RDF document that describes the service
Testing the Service
In order to test the service, we need to download and parse the RDF document into a MobyService, using the
-
ServiceInstanceParser
// instantiate our ServiceInstanceParser with the URL to our // Service description ServiceInstanceParser sip = new ServiceInstanceParser( "http://tinyurl.com/example-unit-test"); // parse the RDF document and get our service MobyService service = sip.getMobyServicesFromRDF()[0];
Once we have our MobyService object, we can get all the unit tests for the object by calling getUnitTests(). In this case, there is only one UnitTest, so we will take the first one
// extract the unit test MobyUnitTest unitTest = service.getUnitTests()[0];
Now all that is left is to call the service using the example input (as specified by the service provider)
// call the synchronous moby service String service_output = doCall(service_name, service_url, service_input);
Once we have the output, we can then proceed to test the service.
We can compare the example output to the actual service_output and get any semantic differences:
// compare outputXML if (!unitTest.compareOutputXML(service_output)) System.out.println(unitTest.getXMLDifferences(service_output)); else { System.out.println("Matched successfully!"); }
Or, we can apply the regular expression to our service_output:
// test against regular expression if (!unitTest.getValidREGEX().trim().equals("") && !unitTest.compareXmlWithREGEX(service_output, true)) System.out.println("Regex didn't match!"); else { System.out.println("Regex matched successfully!"); }
Or, we can apply the defined XPATH expression to our service_output:
// test against xpath expression if (!unitTest.getValidXPath().trim().equals("") && !unitTest.compareXmlWithXpath(service_output)) System.out.println("XPATH didn't match!"); else { System.out.println("XPATH matched successfully!"); }
That is all that there is to unit testing your services in JAVA!
The full source code for this example:
import org.biomoby.client.CentralImpl; import org.biomoby.shared.MobyException; import org.biomoby.shared.MobyService; import org.biomoby.shared.MobyUnitTest; import org.biomoby.shared.extended.ServiceInstanceParser; public class UnitTestTest { public static void main(String[] args) throws MobyException { // instantiate our ServiceInstanceParser with the URL to our // Service description ServiceInstanceParser sip = new ServiceInstanceParser( "http://tinyurl.com/example-unit-test"); // parse the RDF document MobyService service = sip.getMobyServicesFromRDF()[0]; // extract the unit test MobyUnitTest unitTest = service.getUnitTests()[0]; // print out the Unit Test as obtained from the URL System.out.println("Here is the unit test obtained from the url:\n" + unitTest); // extract information for calling the service String service_url = service.getURL(); String service_name = service.getName(); String service_input = unitTest.getExampleInput(); // call the service synchronous moby service String service_output = doCall(service_name, service_url, service_input); // compare outputXML if (!unitTest.compareOutputXML(service_output)) System.out.println(unitTest.getXMLDifferences(service_output)); else { System.out.println("Matched successfully!"); } // test against regular expression if (!unitTest.getValidREGEX().trim().equals("") && !unitTest.compareXmlWithREGEX(service_output, true)) System.out.println("Regex didn't match!"); else { System.out.println("Regex matched successfully!"); } // test against xpath expression if (!unitTest.getValidXPath().trim().equals("") && !unitTest.compareXmlWithXpath(service_output)) System.out.println("XPATH didn't match!"); else { System.out.println("XPATH matched successfully!"); } } /** * * @param servicename * the name of the service to test * @param endpoint * the url to the service * @param input * a full BioMOBY message to use as input for the service * @return a string of XML representing the output of the service * @throws MobyException * if there is a problem calling the service */ public static String doCall(String servicename, String endpoint, String input) throws MobyException { return new CentralImpl(endpoint, "http://biomoby.org/") .call(servicename, input); } }