Skip to content

Commit

Permalink
Merge pull request #481 from RHEAGROUP/Feat/move-bookconfiguration-to…
Browse files Browse the repository at this point in the history
…-configurationservices

Feat/add-namingconventions-service-and-cherrypick
  • Loading branch information
Robbware authored Oct 19, 2023
2 parents e71e4ae + 168dcbc commit e89c8ab
Show file tree
Hide file tree
Showing 12 changed files with 802 additions and 2 deletions.
7 changes: 5 additions & 2 deletions COMET.Web.Common.Tests/COMET.Web.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<None Remove="Data\server_configuration_tests.json" />
<Content Include="Data\server_configuration_tests.json">
<Content Include="Data\server_configuration_tests.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Remove="Resources\configuration\naming_convention.json" />
<Content Include="Resources\configuration\naming_convention.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</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,73 @@
// --------------------------------------------------------------------------------------------------------------------
// <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, Nabil Abbar
//
// 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.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://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.
//
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace COMET.Web.Common.Tests.Services.NamingConventionService
{
using COMET.Web.Common.Server.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>();

Assert.Multiple(() =>
{
foreach (var namingConventionKind in enumValues)
{
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,72 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CherryPickRunnerTestFixture.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, Nabil Abbar
//
// 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.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://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.
//
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace COMET.Web.Common.Tests.Utilities.CherryPick
{
using NUnit.Framework;

using CDP4Common.SiteDirectoryData;

using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.Utilities.CherryPick;

using Moq;

[TestFixture]
public class CherryPickRunnerTestFixture
{
private CherryPickRunner viewModel;
private Mock<ISessionService> sessionService;
private Mock<INeedCherryPickedData> needCherryPickedData;

[SetUp]
public void Setup()
{
this.sessionService = new Mock<ISessionService>();
this.needCherryPickedData = new Mock<INeedCherryPickedData>();
this.viewModel = new CherryPickRunner(this.sessionService.Object);
}

[Test]
public async Task VerifyProperties()
{
Assert.That(this.viewModel.IsCherryPicking, Is.False);
this.viewModel.InitializeProperties(new List<INeedCherryPickedData> { this.needCherryPickedData.Object });

this.sessionService.Setup(x => x.Session.RetrieveSiteDirectory()).Returns(new SiteDirectory());

var propertyInfo = typeof(CherryPickRunner).GetProperty("IsCherryPicking", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

propertyInfo?.SetValue(this.viewModel, true, null);
await this.viewModel.RunCherryPick();
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<CDP4Common.DTO.Thing>>>()), Times.Never);

propertyInfo?.SetValue(this.viewModel, false, null);
await this.viewModel.RunCherryPick();
this.needCherryPickedData.Verify(x => x.ProcessCherryPickedData(Moq.It.IsAny<IEnumerable<IEnumerable<CDP4Common.DTO.Thing>>>()), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// --------------------------------------------------------------------------------------------------------------------
// <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, Nabil Abbar
//
// 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.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://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.
//
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace COMET.Web.Common.Tests.WebAssembly.Services.NamingConventionService
{
using System.Net;

using COMET.Web.Common.Services.NamingConventionService;
using COMET.Web.Common.Test.Helpers;
using COMET.Web.Common.WebAssembly.Services.NamingConventionService;

using Microsoft.Extensions.Logging;

using Moq;

using NUnit.Framework;

using RichardSzalay.MockHttp;

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

[SetUp]
public void Setup()
{
this.mockHttpMessageHandler = new MockHttpMessageHandler();
var httpClient = this.mockHttpMessageHandler.ToHttpClient();
httpClient.BaseAddress = new Uri("http://localhost/");
this.logger = new Mock<ILogger<INamingConventionService<NamingConventionKindTestEnum>>>();
this.service = new NamingConventionService<NamingConventionKindTestEnum>(this.logger.Object, httpClient);
}

[Test]
public async Task VerifyService()
{
this.mockHttpMessageHandler.When(HttpMethod.Get, "/_content/CDP4.WEB.Common/naming_convention.json")
.Throw(new Exception());

await this.service.InitializeService();
this.logger.Verify(LogLevel.Critical, o => o!.ToString()!.Contains("Exception has been raised"), Times.Once());

this.mockHttpMessageHandler.ResetBackendDefinitions();

var httpResponse = new HttpResponseMessage()
{
StatusCode = HttpStatusCode.InternalServerError
};

this.mockHttpMessageHandler.When(HttpMethod.Get, "/_content/CDP4.WEB.Common/naming_convention.json")
.Respond(_ => httpResponse);

await this.service.InitializeService();
this.logger.Verify(LogLevel.Error, o => o!.ToString()!.Contains("Error fetching naming conventions. Status code:"), Times.Once());

httpResponse.StatusCode = HttpStatusCode.NotFound;

await this.service.InitializeService();
this.logger.Verify(LogLevel.Error, o => o!.ToString()!.Contains("Naming conventions file not found at "), Times.Once());

httpResponse.StatusCode = HttpStatusCode.OK;

var json = """
{
"TestValue1": "TestValue1",
"TestValue2": "TestValue2"
}
""";

httpResponse.Content = new StringContent(json);
await this.service.InitializeService();

var enumValues = Enum.GetValues<NamingConventionKindTestEnum>();

Assert.Multiple(() =>
{
foreach (var namingConventionKind in enumValues)
{
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,68 @@
// --------------------------------------------------------------------------------------------------------------------
// <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, Nabil Abbar
//
// 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.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://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.
//
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

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

using COMET.Web.Common.Services.NamingConventionService;

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> : BaseNamingConventionService<TEnum> where TEnum : Enum
{
/// <summary>
/// The <see cref="ILogger{TCategoryName}" />
/// </summary>
private readonly ILogger<INamingConventionService<TEnum>> logger;

public NamingConventionService(ILogger<INamingConventionService<TEnum>> logger) : base(logger)
{
this.logger = logger;
}

/// <summary>
/// Gets the naming convention configuration
/// </summary>
/// <returns>A <see cref="IReadOnlyDictionary{TKey,TValue}"/> of the naming convention configuration</returns>
protected override async Task<IReadOnlyDictionary<string, string>> GetNamingConventionConfiguration()
{
try
{
var namingConvention = JsonSerializer.Deserialize<Dictionary<string, string>>(await File.ReadAllTextAsync(Path.Combine("Resources", "configuration", "naming_convention.json")))!;
return new Dictionary<string, string>(namingConvention, StringComparer.OrdinalIgnoreCase);
}
catch (Exception e)
{
this.logger.LogError(e, "Error while getting the naming convention configuration file.");
throw;
}
}
}
}
Loading

0 comments on commit e89c8ab

Please sign in to comment.