Example: test exec result update from Java based automation

Discussion and information for XML-RPC interface.

Moderators: Amaradana, TurboPT, TL Developers

Example: test exec result update from Java based automation

Postby zeevikn » Tue Aug 31, 2010 11:25 am

Hi.

We're using a Java based automation system - Jsystem (open source, by Aqua) - http://www.jsystemtest.org/
We wanted to integrate into the test automation system the ability to update a test case's result.

Using Java XML-RPC jar (xml-rpc-clien-3.1.2.jar, xml-rpc-server-3.1.2.jar, xml-rpc-common-3.1.2.jar), we added the following capability (code is below):
The "main" method is reportTCResult(int testPlanID, String testCaseExternalID, TestCaseStatus status).
This method gets testPlanID, testCaseExternalID and TestCaseStatus (pass, fail, etc).
What about the buildId - this is the tricky part :)

the buildName and buildId are retrieved in the following way:
the build name is retrieved from the automation system (which gets it from the SUT) - Summary.getInstance().getProperty("Build").toString()
Then, it looks for that buildName's ID in TestLink system. If it does not find such, it simply creates a new one.
(This way, we do not need to maintain the build's names, as it may increase twice a day)

Feel free to comment, suggest or ask any questions.
Zeevik.

Code: Select all
package com.altor.testlink;

import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import jsystem.framework.JSystemProperties;
import jsystem.framework.report.Summary;
import jsystem.framework.system.SystemObjectImpl;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

public class TestLinkXmlRpcClient extends SystemObjectImpl {
   
   private String server;
      
   public void setServer(String server) {
      this.server = server;
   }

   public String getServer() {
      return server;
   }
   
   public Object execute(String command, Object...params) throws Exception {
      XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
       config.setServerURL(new URL("http://" + getServer() + "/testlink/lib/api/xmlrpc.php"));
       XmlRpcClient client = new XmlRpcClient();
       client.setConfig(config);
      Object returnedObject = client.execute(command, Arrays.asList(params));
      if (returnedObject instanceof Exception) {
         throw (Exception) returnedObject;
      }
      return returnedObject;
   }
   
   public String ping() throws Exception {
      Object res = execute("tl.ping");
      if (res instanceof String) {
         return res.toString();
      } else {
         throw new Exception("Unknown return type: " + res.getClass().getName());
      }
   }
   
   public HashMap<?, ?> getProjects() throws Exception {
      /*
       * devKey     CLIENTSAMPLEDEVKEY
       */
      Object params[] = new Object[1];
      params[0] = getDevKey();
      Object res = execute("tl.getProjects", params);
       HashMap<?, ?> map = null;
       if(res instanceof Object[]){
          if (((Object[])res)[0] instanceof HashMap<?, ?>) {
             map = (HashMap<?, ?>)((Object[])res)[0];
          } else {
             throw new Exception("Unknown return type: " + ((Object[])res)[0].getClass().getName());
          }
       } else{
          throw new Exception("Unknown return type: " + res.getClass().getName());
       }
       reportMessage(map);
       return map;   
   }
   
   public HashMap<?, ?> reportTCResult(int testPlanID, String testCaseExternalID, TestCaseStatus status) throws Exception {
      /*
       * devKey     CLIENTSAMPLEDEVKEY
       * testplanid    54
       * testcaseexternalid    ignr-5
       * buildid    8
       * notes    Call using all EXTERNAL ID (ignr-5) - status=f
       * status    f
       * platformname    Solaris 9
       * overwrite    TRUE
       */
      Map<String, Object> reportTCResultParams = new HashMap<String,Object>();
      reportTCResultParams.put("devKey", getDevKey());
      reportTCResultParams.put("testplanid", testPlanID);
      reportTCResultParams.put("testcaseexternalid", testCaseExternalID);
      reportTCResultParams.put("buildid", getBuildID(testPlanID));
      reportTCResultParams.put("notes", "Call using all EXTERNAL ID (" + testCaseExternalID + ") - status=" + status.getValue());
      reportTCResultParams.put("status", status.getValue());
      reportTCResultParams.put("platformname", "Solaris 9");
      reportTCResultParams.put("overwrite", "TRUE");
       Object params[] = new Object[1];
       params[0] = reportTCResultParams;
      Object res = execute("tl.reportTCResult", params);
      HashMap<?, ?> map = null;
       if(res instanceof Object[]){
          if (((Object[])res)[0] instanceof HashMap<?, ?>) {
             map = (HashMap<?, ?>)((Object[])res)[0];
          } else {
             throw new Exception("Unknown return type: " + ((Object[])res)[0].getClass().getName());
          }
       } else{
          throw new Exception("Unknown return type: " + res.getClass().getName());
       }
       reportMessage(map);
       return map;   
   }
   
   public Object[] getBuildsForTestPlan(int testPlanID) throws Exception {
      /*
       * devKey     CLIENTSAMPLEDEVKEY
       * testplanid    54
       */
      Map<String, Object> getBuildsForTestPlanParams = new HashMap<String,Object>();
      getBuildsForTestPlanParams.put("devKey", getDevKey());
      getBuildsForTestPlanParams.put("testplanid", testPlanID);
      Object params[] = new Object[1];
       params[0] = getBuildsForTestPlanParams;
       Object res = execute("tl.getBuildsForTestPlan", params);
       if(res instanceof Object[]){
          if (((Object[])res)[0] instanceof HashMap<?, ?>) {
             return (Object[])res;
          } else {
             throw new Exception("Unknown return type: " + ((Object[])res)[0].getClass().getName());
          }
       } else{
          throw new Exception("Unknown return type: " + res.getClass().getName());
       }
       //{id=6, name=5.0.c-1-1, release_date=, is_open=1, active=1, testplan_id=54, notes=}
   }
   
   private String getBuildID(int testPlanID) throws Exception {
      String buildID = null;
      boolean isBuildExist = false;
      String buildName = Summary.getInstance().getProperty("Build").toString();
      buildName = (buildName.contains("(")) ? buildName.replace("(", "-").substring(0, buildName.indexOf(")")) : buildName;
      Object[] builds = getBuildsForTestPlan(testPlanID);
      for (Object build : builds) {
         HashMap<?, ?> buildMap = (HashMap<?, ?>)build;
         if (buildName.equals(buildMap.get("name").toString())) {
            buildID = buildMap.get("id").toString();
            isBuildExist = true;
            break;
         }
      }
      if (!isBuildExist) {
         buildID = createBuild(testPlanID, buildName);
      }
      return buildID;
   }
   
   public String createBuild(int testPlanID, String buildName) throws Exception {
      /*
       * devKey     CLIENTSAMPLEDEVKEY
       * testplanid     3
       * buildname    TEST API BUILD
       * buildnotes    Created via API
       */
      Map<String, Object> createBuildParams = new HashMap<String,Object>();
      createBuildParams.put("devKey", getDevKey());
      createBuildParams.put("testplanid", testPlanID);
      createBuildParams.put("buildname", buildName);
      createBuildParams.put("buildnotes", "Created b automation");
      Object params[] = new Object[1];
       params[0] = createBuildParams;
       Object res = execute("tl.createBuild", params);
       HashMap<?, ?> map = null;
       if(res instanceof Object[]){
          if (((Object[])res)[0] instanceof HashMap<?, ?>) {
             map = (HashMap<?, ?>)((Object[])res)[0];
          } else {
             throw new Exception("Unknown return type: " + ((Object[])res)[0].getClass().getName());
          }
       } else{
          throw new Exception("Unknown return type: " + res.getClass().getName());
       }
       reportMessage(map);
       return map.get("id").toString();   
   }
   
   private String getDevKey() throws Exception {
      String devKey = JSystemProperties.getInstance().getPreference("tl.devKey");
      if (devKey == null) {
         throw new TestLinkXmlRpcException("devKey value is null! (Please, set 'tl.devKey' property in jsystem.properies file)");
      }
      return devKey;
   }
   
   private void reportMessage(HashMap<?, ?> map) throws Exception {
      String message = "";
      boolean status = false;
      if (map.get("status") != null) {
         status = Boolean.parseBoolean(map.get("status").toString());
      }
      if (map.get("message") != null) {
         message = map.get("message").toString();
      }
      if (!message.isEmpty()) {
         report.report(message, status);
      }
   }

   public enum TestCaseStatus {
      FAILED("f"), PASSED("p"), BLOCKED("b");
      
      private String value;
      
      TestCaseStatus(String value) {
         this.value = value;
      }
      
      public String getValue() {
         return value;
      }
   }
}



class TestLinkXmlRpcException extends Exception {
   private static final long serialVersionUID = 4309232045247843783L;

   public TestLinkXmlRpcException() {
      super();
   }

   public TestLinkXmlRpcException(String message) {
      super(message);
   }
}
zeevikn
Advanced user
 
Posts: 18
Joined: Tue Aug 17, 2010 3:08 pm

Re: Example: test exec result update from Java based automat

Postby fman » Tue Aug 31, 2010 5:54 pm

please open issue on mantis CONTRIBUTION - JAVA API example and upload there zip file with your code.
then we will give a look and try to add to examples on TL distribution
fman
Member of TestLink Community
 
Posts: 3063
Joined: Tue Nov 15, 2005 7:19 am


Return to XML-RPC API



Who is online

Users browsing this forum: No registered users and 2 guests