Skip to content

Commit

Permalink
Update with new tst for endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHinsh committed Sep 3, 2024
1 parent 8728530 commit 11eae5f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -97,5 +100,84 @@ protected TfsWorkItemEndpoint GetTfsWorkItemEndPoint(string key = "Source", TfsW
return (TfsWorkItemEndpoint)services.BuildServiceProvider().GetRequiredKeyedService<IEndpoint>(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<EndpointEnricherContainer>();
serviceCollection.AddMigrationToolServicesForUnitTests();
serviceCollection.AddConfiguredEndpoints(configuration);
// Create a service provider from the service collection
var serviceProvider = serviceCollection.BuildServiceProvider();
var endpoint = serviceProvider.GetKeyedService<IEndpoint>("Source");
Assert.IsNotNull(endpoint, "Endpoint not found.");
Endpoint<TfsTeamProjectEndpointOptions> endpoint1 = endpoint as Endpoint<TfsTeamProjectEndpointOptions>;
// 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;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,18 @@ public TfsWorkItemMigrationClient(IOptions<TfsTeamProjectEndpointOptions> option
{
_telemetry = telemetry;
_workItemQueryBuilderFactory = workItemQueryBuilderFactory;
InnerConfigure(migrationClient);
_bypassRules = WorkItemStoreFlags.BypassRules;

Lazy<WorkItemStore> _wistore = new Lazy<WorkItemStore>(() =>
{
Console.WriteLine("Initializing expensive WorkItemStore...");
return GetWorkItemStore();
});
Lazy<ProjectData> _project = new Lazy<ProjectData>(() =>
{
Console.WriteLine("Initializing expensive ProjectData from WorkItemStore...");
return GetProject();
});
}

new TfsTeamProjectEndpointOptions Options => (TfsTeamProjectEndpointOptions)base.Options;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
3 changes: 3 additions & 0 deletions src/MigrationTools.Shadows/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using MigrationTools.Clients;
using MigrationTools.Clients.Shadows;
using MigrationTools.Services;
using MigrationTools.Services.Shadows;
using Serilog;
Expand Down Expand Up @@ -29,6 +31,7 @@ public static void AddMigrationToolServicesForUnitTests(this IServiceCollection
// Add Telemitery Adapter
context.AddSingleton<ITelemetryLogger, TelemetryLoggerFake>();
context.AddSingleton<IMigrationToolVersion, FakeMigrationToolVersion>();
context.AddSingleton<IWorkItemQueryBuilderFactory, WorkItemQueryBuilderFactoryFake>();
}
}
}

0 comments on commit 11eae5f

Please sign in to comment.