Uh oh!
There was an error while loading. Please reload this page.
forked from nmusaelian-rally/rally-java-rest-apps
- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCreateTestResultAddAttachment.java
More file actions
Latest commit
152 lines (134 loc) · 7.79 KB
/
Copy pathCreateTestResultAddAttachment.java
File metadata and controls
152 lines (134 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
importjava.io.IOException;
importjava.io.RandomAccessFile;
importjava.net.URI;
importjava.net.URISyntaxException;
importorg.apache.commons.codec.binary.Base64;
importcom.google.gson.JsonArray;
importcom.google.gson.JsonElement;
importcom.google.gson.JsonObject;
importcom.rallydev.rest.RallyRestApi;
importcom.rallydev.rest.request.CreateRequest;
importcom.rallydev.rest.request.QueryRequest;
importcom.rallydev.rest.util.Fetch;
importcom.rallydev.rest.response.CreateResponse;
importcom.rallydev.rest.response.QueryResponse;
importcom.rallydev.rest.util.QueryFilter;
publicclassCreateTestResultAddAttachment{
publicstaticvoidmain(String[] args) throwsURISyntaxException, IOException {
Stringhost = "https://rally1.rallydev.com";
StringapiKey = "_abc123"; //nick@chicago
StringworkspaceRef = "/workspace/12352608129";
StringapplicationName = "Nick:createTCR-addAttachment";
RallyRestApirestApi = newRallyRestApi(newURI(host),apiKey);
restApi.setApplicationName(applicationName);
//Read User
QueryRequestuserRequest = newQueryRequest("User");
userRequest.setFetch(newFetch("UserName", "Subscription", "DisplayName", "SubscriptionAdmin"));
userRequest.setQueryFilter(newQueryFilter("UserName", "=", "nick@denver.com"));
QueryResponseuserQueryResponse = restApi.query(userRequest);
JsonArrayuserQueryResults = userQueryResponse.getResults();
JsonElementuserQueryElement = userQueryResults.get(0);
JsonObjectuserQueryObject = userQueryElement.getAsJsonObject();
StringuserRef = userQueryObject.get("_ref").getAsString();
System.out.println(userRef);
// Query for Test Case to which we want to add results
QueryRequesttestCaseRequest = newQueryRequest("TestCase");
testCaseRequest.setFetch(newFetch("FormattedID","Name"));
testCaseRequest.setWorkspace(workspaceRef);
testCaseRequest.setQueryFilter(newQueryFilter("FormattedID", "=", "TC1180"));
QueryResponsetestCaseQueryResponse = restApi.query(testCaseRequest);
JsonObjecttestCaseJsonObject = testCaseQueryResponse.getResults().get(0).getAsJsonObject();
StringtestCaseRef = testCaseQueryResponse.getResults().get(0).getAsJsonObject().get("_ref").getAsString();
try {
//Add a Test Case Result
System.out.println(testCaseRef);
System.out.println("Creating Test Case Result...");
JsonObjectnewTestCaseResult = newJsonObject();
newTestCaseResult.addProperty("Verdict", "Pass");
newTestCaseResult.addProperty("Date", "2016-02-08T00:00:00.000Z");
newTestCaseResult.addProperty("Notes", "Some Scheduled Test");
newTestCaseResult.addProperty("Build", "4.0");
newTestCaseResult.addProperty("Tester", userRef);
newTestCaseResult.addProperty("TestCase", testCaseRef);
/*
Error if workspace is not specified below while the user that makes a request (whose ApiKey is used in the code) has a default workspace different from the destination workspace
Could not set value for Test Case: Cannot connect object to value, Test Case value is in a different workspace. [object workspace OID=1011574887, value workspace OID=12352608129
*/
newTestCaseResult.addProperty("Workspace", workspaceRef);
CreateRequestcreateRequest = newCreateRequest("testcaseresult", newTestCaseResult);
CreateResponsecreateResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
addAttachment(restApi, createResponse.getObject().get("_ref").getAsString(), userRef, workspaceRef);
}
else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error occurred creating Test Case Result: ");
for (inti = 0; i < createErrors.length; i++) {
System.out.println(createErrors[i]);
}
}
} finally {
//Release all resources
restApi.close();
}
}
privatestaticvoidaddAttachment(RallyRestApirestApi, StringtestCaseResultRef, StringuserRef, StringworkspaceRef) throwsURISyntaxException,IOException {
System.out.println("Created " + testCaseResultRef);
StringimageFilePath = "/Users/nmusaelian/Desktop/";
StringimageFileName = "goodnight-moon_3.jpg";
StringfullImageFile = imageFilePath + imageFileName;
StringimageBase64String;
longattachmentSize;
// Open file
RandomAccessFilemyImageFileHandle = newRandomAccessFile(fullImageFile, "r");
try {
longlongLength = myImageFileHandle.length();
longmaxLength = 5000000;
if (longLength >= maxLength) thrownewIOException("File size >= 5 MB Upper limit for Rally.");
intfileLength = (int) longLength;
// Read file and return data
byte[] fileBytes = newbyte[fileLength];
myImageFileHandle.readFully(fileBytes);
imageBase64String = Base64.encodeBase64String(fileBytes);
attachmentSize = fileLength;
// First create AttachmentContent from image string
JsonObjectmyAttachmentContent = newJsonObject();
myAttachmentContent.addProperty("Content", imageBase64String);
CreateRequestattachmentContentCreateRequest = newCreateRequest("AttachmentContent", myAttachmentContent);
//java.lang.NullPointerException if workspace parameter is not set as below
attachmentContentCreateRequest.addParam("workspace", workspaceRef);
CreateResponseattachmentContentResponse = restApi.create(attachmentContentCreateRequest);
StringmyAttachmentContentRef = attachmentContentResponse.getObject().get("_ref").getAsString();
System.out.println("Attachment Content created: " + myAttachmentContentRef);
// Now create the Attachment itself
JsonObjectmyAttachment = newJsonObject();
myAttachment.addProperty("TestCaseResult", testCaseResultRef);
myAttachment.addProperty("Content", myAttachmentContentRef);
myAttachment.addProperty("Name", "AttachmentFromREST.png");
myAttachment.addProperty("Description", "Attachment From REST");
myAttachment.addProperty("ContentType","image/png");
myAttachment.addProperty("Size", attachmentSize);
myAttachment.addProperty("User", userRef);
CreateRequestattachmentCreateRequest = newCreateRequest("Attachment", myAttachment);
//java.lang.NullPointerException if workspace parameter is not set as below
attachmentCreateRequest.addParam("workspace", workspaceRef);
CreateResponseattachmentResponse = restApi.create(attachmentCreateRequest);
StringmyAttachmentRef = attachmentResponse.getObject().get("_ref").getAsString();
System.out.println("Attachment created: " + myAttachmentRef);
if (attachmentResponse.wasSuccessful()) {
System.out.println("Successfully created Attachment");
} else {
String[] attachmentContentErrors;
attachmentContentErrors = attachmentResponse.getErrors();
System.out.println("Error occurred creating Attachment: ");
for (intj=0; j<attachmentContentErrors.length;j++) {
System.out.println(attachmentContentErrors[j]);
}
}
}catch (Exceptione) {
System.out.println("Exception occurred while attempting to create Content and/or Attachment: ");
e.printStackTrace();
}
}
}