public void Should_allow_valid_transition_for_any_entities() { IMobStator mobStator = GetMobStatorWithFiveCorrectEntities(); var user = new User { Status = UserStatus.Striked }; var subscription = new UserSubscription { Status = SubscriptionStatus.Active }; var space = new PersonalSpace { Status = PersonalSpaceStatus.Granted }; var ranking = new Ranking { Status = RankingStatus.Tracked }; var account = new Account { Status = AccountStatus.Blocked }; var @event = new UserBlockedEvent(); var result = mobStator.Go(@event, user, subscription, account, space, ranking); Assert.Throws <StatorConfigurationException>(() => mobStator.Go(@event, user, space)); Assert.Equal(UserStatus.Deleted, user.Status); Assert.Equal(SubscriptionStatus.Closed, subscription.Status); Assert.Equal(PersonalSpaceStatus.Blocked, space.Status); Assert.Equal(RankingStatus.Untracked, ranking.Status); Assert.Equal(AccountStatus.Deleted, account.Status); Assert.True(result.Success); }
public void Should_reject_transition_for_any_inconsistent_entities() { var mobStator = GetMobStatorWithFiveCorrectEntities(); var user = new User { Status = UserStatus.Active }; var subscription = new UserSubscription { Status = SubscriptionStatus.Active }; var space = new PersonalSpace { Status = PersonalSpaceStatus.Granted }; var account = new Account { Status = AccountStatus.Blocked }; var account2 = new Account { Status = AccountStatus.Existed }; var @event = new UserBlockedEvent(); Assert.Throws <StatorConfigurationException>(() => mobStator.Go(@event, user, subscription, account, space, account2)); Assert.Throws <StatorConfigurationException>(() => mobStator.Go(@event, user)); Assert.Equal(UserStatus.Active, user.Status); Assert.Equal(SubscriptionStatus.Active, subscription.Status); Assert.Equal(PersonalSpaceStatus.Granted, space.Status); Assert.Equal(AccountStatus.Blocked, account.Status); Assert.Equal(AccountStatus.Existed, account2.Status); }
public void Should_do_rollback_for_5_entities() { IMobStator mobStator = GetBrokenMobStatorWithFiveEntities(true); var user = new User { Status = UserStatus.Striked }; var subscription = new UserSubscription { Status = SubscriptionStatus.Active }; var space = new PersonalSpace { Status = PersonalSpaceStatus.Granted }; var ranking = new Ranking { Status = RankingStatus.Tracked }; var account = new Account { Status = AccountStatus.Blocked }; var @event = new UserBlockedEvent(); Assert.Throws <Exception>(() => mobStator.Go(@event, user, subscription, account, space, ranking)); Assert.Equal(UserStatus.Striked, user.Status); Assert.Equal(SubscriptionStatus.Active, subscription.Status); Assert.Equal(PersonalSpaceStatus.Granted, space.Status); Assert.Equal(RankingStatus.Tracked, ranking.Status); Assert.Equal(AccountStatus.Blocked, account.Status); }
public void Should_invoke_transition_handlers() { var handlerBeforeTransition1Fired = false; var handlerAfterTransition1Fired = false; var handlerBeforeTransition2Fired = false; var handlerAfterTransition2Fired = false; var stator = Stator <User, UserStatus> .InitStator() .State(x => x.Status) .ForEvent <UserBlockedEvent>() .SetTransition(UserStatus.Active, UserStatus.Inactive) .WithActionBeforeTransition((entity, e) => handlerBeforeTransition1Fired = true) .WithActionAfterTransition((entity, e) => handlerAfterTransition1Fired = true) .ConfirmTransition() .ConfirmEvent() .Build(); var @event = new UserBlockedEvent(); var user = new User { Status = UserStatus.Active }; var result = stator.Go(user, @event); Assert.True(handlerBeforeTransition1Fired); Assert.False(handlerBeforeTransition2Fired); Assert.True(handlerAfterTransition1Fired); Assert.False(handlerAfterTransition2Fired); }
private void TryRaiseUserBlockedEvents(string eventName, JObject jsonObjectEvent) { var json = jsonObjectEvent.ToString(); var userBlockedEvent = jsonObjectEvent[eventName]; var blockedEventInfos = ExtractUserToUserEventDTOs(userBlockedEvent); blockedEventInfos.ForEach(blockedEventInfo => { var sourceUser = _factories.CreateUser(blockedEventInfo.Source); var targetUser = _factories.CreateUser(blockedEventInfo.Target); var timestamp = long.Parse(blockedEventInfo.CreatedTimestamp); var dateOffset = DateTimeOffset.FromUnixTimeMilliseconds(timestamp); var accountActivityEvent = new AccountActivityEvent <Tuple <IUser, IUser> >(new Tuple <IUser, IUser>(sourceUser, targetUser)) { AccountUserId = AccountUserId, EventDate = dateOffset.UtcDateTime, Json = json }; if (blockedEventInfo.Type == "block") { var eventArgs = new UserBlockedEvent(accountActivityEvent); this.Raise(UserBlocked, eventArgs); if (eventArgs.InResultOf == UserBlockedRaisedInResultOf.Unknown) { this.Raise(EventKnownButNotFullySupportedReceived, new EventKnownButNotSupported(json, eventArgs)); } } else if (blockedEventInfo.Type == "unblock") { var eventArgs = new UserUnblockedEvent(accountActivityEvent); this.Raise(UserUnblocked, eventArgs); if (eventArgs.InResultOf == UserUnblockedRaisedInResultOf.Unknown) { this.Raise(EventKnownButNotFullySupportedReceived, new EventKnownButNotSupported(json, eventArgs)); } } else { this.Raise(UnsupportedEventReceived, new UnsupportedMessageReceivedEvent(json)); } }); }
public void Should_not_do_rollback_for_2_entities() { IMobStator <User, UserSubscription> mobStator = GetBrokenMobStatorForTwoEntities(false); var user = new User { Status = UserStatus.Active }; var subscription = new UserSubscription { Status = SubscriptionStatus.Active }; var @event = new UserBlockedEvent(); Assert.Throws <Exception>(() => mobStator.Go(@event, Tuple.Create(user, subscription))); Assert.NotEqual(UserStatus.Active, user.Status); Assert.NotEqual(SubscriptionStatus.Active, subscription.Status); }
public void Should_invoke_transition_miss_handlers() { var exMessage = "TM"; var stator = Stator <User, UserStatus> .InitStator() .State(x => x.Status) .ForEvent <UserBlockedEvent>() .WithTransitionMissHandler((_, __) => throw new Exception(exMessage)) .SetTransition(UserStatus.Active, UserStatus.Inactive) .ConfirmTransition() .ConfirmEvent() .Build(); var @event = new UserBlockedEvent(); var user = new User { Status = UserStatus.Deleted }; var ex = Assert.Throws <Exception>(() => stator.Go(user, @event)); Assert.Equal(exMessage, ex.Message); }
public void Should_check_condition_and_invoke_condition_mismatch_handlers() { var exMessage = "CF"; var stator = Stator <User, UserStatus> .InitStator() .State(x => x.Status) .ForEvent <UserBlockedEvent>() .SetTransition(UserStatus.Active, UserStatus.Inactive) .Match((u, e) => e.Args?.Length > 0) .Or((_, __) => throw new Exception(exMessage)) .ConfirmTransition() .ConfirmEvent() .Build(); var @event = new UserBlockedEvent(); var user = new User { Status = UserStatus.Active }; var ex = Assert.Throws <Exception>(() => stator.Go(user, @event)); Assert.Equal(exMessage, ex.Message); }
private void UserBlocked(object sender, UserBlockedEvent e) { Console.WriteLine($">>> Account user ({e.BlockedBy}) has blocked {e.BlockedUser}"); }