Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        public void Write(OdcmMethod odcmMethod)
        {
            var returnType = NamesService.GetPublicTypeName(odcmMethod.ReturnType);

            var parameters = string.Join(", ",
                                         (from i in odcmMethod.Parameters select NamesService.GetPublicTypeName(i.Type, i.IsCollection) + " " + i.Name));
        }
Ejemplo n.º 3
0
        public void When_the_verb_is_POST_the_Fetcher_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 fetcherPath     = Class.GetDefaultEntityPath(entityKeyValues);

            using (var mockService = new MockService()
                   )
            {
                var fetcher = mockService
                              .GetDefaultContext(Model)
                              .CreateFetcher(FetcherType, fetcherPath);

                mockService.ValidateParameterPassing("POST", fetcher, fetcherPath, Method,
                                                     ServerMethodNameGenerator(), null);
            }
        }
        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[]
            {
                FetcherInterface.GetMethod(_expectedMethodName),
                FetcherType.GetMethod(_expectedMethodName)
            };

            foreach (var methodInfo in methodInfos)
            {
                methodInfo.ReturnType
                .Should().Be(_expectedReturnType);
            }
        }
        public void When_the_verb_is_GET_the_Fetcher_passes_parameters_on_the_URI()
        {
            base.Init(m =>
            {
                Method              = Any.OdcmMethodGet();
                Method.Class        = Class;
                Method.ReturnType   = Class;
                Method.IsCollection = IsCollection;
                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);

                mockService.ValidateParameterPassing("GET", fetcher, fetcherPath, Method,
                                                     TargetEntity);
            }
        }
Ejemplo n.º 6
0
            private OdcmMethod WriteMethod(OdcmClass odcmClass, IEdmOperation operation)
            {
                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,
                    IsFunction          = operation.IsFunction(),
                    IsBoundToCollection = isBoundToCollection,
                    Verbs = operation.IsAction() ? OdcmAllowedVerbs.Post : OdcmAllowedVerbs.Any,
                    Class = odcmClass
                };

                AddVocabularyAnnotations(odcmMethod, operation);

                if (!odcmMethod.IsDeprecated && odcmClass.IsDeprecated)
                {
                    odcmMethod.Deprecation = odcmClass.Deprecation;
                }

                if (operation.ReturnType != null)
                {
                    odcmMethod.ReturnType   = ResolveType(operation.ReturnType);
                    odcmMethod.IsCollection = operation.ReturnType.IsCollection();

                    if (!odcmMethod.IsDeprecated && odcmMethod.ReturnType.IsDeprecated)
                    {
                        odcmMethod.Deprecation = odcmMethod.ReturnType.Deprecation;
                    }
                }

                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);
                }

                return(odcmMethod);
            }
        public string GetMethodHeader(OdcmMethod action)
        {
            var returnString = action.ReturnType == null ? "int returnValue"
            : this.GetTypeForAction(action) + action.ReturnType.Name.ToLowerFirstChar();

            return(string.Format("- (void){0}{1}:(void (^)({2}, MSOrcError *error))callback",
                                 action.Name.ToLowerFirstChar(), this.GetParamsString(action.Parameters), returnString));
        }
Ejemplo n.º 8
0
 public static Method ForEntityType(OdcmMethod odcmMethod)
 {
     return(odcmMethod.ReturnType == null
         ? (Method) new EntityVoidMethod(odcmMethod)
         : odcmMethod.IsCollection
             ? (Method) new EntityCollectionFunctionMethod(odcmMethod)
             : (Method) new EntityInstanceFunctionMethod(odcmMethod));
 }
        /// Returns a List containing the supplied method plus its overloads
        public static List <OdcmMethod> WithOverloads(this OdcmMethod odcmMethod)
        {
            var methods = new List <OdcmMethod>();

            methods.Add(odcmMethod);
            methods.AddRange(odcmMethod.Overloads);
            return(methods);
        }
        public Given_an_OdcmClass_Entity_Bound_Action()
        {
            _method = Any.OdcmMethod();

            _expectedMethodName = _method.Name + "Async";

            Init(m => m.Namespaces[0].Classes.First().Methods.Add(_method));
        }
        public Given_an_OdcmClass_Service_Bound_VoidMethod()
        {
            _method = Any.OdcmMethod();

            _expectedMethodName = _method.Name + "Async";

            Init(m => m.EntityContainer.Methods.Add(_method));
        }
Ejemplo n.º 12
0
 public EntityVoidMethod(OdcmMethod odcmMethod)
 {
     IsAsync    = true;
     ModelName  = odcmMethod.Name;
     Name       = odcmMethod.Name + "Async";
     Parameters = odcmMethod.Parameters.Select(Parameter.FromOdcmParameter);
     ReturnType = new Type(new Identifier("System.Threading.Tasks", "Task"));
 }
Ejemplo n.º 13
0
        public Given_an_OdcmClass_Entity_Bound_Function()
        {
            Init(model => model.Namespaces[0].Classes.First()
                 .Methods.Add(_method = Any.OdcmMethod(m => m.ReturnType = model.Namespaces[0].Classes.First())));

            _expectedReturnType = typeof(Task <>).MakeGenericType(ConcreteInterface);

            _expectedMethodName = _method.Name + "Async";
        }
