Skip to content

Commit

Permalink
Create the NamingConventionService on the common lib and adapt it so …
Browse files Browse the repository at this point in the history
…it can work for any type of enum that is provided via IOC
  • Loading branch information
Robbware committed Oct 18, 2023
1 parent b8c333c commit d4d0c38
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 0 deletions.
4 changes: 4 additions & 0 deletions COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<None Remove="Resources\configuration\naming_convention.json" />
<Content Include="Resources\configuration\naming_convention.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"TestValue1": "TestValue1",
"TestValue2": "TestValue2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NamingConventionServiceTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2023 RHEA System S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine
//
// This file is part of COMET WEB Community Edition
// The COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C.
//
// The COMET WEB Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The COMET WEB Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace COMET.Web.Common.Tests.Services.NamingConventionService
{
using COMET.Web.Common.Services.NamingConventionService;

using Microsoft.Extensions.Logging;

using Moq;

using NUnit.Framework;

[TestFixture]
public class NamingConventionServiceTestFixture
{
private NamingConventionService<NamingConventionKindTestEnum> service;
private Mock<ILogger<INamingConventionService<NamingConventionKindTestEnum>>> logger;

[SetUp]
public void Setup()
{
this.logger = new Mock<ILogger<INamingConventionService<NamingConventionKindTestEnum>>>();
this.service = new NamingConventionService<NamingConventionKindTestEnum>(this.logger.Object);
}

[Test]
public async Task VerifyInitializationAndGetNamingConvention()
{
await this.service.InitializeService();
var enumValues = Enum.GetValues<NamingConventionKindTestEnum>();

foreach (var namingConventionKind in enumValues)
{
Assert.Multiple(() =>
{
Assert.That(this.service.GetNamingConventionValue(namingConventionKind), Is.Not.Empty);
});
}
}

/// To be used for testing purposes only
public enum NamingConventionKindTestEnum
{
TestValue1,
TestValue2
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="INamingConventionsService.cs" company="RHEA System S.A.">
// Copyright (c) 2023 RHEA System S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine
//
// This file is part of COMET WEB Community Edition
// The COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C.
//
// The COMET WEB Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The COMET WEB Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace COMET.Web.Common.Services.NamingConventionService
{
/// <summary>
/// The <see cref="INamingConventionService"/> provides static information based on defined naming convention, like for names of <see cref="Category"/> to use for example
/// </summary>
public interface INamingConventionService<TEnum> where TEnum : Enum

Check warning on line 30 in COMET.Web.Common/Services/NamingConventionService/INamingConventionService.cs

View workflow job for this annotation

GitHub Actions / Build

Add the 'in' keyword to parameter 'TEnum' to make it 'contravariant'. (https://rules.sonarsource.com/csharp/RSPEC-3246)
{
/// <summary>
/// Initializes this service
/// </summary>
/// <returns>A <see cref="Task" /></returns>
Task InitializeService();

/// <summary>
/// Gets the value for naming convention
/// </summary>
/// <param name="namingConventionKey">The naming convention key</param>
/// <returns>The defined naming convention, if exists</returns>
string GetNamingConventionValue(string namingConventionKey);

/// <summary>
/// Gets the value for naming convention
/// </summary>
/// <param name="namingConventionKind">The <see cref="NamingConventionKind" /></param>
/// <returns>The defined naming convention, if exists</returns>
string GetNamingConventionValue(TEnum namingConventionKind);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="NamingConventionService.cs" company="RHEA System S.A.">
// Copyright (c) 2023 RHEA System S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine
//
// This file is part of COMET WEB Community Edition
// The COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C.
//
// The COMET WEB Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The COMET WEB Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace COMET.Web.Common.Services.NamingConventionService
{
using System.Text.Json;

using Microsoft.Extensions.Logging;

/// <summary>
/// The <see cref="NamingConventionService" /> provides static information based on defined naming convention, like for names of
/// <see cref="Category" /> to use for example
/// </summary>
public class NamingConventionService<TEnum> : INamingConventionService<TEnum> where TEnum : Enum
{
/// <summary>
/// <see cref="Dictionary{TKey,TValue}" /> that holds the defined naming convention
/// </summary>
private readonly Dictionary<string, string> definedNaming = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// The <see cref="ILogger{TCategoryName}" />
/// </summary>
private readonly ILogger<INamingConventionService<TEnum>> logger;

/// <summary>
/// Initializes a new instance of the <see cref="NamingConventionService" /> class.
/// </summary>
/// <param name="logger">The <see cref="ILogger{TCategoryName}" /></param>
public NamingConventionService(ILogger<INamingConventionService<TEnum>> logger)
{
this.logger = logger;
}

/// <summary>
/// Initializes this service
/// </summary>
/// <returns>A <see cref="Task" /></returns>
public async Task InitializeService()
{
Dictionary<string, string> namingConvention;

try
{
namingConvention = JsonSerializer.Deserialize<Dictionary<string, string>>(await File.ReadAllTextAsync(Path.Combine("Resources", "configuration", "naming_convention.json")))!;
}
catch (Exception e)
{
this.logger.LogError(e, "Error while getting the naming convention configuration file.");
return;
}

namingConvention = new Dictionary<string, string>(namingConvention, StringComparer.OrdinalIgnoreCase);

foreach (var namingConventionKind in Enum.GetValues(typeof(TEnum)))
{
if (namingConvention.TryGetValue(namingConventionKind.ToString(), out var namingConventionValue))
{
this.definedNaming[namingConventionKind.ToString()] = namingConventionValue;
}
else
{
this.logger.LogWarning("{namingConventionKind} is missing from the Naming Convention configuration file", namingConventionKind.ToString());
}
}
}

/// <summary>
/// Gets the value for naming convention
/// </summary>
/// <param name="namingConventionKey">The naming convention key</param>
/// <returns>The defined naming convention, if exists</returns>
public string GetNamingConventionValue(string namingConventionKey)
{
return this.definedNaming.TryGetValue(namingConventionKey, out var namingConventionValue) ? namingConventionValue : string.Empty;
}

/// <summary>
/// Gets the value for naming convention
/// </summary>
/// <param name="namingConventionKind">The <see cref="NamingConventionKind" /></param>
/// <returns>The defined naming convention, if exists</returns>
public string GetNamingConventionValue(TEnum namingConventionKind)
{
return this.GetNamingConventionValue(namingConventionKind.ToString());
}
}
}

0 comments on commit d4d0c38

Please sign in to comment.