public async Task GivenNoPreviousPaths_WhenPublishingPathCreatedEvent_ThenWePublish()
        {
            var client = this.GivenIEventStoreClient()
                         .WithAppendToStreamAsync(this._pathStream);

            var reader = this.GivenIReadStreamedEvents <PathStream>()
                         .WithEvents(Enumerable.Empty <IEvent>());

            var created = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me/show/you",
            };

            var aggregate = await this
                            .GivenAggregate(reader.Object, client.Object)
                            .WithLoad();

            await aggregate
            .WhenPublishing(created)
            .ThenWePublish(client, created);

            aggregate.Paths.Select(_ => _.Name)
            .Should()
            .BeEquivalentTo(new List <string> {
                "let",
                "let/me",
                "let/me/show",
                "let/me/show/you"
            });
        }
        public async Task GivenOnePreExistingPath_WhenRemovingThePath_ThenThePathsAreEmptied()
        {
            var created = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me/show/you"
            };

            var removed = new PathRemovedEvent {
                FeatureRemoved = "bob",
                Path           = "let/me/show/you"
            };

            var client = this.GivenIEventStoreClient()
                         .WithAppendToStreamAsync(this._pathStream);

            var reader = this.GivenIReadStreamedEvents <PathStream>()
                         .WithEvents(new List <IEvent> {
                created, removed
            });

            var aggregate = await this
                            .GivenAggregate(reader.Object, client.Object)
                            .WithLoad();

            aggregate.Paths.Should().BeEmpty();
        }
        public async Task GivenPreExistingPaths_WhenRemovingThePath_ThenThePathsAreUpdated()
        {
            var created = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me/show/you"
            };

            var createdSecond = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me/show"
            };

            var createdThird = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me"
            };

            var client = this.GivenIEventStoreClient()
                         .WithAppendToStreamAsync(this._pathStream);

            var reader = this.GivenIReadStreamedEvents <PathStream>()
                         .WithEvents(new List <IEvent> {
                created, createdSecond, createdThird
            });

            var removed = new PathRemovedEvent {
                FeatureRemoved = "bob",
                Path           = "let/me/show/you"
            };

            var aggregate = await this
                            .GivenAggregate(reader.Object, client.Object)
                            .WithLoad();

            await aggregate
            .WhenPublishing(removed)
            .ThenWePublish(client, removed);

            aggregate.Paths.Should()
            .BeEquivalentTo(new List <Path>
            {
                new Path
                {
                    Name          = "let",
                    TotalFeatures = 2
                },
                new Path
                {
                    Name          = "let/me",
                    TotalFeatures = 2
                },
                new Path
                {
                    Name          = "let/me/show",
                    TotalFeatures = 1
                }
            });
        }
Esempio n. 4
0
        public static EventData ToEventData(this PathCreatedEvent pathCreatedEvent, JsonSerializerOptions settings = null !)
        {
            var contentBytes = JsonSerializer.SerializeToUtf8Bytes(pathCreatedEvent, settings);

            return(new EventData(
                       eventId: Uuid.NewUuid(),
                       type: pathCreatedEvent.Type,
                       data: contentBytes
                       ));
        }
