private static void VerifyError(Func <AstoriaResponse, bool, string> responseParser, AstoriaResponse response, string expectedErrorString, ComparisonFlag comparison) { bool inStream = InStreamErrorExpected(response); // actual error returned string actualErrorString = responseParser(response, inStream); // expected error string (from service op in service) AstoriaTestLog.TraceInfo("Actual Error String --> " + actualErrorString); AstoriaTestLog.TraceInfo("Expected Error String --> " + expectedErrorString); ResourceUtil.VerifyMessage(actualErrorString, expectedErrorString, comparison); }
//--------------------------------------------------------------------- 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); }
internal bool CompareSimpleType(PayloadSimpleProperty payloadProperty, object element, bool isEntity) { Object expectedResult, actualResult = null; // Verify property data string propertyName = payloadProperty.Name; if (isEntity) { #if !ClientSKUFramework if (element is RowEntityType) { expectedResult = ((RowEntityType)element).Properties[propertyName]; } else if (element is RowComplexType) { expectedResult = ((RowComplexType)element).Properties[propertyName]; } else #endif { PropertyInfo propertyInfo = element.GetType().GetProperty(propertyName); if (propertyInfo == null) { AstoriaTestLog.TraceInfo(String.Format("Couldn't find property {0}", propertyName)); return(true); } expectedResult = propertyInfo.GetValue(element, null); } } else { expectedResult = element; } if (expectedResult is System.Byte[]) { int actualInt32Result; object newExpectedResult; // If prop value is int, then it just contains the length, not the acutally binary data so compare that if (Int32.TryParse(payloadProperty.Value, out actualInt32Result)) { actualResult = actualInt32Result; newExpectedResult = ((byte[])expectedResult).Length; } else { actualResult = payloadProperty.Value; newExpectedResult = System.Convert.ToBase64String((System.Byte[])expectedResult); } return(this.InternalEquals(actualResult, newExpectedResult)); } else { if (expectedResult == null) { actualResult = payloadProperty.Value; } else if (expectedResult is System.DateTime) { switch (((DateTime)expectedResult).Kind) { case DateTimeKind.Local: expectedResult = ((DateTime)expectedResult).ToUniversalTime(); break; case DateTimeKind.Unspecified: expectedResult = new DateTime(((DateTime)expectedResult).Ticks, DateTimeKind.Utc); break; case DateTimeKind.Utc: break; } actualResult = this.ParseDate(payloadProperty.Value); } else if (expectedResult is System.Guid) { expectedResult = ((Guid)expectedResult).ToString(); actualResult = payloadProperty.Value; } #if !ClientSKUFramework else if (expectedResult is System.Data.Linq.Binary || expectedResult is System.Xml.Linq.XElement) { actualResult = StripQuotes(payloadProperty.Value); expectedResult = StripQuotes(expectedResult.ToString()); } #endif else { if (payloadProperty.Value == null) { actualResult = null; } else { #if !ClientSKUFramework actualResult = AstoriaUnitTests.Data.TypeData.ObjectFromXmlValue(payloadProperty.Value, expectedResult.GetType()); #endif } } return(this.InternalEquals(actualResult, expectedResult)); } }
public static void CompareMetadata(IEdmModel expectedItems, IEdmModel actualItems, bool isReflectionBasedProvider, string defaultEntityContainerName, ServiceContainer serviceContainer) { // // Verify the metadata version is correct, the number of entity containers is 1, the entity // container names match, and the number of entity types are the same // AstoriaTestLog.AreEqual(1.1, actualItems.GetEdmVersion(), "The metadata version was not correct"); AstoriaTestLog.AreEqual(serviceContainer.Workspace.ContextTypeName, actualItems.EntityContainer.Name, "Entity Container names do not match"); if (!isReflectionBasedProvider) { AstoriaTestLog.AreEqual(expectedItems.SchemaElements.OfType <IEdmEntityType>().Count(), actualItems.SchemaElements.OfType <IEdmEntityType>().Count(), "Entity Type Counts do not match"); } foreach (IEdmOperationImport metadataExposedFunction in actualItems.EntityContainer.OperationImports()) { AstoriaTestLog.TraceInfo("--> " + metadataExposedFunction.Name); } // // For each entity type exposed through the metadata endpoint, verify the following // 1. Verify it has an equivalent definition in the resource container // 2. Verify the entity type name is correct // 3. Verify that no navigation property is exposed in the entity type // 4. Verify that the property count in the entity type // IEnumerable <ComplexType> rtTypeCollection = serviceContainer.AllTypes.Where(a => a.GetType().Name.Equals("ResourceType")); AstoriaTestLog.TraceInfo(rtTypeCollection.Count().ToString()); foreach (IEdmEntityType metadataExposedEntityType in actualItems.SchemaElements.OfType <IEdmEntityType>()) { ResourceType resourceContainerEntityType = serviceContainer.AllTypes.Where(b => b.GetType().Name.Equals("ResourceType") & (b.FullName.Equals(metadataExposedEntityType.FullName()))).FirstOrDefault() as ResourceType; if (resourceContainerEntityType == null) { continue; } AstoriaTestLog.IsNotNull(resourceContainerEntityType, "Did not find entity type in resource container"); AstoriaTestLog.AreEqual(resourceContainerEntityType.FullName, metadataExposedEntityType.FullName(), "Entity Type name mismatch"); // // Verify the name, type, and nullable attribute values // ResourceContainer rc = serviceContainer.ResourceContainers.Where(a => a.BaseType.Name.Equals(metadataExposedEntityType.Name)).FirstOrDefault(); if (rc == null) { continue; } int navCount = rc.BaseType.Properties.Cast <NodeProperty>().Cast <ResourceProperty>().Where(a => a.IsNavigation).Count(); AstoriaTestLog.TraceInfo(rc.Name); AstoriaTestLog.TraceInfo(navCount.ToString()); AstoriaTestLog.AreEqual(resourceContainerEntityType.Properties.Count - navCount, metadataExposedEntityType.Properties().Count(), "Edm Property count mismatch"); foreach (IEdmProperty metadataExposedProperty in metadataExposedEntityType.Properties()) { string errorStringEntityTypeProperty = metadataExposedEntityType.FullName() + " : " + metadataExposedProperty.Name; NodeProperty resourceContainerProperty = resourceContainerEntityType.Properties.Where(a => a.Name.Equals(metadataExposedProperty.Name)).FirstOrDefault(); AstoriaTestLog.IsNotNull(resourceContainerProperty, "Did not find property -->" + errorStringEntityTypeProperty + "<-- in resource container"); if (resourceContainerProperty == null) { continue; } if (metadataExposedProperty.Type.IsBinary()) { if (AstoriaTestProperties.DataLayerProviderKinds.Contains(DataLayerProviderKind.LinqToSql)) { AstoriaTestLog.AreEqual("System.Data.Linq.Binary", resourceContainerProperty.Type.ClrType.FullName, errorStringEntityTypeProperty + " type is incorrect"); } else { AstoriaTestLog.AreEqual("System.Byte[]", resourceContainerProperty.Type.ClrType.FullName, errorStringEntityTypeProperty + " type is incorrect"); } } else { AstoriaTestLog.AreEqual(metadataExposedProperty.Type.FullName(), resourceContainerProperty.Type.FullName, errorStringEntityTypeProperty + " type is incorrect"); } } } }
public static void VerifyException(Exception e, ResourceIdentifier identifier, params Object[] args) { if (identifier == null) { throw new TestFailedException("VerifyException: an exception was expected, but ResourceIdentifier is null"); } //Exception not thrown, and should have if (e == null) { throw new TestFailedException("VerifyException: an exception was expected, but not thrown Id='" + identifier.CompleteIdInfo + "'"); } if (identifier.ExpectedExceptionType == null) { throw new TestFailedException("VerifyException: ResourceIdentifier doesn't contain a ExpectedException"); } //Reflection while (e is TargetInvocationException && e.InnerException != null && identifier.ExpectedExceptionType != typeof(TargetInvocationException)) { e = e.InnerException; } //Test Exceptions (never expected) if (e is TestException) { throw e; } if (String.IsNullOrEmpty(identifier.Id)) { if (e != null) { throw new TestFailedException("VerifyException: an exception was thrown, incorrect Identifier"); } return; } //Verify the type if (identifier.ExpectedExceptionType != null && e.GetType() != identifier.ExpectedExceptionType) { throw new TestFailedException("Verify exception type", e.GetType(), identifier.ExpectedExceptionType, e); } if (e.Source == null || e.Source == String.Empty) { throw new TestFailedException("Exception is thrown, but the Source property of the exception is not set."); } //Exception not thrown, and should have if (e == null && identifier.Id != null) { throw new TestFailedException("VerifyException: an exception was expected, but not thrown Id='" + identifier.Id + "'"); } AstoriaTestLog.TraceInfo("VerifyException: " + identifier.CompleteIdInfo + " - " + e.GetType().Name); string expectedMessage = GetLocalizedResourceString(identifier); VerifyMessage(e.Message, expectedMessage, identifier.ComparisonFlag); }