Exemple #1
0
        public static void Execute___Should_return_registered_protocol___When_called()
        {
            // Arrange
            var systemUnderTest = new ChainOfResponsibilityProtocolFactory();

            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();
            IProtocol protocol3 = new SharedOperationProtocol2();

            var protocolFactory1 = protocol1.ToProtocolFactory();
            var protocolFactory2 = protocol2.ToProtocolFactory();
            var protocolFactory3 = protocol3.ToProtocolFactory();

            systemUnderTest.AddToEndOfChain(protocolFactory1);
            systemUnderTest.AddToEndOfChain(protocolFactory2);
            systemUnderTest.AddToEndOfChain(protocolFactory3);

            var operation1 = new GetProtocolOp(new SharedOperation());
            var operation2 = new GetProtocolOp(new SiblingOperation1());
            var operation3 = new GetProtocolOp(new SiblingOperation2());

            // Act
            var actual1 = systemUnderTest.Execute(operation1);
            var actual2 = systemUnderTest.Execute(operation2);
            var actual3 = systemUnderTest.Execute(operation3);

            // Assert
            actual1.AsTest().Must().BeSameReferenceAs(protocol1);
            actual2.AsTest().Must().BeSameReferenceAs(protocol2);
            actual3.AsTest().Must().BeSameReferenceAs(protocol2);
        }
Exemple #2
0
        public static void Constructor___Should_register_protocol_factories_in_order_specified___When_called()
        {
            // Arrange
            IProtocol protocol1 = new SharedOperationProtocol1();
            IProtocol protocol2 = new SiblingOperationProtocol();
            IProtocol protocol3 = new SharedOperationProtocol2();

            var protocolFactory1 = protocol1.ToProtocolFactory();
            var protocolFactory2 = protocol2.ToProtocolFactory();
            var protocolFactory3 = protocol3.ToProtocolFactory();

            var systemUnderTest = new ChainOfResponsibilityProtocolFactory(new[] { protocolFactory1, protocolFactory2, protocolFactory3 });

            var operation1 = new GetProtocolOp(new SharedOperation());
            var operation2 = new GetProtocolOp(new SiblingOperation1());
            var operation3 = new GetProtocolOp(new SiblingOperation2());

            // Act
            var actual1 = systemUnderTest.Execute(operation1);
            var actual2 = systemUnderTest.Execute(operation2);
            var actual3 = systemUnderTest.Execute(operation3);

            // Assert
            actual1.AsTest().Must().BeSameReferenceAs(protocol1);
            actual2.AsTest().Must().BeSameReferenceAs(protocol2);
            actual3.AsTest().Must().BeSameReferenceAs(protocol2);
        }
Exemple #3
0
        public static void Execute___Should_throw_ArgumentNullException___When_parameter_operation_is_null()
        {
            // Arrange
            var systemUnderTest = new ChainOfResponsibilityProtocolFactory();

            // Act
            var actual = Record.Exception(() => systemUnderTest.Execute(null));

            // Assert
            actual.AsTest().Must().BeOfType <ArgumentNullException>();
            actual.Message.AsTest().Must().ContainString("operation");
        }
Exemple #4
0
        private static ChainOfResponsibilityProtocolFactory BuildProtocolFactoryToExecuteAllOperations(
            this ReportCache reportCache,
            DateTime timestampUtc,
            IReadOnlyCollection <Func <IProtocolFactory, IProtocolFactory> > protocolFactoryFuncs,
            IReadOnlyCollection <Type> additionalTypesForCoreCellOps,
            Func <RecalcPhase> getRecalcPhaseFunc)
        {
            protocolFactoryFuncs = protocolFactoryFuncs ?? new List <Func <IProtocolFactory, IProtocolFactory> >();

            if (protocolFactoryFuncs.Any(_ => _ == null))
            {
                throw new ArgumentException(Invariant($"{nameof(protocolFactoryFuncs)} contains a null element."));
            }

            additionalTypesForCoreCellOps = additionalTypesForCoreCellOps ?? new List <Type>();

            if (additionalTypesForCoreCellOps.Any(_ => _ == null))
            {
                throw new ArgumentException(Invariant($"{nameof(additionalTypesForCoreCellOps)} contains a null element."));
            }

            var result = new ChainOfResponsibilityProtocolFactory();

            // Add caller's protocols to the chain of responsibility.
            foreach (var protocolFactoryFunc in protocolFactoryFuncs)
            {
                result.AddToEndOfChain(protocolFactoryFunc(result));
            }

            // Add DataStructureCellProtocols{TValue} and DataStructureConvenienceProtocols{TResult} to chain of responsibility.
            var typesForCoreCellOps = DefaultTypesSupportedForCoreCellOps
                                      .Concat(additionalTypesForCoreCellOps)
                                      .ToList();

            var coreProtocolsFactory = new ProtocolFactory();

            ConstructorInfo GetCellProtocolsFunc(Type type) => typeof(DataStructureCellProtocols <>).MakeGenericType(type).GetConstructors().Single();
            ConstructorInfo GetConvenienceProtocolsFunc(Type type) => typeof(DataStructureConvenienceProtocols <>).MakeGenericType(type).GetConstructors().Single();

            var cellProtocolsConstructorInfoParams        = new object[] { reportCache, result, timestampUtc, getRecalcPhaseFunc };
            var convenienceProtocolsConstructorInfoParams = new object[] { result };

            foreach (var typeForCoreCellOps in typesForCoreCellOps)
            {
                RegisterProtocols(typeForCoreCellOps, CachedTypeToCellProtocolsConstructorInfoMap, coreProtocolsFactory, GetCellProtocolsFunc, cellProtocolsConstructorInfoParams);

                RegisterProtocols(typeForCoreCellOps, CachedTypeToConvenienceProtocolsConstructorInfoMap, coreProtocolsFactory, GetConvenienceProtocolsFunc, convenienceProtocolsConstructorInfoParams);
            }

            result.AddToEndOfChain(coreProtocolsFactory);

            return(result);
        }
Exemple #5
0
        public static void Execute___Should_return_null___When_there_is_no_protocol_registered_for_the_operation_and_missingProtocolStrategy_is_ReturnNull()
        {
            // Arrange
            var systemUnderTest = new ChainOfResponsibilityProtocolFactory();

            var operation = new GetProtocolOp(new SharedOperation(), MissingProtocolStrategy.ReturnNull);

            // Act
            var actual = systemUnderTest.Execute(operation);

            // Assert
            actual.AsTest().Must().BeNull();
        }
Exemple #6
0
        public static void Execute___Should_throw_OpExecutionFailedException___When_there_is_no_protocol_registered_for_the_operation_and_missingProtocolStrategy_is_Throw()
        {
            // Arrange
            var systemUnderTest = new ChainOfResponsibilityProtocolFactory();

            var operation = new GetProtocolOp(new SharedOperation(), MissingProtocolStrategy.Throw);

            // Act
            var actual = Record.Exception(() => systemUnderTest.Execute(operation));

            // Assert
            actual.AsTest().Must().BeOfType <OpExecutionFailedException>();
            actual.Message.AsTest().Must().ContainString(Invariant($"There is no protocol registered for the specified operation: '{nameof(ChainOfResponsibilityProtocolFactoryTest)}.{nameof(SharedOperation)}'"));
        }