Skip to content

Commit

Permalink
feat(wallet): add existence check for wallet creation
Browse files Browse the repository at this point in the history
Refs: #66
  • Loading branch information
Phil91 committed Aug 2, 2024
1 parent 767e121 commit 05a5871
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Dim.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DimProcess.Executor.Tests",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DimProcess.Library.Tests", "..\tests\processes\DimProcess.Library.Tests\DimProcess.Library.Tests.csproj", "{85D316A0-17BE-4983-AB06-5C72365ABD9B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dim.Web.Tests", "..\tests\web\Dim.Web.Tests\Dim.Web.Tests.csproj", "{CE87E424-36CF-4597-9E08-2D687E67F259}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -106,6 +108,10 @@ Global
{85D316A0-17BE-4983-AB06-5C72365ABD9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85D316A0-17BE-4983-AB06-5C72365ABD9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85D316A0-17BE-4983-AB06-5C72365ABD9B}.Release|Any CPU.Build.0 = Release|Any CPU
{CE87E424-36CF-4597-9E08-2D687E67F259}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE87E424-36CF-4597-9E08-2D687E67F259}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE87E424-36CF-4597-9E08-2D687E67F259}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE87E424-36CF-4597-9E08-2D687E67F259}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{8356C7AF-6F88-4A62-B3E9-5656634A6FEA} = {B84A3CAB-AC86-4B2D-A490-79E1002350FF}
Expand All @@ -123,5 +129,6 @@ Global
{0D288AF0-1CE5-4B2B-9F80-532040F24BCF} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
{A44447B0-794D-451A-A571-E3B761174B48} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
{85D316A0-17BE-4983-AB06-5C72365ABD9B} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
{CE87E424-36CF-4597-9E08-2D687E67F259} = {CB1B7D43-9AFC-47EF-8915-A547F7F553AB}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ public interface ITenantRepository
Task<(bool Exists, Guid TechnicalUserId, Guid ProcessId)> GetTechnicalUserForBpn(string bpn, string technicalUserName);
Task<Guid> GetExternalIdForTechnicalUser(Guid technicalUserId);
void RemoveTechnicalUser(Guid technicalUserId);
Task<bool> IsTenantExisting(string companyName, string bpn);
}
6 changes: 5 additions & 1 deletion src/database/Dim.DbAccess/Repositories/TenantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,9 @@ public Task<Guid> GetExternalIdForTechnicalUser(Guid technicalUserId) =>

public void RemoveTechnicalUser(Guid technicalUserId) =>
context.TechnicalUsers
.Remove(new TechnicalUser(technicalUserId, default, default, null!, default));
.Remove(new TechnicalUser(technicalUserId, Guid.Empty, Guid.Empty, null!, Guid.Empty));

