Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. > import com.google.gson.JsonArray;
  2. import com.google.gson.JsonObject;
  3. import com.rallydev.rest.RallyRestApi;
  4. import com.rallydev.rest.request.CreateRequest;
  5. import com.rallydev.rest.request.QueryRequest;
  6. import com.rallydev.rest.request.UpdateRequest;
  7. import com.rallydev.rest.response.CreateResponse;
  8. import com.rallydev.rest.response.QueryResponse;
  9. import com.rallydev.rest.response.UpdateResponse;
  10. import com.rallydev.rest.util.Fetch;
  11. import com.rallydev.rest.util.QueryFilter;
  12. import com.rallydev.rest.util.Ref;
  13.  
  14. import java.io.IOException;
  15. import java.net.URI;
  16. import java.net.URISyntaxException;
  17.  
  18. public class addDefectToSuite {
  19.  
  20. public static void main(String[] args) throws URISyntaxException, IOException {
  21.  
  22. String host = "https://rally1.rallydev.com";
  23. String username = "user@company.com";
  24. String password = "secret";
  25. String wsapiVersion = "v2.0";
  26. String projectRef = "/project/12352608219";
  27. String workspaceRef = "/workspace/12352608129";
  28. String applicationName = "Create defect, add to a defectsuite";
  29.  
  30.  
  31. RallyRestApi restApi = new RallyRestApi(
  32. new URI(host),
  33. username,
  34. password);
  35. restApi.setWsapiVersion(wsapiVersion);
  36. restApi.setApplicationName(applicationName);
  37.  
  38. QueryRequest defectSuiteRequest = new QueryRequest("DefectSuite");
  39. defectSuiteRequest.setFetch(new Fetch("FormattedID","Name", "Defects"));
  40. defectSuiteRequest.setWorkspace(workspaceRef);
  41. defectSuiteRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "DS1"));
  42. QueryResponse defectSuiteQueryResponse = restApi.query(defectSuiteRequest);
  43. JsonObject defectSuiteJsonObject = defectSuiteQueryResponse.getResults().get(0).getAsJsonObject();
  44. System.out.println("defectSuiteJsonObject" + defectSuiteJsonObject);
  45. String defectSuiteRef = defectSuiteJsonObject.get("_ref").getAsString();
  46. int numberOfDefects = defectSuiteJsonObject.getAsJsonObject("Defects").get("Count").getAsInt();
  47. System.out.println(defectSuiteJsonObject.get("Name") + " ref: " + defectSuiteRef + "number of defects: " + numberOfDefects + " " + defectSuiteJsonObject.get("Defects"));
  48.  
  49.  
  50. try {
  51. JsonObject defect = new JsonObject();
  52. defect.addProperty("Name", "bad defect 668");
  53.  
  54. CreateRequest createRequest = new CreateRequest("defect", defect);
  55. CreateResponse createResponse = restApi.create(createRequest);
  56. if (createResponse.wasSuccessful()) {
  57. JsonObject defectJsonObject = createResponse.getObject();
  58. String defectRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
  59. System.out.println(String.format("Created %s", defectRef));
  60. JsonObject defectSuitesOfThisDefect = (JsonObject) defectJsonObject.get("DefectSuites");
  61. int numberOfSuites = defectSuitesOfThisDefect.get("Count").getAsInt();
  62. System.out.println("number of defect suites this defect is part of: " + numberOfSuites);
  63. QueryRequest defectSuitesOfThisDefectRequest = new QueryRequest(defectSuitesOfThisDefect);
  64. JsonArray suites = restApi.query(defectSuitesOfThisDefectRequest).getResults();
  65. System.out.println("suites: " + suites);
  66. suites.add(defectSuiteJsonObject);
  67. System.out.println("suites after add: " + suites);
  68. //Update defect: add to defectsutites collection
  69. JsonObject defectUpdate = new JsonObject();
  70. defectUpdate.add("DefectSuites", suites);
  71. UpdateRequest updateDefectRequest = new UpdateRequest(defectRef,defectUpdate);
  72. UpdateResponse updateResponse = restApi.update(updateDefectRequest);
  73. if (updateResponse.wasSuccessful()) {
  74. System.out.println("Successfully updated defect: " + defectJsonObject.get("FormattedID"));
  75. }
  76. else {
  77. String[] updateErrors;
  78. updateErrors = createResponse.getErrors();
  79. System.out.println("Error");
  80. for (int i=0; i<updateErrors.length;i++) {
  81. System.out.println(updateErrors[i]);
  82. }
  83. }
  84.  
  85. } else {
  86. System.out.println("error");
  87. }
  88.  
  89. } finally {
  90. restApi.close();
  91. }
  92.  
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement