public void AuthorizationContext_Ctor_Template_With_ServiceProvider()
        {
            IServiceProvider provider = new AuthorizationContextServiceProvider();
            using (AuthorizationContext context = new AuthorizationContext(provider))
            {

                // GetService should delegate to supplied provider
                string mockStringService = context.GetService(typeof(string)) as string;
                Assert.AreEqual("mockStringService", mockStringService, "Expected GetService to delegate to mock provider");

                // Shared validation logic for templates and container
                this.Validate_AuthorizationContext_Template(context);
                this.Validate_AuthorizationContext_ServiceContainer(context);
            }
        }
        public void AuthorizationContext_Ctor_Template_With_ServiceProvider()
        {
            IServiceProvider provider = new AuthorizationContextServiceProvider();

            using (AuthorizationContext context = new AuthorizationContext(provider))
            {
                // GetService should delegate to supplied provider
                string mockStringService = context.GetService(typeof(string)) as string;
                Assert.AreEqual("mockStringService", mockStringService, "Expected GetService to delegate to mock provider");

                // Shared validation logic for templates and container
                this.Validate_AuthorizationContext_Template(context);
                this.Validate_AuthorizationContext_ServiceContainer(context);
            }
        }
        public void AuthorizationContext_Ctor_And_Properties()
        {
            // Operation param cannot be null or empty
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, /*operation*/ null, "operationType", /*serviceProvider*/ null, items: null), "operation");
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, /*operation*/ string.Empty, "operationType", /*serviceProvider*/ null, items: null), "operation");

            // Operation param cannot be null or empty
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, "operation", /*operationType*/ null, /*serviceProvider*/ null, items: null), "operationType");
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, "operation", /*operationType*/ string.Empty, /*serviceProvider*/ null, items: null), "operationType");

            string instance = "mockEntity";   // type is only required to be object
            string operation = "testOp";
            string operationType = "Query";
            IServiceProvider serviceProvider = new AuthorizationContextServiceProvider();
            Dictionary<object, object> items = new Dictionary<object, object>();
            items["mockItem"] = "mockValue";

            // Fully formed valid ctor
            using (AuthorizationContext context = new AuthorizationContext(instance, operation, operationType, serviceProvider, items))
            {

                // Instance and Operation should be what the ctor set
                Assert.AreEqual(instance, context.Instance, "AuthorizationContext.Instance property failure");
                Assert.AreEqual(operation, context.Operation, "AuthorizationContext.Operation property failure");
                Assert.AreEqual(operationType, context.OperationType, "AuthorizationContext.Operation property failure");

                // The service provider gets wrapped, so verify we can ask for a service it knew how to provide
                string mockStringService = (string)context.GetService(typeof(string));
                Assert.AreEqual("mockStringService", mockStringService, "AuthorizationContext.GetService() failed to respect input service provider");

                // The items param should have been cloned, so verify it has the same content
                Assert.IsNotNull(context.Items, "AuthorizationContext.Items property failure");
                Assert.IsTrue(context.Items.ContainsKey("mockItem"), "AuthorizationContext.Items failed to clone input items");
                Assert.AreEqual("mockValue", context.Items["mockItem"], "AuthorizationContext.Items failed to set initial value correctly");

                // Add item to original.  The snapshot in the context should be unaffected.
                items["mockItem2"] = "mockValue2";
                Assert.IsFalse(context.Items.ContainsKey("mockItem2"), "AuthorizationContext items should have copied snapshot input Items");

                // Add an item to the context's Items to verify it is mutable
                context.Items["mockItem3"] = "mockValue3";
                Assert.IsTrue(context.Items.ContainsKey("mockItem3") && (string)context.Items["mockItem3"] == "mockValue3", "Could not modify AuthorizationContext.Items");

                // Share logic to validate ServiceContainer
                this.Validate_AuthorizationContext_ServiceContainer(context);
            }
        }
        public void AuthorizationContext_Ctor_And_Properties()
        {
            // Operation param cannot be null or empty
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, /*operation*/ null, "operationType", /*serviceProvider*/ null, items: null), "operation");
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, /*operation*/ string.Empty, "operationType", /*serviceProvider*/ null, items: null), "operation");

            // Operation param cannot be null or empty
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, "operation", /*operationType*/ null, /*serviceProvider*/ null, items: null), "operationType");
            ExceptionHelper.ExpectArgumentNullExceptionStandard(() => new AuthorizationContext(/*instance*/ null, "operation", /*operationType*/ string.Empty, /*serviceProvider*/ null, items: null), "operationType");

            string                      instance        = "mockEntity"; // type is only required to be object
            string                      operation       = "testOp";
            string                      operationType   = "Query";
            IServiceProvider            serviceProvider = new AuthorizationContextServiceProvider();
            Dictionary <object, object> items           = new Dictionary <object, object>();

            items["mockItem"] = "mockValue";

            // Fully formed valid ctor
            using (AuthorizationContext context = new AuthorizationContext(instance, operation, operationType, serviceProvider, items))
            {
                // Instance and Operation should be what the ctor set
                Assert.AreEqual(instance, context.Instance, "AuthorizationContext.Instance property failure");
                Assert.AreEqual(operation, context.Operation, "AuthorizationContext.Operation property failure");
                Assert.AreEqual(operationType, context.OperationType, "AuthorizationContext.Operation property failure");

                // The service provider gets wrapped, so verify we can ask for a service it knew how to provide
                string mockStringService = (string)context.GetService(typeof(string));
                Assert.AreEqual("mockStringService", mockStringService, "AuthorizationContext.GetService() failed to respect input service provider");

                // The items param should have been cloned, so verify it has the same content
                Assert.IsNotNull(context.Items, "AuthorizationContext.Items property failure");
                Assert.IsTrue(context.Items.ContainsKey("mockItem"), "AuthorizationContext.Items failed to clone input items");
                Assert.AreEqual("mockValue", context.Items["mockItem"], "AuthorizationContext.Items failed to set initial value correctly");

                // Add item to original.  The snapshot in the context should be unaffected.
                items["mockItem2"] = "mockValue2";
                Assert.IsFalse(context.Items.ContainsKey("mockItem2"), "AuthorizationContext items should have copied snapshot input Items");

                // Add an item to the context's Items to verify it is mutable
                context.Items["mockItem3"] = "mockValue3";
                Assert.IsTrue(context.Items.ContainsKey("mockItem3") && (string)context.Items["mockItem3"] == "mockValue3", "Could not modify AuthorizationContext.Items");

                // Share logic to validate ServiceContainer
                this.Validate_AuthorizationContext_ServiceContainer(context);
            }
        }
        public void AuthorizationContext_Ctor_From_Template()
        {
            IServiceProvider serviceProvider = new AuthorizationContextServiceProvider();

            using (AuthorizationContext template = new AuthorizationContext(serviceProvider))
            {
                // Put a known value in the template's dictionary
                IDictionary <object, object> items = template.Items;
                Assert.IsNotNull(items, "template did not autocreate Items");
                items["mockItem"] = "mockValue";

                string instance      = "mockEntity"; // type is only required to be object
                string operation     = "testOp";
                string operationType = "Query";

                // Call template-based ctor
                using (AuthorizationContext context = new AuthorizationContext(instance, operation, operationType, template))
                {
                    // Instance and Operation should be what the ctor set
                    Assert.AreEqual(instance, context.Instance, "AuthorizationContext.Instance property failure");
                    Assert.AreEqual(operation, context.Operation, "AuthorizationContext.Operation property failure");
                    Assert.AreEqual(operationType, context.OperationType, "AuthorizationContext.Operation property failure");

                    // Verify context delegates back to template's service provider
                    string mockStringService = (string)context.GetService(typeof(string));
                    Assert.AreEqual("mockStringService", mockStringService, "AuthorizationContext.GetService() failed to respect input service provider");

                    // The items param should have been cloned, so verify it has the same content
                    Assert.IsNotNull(context.Items, "AuthorizationContext.Items property failure");
                    Assert.IsTrue(context.Items.ContainsKey("mockItem"), "AuthorizationContext.Items failed to clone input items");
                    Assert.AreEqual("mockValue", context.Items["mockItem"], "AuthorizationContext.Items failed to set initial value correctly");

                    // Add item to original.  The snapshot in the context should be unaffected.
                    items["mockItem2"] = "mockValue2";
                    Assert.IsFalse(context.Items.ContainsKey("mockItem2"), "AuthorizationContext items should have copied snapshot input Items");

                    // Add an item to the context's Items to verify it is mutable
                    context.Items["mockItem3"] = "mockValue3";
                    Assert.IsTrue(context.Items.ContainsKey("mockItem3") && (string)context.Items["mockItem3"] == "mockValue3", "Could not modify AuthorizationContext.Items");

                    // Share logic to validate ServiceContainer
                    this.Validate_AuthorizationContext_ServiceContainer(context);
                }
            }
        }
        public void AuthorizationContext_Ctor_From_Template()
        {
            IServiceProvider serviceProvider = new AuthorizationContextServiceProvider();
            using (AuthorizationContext template = new AuthorizationContext(serviceProvider))
            {
                // Put a known value in the template's dictionary
                IDictionary<object, object> items = template.Items;
                Assert.IsNotNull(items, "template did not autocreate Items");
                items["mockItem"] = "mockValue";

                string instance = "mockEntity";   // type is only required to be object
                string operation = "testOp";
                string operationType = "Query";

                // Call template-based ctor
                using (AuthorizationContext context = new AuthorizationContext(instance, operation, operationType, template))
                {
                    // Instance and Operation should be what the ctor set
                    Assert.AreEqual(instance, context.Instance, "AuthorizationContext.Instance property failure");
                    Assert.AreEqual(operation, context.Operation, "AuthorizationContext.Operation property failure");
                    Assert.AreEqual(operationType, context.OperationType, "AuthorizationContext.Operation property failure");

                    // Verify context delegates back to template's service provider
                    string mockStringService = (string)context.GetService(typeof(string));
                    Assert.AreEqual("mockStringService", mockStringService, "AuthorizationContext.GetService() failed to respect input service provider");

                    // The items param should have been cloned, so verify it has the same content
                    Assert.IsNotNull(context.Items, "AuthorizationContext.Items property failure");
                    Assert.IsTrue(context.Items.ContainsKey("mockItem"), "AuthorizationContext.Items failed to clone input items");
                    Assert.AreEqual("mockValue", context.Items["mockItem"], "AuthorizationContext.Items failed to set initial value correctly");

                    // Add item to original.  The snapshot in the context should be unaffected.
                    items["mockItem2"] = "mockValue2";
                    Assert.IsFalse(context.Items.ContainsKey("mockItem2"), "AuthorizationContext items should have copied snapshot input Items");

                    // Add an item to the context's Items to verify it is mutable
                    context.Items["mockItem3"] = "mockValue3";
                    Assert.IsTrue(context.Items.ContainsKey("mockItem3") && (string)context.Items["mockItem3"] == "mockValue3", "Could not modify AuthorizationContext.Items");

                    // Share logic to validate ServiceContainer
                    this.Validate_AuthorizationContext_ServiceContainer(context);
                }
            }
        }