Skip to content

Commit a7acbaa

Browse files
authored
Merge pull request #1 from delegateas/add_managed_identity
Add IManagedIdentityService to DI setup
2 parents 5b491b2 + 09ee881 commit a7acbaa

File tree

7 files changed

+227
-1
lines changed

7 files changed

+227
-1
lines changed

XrmPluginCore.Tests/Helpers/MockServiceProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ private void SetupMocks()
5252
serviceCollection.AddScoped(_ => TracingService);
5353
serviceCollection.AddScoped(_ => PluginTelemetryLogger);
5454
serviceCollection.AddScoped(_ => OrganizationServiceFactory);
55+
serviceCollection.AddScoped(_ => Substitute.For<IManagedIdentityService>());
5556
ServiceProvider = serviceCollection.BuildServiceProvider();
5657
}
5758

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Xrm.Sdk;
3+
using Microsoft.Xrm.Sdk.PluginTelemetry;
4+
using NSubstitute;
5+
using System;
6+
using XrmPluginCore.Enums;
7+
8+
namespace XrmPluginCore.Tests.Helpers
9+
{
10+
public class MockServiceProviderNoManagedIdentity
11+
{
12+
public IServiceProvider ServiceProvider { get; private set; }
13+
public IPluginExecutionContext PluginExecutionContext { get; private set; }
14+
public ITracingService TracingService { get; private set; }
15+
public IOrganizationServiceFactory OrganizationServiceFactory { get; private set; }
16+
public IOrganizationService OrganizationService { get; private set; }
17+
public IOrganizationService OrganizationAdminService { get; private set; }
18+
public ILogger PluginTelemetryLogger { get; private set; }
19+
20+
public MockServiceProviderNoManagedIdentity()
21+
{
22+
SetupMocks();
23+
}
24+
25+
private void SetupMocks()
26+
{
27+
PluginExecutionContext = Substitute.For<IPluginExecutionContext>();
28+
TracingService = Substitute.For<ITracingService>();
29+
OrganizationServiceFactory = Substitute.For<IOrganizationServiceFactory>();
30+
OrganizationService = Substitute.For<IOrganizationService>();
31+
OrganizationAdminService = Substitute.For<IOrganizationService>();
32+
PluginTelemetryLogger = Substitute.For<ILogger>();
33+
34+
// Setup default values
35+
PluginExecutionContext.UserId.Returns(Guid.NewGuid());
36+
PluginExecutionContext.CorrelationId.Returns(Guid.NewGuid());
37+
PluginExecutionContext.InitiatingUserId.Returns(Guid.NewGuid());
38+
PluginExecutionContext.MessageName.Returns("Create");
39+
PluginExecutionContext.PrimaryEntityName.Returns("account");
40+
PluginExecutionContext.Stage.Returns((int)ExecutionStage.PreOperation); // Pre-operation
41+
42+
// Setup organization service factory
43+
OrganizationServiceFactory.CreateOrganizationService(Arg.Any<Guid?>()).Returns(callInfo =>
44+
{
45+
var userId = callInfo.Arg<Guid?>();
46+
return userId.HasValue ? OrganizationService : OrganizationAdminService;
47+
});
48+
49+
// Setup service provider to return mocked services
50+
var serviceCollection = new ServiceCollection();
51+
serviceCollection.AddScoped(_ => PluginExecutionContext);
52+
serviceCollection.AddScoped(_ => TracingService);
53+
serviceCollection.AddScoped(_ => PluginTelemetryLogger);
54+
serviceCollection.AddScoped(_ => OrganizationServiceFactory);
55+
ServiceProvider = serviceCollection.BuildServiceProvider();
56+
}
57+
58+
public void SetupInputParameters(ParameterCollection inputParameters)
59+
{
60+
PluginExecutionContext.InputParameters.Returns(inputParameters);
61+
}
62+
63+
public void SetupOutputParameters(ParameterCollection outputParameters)
64+
{
65+
PluginExecutionContext.OutputParameters.Returns(outputParameters);
66+
}
67+
68+
public void SetupPreEntityImages(EntityImageCollection preImages)
69+
{
70+
PluginExecutionContext.PreEntityImages.Returns(preImages);
71+
}
72+
73+
public void SetupPostEntityImages(EntityImageCollection postImages)
74+
{
75+
PluginExecutionContext.PostEntityImages.Returns(postImages);
76+
}
77+
78+
public void SetupPrimaryEntityName(string entityName)
79+
{
80+
PluginExecutionContext.PrimaryEntityName.Returns(entityName);
81+
}
82+
83+
public void SetupMessageName(string messageName)
84+
{
85+
PluginExecutionContext.MessageName.Returns(messageName);
86+
}
87+
88+
public void SetupStage(int stage)
89+
{
90+
PluginExecutionContext.Stage.Returns(stage);
91+
}
92+
93+
public void SetupUserId(Guid userId)
94+
{
95+
PluginExecutionContext.UserId.Returns(userId);
96+
}
97+
}
98+
}

XrmPluginCore.Tests/PluginTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,75 @@ public void Execute_MatchingRegistration_ShouldExecuteAction_ServiceProvider_DI(
9090
sampleService.PluginContext.Should().Be(mockProvider.PluginExecutionContext);
9191
sampleService.OrganizationService.Should().Be(mockProvider.OrganizationService);
9292
}
93+
94+
[Fact]
95+
public void Execute_MatchingRegistration_ShouldExecuteAction_ServiceProvider_DI_NoManagedIdentity()
96+
{
97+
// Arrange
98+
var plugin = new SamplePluginNoManagedIdentity();
99+
var mockProvider = new MockServiceProviderNoManagedIdentity();
100+
101+
// Setup context for account create
102+
mockProvider.SetupPrimaryEntityName("account");
103+
mockProvider.SetupMessageName("Create");
104+
mockProvider.SetupStage((int)ExecutionStage.PreOperation);
105+
106+
// Act
107+
plugin.Execute(mockProvider.ServiceProvider);
108+
109+
// Assert
110+
var sampleService = plugin.SampleService as SampleService;
111+
sampleService.Should().NotBeNull();
112+
sampleService.HandleCreateCalled.Should().BeTrue();
113+
sampleService.PluginContext.Should().Be(mockProvider.PluginExecutionContext);
114+
sampleService.OrganizationService.Should().Be(mockProvider.OrganizationService);
115+
}
116+
117+
[Fact]
118+
public void Execute_MatchingRegistration_ShouldExecuteAction_ServiceProvider_DI_NoManagedIdentity_ProviderHas()
119+
{
120+
// Arrange
121+
var plugin = new SamplePluginNoManagedIdentity();
122+
var mockProvider = new MockServiceProvider();
123+
124+
// Setup context for account create
125+
mockProvider.SetupPrimaryEntityName("account");
126+
mockProvider.SetupMessageName("Create");
127+
mockProvider.SetupStage((int)ExecutionStage.PreOperation);
128+
129+
// Act
130+
plugin.Execute(mockProvider.ServiceProvider);
131+
132+
// Assert
133+
var sampleService = plugin.SampleService as SampleService;
134+
sampleService.Should().NotBeNull();
135+
sampleService.HandleCreateCalled.Should().BeTrue();
136+
sampleService.PluginContext.Should().Be(mockProvider.PluginExecutionContext);
137+
sampleService.OrganizationService.Should().Be(mockProvider.OrganizationService);
138+
}
139+
140+
[Fact]
141+
public void Execute_MatchingRegistration_ShouldExecuteAction_ServiceProvider_DI_NoManagedIdentityFromProvider()
142+
{
143+
// Arrange
144+
var plugin = new SamplePlugin();
145+
var mockProvider = new MockServiceProviderNoManagedIdentity();
146+
147+
// Setup context for account create
148+
mockProvider.SetupPrimaryEntityName("account");
149+
mockProvider.SetupMessageName("Create");
150+
mockProvider.SetupStage((int)ExecutionStage.PreOperation);
151+
152+
// Act
153+
plugin.Execute(mockProvider.ServiceProvider);
154+
155+
// Assert
156+
var sampleService = plugin.SampleService as SampleService;
157+
sampleService.Should().NotBeNull();
158+
sampleService.HandleCreateCalled.Should().BeTrue();
159+
sampleService.PluginContext.Should().Be(mockProvider.PluginExecutionContext);
160+
sampleService.OrganizationService.Should().Be(mockProvider.OrganizationService);
161+
}
93162

