Skip to content

Commit

Permalink
Merge pull request #30 from The-Standard-Organization/users/lboullosa…
Browse files Browse the repository at this point in the history
…/foundations-dependency-injection-service

FOUNDATIONS: IServiceProvider part in InjectionDependencyService
  • Loading branch information
glhays authored Oct 9, 2024
2 parents df9a318 + 35c34a3 commit 9669258
Show file tree
Hide file tree
Showing 23 changed files with 993 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SPAL.Core should provide the following features:

![Current Engineering Overview](https://github.com/The-Standard-Organization/STX.SPAL.Core/blob/main/STX.SPAL.Core/Resources/Diagrams/spal-diagram-ovtab-05302024.png)

![Current Engineering Status](https://github.com/The-Standard-Organization/STX.SPAL.Core/blob/main/STX.SPAL.Core/Resources/Diagrams/spal-diagram-coretab-06072024.png)
![Current Engineering Status](https://github.com/The-Standard-Organization/STX.SPAL.Core/blob/main/STX.SPAL.Core/Resources/Diagrams/spal-diagram-coretab-05102024.png)

## Standard-Compliance
This library was built according to The Standard. The library follows engineering principles, patterns and tooling as recommended by The Standard.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private void ShouldThrowDependencyExceptionOnLoadAssemblyIfExternalExceptionOccu
Assert.Throws<AssemblyDependencyException>(
getAssemblyFunction);

//then
// then
actualAssemblyDependencyException.Should().BeEquivalentTo(
expectedAssemblyDependencyException);

Expand Down Expand Up @@ -90,7 +90,7 @@ private void ShouldThrowValidationDependencyExceptionOnLoadAssemblyIfExternalExc
Assert.Throws<AssemblyValidationDependencyException>(
getAssemblyFunction);

//then
// then
actualAssemblyValidationDependencyException.Should().BeEquivalentTo(
expectedAssemblyValidationDependencyException);

Expand Down Expand Up @@ -135,7 +135,7 @@ private void ShouldThrowServiceExceptionOnLoadAssemblyIfExceptionOccurs(
Assert.Throws<AssemblyServiceException>(
getAssemblyFunction);

//then
// then
actualAssemblyServiceException.Should().BeEquivalentTo(
expectedAssemblyServiceException);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System.Threading.Tasks;
using FluentAssertions;
using Moq;

Expand Down Expand Up @@ -31,7 +30,7 @@ private void ShouldGetApplicationPathAssemblies()
string[] actualApplicationPathsAssemblies =
this.assemblyService.GetApplicationPathsAssemblies();

//then
// then
actualApplicationPathsAssemblies.Should()
.BeEquivalentTo(expectedApplicationPathsAssemblies);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private void ShouldGetAssembly()
Assembly actualAssembly =
this.assemblyService.GetAssembly(inputPathAssembly);

//then
// then
actualAssembly.Should().BeSameAs(expectedAssembly);

this.assemblyBroker.Verify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void ShouldThrowValidationExceptionIfInvalidAssemblyPath(
Assert.Throws<AssemblyValidationException>(
getAssemblyFunction);

//then
// then
actualAssemblyValidationException.Should().BeEquivalentTo(
expectedAssemblyValidationException);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private void ShouldThrowValidationDependencyExceptionOnRegisterServiceDescriptor

var expectedServiceCollectionValidationDependencyException =
new DependencyInjectionValidationDependencyException(
message: "Service collection validation dependency error occurred, contact support.",
message: "Dependency Injection validation dependency error occurred, contact support.",
innerException: addServiceDescriptorException);

this.dependencyInjectionBroker
Expand All @@ -57,7 +57,7 @@ private void ShouldThrowValidationDependencyExceptionOnRegisterServiceDescriptor
Assert.Throws<DependencyInjectionValidationDependencyException>(
registerServiceDescriptorFunction);

//then
// then
actualServiceCollectionValidationDependencyException.Should().BeEquivalentTo(
expectedServiceCollectionValidationDependencyException);

Expand Down Expand Up @@ -88,7 +88,7 @@ private void ShouldThrowServiceExceptionOnRegisterServiceDescriptorIfExceptionOc

var expectedServiceCollectionServiceException =
new DependencyInjectionServiceException(
message: "ServiceCollection service error occurred, contact support.",
message: "Dependency Injection service error occurred, contact support.",
innerException: assemblyLoadException);

this.dependencyInjectionBroker
Expand All @@ -115,7 +115,7 @@ private void ShouldThrowServiceExceptionOnRegisterServiceDescriptorIfExceptionOc
Assert.Throws<DependencyInjectionServiceException>(
registerServiceDescriptorFunction);

//then
// then
actualServiceCollectionServiceException.Should().BeEquivalentTo(
expectedServiceCollectionServiceException);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using STX.SPAL.Core.Models.Services.Foundations.DependenciesInjections;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.DependenciesInjections
{
public partial class DependencyInjectionServiceTests
{
[Fact]
private void ShouldBuildServiceProvider()
{
// given
dynamic randomProperties = CreateRandomProperties();
dynamic inputProperties = randomProperties;

ServiceDescriptor randomServiceDescriptor = randomProperties.ServiceDescriptor;
ServiceDescriptor inputServiceDescriptor = randomServiceDescriptor;
ServiceDescriptor expectedServiceDescriptor = inputServiceDescriptor;

DependencyInjection inputDependencyInjection = inputProperties.DependencyInjection;
inputDependencyInjection.ServiceCollection.Add(inputServiceDescriptor);

DependencyInjection expectedDependencyInjection =
new DependencyInjection
{
ServiceCollection = inputDependencyInjection.ServiceCollection,
ServiceProvider = inputDependencyInjection.ServiceCollection.BuildServiceProvider()
};

DependencyInjection returnedDependencyInjection = expectedDependencyInjection;

this.dependencyInjectionBroker
.Setup(broker =>
broker.BuildServiceProvider(
It.Is<IServiceCollection>(actualServiceCollection =>
SameServiceCollectionAs(
actualServiceCollection,
expectedDependencyInjection.ServiceCollection)
.Compile()
.Invoke(inputDependencyInjection.ServiceCollection))))
.Returns(returnedDependencyInjection.ServiceProvider);

// when
DependencyInjection actualDependencyInjection =
this.dependencyInjectionService.BuildServiceProvider(
inputProperties.DependencyInjection);

// then
actualDependencyInjection.Should().BeEquivalentTo(expectedDependencyInjection);

this.dependencyInjectionBroker.Verify(
broker =>
broker.BuildServiceProvider(
It.Is<IServiceCollection>(actualServiceCollection =>
SameServiceCollectionAs(
actualServiceCollection,
expectedDependencyInjection.ServiceCollection)
.Compile()
.Invoke(actualDependencyInjection.ServiceCollection))),
Times.Once);

this.dependencyInjectionBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// ----------------------------------------------------------------------------------
// Copyright (c) The Standard Organization: A coalition of the Good-Hearted Engineers
// ----------------------------------------------------------------------------------

using System;
using FluentAssertions;
using Force.DeepCloner;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using STX.SPAL.Abstractions;
using STX.SPAL.Core.Models.Services.Foundations.DependenciesInjections;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.DependenciesInjections
{
public partial class DependencyInjectionServiceTests
{
[Fact]
private void ShouldGetService()
{
// given
dynamic randomProperties = CreateRandomProperties();
dynamic inputProperties = randomProperties;

ServiceDescriptor randomServiceDescriptor = randomProperties.ServiceDescriptor;
ServiceDescriptor inputServiceDescriptor = randomServiceDescriptor;
ServiceDescriptor expectedServiceDescriptor = inputServiceDescriptor;

IServiceCollection inputServiceCollection = inputProperties.DependencyInjection.ServiceCollection;
inputServiceCollection.Add(inputServiceDescriptor);

DependencyInjection inputDependencyInjection =
new DependencyInjection
{
ServiceCollection = inputServiceCollection,
ServiceProvider = inputServiceCollection.BuildServiceProvider()
};

Type implementationType = randomProperties.ImplementationType;

ISPALBase returnedService =
Activator.CreateInstance(implementationType) as ISPALBase;

ISPALBase expectedService = returnedService;

DependencyInjection expectedDependencyInjection = inputDependencyInjection.DeepClone();
DependencyInjection returnedDependencyInjection = expectedDependencyInjection;

this.dependencyInjectionBroker
.Setup(broker =>
broker.GetService<ISPALBase>(
It.Is<IServiceProvider>(actualServiceProvider =>
SameServiceProviderAs(
actualServiceProvider,
expectedDependencyInjection.ServiceProvider)
.Compile()
.Invoke(inputDependencyInjection.ServiceProvider))))
.Returns(returnedService);

// when
ISPALBase actualService =
this.dependencyInjectionService.GetService<ISPALBase>(
inputDependencyInjection);

//then
actualService.Should().BeEquivalentTo(expectedService);

this.dependencyInjectionBroker.Verify(
broker =>
broker.GetService<ISPALBase>(
It.Is<IServiceProvider>(actualServiceProvider =>
SameServiceProviderAs(
actualServiceProvider,
expectedDependencyInjection.ServiceProvider)
.Compile()
.Invoke(inputDependencyInjection.ServiceProvider))),
Times.Once);

this.dependencyInjectionBroker.VerifyNoOtherCalls();
}

[Fact]
private void ShouldGetServiceWithSpalId()
{
// given
dynamic randomProperties = CreateRandomProperties();
dynamic inputProperties = randomProperties;

ServiceDescriptor randomServiceDescriptor = randomProperties.ServiceDescriptorWithSpalId;
ServiceDescriptor inputServiceDescriptor = randomServiceDescriptor;
ServiceDescriptor expectedServiceDescriptor = inputServiceDescriptor;


IServiceCollection inputServiceCollection = inputProperties.DependencyInjection.ServiceCollection;
inputServiceCollection.Add(inputServiceDescriptor);

DependencyInjection inputDependencyInjection =
new DependencyInjection
{
ServiceCollection = inputServiceCollection,
ServiceProvider = inputServiceCollection.BuildServiceProvider()
};

Type implementationType = randomProperties.ImplementationType;

ISPALBase returnedService =
Activator.CreateInstance(implementationType) as ISPALBase;

ISPALBase expectedService = returnedService;

DependencyInjection expectedDependencyInjection = inputDependencyInjection.DeepClone();
DependencyInjection returnedDependencyInjection = expectedDependencyInjection;

this.dependencyInjectionBroker
.Setup(broker =>
broker.GetService<ISPALBase>(
It.Is<IServiceProvider>(actualServiceProvider =>
SameServiceProviderAs(
actualServiceProvider,
expectedDependencyInjection.ServiceProvider)
.Compile()
.Invoke(inputDependencyInjection.ServiceProvider)),
It.IsAny<string>()))
.Returns(returnedService);

// when
ISPALBase actualService =
this.dependencyInjectionService.GetService<ISPALBase>(
inputDependencyInjection,
inputProperties.SpalId);

// then
actualService.Should().BeEquivalentTo(expectedService);

this.dependencyInjectionBroker.Verify(
broker =>
broker.GetService<ISPALBase>(
It.Is<IServiceProvider>(actualServiceProvider =>
SameServiceProviderAs(
actualServiceProvider,
expectedDependencyInjection.ServiceProvider)
.Compile()
.Invoke(inputDependencyInjection.ServiceProvider)),
It.IsAny<string>()),
Times.Once);

this.dependencyInjectionBroker.VerifyNoOtherCalls();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void ShouldRegisterServiceDescriptor()
inputProperties.ImplementationType,
inputProperties.ServiceLifeTime);

//then
// then
actualDependencyInjection.Should().BeEquivalentTo(expectedDependencyInjection);

this.dependencyInjectionBroker.Verify(
Expand Down Expand Up @@ -107,7 +107,7 @@ private void ShouldRegisterServiceDescriptorWithSpalId()
inputProperties.ImplementationType,
inputProperties.ServiceLifeTime);

//then
// then
actualDependencyInjection.Should().BeEquivalentTo(expectedDependencyInjection);

this.dependencyInjectionBroker.Verify(
Expand Down
Loading

0 comments on commit 9669258

Please sign in to comment.