public virtual void CompareCountInline(IQueryable objectsInResponse, object baselineCount) { if (baselineCount == null) { baselineCount = 0; } AstoriaTestLog.Compare(this.Count == (Int32)baselineCount, "Count in Payload not equal to Baseline"); // Compare the other objects normally. Compare(objectsInResponse); }
public static KeyExpression GetContainingKey(this ContainmentAttribute att, KeyExpression childKey, ResourceType parentType, bool abbreviate) { AstoriaTestLog.Compare(childKey.ResourceContainer == att.ChildContainer, String.Format("ChildKey does not belong to expected set (Expected '{0}', got '{1}'", att.ChildContainer.Name, childKey.ResourceContainer.Name)); List <PropertyExpression> parentProperties = new List <PropertyExpression>(); List <ConstantExpression> parentValues = new List <ConstantExpression>(); foreach (NodeProperty p_prop in att.ParentContainer.BaseType.Key.Properties) { string c_name; if (!att.KeyMapping.TryGetValue(p_prop.Name, out c_name)) { AstoriaTestLog.FailAndThrow(String.Format("Parent key property {0} does not appear in derived key mapping", p_prop.Name)); } // need to get the offset now int c_offset = 0; for (; c_offset < childKey.Properties.Length; c_offset++) { if (childKey.Properties[c_offset].Name == c_name) { break; } } if (c_offset >= childKey.Properties.Length) { AstoriaTestLog.FailAndThrow(String.Format("Could not find property '{0}' in child key", c_name)); } NodeProperty c_prop = childKey.Properties[c_offset]; parentProperties.Add(p_prop.Property()); parentValues.Add(new ConstantExpression(childKey.Values[c_offset])); if (abbreviate) { childKey.IncludeInUri[c_offset] = false; } } return(new KeyExpression(att.ParentContainer, parentType, parentProperties.ToArray(), parentValues.ToArray())); }
private void InferProperties() { ParentNavigationProperty = (from p in ParentContainer.BaseType.Properties.OfType <ResourceProperty>() where p.IsNavigation && p.OtherAssociationEnd.ResourceType == ChildContainer.BaseType select p).FirstOrDefault(); AstoriaTestLog.Compare(ParentNavigationProperty != null, "Failed to identify parent navigation property"); // figure out keymapping // each property in the key of the parent and child COULD be a local or foreign key // if it is a foreign key, then it could point to foreach (ResourceProperty c_prop in ChildContainer.BaseType.Key.Properties.Cast <ResourceProperty>()) { ResourceProperty c_foreign = GetReferencedProperty(c_prop); foreach (ResourceProperty p_prop in ParentContainer.BaseType.Key.Properties.Cast <ResourceProperty>()) { ResourceProperty p_foreign = GetReferencedProperty(p_prop); if (c_foreign == p_foreign) { KeyMapping.Add(p_prop.Name, c_prop.Name); break; } } } //AccessPathAttribute(string parentEntitySetName, // string parentNavigationPropertyName, // string childEntitySetName, // string parentKeys, // string childKeys) this.orderedParams.Add(String.Format("\"{0}\"", ParentContainer.Name)); this.orderedParams.Add(String.Format("\"{0}\"", ParentNavigationProperty.Name)); this.orderedParams.Add(String.Format("\"{0}\"", ChildContainer.Name)); this.orderedParams.Add(String.Format("\"{0}\"", String.Join(",", KeyMapping.Keys.ToArray()))); this.orderedParams.Add(String.Format("\"{0}\"", String.Join(",", KeyMapping.Values.ToArray()))); }
//--------------------------------------------------------------------- public AstoriaResponse SendAndVerify(object expectedPayload, params string[] headers) { // Set auxiliary header values, if any (like ETag). for (int i = 0; i < headers.Length; i += 2) { base.Headers[headers[i]] = headers[i + 1]; } // Determine lowest possible protocol versions. string requestVersion = "1.0"; string responseVersion = "1.0"; if (base.ExpectedStatusCode == HttpStatusCode.OK) { if (base.URI.Contains("$select=")) { requestVersion = "2.0"; responseVersion = "1.0"; } if (base.URI.Contains("$inlinecount=")) { requestVersion = "2.0"; responseVersion = "2.0"; } if (base.URI.Contains("/$count")) { requestVersion = "2.0"; responseVersion = "2.0"; } } // Set request version headers semi-randomly. switch (base.URI.GetHashCode() & 3) { case 0: base.DataServiceVersion = requestVersion; break; case 1: base.DataServiceVersion = null; break; } switch ((base.URI.GetHashCode() >> 2) & 3) { case 0: base.MaxDataServiceVersion = requestVersion; break; case 1: base.MaxDataServiceVersion = Versioning.Server.DataServiceVersion; break; } // Send request to server. AstoriaResponse response = GetResponse(); // Update last seen MLE or MR ETags. if (response.ETagHeaderFound) { if (base.URI.Contains("$value")) { ETagMRR = response.ETagHeader; } else { ETagMLE = response.ETagHeader; } } if ((AstoriaTestProperties.DataLayerProviderKinds.Contains(DataLayerProviderKind.NonClr)) && (AstoriaTestProperties.UseOpenTypes)) { // // This section of code is handling the DSV values returned from server changes // bool not_3_0 = response.DataServiceVersion.StartsWith("1.0"); not_3_0 = response.DataServiceVersion.StartsWith("2.0") | not_3_0; AstoriaTestLog.Compare(not_3_0, "DataServiceVersion response header was returned with " + response.DataServiceVersion); AstoriaTestLog.TraceInfo("Returned DSV was " + response.DataServiceVersion); } else { // Verify response version header. AstoriaTestLog.Compare(response.DataServiceVersion.StartsWith(responseVersion), "DataServiceVersion response header must be " + responseVersion + " but was " + response.DataServiceVersion); } // Verify content type: xml for Atom, json for JSON. if (!base.Batched && !string.IsNullOrEmpty(response.Payload)) { if (base.Format == SerializationFormatKind.Atom && !response.ContentType.Contains("xml") || (base.Format == SerializationFormatKind.JSON && !response.ContentType.Contains("json"))) { AstoriaTestLog.WriteLine(string.Format("Wrong Content-Type {0} in response to {1} request: ", response.ContentType, base.Format)); AstoriaTestLog.WriteLine("Payload:"); AstoriaTestLog.FailAndThrow(response.Payload ?? "{null}"); } } // Verify MLE (BlobsPayload) or MR response payload. if (expectedPayload is BlobsPayload) { // Compare MLE payloads. BlobsPayload actualPayload = (expectedPayload as BlobsPayload); if (actualPayload.ToString() != expectedPayload.ToString()) { AstoriaTestLog.FailAndThrow( "MLE received:" + TestLog.NewLine + actualPayload + TestLog.NewLine + "MLE expected:" + TestLog.NewLine + expectedPayload); } // Temporarily morph MLE into normal entity and call Verify(). string originalPayload = response.Payload; response.Payload = actualPayload.AdjustedForVerify(); response.Verify(); response.Payload = originalPayload; } else { // Compare MR payloads. string actualPayload = response.Payload; if (expectedPayload != null && actualPayload != expectedPayload as string) { AstoriaTestLog.FailAndThrow( "MR received:" + TestLog.NewLine + actualPayload + TestLog.NewLine + "MR expected:" + TestLog.NewLine + expectedPayload); } response.Verify(); } // Remove auxiliary headers, if any. for (int i = 0; i < headers.Length; i += 2) { base.Headers.Remove(headers[i]); } return(response); }
public static void ComparePrimitiveValuesObjectAndString(object expected, Type expectedType, string actual, bool valueUri, SerializationFormatKind serializationKind, bool throwOnError) { object actualObject = DeserializeStringToObject(actual, expectedType, valueUri, serializationKind); string expectedStr = "null"; if (expected != null) { expectedStr = AstoriaUnitTests.Data.TypeData.XmlValueFromObject(expected); } string errorMessage = String.Format("Error Primitive Value of type:{0} not equal: \r\n\tExpected:\t{1} \r\n\tActual:\t{2}", expectedType.Name, expectedStr, actual); //Fixups if (expected != null && expectedType.Equals(typeof(string))) { if (actualObject != null) { actualObject = (actualObject as string).Trim(); } expected = (expected as string).Trim(); } if (expectedType == typeof(System.Xml.Linq.XElement)) { expectedStr = null; if (expected != null) { System.Xml.Linq.XElement xElement = expected as System.Xml.Linq.XElement; expectedStr = xElement.ToString(); } AstoriaTestLog.AreEqual(expectedStr, actual, errorMessage); } else if (expectedType == typeof(Single) || expectedType == typeof(Double)) { if (serializationKind == SerializationFormatKind.JSON) { if (expectedType == typeof(Double)) { Double d; if (expected != null) { d = Convert.ToDouble(expected); expectedStr = d.ToString("r", System.Globalization.CultureInfo.InvariantCulture); } if (actual != null) { if (double.TryParse(actual, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out d)) { actual = d.ToString("r", CultureInfo.InvariantCulture.NumberFormat); } } } else if (expectedType == typeof(Single)) { Single s; if (expected != null) { s = Convert.ToSingle(expected); expectedStr = s.ToString("r", System.Globalization.CultureInfo.InvariantCulture); } if (actual != null) { if (Single.TryParse(actual, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out s)) { actual = s.ToString("r", CultureInfo.InvariantCulture.NumberFormat); } } } } //Fixup if (expectedStr == "null") { expectedStr = null; } if (expectedStr != null && actual != null) { expectedStr = expectedStr.Trim(); actual = actual.Trim(); } if (throwOnError) { if (expectedStr != null) { AstoriaTestLog.Compare(expectedStr.Equals(actual), errorMessage); } else { AstoriaTestLog.Compare(actual == null, errorMessage); } } else { AstoriaTestLog.AreEqual(expectedStr, actual, errorMessage); } } #if !ClientSKUFramework //For some reason byte[] comparision is not working correctly else if ((expectedType == typeof(byte[]) || expectedType == typeof(System.Data.Linq.Binary)) && valueUri) { string expectedBytesStr = null; if (expected == null) { expectedBytesStr = null; } else if (expectedType == typeof(byte[])) { expectedBytesStr = (new System.Text.UTF8Encoding()).GetString((byte[])expected); } else if (expectedType == typeof(System.Data.Linq.Binary)) { System.Data.Linq.Binary binary = expected as System.Data.Linq.Binary; expectedBytesStr = (new System.Text.UTF8Encoding()).GetString((byte[])binary.ToArray()); } if (throwOnError) { if (expected == null) { AstoriaTestLog.Compare(null == actual, errorMessage); } else { AstoriaTestLog.Compare(expectedBytesStr.Equals(actual), errorMessage); } } else { AstoriaTestLog.AreEqual(expectedBytesStr, actual, errorMessage); } } #endif else if (expectedType == typeof(byte[]) && valueUri == false) { AreBytesEqual((byte[])expected, (byte[])actualObject, throwOnError); } else { if (throwOnError) { bool objectsEqual = true; if (expected == null) { if (actualObject != null) { objectsEqual = false; } } else { objectsEqual = expected.Equals(actualObject); } AstoriaTestLog.Compare(objectsEqual, errorMessage); } else { AstoriaTestLog.AreEqual(expected, actualObject, errorMessage); } } }
private static void AreBytesEqual(byte[] expected, byte[] actual, bool throwOnError) { if (expected == null && actual == null) { return; } else if (expected == null && actual != null) { if (throwOnError) { AstoriaTestLog.Compare(false, "Expected Actual to be null like expected"); } else { AstoriaTestLog.AreEqual(expected, actual, "Bytes not equal"); } return; } else if (expected != null && actual == null) { if (throwOnError) { AstoriaTestLog.Compare(false, "Expected Actual to be a value like expected"); } else { AstoriaTestLog.AreEqual(expected, actual, "Bytes not equal"); } return; } if (throwOnError) { AstoriaTestLog.Compare(expected.Length == actual.Length, "Bytes lengths are not equal"); } else { AstoriaTestLog.AreEqual(expected.Length, actual.Length, "Bytes lengths are not equal"); } bool errorFound = false; for (int i = 0; i < expected.Length; i++) { if (expected[i] != actual[i]) { errorFound = true; string errorMessage = String.Format("Byte arrays not equal at index {0}, expected:{1} actual:{2}", i, expected[i], actual[i]); if (throwOnError) { AstoriaTestLog.Compare(expected[i] == actual[i], errorMessage); } else { AstoriaTestLog.AreEqual(expected.Length, actual.Length, errorMessage); } } if (errorFound == true) { break; } } }
public static void Inconclusive(string message) { AstoriaTestLog.Compare(false, message); }
public static void FailAndThrow(string message) { AstoriaTestLog.Compare(false, message); }
public static void IsTrue(bool condition, string message) { AstoriaTestLog.Compare(condition == true, message); }
public static void AreSame(object expected, object actual, string message) { AstoriaTestLog.Compare(expected.Equals(actual), message); }
public static void IsNull(object value, string message) { AstoriaTestLog.Compare(value == null, message); }