public Task Clone_MergeGraphSyncerThrows_ExceptionPropagates()
        {
            A.CallTo(() => PreviewAllowSync.Result)
            .Returns(AllowSyncResult.Allowed);

            A.CallTo(() => PreviewMergeGraphSyncer.SyncToGraphReplicaSet())
            .Throws(() => new Exception());

            return(Assert.ThrowsAsync <Exception>(() => SyncOrchestrator.Clone(ContentItem)));
        }
        public async Task Clone_SyncAllowedMatrix_SyncCalled(
            AllowSyncResult allowSyncAllowedResult,
            bool expectedSyncCalled)
        {
            A.CallTo(() => PreviewAllowSync.Result)
            .Returns(allowSyncAllowedResult);

            await SyncOrchestrator.Clone(ContentItem);

            A.CallTo(() => PreviewMergeGraphSyncer.SyncToGraphReplicaSet())
            .MustHaveHappened(expectedSyncCalled?1:0, Times.Exactly);
        }
        public async Task Update_PreviewSyncToGraphReplicaSetThrows_ExceptionPropagates()
        {
            A.CallTo(() => PublishedAllowSync.Result)
            .Returns(AllowSyncResult.Allowed);

            A.CallTo(() => PreviewAllowSync.Result)
            .Returns(AllowSyncResult.Allowed);

            A.CallTo(() => PreviewMergeGraphSyncer.SyncToGraphReplicaSet())
            .Throws(() => new Exception());

            await Assert.ThrowsAsync <Exception>(() => SyncOrchestrator.Update(ContentItem, ContentItem));
        }
        public async Task Update_SyncToGraphReplicaSetOnPreviewGraphCalled(
            AllowSyncResult publishedAllowSyncResult,
            AllowSyncResult previewAllowSyncResult,
            int publishedCalled)
        {
            A.CallTo(() => PublishedAllowSync.Result)
            .Returns(publishedAllowSyncResult);

            A.CallTo(() => PreviewAllowSync.Result)
            .Returns(previewAllowSyncResult);

            await SyncOrchestrator.Update(ContentItem, ContentItem);

            A.CallTo(() => PreviewMergeGraphSyncer.SyncToGraphReplicaSet())
            .MustHaveHappened(publishedCalled, Times.Exactly);
        }
        public SyncOrchestratorTestsBase()
        {
            ContentDefinitionManager = A.Fake <IContentDefinitionManager>();
            Notifier = A.Fake <GraphSyncNotifier>();

            PreviewGraphReplicaSet = A.Fake <IGraphReplicaSet>();
            A.CallTo(() => PreviewGraphReplicaSet.Name)
            .Returns(GraphReplicaSetNames.Preview);

            PublishedGraphReplicaSet = A.Fake <IGraphReplicaSet>();
            A.CallTo(() => PublishedGraphReplicaSet.Name)
            .Returns(GraphReplicaSetNames.Published);

            var graphReplicaSets = new Dictionary <string, IGraphReplicaSet>
            {
                { GraphReplicaSetNames.Preview, PreviewGraphReplicaSet },
                { GraphReplicaSetNames.Published, PublishedGraphReplicaSet },
            };

            GraphCluster = A.Fake <IGraphCluster>();
            A.CallTo(() => GraphCluster.GetGraphReplicaSet(A <string> ._))
            .ReturnsLazily <IGraphReplicaSet, string>(graphReplicaSetName => graphReplicaSets[graphReplicaSetName]);

            PublishedContentItemVersion = A.Fake <IPublishedContentItemVersion>();
            A.CallTo(() => PublishedContentItemVersion.GraphReplicaSetName)
            .Returns(GraphReplicaSetNames.Published);

            ServiceProvider = A.Fake <IServiceProvider>();
            Logger          = A.Fake <ILogger <SyncOrchestrator> >();

            ContentItem = new ContentItem();

            ContentManager = A.Fake <IContentManager>();
            A.CallTo(() => ServiceProvider.GetService(A <Type> .That.Matches(
                                                          t => t.Name == nameof(IContentManager))))
            .Returns(ContentManager);

            PreviewMergeGraphSyncer   = A.Fake <IMergeGraphSyncer>();
            PublishedMergeGraphSyncer = A.Fake <IMergeGraphSyncer>();

            PreviewAllowSync = A.Fake <IAllowSync>();
            A.CallTo(() => PreviewMergeGraphSyncer.SyncAllowed(
                         A <IGraphReplicaSet> .That.Matches(s => s.Name == GraphReplicaSetNames.Preview),
                         A <ContentItem> ._, A <IContentManager> ._, A <IGraphMergeContext?> ._))
            .Returns(PreviewAllowSync);

            PublishedAllowSync = A.Fake <IAllowSync>();
            A.CallTo(() => PublishedMergeGraphSyncer.SyncAllowed(
                         A <IGraphReplicaSet> .That.Matches(s => s.Name == GraphReplicaSetNames.Published),
                         A <ContentItem> ._, A <IContentManager> ._, A <IGraphMergeContext?> ._))
            .Returns(PublishedAllowSync);

            EventGridPublishingHandler = A.Fake <IContentOrchestrationHandler>();
            A.CallTo(() => EventGridPublishingHandler.Published(A <IOrchestrationContext> .Ignored)).Returns(Task.CompletedTask);
            A.CallTo(() => EventGridPublishingHandler.DraftSaved(A <IOrchestrationContext> .Ignored)).Returns(Task.CompletedTask);
            A.CallTo(() => EventGridPublishingHandler.Cloned(A <IOrchestrationContext> .Ignored)).Returns(Task.CompletedTask);
            A.CallTo(() => EventGridPublishingHandler.Unpublished(A <IOrchestrationContext> .Ignored)).Returns(Task.CompletedTask);
            A.CallTo(() => EventGridPublishingHandler.DraftDiscarded(A <IOrchestrationContext> .Ignored)).Returns(Task.CompletedTask);
            A.CallTo(() => EventGridPublishingHandler.Deleted(A <IOrchestrationContext> .Ignored)).Returns(Task.CompletedTask);

            SyncOrchestrator = new SyncOrchestrator(
                ContentDefinitionManager,
                Notifier,
                GraphCluster,
                ServiceProvider,
                Logger,
                PublishedContentItemVersion,
                new List <IContentOrchestrationHandler> {
                EventGridPublishingHandler
            });

            TestActivity = new Activity("UnitTest").Start();
        }