Skip to content

Commit

Permalink
Parse PR feedback: namespace blocks, remove namespace usage, add docu…
Browse files Browse the repository at this point in the history
…mentation
  • Loading branch information
Robbware committed Oct 19, 2023
1 parent 4c84a11 commit 3098194
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,35 @@ namespace COMET.Web.Common.Tests.Utilities.CherryPick

using CDP4Common.SiteDirectoryData;

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

using Moq;

[TestFixture]
public class CherryPickRunnerTestFixture
{
private Common.Utilities.CherryPick.CherryPickRunner viewModel;
private Mock<Common.Services.SessionManagement.ISessionService> sessionService;
private Mock<Common.Utilities.CherryPick.INeedCherryPickedData> needCherryPickedData;
private CherryPickRunner viewModel;
private Mock<ISessionService> sessionService;
private Mock<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);
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<Common.Utilities.CherryPick.INeedCherryPickedData> { this.needCherryPickedData.Object });
this.viewModel.InitializeProperties(new List<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);
var propertyInfo = typeof(CherryPickRunner).GetProperty("IsCherryPicking", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

propertyInfo?.SetValue(this.viewModel, true, null);
await this.viewModel.RunCherryPick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,93 +23,94 @@
// </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;
namespace COMET.Web.Common.Tests.WebAssembly.Services.NamingConventionService
{
using System.Net;

using Microsoft.Extensions.Logging;
using COMET.Web.Common.Services.NamingConventionService;
using COMET.Web.Common.Test.Helpers;
using COMET.Web.Common.WebAssembly.Services.NamingConventionService;

using Moq;
using Microsoft.Extensions.Logging;

using NUnit.Framework;
using Moq;

using RichardSzalay.MockHttp;
using NUnit.Framework;

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

[SetUp]
public void Setup()
[TestFixture]
public class NamingConventionServiceTestFixture
{
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);
}
private NamingConventionService<NamingConventionKindTestEnum> service;
private MockHttpMessageHandler mockHttpMessageHandler;
private Mock<ILogger<INamingConventionService<NamingConventionKindTestEnum>>> logger;

[Test]
public async Task VerifyService()
{
this.mockHttpMessageHandler.When(HttpMethod.Get, "/_content/CDP4.WEB.Common/naming_convention.json")
.Throw(new Exception());
[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());
await this.service.InitializeService();
this.logger.Verify(LogLevel.Critical, o => o!.ToString()!.Contains("Exception has been raised"), Times.Once());

this.mockHttpMessageHandler.ResetBackendDefinitions();
this.mockHttpMessageHandler.ResetBackendDefinitions();

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

this.mockHttpMessageHandler.When(HttpMethod.Get, "/_content/CDP4.WEB.Common/naming_convention.json")
.Respond(_ => httpResponse);
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());
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;
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());
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;
httpResponse.StatusCode = HttpStatusCode.OK;

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

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

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

Assert.Multiple(() =>
{
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
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
Expand Up @@ -23,79 +23,85 @@
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace COMET.Web.Common.Services.NamingConventionService;

using Microsoft.Extensions.Logging;

public class BaseNamingConventionService<TEnum> : INamingConventionService<TEnum> where TEnum : Enum
namespace COMET.Web.Common.Services.NamingConventionService
{
/// <summary>
/// <see cref="Dictionary{TKey,TValue}" /> that holds the defined naming convention
/// </summary>
private readonly Dictionary<string, string> definedNaming = new(StringComparer.OrdinalIgnoreCase);
using Microsoft.Extensions.Logging;

/// <summary>
/// The <see cref="ILogger{TCategoryName}" />
/// The <see cref="BaseNamingConventionService{TEnum}" /> provides static information based on defined naming convention, like for names of
/// <see cref="Category" /> to use for example
/// </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>
protected BaseNamingConventionService(ILogger<INamingConventionService<TEnum>> logger)
/// <typeparam name="TEnum">Any type of enumeration that will contain the different types of naming conventions</typeparam>
public class BaseNamingConventionService<TEnum> : INamingConventionService<TEnum> where TEnum : Enum
{
this.logger = logger;
}
/// <summary>
/// <see cref="Dictionary{TKey,TValue}" /> that holds the defined naming convention
/// </summary>
private readonly Dictionary<string, string> definedNaming = new(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Initializes this service
/// </summary>
/// <returns>A <see cref="Task" /></returns>
public async Task InitializeService()
{
var namingConvention = await this.GetNamingConventionConfiguration();
/// <summary>
/// The <see cref="ILogger{TCategoryName}" />
/// </summary>
protected readonly ILogger<INamingConventionService<TEnum>> Logger;

foreach (var namingConventionKind in Enum.GetValues(typeof(TEnum)))
/// <summary>
/// Initializes a new instance of the <see cref="BaseNamingConventionService{TEnum}" /> class.
/// </summary>
/// <param name="logger">The <see cref="ILogger{TCategoryName}" /></param>
protected BaseNamingConventionService(ILogger<INamingConventionService<TEnum>> logger)
{
if (namingConvention.TryGetValue(namingConventionKind.ToString(), out var namingConventionValue))
{
this.definedNaming[namingConventionKind.ToString()] = namingConventionValue;
}
else
this.Logger = logger;
}

/// <summary>
/// Initializes this service
/// </summary>
/// <returns>A <see cref="Task" /></returns>
public async Task InitializeService()
{
var namingConvention = await this.GetNamingConventionConfiguration();

foreach (var namingConventionKind in Enum.GetValues(typeof(TEnum)))
{
this.logger.LogWarning("{namingConventionKind} is missing from the Naming Convention configuration file", namingConventionKind.ToString());
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="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());
}
/// <summary>
/// Gets the value for naming convention
/// </summary>
/// <param name="namingConventionKind">The enum that is used by the service</param>
/// <returns>The defined naming convention, if exists</returns>
public string GetNamingConventionValue(TEnum namingConventionKind)
{
return this.GetNamingConventionValue(namingConventionKind.ToString());
}

/// <summary>
/// Gets the naming convention configuration
/// </summary>
/// <returns>A <see cref="IReadOnlyDictionary{TKey,TValue}"/> of the naming convention configuration</returns>
/// <exception cref="NotImplementedException"></exception>
public virtual Task<IReadOnlyDictionary<string, string>> GetNamingConventionConfiguration()
{
throw new NotImplementedException();
/// <summary>
/// Gets the naming convention configuration
/// </summary>
/// <returns>A <see cref="IReadOnlyDictionary{TKey,TValue}"/> of the naming convention configuration</returns>
/// <exception cref="NotImplementedException"></exception>
public virtual Task<IReadOnlyDictionary<string, string>> GetNamingConventionConfiguration()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ 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>
/// <typeparam name="TEnum">Any type of enumeration that will contain the different types of naming conventions</typeparam>
public interface INamingConventionService<in TEnum> where TEnum : Enum
{
/// <summary>
Expand All @@ -50,6 +51,11 @@ public interface INamingConventionService<in TEnum> where TEnum : Enum
/// <returns>The defined naming convention, if exists</returns>
string GetNamingConventionValue(TEnum namingConventionKind);

/// <summary>
/// Gets the naming convention configuration
/// </summary>
/// <returns>A <see cref="IReadOnlyDictionary{TKey,TValue}"/> of the naming convention configuration</returns>
/// <exception cref="NotImplementedException"></exception>
Task<IReadOnlyDictionary<string, string>> GetNamingConventionConfiguration();
}
}
Loading

0 comments on commit 3098194

Please sign in to comment.