Exemplo n.º 1
0
        public void Given_setup_policy_When_restoring_from_snapshot_Then_policy_is_the_same_as_before()
        {
            var            policy          = new InsurancePolicy();
            var            address         = Address.New <InsurancePolicy>();
            IAggregateRoot policyAggregate = policy;

            policyAggregate.Apply(new PolicyCreatedEvent(address.Id));
            var durationSetEvent =
                policyAggregate.ApplyEvent(new PolicyDurationSetEvent(address.Id, TimeSpan.FromDays(30)));
            var policyAmountEvent = policyAggregate.ApplyEvent(new PolicyAmountSetEvent(address.Id, 20m));
            var issuedEvent       = policyAggregate.ApplyEvent(new PolicyIssuedEvent(address.Id, DateTimeOffset.Now));

            policyAggregate.Apply(new PolicyTimePassedEvent(address.Id, DateTimeOffset.Now.AddMinutes(1)));
            policyAggregate.Apply(new ClaimFulfilledEvent(address.Id, 2m));
            policyAggregate.Apply(new PolicyExpiredEvent(address.Id));

            var snapshot = (SnapshotState <PolicyState>)(policy as ISupportSnapshots).BuildSnapshot();

            snapshot.Address.Should().BeEquivalentTo(address);
            snapshot.Id.Should().NotBeNullOrEmpty();
            snapshot.Version.Should().Be(policy.Version);
            snapshot.State.Duration.Should().Be(durationSetEvent.Duration);
            snapshot.State.IssueDate.Should().Be(issuedEvent.Issued);

            var restoredSnapshot =
                ((InsurancePolicy) new InsurancePolicy().RestoreFromSnapshot(snapshot)).BuildSnapshot();

            snapshot.Should().BeEquivalentTo(restoredSnapshot, o => o.Excluding(s => s.Id));
        }
Exemplo n.º 2
0
        public void Given_policy_with_duration_When_issuing_without_amount_Then_get_an_error()
        {
            var            policy          = new InsurancePolicy();
            IAggregateRoot policyAggregate = policy;

            policyAggregate.Apply(new PolicyCreatedEvent());
            policyAggregate.Apply(new PolicyDurationSetEvent(policy.Address.Id, TimeSpan.FromDays(1)));

            policy.Invoking(async p => await p.Execute(new IssuePolicyCommand(policy.Address)))
            .Should().Throw <PolicyZeroAmountException>();
        }
Exemplo n.º 3
0
        public async Task Given_policy_with_duration_and_amount_When_issuing_Then_get_issued_event()
        {
            var            policy          = new InsurancePolicy();
            IAggregateRoot policyAggregate = policy;

            policyAggregate.Apply(new PolicyCreatedEvent());
            policyAggregate.Apply(new PolicyDurationSetEvent(policy.Address.Id, TimeSpan.FromDays(1)));
            policyAggregate.Apply(new PolicyAmountSetEvent(policy.Address.Id, 10m));

            var issueDate = DateTimeOffset.Now;
            var events    = await policy.Execute(new IssuePolicyCommand(policy.Address, issueDate));

            events.Should().BeLike(new PolicyIssuedEvent(policy.Address.Id, issueDate));
        }
Exemplo n.º 4
0
        public void Given_policy_When_apply_event_Then_version_is_changed()
        {
            var            policy          = new InsurancePolicy();
            IAggregateRoot policyAggregate = policy;

            policy.Version.Should().Be(0);
            policyAggregate.Apply(new PolicyCreatedEvent());
        }
Exemplo n.º 5
0
        public static IAggregateRoot Apply(this IAggregateRoot aggregate, params IAggregateEvent[] events)
        {
            foreach (var e in events)
            {
                aggregate.Apply(e);
            }

            return(aggregate);
        }
Exemplo n.º 6
0
        public void Given_policy_When_issuing_without_duration_Then_get_an_error()
        {
            var            policy          = new InsurancePolicy();
            IAggregateRoot policyAggregate = policy;

            policyAggregate.Apply(new PolicyCreatedEvent());

            policy.Invoking(async p => await p.Execute(new IssuePolicyCommand(policy.Address)))
            .Should().Throw <PolicyMissingDurationException>();
        }
Exemplo n.º 7
0
        public void Given_policy_When_building_a_snapshot_Then_it_has_fields_from_the_aggregate()
        {
            var            policy          = new InsurancePolicy();
            var            address         = Address.New <InsurancePolicy>();
            IAggregateRoot policyAggregate = policy;

            policyAggregate.Apply(new PolicyCreatedEvent(address.Id));
            policyAggregate.Apply(new PolicyDurationSetEvent(address.Id, TimeSpan.FromDays(30)));
            var issuedDate = DateTimeOffset.Now;

            policyAggregate.Apply(new PolicyIssuedEvent(address.Id, issuedDate));

            var snapshot = (SnapshotState <PolicyState>)(policy as ISupportSnapshots).BuildSnapshot();

            snapshot.Address.Should().BeEquivalentTo(address);
            snapshot.Id.Should().NotBeNullOrEmpty();
            snapshot.Version.Should().Be(policy.Version);
            snapshot.State.Duration.Should().Be(TimeSpan.FromDays(30));
            snapshot.State.IssueDate.Should().Be(issuedDate);
        }
Exemplo n.º 8
0
 public static T ApplyEvent <T>(this IAggregateRoot aggregate, T evt) where T : IAggregateEvent
 {
     aggregate.Apply(evt);
     return(evt);
 }