问题
I am drawing my code my user NickM's github located at https://github.com/nmusaelian-rally/rally-java-rest-apps/blob/master/addTCtoTF.java
I am needing to create a Test Folder Hierarchy >= 3 generations deep(grandparent, parent, child etc. for example). The code I currently have with my additions is below.
The Java code works with my additions I made to the code. However it is still not creating the 3 generation deep Test Folder set that I want(still creating just 2 deep). Can someone help point out and give some examples of corrections I can make to the code to accomplish this? Thanks
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.util.Ref;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.GetRequest;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.GetResponse;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import com.rallydev.rest.util.Ref;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestFolderTestCaseCreation {
// TODO Auto-generated constructor stub
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://rally1.rallydev.com";
String username = "user@company.com";
String password = "secret";
String wsapiVersion = "v2.0";
String projectRef = "/project/xxxxx";
//String myWorkspace = "/workspace/xxxxx";
String applicationName = "RestExample_createTFandTC";
RallyRestApi restApi = new RallyRestApi(
new URI(host),
username,
password);
restApi.setWsapiVersion(wsapiVersion);
restApi.setApplicationName(applicationName);
try {
for (int i=0; i<1; i++) {
System.out.println("Creating a test folder...");
JsonObject newTF = new JsonObject();
newTF.addProperty("Name", "Grandparent");
newTF.addProperty("Project", projectRef);
//Created Grandparent
CreateRequest createRequest = new CreateRequest("testfolder", newTF);
CreateResponse createResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
String folderRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading TestFolder %s...",folderRef));
System.out.println("Creating a child test folder...");
JsonObject newChildTF = new JsonObject();
newChildTF.addProperty("Name", "Parent");
newChildTF.addProperty("Project", projectRef);
//newChildTF.addProperty("Workspace", myWorkspace);
newChildTF.addProperty("Parent", folderRef);
String folderRef2 = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading TestFolder %s...",folderRef2));
JsonObject newChildTF2 = new JsonObject();
newChildTF2.addProperty("Name", "Child");
newChildTF2.addProperty("Project", projectRef);
newChildTF2.addProperty("Parent", folderRef2);
//Test Folder2
CreateRequest createRequest2 = new CreateRequest("testfolder", newChildTF);
CreateResponse createResponse2 = restApi.create(createRequest2);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
String childFolderRef = Ref.getRelativeRef(createResponse2.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Child TestFolder %s...",childFolderRef));
//TestCase
System.out.println("Creating a test case...");
JsonObject newTC = new JsonObject();
newTC.addProperty("Name", "tc via java");
newTC.addProperty("Project", projectRef);
//newChildTF.addProperty("Workspace", myWorkspace);
newTC.addProperty("TestFolder", childFolderRef);
newTC.addProperty("Method", "Manual");
CreateRequest createRequest3 = new CreateRequest("testcase", newTC);
CreateResponse createResponse3 = restApi.create(createRequest3);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse3.getObject().get("_ref").getAsString()));
String testCaseRef = Ref.getRelativeRef(createResponse3.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading TestCase %s...",testCaseRef));
}
}
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error!");
for (int j=0; i<createErrors.length;j++) {
System.out.println(createErrors[j]);
}
}
}
} finally {
restApi.close();
}
}
}
回答1:
I modified the code to create a grandchild level test folder and add a test case to it. See the code in this github repo.
try {
//-----creating a parent folder
System.out.println("Creating a test folder...");
JsonObject newTF = new JsonObject();
newTF.addProperty("Name", "parent tf via java");
newTF.addProperty("Project", projectRef);
CreateRequest createRequest = new CreateRequest("testfolder", newTF);
CreateResponse createResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
//Read test folder
String folderRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading TestFolder %s...",folderRef));
System.out.println("Creating a child test folder...");
JsonObject newChildTF = new JsonObject();
newChildTF.addProperty("Name", "child tf via java");
newChildTF.addProperty("Project", projectRef);
newChildTF.addProperty("Parent", folderRef);
CreateRequest createRequest2 = new CreateRequest("testfolder", newChildTF);
CreateResponse createResponse2 = restApi.create(createRequest2);
if (createResponse2.wasSuccessful()) {
//----------------creating a grandchild folder
System.out.println(String.format("Created %s", createResponse2.getObject().get("_ref").getAsString()));
//Read test folder
String childFolderRef = Ref.getRelativeRef(createResponse2.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading TestFolder %s...",childFolderRef));
System.out.println("Creating a grandchild test folder...");
JsonObject newGrandChildTF = new JsonObject();
newGrandChildTF.addProperty("Name", "grandchild tf via java");
newGrandChildTF.addProperty("Project", projectRef);
newGrandChildTF.addProperty("Parent", childFolderRef);
CreateRequest createRequest3 = new CreateRequest("testfolder", newGrandChildTF);
CreateResponse createResponse3 = restApi.create(createRequest3);
//----------------creating a test case
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
//Read test folder
String grandChildFolderRef = Ref.getRelativeRef(createResponse3.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Child TestFolder %s...",grandChildFolderRef));
System.out.println("Creating a test case...");
JsonObject newTC = new JsonObject();
newTC.addProperty("Name", "tc via java");
newTC.addProperty("Project", projectRef);
newTC.addProperty("TestFolder", grandChildFolderRef);
newTC.addProperty("Method", "Manual");
CreateRequest createRequest4 = new CreateRequest("testcase", newTC);
CreateResponse createResponse4 = restApi.create(createRequest4);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse4.getObject().get("_ref").getAsString()));
//Read test folder
String testCaseRef = Ref.getRelativeRef(createResponse4.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading TestCase %s...",testCaseRef));
}
}
}
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error!");
for (int j=0; j<createErrors.length;j++) {
System.out.println(createErrors[j]);
}
}
} finally {
//Release all resources
restApi.close();
}
来源:https://stackoverflow.com/questions/24288059/creating-3-generation-deep-test-folders