Ejemplo n.º 14
0
 public EntityFunctionMethod(OdcmMethod odcmMethod)
 {
     InstanceName = NamesService.GetConcreteTypeName(odcmMethod.ReturnType);
     IsAsync      = true;
     ModelName    = odcmMethod.Name;
     Name         = odcmMethod.Name + "Async";
     Parameters   = odcmMethod.Parameters.Select(Parameter.FromOdcmParameter);
     ReturnType   = new Type(new Identifier("System.Threading.Tasks", "Task"), new Type(NamesService.GetPublicTypeName(odcmMethod.ReturnType)));
 }
Ejemplo n.º 15
0
        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;
        }
Ejemplo n.º 16
0
        public Given_an_OdcmClass_Entity_Collection_Bound_Action()
        {
            _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));
        }
Ejemplo n.º 17
0
        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;
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Use this method to get a collection of methods on the return type
        /// of a composable function. This will include the methods and overloads.
        /// </summary>
        /// <param name="odcmMethod">The OdcmMethod to target.</param>
        /// <returns>An ordered (by name) list of methods bound to the return
        /// type. Can be an empty list.</returns>
        public static List <OdcmMethod> GetComposableFunctionReturnTypeMethods(this OdcmMethod odcmMethod)
        {
            if (!odcmMethod.IsComposable)
            {
                throw new InvalidOperationException("This extension method is intended " +
                                                    "to only be called on a composable function.");
            }

            return(odcmMethod.ReturnType.AsOdcmClass().MethodsAndOverloads()
                   .OrderBy(m => m.Name)
                   .ToList());
        }
 public string GetTypeForAction(OdcmMethod action)
 {
     if (action.ReturnType.IsComplex())
     {
         if (action.IsCollection)
         {
             return("NSArray *");
         }
         return(action.ReturnType.IsSystem() ? action.ReturnType.GetTypeString() : action.ReturnType.GetTypeString() + " *");
     }
     return(action.ReturnType.GetTypeString());
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Deduplicates the parameter list for an overloaded method.
        /// </summary>
        /// <param name="odcmMethod">Method with potential overloads and duplicate parameters across overloads.</param>
        /// <returns>A deduplicated list of OdcmParameter.</returns>
        public static List <OdcmParameter> WithDistinctParameters(this OdcmMethod odcmMethod)
        {
            var distinctMethods = odcmMethod.WithDistinctOverloads();

            var parameters = new List <OdcmParameter>();

            foreach (var method in distinctMethods)
            {
                parameters.AddRange(method.Parameters);
            }

            return(parameters.Distinct(paramComparer).ToList());
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Use this method to get a collection of navigation properties on the return type
        /// of a composable function.
        /// </summary>
        /// <param name="odcmMethod">The OdcmMethod to target.</param>
        /// <returns>An ordered (by name) list of navigation properties bound
        /// to the return type. Can be an empty list.</returns>
        public static List <OdcmProperty> GetComposableFunctionReturnTypeNavigations(this OdcmMethod odcmMethod)
        {
            if (!odcmMethod.IsComposable)
            {
                throw new InvalidOperationException("This extension method is intended " +
                                                    "to only be called on a composable function.");
            }

            return((odcmMethod.ReturnType as OdcmClass).Properties
                   .Where(p => p.IsLink)
                   .OrderBy(p => p.Name)
                   .ToList());
        }
        public Given_an_OdcmClass_Service_Bound_Function()
        {
            Init(
                model =>
            {
                model.AddType(_expectedReturnClass = Any.OdcmClass());
                model.EntityContainer.Methods.Add(
                    _method = Any.OdcmMethod(m => m.ReturnType = _expectedReturnClass));
            });

            _expectedReturnType =
                typeof(Task <>).MakeGenericType(Proxy.GetClass(_expectedReturnClass.Namespace, _expectedReturnClass.Name));

            _expectedMethodName = _method.Name + "Async";
        }
Ejemplo n.º 23
0
        public Given_an_OdcmClass_Entity_Collection_Bound_Function()
        {
            Init(model => model.Namespaces[0].Classes.First()
                 .Methods.Add(_method = Any.OdcmMethod(m =>
            {
                m.ReturnType          = model.Namespaces[0].Classes.First();
                m.IsBoundToCollection = true;
            })));

            _expectedReturnType = typeof(Task <>).MakeGenericType(ConcreteInterface);

            _expectedMethodName = _method.Name + "Async";

            _expectedMethodParameters = _method.Parameters.Select(p => Proxy.GetClass(p.Type.Namespace, p.Type.Name));
        }
Ejemplo n.º 24
0
        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);
        }
Ejemplo n.º 25
0
        public static OdcmMethod OdcmMethod(Action <OdcmMethod> config = null)
        {
            var retVal = new OdcmMethod(Any.CSharpIdentifier());

            retVal.Parameters.AddRange(
                Any.Sequence(s => new OdcmParameter(Any.CSharpIdentifier())
            {
                Type = Any.PrimitiveOdcmType()
            }, Any.Int(0, 3)));

            if (config != null)
            {
                config(retVal);
            }

            return(retVal);
        }
        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";
        }
Ejemplo n.º 27
0
        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";
        }
Ejemplo n.º 28
0
            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)
                {
                    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_GET_the_Collection_passes_parameters_on_the_URI()
        {
            Init(model =>
            {
                Method                     = Any.OdcmMethodGet();
                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("GET", service, "", Method,
                                                     TargetEntity);
            }
        }
        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));
        }