Ejemplo n.º 1
0
 public static User ToUser(this UserState state)
 => User.From(
     EntityId.From(state.Id),
     Name.From(state.Name),
     SocialSecurityId.From(state.Cnpj),
     Email.From(state.CommercialEmail),
     Version.From(BitConverter.ToInt32(state.RowVersion)));
Ejemplo n.º 2
0
        public void user_aggregate_create()
        {
            var fixture = new Fixture();

            fixture.Register <EntityId>(() => EntityId.From(fixture.Create <Guid>()));
            fixture.Register <Name>(() => Name.From(fixture.Create <string>()));
            fixture.Register <SocialSecurityId>(() => SocialSecurityId.From(fixture.Create <string>()));
            fixture.Register <Email>(() => Email.From(string.Format($"{fixture.Create<string>()}@teste.com")));

            fixture.Register <UserAggregationRoot>(
                () => UserAggregationRoot.CreateFrom(fixture.Create <Name>(),
                                                     fixture.Create <SocialSecurityId>(),
                                                     fixture.Create <Email>()));

            var agg = fixture.Create <UserAggregationRoot>();

            Assert.True(agg.ValidationResults.IsValid);
        }
Ejemplo n.º 3
0
        protected override CommandResult <Guid> ExecuteCommand(AddUserCommand command)
        {
            var agg = UserAggregationRoot.CreateFrom(
                Name.From(command.Name),
                SocialSecurityId.From(command.Cnpj),
                Email.From(command.CommercialEmail));

            var isSucceed = false;
            var okId      = Guid.Empty;

            if (agg.ValidationResults.IsValid)
            {
                _dbSession.Repository.Add(agg.GetChange());
                _dbSession.SaveChanges();

                agg.GetEvents().ToImmutableList()
                .ForEach(ev => Publisher.Publish(ev));

                isSucceed = true;
                okId      = agg.GetChange().Id.Value;
            }

            return(new CommandResult <Guid>(isSucceed, okId, agg.ValidationResults.Errors.ToImmutableList()));
        }
        public void user_add_command_succed()
        {
            var fixture = new Fixture().Customize(new AutoNSubstituteCustomization {
                ConfigureMembers = true
            });

            fixture.Register <EntityId>(() => EntityId.From(fixture.Create <Guid>()));
            fixture.Register <Name>(() => Name.From(fixture.Create <string>()));
            fixture.Register <Version>(() => Version.From(fixture.Create <int>()));
            fixture.Register <SocialSecurityId>(() => SocialSecurityId.From(fixture.Create <string>()));
            fixture.Register <Email>(() => Email.From(string.Format($"{fixture.Create<string>()}@teste.com")));
            fixture.Register <User>(() => User.From(fixture.Create <EntityId>(),
                                                    fixture.Create <Name>(), fixture.Create <SocialSecurityId>(),
                                                    fixture.Create <Email>(), fixture.Create <Version>()));

            var finalUser = fixture.Create <User>();

            var command = fixture.Build <AddUserCommand>()
                          .With(user => user.CommercialEmail, finalUser.CommercialEmail.Value)
                          .With(user => user.Cnpj, finalUser.Cnpj.Value)
                          .With(user => user.Name, finalUser.Name.Value)
                          .Create();

            var mediator = fixture.Create <IMediator>();
            var logger   = fixture.Create <ILogger <AddUserCommandHandler> >();
            var db       = fixture.Create <IDbSession <IUserRepository> >();
            var handler  = new AddUserCommandHandler(logger, mediator, db);

            var result = handler.Execute(command);

            db.Received().Repository.Add(finalUser);
            db.Received().SaveChanges();
            mediator.Received(1).Publish(Arg.Any <UserAddedEvent>());

            Assert.True(result.IsSucceed);
        }