Skip to content

Commit

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

Issue #469 - Move book configuration to the existing ServiceConfiguration
  • Loading branch information
Robbware authored Oct 6, 2023
2 parents 285ea72 + f5f4df7 commit c3c7838
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace COMET.Web.Common.Tests.Components.BookEditor

using COMET.Web.Common.Components;
using COMET.Web.Common.Components.BookEditor;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.Test.Helpers;
using COMET.Web.Common.ViewModels.Components.BookEditor;
Expand Down Expand Up @@ -62,23 +64,18 @@ public class EditotPopupTestFixture
private bool onCancelCalled;
private bool onAcceptCalled;
private Mock<ISessionService> sessionService;
private MockHttpMessageHandler mockHttpMessageHandler;
private HttpClient httpClient;
private Mock<IConfigurationService> configurationService;

[SetUp]
public void Setup()
{
this.context = new TestContext();
this.context.ConfigureDevExpressBlazor();
this.sessionService = new Mock<ISessionService>();
this.configurationService = new Mock<IConfigurationService>();
this.configurationService.Setup(x => x.ServerConfiguration).Returns(new ServerConfiguration());
this.context.Services.AddSingleton(this.sessionService.Object);
this.mockHttpMessageHandler = new MockHttpMessageHandler();
this.httpClient = this.mockHttpMessageHandler.ToHttpClient();
this.httpClient.BaseAddress = new Uri("http://localhost/");
this.context.Services.AddScoped(_ => this.httpClient);
var httpResponse = new HttpResponseMessage();
httpResponse.Content = new StringContent("{\n \"ShowName\": true,\n \"ShowShortName\" : true \n}\n");
this.mockHttpMessageHandler.When(HttpMethod.Get, "/_content/CDP4.WEB.Common/BookInputConfiguration.json").Respond(_ => httpResponse);
this.context.Services.AddSingleton(this.configurationService.Object);
this.book = new Book();

this.activeDomains = new List<DomainOfExpertise>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace COMET.Web.Common.Tests.Components
using COMET.Web.Common.Components;
using COMET.Web.Common.Extensions;
using COMET.Web.Common.Model;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.RegistrationService;
using COMET.Web.Common.Services.SessionManagement;
Expand Down Expand Up @@ -68,6 +69,7 @@ public void Setup()
this.versionService = new Mock<IVersionService>();
this.sessionService = new Mock<ISessionService>();
this.serverConnectionService = new Mock<IConfigurationService>();
this.serverConnectionService.Setup(x => x.ServerConfiguration).Returns(new ServerConfiguration());
this.sourceList = new SourceList<Iteration>();
this.sessionService.Setup(x => x.OpenIterations).Returns(this.sourceList);

Expand Down
3 changes: 2 additions & 1 deletion COMET.Web.Common.Tests/Components/LoginTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace COMET.Web.Common.Tests.Components

using COMET.Web.Common.Components;
using COMET.Web.Common.Enumerations;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Model.DTO;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.SessionManagement;
Expand Down Expand Up @@ -57,7 +58,7 @@ public void Setup()
{
this.authenticationService = new Mock<IAuthenticationService>();
this.serverConnectionService = new Mock<IConfigurationService>();
this.serverConnectionService.Setup(x => x.ServerAddress).Returns("http://localhost.com");
this.serverConnectionService.Setup(x => x.ServerConfiguration).Returns(new ServerConfiguration { ServerAddress = "http://localhost.com" });
this.context = new TestContext();
this.viewModel = new LoginViewModel(this.authenticationService.Object, this.serverConnectionService.Object);
this.context.Services.AddSingleton(this.viewModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace COMET.Web.Common.Tests.Components
using COMET.Web.Common.Components;
using COMET.Web.Common.Components.Selectors;
using COMET.Web.Common.Extensions;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.Services.StringTableService;
Expand Down Expand Up @@ -74,9 +75,11 @@ public void Setup()
sessionService.Setup(x => x.Session).Returns(session.Object);
sessionService.Setup(x => x.GetDomainOfExpertise(It.IsAny<Iteration>())).Returns(new DomainOfExpertise(){Iid = Guid.NewGuid()});
this.viewModel.Setup(x => x.SessionService).Returns(sessionService.Object);
var mockConfigurationService = new Mock<IConfigurationService>();
mockConfigurationService.Setup(x => x.ServerConfiguration).Returns(new ServerConfiguration());
this.context.Services.AddSingleton(this.viewModel.Object);
this.context.Services.AddSingleton<IOpenModelViewModel, OpenModelViewModel>();
this.context.Services.AddSingleton(new Mock<IConfigurationService>().Object);
this.context.Services.AddSingleton(mockConfigurationService.Object);
this.context.Services.AddSingleton(new Mock<IStringTableService>().Object);
this.context.Services.AddSingleton(sessionService.Object);
this.context.ConfigureDevExpressBlazor();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"ServerAddress": ""
"ServerAddress": "",
"BookInputConfiguration": {
"ShowName": true,
"ShowShortName": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,49 +25,72 @@

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

using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Server.Services.ConfigurationService;

using Microsoft.Extensions.Configuration;

using Moq;

using NUnit.Framework;

[TestFixture]
public class ConfigurationServiceTestFixture
{
[Test]
public async Task VerifyInitializeServiceWithEmptyConfiguration()
{
var configuration = new Mock<IConfiguration>();
configuration.Setup(x => x.GetSection(ConfigurationService.AddressSection)).Returns(new Mock<IConfigurationSection>().Object);
configuration.Setup(x => x.GetSection(ConfigurationService.ServerConfigurationSection)).Returns(new Mock<IConfigurationSection>().Object);
var service = new ConfigurationService(configuration.Object);
await service.InitializeService();

Assert.Multiple(() =>
{
configuration.Verify(x => x.GetSection(ConfigurationService.AddressSection), Times.Once);
Assert.That(service.ServerAddress, Is.Null);
configuration.Verify(x => x.GetSection(ConfigurationService.ServerConfigurationSection), Times.Once);
Assert.That(service.ServerConfiguration.ServerAddress, Is.Null);
Assert.That(service.ServerConfiguration.BookInputConfiguration, Is.Null);
});

await service.InitializeService();
configuration.Verify(x => x.GetSection(ConfigurationService.AddressSection), Times.Once);

Assert.Multiple(() =>
{
configuration.Verify(x => x.GetSection(ConfigurationService.ServerConfigurationSection), Times.Once);
});
}

[Test]
public async Task VerifyInitializeServiceWithConfiguration()
{
var configurationSection = new Mock<IConfigurationSection>();
configurationSection.Setup(x => x.Value).Returns("https://a.b.c");
var serverAddressMockConfigurationSection = new Mock<IConfigurationSection>();

var serverConfiguration = new ServerConfiguration
{
ServerAddress = "https://a.b.c",
BookInputConfiguration = new BookInputConfiguration()
{
ShowName = true,
ShowShortName = true
}
};

var serverConfigurationJson = JsonSerializer.Serialize(serverConfiguration);
serverAddressMockConfigurationSection.Setup(x => x.Value).Returns(serverConfigurationJson);

var configuration = new Mock<IConfiguration>();
configuration.Setup(x => x.GetSection(ConfigurationService.AddressSection)).Returns(configurationSection.Object);
configuration.Setup(x => x.GetSection(ConfigurationService.ServerConfigurationSection)).Returns(serverAddressMockConfigurationSection.Object);
var service = new ConfigurationService(configuration.Object);
await service.InitializeService();

Assert.Multiple(() =>
{
configuration.Verify(x => x.GetSection(ConfigurationService.AddressSection), Times.Once);
Assert.That(service.ServerAddress, Is.EqualTo(configurationSection.Object.Value));
Assert.That(service.ServerConfiguration.ServerAddress, Is.EqualTo(serverConfiguration.ServerAddress));
Assert.That(service.ServerConfiguration.BookInputConfiguration, Is.Not.Null);
Assert.That(service.ServerConfiguration.BookInputConfiguration.ShowName, Is.EqualTo(serverConfiguration.BookInputConfiguration.ShowName));
Assert.That(service.ServerConfiguration.BookInputConfiguration.ShowShortName, Is.EqualTo(serverConfiguration.BookInputConfiguration.ShowShortName));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task VerifiyInitialization()
httpResponse.StatusCode = HttpStatusCode.OK;
httpResponse.Content = new StringContent("{\"ServerAddress\":\"http://localhost\"}");
await this.configurationService.InitializeService();
Assert.That(this.configurationService.ServerAddress, Is.EqualTo("http://localhost"));
Assert.That(this.configurationService.ServerConfiguration.ServerAddress, Is.EqualTo("http://localhost"));
}
}
}
3 changes: 0 additions & 3 deletions COMET.Web.Common/COMET.Web.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
<Content Update="wwwroot\DefaultTextConfiguration.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="wwwroot\BookInputConfiguration.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="wwwroot\server_configuration.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
51 changes: 7 additions & 44 deletions COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ namespace COMET.Web.Common.Components.BookEditor
using CDP4Common.ReportingData;

using COMET.Web.Common.Model;
using COMET.Web.Common.Model.DTO;
using COMET.Web.Common.Services;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Utilities;
using COMET.Web.Common.ViewModels.Components.BookEditor;

Expand All @@ -57,7 +59,7 @@ public partial class EditorPopup
/// Gets or sets the <see cref="HttpClient"/>
/// </summary>
[Inject]
public HttpClient HttpClient { get; set; }
public IConfigurationService ConfigurationService { get; set; }

/// <summary>
/// Gets or sets the <see cref="IOptions{TOptions}"/>
Expand All @@ -73,21 +75,11 @@ public partial class EditorPopup
/// </summary>
private bool showName;

/// <summary>
/// The name of the ShowName property on the configuration file
/// </summary>
private const string showNameConfigurationProperty = "ShowName";

/// <summary>
/// Sets if the component should show the shorname field
/// </summary>
private bool showShortName;

/// <summary>
/// The name of the ShowShortName property on the configuration file
/// </summary>
private const string showShortNameConfigurationProperty = "ShowShortName";

/// <summary>
/// Method invoked when the component is ready to start, having received its
/// initial parameters from its parent in the render tree.
Expand All @@ -100,40 +92,11 @@ protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
this.Disposables.Add(this.ViewModel.ValidationErrors.Connect().Subscribe(_ => this.InvokeAsync(this.StateHasChanged)));

var jsonFile = this.Options.Value.JsonConfigurationFile ?? "BookInputConfiguration.json";

try
{
var configurations = await this.GetBookInputConfigurationAsync(jsonFile);

if (configurations.TryGetValue(showNameConfigurationProperty, out var showNameValue))
{
this.showName = showNameValue;
}
var configurations = this.ConfigurationService.ServerConfiguration.BookInputConfiguration;

if (configurations.TryGetValue(showShortNameConfigurationProperty, out var showShortNameValue))
{
this.showShortName = showShortNameValue;
}
}
catch (Exception e)
{
this.Logger.LogError(e, "Error while getting the configuration file.");
}
}

/// <summary>
/// Acquires the BookInput configurations
/// </summary>
/// <param name="fileName">The file name that contains the configurations</param>
/// <returns>A KeyValuePair collection with each available configuration</returns>
private async Task<Dictionary<string, bool>> GetBookInputConfigurationAsync(string fileName)
{
var path = ContentPathBuilder.BuildPath(fileName);
var jsonContent = await this.HttpClient.GetStreamAsync(path);
var configurations = JsonSerializer.Deserialize<Dictionary<string, bool>>(jsonContent);
return configurations;
//The fields will be shown by default
this.showName = configurations?.ShowName ?? true;
this.showShortName = configurations?.ShowShortName ?? true;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion COMET.Web.Common/Components/Login.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<EditForm Context="editFormContext" Model="@(this.ViewModel.AuthenticationDto)" OnValidSubmit="this.ExecuteLogin">
<DataAnnotationsValidator/>
<DxFormLayout CaptionPosition="CaptionPosition.Vertical">
@if (string.IsNullOrEmpty(this.ViewModel.serverConnectionService.ServerAddress))
@if (string.IsNullOrEmpty(this.ViewModel.serverConnectionService.ServerConfiguration.ServerAddress))
{
<DxFormLayoutItem Caption="Source Address:" ColSpanLg="12">
<Template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ internal void SetCorrectUrl()
currentOptions[QueryKeys.IterationKey] = this.ViewModel.SelectedIteration.Iid.ToShortGuid();
currentOptions[QueryKeys.ModelKey] = this.ViewModel.SelectedIteration.IterationSetup.Container.Iid.ToShortGuid();

if (string.IsNullOrEmpty(this.ServerConnectionService.ServerAddress))
if (string.IsNullOrEmpty(this.ServerConnectionService.ServerConfiguration.ServerAddress))
{
currentOptions[QueryKeys.ServerKey] = this.ViewModel.SessionService.Session.DataSourceUri;

Expand Down
41 changes: 41 additions & 0 deletions COMET.Web.Common/Model/Configuration/BookInputConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BookInputConfigurationDto.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.Model.Configuration;

/// <summary>
/// Holds all of the configuration related to the Book feature
/// </summary>
public class BookInputConfiguration
{
/// <summary>
/// Verifies if the Name field will be displayed on the Book Input form
/// </summary>
public bool ShowName { get; set; }

/// <summary>
/// Verifies if the ShortName field will be displayed on the Book Input form
/// </summary>
public bool ShowShortName { get; set; }
}
41 changes: 41 additions & 0 deletions COMET.Web.Common/Model/Configuration/ServerConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ServerConfiguration.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.Model.Configuration;

/// <summary>
/// This class holds all of the configuration related properties that are stored on the local configuration files.
/// </summary>
public class ServerConfiguration
{
/// <summary>
/// The Server Address to use
/// </summary>
public string ServerAddress { get; set; }

/// <summary>
/// The configuration values for the Book feature
/// </summary>
public BookInputConfiguration BookInputConfiguration { get; set; }
}
Loading

0 comments on commit c3c7838

Please sign in to comment.