public static Method ForEntityType(OdcmMethod odcmMethod) { return odcmMethod.ReturnType == null ? (Method) new EntityVoidMethod(odcmMethod) : odcmMethod.IsCollection ? (Method) new EntityCollectionFunctionMethod(odcmMethod) : (Method) new EntityInstanceFunctionMethod(odcmMethod); }
public Given_an_OdcmClass_Service_Bound_VoidMethod() { Method = Any.OdcmMethod(); _expectedMethodName = Method.Name + "Async"; Init(m => m.EntityContainer.Methods.Add(Method)); }
public Given_an_OdcmClass_Entity_Bound_VoidMethod() { Method = Any.OdcmMethod(); _expectedMethodName = Method.Name + "Async"; Init(m => m.Namespaces[0].Classes.First().Methods.Add(Method)); ServerMethodNameGenerator = () => Method.FullName; }
public Given_an_OdcmClass_Entity_Collection_Bound_VoidMethod() { Method = Any.OdcmMethod(m => m.IsBoundToCollection = true); _expectedMethodName = Method.Name + "Async"; _expectedMethodParameters = Method.Parameters.Select(p => Proxy.GetClass(p.Type.Namespace, p.Type.Name)); Init(m => m.Namespaces[0].Classes.First().Methods.Add(Method)); ServerMethodNameGenerator = () => Method.FullName; }
protected void Init(Action<OdcmMethod> config = null) { Init(model => model.Namespaces[0].Classes.First() .Methods.Add(Method = Any.OdcmMethod(m => { m.ReturnType = model.Namespaces[0].Classes.First(); m.IsCollection = IsCollection; if (config != null) config(m); }))); _expectedReturnType = ReturnTypeGenerator(ConcreteInterface); _expectedMethodName = Method.Name + "Async"; }
public void Init(Action<OdcmMethod> config = null) { Init(model => model.EntityContainer .Methods.Add(Method = Any.OdcmMethod(m => { m.ReturnType = model.Namespaces[0].Classes.First(); m.IsCollection = IsCollection; m.IsBoundToCollection = false; if (config != null) config(m); }))); _expectedReturnType = ReturnTypeGenerator(ConcreteInterface); _expectedMethodName = Method.Name + "Async"; _expectedMethodParameters = GetMethodParameterTypes(); }
public Given_an_OdcmParameter() { _model = new OdcmModel(Any.ServiceMetadata()); _namespace = Any.EmptyOdcmNamespace(); _model.Namespaces.Add(_namespace); _class = Any.OdcmEntityClass(_namespace); _model.AddType(_class); _method = Any.OdcmMethod(m => m.Parameters.Clear()); _class.Methods.Add(_method); _param = Any.OdcmParameter(); _method.Parameters.Add(_param); _expectedMethodName = _method.Name + "Async"; }
public void Init(Action<OdcmMethod> config = null) { Init(model => model.Namespaces[0].Classes.First() .Methods.Add(Method = Any.OdcmMethod(m => { m.ReturnType = model.Namespaces[0].Classes.First(); m.IsCollection = IsCollection; m.IsBoundToCollection = true; if (config != null) config(m); }))); _expectedReturnType = ReturnTypeGenerator(ConcreteInterface); _expectedMethodName = Method.Name + "Async"; _expectedMethodParameters = Method.Parameters.Select(p => Proxy.GetClass(p.Type.Namespace, p.Type.Name)); }
protected ServerMethod(OdcmMethod odcmMethod) { BodyParameters = odcmMethod.Parameters .Where(p => p.CallingConvention == OdcmCallingConvention.InHttpMessageBody) .Select(Parameter.FromOdcmParameter); UriParameters = odcmMethod.Parameters .Where(p => p.CallingConvention == OdcmCallingConvention.InHttpRequestUri) .Select(Parameter.FromOdcmParameter); switch (odcmMethod.Verbs) { case OdcmAllowedVerbs.Any: HttpMethod = "GET"; break; case OdcmAllowedVerbs.Delete: HttpMethod = "DELETE"; break; case OdcmAllowedVerbs.Get: HttpMethod = "GET"; break; case OdcmAllowedVerbs.Patch: HttpMethod = "PATCH"; break; case OdcmAllowedVerbs.Post: HttpMethod = "POST"; break; case OdcmAllowedVerbs.Put: HttpMethod = "PUT"; break; default: throw new ArgumentOutOfRangeException(); } IsAsync = true; ModelName = ConfigurationService.Settings.OmitMethodNamespaceInUriPaths ? odcmMethod.Name : odcmMethod.FullName; Description = odcmMethod.Description; Name = odcmMethod.Name + "Async"; Parameters = odcmMethod.Parameters.Select(Parameter.FromOdcmParameter); }
public void When_the_verb_is_POST_the_Collection_passes_parameters_on_the_URI_and_in_the_body() { Init(model => { Method = Any.OdcmMethodPost(); Method.Class = model.EntityContainer; Method.ReturnType = Class; Method.IsCollection = false; Method.IsBoundToCollection = false; model.EntityContainer.Methods.Add(Method); }); using (var mockService = new MockService()) { var service = mockService .CreateContainer(EntityContainerType); mockService.ValidateParameterPassing("POST", service, "", Method, ServerMethodNameGenerator(), TargetEntity); } }
public EntityCollectionFunctionMethod(OdcmMethod odcmMethod) : base(odcmMethod) { ReturnType = Type.TaskOf(Type.IEnmerableOf(new Type(NamesService.GetPublicTypeName(odcmMethod.ReturnType)))); }
public static OdcmMethod OdcmMethodGet(Action<OdcmMethod> config = null) { var retVal = new OdcmMethod(Any.CSharpIdentifier(), Any.EmptyOdcmNamespace()); retVal.Verbs = OdcmAllowedVerbs.Get; retVal.Parameters.AddRange( Any.Sequence( s => new OdcmParameter(Any.CSharpIdentifier()) { Type = Any.PrimitiveOdcmType(), CallingConvention = OdcmCallingConvention.InHttpRequestUri }, Any.Int(1, 3))); if (config != null) config(retVal); return retVal; }
public static OdcmMethod OdcmMethod(Action<OdcmMethod> config = null) { var retVal = new OdcmMethod(Any.CSharpIdentifier(), Any.EmptyOdcmNamespace()); retVal.Verbs = EnumValue<OdcmAllowedVerbs>(); retVal.Parameters.AddRange( Any.Sequence(s => new OdcmParameter(Any.CSharpIdentifier()) { Type = Any.PrimitiveOdcmType() }, Any.Int(0, 3))); if (config != null) config(retVal); return retVal; }
public void When_the_verb_is_POST_the_Collection_passes_parameters_on_the_URI_and_in_the_body() { Init(m => { Method = Any.OdcmMethodPost(); Method.Class = Class; Method.ReturnType = null; Method.IsCollection = false; Method.IsBoundToCollection = true; Class.Methods.Add(Method); }); using (var mockService = new MockService(true)) { var collectionPath = Any.UriPath(1); var collection = mockService .GetDefaultContext(Model) .CreateCollection(CollectionType, ConcreteType, collectionPath); mockService.ValidateParameterPassing("POST", collection, "/" + collectionPath, Method, ServerMethodNameGenerator(), null); } }
private void WriteMethod(OdcmClass odcmClass, IEdmOperation operation, IEdmOperationImport operationImport = null) { var parameters = operation.IsBound ? (from parameter in operation.Parameters where parameter != operation.Parameters.First() select parameter) : (operation.Parameters); var isBoundToCollection = operation.IsBound && operation.Parameters.First().Type.IsCollection(); var odcmMethod = new OdcmMethod(operation.Name, odcmClass.Namespace) { IsComposable = operation.IsFunction() && ((IEdmFunction)operation).IsComposable, IsBoundToCollection = isBoundToCollection, Verbs = operation.IsAction() ? OdcmAllowedVerbs.Post : OdcmAllowedVerbs.Any, Class = odcmClass }; AddVocabularyAnnotations(odcmMethod, operation); if (operationImport != null) { AddVocabularyAnnotations(odcmMethod, operationImport); } odcmClass.Methods.Add(odcmMethod); if (operation.ReturnType != null) { odcmMethod.ReturnType = ResolveType(operation.ReturnType); odcmMethod.IsCollection = operation.ReturnType.IsCollection(); } var callingConvention = operation.IsAction() ? OdcmCallingConvention.InHttpMessageBody : OdcmCallingConvention.InHttpRequestUri; foreach (var parameter in parameters) { var odcmParameter = new OdcmParameter(parameter.Name) { CallingConvention = callingConvention, Type = ResolveType(parameter.Type), IsCollection = parameter.Type.IsCollection(), IsNullable = parameter.Type.IsNullable }; AddVocabularyAnnotations(odcmParameter, parameter); odcmMethod.Parameters.Add(odcmParameter); } }
public EntityVoidMethod(OdcmMethod odcmMethod) : base(odcmMethod) { ReturnType = new Type(new Identifier("System.Threading.Tasks", "Task")); }
public EntityInstanceFunctionMethod(OdcmMethod odcmMethod) : base(odcmMethod) { InstanceName = NamesService.GetConcreteTypeName(odcmMethod.ReturnType); ReturnType = Type.TaskOf(new Type(NamesService.GetPublicTypeName(odcmMethod.ReturnType))); }
public void When_the_return_type_is_primitive_it_is_mapped_to_an_IEnumerable_of_DotNet_Primitives() { Init(model => model.Namespaces[0].Classes.First() .Methods.Add(Method = Any.OdcmMethod(m => { m.ReturnType = new OdcmPrimitiveType("Stream", OdcmNamespace.Edm); m.IsCollection = IsCollection; }))); _expectedReturnType = ReturnTypeGenerator(typeof(Microsoft.OData.Client.DataServiceStreamLink)); _expectedMethodName = Method.Name + "Async"; var methodInfos = new[] { ConcreteInterface.GetMethod(_expectedMethodName), ConcreteType.GetMethod(_expectedMethodName), FetcherInterface.GetMethod(_expectedMethodName), FetcherType.GetMethod(_expectedMethodName) }; foreach (var methodInfo in methodInfos) { methodInfo.ReturnType .Should().Be(_expectedReturnType); } }
public static void ValidateParameterPassing(this MockService mockService, string httpMethod, object instance, string instancePath, OdcmMethod method, string serverMethodName, EntityArtifacts entityArtifacts) { var expectedMethodName = method.Name + "Async"; var methodArguments = method.GetSampleArguments().ToArray(); var uriArguments = method.UriParameters() .Select(p => methodArguments.First(a => a.Item1 == p.Name)); var bodyArguments = method.BodyParameters() .Select(p => methodArguments.First(a => a.Item1 == p.Name)); var responseBuilder = mockService .OnInvokeMethodRequest(httpMethod, instancePath + "/" + serverMethodName, uriArguments.ToTestReadableStringCollection(), ArgumentOfTupleExtensions.ToJObject(bodyArguments)); if (entityArtifacts == null) responseBuilder.RespondWith(r => r.Response.StatusCode = 200); else responseBuilder.RespondWithGetEntity(entityArtifacts.Class.GetDefaultEntitySetName(), entityArtifacts.Class.GetSampleJObject()); instance.InvokeMethod<Task>(expectedMethodName, methodArguments.Select(t => t.Item2).ToArray()) .Wait(); }
} [Fact] public void When_the_verb_is_GET_the_Fetcher_passes_parameters_on_the_URI() { Init(m => { Method = Any.OdcmMethodGet(); Method.Class = Class; Method.ReturnType = null; Method.IsCollection = false; Class.Methods.Add(Method); }); var entityKeyValues = Class.GetSampleKeyArguments().ToArray(); var fetcherPath = Class.GetDefaultEntityPath(entityKeyValues); using (var mockService = new MockService() ) { var fetcher = mockService .GetDefaultContext(Model) .CreateFetcher(FetcherType, fetcherPath);
private void WriteMethod(OdcmClass odcmClass, IEdmFunctionImport operation) { IEnumerable<IEdmFunctionParameter> parameters = operation.IsBindable ? (from parameter in operation.Parameters where parameter != operation.Parameters.First() select parameter) : (operation.Parameters); bool isBoundToCollection = operation.IsBindable && operation.Parameters.First().Type.IsCollection(); var odcmMethod = new OdcmMethod(operation.Name, odcmClass.Namespace) { Verbs = operation.IsSideEffecting ? OdcmAllowedVerbs.Post : OdcmAllowedVerbs.Any, IsBoundToCollection = isBoundToCollection, IsComposable = operation.IsComposable, Class = odcmClass }; odcmClass.Methods.Add(odcmMethod); if (operation.ReturnType != null) { odcmMethod.ReturnType = ResolveType(operation.ReturnType); odcmMethod.IsCollection = operation.ReturnType.IsCollection(); } var callingConvention = operation.IsSideEffecting ? OdcmCallingConvention.InHttpMessageBody : OdcmCallingConvention.InHttpRequestUri; foreach (var parameter in parameters) { odcmMethod.Parameters.Add(new OdcmParameter(parameter.Name) { CallingConvention = callingConvention, Type = ResolveType(parameter.Type), IsCollection = parameter.Type.IsCollection(), IsNullable = parameter.Type.IsNullable }); } }
public void When_the_verb_is_POST_the_Concrete_passes_parameters_on_the_URI_and_in_the_body() { Init(m => { Method = Any.OdcmMethodPost(); Method.Class = Class; Method.ReturnType = null; Method.IsCollection = false; Class.Methods.Add(Method); }); var entityKeyValues = Class.GetSampleKeyArguments().ToArray(); var instancePath = Class.GetDefaultEntityPath(entityKeyValues); using (var mockService = new MockService(true) .SetupPostEntity(TargetEntity, entityKeyValues) ) { var concrete = mockService .GetDefaultContext(Model) .CreateConcrete(ConcreteType); mockService.ValidateParameterPassing("POST", concrete, instancePath, Method, ServerMethodNameGenerator(), null); }
public void When_the_verb_is_GET_the_Collection_passes_parameters_on_the_URI() { base.Init(m => { Method = Any.OdcmMethodGet(); Method.Class = Class; Method.ReturnType = Class; Method.IsCollection = IsCollection; Method.IsBoundToCollection = true; Class.Methods.Add(Method); }); var collectionPath = Any.UriPath(1); using (var mockService = new MockService()) { var collection = mockService .GetDefaultContext(Model) .CreateCollection(CollectionType, ConcreteType, collectionPath); mockService.ValidateParameterPassing("GET", collection, "/" + collectionPath, Method, ServerMethodNameGenerator(), TargetEntity); } }