public void AppendAsyncTwicePageSizeEventDoesNotAddPreviousLinkToPreviousPage(
            [Frozen(As = typeof(IAtomEventStorage))] AtomEventsInMemory storage,
            AtomFeedParser <XmlContentSerializer> parser,
            AtomEventStream <XmlAttributedTestEventX> sut,
            Generator <XmlAttributedTestEventX> eventGenerator)
        {
            var before = DateTimeOffset.Now;
            var events = eventGenerator.Take(sut.PageSize * 2).ToList();

            events.ForEach(e => sut.AppendAsync(e).Wait());

            var writtenIndex = storage.Feeds
                               .Select(parser.Parse)
                               .Single(f => f.Id == sut.Id);
            UuidIri previousPageId =
                Guid.Parse(
                    writtenIndex.Links
                    .Single(AtomEventStream.IsPreviousFeedLink)
                    .Href.ToString());
            var previousPage = storage.Feeds
                               .Select(parser.Parse)
                               .Single(f => f.Id == previousPageId);

            Assert.Equal(
                0,
                previousPage.Links.Count(AtomEventStream.IsPreviousFeedLink));
        }
        public void AppendAsyncMoreThanPageSizeEventsStoresPreviousPage(
            [Frozen(As = typeof(IAtomEventStorage))] AtomEventsInMemory storage,
            AtomFeedParser <XmlContentSerializer> parser,
            AtomEventStream <XmlAttributedTestEventX> sut,
            Generator <XmlAttributedTestEventX> eventGenerator)
        {
            var before = DateTimeOffset.Now;
            var events = eventGenerator.Take(sut.PageSize + 1).ToList();

            events.ForEach(e => sut.AppendAsync(e).Wait());

            var writtenIndex = storage.Feeds
                               .Select(parser.Parse)
                               .Single(f => f.Id == sut.Id);
            UuidIri previousPageId =
                Guid.Parse(
                    writtenIndex.Links
                    .Single(AtomEventStream.IsPreviousFeedLink)
                    .Href.ToString());

            Assert.True(
                storage.Feeds
                .Select(parser.Parse)
                .Any(f => f.Id == previousPageId),
                "The previous feed should have been stored.");
        }
Exemplo n.º 3
0
        public void NewIdReturnsUniqueId()
        {
            var x = UuidIri.NewId();
            var y = UuidIri.NewId();

            Assert.NotEqual(x, y);
        }
        public void ReadNonPersistedFeedReturnsCorrectFeed(
            AtomEventsInMemory sut,
            UuidIri id,
            IContentSerializer dummySerializer)
        {
            var expectedSelfLink = AtomLink.CreateSelfLink(
                new Uri(
                    ((Guid)id).ToString(),
                    UriKind.Relative));
            var before = DateTimeOffset.Now;

            using (var r = sut.CreateFeedReaderFor(expectedSelfLink.Href))
            {
                var actual = AtomFeed.ReadFrom(r, dummySerializer);

                Assert.Equal(id, actual.Id);
                Assert.Equal("Index of event stream " + (Guid)id, actual.Title);
                Assert.True(before <= actual.Updated, "Updated should be very recent.");
                Assert.True(actual.Updated <= DateTimeOffset.Now, "Updated should not be in the future.");
                Assert.Empty(actual.Entries);
                Assert.Contains(
                    expectedSelfLink,
                    actual.Links);
            }
        }
Exemplo n.º 5
0
        private static AtomFeed FindIndex(IEnumerable <AtomFeed> pages, UuidIri id)
        {
            var index = pages.SingleOrDefault(f => f.Id == id);

            Assert.NotNull(index);
            return(index);
        }
        public void AppendAsyncMoreThanPageSizeEventsStoresOldestEventsInPreviousPage(
            [Frozen(As = typeof(IAtomEventStorage))] AtomEventsInMemory storage,
            AtomFeedParser <XmlContentSerializer> parser,
            AtomEventStream <XmlAttributedTestEventX> sut,
            Generator <XmlAttributedTestEventX> eventGenerator)
        {
            var before = DateTimeOffset.Now;
            var events = eventGenerator.Take(sut.PageSize + 1).ToList();

            events.ForEach(e => sut.AppendAsync(e).Wait());

            var writtenIndex = storage.Feeds
                               .Select(parser.Parse)
                               .Single(f => f.Id == sut.Id);
            UuidIri previousPageId =
                Guid.Parse(
                    writtenIndex.Links
                    .Single(AtomEventStream.IsPreviousFeedLink)
                    .Href.ToString());
            var actualPreviousPage = storage.Feeds
                                     .Select(parser.Parse)
                                     .Single(f => f.Id == previousPageId);
            var expectedPreviousPage = new AtomFeedLikeness(
                before,
                previousPageId,
                events.AsEnumerable().Reverse().Skip(1).ToArray());

            Assert.True(
                expectedPreviousPage.Equals(actualPreviousPage),
                "Expected feed must match actual feed.");
        }
        public void IdIsCorrect(
            [Frozen] UuidIri expected,
            AtomEventStream <DataContractTestEventX> sut)
        {
            UuidIri actual = sut.Id;

            Assert.Equal(expected, actual);
        }
Exemplo n.º 8
0
        public void GuidCorrectlyConvertsToSut(
            UuidIri sut)
        {
            Guid    expected = sut;
            UuidIri actual   = expected;

            Assert.Equal <Guid>(expected, actual);
        }
Exemplo n.º 9
0
        public void SutCorrectlyConvertsToGuid(
            [Frozen] Guid expected,
            UuidIri sut)
        {
            Guid actual = sut;

            Assert.Equal(expected, actual);
        }
Exemplo n.º 10
0
        public void IsPreviousFeedLinkReturnsTrueForPreviousFeedLink(
            UuidIri id)
        {
            var  link   = AtomEventStream.CreatePreviousLinkFrom(id);
            bool actual = AtomEventStream.IsPreviousFeedLink(link);

            Assert.True(actual);
        }
Exemplo n.º 11
0
        public void IsPreviousFeedLinkReturnsFalsForLinkWithIncorrectRel(
            UuidIri id)
        {
            var link   = AtomEventStream.CreateSelfLinkFrom(id);
            var actual = AtomEventStream.IsPreviousFeedLink(link);

            Assert.False(actual);
        }
Exemplo n.º 12
0
        public void GetHashCodeReturnsCorrectResult(
            UuidIri sut)
        {
            var actual = sut.GetHashCode();

            var expected = ((Guid)sut).GetHashCode();

            Assert.Equal(expected, actual);
        }
Exemplo n.º 13
0
 public AtomFeedLikeness(
     DateTimeOffset minimumTime,
     UuidIri expectedId,
     params object[] expectedEvents)
 {
     this.minimumTime = minimumTime;
     this.expectedId = expectedId;
     this.expectedEvents = expectedEvents;
 }
Exemplo n.º 14
0
 public AtomFeedLikeness(
     DateTimeOffset minimumTime,
     UuidIri expectedId,
     params object[] expectedEvents)
 {
     this.minimumTime    = minimumTime;
     this.expectedId     = expectedId;
     this.expectedEvents = expectedEvents;
 }
Exemplo n.º 15
0
        public void ParseNullReturnsCorrectResult()
        {
            UuidIri actualFromTry;
            bool    couldParse = UuidIri.TryParse(null, out actualFromTry);

            Assert.Throws <ArgumentNullException>(() => UuidIri.Parse(null));

            Assert.False(couldParse, "TryParse should fail.");
            Assert.Equal(default(UuidIri), actualFromTry);
        }
Exemplo n.º 16
0
        private static AtomFeed FindLastPage(IEnumerable <AtomFeed> pages, UuidIri id)
        {
            var page = FindFirstPage(pages, id);

            while (page.Links.Any(l => l.IsNextLink))
            {
                page = FindNextPage(page, pages);
            }

            return(page);
        }
Exemplo n.º 17
0
        private static AtomFeed FindFirstPage(IEnumerable <AtomFeed> pages, UuidIri id)
        {
            var index     = FindIndex(pages, id);
            var firstLink = index.Links.SingleOrDefault(l => l.IsFirstLink);

            Assert.NotNull(firstLink);
            var firstPage = pages.SingleOrDefault(f => f.Links.Single(l => l.IsSelfLink).Href == firstLink.Href);

            Assert.NotNull(firstPage);
            return(firstPage);
        }
