public void LinkFieldSerialization(IServiceProvider serviceProvider, IObjectRepositoryContainer container, string name, string path)
        {
            // Arrange
            var sut       = CreateJsonRepositorySerializer(serviceProvider, new ModelObjectSerializationContext(container));
            var link      = new LazyLink <Page>(container, new ObjectPath(UniqueId.CreateNew(), path));
            var linkField = new Field.Builder(serviceProvider)
            {
                Id      = UniqueId.CreateNew(),
                Name    = name,
                Content = FieldContent.NewLink(new FieldLinkContent(link))
            }.ToImmutable();

            // Act
            var json = Serialize(sut, linkField);

            using (var stream = new MemoryStream(Encoding.Default.GetBytes(json)))
            {
                var deserialized         = (Field)sut.Deserialize(stream, _ => throw new NotSupportedException());
                var deserializedLazyLink = deserialized.Content.MatchOrDefault(matchLink: c => c.Target);

                // Assert
                Assert.That(deserialized.Id, Is.EqualTo(linkField.Id));
                Assert.That(deserialized.Name, Is.EqualTo(name));
                Assert.That(deserializedLazyLink.Path.Path, Is.EqualTo(path));
            }
        }
Esempio n. 2
0
        public void WithModifiesLink(ObjectRepository repository, Page newLinkedPage)
        {
            // Arrange
            var field = repository.Flatten().OfType <Field>().First(
                f => f.Content.Match(() => false, matchLink: l => true));

            // Act
            var modified = repository.With(field, f => f.Content, FieldContent.NewLink(new FieldLinkContent(new LazyLink <Page>(repository.Container, newLinkedPage))));
            var link     = modified.Content.MatchOrDefault(matchLink: l => l.Target);

            // Assert
            Assert.That(link.Link, Is.EqualTo(newLinkedPage));
        }
Esempio n. 3
0
        public void LinkWithWrongObjectPathIsDetected(IObjectRepositoryContainer container, ObjectRepository repository)
        {
            // Arrange
            var linkField = repository.Flatten().OfType <Field>().FirstOrDefault(
                f => f.Content.MatchOrDefault(matchLink: l => true));

            // Act
            var failingLink = new LazyLink <Page>(container, new ObjectPath(linkField.Repository.Id, "foo"));
            var modified    = repository.With(linkField, f => f.Content, FieldContent.NewLink(new FieldLinkContent(failingLink)));
            var result      = modified.Repository.Validate();

            // Assert
            Assert.That(result, Has.Property(nameof(ValidationResult.IsValid)).False);
            Assert.That(result.Errors, Has.Count.EqualTo(1));
            Assert.That(result.ToString(), Does.Contain("Unexisting object"));
            Assert.That(result.Errors[0], Has.Property(nameof(ValidationFailure.PropertyName)).EqualTo("Content.fieldLinkContent.<Target>k__BackingField.Path"));
            Assert.That(result.Errors[0].Context.Instance, Is.EqualTo(modified));
            Assert.That(result.Errors[0].Context.Parent.Instance, Is.EqualTo(modified.Parent));
        }
Esempio n. 4
0
        public void PreCommitWhenPropertyChangeGetsFired(GitHooks sut, ObjectRepository repository, IObjectRepositoryContainer <ObjectRepository> container, Signature signature, string message)
        {
            // Arrange
            repository = container.AddRepository(repository, signature, message);
            CommitStartedEventArgs lastEvent = null;

            sut.CommitStarted += (_, args) => lastEvent = args;
            var field = repository.Flatten().OfType <Field>().First(
                f => f.Content.MatchOrDefault(matchLink: l => true));

            // Act
            var page          = repository.Applications[0].Pages[1];
            var newLinkedPage = repository.Applications[1].Pages[2];
            var modified      = repository.With(c => c
                                                .Update(field, f => f.Name, "modified field name")
                                                .Update(field, f => f.Content, FieldContent.NewLink(new FieldLinkContent(new LazyLink <Page>(container, newLinkedPage))))
                                                .Update(page, p => p.Name, "modified page name"));

            container.Commit(modified.Repository, signature, message);

            // Assert
            Assert.That(lastEvent, Is.Not.Null);
            Assert.That(lastEvent.Changes, Has.Count.EqualTo(2));
        }
Esempio n. 5
0
        public void Customize(IFixture fixture)
        {
            fixture.Register(UniqueId.CreateNew);

            var serviceProvider  = fixture.Create <IServiceProvider>();
            var containerFactory = serviceProvider.GetRequiredService <IObjectRepositoryContainerFactory>();

            var path = ContainerPath ?? RepositoryFixture.GetAvailableFolderPath();

            fixture.Customizations.Add(new ObjectRepositoryContainerSpecimenBuilder(path));
            var container = fixture.Freeze <IObjectRepositoryContainer <ObjectRepository> >();

            ObjectRepository lastRepository = default;
            var createdPages = new List <Page>();

            Page CreatePage(int position)
            {
                var page = new Page.Builder(serviceProvider)
                {
                    Id          = UniqueId.CreateNew(),
                    Name        = $"Page {position}",
                    Description = $"Description for {position}",
                    Fields      = new LazyChildren <Field>(
                        Enumerable.Range(1, FieldPerPageCount).Select(f =>
                                                                      CreateField(f))
                        .OrderBy(f => f.Id).ToImmutableList())
                }.ToImmutable();

                createdPages.Add(page);
                return(page);
            }

            Field CreateField(int position) =>
            new Field.Builder(serviceProvider)
            {
                Id      = UniqueId.CreateNew(),
                Name    = $"Field {position}",
                Content = createdPages.Any() && position % 3 == 0 ?
                          FieldContent.NewLink(new FieldLinkContent(new LazyLink <Page>(container, PickRandomPage(_ => true)))) :
                          FieldContent.Default
            }.ToImmutable();

            ObjectRepository CreateModule()
            {
                createdPages.Clear();
                lastRepository = new ObjectRepository(serviceProvider, container,
                                                      UniqueId.CreateNew(),
                                                      "Some repository",
                                                      new Version(1, 0, 0),
                                                      ImmutableList.Create <RepositoryDependency>(),
                                                      new LazyChildren <IMigration>(),
                                                      new LazyChildren <IObjectRepositoryIndex>(
                                                          ImmutableList.Create <IObjectRepositoryIndex>(new LinkFieldReferrerIndex(serviceProvider, UniqueId.CreateNew(), nameof(LinkFieldReferrerIndex)))),
                                                      new LazyChildren <Application>(
                                                          Enumerable.Range(1, ApplicationCount).Select(a =>
                                                                                                       new Application(serviceProvider, UniqueId.CreateNew(), $"Application {a}", new LazyChildren <Page>(
                                                                                                                           Enumerable.Range(1, PagePerApplicationCount).Select(p =>
                                                                                                                                                                               CreatePage(p))
                                                                                                                           .OrderBy(p => p.Id).ToImmutableList())))
                                                          .OrderBy(a => a.Id).ToImmutableList()));
                return(lastRepository);
            }

            Application PickRandomApplication() => (lastRepository ?? CreateModule()).Applications.PickRandom();
            Page PickRandomPage(Func <Page, bool> predicate) =>
            !createdPages.Any() ?
            PickRandomApplication().Pages.Last(predicate) :
            createdPages.Last(predicate);
            Field PickRandomField() => PickRandomPage(_ => true).Fields.PickRandom();

            fixture.Register(PickRandomApplication);
            fixture.Register(() => PickRandomPage(_ => true));
            fixture.Register(PickRandomField);
            fixture.Register(CreateModule);
        }