Ejemplo n.º 1
0
        public void apply_to_should_assign_model_with_declared_api_versions_from_mapped_convention_and_attributes()
        {
            // arrange
            var controllerBuilder     = new ControllerApiVersionConventionBuilder <DecoratedController>();
            var actionBuilder         = new ActionApiVersionConventionBuilder <DecoratedController>(controllerBuilder);
            var method                = typeof(DecoratedController).GetMethod(nameof(DecoratedController.Get));
            var attributes            = method.GetCustomAttributes().Cast <object>().ToArray();
            var actionModel           = new ActionModel(method, attributes);
            var empty                 = Enumerable.Empty <ApiVersion>();
            var controllerVersionInfo = Tuple.Create(empty, empty, empty, empty);

            actionModel.SetProperty(controllerVersionInfo);
            actionBuilder.MapToApiVersion(new ApiVersion(2, 0))
            .MapToApiVersion(new ApiVersion(3, 0));

            // act
            actionBuilder.ApplyTo(actionModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = new[] { new ApiVersion(2, 0), new ApiVersion(3, 0) },
                SupportedApiVersions   = new ApiVersion[0],
                DeprecatedApiVersions  = new ApiVersion[0],
                ImplementedApiVersions = new ApiVersion[0],
            });
        }
Ejemplo n.º 2
0
        public void apply_to_should_assign_empty_model_without_api_versions_from_mapped_convention()
        {
            // arrange
            var controllerBuilder     = new ControllerApiVersionConventionBuilder <UndecoratedController>();
            var actionBuilder         = new ActionApiVersionConventionBuilder <UndecoratedController>(controllerBuilder);
            var method                = typeof(UndecoratedController).GetMethod(nameof(UndecoratedController.Get));
            var actionModel           = new ActionModel(method, new object[0]);
            var empty                 = Enumerable.Empty <ApiVersion>();
            var controllerVersionInfo = Tuple.Create(empty, empty, empty, empty);

            actionModel.SetProperty(controllerVersionInfo);

            // act
            actionBuilder.ApplyTo(actionModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = new ApiVersion[0],
                SupportedApiVersions   = new ApiVersion[0],
                DeprecatedApiVersions  = new ApiVersion[0],
                ImplementedApiVersions = new ApiVersion[0],
            });
        }
        public void apply_to_should_assign_model_with_declared_api_versions_from_mapped_convention()
        {
            // arrange
            var controllerBuilder     = new ControllerApiVersionConventionBuilder(typeof(UndecoratedController));
            var actionBuilder         = new ActionApiVersionConventionBuilder(controllerBuilder);
            var method                = typeof(UndecoratedController).GetMethod(nameof(UndecoratedController.Get));
            var attributes            = new object[] { new MapToApiVersionAttribute("2.0") };
            var actionModel           = new ActionModel(method, attributes);
            var empty                 = Enumerable.Empty <ApiVersion>();
            var controllerVersionInfo = new ControllerVersionInfo(empty, empty, empty, empty);

            actionModel.SetProperty(controllerVersionInfo);
            actionBuilder.MapToApiVersion(new ApiVersion(2, 0));

            // act
            actionBuilder.ApplyTo(actionModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().ShouldBeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = new[] { new ApiVersion(2, 0) },
                SupportedApiVersions   = new ApiVersion[0],
                DeprecatedApiVersions  = new ApiVersion[0],
                ImplementedApiVersions = new ApiVersion[0]
            });
        }
Ejemplo n.º 4
0
        public void apply_to_should_assign_model_to_controller_from_conventions_and_attributes()
        {
            // arrange
            var controllerType  = typeof(DecoratedController);
            var action          = controllerType.GetRuntimeMethod(nameof(DecoratedController.Get), Type.EmptyTypes);
            var attributes      = controllerType.GetTypeInfo().GetCustomAttributes().Cast <object>().ToArray();
            var actionModel     = new ActionModel(action, Array.Empty <object>());
            var controllerModel = new ControllerModel(controllerType.GetTypeInfo(), attributes)
            {
                Actions = { actionModel }
            };
            var controllerBuilder = new ControllerApiVersionConventionBuilder(controllerType);

            controllerBuilder.HasApiVersion(1, 0)
            .AdvertisesApiVersion(4, 0);

            // act
            controllerBuilder.ApplyTo(controllerModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = false,
                DeclaredApiVersions    = Array.Empty <ApiVersion>(),
                SupportedApiVersions   = new[] { new ApiVersion(1, 0), new ApiVersion(2, 0), new ApiVersion(3, 0), new ApiVersion(4, 0) },
                DeprecatedApiVersions  = new[] { new ApiVersion(0, 9), new ApiVersion(3, 0, "Beta") },
                ImplementedApiVersions = new[] { new ApiVersion(0, 9), new ApiVersion(1, 0), new ApiVersion(2, 0), new ApiVersion(3, 0), new ApiVersion(3, 0, "Beta"), new ApiVersion(4, 0) }
            });
        }
