Esempio n. 1
0
        static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile("config.json")
                         .Build();

            var serviceProvider = new ServiceCollection()
                                  .AddTransient <EventPublisher, EventPublisher>()
                                  .AddTransient <IEventStore>(s => new SqlEventStore(config["connectionstring"], s.GetService <EventPublisher>()))
                                  .AddTransient <StateConnector, StateConnector>()
                                  .AddTransient <ICommandHandler <CreateNewPersonCommand>, PersonCommandHandler>()
                                  .AddTransient <ICommandHandler <ChangeAgeCommand>, PersonCommandHandler>()
                                  .AddTransient <ICommandHandler <ChangeNameCommand>, PersonCommandHandler>()
                                  .BuildServiceProvider();

            Guid id = Guid.NewGuid();

            CreateNewPersonCommand newPersonCommand = new CreateNewPersonCommand
            {
                AggregateRootId = id,
                Age             = 28,
                Name            = "John Doe"
            };

            ChangeNameCommand nameToJane = new ChangeNameCommand
            {
                AggregateRootId = id,
                Name            = "Jane Doe"
            };

            ChangeAgeCommand ageTo29Command = new ChangeAgeCommand
            {
                AggregateRootId = id,
                Age             = 29
            };

            ChangeAgeCommand ageTo30Command = new ChangeAgeCommand
            {
                AggregateRootId = id,
                Age             = 30
            };

            CommandHandler commandHandler = new CommandHandler(serviceProvider);

            commandHandler.Handle(newPersonCommand);
            commandHandler.Handle(nameToJane);
            commandHandler.Handle(ageTo29Command);
            commandHandler.Handle(ageTo30Command);

            StateConnector connector   = serviceProvider.GetService <StateConnector>();
            var            foundPerson = connector.Get <Person>(id);

            Console.WriteLine(foundPerson.ToString());
        }
Esempio n. 2
0
        public void CanMoveTest()
        {
            #region Init

            var identityMock = Mock.Create<IMQ1Identity>();
            var principalMock = Mock.Create<IPrincipal>();
            Mock.Arrange(() => principalMock.Identity).Returns(identityMock);
            Csla.ApplicationContext.User = principalMock;

            var state = new StateTest("Default", "Default process state.", 1, Guid.Parse("11111111-1111-1111-1111-111111111111"), true, 0, 0, 0, 0);

            var supportStatesMock = Mock.Create<SupportStatesStub>();
            Mock.Arrange(() => supportStatesMock.CurrentState).Returns(state.Id);
            Mock.Arrange(() => supportStatesMock.CurrentStateGuid).Returns(state.Guid);
            Mock.Arrange(() => supportStatesMock.CurrentStateName).Returns(state.Name);

            var securityConfigurations = new List<IStateConnectorSecurityConfiguration>
                {
                    new StateConnectorSecurityConfiguration(1, 1, Constants.AllPersonFieldsSystemName, true),
                };
            var connector = new StateConnector(state, securityConfigurations);

            #endregion Init

            // Find exact match in security configurations.
            // not implemented
            // todo: Add test as soon as will be implemented

            #region Find config with 2 matches

            // verification by specific person isn't implemented
            // todo: Add other tests as soon as will be implemented verification by person
            Mock.Arrange(() => identityMock.IsAdmin).Returns(false);
            Mock.Arrange(() => identityMock.IsAuthenticated).Returns(true);
            Mock.Arrange(() => identityMock.BusinessUnitId).Returns(1);
            Mock.Arrange(() => identityMock.BusinessUnit).Returns("IT");
            Mock.Arrange(() => identityMock.RolesId).Returns(new List<int> { 1 });
            Mock.Arrange(() => identityMock.UserRoles).Returns("Developer");
            Assert.IsTrue(StateManagerTest.CanMove(supportStatesMock, connector));

            securityConfigurations = new List<IStateConnectorSecurityConfiguration> { new StateConnectorSecurityConfiguration(1, 1, Constants.AllPersonFieldsSystemName, false), };
            connector = new StateConnector(state, securityConfigurations);
            Assert.IsFalse(StateManagerTest.CanMove(supportStatesMock, connector));

            #endregion Find config with 2 matches

            #region Find config with 1 match

            // 1 match by Role
            Mock.Arrange(() => identityMock.BusinessUnitId).Returns(111);
            securityConfigurations = new List<IStateConnectorSecurityConfiguration>
                {
                    new StateConnectorSecurityConfiguration(Constants.AllBusinessUnitsId, 1, Constants.AllPersonFieldsSystemName, true),
                };
            connector = new StateConnector(state, securityConfigurations);
            Assert.IsTrue(StateManagerTest.CanMove(supportStatesMock, connector));

            securityConfigurations = new List<IStateConnectorSecurityConfiguration>
                {
                    new StateConnectorSecurityConfiguration(Constants.AllBusinessUnitsId, 1, Constants.AllPersonFieldsSystemName, false),
                };
            connector = new StateConnector(state, securityConfigurations);
            Assert.IsFalse(StateManagerTest.CanMove(supportStatesMock, connector));

            // 1 match by BusinessUnit
            Mock.Arrange(() => identityMock.BusinessUnitId).Returns(1);
            Mock.Arrange(() => identityMock.RolesId).Returns(new List<int> { 111 });
            securityConfigurations = new List<IStateConnectorSecurityConfiguration> { new StateConnectorSecurityConfiguration(1, Constants.AllRolesId, Constants.AllPersonFieldsSystemName, true), };
            connector = new StateConnector(state, securityConfigurations);
            Assert.IsTrue(StateManagerTest.CanMove(supportStatesMock, connector));

            #endregion Find config with 1 match

            #region Find config with any match

            Mock.Arrange(() => identityMock.BusinessUnitId).Returns(222);
            Mock.Arrange(() => identityMock.RolesId).Returns(new List<int>{222});
            securityConfigurations = new List<IStateConnectorSecurityConfiguration>
                                         {
                                             new StateConnectorSecurityConfiguration(
                                                 Constants.AllBusinessUnitsId,
                                                 Constants.AllRolesId,
                                                 Constants.AllPersonFieldsSystemName,
                                                 true),
                                         };
            connector = new StateConnector(state, securityConfigurations);
            Assert.IsTrue(StateManagerTest.CanMove(supportStatesMock, connector));

            securityConfigurations = new List<IStateConnectorSecurityConfiguration>
                                         {
                                             new StateConnectorSecurityConfiguration(
                                                 Constants.AllBusinessUnitsId,
                                                 Constants.AllRolesId,
                                                 Constants.AllPersonFieldsSystemName,
                                                 false),
                                         };
            connector = new StateConnector(state, securityConfigurations);
            Assert.IsFalse(StateManagerTest.CanMove(supportStatesMock, connector));

            #endregion Find config with any match

            #region identity dependent

            // if not authenticated
            Mock.Arrange(() => identityMock.IsAuthenticated).Returns(false);
            Mock.Arrange(() => identityMock.IsAdmin).Returns(true);
            Assert.IsFalse(StateManagerTest.CanMove(supportStatesMock, connector));


            // if admin
            Mock.Arrange(() => identityMock.IsAuthenticated).Returns(true);
            Mock.Arrange(() => identityMock.IsAdmin).Returns(true);
            Assert.IsTrue(StateManagerTest.CanMove(supportStatesMock, connector));

            // identity is null
            Csla.ApplicationContext.User = null;
            Assert.IsFalse(StateManagerTest.CanMove(supportStatesMock, connector));

            #endregion identity dependent
        }
 public PersonCommandHandler(StateConnector connector)
 {
     _stateConnector = connector;
 }