Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add-namingconventions-service-and-cherrypick #481

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,69 @@
// --------------------------------------------------------------------------------------------------------------------
// <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 Moq;

[TestFixture]
public class CherryPickRunnerTestFixture
{
private Common.Utilities.CherryPick.CherryPickRunner viewModel;
private Mock<Common.Services.SessionManagement.ISessionService> sessionService;
Robbware marked this conversation as resolved.
Show resolved Hide resolved
private Mock<Common.Utilities.CherryPick.INeedCherryPickedData> needCherryPickedData;

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

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

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

var propertyInfo = typeof(Common.Utilities.CherryPick.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,115 @@
// --------------------------------------------------------------------------------------------------------------------
// <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;
Robbware marked this conversation as resolved.
Show resolved Hide resolved

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
Robbware marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// The <see cref="ILogger{TCategoryName}" />
/// </summary>
private readonly ILogger<INamingConventionService<TEnum>> logger;
Robbware marked this conversation as resolved.
Show resolved Hide resolved

public NamingConventionService(ILogger<INamingConventionService<TEnum>> logger) : base(logger)
Robbware marked this conversation as resolved.
Show resolved Hide resolved
{
this.logger = logger;
}

/// <summary>
/// Gets the naming convention configuration
/// </summary>
/// <returns>A <see cref="IReadOnlyDictionary{TKey,TValue}"/> of the naming convention configuration</returns>
public 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
Loading