Exemplo n.º 18
0
        public void CreatePreviousLinkFromReturnsCorrectResult(
            UuidIri id)
        {
            AtomLink actual = AtomEventStream.CreatePreviousLinkFrom(id);

            var expected = AtomLink.CreatePreviousLink(
                new Uri(
                    ((Guid)id).ToString(),
                    UriKind.Relative));

            Assert.Equal(expected, actual);
        }
Exemplo n.º 19
0
        public void ParseStringWithIncorrectIdReturnsCorrectResult(
            DateTimeOffset notAGuid)
        {
            var incorrectlyIdd = "urn:uuid:" + notAGuid.ToString();

            UuidIri actualFromTry;
            bool    couldParse = UuidIri.TryParse(incorrectlyIdd, out actualFromTry);

            Assert.Throws <ArgumentException>(() => UuidIri.Parse(incorrectlyIdd));

            Assert.False(couldParse, "TryParse should fail.");
            Assert.Equal(default(UuidIri), actualFromTry);
        }
Exemplo n.º 20
0
        public void ParseCorrectlyFormattedStringReturnsCorrectResult(
            UuidIri expected)
        {
            var correctlyFormatted = expected.ToString();

            UuidIri actualFromTry;
            bool    couldParse = UuidIri.TryParse(correctlyFormatted, out actualFromTry);
            UuidIri actual     = UuidIri.Parse(correctlyFormatted);

            Assert.True(couldParse, "TryParse should succeed.");
            Assert.Equal(expected, actualFromTry);
            Assert.Equal(expected, actual);
        }
Exemplo n.º 21
0
        public void EnumerationStartsFromMostRecentEventEvenIfLastLinkIsStale(
            int pageCount,
            int staleCount,
            [Frozen(As = typeof(ITypeResolver))] TestEventTypeResolver dummyResolver,
            [Frozen(As = typeof(IContentSerializer))] XmlContentSerializer dummySerializer,
            [Frozen(As = typeof(IAtomEventStorage))] AtomEventsInMemory storage,
            [Frozen] UuidIri id,
            LifoEvents <IXmlAttributedTestEvent> sut,
            AtomEventObserver <XmlAttributedTestEventX> writer,
            Generator <XmlAttributedTestEventX> eventGenerator)
        {
            // Fixture setup
            var events =
                eventGenerator.Take(writer.PageSize * pageCount + 1).ToList();

            events.ForEach(writer.OnNext);

            /* Point the 'last' link to an older page, instead of to the last
             * page. This simulates that when the true last page was created,
             * the index wasn't correctly updated. This could for example
             * happen due to a network failure. */
            var writtenFeeds = storage.Feeds.Select(ParseAtomFeed);
            var lastPage     = FindLastPage(writtenFeeds, id);
            var olderPage    =
                FindPreviousPage(lastPage, writtenFeeds, staleCount);
            var staleLastLink =
                olderPage.Links.Single(l => l.IsSelfLink).ToLastLink();
            var index = FindIndex(writtenFeeds, id);

            index = index.WithLinks(index.Links
                                    .Where(l => !l.IsLastLink)
                                    .Concat(new[] { staleLastLink }));
            using (var w = storage.CreateFeedWriterFor(index))
                index.WriteTo(w, sut.Serializer);

            // Exercise system

            /* (The method being exercised is actual GetEnumerator and the
             * returned implementation of IEnumerator<T>, but ToList or ToArray
             * or similar methods triggers that.) */
            var actual = sut.ToList();

            // Verify outcome
            var expected = events.AsEnumerable().Reverse();

            Assert.True(
                expected.SequenceEqual(actual),
                "All written events should be enumerated in correct order.");
        }
