Skip to content

Commit

Permalink
Api dependency validator was added
Browse files Browse the repository at this point in the history
  • Loading branch information
litichevskiydv committed Sep 11, 2024
1 parent 9c79c45 commit c4c231d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Byndyusoft.ArchitectureTesting.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api", "examples\Api\Api.csp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api.Tests", "examples\Api.Tests\Api.Tests.csproj", "{DB9964A3-5C44-453D-8066-BEA6A9920209}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Validation.StorageDependencyValidators", "src\Validation.StorageDependencyValidators\Validation.StorageDependencyValidators.csproj", "{368D567C-90A9-40A8-B5A1-4F624754A062}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Validation.StorageDependencyValidators", "src\Validation.StorageDependencyValidators\Validation.StorageDependencyValidators.csproj", "{368D567C-90A9-40A8-B5A1-4F624754A062}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Validation.ApiDependencyValidators", "src\Validation.ApiDependencyValidators\Validation.ApiDependencyValidators.csproj", "{7A7D8C81-CE36-4E92-8B08-D19DF903B23F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -81,6 +83,10 @@ Global
{368D567C-90A9-40A8-B5A1-4F624754A062}.Debug|Any CPU.Build.0 = Debug|Any CPU
{368D567C-90A9-40A8-B5A1-4F624754A062}.Release|Any CPU.ActiveCfg = Release|Any CPU
{368D567C-90A9-40A8-B5A1-4F624754A062}.Release|Any CPU.Build.0 = Release|Any CPU
{7A7D8C81-CE36-4E92-8B08-D19DF903B23F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A7D8C81-CE36-4E92-8B08-D19DF903B23F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A7D8C81-CE36-4E92-8B08-D19DF903B23F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A7D8C81-CE36-4E92-8B08-D19DF903B23F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -95,6 +101,7 @@ Global
{2C85FA76-AF62-4D6D-89BF-EE9B52AFDC72} = {3E802220-D845-4B0F-9F98-ACCE8861F90D}
{DB9964A3-5C44-453D-8066-BEA6A9920209} = {3E802220-D845-4B0F-9F98-ACCE8861F90D}
{368D567C-90A9-40A8-B5A1-4F624754A062} = {9D5D61C4-D494-4124-A184-59C12C2BB2BC}
{7A7D8C81-CE36-4E92-8B08-D19DF903B23F} = {9D5D61C4-D494-4124-A184-59C12C2BB2BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A79E4AD9-244B-42E6-BD30-255B9C3DEC46}
Expand Down
50 changes: 50 additions & 0 deletions src/Validation.ApiDependencyValidators/ApiDependencyValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace Byndyusoft.ArchitectureTesting.Validation.ApiDependencyValidators
{
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Abstractions.ServiceContracts.Dependencies;
using Abstractions.ServiceImplementations;
using Abstractions.Validation.DependencyValidators;
using Abstractions.Validation.Extensions;

public class ApiDependencyValidator : DependencyValidatorBase<ApiDependency>
{
private static bool IsApiClient(Assembly assembly)
{
var cleanedAssemblyName = assembly.GetName().Name!.CleanString();
if (cleanedAssemblyName.Equals("Byndyusoft.ApiClient".CleanString()))
return false;

return cleanedAssemblyName.EndsWith(".Api.Client".CleanString())
|| cleanedAssemblyName.EndsWith(".Api.Clients".CleanString());
}

private static bool IsRepositoryApiClientAssembly(Assembly assembly, string repositoryName)
{
var cleanedAssemblyName = assembly.GetName().Name!.CleanString();
return cleanedAssemblyName.Equals($"{repositoryName}.Client".CleanString())
|| cleanedAssemblyName.Equals($"{repositoryName}.Clients".CleanString());
}

private static bool IsValidServiceAssembly(Assembly serviceAssembly, IEnumerable<ApiDependency> dependencies)
=> IsApiClient(serviceAssembly) == false
|| dependencies.Any(dependency => IsRepositoryApiClientAssembly(serviceAssembly, dependency.Name));

private static bool IsValidDependency(DependencyBase dependency, IEnumerable<Assembly> serviceAssemblies)
=> serviceAssemblies.Any(serviceAssembly => IsRepositoryApiClientAssembly(serviceAssembly, dependency.Name));

protected override IEnumerable<string> Validate(ApiDependency[] dependencies, ServiceImplementation serviceImplementation)
=> Enumerable.Empty<string>()
.Concat(
serviceImplementation.ServiceAssemblies
.Where(serviceAssembly => IsValidServiceAssembly(serviceAssembly, dependencies) == false)
.Select(serviceAssembly => $"Assembly {serviceAssembly.GetName().Name} is not allowed by architecture")
)
.Concat(
dependencies
.Where(dependency => IsValidDependency(dependency, serviceImplementation.ServiceAssemblies) == false)
.Select(dependency => $"Dependency {nameof(ApiDependency)}: {dependency} is not implemented in service")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\Abstractions\Abstractions.csproj" />
</ItemGroup>
</Project>

0 comments on commit c4c231d

Please sign in to comment.