-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create the NamingConventionService on the common lib and adapt it so …
…it can work for any type of enum that is provided via IOC
- Loading branch information
Showing
5 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
COMET.Web.Common.Tests/Resources/configuration/naming_convention.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"TestValue1": "TestValue1", | ||
"TestValue2": "TestValue2" | ||
} |
70 changes: 70 additions & 0 deletions
70
...T.Web.Common.Tests/Services/NamingConventionService/NamingConventionServiceTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
COMET.Web.Common/Services/NamingConventionService/INamingConventionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/// <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); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
COMET.Web.Common/Services/NamingConventionService/NamingConventionService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |