public void DbEntityValidationFailureProducesUsableMessage() { using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = ContextType; request.ForceVerboseErrors = true; request.RequestContentType = UnitTestsUtil.AtomFormat; string uri = "/Elevators(2)"; XName elementName = XName.Get("SerialNumber", ODataNamespace); const string changedValue = "123456"; request.HttpMethod = "GET"; request.Accept = "application/atom+xml,application/xml"; request.RequestStream = null; request.RequestUriString = uri; request.SendRequest(); XDocument entry = request.GetResponseStreamAsXDocument(); XElement element = entry.Descendants(elementName).Single(); element.Value = changedValue; request.HttpMethod = "PATCH"; request.Accept = "application/atom+xml,application/xml"; request.RequestStream = IOUtil.CreateStream(entry.ToString(SaveOptions.DisableFormatting)); System.Net.WebException exception = TestUtil.RunCatching <System.Net.WebException>(() => request.SendRequest()); Assert.IsNotNull(exception, "Expected an exception, but none occurred."); Assert.IsNotNull(exception.InnerException, "Expected an inner exception, but found none"); Assert.AreEqual("The field SerialNumber must be a string with a minimum length of 5 and a maximum length of 5.", exception.InnerException.Message, "Didn't get the expected error message"); } }
public void GeometryAsOpenProperty_AtomWithTypeDeserialize() { // tests atom deserialization with m:type information var testCases = testData.Select(kvp => new { Type = SpatialTestUtil.GeometryTypeFor(kvp.Key), EdmName = SpatialTestUtil.GeometryEdmNameFor(kvp.Key), WktData = kvp.Value }); TestUtil.RunCombinations(testCases, (tcase) => { using (TestWebRequest request = CreateSpatialPropertyService(new Geometry[tcase.WktData.Length], tcase.Type, true, true).CreateForInProcessWcf()) { request.RequestUriString = "/Entities"; request.HttpMethod = "POST"; request.Accept = "application/atom+xml,application/xml"; request.RequestContentType = UnitTestsUtil.AtomFormat; request.SetRequestStreamAsText(AggregateAtomPayloadFromWkt(tcase.Type.Name, tcase.WktData, tcase.EdmName)); request.SendRequest(); var response = request.GetResponseStreamAsXDocument(); string rootXpath = "atom:entry/atom:content/adsm:properties/ads:" + tcase.Type.Name; UnitTestsUtil.VerifyXPaths(response, tcase.WktData.Select((v, i) => rootXpath + i + "[@adsm:type = '" + tcase.EdmName + "' and namespace-uri(*) = 'http://www.opengis.net/gml']").ToArray()); } }); }
public void FilterNavigationWithAnyAll_TypeCasts() { using (OpenWebDataServiceHelper.AcceptAnyAllRequests.Restore()) { OpenWebDataServiceHelper.AcceptAnyAllRequests.Value = true; using (TestWebRequest request = TestWebRequest.CreateForInProcessWcf()) { request.DataServiceType = typeof(CustomDataContext); request.StartService(); string[] filters = new string[] { "Orders/all(o: o/Customer/AstoriaUnitTests.Stubs.CustomerWithBirthday/Birthday gt 1911-04-22T15:20:45.907Z)", "Orders/all(o: isof(o/Customer, 'AstoriaUnitTests.Stubs.CustomerWithBirthday') and cast(o/Customer, 'AstoriaUnitTests.Stubs.CustomerWithBirthday')/Birthday gt 1911-04-22T15:20:45.907Z)", "Orders/all(o: isof(o/Customer, 'AstoriaUnitTests.Stubs.CustomerWithBirthday') and cast(o/Customer, 'AstoriaUnitTests.Stubs.CustomerWithBirthday')/Orders/any())", "isof(Orders/any(),'Edm.Boolean') and ID eq 1", "isof(Orders/any(o: $it/ID eq 2),'Edm.Boolean') and isof(Orders/all(o: $it/ID eq 2),'Edm.Boolean') and ID eq 1", }; foreach (var filter in filters) { request.RequestUriString = "/Customers?$format=atom&$filter=" + filter; Exception e = TestUtil.RunCatching(request.SendRequest); Assert.IsNull(e, "Not expecting exception."); var xdoc = request.GetResponseStreamAsXDocument(); XElement customerWithBirthday = xdoc.Root.Elements(XName.Get("{http://www.w3.org/2005/Atom}entry")).Single(); XElement typeName = customerWithBirthday.Elements(XName.Get("{http://www.w3.org/2005/Atom}category")).Single(); Assert.IsTrue(typeName.Attribute("term").Value.EndsWith("CustomerWithBirthday"), "typeName.Attribute(\"term\").Value.EndsWith(\"CustomerWithBirthday\")"); } } } }
private static void GetResponseAndVerify(TestWebRequest request, int expectedStatusCode, string xpath) { TestUtil.RunCatching(request.SendRequest); Assert.AreEqual(expectedStatusCode, request.ResponseStatusCode); var responsePayload = request.GetResponseStreamAsXDocument(); UnitTestsUtil.VerifyXPaths(responsePayload, new[] { xpath }); }
public void GeometryCollection_Deserialize() { StringBuilder payload = new StringBuilder(); payload.Append("{ \"__metadata\":{ \"uri\": \"http://host/Entities(0)\" }, \"ID\": 0"); foreach (var kvp in testData) { payload.AppendLine(","); payload.Append(JsonCollectionPropertyFromWkt(SpatialTestUtil.GeometryTypeFor(kvp.Key).Name, kvp.Value, SpatialTestUtil.GeometryEdmNameFor(kvp.Key))); } payload.AppendLine("}"); var svc = CreateCollectionWriteService(testData.Keys.Select(t => SpatialTestUtil.GeometryTypeFor(t)).ToList()); using (TestWebRequest request = svc.CreateForInProcess()) { System.Data.Test.Astoria.TestUtil.RunCombinations(UnitTestsUtil.ResponseFormats, (format) => { request.RequestUriString = "/Entities"; request.HttpMethod = "POST"; request.Accept = "application/atom+xml,application/xml"; request.RequestContentType = format; if (format == UnitTestsUtil.JsonLightMimeType) { request.SetRequestStreamAsText(payload.ToString()); } else { var xDoc = JsonValidator.ConvertToXDocument(payload.ToString()); var atomPayload = UnitTestsUtil.Json2AtomXLinq(xDoc, true).ToString(); request.SetRequestStreamAsText(atomPayload); } request.SendRequest(); var response = request.GetResponseStreamAsXDocument(); UnitTestsUtil.VerifyXPaths(response, testData.Select(d => String.Format("count(atom:entry/atom:content/adsm:properties/ads:Collection{0}/adsm:element) = {1}", SpatialTestUtil.GeometryTypeFor(d.Key).Name, d.Value.Length)).ToArray()); }); } }
public void GeometryAsOpenProperty_Deserialize() { var testCases = testData.Select(kvp => new { Type = SpatialTestUtil.GeometryTypeFor(kvp.Key), EdmName = SpatialTestUtil.GeometryEdmNameFor(kvp.Key), Data = new Geometry[kvp.Value.Length], Payload = AggregateJsonPayloadFromWkt(SpatialTestUtil.GeometryTypeFor(kvp.Key).Name, kvp.Value, SpatialTestUtil.GeometryEdmNameFor(kvp.Key)) }); TestUtil.RunCombinations(testCases, UnitTestsUtil.ResponseFormats, (tcase, format) => { using (TestWebRequest request = CreateSpatialPropertyService(tcase.Data, tcase.Type, true, true).CreateForInProcessWcf()) { request.RequestUriString = "/Entities"; request.HttpMethod = "POST"; request.Accept = "application/atom+xml,application/xml"; request.RequestContentType = format; if (format == UnitTestsUtil.JsonLightMimeType) { request.SetRequestStreamAsText(tcase.Payload); } else { // atom from Json - this payload has no m:type var xDoc = JsonValidator.ConvertToXDocument(tcase.Payload.ToString()); var atomPayload = UnitTestsUtil.Json2AtomXLinq(xDoc, true).ToString(); request.SetRequestStreamAsText(atomPayload); } request.SendRequest(); var response = request.GetResponseStreamAsXDocument(); string rootXpath = "atom:entry/atom:content/adsm:properties/ads:" + tcase.Type.Name; string[] xpaths = tcase.Data.Select((v, i) => rootXpath + i + "[@adsm:type = '" + tcase.EdmName + "' and namespace-uri(*) = 'http://www.opengis.net/gml']").ToArray(); UnitTestsUtil.VerifyXPaths(response, xpaths); } }); }
public void FilterCollectionWithAnyAll() { using (OpenWebDataServiceHelper.AcceptAnyAllRequests.Restore()) using (TestWebRequest request = TestWebRequest.CreateForInProcessWcf()) using (TestUtil.RestoreStaticValueOnDispose(typeof(TypedCustomDataContext <EntityForQuery>), "PreserveChanges")) { OpenWebDataServiceHelper.AcceptAnyAllRequests.Value = true; request.Accept = UnitTestsUtil.AtomFormat; request.DataServiceType = typeof(TypedCustomDataContext <EntityForQuery>); TypedCustomDataContext <EntityForQuery> .ClearHandlers(); TypedCustomDataContext <EntityForQuery> .ClearValues(); TypedCustomDataContext <EntityForQuery> .PreserveChanges = true; TypedCustomDataContext <EntityForQuery> .ValuesRequested += (sender, args) => { TypedCustomDataContext <EntityForQuery> typedContext = (TypedCustomDataContext <EntityForQuery>)sender; typedContext.SetValues(new EntityForQuery[] { new EntityForQuery { ID = 0, CollectionOfInt = new List <int>() { 1, 2, 3 }, CollectionOfString = new List <string>() { "a", "b", "c" }, CollectionOfComplexType = new List <MVComplexType>() }, new EntityForQuery { ID = 1, CollectionOfInt = new List <int>() { 4, 5, 6 }, CollectionOfString = new List <string>() { "d", "e", "f" }, CollectionOfComplexType = new List <MVComplexType>() }, new EntityForQuery { ID = 2, CollectionOfInt = new List <int>() { 7, 8, 9 }, CollectionOfString = new List <string>() { "x", "y", "z" }, CollectionOfComplexType = new List <MVComplexType>() { new MVComplexType { Name = "mvcomplextype", Numbers = new List <int>() { 100, 101 } } } }, }); }; request.StartService(); string[] requestStrings = new string[] { "/Values?$filter=CollectionOfInt/any() and (ID eq 2)", "/Values?$filter=CollectionOfString/any(s: s eq 'y')", "/Values?$filter=CollectionOfInt/any(i: cast(i, 'Edm.Int64') eq 7)", "/Values?$filter=CollectionOfInt/all(i: (i ge 7) and (i le 9))", "/Values?$filter=CollectionOfInt/all(i: isof(i, 'Edm.Int32') and ($it/ID eq 2))", "/Values?$filter=CollectionOfComplexType/any(ct: (ct/Name eq 'mvcomplextype') and ct/Numbers/all(n: (n ge 100) and (ct/Name eq 'mvcomplextype')))" }; foreach (var uri in requestStrings) { request.RequestUriString = uri; request.ForceVerboseErrors = true; Exception e = TestUtil.RunCatching(request.SendRequest); Assert.IsNull(e, "Not expecting exception."); var xdoc = request.GetResponseStreamAsXDocument(); UnitTestsUtil.VerifyXPaths(xdoc, "count(//atom:entry)=1", "boolean(//atom:entry/atom:id[contains(.,'Values(2)')])"); } } }
public void TestAnyAllWithQueryInterceptor() { using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = typeof(CustomDataContextWithQueryInterception); request.StartService(); var testCases = new[] { new{ filter = "Orders/any()", xpaths = new string[] { "count(//atom:entry)=2", "/atom:feed/atom:entry[atom:id='http://host/Customers(1)']", "/atom:feed/atom:entry[atom:id='http://host/Customers(2)']", } }, new{ filter = "BestFriend/Orders/any()", xpaths = new string[] { "count(//atom:entry)=1", "/atom:feed/atom:entry[atom:id='http://host/Customers(2)']", } }, new{ // use the same name for the parameter for the any() and QueryInterceptor filter = "Orders/any(o: o/Customer/ID eq 1)", xpaths = new string[] { "count(//atom:entry)=1", "/atom:feed/atom:entry[atom:id='http://host/Customers(1)']", } }, new{ filter = "Orders/any(o: isof(o/Customer, 'AstoriaUnitTests.Stubs.CustomerWithBirthday'))", xpaths = new string[] { "count(//atom:entry)=1", "/atom:feed/atom:entry[atom:id='http://host/Customers(1)']", } }, new{ filter = "Orders/any(o: o/Customer/ID eq $it/ID)", xpaths = new string[] { "count(//atom:entry)=2", "/atom:feed/atom:entry[atom:id='http://host/Customers(1)']", "/atom:feed/atom:entry[atom:id='http://host/Customers(2)']", } }, new{ // since QueryInterceptor filters Orders for Customer 0, all() returns true even if predicate says it has to be 1. filter = "Orders/all(o: o/Customer/ID eq 1)", xpaths = new string[] { "count(//atom:entry)=2", "/atom:feed/atom:entry[atom:id='http://host/Customers(0)']", "/atom:feed/atom:entry[atom:id='http://host/Customers(1)']", } }, }; TestUtil.RunCombinations(testCases, (testCase) => { request.RequestUriString = "/Customers?$format=atom&$filter=" + testCase.filter; request.SendRequest(); var response = request.GetResponseStreamAsXDocument(); UnitTestsUtil.VerifyXPaths(response, testCase.xpaths); }); } }
public void FilterNavigationWithAnyAll() { ocs.PopulateData.EntityConnection = null; using (OpenWebDataServiceHelper.AcceptAnyAllRequests.Restore()) using (System.Data.EntityClient.EntityConnection connection = ocs.PopulateData.CreateTableAndPopulateData()) { OpenWebDataServiceHelper.AcceptAnyAllRequests.Value = true; Type[] types = new Type[] { typeof(ocs.CustomObjectContext), typeof(CustomDataContext), typeof(CustomRowBasedContext) }; var testCases = new[] { new { filters = new string[] { "Orders/any() eq false", "Orders/any() and ID eq 100", "Orders/any(o: o/ID eq 500)", "Orders/any(o: o/ID eq 100 and o/$(Customer)/ID ne 0)", "Orders/any(o: isof(o/$(Customer),'$(CustomerWithBirthDayType)') and o/$(Customer)/ID ne 1)", "Orders/all(o: o/$(Customer)/ID eq 1 and $it/ID ne 1)", "Orders/all(o: o/ID eq 100)", "Orders/all(o: o/ID eq $it/ID)", "Orders/all(o: o/$(Customer)/Orders/any() eq false)", "Orders/all(o: $it/ID eq 1 and o/$(Customer)/Orders/any(o1: o1/ID eq 500))", "Orders/all(o: isof(o/$(Customer),'$(CustomerWithBirthDayType)') and o/$(Customer)/ID ne 1)", }, xpaths = new string[] { "count(//atom:entry)=0" } }, new { filters = new string[] { "Orders/any()", "Orders/any() and ID lt 100", "ID lt 100 and Orders/any()", "Orders/all(o: o/$(Customer)/Orders/any())", }, xpaths = new string[] { "count(//atom:entry)=3" } }, new { filters = new string[] { "Orders/any() and (ID eq 1)", "Orders/any() and isof('$(CustomerWithBirthDayType)')", "Orders/any(o: $it/ID eq 1)", "Orders/any(o: o/$(Customer)/ID eq 1)", "Orders/any(o: isof(o/$(Customer), '$(CustomerWithBirthDayType)'))", "Orders/all(o: $it/ID eq 1)", "Orders/all(o: o/$(Customer)/ID eq 1)", "Orders/all(o: isof(o/$(Customer), '$(CustomerWithBirthDayType)'))", // using the same range variable name in multiple not-nested predicates "Orders/any(o: o/$(Customer)/ID eq 1) and Orders/all(o: o/$(Customer)/ID eq 1)", // nested queries "Orders/any(o: o/$(Customer)/Orders/any(o1: o1/$(Customer)/Orders/all(o2: o2/$(Customer)/ID eq 1)) or $it/ID eq 1)", "Orders/all(o: $it/ID eq 1 and o/$(Customer)/Orders/all(o1: o1/$(Customer)/Orders/any(o2: o2/$(Customer)/ID eq 1)))", // keywords "Orders/any(event: event/$(Customer)/ID eq 1)", "Orders/any(while: while/$(Customer)/ID eq 1)", }, xpaths = new string[] { "count(//atom:entry)=1", "boolean(//atom:entry/atom:category[contains(@term,'CustomerWithBirthday')])" } } }; TestUtil.RunCombinations(types, (type) => { using (TestWebRequest request = TestWebRequest.CreateForInProcess()) { request.DataServiceType = type; request.StartService(); TestUtil.RunCombinations(testCases, (testCase) => { foreach (var str in testCase.filters) { string filter = UnitTestsUtil.ProcessStringVariables(str, (variable) => { if (type == typeof(ocs.CustomObjectContext)) { switch (variable) { case "Customer": return("Customers"); case "CustomerWithBirthDayType": return("AstoriaUnitTests.ObjectContextStubs.Types.CustomerWithBirthday"); } } else if (type == typeof(CustomDataContext)) { switch (variable) { case "CustomerWithBirthDayType": return("AstoriaUnitTests.Stubs.CustomerWithBirthday"); } } else if (type == typeof(CustomRowBasedContext)) { switch (variable) { case "CustomerWithBirthDayType": return("AstoriaUnitTests.Stubs.CustomerWithBirthday"); } } return(variable); }); request.RequestUriString = "/Customers?$format=atom&$filter=" + filter; request.SendRequest(); var response = request.GetResponseStreamAsXDocument(); UnitTestsUtil.VerifyXPaths(response, testCase.xpaths); } }); } }); } }
public void IncomingMessagePropertiesTest() { var testCases = new[] { new { ExpectFailure = false, ErrorCode = 0, IncomingMessageProperties = new Dictionary <string, object>() { { "MicrosoftDataServicesRequestUri", new Uri("http://mappedhost/mappedService.svc/Customers", UriKind.Absolute) }, { "MicrosoftDataServicesRootUri", new Uri("http://mappedhost/mappedService.svc/", UriKind.Absolute) } } }, new { ExpectFailure = true, ErrorCode = 500, IncomingMessageProperties = new Dictionary <string, object>() { { "MicrosoftDataServicesRequestUri", "http://mappedhost/mappedService.svc/Customers" }, } }, new { ExpectFailure = true, ErrorCode = 500, IncomingMessageProperties = new Dictionary <string, object>() { { "MicrosoftDataServicesRootUri", "http://mappedhost/mappedService.svc/" } } }, new { ExpectFailure = true, ErrorCode = 400, IncomingMessageProperties = new Dictionary <string, object>() { { "MicrosoftDataServicesRequestUri", new Uri("http://mappedhost/mappedService.svc/Customers", UriKind.Absolute) }, } }, new { ExpectFailure = true, ErrorCode = 400, IncomingMessageProperties = new Dictionary <string, object>() { { "MicrosoftDataServicesRootUri", new Uri("http://mappedhost/mappedService.svc/", UriKind.Absolute) } } } }; foreach (var testCase in testCases) { using (CustomDataContext.CreateChangeScope()) using (TestWebRequest request = TestWebRequest.CreateForInProcessWcf()) { OpenWebDataServiceHelper.ForceVerboseErrors = true; request.DataServiceType = typeof(CustomDataContext); request.HttpMethod = "GET"; request.RequestUriString = "/Customers?$format=atom"; ((InProcessWcfWebRequest)request).IncomingMessageProperties = testCase.IncomingMessageProperties; Exception e = TestUtil.RunCatching(request.SendRequest); TestUtil.AssertExceptionExpected(e, testCase.ExpectFailure); if (e == null) { XDocument response = request.GetResponseStreamAsXDocument(); var idElements = response.Descendants(UnitTestsUtil.AtomNamespace + "feed").Descendants(UnitTestsUtil.AtomNamespace + "id"); foreach (var id in idElements) { TestUtil.AssertContains(id.Value, ((Uri)((InProcessWcfWebRequest)request).IncomingMessageProperties["MicrosoftDataServicesRequestUri"]).AbsoluteUri); } } else { TestUtil.AssertExceptionStatusCode(e, testCase.ErrorCode, null); } } } }