Skip to content

Commit

Permalink
Factory for submodel service provider
Browse files Browse the repository at this point in the history
Changed the implementation to use submodel service provider for creating
provider for submodels instead of extension method and added tests for that

References eclipse-basyx#3
  • Loading branch information
bjoernhoeper committed Oct 16, 2022
1 parent c9b374d commit 938fbcb
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2022 LTSoft - Agrentur für Leittechnik-Software GmbH
* Author: Björn Höper ([email protected])
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

using BaSyx.Models.Core.AssetAdministrationShell.Generics;

namespace BaSyx.API.Components;

/// <summary>
/// Creates service providers for submodels
/// </summary>
public interface ISubmodelServiceProviderFactory
{
/// <summary>
/// Create a new submodel service provider for
/// </summary>
/// <param name="submodel"></param>
/// <returns></returns>
ISubmodelServiceProvider CreateSubmodelServiceProvider(ISubmodel submodel);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2022 LTSoft - Agrentur für Leittechnik-Software GmbH
* Author: Björn Höper ([email protected])
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

using BaSyx.Models.Core.AssetAdministrationShell.Generics;

namespace BaSyx.API.Components;

/// <summary>
/// Provides the internal submodel service provider as default implementation
/// </summary>
public class InternalSubmodelServiceProviderFactory : ISubmodelServiceProviderFactory
{
public ISubmodelServiceProvider CreateSubmodelServiceProvider(ISubmodel submodel)
{
InternalSubmodelServiceProvider sp = new InternalSubmodelServiceProvider(submodel);

return sp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace BaSyx.API.Components
{
public class SubmodelRepositoryServiceProvider : ISubmodelRepositoryServiceProvider
{
private readonly ISubmodelServiceProviderFactory _serviceProviderFactory;
public IEnumerable<ISubmodel> Submodels => GetBinding();

private Dictionary<string, ISubmodelServiceProvider> SubmodelServiceProviders { get; }
Expand All @@ -40,13 +41,14 @@ private set
_serviceDescriptor = value;
}
}
public SubmodelRepositoryServiceProvider(ISubmodelRepositoryDescriptor descriptor) : this()
public SubmodelRepositoryServiceProvider(ISubmodelRepositoryDescriptor descriptor, ISubmodelServiceProviderFactory serviceProviderFactory) : this(serviceProviderFactory)
{
ServiceDescriptor = descriptor;
}

public SubmodelRepositoryServiceProvider()
public SubmodelRepositoryServiceProvider(ISubmodelServiceProviderFactory serviceProviderFactory)
{
_serviceProviderFactory = serviceProviderFactory;
SubmodelServiceProviders = new Dictionary<string, ISubmodelServiceProvider>();
}

Expand Down Expand Up @@ -78,15 +80,14 @@ public IResult<ISubmodel> CreateSubmodel(ISubmodel submodel)
if (submodel == null)
return new Result<ISubmodel>(new ArgumentNullException(nameof(submodel)));

var registered = RegisterSubmodelServiceProvider(submodel.Identification.Id, submodel.CreateServiceProvider());
if (!registered.Success)
return new Result<ISubmodel>(registered);

var createdServiceProvider = _serviceProviderFactory.CreateSubmodelServiceProvider(submodel);
RegisterSubmodelServiceProvider(submodel.Identification.Id, createdServiceProvider);

var retrievedSubmodelServiceProvider = GetSubmodelServiceProvider(submodel.Identification.Id);
if (retrievedSubmodelServiceProvider.TryGetEntity(out ISubmodelServiceProvider serviceProvider))
return new Result<ISubmodel>(true, serviceProvider.GetBinding());
else
return new Result<ISubmodel>(false, new Message(MessageType.Error, "Could not retrieve Submodel Service Provider"));

return new Result<ISubmodel>(false, new Message(MessageType.Error, "Could not retrieve Submodel Service Provider"));
}

public IResult DeleteSubmodel(string submodelId)
Expand Down
10 changes: 8 additions & 2 deletions BaSyx.Core.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29418.71
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BaSyx.API", "BaSyx.API\BaSyx.API.csproj", "{87045925-FD2C-4BC9-BA55-8B4080BBA49C}"
EndProject
Expand All @@ -23,6 +23,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
LICENSE-3RD-PARTY.txt = LICENSE-3RD-PARTY.txt
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Basyx.API.Tests", "tests\Basyx.API.Tests\Basyx.API.Tests.csproj", "{7EF88D79-BF8F-4284-97C3-37ABF14CF86B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{CBA318C0-06E2-4AB7-B4A1-A5AC63A761EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBA318C0-06E2-4AB7-B4A1-A5AC63A761EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBA318C0-06E2-4AB7-B4A1-A5AC63A761EE}.Release|Any CPU.Build.0 = Release|Any CPU
{7EF88D79-BF8F-4284-97C3-37ABF14CF86B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EF88D79-BF8F-4284-97C3-37ABF14CF86B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EF88D79-BF8F-4284-97C3-37ABF14CF86B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EF88D79-BF8F-4284-97C3-37ABF14CF86B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
33 changes: 33 additions & 0 deletions tests/Basyx.API.Tests/Basyx.API.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Components\ServiceProvider\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\BaSyx.API\BaSyx.API.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2022 LTSoft - Agrentur für Leittechnik-Software GmbH
* Author: Björn Höper ([email protected])
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

using BaSyx.API.Components;
using BaSyx.Models.Core.AssetAdministrationShell.Generics;
using BaSyx.Models.Core.AssetAdministrationShell.Identification;
using BaSyx.Models.Core.AssetAdministrationShell.Implementations;
using Moq;

namespace Basyx.API.Tests.Components.ServiceProvider;

public class SubmodelRepositoryServiceProviderTests
{
[Fact]
public async Task CreateSubmodel_WhenCorrect_CallsFactory()
{
var submodelMock = new Mock<ISubmodel>();
submodelMock.Setup(s => s.Identification)
.Returns(new Identifier("http://assetadminshell.io/1/0/0/testmodel", KeyType.URI));

var serviceProviderMock = new Mock<ISubmodelServiceProvider>();
serviceProviderMock.Setup(p => p.GetBinding()).Returns(submodelMock.Object);

var factoryMock = new Mock<ISubmodelServiceProviderFactory>();
factoryMock.Setup(f => f.CreateSubmodelServiceProvider(submodelMock.Object)).Returns(serviceProviderMock.Object);

var submodelRepositoryServiceProvider = new SubmodelRepositoryServiceProvider(factoryMock.Object);
var result = submodelRepositoryServiceProvider.CreateSubmodel(submodelMock.Object);

factoryMock.Verify(f => f.CreateSubmodelServiceProvider(submodelMock.Object), Times.AtLeastOnce);

Assert.Equal(submodelMock.Object, result.Entity);
}
}
1 change: 1 addition & 0 deletions tests/Basyx.API.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;

0 comments on commit 938fbcb

Please sign in to comment.