Exemplo n.º 22
0
        public void ParseIncorrectlyPrefixedStringReturnsCorrectResult(
            string incorrectPrefix,
            Guid guid)
        {
            Assert.NotEqual("urn:uuid:", incorrectPrefix);
            var inccorectlyPrefixed = incorrectPrefix + guid;

            UuidIri actualFromTry;
            bool    couldParse = UuidIri.TryParse(inccorectlyPrefixed, out actualFromTry);

            Assert.Throws <ArgumentException>(() => UuidIri.Parse(inccorectlyPrefixed));

            Assert.False(couldParse, "TryParse should fail.");
            Assert.Equal(default(UuidIri), actualFromTry);
        }
Exemplo n.º 23
0
        public void TwoSutsWithIdenticalIdAreEqual(Guid guid)
        {
            UuidIri sut   = guid;
            UuidIri other = guid;

            var actual1 = sut.Equals((object)other);
            var actual2 = sut.Equals(other);
            var actual3 = sut == other;
            var actual4 = sut != other;

            Assert.True(actual1, "Equals(object)");
            Assert.True(actual2, "Equals(UuidIri)");
            Assert.True(actual3, "==");
            Assert.False(actual4, "!=");
        }
Exemplo n.º 24
0
        public void SutCanAppendAndYieldPolymorphicEvents(
            [Frozen(As = typeof(ITypeResolver))] TestEventTypeResolver dummyResolver,
            [Frozen(As = typeof(IContentSerializer))] XmlContentSerializer dummySerializer,
            [Frozen(As = typeof(IAtomEventStorage))] AtomEventsInMemory dummyInjectedIntoSut,
            [Frozen] UuidIri dummyId,
            AtomEventObserver <IXmlAttributedTestEvent> writer,
            LifoEvents <IXmlAttributedTestEvent> sut,
            XmlAttributedTestEventX tex,
            XmlAttributedTestEventY tey)
        {
            writer.AppendAsync(tex).Wait();
            writer.AppendAsync(tey).Wait();

            var expected = new IXmlAttributedTestEvent[] { tey, tex };

            Assert.True(expected.SequenceEqual(sut));
        }
Exemplo n.º 25
0
        public void SutYieldsCorrectEvents(
            [Frozen(As = typeof(ITypeResolver))] TestEventTypeResolver dummyResolver,
            [Frozen(As = typeof(IContentSerializer))] XmlContentSerializer dummySerializer,
            [Frozen(As = typeof(IAtomEventStorage))] AtomEventsInMemory dummyInjectedIntoSut,
            [Frozen] UuidIri dummyId,
            AtomEventObserver <XmlAttributedTestEventX> writer,
            FifoEvents <XmlAttributedTestEventX> sut,
            List <XmlAttributedTestEventX> expected)
        {
            expected.ForEach(e => writer.AppendAsync(e).Wait());

            Assert.True(
                expected.SequenceEqual(sut),
                "Events should be yielded in a FIFO order");
            Assert.True(
                expected.Cast <object>().SequenceEqual(sut.OfType <object>()),
                "Events should be yielded in a FIFO order");
        }
Exemplo n.º 26
0
        public void ReverseReturnsCorrectResult(
            UuidIri id,
            AtomEventsInMemory storage,
            XmlContentSerializer serializer)
        {
            var sut =
                new LifoEvents <XmlAttributedTestEventX>(id, storage, serializer);
            var expected =
                new FifoEvents <XmlAttributedTestEventX>(id, storage, serializer);

            var actual = sut.Reverse();

            var fifo = Assert.IsType <FifoEvents <XmlAttributedTestEventX> >(actual);

            Assert.Equal(expected.Id, fifo.Id);
            Assert.Equal(expected.Storage, fifo.Storage);
            Assert.Equal(expected.Serializer, fifo.Serializer);
        }
Exemplo n.º 27
0
        public void TwoSutsWithDifferentIdsAreNotEquals(
            Guid x,
            Guid y)
        {
            Assert.NotEqual(x, y);

            UuidIri sut   = x;
            UuidIri other = y;

            var actual1 = sut.Equals((object)other);
            var actual2 = sut.Equals(other);
            var actual3 = sut == other;
            var actual4 = sut != other;

            Assert.False(actual1, "Equals(object)");
            Assert.False(actual2, "Equals(UuidIri)");
            Assert.False(actual3, "==");
            Assert.True(actual4, "!=");
        }
