@@ -16,8 +16,6 @@ namespace Rally.RestApi.Test
1616 [ TestClass ]
1717 public class RallyRestApiTest
1818 {
19- private static string defectOid ;
20-
2119 public static RallyRestApi GetRallyRestApi ( string userName = "" , string password = "" ,
2220 string server = "" , string wsapiVersion = "" )
2321 {
@@ -116,12 +114,16 @@ public void CreateTest2x()
116114
117115 private void AssertCanCreate ( RallyRestApi restApi )
118116 {
119- CreateResult response = CreateDefect ( restApi ) ;
117+ var dynamicJson = new DynamicJsonObject ( ) ;
118+ dynamicJson [ "Name" ] = "C# Json Rest Toolkit Test Defect" ;
119+ CreateResult response = restApi . Create ( "defect" , dynamicJson ) ;
120120 Assert . AreEqual ( 0 , response . Errors . Count ) ;
121121 Assert . IsTrue ( response . Reference . ToLower ( ) . Contains ( "defect" ) ) ;
122122 dynamic testDefect = restApi . GetByReference ( response . Reference ) ;
123- Assert . AreEqual ( "C# Json Rest Toolkit Test Defect" , testDefect . Name ) ;
124- defectOid = Ref . GetOidFromRef ( response . Reference ) ;
123+ Assert . AreEqual ( dynamicJson [ "Name" ] , testDefect . Name ) ;
124+
125+ // Now delete it
126+ TestHelperDeleteItem ( restApi , response . Reference ) ;
125127 }
126128
127129 [ TestMethod ]
@@ -149,44 +151,54 @@ private void AssertCreateFailure(RallyRestApi restApi)
149151 Assert . IsFalse ( creationResult . Success ) ;
150152 }
151153
152- [ TestMethod ]
153- public void AddToCollection2x ( )
154- {
155- RallyRestApi restApi = GetRallyRestApi2x ( ) ;
156- var itemRef = CreateDefect ( restApi ) . Reference ;
157- DynamicJsonObject newTask = new DynamicJsonObject ( ) ;
158- newTask [ "Name" ] = "New Task Added via collection" ;
159- NameValueCollection parameters = new NameValueCollection ( ) ;
160- parameters . Add ( "fetch" , "FormattedID" ) ;
161- OperationResult result = restApi . AddToCollection ( itemRef , "Tasks" , new List < DynamicJsonObject > ( ) { newTask } , parameters ) ;
162- Assert . IsTrue ( result . Success ) ;
163- Assert . AreEqual ( 1 , result . Results . Count ) ;
164- Assert . IsNotNull ( result . Results [ 0 ] [ "FormattedID" ] ) ;
165- }
166-
167- [ TestMethod ]
168- public void RemoveFromCollection2x ( )
169- {
170- RallyRestApi restApi = GetRallyRestApi2x ( ) ;
171- DynamicJsonObject newStory = new DynamicJsonObject ( ) ;
172- newStory [ "Name" ] = "Test Story" ;
173- var itemRef = restApi . Create ( "hierarchicalrequirement" , newStory ) . Reference ;
174- DynamicJsonObject newDefect = new DynamicJsonObject ( ) ;
175- newDefect [ "Name" ] = "New Defect Added via collection" ;
176- newDefect [ "Requirement" ] = itemRef ;
177- CreateResult newTaskResult = restApi . Create ( "defect" , newDefect ) ;
178- DynamicJsonObject story = restApi . GetByReference ( itemRef , "Defects" ) ;
179- Assert . AreEqual ( 1 , story [ "Defects" ] [ "Count" ] ) ;
180- DynamicJsonObject taskToRemove = new DynamicJsonObject ( ) ;
181- taskToRemove [ "_ref" ] = newTaskResult . Reference ;
182- OperationResult result = restApi . RemoveFromCollection ( itemRef , "Defects" , new List < DynamicJsonObject > ( ) { taskToRemove } , new NameValueCollection ( ) ) ;
183- Assert . IsTrue ( result . Success ) ;
184- Assert . AreEqual ( 0 , result . Results . Count ) ;
185- story = restApi . GetByReference ( itemRef , "Defects" ) ;
186- Assert . AreEqual ( 0 , story [ "Defects" ] [ "Count" ] ) ;
187- }
188-
189- [ TestMethod ]
154+ [ TestMethod ]
155+ public void AddToCollection2x ( )
156+ {
157+ RallyRestApi restApi = GetRallyRestApi2x ( ) ;
158+ var itemRef = TestHelperCreateDefect ( restApi ) ;
159+ DynamicJsonObject newTask = new DynamicJsonObject ( ) ;
160+ newTask [ "Name" ] = "New Task Added via collection" ;
161+ NameValueCollection parameters = new NameValueCollection ( ) ;
162+ parameters . Add ( "fetch" , "FormattedID" ) ;
163+ OperationResult result = restApi . AddToCollection ( itemRef , "Tasks" , new List < DynamicJsonObject > ( ) { newTask } , parameters ) ;
164+ Assert . IsTrue ( result . Success ) ;
165+ Assert . AreEqual ( 1 , result . Results . Count ) ;
166+ Assert . IsNotNull ( result . Results [ 0 ] [ "FormattedID" ] ) ;
167+
168+ // Now delete it
169+ TestHelperDeleteItem ( restApi , itemRef ) ;
170+ }
171+
172+ [ TestMethod ]
173+ public void RemoveFromCollection2x ( )
174+ {
175+ RallyRestApi restApi = GetRallyRestApi2x ( ) ;
176+ DynamicJsonObject newStory = new DynamicJsonObject ( ) ;
177+ newStory [ "Name" ] = "Test Story" ;
178+ var itemRef = restApi . Create ( "hierarchicalrequirement" , newStory ) . Reference ;
179+ DynamicJsonObject newDefect = new DynamicJsonObject ( ) ;
180+ newDefect [ "Name" ] = "New Defect Added via collection" ;
181+ newDefect [ "Requirement" ] = itemRef ;
182+ CreateResult newTaskResult = restApi . Create ( "defect" , newDefect ) ;
183+
184+ DynamicJsonObject story = restApi . GetByReference ( itemRef , "Defects" ) ;
185+ Assert . AreEqual ( 1 , story [ "Defects" ] [ "Count" ] ) ;
186+
187+ DynamicJsonObject taskToRemove = new DynamicJsonObject ( ) ;
188+ taskToRemove [ "_ref" ] = newTaskResult . Reference ;
189+ OperationResult result = restApi . RemoveFromCollection ( itemRef , "Defects" , new List < DynamicJsonObject > ( ) { taskToRemove } , new NameValueCollection ( ) ) ;
190+
191+ Assert . IsTrue ( result . Success ) ;
192+ Assert . AreEqual ( 0 , result . Results . Count ) ;
193+ story = restApi . GetByReference ( itemRef , "Defects" ) ;
194+ Assert . AreEqual ( 0 , story [ "Defects" ] [ "Count" ] ) ;
195+
196+ // Now delete the defect and story
197+ TestHelperDeleteItem ( restApi , newTaskResult . Reference ) ;
198+ TestHelperDeleteItem ( restApi , itemRef ) ;
199+ }
200+
201+ [ TestMethod ]
190202 public void Delete1x ( )
191203 {
192204 RallyRestApi restApi = GetRallyRestApi1x ( ) ;
@@ -200,27 +212,14 @@ public void Delete2x()
200212 AssertCanDelete ( restApi ) ;
201213 }
202214
203- private CreateResult CreateDefect ( RallyRestApi restApi )
204- {
205- var dynamicJson = new DynamicJsonObject ( ) ;
206- dynamicJson [ "Name" ] = "C# Json Rest Toolkit Test Defect" ;
207- return restApi . Create ( "defect" , dynamicJson ) ;
208- }
209-
210215 private void AssertCanDelete ( RallyRestApi restApi , bool includeFullData = false )
211216 {
212- var dynamicJson = new DynamicJsonObject ( ) ;
213- dynamicJson [ "Name" ] = "C# Json Rest Toolkit Test Defect" ;
214- if ( includeFullData )
215- {
216- dynamicJson [ "Owner" ] = restApi . GetCurrentUser ( ) [ "_ref" ] ;
217- dynamicJson [ "Package" ] = "Package A" ;
218- }
219- CreateResult response = restApi . Create ( "defect" , dynamicJson ) ;
220- Assert . AreEqual ( 0 , response . Errors . Count ) ;
221- Assert . IsTrue ( response . Reference . ToLower ( ) . Contains ( "defect" ) ) ;
222- OperationResult deleteResponse = restApi . Delete ( Ref . GetRelativeRef ( response . Reference ) ) ;
223- dynamic testDefectEmpty = restApi . GetByReference ( response . Reference ) ;
217+ // Create test defect
218+ var defect = TestHelperCreateDefect ( restApi , includeFullData ) ;
219+ var defectOid = Ref . GetOidFromRef ( defect ) ;
220+
221+ OperationResult deleteResponse = restApi . Delete ( Ref . GetRelativeRef ( defect ) ) ;
222+ dynamic testDefectEmpty = restApi . GetByReference ( defect ) ;
224223 Assert . IsNull ( testDefectEmpty ) ;
225224 }
226225
@@ -240,21 +239,59 @@ public void Update2x()
240239
241240 private void AssertCanUpdate ( RallyRestApi restApi )
242241 {
242+ // Create test defect
243+ var defect = TestHelperCreateDefect ( restApi ) ;
244+ var defectOid = Ref . GetOidFromRef ( defect ) ;
245+
243246 var dynamicJson = new DynamicJsonObject ( ) ;
244247 dynamicJson [ "Name" ] = "Dont delete me please " + DateTime . Now . Second ;
245248 OperationResult response = restApi . Update ( "Defect" , defectOid , dynamicJson ) ;
246249 Assert . AreEqual ( 0 , response . Errors . Count ) ;
247250 dynamic updateDefect = restApi . GetByReference ( "/Defect/" + defectOid + ".js" ) ;
248251 Assert . AreEqual ( dynamicJson [ "Name" ] , updateDefect . Name ) ;
252+
253+ // Now delete it
254+ TestHelperDeleteItem ( restApi , defect ) ;
255+ }
256+
257+ private string TestHelperCreateDefect ( RallyRestApi restApi , bool includeFullData = false )
258+ {
259+ var dynamicJson = new DynamicJsonObject ( ) ;
260+ dynamicJson [ "Name" ] = "C# Json Rest Toolkit Test Defect" ;
261+ if ( includeFullData )
262+ {
263+ dynamicJson [ "Owner" ] = restApi . GetCurrentUser ( ) [ "_ref" ] ;
264+ dynamicJson [ "Package" ] = "Package A" ;
265+ }
266+
267+ CreateResult response = restApi . Create ( "defect" , dynamicJson ) ;
268+ Assert . AreEqual ( 0 , response . Errors . Count ) ;
269+ Assert . IsTrue ( response . Reference . ToLower ( ) . Contains ( "defect" ) ) ;
270+
271+ return response . Reference ;
272+ }
273+
274+ private void TestHelperDeleteItem ( RallyRestApi restApi , string reference )
275+ {
276+ OperationResult deleteResponse = restApi . Delete ( Ref . GetRelativeRef ( reference ) ) ;
277+ dynamic testEmpty = restApi . GetByReference ( reference ) ;
278+ Assert . IsNull ( testEmpty ) ;
249279 }
250280
251281 [ TestMethod ]
252282 public void GetByReferenceTest ( )
253283 {
254284 RallyRestApi restApi = GetRallyRestApi ( ) ;
255- AssertCanCreate ( restApi ) ;
285+
286+ // Create test defect
287+ var defect = TestHelperCreateDefect ( restApi ) ;
288+ var defectOid = Ref . GetOidFromRef ( defect ) ;
289+
256290 dynamic response = restApi . GetByReference ( "/Defect/" + defectOid + ".js" ) ;
257291 Assert . AreEqual ( defectOid , response . ObjectID . ToString ( ) ) ;
292+
293+ // Now delete it
294+ TestHelperDeleteItem ( restApi , defect ) ;
258295 }
259296
260297 [ TestMethod ]
@@ -300,8 +337,6 @@ public void GetByReferenceSubscriptionTest()
300337 Assert . IsNotNull ( response . ObjectID ) ;
301338 }
302339
303-
304-
305340 [ TestMethod ]
306341 public void TestAttribute1x ( )
307342 {
@@ -322,8 +357,8 @@ public void TestAttribute2x()
322357 public void FormatCreateString ( )
323358 {
324359 RallyRestApi restApi = GetRallyRestApi ( ) ;
325- NameValueCollection parameters = new NameValueCollection ( ) ;
326- parameters [ "fetch" ] = "Name" ;
360+ NameValueCollection parameters = new NameValueCollection ( ) ;
361+ parameters [ "fetch" ] = "Name" ;
327362 Uri result = restApi . FormatCreateUri ( "defect" , parameters ) ;
328363 var expected = new Uri ( Settings . Default . TestServer + "/slm/webservice/" + RallyRestApi . DEFAULT_WSAPI_VERSION + "/defect/create.js?fetch=Name" ) ;
329364 Assert . AreEqual ( expected , result ) ;
@@ -333,9 +368,9 @@ public void FormatCreateString()
333368 public void FormatUpdateString ( )
334369 {
335370 RallyRestApi restApi = GetRallyRestApi ( ) ;
336- NameValueCollection parameters = new NameValueCollection ( ) ;
337- parameters [ "fetch" ] = "Name" ;
338- Uri result = restApi . FormatUpdateUri ( "defect" , "2121901027" , parameters ) ;
371+ NameValueCollection parameters = new NameValueCollection ( ) ;
372+ parameters [ "fetch" ] = "Name" ;
373+ Uri result = restApi . FormatUpdateUri ( "defect" , "2121901027" , parameters ) ;
339374 var expected = new Uri ( Settings . Default . TestServer + "/slm/webservice/" + RallyRestApi . DEFAULT_WSAPI_VERSION + "/defect/2121901027.js?fetch=Name" ) ;
340375 Assert . AreEqual ( expected , result ) ;
341376 }
0 commit comments