Esempio n. 5
0
        private void Apply(PathCreatedEvent e)
        {
            var sections = PathHelper.TranformToPathLevels(e.Path);

            var existingPaths = this.Paths
                                .Where(_ => sections.Contains(_.Name))
                                .Select(_ => _.Name)
                                .ToList();
            var missingSections = sections
                                  .Except(existingPaths)
                                  .ToList();

            if (existingPaths.Any())
            {
                this.Paths = this.Paths
                             .Select(p =>
                {
                    if (sections.Contains(p.Name))
                    {
                        return(new Domain.Path
                        {
                            Name = p.Name,
                            TotalFeatures = p.TotalFeatures + 1,
                        });
                    }

                    return(p);
                })
                             .ToList();

                if (missingSections.Any())
                {
                    this.Paths = this.Paths.Concat(
                        missingSections.Select(_ =>
                                               new Domain.Path
                    {
                        Name          = _,
                        TotalFeatures = 1,
                    }))
                                 .ToList();
                }
            }
            else if (!existingPaths.Any())
            {
                this.Paths = this.Paths.Concat(
                    sections.Select(_ =>
                                    new Domain.Path
                {
                    Name          = _,
                    TotalFeatures = 1,
                }))
                             .ToList();
            }
        }
        public async Task GivenNotConflictingPaths_WhenPublishingPathCreatedEvent_ThenWePublishTheFeature()
        {
            var createdAlready = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me/show/you",
            };

            var client = this.GivenIEventStoreClient()
                         .WithAppendToStreamAsync(this._pathStream);

            var reader = this.GivenIReadStreamedEvents <PathStream>()
                         .WithEvents(new List <IEvent> {
                createdAlready
            });

            var created = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me/show",
            };

            var aggregate = await this
                            .GivenAggregate(reader.Object, client.Object)
                            .WithLoad();

            await aggregate
            .WhenPublishing(created)
            .ThenWePublish(client, created);

            aggregate.Paths.Should()
            .BeEquivalentTo(new List <Path>
            {
                new Path
                {
                    Name          = "let",
                    TotalFeatures = 2,
                },
                new Path
                {
                    Name          = "let/me",
                    TotalFeatures = 2,
                },
                new Path
                {
                    Name          = "let/me/show",
                    TotalFeatures = 2,
                },
                new Path
                {
                    Name          = "let/me/show/you",
                    TotalFeatures = 1,
                },
            });
        }
        public async Task GivenOnePreExistingPath_WhenRemovingAnUnknownPath_ThenThePathIsUntouched()
        {
            var created = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let/me/show/you"
            };

            var removed = new PathRemovedEvent {
                FeatureRemoved = "bob",
                Path           = "let/me/show/arrrrgh"
            };

            var client = this.GivenIEventStoreClient()
                         .WithAppendToStreamAsync(this._pathStream);

            var reader = this.GivenIReadStreamedEvents <PathStream>()
                         .WithEvents(new List <IEvent> {
                created, removed
            });

            var aggregate = await this
                            .GivenAggregate(reader.Object, client.Object)
                            .WithLoad();

            aggregate.Paths.Should()
            .BeEquivalentTo(new List <Path>
            {
                new Path
                {
                    Name          = "let",
                    TotalFeatures = 1
                },
                new Path
                {
                    Name          = "let/me",
                    TotalFeatures = 1
                },
                new Path
                {
                    Name          = "let/me/show",
                    TotalFeatures = 1
                },
                new Path
                {
                    Name          = "let/me/show/you",
                    TotalFeatures = 1
                }
            });
        }
        public async Task GivenUnsupportedEventInStream_WhenReading_ThenWeReturnEmptyList()
        {
            var eventOne = new PathCreatedEvent
            {
                Path = "🦄",
            };

            var results = new Dictionary <string, byte[]>
            {
                { eventOne.Type, JsonSerializer.SerializeToUtf8Bytes(eventOne) },
            };

            var client = this.GivenIEventStoreClient()
                         .WithReadStreamAsync(this._stream, results);

            await this
            .GivenFeatureStreamEventsReader(client.Object)
            .WhenReading()
            .ThenWeReturnEmptyList();
        }
        public static async Task ThenWePublish(
            this Func <Task> funk,
            Mock <IEventStoreClient> mockedClient,
            PathCreatedEvent e)
        {
            await funk();

            mockedClient.Verify(
                _ => _.AppendToStreamAsync(
                    It.IsAny <PathStream>(),
                    It.IsAny <StreamState>(),
                    It.Is <IEnumerable <EventData> >(items =>
                                                     items.All(ed =>
                                                               ed.Type.Equals(EventTypes.PathCreated) &&
                                                               JsonSerializer.Deserialize <PathCreatedEvent>(ed.Data.ToArray(), null) !.Path.Equals(e.Path, StringComparison.InvariantCultureIgnoreCase)
                                                               )),
                    It.IsAny <Action <EventStoreClientOperationOptions>?>(),
                    It.IsAny <UserCredentials?>(),
                    It.IsAny <CancellationToken>()),
                Times.Once());
        }
Esempio n. 10
0
        public async Task GivenEventsInStream_Whenreading_ThenWeReturnEventList()
        {
            var eventOne = new PathCreatedEvent
            {
                Path = "🦄",
            };

            var results = new Dictionary <string, byte[]>
            {
                { eventOne.Type, JsonSerializer.SerializeToUtf8Bytes(eventOne) },
            };

            var client = this.GivenIEventStoreClient()
                         .WithReadStreamAsync(this._stream, results);

            await PathStreamEventsReaderTestsExtenstions.ThenWeReturnEventList(this
                                                                               .GivenPathStreamEventsReader(client.Object)
                                                                               .WhenReading(), new List <IEvent> {
                eventOne
            });
        }
Esempio n. 11
0
        public async Task GivenOtherPaths_WhenPublishingPathCreatedEvent_ThenWePublish()
        {
            var createdAlready = new PathCreatedEvent {
                FeatureAdded = "derpy",
                Path         = "derpy/wants/muffins",
            };

            var client = this.GivenIEventStoreClient()
                         .WithAppendToStreamAsync(this._pathStream);

            var reader = this.GivenIReadStreamedEvents <PathStream>()
                         .WithEvents(new List <IEvent> {
                createdAlready
            });

            var created = new PathCreatedEvent {
                FeatureAdded = "bob",
                Path         = "let.me",
            };

            var aggregate = await this
                            .GivenAggregate(reader.Object, client.Object)
                            .WithLoad();

            await aggregate
            .WhenPublishing(created)
            .ThenWePublish(client, created);

            aggregate.Paths.Select(_ => _.Name)
            .Should()
            .BeEquivalentTo(new List <string> {
                "derpy",
                "derpy/wants",
                "derpy/wants/muffins",
                "let",
                "let.me"
            });
        }