Exemplo n.º 28
0
        public void SutYieldsPagedEvents(
            [Frozen(As = typeof(ITypeResolver))] TestEventTypeResolver dummyResolver,
            [Frozen(As = typeof(IContentSerializer))] XmlContentSerializer dummySerializer,
            [Frozen(As = typeof(IAtomEventStorage))] AtomEventsInMemory dummyInjectedIntoSut,
            [Frozen] UuidIri dummyId,
            AtomEventObserver <XmlAttributedTestEventX> writer,
            LifoEvents <XmlAttributedTestEventX> sut,
            Generator <XmlAttributedTestEventX> eventGenerator)
        {
            var expected = eventGenerator.Take(writer.PageSize * 2 + 1).ToList();

            Enumerable
            .Reverse(expected)
            .ToList()
            .ForEach(e => writer.AppendAsync(e).Wait());

            Assert.True(
                expected.SequenceEqual(sut),
                "Events should be yielded in a LIFO order");
            Assert.True(
                expected.Cast <object>().SequenceEqual(sut.OfType <object>()),
                "Events should be yielded in a LIFO order");
        }
Exemplo n.º 29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AtomFeed"/> class.
        /// </summary>
        /// <param name="id">The ID of the Atom Feed.</param>
        /// <param name="title">The title of the Atom Feed.</param>
        /// <param name="updated">
        /// The date and time the Atom Feed was last updated.
        /// </param>
        /// <param name="author">The author of the Atom Feed.</param>
        /// <param name="entries">The entries of the Atom Feed.</param>
        /// <param name="links">The links of the Atom Feed itself.</param>
        /// <remarks>
        /// <para>
        /// All values passed into this constructor are subsequently available
        /// as properties on the instance.
        /// </para>
        /// </remarks>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="title" />
        /// or
        /// <paramref name="author" />
        /// or
        /// <paramref name="entries" />
        /// or
        /// <paramref name="links" />
        /// is <see langword="null" />.
        /// </exception>
        public AtomFeed(
            UuidIri id,
            string title, 
            DateTimeOffset updated,
            AtomAuthor author,
            IEnumerable<AtomEntry> entries,
            IEnumerable<AtomLink> links)
        {
            if (title == null)
                throw new ArgumentNullException("title");
            if (author == null)
                throw new ArgumentNullException("author");
            if (entries == null)
                throw new ArgumentNullException("entries");
            if (links == null)
                throw new ArgumentNullException("links");

            this.id = id;
            this.title = title;
            this.updated = updated;
            this.author = author;
            this.entries = entries;
            this.links = links;
        }
        public void ReadNonPersistedFeedReturnsCorrectFeed(
            AtomEventsInMemory sut,
            UuidIri id,
            IContentSerializer dummySerializer)
        {
            var expectedSelfLink = AtomLink.CreateSelfLink(
                new Uri(
                    ((Guid)id).ToString(),
                    UriKind.Relative));
            var before = DateTimeOffset.Now;

            using (var r = sut.CreateFeedReaderFor(expectedSelfLink.Href))
            {
                var actual = AtomFeed.ReadFrom(r, dummySerializer);

                Assert.Equal(id, actual.Id);
                Assert.Equal("Index of event stream " + (Guid)id, actual.Title);
                Assert.True(before <= actual.Updated, "Updated should be very recent.");
                Assert.True(actual.Updated <= DateTimeOffset.Now, "Updated should not be in the future.");
                Assert.Empty(actual.Entries);
                Assert.Contains(
                    expectedSelfLink,
                    actual.Links);
            }
        }
Exemplo n.º 31
0
        public void ToStringReturnsCorrectResult([Frozen] Guid g, UuidIri sut)
        {
            var actual = sut.ToString();

            Assert.Equal("urn:uuid:" + g, actual);
        }
Exemplo n.º 32
0
 public void SutIsEquatable(UuidIri sut)
 {
     Assert.IsAssignableFrom <IEquatable <UuidIri> >(sut);
 }
Exemplo n.º 33
0
        public void IdIsCorrect([Frozen] UuidIri expected, AtomEntry sut)
        {
            UuidIri actual = sut.Id;

            Assert.Equal(expected, actual);
        }