Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
test(arch): architecture testing
Browse files Browse the repository at this point in the history
  • Loading branch information
foxminchan committed May 12, 2024
1 parent b6e14be commit 8e1c797
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/RookieShop.IdentityService/Config.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Duende.IdentityServer.Models;
using Duende.IdentityServer;
using Duende.IdentityServer.Models;
using RookieShop.Domain.Constants;
using Duende.IdentityServer;

namespace RookieShop.IdentityService;

Expand All @@ -15,7 +15,7 @@ public static class Config
public static IEnumerable<ApiScope> ApiScopes =>
[
new(AuthScope.Read, "Read Access to API"),
new(AuthScope.Write, "Write Access to API"),
new(AuthScope.Write, "Write Access to API")
];

public static IEnumerable<ApiResource> ApiResources =>
Expand All @@ -36,7 +36,7 @@ public static IEnumerable<Client> Clients(IConfiguration configuration) =>
ClientName = "Resource Owner Client",
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
ClientSecrets = { new("secret".Sha256()) },
AllowedScopes = { AuthScope.Read, AuthScope.Write },
AllowedScopes = { AuthScope.Read, AuthScope.Write }
},
new()
{
Expand Down
78 changes: 78 additions & 0 deletions tests/RookieShop.ArchTests/ApplicationLayerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using RookieShop.Application;

namespace RookieShop.ArchTests;

public sealed class ApplicationLayerTest
{
[Fact]
public void Application_Should_HaveDependencyOnDomain()
{
// Arrange
var applicationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(applicationAssembly)
.That()
.ResideInNamespace(nameof(Application))
.Should()
.HaveDependencyOn(nameof(Domain))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Application_Should_HaveDependencyOnInfrastructure()
{
// Arrange
var applicationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(applicationAssembly)
.That()
.ResideInNamespace(nameof(Application))
.Should()
.HaveDependencyOn(nameof(Infrastructure))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Application_Should_HaveNotDependencyOnPersistence()
{
// Arrange
var applicationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(applicationAssembly)
.That()
.ResideInNamespace(nameof(Application))
.ShouldNot()
.HaveDependencyOn(nameof(Persistence))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Application_Should_HaveNotDependencyOnApiService()
{
// Arrange
var applicationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(applicationAssembly)
.That()
.ResideInNamespace(nameof(Application))
.ShouldNot()
.HaveDependencyOn(nameof(ApiService))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}
}
65 changes: 65 additions & 0 deletions tests/RookieShop.ArchTests/DomainLayerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Reflection;
using RookieShop.Domain;
using RookieShop.Domain.SeedWork;

namespace RookieShop.ArchTests;

public sealed class DomainLayerTest
{
[Fact]
public void DomainLayer_Should_BeSealed()
{
// Arrange
var assembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(assembly)
.That()
.AreNotNestedPrivate()
.And()
.HaveNameEndingWith(nameof(Domain))
.Should()
.BeSealed()
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Entities_Should_HavePublicParameterlessConstructor()
{
// Arrange
var entityType = Types.InAssembly(AssemblyReference.Assembly)
.That()
.Inherit(typeof(EntityBase))
.GetTypes();

// Act
var failingTypes = entityType.Where(type =>
Array.Find(type.GetConstructors(BindingFlags.Public | BindingFlags.Instance),
c => c.GetParameters().Length == 0) is null
).ToList();

// Assert
failingTypes.Should().BeEmpty();
}

[Fact]
public void Domain_Should_NotHaveDependencyOnApplication()
{
// Arrange
var domainAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(domainAssembly)
.That()
.ResideInNamespace(nameof(Domain))
.ShouldNot()
.HaveDependencyOn(nameof(Application))
.GetResult();

// Assert
result.IsSuccessful.Should().BeTrue();
}
}
42 changes: 42 additions & 0 deletions tests/RookieShop.ArchTests/InfrastructureLayerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using RookieShop.Infrastructure;

namespace RookieShop.ArchTests;

public sealed class InfrastructureLayerTest
{
[Fact]
public void Infrastructure_Should_HaveDependencyOnDomain()
{
// Arrange
var infrastructureAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(infrastructureAssembly)
.That()
.ResideInNamespace(nameof(Infrastructure))
.Should()
.HaveDependencyOn(nameof(Domain))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Infrastructure_Should_HaveNotDependencyOnPersistence()
{
// Arrange
var infrastructureAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(infrastructureAssembly)
.That()
.ResideInNamespace(nameof(Infrastructure))
.ShouldNot()
.HaveDependencyOn(nameof(Persistence))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}
}
79 changes: 79 additions & 0 deletions tests/RookieShop.ArchTests/PersistenceLayerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using RookieShop.Persistence;

namespace RookieShop.ArchTests;

public sealed class PersistenceLayerTest
{
[Fact]
public void Persistence_Should_HaveDependencyOnDomain()
{
// Arrange
var persistenceAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(persistenceAssembly)
.That()
.ResideInNamespace(nameof(Persistence))
.Should()
.HaveDependencyOn(nameof(Domain))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Persistence_Should_HaveDependencyOnInfrastructure()
{
// Arrange
var persistenceAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(persistenceAssembly)
.That()
.ResideInNamespace(nameof(Persistence))
.Should()
.HaveDependencyOn(nameof(Infrastructure))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Persistence_Should_HaveNotDependencyOnApplication()
{
// Arrange
var persistenceAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(persistenceAssembly)
.That()
.ResideInNamespace(nameof(Persistence))
.ShouldNot()
.HaveDependencyOn(nameof(Application))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void ApplicationDbContext_Should_HaveBeInPersistenceLayer()
{
// Arrange
var assembly = AssemblyReference.Assembly;

// Act
var result = Types
.InAssembly(assembly)
.That()
.ResideInNamespace(nameof(Persistence))
.Should()
.HaveName(nameof(ApplicationDbContext))
.GetResult();

// Assert
result.IsSuccessful.Should().BeTrue(result.ToString());
}
}
78 changes: 78 additions & 0 deletions tests/RookieShop.ArchTests/PresentationLayerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using RookieShop.ApiService;

namespace RookieShop.ArchTests;

public sealed class PresentationLayerTest
{
[Fact]
public void Presentation_Should_HaveDependencyOnApplication()
{
// Arrange
var presentationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(presentationAssembly)
.That()
.ResideInNamespace(nameof(ApiService))
.Should()
.HaveDependencyOn(nameof(Application))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Presentation_Should_HaveNotDependencyOnDomain()
{
// Arrange
var presentationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(presentationAssembly)
.That()
.ResideInNamespace(nameof(ApiService))
.ShouldNot()
.HaveDependencyOn(nameof(Domain))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Presentation_Should_HaveNotDependencyOnInfrastructure()
{
// Arrange
var presentationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(presentationAssembly)
.That()
.ResideInNamespace(nameof(ApiService))
.ShouldNot()
.HaveDependencyOn(nameof(Infrastructure))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}

[Fact]
public void Presentation_Should_HaveNotDependencyOnPersistence()
{
// Arrange
var presentationAssembly = AssemblyReference.Assembly;

// Act
var result = Types.InAssembly(presentationAssembly)
.That()
.ResideInNamespace(nameof(ApiService))
.ShouldNot()
.HaveDependencyOn(nameof(Persistence))
.GetResult();

// Assert
Assert.True(result.IsSuccessful);
}
}
Loading

0 comments on commit 8e1c797

Please sign in to comment.