/// <summary> /// Returns a test instance of one of the provider OM types. /// </summary> /// <param name="type">The type to get instance of.</param> /// <returns>The </returns> public static object GetTestInstance(Type type) { if (type == typeof(ResourceType)) { ResourceProperty p = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))) { CanReflectOnInstanceTypeProperty = false }; ResourceType resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "NoBaseType", false); Assert.IsTrue(resourceType.KeyProperties.Count == 0, "It's okay not to have key properties, since we haven't set this type to readonly yet"); resourceType.AddProperty(p); return(resourceType); } else if (type == typeof(ResourceProperty)) { return(new ResourceProperty("NonKeyProperty", ResourcePropertyKind.Primitive, ResourceType.GetPrimitiveResourceType(typeof(int)))); } else if (type == typeof(ServiceOperation)) { ServiceOperationParameter parameter = new ServiceOperationParameter("o", ResourceType.GetPrimitiveResourceType(typeof(string))); ServiceOperation operation = new ServiceOperation("Dummy", ServiceOperationResultKind.Void, null, null, "POST", new ServiceOperationParameter[] { parameter }); return(operation); } else if (type == typeof(ServiceOperationParameter)) { return(new ServiceOperationParameter("o", ResourceType.GetPrimitiveResourceType(typeof(string)))); } else if (type == typeof(ResourceSet)) { ResourceProperty p = new ResourceProperty("ID", ResourcePropertyKind.Primitive | ResourcePropertyKind.Key, ResourceType.GetPrimitiveResourceType(typeof(int))); ResourceType resourceType = new ResourceType(typeof(object), ResourceTypeKind.EntityType, null, "Foo", "NoBaseType", false); resourceType.AddProperty(p); return(new ResourceSet("Customers", resourceType)); } throw new Exception(String.Format("Unexpected type encountered: '{0}'", type.FullName)); }
private void AddServiceOperation(MethodInfo method, string protocol, object customState) { ServiceOperationResultKind resultKind; Type returnType; ResourceType returnResourceType; if (_serviceOperations.ContainsKey(method.Name)) { throw new InvalidOperationException(string.Format("A service operation with the name {0} already exists.", method.Name)); } if (method.ReturnType == typeof(void)) { resultKind = ServiceOperationResultKind.Void; returnResourceType = null; } else { bool hasSingleResult = GetSingleAttribute <SingleResultAttribute>(method) != null; var queryableElementType = GetGenericElementType(method.ReturnType, typeof(IQueryable <>)); if (queryableElementType == null) { var enumerableElementType = GetGenericElementType(method.ReturnType, typeof(IEnumerable <>)); if (enumerableElementType == null) { returnType = method.ReturnType; resultKind = ServiceOperationResultKind.DirectValue; } else { returnType = enumerableElementType; resultKind = ServiceOperationResultKind.Enumeration; if (hasSingleResult) { throw new InvalidOperationException(string.Format("Method {0} has a return type of IEnumerable which cannot have a SingleResultAttribute applied.", method.Name)); } } } else { returnType = queryableElementType; if (hasSingleResult) { resultKind = ServiceOperationResultKind.QueryWithSingleResult; } else { resultKind = ServiceOperationResultKind.QueryWithMultipleResults; } } returnResourceType = ResourceType.GetPrimitiveResourceType(returnType); if (returnResourceType == null) { if (!_knownResourceTypes.TryGetValue(returnType, out returnResourceType)) { returnResourceType = BuildHierarchyForType(returnType, ResourceTypeKind.ComplexType); } } } var parameters = method.GetParameters(); var serviceOperationParameters = new ServiceOperationParameter[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { var parameter = parameters[i]; if (parameter.IsOut || parameter.IsRetval) { throw new InvalidOperationException(string.Format("Method {0} has Out or Retval parameters which cannot be used in ServiceOperations.", method.Name)); } var parameterResourceType = ResourceType.GetPrimitiveResourceType(parameter.ParameterType); if (parameterResourceType == null) { throw new InvalidOperationException(string.Format("Method {0} has a parameter of type {1} which cannot be used in a ServiceOperation. Only primitive types are allowed as parameters.", method.Name, parameter.ParameterType.Name)); } var parameterName = parameter.Name ?? "p" + i; serviceOperationParameters[i] = new ServiceOperationParameter(parameterName, parameterResourceType); } ResourceSet resourceSet = null; foreach (var set in _resourceSets.Values) { if (set.ResourceType.InstanceType.IsAssignableFrom(returnResourceType.InstanceType)) { resourceSet = set; break; } } if (returnResourceType == null || returnResourceType.ResourceTypeKind != ResourceTypeKind.EntityType || resourceSet != null) { var serviceOperation = new ServiceOperation(method.Name, resultKind, returnResourceType, resourceSet, protocol, serviceOperationParameters); serviceOperation.CustomState = method; var mimeTypeAttribute = GetSingleAttribute <MimeTypeAttribute>(method); if (mimeTypeAttribute != null) { serviceOperation.MimeType = mimeTypeAttribute.MimeType; } _serviceOperations.Add(serviceOperation.Name, serviceOperation); } }
public void TestServiceOperationsWithSpatialParameters() { Action <string, Type, DSPMetadata> AddIdentityServiceOp = (name, type, dspMetadata) => { var primitiveType = Microsoft.OData.Service.Providers.ResourceType.GetPrimitiveResourceType(type); var parameter = new ServiceOperationParameter("param1", primitiveType); dspMetadata.AddServiceOperation(name, ServiceOperationResultKind.DirectValue, primitiveType, null, "GET", new ServiceOperationParameter[] { parameter }); }; DSPUnitTestServiceDefinition roadTripServiceDefinition = GetRoadTripServiceDefinition(typeof(GeographyPoint), TestPoint.DefaultValues, false, false, (m) => { AddIdentityServiceOp("GetGeographyPoint", typeof(GeographyPoint), m); AddIdentityServiceOp("GetGeometryPoint", typeof(GeometryPoint), m); AddIdentityServiceOp("GetDouble", typeof(double), m); }); Func <object[], object> sopCallBack = (args) => { return(args[0]); }; roadTripServiceDefinition.CreateDataSource = (metadata) => { return(new DSPContext() { ServiceOperations = new Dictionary <string, Func <object[], object> >() { { "GetGeographyPoint", sopCallBack }, { "GetGeometryPoint", sopCallBack }, { "GetDouble", sopCallBack }, } }); }; using (TestWebRequest request = roadTripServiceDefinition.CreateForInProcess()) { request.StartService(); // GET request.Accept = "application/xml"; request.RequestContentType = null; request.HttpMethod = "GET"; request.RequestUriString = "/GetDouble?param1=1.2"; request.SendRequest(); var response = request.GetResponseStreamAsText(); StringAssert.Contains(response, "1.2", "didn't get the identity back"); const string wellKnownText = "SRID=4326;POINT (177.508 51.9917)"; // GET request.Accept = "application/xml"; request.RequestContentType = null; request.HttpMethod = "GET"; request.RequestUriString = string.Format("/GetGeographyPoint?param1=geography'{0}'", wellKnownText); request.SendRequest(); response = request.GetResponseStreamAsText(); var geographyPoint = WellKnownTextSqlFormatter.Create().Read <Geography>(new StringReader(wellKnownText)); string geographyPointReturn = GmlFormatter.Create().Write(geographyPoint); // The gml namespace is already declared on the top-level element geographyPointReturn = geographyPointReturn.Replace(" xmlns:gml=\"http://www.opengis.net/gml\"", string.Empty); StringAssert.Contains(response, geographyPointReturn, "didn't get the identity back"); // GET request.Accept = "application/xml"; request.RequestContentType = null; request.HttpMethod = "GET"; request.RequestUriString = string.Format("/GetGeometryPoint?param1=geometry'{0}'", wellKnownText); request.SendRequest(); response = request.GetResponseStreamAsText(); var geometryPoint = WellKnownTextSqlFormatter.Create().Read <Geometry>(new StringReader(wellKnownText)); string geometryPointReturn = GmlFormatter.Create().Write(geometryPoint); // The gml namespace is already declared on the top-level element geometryPointReturn = geometryPointReturn.Replace(" xmlns:gml=\"http://www.opengis.net/gml\"", string.Empty); StringAssert.Contains(response, geometryPointReturn, "didn't get the identity back"); } }