94163
[Fact]
95164
public void Execute_NonMatchingEntity_ShouldNotExecuteAction()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using XrmPluginCore.Enums;
2+
using XrmPluginCore.Tests;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using XrmPluginCore;
5+
6+
namespace XrmPluginCore.Tests.TestPlugins.Bedrock
7+
{
8+
public abstract class PluginBaseNoMangedIdentity : Plugin
9+
{
10+
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
11+
{
12+
return services.AddScoped<ISampleService, SampleServiceNoManagedIdentity>();
13+
}
14+
}
15+
16+
public class SamplePluginNoManagedIdentity : PluginBase
17+
{
18+
internal ISampleService SampleService { get; private set; }
19+
20+
public SamplePluginNoManagedIdentity()
21+
{
22+
RegisterStep<TestAccount, ISampleService>(
23+
EventOperation.Create,
24+
ExecutionStage.PreOperation,
25+
s =>
26+
{
27+
// We only do this for testing purposes
28+
SampleService = s;
29+
30+
// Call the service
31+
s.HandleCreate();
32+
});
33+
}
34+
}
35+
}

XrmPluginCore.Tests/TestPlugins/Bedrock/SampleService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SampleService : ISampleService
1313
public IPluginExecutionContext PluginContext { get; }
1414
public IOrganizationService OrganizationService { get; }
1515

16-
public SampleService(IPluginExecutionContext pluginContext, IOrganizationServiceFactory organizationServiceFactory)
16+
public SampleService(IPluginExecutionContext pluginContext, IOrganizationServiceFactory organizationServiceFactory, IManagedIdentityService managedIdentityService)
1717
{
1818
PluginContext = pluginContext;
1919
OrganizationService = organizationServiceFactory.CreateOrganizationService(pluginContext.UserId);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.Xrm.Sdk;
2+
3+
namespace XrmPluginCore.Tests.TestPlugins.Bedrock
4+
{
5+
public class SampleServiceNoManagedIdentity : ISampleService
6+
{
7+
public bool HandleCreateCalled { get; private set; }
8+
public IPluginExecutionContext PluginContext { get; }
9+
public IOrganizationService OrganizationService { get; }
10+
11+
public SampleServiceNoManagedIdentity(IPluginExecutionContext pluginContext, IOrganizationServiceFactory organizationServiceFactory)
12+
{
13+
PluginContext = pluginContext;
14+
OrganizationService = organizationServiceFactory.CreateOrganizationService(pluginContext.UserId);
15+
}
16+
17+
public void HandleCreate()
18+
{
19+
HandleCreateCalled = true;
20+
}
21+
}
22+
}

XrmPluginCore/Extensions/ServiceProviderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static ExtendedServiceProvider BuildServiceProvider(this IServiceProvider
2727
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext7>());
2828

2929
services.AddScoped(_ => serviceProvider.GetService<IOrganizationServiceFactory>());
30+
services.AddScoped(_ => serviceProvider.GetService<IManagedIdentityService>());
3031
services.AddScoped(_ => telemetryService);
3132
services.AddScoped<ITracingService>(_ => extendedTracingService);
3233
services.AddScoped<IExtendedTracingService>(_ => extendedTracingService);

0 commit comments

Comments
 (0)