Ejemplo n.º 5
0
        public void apply_to_should_assign_empty_conventions_to_api_version_neutral_controller()
        {
            // arrange
            var controllerType  = typeof(UndecoratedController);
            var action          = controllerType.GetRuntimeMethod(nameof(UndecoratedController.Get), Type.EmptyTypes);
            var attributes      = Array.Empty <object>();
            var actionModel     = new ActionModel(action, attributes);
            var controllerModel = new ControllerModel(controllerType.GetTypeInfo(), attributes)
            {
                Actions = { actionModel }
            };
            var controllerBuilder = new ControllerApiVersionConventionBuilder(controllerType);

            controllerBuilder.HasDeprecatedApiVersion(0, 9)
            .HasApiVersion(2, 0)
            .AdvertisesApiVersion(3, 0)
            .AdvertisesDeprecatedApiVersion(3, 0, "Beta")
            .IsApiVersionNeutral();

            // act
            controllerBuilder.ApplyTo(controllerModel);

            // assert
            actionModel.GetProperty <ApiVersionModel>().Should().BeEquivalentTo(
                new
            {
                IsApiVersionNeutral    = true,
                DeclaredApiVersions    = Array.Empty <ApiVersion>(),
                SupportedApiVersions   = Array.Empty <ApiVersion>(),
                DeprecatedApiVersions  = Array.Empty <ApiVersion>(),
                ImplementedApiVersions = Array.Empty <ApiVersion>()
            });
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Applies the builder conventions to the specified controller action.
        /// </summary>
        /// <param name="actionModel">The <see cref="ActionModel">action model</see> to apply the conventions to.</param>
        public void ApplyTo(ActionModel actionModel)
        {
            Arg.NotNull(actionModel, nameof(actionModel));

            mappedVersions.UnionWith(from provider in actionModel.Attributes.OfType <IApiVersionProvider>()
                                     where !provider.AdvertiseOnly && !provider.Deprecated
                                     from version in provider.Versions
                                     select version);

            var controllerControllerVersionInfo = actionModel.GetProperty <ControllerVersionInfo>();
            var versionModel = new ApiVersionModel(
                declaredVersions: mappedVersions,
                supportedVersions: controllerControllerVersionInfo.Item1,
                deprecatedVersions: controllerControllerVersionInfo.Item2,
                advertisedVersions: controllerControllerVersionInfo.Item3,
                deprecatedAdvertisedVersions: controllerControllerVersionInfo.Item4);

            actionModel.SetProperty(versionModel);
        }
Ejemplo n.º 7
0
        public void apply_should_apply_configured_conventions()
        {
            // arrange
            var controllerType  = typeof(v2.UndecoratedController).GetTypeInfo();
            var action          = controllerType.GetRuntimeMethod(nameof(v2.UndecoratedController.Get), Type.EmptyTypes);
            var attributes      = Array.Empty <object>();
            var actionModel     = new ActionModel(action, attributes);
            var controllerModel = new ControllerModel(controllerType, attributes)
            {
                Actions = { actionModel }
            };
            var conventionBuilder = new ApiVersionConventionBuilder();
            var actionDescriptor  = new ActionDescriptor();

            conventionBuilder.Add(new VersionByNamespaceConvention());

            // act
            conventionBuilder.ApplyTo(controllerModel);
            actionDescriptor.SetProperty(controllerModel);
            actionDescriptor.SetProperty(actionModel.GetProperty <ApiVersionModel>());

            // assert
            actionDescriptor.MappingTo(new ApiVersion(2, 0)).Should().Be(Implicit);
        }