public Task<bool> IsTenantExisting(string companyName, string bpn) =>
context.Tenants
.AnyAsync(x => x.CompanyName == companyName && x.Bpn == bpn);
}
5 changes: 5 additions & 0 deletions src/web/Dim.Web/BusinessLogic/DimBusinessLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class DimBusinessLogic(

public async Task StartSetupDim(string companyName, string bpn, string didDocumentLocation, bool isIssuer)
{
if (await dimRepositories.GetInstance<ITenantRepository>().IsTenantExisting(companyName, bpn).ConfigureAwait(ConfigureAwaitOptions.None))
{
throw new ConflictException($"Tenant {companyName} with Bpn {bpn} already exists");
}

var processStepRepository = dimRepositories.GetInstance<IProcessStepRepository>();
var processId = processStepRepository.CreateProcess(ProcessTypeId.SETUP_DIM).Id;
processStepRepository.CreateProcessStep(ProcessStepTypeId.CREATE_SUBACCOUNT, ProcessStepStatusId.TODO, processId);
Expand Down
31 changes: 31 additions & 0 deletions tests/web/Dim.Web.Tests/Dim.Web.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<RootNamespace>Dim.Web.Tests</RootNamespace>
<AssemblyName>Dim.Web.Tests</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture.AutoFakeItEasy" Version="4.18.1" />
<PackageReference Include="FakeItEasy" Version="8.1.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\clients\Dim.Clients\Dim.Clients.csproj" />
<ProjectReference Include="..\..\..\src\database\Dim.DbAccess\Dim.DbAccess.csproj" />
<ProjectReference Include="..\..\..\src\web\Dim.Web\Dim.Web.csproj" />
<ProjectReference Include="..\..\shared\Tests.Shared\Tests.Shared.csproj" />
</ItemGroup>
</Project>
118 changes: 118 additions & 0 deletions tests/web/Dim.Web.Tests/DimBusinessLogicTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/********************************************************************************
* Copyright 2024 SAP SE or an SAP affiliate company and ssi-dim-middle-layer contributors.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Dim.Clients.Api.Cf;
using Dim.Clients.Api.Dim;
using Dim.DbAccess;
using Dim.DbAccess.Repositories;
using Dim.Entities.Entities;
using Dim.Entities.Enums;
using Dim.Web.BusinessLogic;
using Microsoft.Extensions.Options;
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling;

namespace Dim.Web.Tests;

public class DimBusinessLogicTests
{
private static readonly Guid OperatorId = Guid.NewGuid();
private readonly IDimBusinessLogic _sut;
private readonly ICfClient _cfClient;
private readonly IDimClient _dimClient;
private readonly ITenantRepository _tenantRepository;
private readonly IProcessStepRepository _processStepRepository;

public DimBusinessLogicTests()
{
var fixture = new Fixture().Customize(new AutoFakeItEasyCustomization { ConfigureMembers = true });
fixture.Behaviors.OfType<ThrowingRecursionBehavior>().ToList()
.ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new OmitOnRecursionBehavior());

var repositories = A.Fake<IDimRepositories>();
_dimClient = A.Fake<IDimClient>();
_cfClient = A.Fake<ICfClient>();

_tenantRepository = A.Fake<ITenantRepository>();
_processStepRepository = A.Fake<IProcessStepRepository>();

A.CallTo(() => repositories.GetInstance<ITenantRepository>()).Returns(_tenantRepository);
A.CallTo(() => repositories.GetInstance<IProcessStepRepository>()).Returns(_processStepRepository);

_sut = new DimBusinessLogic(repositories, _cfClient, _dimClient, Options.Create(new DimSettings
{
OperatorId = OperatorId
}));
}

[Fact]
public async Task StartSetupDim_WithExisting_ThrowsConflictException()
{
// Arrange
A.CallTo(() => _tenantRepository.IsTenantExisting(A<string>._, A<string>._))
.Returns(true);
async Task Act() => await _sut.StartSetupDim("testCompany", "BPNL00000001TEST", "https://example.org/test", false);

// Act
var result = await Assert.ThrowsAsync<ConflictException>(Act);

// Assert
result.Message.Should().Be($"Tenant testCompany with Bpn BPNL00000001TEST already exists");
}

[Fact]
public async Task StartSetupDim_WithNewData_CreatesExpected()
{
// Arrange
var processId = Guid.NewGuid();
var processes = new List<Process>();
var processSteps = new List<ProcessStep>();
var tenants = new List<Tenant>();
A.CallTo(() => _tenantRepository.IsTenantExisting(A<string>._, A<string>._))
.Returns(false);
A.CallTo(() => _processStepRepository.CreateProcess(A<ProcessTypeId>._))
.Invokes((ProcessTypeId processTypeId) =>
{
processes.Add(new Process(processId, processTypeId, Guid.NewGuid()));
});
A.CallTo(() => _processStepRepository.CreateProcessStep(A<ProcessStepTypeId>._, A<ProcessStepStatusId>._, A<Guid>._))
.Invokes((ProcessStepTypeId processStepTypeId, ProcessStepStatusId processStepStatusId, Guid pId) =>
{
processSteps.Add(new ProcessStep(Guid.NewGuid(), processStepTypeId, processStepStatusId, processId, DateTimeOffset.UtcNow));
});
A.CallTo(() =>
_tenantRepository.CreateTenant(A<string>._, A<string>._, A<string>._, A<bool>._, A<Guid>._, A<Guid>._))
.Invokes((string companyName, string bpn, string didDocumentLocation, bool isIssuer, Guid pId,
Guid operatorId) =>
{
tenants.Add(new Tenant(Guid.NewGuid(), companyName, bpn, didDocumentLocation, isIssuer, pId, operatorId));
});

// Act
await _sut.StartSetupDim("testCompany", "BPNL00000001TEST", "https://example.org/test", false);

// Assert
processes.Should().ContainSingle()
.Which.ProcessTypeId.Should().Be(ProcessTypeId.SETUP_DIM);
processSteps.Should().ContainSingle()
.And.Satisfy(x => x.ProcessId == processId && x.ProcessStepTypeId == ProcessStepTypeId.CREATE_SUBACCOUNT);
tenants.Should().ContainSingle()
.And.Satisfy(x => x.CompanyName == "testCompany" && x.Bpn == "BPNL00000001TEST");
}
}
24 changes: 24 additions & 0 deletions tests/web/Dim.Web.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/********************************************************************************
* Copyright 2024 SAP SE or an SAP affiliate company and ssi-dim-middle-layer contributors.
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

global using AutoFixture;
global using AutoFixture.AutoFakeItEasy;
global using FakeItEasy;
global using FluentAssertions;
global using Xunit;

0 comments on commit 05a5871

Please sign in to comment.