public async Task GivenIHaveCreatedAndPersistedANewInstanceOfTheDataCatalogWorkflowWithId(string workflowId)
        {
            IWorkflowMessageQueue workflowMessageQueue =
                ContainerBindings.GetServiceProvider(this.featureContext).GetService <IWorkflowMessageQueue>();
            Workflow workflow = DataCatalogWorkflowFactory.Create(workflowId, workflowMessageQueue);

            ITenantedWorkflowStoreFactory storeFactory =
                ContainerBindings.GetServiceProvider(this.featureContext).GetService <ITenantedWorkflowStoreFactory>();
            ITenantProvider tenantProvider =
                ContainerBindings.GetServiceProvider(this.featureContext).GetService <ITenantProvider>();

            IWorkflowStore store = await storeFactory.GetWorkflowStoreForTenantAsync(tenantProvider.Root).ConfigureAwait(false);

            await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(async() =>
            {
                try
                {
                    Workflow oldWorkflow = await store.GetWorkflowAsync(workflowId).ConfigureAwait(false);
                    workflow.ETag        = oldWorkflow.ETag;
                }
                catch (WorkflowNotFoundException)
                {
                    // Don't care if there is no old workflow.
                }

                await store.UpsertWorkflowAsync(workflow).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        public async Task ThenTheWorkflowInstanceWithIdShouldContainContextItems(string instanceId, Table expectedContextItems)
        {
            ITenantedWorkflowInstanceStoreFactory instanceStoreFactory = ContainerBindings.GetServiceProvider(this.featureContext)
                                                                         .GetService <ITenantedWorkflowInstanceStoreFactory>();

            ITenantProvider tenantProvider = ContainerBindings.GetServiceProvider(this.featureContext)
                                             .GetService <ITenantProvider>();

            IWorkflowInstanceStore instanceStore = await instanceStoreFactory.GetWorkflowInstanceStoreForTenantAsync(tenantProvider.Root).ConfigureAwait(false);

            WorkflowInstance instance = await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                () => instanceStore.GetWorkflowInstanceAsync(instanceId)).ConfigureAwait(false);

            Assert.AreEqual(
                expectedContextItems.Rows.Count,
                instance.Context.Count,
                "The number of context items in the workflow instance is different to the number of items in the specified list");

            foreach (TableRow current in expectedContextItems.Rows)
            {
                Assert.IsTrue(
                    instance.Context.TryGetValue(current[0], out string actualValue),
                    $"The instance context does not contain expected item with key '{current[0]}'");

                Assert.AreEqual(current[1], actualValue, $"The instance context does not contain the expected value for key '{current[0]}'");
            }
        }
        public async Task ThenANewDataCatalogItemWithIdShouldHaveBeenAddedToTheDataCatalogStore(string catalogItemId)
        {
            Container repo = ContainerBindings.GetServiceProvider(this.featureContext)
                             .GetService <DataCatalogItemRepositoryFactory>()
                             .GetRepository();

            ItemResponse <CatalogItem> item = await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                () => repo.ReadItemAsync <CatalogItem>(catalogItemId, new PartitionKey(catalogItemId))).ConfigureAwait(false);

            Assert.IsNotNull(item);
        }
        public async Task ThenTheDataCatalogItemWithIdShouldHaveNotesOf(string catalogItemId, string expectedNotes)
        {
            Container repo = ContainerBindings.GetServiceProvider(this.featureContext)
                             .GetService <DataCatalogItemRepositoryFactory>()
                             .GetRepository();

            ItemResponse <CatalogItem> item = await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                () => repo.ReadItemAsync <CatalogItem>(catalogItemId, new PartitionKey(catalogItemId))).ConfigureAwait(false);

            Assert.AreEqual(expectedNotes, item.Resource.Notes);
        }
        public async Task ThenTheWorkflowInstanceWithIdShouldHaveStatus(string instanceId, string expectedStatus)
        {
            ITenantedWorkflowInstanceStoreFactory instanceStoreFactory = ContainerBindings.GetServiceProvider(this.featureContext)
                                                                         .GetService <ITenantedWorkflowInstanceStoreFactory>();

            ITenantProvider tenantProvider = ContainerBindings.GetServiceProvider(this.featureContext)
                                             .GetService <ITenantProvider>();

            IWorkflowInstanceStore instanceStore = await instanceStoreFactory.GetWorkflowInstanceStoreForTenantAsync(tenantProvider.Root).ConfigureAwait(false);

            WorkflowInstance instance = await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                () => instanceStore.GetWorkflowInstanceAsync(instanceId)).ConfigureAwait(false);

            Assert.AreEqual(expectedStatus, instance.Status.ToString());
        }
        public async Task GivenGivenIHaveCreatedAndPersistedAWorkflowContainingAnExternalConditionWithIdAsync(string workflowId)
        {
            ExternalServiceBindings.ExternalService externalService            = ExternalServiceBindings.GetService(this.scenarioContext);
            IServiceIdentityAccessTokenSource       serviceIdentityTokenSource =
                ContainerBindings.GetServiceProvider(this.featureContext).GetRequiredService <IServiceIdentityAccessTokenSource>();

            IJsonSerializerSettingsProvider serializerSettingsProvider =
                ContainerBindings.GetServiceProvider(this.featureContext).GetRequiredService <IJsonSerializerSettingsProvider>();

            Workflow workflow = ExternalConditionWorkflowFactory.Create(
                workflowId,
                externalService.TestUrl.ToString(),
                serviceIdentityTokenSource,
                serializerSettingsProvider);

            this.condition = workflow.GetInitialState().Transitions
                             .Single()
                             .Conditions
                             .OfType <InvokeExternalServiceCondition>()
                             .Single();

            ITenantedWorkflowStoreFactory storeFactory = ContainerBindings.GetServiceProvider(this.featureContext)
                                                         .GetService <ITenantedWorkflowStoreFactory>();

            ITenantProvider tenantProvider =
                ContainerBindings.GetServiceProvider(this.featureContext).GetRequiredService <ITenantProvider>();

            IWorkflowStore store = await storeFactory.GetWorkflowStoreForTenantAsync(tenantProvider.Root).ConfigureAwait(false);

            await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(async() =>
            {
                try
                {
                    Workflow oldWorkflow = await store.GetWorkflowAsync(workflowId).ConfigureAwait(false);
                    workflow.ETag        = oldWorkflow.ETag;
                }
                catch (WorkflowNotFoundException)
                {
                    // Don't care if there is no old workflow.
                }

                await store.UpsertWorkflowAsync(workflow).ConfigureAwait(false);
            }).ConfigureAwait(false);
        }
        public async Task ThenANewDataCatalogItemWithIdShouldNotHaveBeenAddedToTheDataCatalogStore(string catalogItemId)
        {
            Container repo = ContainerBindings.GetServiceProvider(this.featureContext)
                             .GetService <DataCatalogItemRepositoryFactory>()
                             .GetRepository();

            try
            {
                await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                    () => repo.ReadItemAsync <CatalogItem>(catalogItemId, new PartitionKey(catalogItemId))).ConfigureAwait(false);

                Assert.Fail(
                    $"Did not expect a Catalog Item with Id {catalogItemId} to have been created, but one was found.");
            }
            catch (CosmosException ex)
            {
                Assert.AreEqual(
                    HttpStatusCode.NotFound,
                    ex.StatusCode,
                    $"Unexpected exception when trying to retrieve document with Id {catalogItemId}");
            }
        }
        public async Task ThenTheWorkflowInstanceWithIdShouldBeInTheStateCalled(string instanceId, string stateName)
        {
            ITenantedWorkflowStoreFactory storeFactory = ContainerBindings.GetServiceProvider(this.featureContext)
                                                         .GetService <ITenantedWorkflowStoreFactory>();
            ITenantedWorkflowInstanceStoreFactory instanceStoreFactory = ContainerBindings.GetServiceProvider(this.featureContext)
                                                                         .GetService <ITenantedWorkflowInstanceStoreFactory>();

            ITenantProvider tenantProvider = ContainerBindings.GetServiceProvider(this.featureContext)
                                             .GetService <ITenantProvider>();

            IWorkflowStore store = await storeFactory.GetWorkflowStoreForTenantAsync(tenantProvider.Root).ConfigureAwait(false);

            IWorkflowInstanceStore instanceStore = await instanceStoreFactory.GetWorkflowInstanceStoreForTenantAsync(tenantProvider.Root).ConfigureAwait(false);

            WorkflowInstance instance = await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                () => instanceStore.GetWorkflowInstanceAsync(instanceId)).ConfigureAwait(false);

            Workflow workflow = await WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                () => store.GetWorkflowAsync(instance.WorkflowId)).ConfigureAwait(false);

            WorkflowState currentState = workflow.GetState(instance.StateId);

            Assert.AreEqual(stateName, currentState.DisplayName);
        }