I want to add Test cases (already present in Rally in Test folders) into a newly created Test sets using Java Rally Rest API.
Basically every time I want to execute the test cases through Rally I need to create new Test sets and add relevant test cases ( either priority wise, automated/ Manual) in test sets.
Any help on this would be of great help.
Test cases are associated to test sets via the TestCases collection on TestSet in the WSAPI. This code example demonstrates creating a few test cases and then creating a test set associated with the newly created test cases.
RallyRestApi restApi = new RallyRestApi(new URI(SERVER),
USERNAME, PASSWORD);
try {
//Create some test cases
JsonArray testCases = new JsonArray();
for(int i = 0; i < 3; i++) {
JsonObject newTestCase = new JsonObject();
newTestCase.addProperty("Name", "New Test Case " + i);
CreateRequest createRequest = new CreateRequest("testcase", newTestCase);
CreateResponse createResponse = restApi.create(createRequest);
String ref = createResponse.getObject().get("_ref").getAsString();
System.out.println(String.format("Created test case %s", ref));
//Keep track of the test case
JsonObject testCase = new JsonObject();
testCase.addProperty("_ref", ref);
testCases.add(testCase);
}
//Create test set
JsonObject newTestSet = new JsonObject();
newTestSet.addProperty("Name", "New Test Set");
newTestSet.add("TestCases", testCases);
CreateRequest createRequest = new CreateRequest("testset", newTestSet);
CreateResponse createResponse = restApi.create(createRequest);
System.out.println(String.format("Created test set %s", createResponse.getObject().get("_ref").getAsString()));
} finally {
//Release all resources
restApi.close();
}
Related
I am trying to trying to test a set of result generated by a service class with some defined expected result. The service produce the actual result.
The service is feed with values from a json file which contain some values and expected result. The output of service is compared with expected result using AssertEquals and the test is passing only when they are equal.
Is it possible to continue test even if some AssertEquals fails and generate a report of how many AssertEquals have passed or failed.
I explored maven surefire but I am not able to get expected result.
Note: There is only one #Test method.Inside this method only I am calling the service with multiple times with different parameters and comparing expected & actual result
#Test
public void createTest() throws Exception {
try {
// some other code to read the file
JSONParser parser = new JSONParser();
// ruleValidationResourcePath is location of the file
JSONArray array = (JSONArray) parser.parse(new FileReader(ruleValidationResourcePath));
//looping this json and passing each object values to a service
for(Object o:array){
JSONObject rulesValidation = (JSONObject) o;
String ruleAdExpr = (String) rulesValidation.get("ruleA");
String _result = (String)rulesValidation.get("result");
PatObj patObj= new PatObj ();
patObj.setRule(ruleAdExpr.trim());
//service is an instance of class which hold props method
PatObj pat = service.props(patObj);
if(patObj.getRule() != null){
String _machineRule =pat .getMachine_rule().toLowerCase().trim();
String expResult = _result.toLowerCase().trim();
// here some _machineRule & expResult may be different
// currently test is failing if they are not equal
// will like to continue test even if they are not equal
// and create report of how many failed/passed
Assert.assertEquals(_machineRule,expResult);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Is it possible to continue test even if some AssertEquals fails.
Yes, it is possible but I wouldn't recommend you doing that because it is misuse of JUnit.
The JUnit methodology follows the rule that a test should check only one particular case per test method.
In your case you JSON loading logic is just a setup, and you check getMachineRule() method with different parameters.
JUnit world has its own mechanisms of handling such cases.
How do you implement it properly:
You need to rework the test by making it parametrised.
First add JUnitParams to your project.
Then you need to introduce a method that will load JSONArray and use its result as parameters for your test.
All together:
#RunWith(JUnitParamsRunner.class)
public class RoleTest {
#Test
#Parameters
public void createTest(JSONObject rulesValidation) throws Exception {
// use the same logic as you have inside your "for" loop
//...
assertEquals(machineRule, expResult);
}
private Object[] parametersForCreateTest() {
// load JSON here and convert it to array
JSONParser parser = new JSONParser();
JSONArray jsonArray = (JSONArray) parser.parse(
new FileReader(ruleValidationResourcePath)
);
return jsonArray.toArray();
}
}
This way the createTest will be performed as many times as you have objects in your JSONArray and you'll see the report for each particular run (even if some of them fail).
Note: the method naming matters. A method that return parameters should be named the same way as the test method but prefixed with parametersFor - in your case parametersForCreateTest. Find more examples in the documentation.
This pretty much goes against what the Assertion is intended. One of the difficulties here is that a failed assertion throws an error and not an exception.
You could use:
catch (AssertionError e) {
...
Once you catch the error, you still need to tally it so the code would be far cleaner if you simply made the comparison using an if statement and recording the result
if(_machineRule.equals(expResult)) {success++;}
and then
Assert.assertEquals(expectedCount,success);
after your loop completes processing of all the elements in your list. Another method might be:
try{
Assert.assertEquals(_machineRule,expResult);
System.out.println(description + " - passed");
}catch(AssertionError e){
System.out.println(description + " - failed");
throw e;
}
I believe you want the org.junit.rules.ErrorCollector rule from the junit 4.7+ API. It should do exactly what you're looking for; allowing a single test to assert multiple times and report the end-state of each check individually.
//This must be public
#Rule
public ErrorCollector collector = new ErrorCollector();
#Test
public void createTest() throws Exception {
try {
// some other code to read the file
JSONParser parser = new JSONParser();
// ruleValidationResourcePath is location of the file
JSONArray array = (JSONArray) parser.parse(new FileReader(ruleValidationResourcePath));
//looping this json and passing each object values to a service
for(Object o:array){
JSONObject rulesValidation = (JSONObject) o;
String ruleAdExpr = (String) rulesValidation.get("ruleA");
String _result = (String)rulesValidation.get("result");
PatObj patObj= new PatObj ();
patObj.setRule(ruleAdExpr.trim());
//service is an instance of class which hold props method
PatObj pat = service.props(patObj);
if(patObj.getRule() != null){
String _machineRule =pat .getMachine_rule().toLowerCase().trim();
String expResult = _result.toLowerCase().trim();
// here some _machineRule & expResult may be different
// currently test is failing if they are not equal
// will like to continue test even if they are not equal
// and create report of how many failed/passed
collector.checkThat(expResult, IsEqual.equalTo(_machineRule));
//Assert.assertEquals(_machineRule,expResult);
}
}
} catch (FileNotFoundException e) {
collector.addError(e);
// e.printStackTrace();
}
}
We are trying to automate the project migration from one Rally workspace to other. Everything seems to work fine like we are able to migrate project and related releases/iterations/userstories/tasks from one workspace to another workspace.
But while trying to migrate BE Initiative/BE Feature/CPM Feature we are getting some exception related to Null Pointer exception but the error we are getting in Response doesn't seem to give much info.
A sample of code is -
String oldProjectObjectId = "12345";
String newProjectObjectId = "67890";
String oldRallyWorkspaceObjectId = "32145";
String newRallyWorkspaceObjectId = "67894";
QueryResponse beInitiativeResponse = queryRally("portfolioitem/beinitiative", "/project/"+this.oldProjectObjectId, "/workspace/"+this.oldRallyWorkspaceObjectId);
int beInitiativeCount = beInitiativeResponse.getTotalResultCount();
if(beInitiativeCount >0){
JsonArray initiativeArray = beInitiativeResponse.getResults();
for(int i=0; i< initiativeArray.size();i++){
JsonObject beInitiativeObject = initiativeArray.get(i).getAsJsonObject();
String oldBeInitiativeObjectId = beInitiativeObject.get("ObjectID").getAsString();
String oldBeInitiativeName = beInitiativeObject.get("_refObjectName").getAsString();
String owner = getObjectId(beInitiativeObject, "Owner");
JsonObject BeInitiativeCreateObject = getJsonObject(oldBeInitiativeName, "/project/"+this.newProjectObjectId, "/workspace/"+this.newRallyWorkspaceObjectId, owner);
CreateResponse beInitiativeCreateResponse = createInRally("portfolioitem/beinitiative", BeInitiativeCreateObject);
if(beInitiativeCreateResponse.wasSuccessful()){
String newBeInitiativeObjectId = beInitiativeCreateResponse.getObject().get("ObjectID").getAsString();
String mapKey = oldBeInitiativeObjectId;
String mapValue= newBeInitiativeObjectId;
this.beInitiativesHashMap.put(mapKey, mapValue);
}
else{
String[] errorList;
errorList = beInitiativeCreateResponse.getErrors();
for (int j = 0; j < errorList.length; j++) {
System.out.println(errorList[j]);
}
}
}
}
queryRally and createInRally functions use Rally rest client to fetch and create the required projects and associated attributes like releases, iterations etc.
After executing CreateResponse beInitiativeCreateResponse = createInRally("portfolioitem/beinitiative", BeInitiativeCreateObject); when it's trying to execute if(beInitiativeCreateResponse.wasSuccessful()) it is instead going to else block and thus printing the below mentioned error.
An unexpected error has occurred.We have recorded this error and will begin to investigate it. In the meantime, if you would like to speak with our Support Team, please reference the information below:java.lang.NullPointerException2017-12-05 11:01 AM PST America/Los_Angeles
But the important point that is when trying to migrate projects and it's related attributes like release/iterations etc. withing same Rally workspace the above piece of code works just fine.
Update1:
While analysing the issue I made the following observations -
The workspace in which I am trying to create the BeInitiative doesn't have BEinitiative, Be Feature, CPM Feature options in Portfolio items dropdown. Rather it has Theme, Initiative and Feature options in it.
Therefore, I think I was getting the previouly mentioned error. Now I made the following changes to the code.
CreateResponse beInitiativeCreateResponse = createInRally("portfolioitem/theme", themeCreateObject);
So now instead of creating the BEInitiative I am trying to create the theme only in new workspace but getting the following error -
Requested type name \"/portfolioitem/theme\" is unknown.
The object that i am passing to CreateResponse function is -
{"Name":"xyz","Project":"/project/1804","Workspace":"/workspace/139"}
Also code for createInRally function is as mentioned below -
public CreateResponse createInRally( String query, JsonObject object) throws IOException{
CreateRequest createRequest = new CreateRequest(query, object);
CreateResponse createResponse = restApi.create(createRequest);
return createResponse;
}
The Unknown Type error was occurring as a result of not passing the workspace's object id in which we were trying to create the portfolio item.
So after modifying the createInRally function to include the workspace object id we were able to create the initiative portfolio item.
The modified createInRally function is as shown below-
CreateRequest createRequest = new CreateRequest(query, object);
createRequest.addParam("workspace", "/workspace/1333333333");
CreateResponse createResponse = restApi.create(createRequest);
return createResponse;
So this is definitely an error in the web services api. You should never get 500 responses with an unhandled nullpointer. My initial guess is that when you're creating your new object some field on it is still referencing an object in the old workspace, and when we try to correctly hook up all the associations it fails to read one of those objects in the new workspace. Can you provide some more information about what your actual object you're sending to create looks like? Specifically what object relationships are you including (that may not be valid in the new workspace)?
I have found that if we pass the definition ConversationPosts it will create the comments in Discussion tab. But how to add a list of Conversations to the discussion in Rally Java rest API
JsonObject newDefect = new JsonObject();
newDefect.addProperty("Type", "ConversationPost");
newDefect.addProperty("Text", "Test Comment 2");
newDefect.addProperty("Artifact",defectReference);
newDefect.addProperty("User", userRef);
CreateRequest createRequest = new CreateRequest("ConversationPost", defectObject);
CreateResponse createResponse = rallyRestAPI.create(createRequest);
If we need to add 2 comments say "Test Comment 1" ,"Test Comment 2" to the same defect how this can be done in a single execution
Unfortunately Rally's WSAPI doesn't have any batch create/update endpoints so you'll just have to create your conversation post items one at a time. Your code above looks good.
String[] comments = {"Test Comment 1", "Test Comment 2"};
for(String comment : comments) {
JsonObject newDefect = new JsonObject();
newDefect.addProperty("Type", "ConversationPost");
newDefect.addProperty("Text", comment);
newDefect.addProperty("Artifact",defectReference);
newDefect.addProperty("User", userRef);
CreateRequest createRequest = new CreateRequest("ConversationPost", defectObject);
CreateResponse createResponse = rallyRestAPI.create(createRequest);
}
I'm developing in java / groovy and am new to the Rally API, I begun using it last week. I want to be able to use the REST API to create a new Test Case Result. On friday (its monday when I wrote this), I got it working using the example below, putting in the data I wanted using arguments into the method. I found this example on another website.
Today when I ran the code, and I dont think I changed anything, I keep getting "ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 1390; received: 1389).
I rewrote the code again, this time not changing anything from the example just to try and get it working again, and I get the same exception. Heres the code I'm using:
public static void createTestCaseResults(){
// Create and configure a new instance of RallyRestApi
RallyRestApi restApi = new RallyRestApi(new URI("https://rally1.rallydev.com"),"username#company.com", "Password");
restApi.setWsapiVersion("1.36");
restApi.setApplicationName("Add Test Case Result");
//Query User
QueryRequest userRequest = new QueryRequest("User");
userRequest.setFetch(new Fetch("UserName", "Subscription", "DisplayName"));
userRequest.setQueryFilter(new QueryFilter("UserName", "=", "username#company.com"));
QueryResponse userQueryResponse = restApi.query(userRequest);
JsonArray userQueryResults = userQueryResponse.getResults();
JsonElement userQueryElement = userQueryResults.get(0);
JsonObject userQueryObject = userQueryElement.getAsJsonObject();
String userRef = userQueryObject.get("_ref").getAsString();
// Query for Test Case to which we want to add results
QueryRequest testCaseRequest = new QueryRequest("TestCase");
testCaseRequest.setFetch(new Fetch("FormattedID","Name"));
testCaseRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "TC7562"));
QueryResponse testCaseQueryResponse = restApi.query(testCaseRequest);
JsonObject testCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
String testCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();
try{
//Add a Test Case Result
System.out.println("Creating Test Case Result...");
JsonObject newTestCaseResult = new JsonObject();
newTestCaseResult.addProperty("Verdict", "Pass");
newTestCaseResult.addProperty("Date", "2012-06-12T18:00:00.000Z");
newTestCaseResult.addProperty("Notes", "Automated Selenium Test Runs");
newTestCaseResult.addProperty("Build", "2012.05.31.0020101");
newTestCaseResult.addProperty("Tester", userRef);
newTestCaseResult.addProperty("TestCase", testCaseRef);
CreateRequest createRequest = new CreateRequest("testcaseresult", newTestCaseResult);
CreateResponse createResponse = restApi.create(createRequest);
if(createResponse.wasSuccessful()){
println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
//Read Test Case
String ref = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Test Case Result %s...", ref));
GetRequest getRequest = new GetRequest(ref);
getRequest.setFetch(new Fetch("Date", "Verdict"));
GetResponse getResponse = restApi.get(getRequest);
JsonObject obj = getResponse.getObject();
println(String.format("Read Test Case Result. Date = %s, Verdict = %s", obj.get("Date").getAsString(), obj.get("Verdict").getAsString()));
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating Test Case: ");
for (int i=0; i<createErrors.length;i++) {
System.out.println(createErrors[i]);
}
}
}
finally{
restApi.close()
}
}
Appreciate any help with this. Thanks. :)
Is this still happening for you today? I just tried your code on both rally1 and our demo system, and it works reliably every time (only changed username and password, and the test case formatted id).
As a possible next step, I'd set a breakpoint in RallyRestApi.doRequest where the server response code is checked and see what additional information was available - for example, the response code, and the body and headers for the response.
This very well may be a bug in the underlying Apache HttpComponents library. I just upgraded to the latest 4.2.1 components. Would you mind giving the new 1.0.2 jar a try?
https://github.com/RallyTools/RallyRestToolkitForJava
Update:
This has been fixed with the 1.0.4 release of the toolkit today:
https://github.com/downloads/RallyTools/RallyRestToolkitForJava/rally-rest-api-1.0.4.jar
I am trying to add attachment to QC Test LAB Test Case run from my Java code using Com4J API. I was able to create a successful run, however while adding attachments below code is throwing invalid parameter for "IAttachment attach = attachfac.addItem(null).queryInterface(IAttachment.class);". In this case additem is expecting Java Item Object. I also tried to pass addItem(""), but then attach.Type(1) is failing with reason:- Attachment Type cannot be changed. Could anyone please help me with this:
IBaseFactory obj2 = testset.tsTestFactory().queryInterface(IBaseFactory.class);
IList tstestlist = obj2.newList("");
for(Com4jObject obj3:tstestlist){
ITSTest tstest = obj3.queryInterface(ITSTest.class);
if(tstest.name().contentEquals("[1]TC1")){
System.out.println("TC found");
IRunFactory runfactory = tstest.runFactory().queryInterface(IRunFactory.class);
IRun run=runfactory.addItem("RunNew").queryInterface(IRun.class);
run.status("Passed");
IAttachmentFactory attachfac = run.attachments().queryInterface(IAttachmentFactory.class);
IAttachment attach = attachfac.addItem("").queryInterface(IAttachment.class);
attach.type(1);
attach.fileName("Path to File TC1");
attach.post();
run.post();.
String fileName = new File(Attachment).getName();
String folderName = new File(Attachment).getParent();
try{
IAttachmentFactory attachfac = tsteststeps.attachments().queryInterface(IAttachmentFactory.class);
IAttachment attach = attachfac.addItem(fileName).queryInterface(IAttachment.class);
IExtendedStorage extAttach = attach.attachmentStorage().queryInterface(IExtendedStorage.class);
extAttach.clientPath(folderName);
extAttach.save(fileName, true);
attach.description(Actual);
attach.post();
attach.refresh();
}catch(Exception e) {
System.out.println("QC Exceptione : "+e.getMessage());
}