44
55namespace Marain . Operations . Api . Specs . Bindings
66{
7- using System ;
87 using System . Threading . Tasks ;
98
10- using Azure . Storage . Blobs ;
9+ using BoDi ;
1110
12- using Corvus . Azure . Cosmos . Tenancy ;
13- using Corvus . Azure . Storage . Tenancy ;
14- using Corvus . Storage . Azure . BlobStorage . Tenancy ;
11+ using Corvus . Storage . Azure . BlobStorage ;
1512 using Corvus . Tenancy ;
1613 using Corvus . Testing . AzureFunctions ;
1714 using Corvus . Testing . AzureFunctions . SpecFlow ;
1815 using Corvus . Testing . SpecFlow ;
1916
20- using Marain . Operations . Storage . Blob ;
21- using Marain . TenantManagement . EnrollmentConfiguration ;
17+ using Marain . Operations . Specs ;
2218 using Marain . TenantManagement . Testing ;
2319
24- using Microsoft . Extensions . Configuration ;
25- using Microsoft . Extensions . DependencyInjection ;
26- using Microsoft . Extensions . Logging ;
27-
2820 using TechTalk . SpecFlow ;
2921
3022 /// <summary>
@@ -36,85 +28,36 @@ public static class TransientTenantBindings
3628 public const string TransientServiceTenantIdFeatureContextKey = "TransientServiceTenantId" ;
3729
3830 /// <summary>
39- /// Creates a new <see cref="ITenant"/> for the current feature, adding a test <see cref="CosmosConfiguration "/>
31+ /// Creates a new <see cref="ITenant"/> for the current feature, adding a test <see cref="BlobContainerConfiguration "/>
4032 /// to the tenant data.
4133 /// </summary>
4234 /// <param name="featureContext">The current <see cref="FeatureContext"/>.</param>
35+ /// <param name="objectContainer">The SpecFlow DI container.</param>
4336 /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
4437 /// <remarks>
45- /// The newly created tenant is added to the <see cref="FeatureContext"/>. Access it via the helper methods
46- /// <see cref="GetTransientTenant(FeatureContext) "/> or <see cref="GetTransientTenantId(FeatureContext)"/> .
38+ /// The newly created client and service tenants are available to tests via DI through the
39+ /// <see cref="OperationsServiceTestTenants "/> type .
4740 /// </remarks>
4841 [ BeforeFeature ( Order = BindingSequence . TransientTenantSetup ) ]
49- public static async Task SetupTransientTenant ( FeatureContext featureContext )
42+ public static async Task SetupTransientTenant ( FeatureContext featureContext , IObjectContainer objectContainer )
5043 {
51- ITenantProvider tenantProvider = ContainerBindings . GetServiceProvider ( featureContext ) . GetRequiredService < ITenantProvider > ( ) ;
52- var transientTenantManager = TransientTenantManager . GetInstance ( featureContext ) ;
53- await transientTenantManager . EnsureInitialised ( ) . ConfigureAwait ( false ) ;
54-
55- // Create a transient service tenant for testing purposes.
56- ITenant transientServiceTenant = await transientTenantManager . CreateTransientServiceTenantFromEmbeddedResourceAsync (
57- typeof ( TransientTenantBindings ) . Assembly ,
58- "Marain.Operations.Api.Specs.ServiceManifests.OperationsServiceManifest.jsonc" ) . ConfigureAwait ( false ) ;
44+ OperationsServiceTestTenants transientTenants = await OperationsTestTenantSetup . CreateTestTenantAndEnrollInClaimsAsync ( featureContext ) ;
5945
6046 // Now update the service Id in our configuration and in the function configuration
61- UpdateServiceConfigurationWithTransientTenantId ( featureContext , transientServiceTenant ) ;
62-
63- // Now we need to construct a transient client tenant for the test, and enroll it in the new
64- // transient service.
65- ITenant transientClientTenant = await transientTenantManager . CreateTransientClientTenantAsync ( ) . ConfigureAwait ( false ) ;
47+ UpdateServiceConfigurationWithTransientTenantId ( featureContext , transientTenants . TransientServiceTenant ) ;
6648
67- await transientTenantManager . AddEnrollmentAsync (
68- transientClientTenant . Id ,
69- transientServiceTenant . Id ,
70- GetOperationsConfig ( featureContext ) ) . ConfigureAwait ( false ) ;
71-
72- // TODO: Temporary hack to work around the fact that the transient tenant manager no longer holds the latest
73- // version of the tenants it's tracking; see https://github.com/marain-dotnet/Marain.TenantManagement/issues/28
74- transientTenantManager . PrimaryTransientClient = await tenantProvider . GetTenantAsync ( transientClientTenant . Id ) . ConfigureAwait ( false ) ;
49+ objectContainer . RegisterInstanceAs ( transientTenants ) ;
7550 }
7651
7752 [ AfterFeature ]
7853 public static async Task TearDownTenants ( FeatureContext featureContext )
7954 {
80- var tenantManager = TransientTenantManager . GetInstance ( featureContext ) ;
81-
82- await featureContext . RunAndStoreExceptionsAsync ( async ( ) =>
83- {
84- IBlobContainerSourceWithTenantLegacyTransition cloudBlobContainerFactory = ContainerBindings . GetServiceProvider ( featureContext ) . GetRequiredService < IBlobContainerSourceWithTenantLegacyTransition > ( ) ;
85- BlobContainerClient testContainer = await cloudBlobContainerFactory . GetBlobContainerClientFromTenantAsync (
86- tenantManager . PrimaryTransientClient ,
87- OperationsRepository . OperationsV2ConfigKey ,
88- OperationsRepository . OperationsV3ConfigKey ) . ConfigureAwait ( false ) ;
89- await testContainer . DeleteIfExistsAsync ( ) . ConfigureAwait ( false ) ;
90- } ) . ConfigureAwait ( false ) ;
55+ await OperationsTestStorageSetup . TearDownBlobContainersAsync ( featureContext ) ;
9156
57+ var tenantManager = TransientTenantManager . GetInstance ( featureContext ) ;
9258 await featureContext . RunAndStoreExceptionsAsync ( ( ) => tenantManager . CleanupAsync ( ) ) . ConfigureAwait ( false ) ;
9359 }
9460
95- /// <summary>
96- /// Retrieves the transient tenant created for the current feature from the supplied <see cref="FeatureContext"/>,
97- /// or null if there is none.
98- /// </summary>
99- /// <param name="context">The current <see cref="FeatureContext"/>.</param>
100- /// <returns>The <see cref="ITenant"/>.</returns>
101- public static ITenant GetTransientTenant ( this FeatureContext context )
102- {
103- return TransientTenantManager . GetInstance ( context ) . PrimaryTransientClient ;
104- }
105-
106- /// <summary>
107- /// Retrieves the Id of the transient tenant created for the current feature from the supplied feature context.
108- /// <see cref="FeatureContext"/>.
109- /// </summary>
110- /// <param name="context">The current <see cref="FeatureContext"/>.</param>
111- /// <returns>The Id of the <see cref="ITenant"/>.</returns>
112- /// <exception cref="ArgumentNullException">There is no current tenant.</exception>
113- public static string GetTransientTenantId ( this FeatureContext context )
114- {
115- return context . GetTransientTenant ( ) . Id ;
116- }
117-
11861 private static void UpdateServiceConfigurationWithTransientTenantId (
11962 FeatureContext featureContext ,
12063 ITenant transientServiceTenant )
@@ -129,36 +72,5 @@ private static void UpdateServiceConfigurationWithTransientTenantId(
12972 "MarainServiceConfiguration:ServiceDisplayName" ,
13073 transientServiceTenant . Name ) ;
13174 }
132-
133- private static EnrollmentConfigurationItem [ ] GetOperationsConfig ( FeatureContext featureContext )
134- {
135- IConfiguration configuration = ContainerBindings
136- . GetServiceProvider ( featureContext )
137- . GetRequiredService < IConfiguration > ( ) ;
138-
139- // Can't create a logger using the generic type of this class because it's static, so we'll do it using
140- // the feature context instead.
141- ILogger < FeatureContext > logger = ContainerBindings
142- . GetServiceProvider ( featureContext )
143- . GetRequiredService < ILogger < FeatureContext > > ( ) ;
144-
145- BlobStorageConfiguration blobStorageConfiguration =
146- configuration . GetSection ( "TestBlobStorageConfiguration" ) . Get < BlobStorageConfiguration > ( )
147- ?? new BlobStorageConfiguration ( ) ;
148-
149- if ( string . IsNullOrEmpty ( blobStorageConfiguration . AccountName ) )
150- {
151- logger . LogDebug ( "No configuration value 'TestBlobStorageConfiguration:AccountName' provided; using local storage emulator." ) ;
152- }
153-
154- return new EnrollmentConfigurationItem [ ]
155- {
156- new EnrollmentBlobStorageConfigurationItem
157- {
158- Key = "operationsStore" ,
159- Configuration = blobStorageConfiguration ,
160- } ,
161- } ;
162- }
16375 }
16476}
0 commit comments