diff --git a/src/MigrationTools.Clients.TfsObjectModel.Tests/Endpoints/TfsWorkItemEndPointTests.cs b/src/MigrationTools.Clients.TfsObjectModel.Tests/Endpoints/TfsWorkItemEndPointTests.cs index e73b78b76..fdb7240e0 100644 --- a/src/MigrationTools.Clients.TfsObjectModel.Tests/Endpoints/TfsWorkItemEndPointTests.cs +++ b/src/MigrationTools.Clients.TfsObjectModel.Tests/Endpoints/TfsWorkItemEndPointTests.cs @@ -15,6 +15,9 @@ using MigrationTools.Tools.Interfaces; using MigrationTools.Tools.Shadows; using MigrationTools.Shadows; +using Microsoft.Extensions.Configuration; +using System.IO; +using System.Text; namespace MigrationTools.Endpoints.Tests { @@ -97,5 +100,84 @@ protected TfsWorkItemEndpoint GetTfsWorkItemEndPoint(string key = "Source", TfsW return (TfsWorkItemEndpoint)services.BuildServiceProvider().GetRequiredKeyedService(key); } + + [TestMethod(), TestCategory("L1")] + public void TfsWorkItemEndPoint_EnvironmentOverrideTest() + { + Environment.SetEnvironmentVariable("MigrationTools__Endpoints__Source__Authentication__AccessToken", "654321"); + IConfigurationBuilder configBuilder = GetSourceTargetBasicConfig(); + var configuration = configBuilder.AddEnvironmentVariables().Build(); + // Create services + IServiceCollection serviceCollection = new ServiceCollection(); + serviceCollection.AddSingleton(); + serviceCollection.AddMigrationToolServicesForUnitTests(); + serviceCollection.AddConfiguredEndpoints(configuration); + // Create a service provider from the service collection + var serviceProvider = serviceCollection.BuildServiceProvider(); + var endpoint = serviceProvider.GetKeyedService("Source"); + Assert.IsNotNull(endpoint, "Endpoint not found."); + Endpoint endpoint1 = endpoint as Endpoint; + // Validate that the correct number of endpoints are registered + Assert.AreEqual("654321", endpoint1.Options.Authentication.AccessToken, "Token not passed."); + + } + + private static IConfigurationBuilder GetSourceTargetBasicConfig() + { + // Create Config + var json = @" + { + ""MigrationTools"": { + ""Version"": ""16.0"", + ""Endpoints"": { + ""Source"": { + ""EndpointType"": ""TfsTeamProjectEndpoint"", + ""Collection"": ""https://dev.azure.com/nkdagility-preview/"", + ""Project"": ""migrationSource1"", + ""AllowCrossProjectLinking"": false, + ""ReflectedWorkItemIDFieldName"": ""Custom.ReflectedWorkItemId"", + ""Authentication"": { + ""AuthenticationMode"": ""AccessToken"", + ""AccessToken"": ""123456"", + ""NetworkCredentials"": { + ""UserName"": """", + ""Password"": """", + ""Domain"": """" + } + }, + ""LanguageMaps"": { + ""AreaPath"": ""Area"", + ""IterationPath"": ""Iteration"" + } + }, + ""Target"": { + ""EndpointType"": ""TfsTeamProjectEndpoint"", + ""Collection"": ""https://dev.azure.com/nkdagility-preview/"", + ""Project"": ""migrationTest5"", + ""TfsVersion"": ""AzureDevOps"", + ""Authentication"": { + ""AuthenticationMode"": ""AccessToken"", + ""AccessToken"": ""none"", + ""NetworkCredentials"": { + ""UserName"": """", + ""Password"": """", + ""Domain"": """" + } + }, + ""ReflectedWorkItemIDFieldName"": ""nkdScrum.ReflectedWorkItemId"", + ""AllowCrossProjectLinking"": false, + ""LanguageMaps"": { + ""AreaPath"": ""Area"", + ""IterationPath"": ""Iteration"" + } + } + }, + } + }"; + var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)); + var configBuilder = new ConfigurationBuilder().AddJsonStream(new MemoryStream(Encoding.UTF8.GetBytes(json))); + return configBuilder; + } + } } \ No newline at end of file diff --git a/src/MigrationTools.Clients.TfsObjectModel/Clients/TfsWorkItemMigrationClient.cs b/src/MigrationTools.Clients.TfsObjectModel/Clients/TfsWorkItemMigrationClient.cs index 1a5f91a71..95be6bc0e 100644 --- a/src/MigrationTools.Clients.TfsObjectModel/Clients/TfsWorkItemMigrationClient.cs +++ b/src/MigrationTools.Clients.TfsObjectModel/Clients/TfsWorkItemMigrationClient.cs @@ -31,7 +31,18 @@ public TfsWorkItemMigrationClient(IOptions option { _telemetry = telemetry; _workItemQueryBuilderFactory = workItemQueryBuilderFactory; - InnerConfigure(migrationClient); + _bypassRules = WorkItemStoreFlags.BypassRules; + + Lazy _wistore = new Lazy(() => + { + Console.WriteLine("Initializing expensive WorkItemStore..."); + return GetWorkItemStore(); + }); + Lazy _project = new Lazy(() => + { + Console.WriteLine("Initializing expensive ProjectData from WorkItemStore..."); + return GetProject(); + }); } new TfsTeamProjectEndpointOptions Options => (TfsTeamProjectEndpointOptions)base.Options; @@ -243,12 +254,7 @@ private Endpoints.IWorkItemQuery GetWorkItemQuery(string WIQLQuery) } } - protected void InnerConfigure(IMigrationClient migrationClient, bool bypassRules = true) - { - _bypassRules = bypassRules ? WorkItemStoreFlags.BypassRules : WorkItemStoreFlags.None; - _wistore = GetWorkItemStore(); - _project = GetProject(); - } + public override WorkItemData PersistWorkItem(WorkItemData workItem) { diff --git a/src/MigrationTools.Shadows/Clients/WorkItemQueryBuilderFactoryFake.cs b/src/MigrationTools.Shadows/Clients/WorkItemQueryBuilderFactoryFake.cs new file mode 100644 index 000000000..2b3a04fdc --- /dev/null +++ b/src/MigrationTools.Shadows/Clients/WorkItemQueryBuilderFactoryFake.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using MigrationTools.Clients; + +namespace MigrationTools.Clients.Shadows +{ + public class WorkItemQueryBuilderFactoryFake : IWorkItemQueryBuilderFactory + { + public IWorkItemQueryBuilder Create() + { + throw new NotImplementedException(); + } + } +} diff --git a/src/MigrationTools.Shadows/ServiceCollectionExtensions.cs b/src/MigrationTools.Shadows/ServiceCollectionExtensions.cs index e8d377aa3..fbdf13c7b 100644 --- a/src/MigrationTools.Shadows/ServiceCollectionExtensions.cs +++ b/src/MigrationTools.Shadows/ServiceCollectionExtensions.cs @@ -1,4 +1,6 @@ using Microsoft.Extensions.DependencyInjection; +using MigrationTools.Clients; +using MigrationTools.Clients.Shadows; using MigrationTools.Services; using MigrationTools.Services.Shadows; using Serilog; @@ -29,6 +31,7 @@ public static void AddMigrationToolServicesForUnitTests(this IServiceCollection // Add Telemitery Adapter context.AddSingleton(); context.AddSingleton(); + context.AddSingleton(); } } }