Created On:  29 September 2011

Problem:

How do I update a Test Plan using HttpClient 4, through the SilkCentral Services Exchange updateTestPlan method?

Resolution:

The code below demonstrates how to update a Test Plan via the Services Exchange updateTestPlan method using HttpClient 4:

//Reads the modified testplan xml file
public static String loadTPUTF8(String sFileName){
     BufferedReader in =
null;
     String s, UTF8Str =
"";

    
try {
          in =
new BufferedReader(new FileReader(sFileName));
          while( ( s = in.readLine() ) != null) {
               UTF8Str =
new String(s.getBytes(),"UTF-8");
          }
    
}
catch (Exception e) {
          e.printStackTrace();
     }

     return
UTF8Str;
}

public static void updateTP(String sFileName, String sServer, int sPort, String sUser, String sPassword){
     try{
         
URL sctm =
new URL("http", sServer, sPort,String.format("/servicesExchange?hid=%s&userName=%s&passWord=%s", "updateTestPlan", sUser, sPassword));

         
//Create an entity with the modified testplan xml file
          MultipartEntity entTP = new MultipartEntity();
          entTP.addPart(sFileName,
new StringBody(ServExchange.loadTPUTF8(sFileName),"text/xml", Charset.forName("UTF-8")));

          //add the testplan xml file entity to the HttpPost object
          HttpPost postTP = new HttpPost(sctm.toExternalForm());
          postTP.setEntity(entTP);

          //Execute the updateTestPlan method with the updated testplan xml
          HttpClient client = new DefaultHttpClient();
          System.
out.println(client.execute(postTP).toString());
     }
     catch(Exception e){
         
e.printStackTrace();
     }
}


Where:
- sFileName is the fully qualified path to the modified testplan xml file
- sServer is the server name or ip address of SilkCentral
- iPort is the port on which SilkCentral can be accessed
- sUser is the username which will be used to access SilkCentral
- sPassword in the password to be used in conjunction with sUser