/// <summary>
		/// Get the value of the specified member
		/// Equivalent to using [name].
		/// </summary>
		/// <param name="name">The specified member</param>
		/// <returns>The value of the specified member</returns>
		private dynamic GetMember(string name)
		{
			// Prevent exceptions
			if (!Dictionary.ContainsKey(name))
				return null;

			var result = Dictionary[name];

			if (result is IDictionary<string, object>)
			{
				result = new DynamicJsonObject(result as IDictionary<string, object>);
			}
			else if (result is ArrayList)
			{
				var list = new ArrayList();
				foreach (var i in result as ArrayList)
				{
					if (i is IDictionary<string, object>)
					{
						list.Add(new DynamicJsonObject(i as IDictionary<string, object>));
					}
					else
						list.Add(i);
				}
				result = list;
			}

			return result;
		}
Exemple #2
0
        /// <summary>
        /// Get the value of the specified member
        /// Equivalent to using [name].
        /// </summary>
        /// <param name="name">The specified member</param>
        /// <returns>The value of the specified member</returns>
        private dynamic GetMember(string name)
        {
            // Prevent exceptions
            if (!Dictionary.ContainsKey(name))
            {
                return(null);
            }

            var result = Dictionary[name];

            if (result is IDictionary <string, object> )
            {
                result = new DynamicJsonObject(result as IDictionary <string, object>);
            }
            else if (result is ArrayList)
            {
                var list = new ArrayList();
                foreach (var i in result as ArrayList)
                {
                    if (i is IDictionary <string, object> )
                    {
                        list.Add(new DynamicJsonObject(i as IDictionary <string, object>));
                    }
                    else
                    {
                        list.Add(i);
                    }
                }
                result = list;
            }

            return(result);
        }
		public void ExampleMethodText()
		{
			string username = "******";
			string password = "******";
			// Initialize the REST API. You can specify a web service version if needed in the constructor.
			RallyRestApi restApi = new RallyRestApi();
			restApi.Authenticate(username, password, "https://preview.rallydev.com", proxy: null, allowSSO: false);

			//Create an item
			DynamicJsonObject toCreate = new DynamicJsonObject();
			toCreate["Name"] = "My Defect";
			CreateResult createResult = restApi.Create("defect", toCreate);

			//Update the item
			DynamicJsonObject toUpdate = new DynamicJsonObject();
			toUpdate["Description"] = "This is my defect.";
			OperationResult updateResult = restApi.Update(createResult.Reference,
											toUpdate);

			//Get the item
			DynamicJsonObject item = restApi.GetByReference(createResult.Reference);

			//Query for items
			Request request = new Request("defect");
			request.Fetch = new List<string>() { "Name", "Description", "FormattedID" };
			request.Query = new Query("Name", Query.Operator.Equals, "My Defect");
			QueryResult queryResult = restApi.Query(request);
			foreach (var result in queryResult.Results)
			{
				//Process item as needed
			}

			//Delete the item
			OperationResult deleteResult = restApi.Delete(createResult.Reference);
		}
		public void SerializeTestArray()
		{
			var value = new DynamicJsonObject();
			(value as dynamic).array = new object[] { "arrayValue1", 8 };
			var target = new DynamicJsonSerializer();
			const string expected = "{\"array\":[\"arrayValue1\",8]}";
			var actual = target.Serialize(value);
			Assert.AreEqual(expected, actual);
		}
		public void DeserializeTestArrayWithObjects()
		{
			var expected = new DynamicJsonObject();
			var inner = new DynamicJsonObject();
			(inner as dynamic).objVal = 90;
			(expected as dynamic).array = new object[] { "arrayValue1", inner };
			var target = new DynamicJsonSerializer();
			const string json = "{\"array\":[\"arrayValue1\",{\"objVal\":90}]}";
			var actual = target.Deserialize(json);
			Assert.AreEqual(expected, actual);
		}
		public void CloneCollection()
		{
			DynamicJsonObject collection = new DynamicJsonObject();
			collection["_ref"] = "/hierarchicalrequirement/12345/defect.js";
			var request = new Request(collection);
			request.Fetch = new List<string>() { "Name", "FormattedID" };

			var request2 = request.Clone();
			Assert.AreEqual(string.Join(",", request.Fetch),
					string.Join(",", request2.Fetch));

			foreach (var parameterKey in request.Parameters.Keys)
			{
				Assert.AreEqual(request.Parameters[parameterKey],
						request2.Parameters[parameterKey]);
			}
			Assert.AreEqual(request.Endpoint, request2.Endpoint);
		}
        public void DynamicJsonObject_ToDictionary()
        {
            dynamic djo = new DynamicJsonObject();
            djo.int1 = -19;
            djo.decimal1 = 1.21M;
            djo.string1 = "hi";

            var dict = djo.ToDictionary();

            Assert.IsNotNull(dict);
            Assert.AreEqual(-19, dict["int1"]);
            Assert.AreEqual(1.21M, dict["decimal1"]);
            Assert.AreEqual("hi", dict["string1"]);

            //Make sure the dictionary returned is readonly.
            dict["int1"] = -20; //change the value we got before.
            Assert.AreEqual(-19, djo.ToDictionary()["int1"]);  //reconvert and test again.
        }
		public void DeserializeTest()
		{

			dynamic expected = new DynamicJsonObject();
			expected.int1 = -19;
			expected.decimal1 = 1.21M;
			expected.string1 = "hi";
			dynamic obj1 = new DynamicJsonObject();
			obj1.int1 = 19;
			obj1.string1 = "hi";
			obj1.decimal1 = -1.21M;
			obj1.array1 = new int[0];
			obj1.array2 = new[] { "arrayValue1", "arrayValue1" };
			expected.obj1 = obj1;
			var target = new DynamicJsonSerializer();
			const string json = "{\"int1\":-19,\"decimal1\":1.21,\"string1\":\"hi\",\"obj1\":{\"int1\":19,\"string1\":\"hi\",\"decimal1\":-1.21,\"array1\":[],\"array2\":[\"arrayValue1\", \"arrayValue1\"]}}";
			var actual = target.Deserialize(json);
			Assert.AreEqual<DynamicJsonObject>(expected, actual);
		}
		public void SerializeTestSimpleObject()
		{
			var value = new DynamicJsonObject();
			var target = new DynamicJsonSerializer();
			const string expected = "{}";
			var actual = target.Serialize(value);
			Assert.AreEqual(expected, actual);
		}
		private void AssertCanUpdate(RallyRestApi restApi)
		{
			var dynamicJson = new DynamicJsonObject();
			dynamicJson["Name"] = "Dont delete me please " + DateTime.Now.Second;
			OperationResult response = restApi.Update("Defect", defectOid, dynamicJson);
			Assert.AreEqual(0, response.Errors.Count);
			dynamic updateDefect = restApi.GetByReference("/Defect/" + defectOid + ".js");
			Assert.AreEqual(dynamicJson["Name"], updateDefect.Name);
		}
		private void AssertCanDelete(RallyRestApi restApi, bool includeFullData = false)
		{
			var dynamicJson = new DynamicJsonObject();
			dynamicJson["Name"] = "C# Json Rest Toolkit Test Defect";
			if (includeFullData)
			{
				dynamicJson["Owner"] = restApi.GetCurrentUser()["_ref"];
				dynamicJson["Package"] = "Package A";
			}
			CreateResult response = restApi.Create("defect", dynamicJson);
			Assert.AreEqual(0, response.Errors.Count);
			Assert.IsTrue(response.Reference.ToLower().Contains("defect"));
			OperationResult deleteResponse = restApi.Delete(Ref.GetRelativeRef(response.Reference));
			dynamic testDefectEmpty = restApi.GetByReference(response.Reference);
			Assert.IsNull(testDefectEmpty);
		}
		private void AssertCreateFailure(RallyRestApi restApi)
		{
			var defect = new DynamicJsonObject();
			defect["Name"] = "Sample Defect with invalid field";
			defect["Iteration"] = "Foo";
			CreateResult creationResult = restApi.Create("defect", defect);
			Assert.IsNull(creationResult.Reference);
			Assert.AreEqual(1, creationResult.Errors.Count);
			Assert.IsFalse(creationResult.Success);
		}
		private void AssertCanCreate(RallyRestApi restApi)
		{
			var dynamicJson = new DynamicJsonObject();
			dynamicJson["Name"] = "C# Json Rest Toolkit Test Defect";
			CreateResult response = restApi.Create("defect", dynamicJson);
			Assert.AreEqual(0, response.Errors.Count);
			Assert.IsTrue(response.Reference.ToLower().Contains("defect"));
			dynamic testDefect = restApi.GetByReference(response.Reference);
			Assert.AreEqual(dynamicJson["Name"], testDefect.Name);
			defectOid = Ref.GetOidFromRef(response.Reference);
		}
		public void TestEndpointCollection()
		{
			DynamicJsonObject collection = new DynamicJsonObject();
			collection["_ref"] = "https://rally1.rallydev.com/slm/webservice/v2.0/defect/12345/tasks";

			var request = new Request(collection);

			Assert.AreEqual("/defect/12345/tasks", request.Endpoint);
		}
		private string TestHelperCreateDefect(RallyRestApi restApi, bool includeFullData = false)
		{
			var dynamicJson = new DynamicJsonObject();
			dynamicJson["Name"] = "C# Json Rest Toolkit Test Defect";
			if (includeFullData)
			{
				dynamicJson["Owner"] = restApi.GetCurrentUser()["_ref"];
				dynamicJson["Package"] = "Package A";
			}

			CreateResult response = restApi.Create("defect", dynamicJson);
			Assert.AreEqual(0, response.Errors.Count);
			Assert.IsTrue(response.Reference.ToLower().Contains("defect"));

			return response.Reference;
		}
		private void AssertCanUpdate(RallyRestApi restApi)
		{
			// Create test defect
			var defect = TestHelperCreateDefect(restApi);
			var defectOid = Ref.GetOidFromRef(defect);

			var dynamicJson = new DynamicJsonObject();
			dynamicJson["Name"] = "Dont delete me please " + DateTime.Now.Second;
			OperationResult response = restApi.Update("Defect", defectOid, dynamicJson);
			Assert.AreEqual(0, response.Errors.Count);
			dynamic updateDefect = restApi.GetByReference("/Defect/" + defectOid + ".js");
			Assert.AreEqual(dynamicJson["Name"], updateDefect.Name);

			// Now delete it
			TestHelperDeleteItem(restApi, defect);
		}
		public void RemoveFromCollection2x()
		{
			RallyRestApi restApi = GetRallyRestApi2x();
			DynamicJsonObject newStory = new DynamicJsonObject();
			newStory["Name"] = "Test Story";
			var itemRef = restApi.Create("hierarchicalrequirement", newStory).Reference;
			DynamicJsonObject newDefect = new DynamicJsonObject();
			newDefect["Name"] = "New Defect Added via collection";
			newDefect["Requirement"] = itemRef;
			CreateResult newTaskResult = restApi.Create("defect", newDefect);

			DynamicJsonObject story = restApi.GetByReference(itemRef, "Defects");
			Assert.AreEqual(1, story["Defects"]["Count"]);

			DynamicJsonObject taskToRemove = new DynamicJsonObject();
			taskToRemove["_ref"] = newTaskResult.Reference;
			OperationResult result = restApi.RemoveFromCollection(itemRef, "Defects", new List<DynamicJsonObject>() { taskToRemove }, new NameValueCollection());

			Assert.IsTrue(result.Success);
			Assert.AreEqual(0, result.Results.Count);
			story = restApi.GetByReference(itemRef, "Defects");
			Assert.AreEqual(0, story["Defects"]["Count"]);

			// Now delete the defect and story
			TestHelperDeleteItem(restApi, newTaskResult.Reference);
			TestHelperDeleteItem(restApi, itemRef);
		}
		public void AddToCollection2x()
		{
			RallyRestApi restApi = GetRallyRestApi2x();
			var itemRef = TestHelperCreateDefect(restApi);
			DynamicJsonObject newTask = new DynamicJsonObject();
			newTask["Name"] = "New Task Added via collection";
			NameValueCollection parameters = new NameValueCollection();
			parameters.Add("fetch", "FormattedID");
			OperationResult result = restApi.AddToCollection(itemRef, "Tasks", new List<DynamicJsonObject>() { newTask }, parameters);
			Assert.IsTrue(result.Success);
			Assert.AreEqual(1, result.Results.Count);
			Assert.IsNotNull(result.Results[0]["FormattedID"]);

			// Now delete it
			TestHelperDeleteItem(restApi, itemRef);
		}
		public void SerializeTest()
		{
			var target = new DynamicJsonSerializer();
			dynamic value = new DynamicJsonObject();
			value.int1 = -19;
			value.decimal1 = 1.21M;
			value.string1 = "hi";
			dynamic obj1 = new DynamicJsonObject();
			obj1.int1 = 19;
			obj1.string1 = "hi";
			obj1.decimal1 = -1.21M;
			value.obj1 = obj1;
			const string expected = "{\"int1\":-19,\"decimal1\":1.21,\"string1\":\"hi\",\"obj1\":{\"int1\":19,\"string1\":\"hi\",\"decimal1\":-1.21}}";
			string actual = target.Serialize(value);
			Assert.AreEqual(expected, actual);
		}
		public void RoundTripTest()
		{
			dynamic val1 = new DynamicJsonObject();
			val1.int1 = -19;
			val1.decimal1 = 1.21M;
			val1.string1 = "hi";
			dynamic obj1 = new DynamicJsonObject();
			obj1.int1 = 19;
			obj1.string1 = "hi";
			obj1.decimal1 = -1.21M;
			val1.obj1 = obj1;
			var target = new DynamicJsonSerializer();
			string json = target.Serialize(val1);
			dynamic val2 = target.Deserialize(json);

			Assert.AreEqual(val2.int1, -19);
			Assert.AreEqual(val2.decimal1, 1.21M);
			Assert.AreEqual(val2.string1, "hi");
			Assert.AreEqual(val2.obj1.int1, obj1.int1);
			Assert.AreEqual(val2.obj1.string1, obj1.string1);
			Assert.AreEqual(val2.obj1.decimal1, obj1.decimal1);
		}
 /// <summary>
 /// Serializes a dynamic JSON Object into a string.
 /// </summary>
 internal string Serialize(DynamicJsonObject value)
 {
     return(SerializeDictionary(value.Dictionary));
 }
		private CachedResult CacheResult(string userName, string sourceUrl, string redirectUrl, DynamicJsonObject responseData)
		{
			string cacheKey = GetCacheKey(userName, sourceUrl);
			CachedResult cachedResult = new CachedResult(redirectUrl, responseData);
			lock (dataLock)
			{
				if (cachedResults.ContainsKey(cacheKey))
					cachedResults[cacheKey] = cachedResult;
				else
					cachedResults.Add(cacheKey, cachedResult);


				FileInfo fileLoction = GetFileLocation(cacheKey);
				File.WriteAllBytes(fileLoction.FullName, SerializeData(cachedResult));
			}

			return cachedResult;
		}
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="obj">The data that was returned for the query against Rally.</param>
		/// <param name="isCachedResult">Is this a cached response?</param>
		public CacheableQueryResult(DynamicJsonObject obj, bool isCachedResult)
			: base(obj)
		{
			IsCachedResult = isCachedResult;
		}
		/// <summary>
		/// Serializes a dynamic JSON Object into a string.
		/// </summary>
		internal string Serialize(DynamicJsonObject value)
		{
			return SerializeDictionary(value.Dictionary);
		}
			public CachedResult(string redirectUrl, DynamicJsonObject responseData)
			{
				Url = redirectUrl;
				ResponseData = responseData;
			}