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 pathCreateUpdateDeleteLoop.java
More file actions
Latest commit
196 lines (169 loc) · 9.82 KB
/
Copy pathCreateUpdateDeleteLoop.java
File metadata and controls
196 lines (169 loc) · 9.82 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
importjava.io.IOException;
importjava.net.URI;
importjava.net.URISyntaxException;
importjava.util.ArrayList;
importjava.util.List;
importcom.google.gson.JsonObject;
importcom.rallydev.rest.RallyRestApi;
importcom.rallydev.rest.request.GetRequest;
importcom.rallydev.rest.response.GetResponse;
importcom.rallydev.rest.request.QueryRequest;
importcom.rallydev.rest.response.QueryResponse;
importcom.rallydev.rest.util.Fetch;
importcom.rallydev.rest.util.QueryFilter;
importcom.rallydev.rest.request.CreateRequest;
importcom.rallydev.rest.response.CreateResponse;
importcom.rallydev.rest.request.DeleteRequest;
importcom.rallydev.rest.response.DeleteResponse;
importcom.rallydev.rest.util.Ref;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.text.DateFormat;
importjava.io.FileOutputStream;
importjava.io.PrintStream;
publicclassCreateUpdateDeleteInLoop {
publicstaticvoidmain(String[] args) throwsURISyntaxException, IOException, InterruptedException {
PrintStreampst = newPrintStream(newFileOutputStream("Log.txt", true));
System.setOut(pst);
System.setErr(pst);
System.out.println("#########################" + newDate() + "#########################");
Stringhost = "https://rally1.rallydev.com";
StringapiKey = "_abc123";
StringapplicationName = "Nick multi thread create-update-delete loop";
RallyRestApirestApi = newRallyRestApi(newURI(host), apiKey);
restApi.setApplicationName(applicationName);
restApi.setProxy(newURI("http://127.0.0.1:8080"));
//get current user
GetRequestgetRequest = newGetRequest("/user");
GetResponsegetResponse = restApi.get(getRequest);
JsonObjectcurrentUser = getResponse.getObject();
StringcurrentUserName = currentUser.get("_refObjectName").getAsString();
StringcurrentUserRef = currentUser.get("_ref").getAsString();
System.out.println("current user: " + currentUserName + currentUserRef);
//get workspaces where the current user has permission
List<String> workspaces = newArrayList<String>();
QueryRequestworkspacePermissionsRequest = newQueryRequest("WorkspacePermissions");
workspacePermissionsRequest.setQueryFilter(newQueryFilter("User", "=", currentUserRef));
workspacePermissionsRequest.setFetch(newFetch("Workspace","ObjectID"));
QueryResponseworkspacePermissionsResponse = restApi.query(workspacePermissionsRequest);
intnumberOfPermissoins = workspacePermissionsResponse.getTotalResultCount();
System.out.println(numberOfPermissoins);
if (numberOfPermissoins > 0) {
for (inti = 0; i < numberOfPermissoins; i++) {
JsonObjectworkspacePermissionObject = workspacePermissionsResponse.getResults().get(i).getAsJsonObject();
StringworkspaceRef = workspacePermissionObject.get("Workspace").getAsJsonObject().get("ObjectID").getAsString();
workspaces.add(workspaceRef);
}
}
System.out.println("workspaces " + workspaces);
classMyRunnableimplementsRunnable {
privateStringoid;
publicMyRunnable(Stringoid) {
this.oid = oid;
}
publicvoidrun() {
try{
getRally(this.oid);
}
catch (Exceptione){
e.printStackTrace();
}
}
}
Thread [] threads = newThread[numberOfPermissoins];
for (inti = 0; i < threads.length; i++)
{
threads[i] = newThread(newMyRunnable(workspaces.get(i)));
threads[i].start();
threads[i].join();
}
if (restApi != null) {
restApi.close();
}
}
privatestaticvoidgetRally(Stringoid) throwsException
{
try {
intcounter = 3;
while(counter > 0){
System.out.println("LOOP: " + counter);
DateFormatiso = newSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
List<String> parentStories = newArrayList<String>();
List<String> features = newArrayList<String>();
Stringhost = "https://rally1.rallydev.com";
StringapiKey = "_abc123";
RallyRestApirestApi = newRallyRestApi(newURI(host), apiKey);
restApi.setProxy(newURI("http://127.0.0.1:8080"));
StringworkspaceRef = "/workspace/" + oid;
QueryRequestprojectRequest = newQueryRequest("Project");
projectRequest.setFetch(newFetch("Name"));
projectRequest.setWorkspace(workspaceRef);
QueryResponseprojectQueryResponse = restApi.query(projectRequest);
JsonObjectprojectJsonObject = projectQueryResponse.getResults().get(0).getAsJsonObject();
StringprojectRef = projectJsonObject.get("_ref").getAsString();
System.out.println(String.format("------ Creating items in project %s",projectJsonObject.get("Name").getAsString()));
for (intx=0; x<1; x++) {
JsonObjectnewFeature = newJsonObject();
newFeature.addProperty("Name", "feature: " + iso.format(newDate()));
newFeature.addProperty("Project", projectRef);
CreateRequestcreateRequest = newCreateRequest("portfolioitem/feature", newFeature);
CreateResponsecreateResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("FormattedID").getAsString()));
System.out.println(String.format(createResponse.getObject().get("_ref").getAsString()));
features.add(createResponse.getObject().get("_ref").getAsString());
//Read feature
StringfeatureRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading feature %s...",featureRef));
for (inty=0; y<2; y++){
JsonObjectnewStory = newJsonObject();
newStory.addProperty("Name", "story: " + iso.format(newDate()));
newStory.addProperty("Project", projectRef);
newStory.addProperty("PortfolioItem", featureRef);
CreateRequestcreateRequest2 = newCreateRequest("hierarchicalrequirement", newStory);
CreateResponsecreateResponse2 = restApi.create(createRequest2);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse2.getObject().get("FormattedID").getAsString()));
System.out.println(String.format(createResponse2.getObject().get("_ref").getAsString()));
parentStories.add(createResponse2.getObject().get("_ref").getAsString());
//Read story
StringstoryRef = Ref.getRelativeRef(createResponse2.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading child story %s...",storyRef));
System.out.println("Creating a child story...");
for (intz=0; z<2; z++){
JsonObjectnewChildStory = newJsonObject();
newChildStory.addProperty("Name", "story: " + iso.format(newDate()));
newChildStory.addProperty("Project", projectRef);
newChildStory.addProperty("Parent", storyRef);
CreateRequestcreateRequest3 = newCreateRequest("hierarchicalrequirement", newChildStory);
CreateResponsecreateResponse3 = restApi.create(createRequest3);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse3.getObject().get("FormattedID").getAsString()));
System.out.println(String.format(createResponse3.getObject().get("_ref").getAsString()));
//Read child story
StringchildStoryRef = Ref.getRelativeRef(createResponse3.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading child story %s...",childStoryRef));
}
}
}
}
}
}
for (ints=0; s<parentStories.size();s++) {
DeleteRequestdeleteStoriesRequest = newDeleteRequest(parentStories.get(s));
System.out.println("Deleting ...." + parentStories.get(s));
DeleteResponsedeleteStoriesResponse = restApi.delete(deleteStoriesRequest);
}
for (intf=0; f<features.size();f++) {
DeleteRequestdeleteFeaturesRequest = newDeleteRequest(features.get(f));
System.out.println("Deleting ...." + features.get(f));
DeleteResponsedeleteFeaturesResponse = restApi.delete(deleteFeaturesRequest);
}
counter--;
}
}
finally{
System.out.println("done with thread " + oid + "\n-------------------------");
}
}
}