From 124868304b792d7bac6dfae769231c857262030b Mon Sep 17 00:00:00 2001 From: Robbware Date: Wed, 4 Oct 2023 13:26:50 +0100 Subject: [PATCH 1/3] Refactor the ShowName/ShowShortName configuration so that it's done on the parent component first (where the validations are made) and set to the child component. --- .../BookEditor/InputEditorTestFixture.cs | 4 + .../Components/BookEditor/EditorPopup.razor | 2 +- .../BookEditor/EditorPopup.razor.cs | 85 +++++++++++++++++-- .../Components/BookEditor/InputEditor.razor | 4 +- .../BookEditor/InputEditor.razor.cs | 73 +++++----------- .../wwwroot/BookInputConfiguration.json | 4 +- COMETwebapp/COMETwebapp.csproj | 6 ++ .../wwwroot/BookInputConfiguration.json | 4 + 8 files changed, 118 insertions(+), 64 deletions(-) create mode 100644 COMETwebapp/wwwroot/BookInputConfiguration.json diff --git a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs index bd9de519..1eddac4b 100644 --- a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs +++ b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs @@ -100,6 +100,8 @@ public void Setup() parameters.Add(p => p.Item, this.book); parameters.Add(p => p.ActiveDomains, this.activeDomains); parameters.Add(p => p.AvailableCategories, this.availableCategories); + parameters.Add(p => p.ShowName, true); + parameters.Add(p => p.ShowShortName, true); }); } @@ -119,6 +121,8 @@ public void VerifyComponent() Assert.IsNotNull(shortNameTextbox); Assert.That(combobox.Instance.Value, Is.EqualTo(this.activeDomains.First())); Assert.That(categoryComboBox.Instance, Is.Not.Null); + Assert.IsTrue(this.component.Instance.ShowName); + Assert.IsTrue(this.component.Instance.ShowShortName); }); this.component.Render(); diff --git a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor index 4d3482fc..994f736e 100644 --- a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor +++ b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor @@ -24,7 +24,7 @@ - +
@foreach (var validationError in this.ViewModel.ValidationErrors.Items) { diff --git a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs index 46434852..34745ef2 100644 --- a/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs +++ b/COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs @@ -24,13 +24,19 @@ // -------------------------------------------------------------------------------------------------------------------- namespace COMET.Web.Common.Components.BookEditor -{ - using CDP4Common.ReportingData; - +{ + using System.Text.Json; + + using CDP4Common.ReportingData; + + using COMET.Web.Common.Model; using COMET.Web.Common.Services; + using COMET.Web.Common.Utilities; using COMET.Web.Common.ViewModels.Components.BookEditor; using Microsoft.AspNetCore.Components; + using Microsoft.Extensions.Logging; + using Microsoft.Extensions.Options; using INamedThing = CDP4Common.CommonData.INamedThing; using IOwnedThing = CDP4Common.EngineeringModelData.IOwnedThing; @@ -47,6 +53,41 @@ public partial class EditorPopup [Parameter] public IEditorPopupViewModel ViewModel { get; set; } + /// + /// Gets or sets the + /// + [Inject] + public HttpClient HttpClient { get; set; } + + /// + /// Gets or sets the + /// + [Inject] + public IOptions Options { get; set; } + + [Inject] + public ILogger Logger { get; set; } + + /// + /// Sets if the component should show the name field + /// + private bool showName; + + /// + /// The name of the ShowName property on the configuration file + /// + private const string showNameConfigurationProperty = "ShowName"; + + /// + /// Sets if the component should show the shorname field + /// + private bool showShortName; + + /// + /// The name of the ShowShortName property on the configuration file + /// + private const string showShortNameConfigurationProperty = "ShowShortName"; + /// /// Method invoked when the component is ready to start, having received its /// initial parameters from its parent in the render tree. @@ -59,6 +100,40 @@ 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; + } + + if (configurations.TryGetValue(showShortNameConfigurationProperty, out var showShortNameValue)) + { + this.showShortName = showShortNameValue; + } + } + catch (Exception e) + { + this.Logger.LogError(e, "Error while getting the configuration file."); + } + } + + /// + /// Acquires the BookInput configurations + /// + /// The file name that contains the configurations + /// A KeyValuePair collection with each available configuration + private async Task> GetBookInputConfigurationAsync(string fileName) + { + var path = ContentPathBuilder.BuildPath(fileName); + var jsonContent = await this.HttpClient.GetStreamAsync(path); + var configurations = JsonSerializer.Deserialize>(jsonContent); + return configurations; } /// @@ -68,13 +143,13 @@ private async Task OnConfirmClick() { var validationErrors = new List(); - if (this.ViewModel.Item is INamedThing namedThing) + if (this.ViewModel.Item is INamedThing namedThing && this.showName) { var error = ValidationService.ValidateProperty(nameof(namedThing.Name), namedThing.Name); validationErrors.Add(error); } - if (this.ViewModel.Item is IShortNamedThing shortNamedThing) + if (this.ViewModel.Item is IShortNamedThing shortNamedThing && this.showShortName) { var error = ValidationService.ValidateProperty(nameof(shortNamedThing.ShortName), shortNamedThing.ShortName); validationErrors.Add(error); diff --git a/COMET.Web.Common/Components/BookEditor/InputEditor.razor b/COMET.Web.Common/Components/BookEditor/InputEditor.razor index 37eda25a..c5c67448 100644 --- a/COMET.Web.Common/Components/BookEditor/InputEditor.razor +++ b/COMET.Web.Common/Components/BookEditor/InputEditor.razor @@ -28,7 +28,7 @@ @typeparam TItem
- @if (this.Item is INamedThing namedThing && this.showName) + @if (this.Item is INamedThing namedThing && this.ShowName) {

Name:

@@ -36,7 +36,7 @@
} - @if (this.Item is IShortNamedThing shortNamedThing && this.showShortName) + @if (this.Item is IShortNamedThing shortNamedThing && this.ShowShortName) {

ShortName:

diff --git a/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs b/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs index 4021b281..5fb6dd0c 100644 --- a/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs +++ b/COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs @@ -27,6 +27,7 @@ namespace COMET.Web.Common.Components.BookEditor { using System.Text.Json; + using CDP4Common.CommonData; using CDP4Common.EngineeringModelData; using CDP4Common.SiteDirectoryData; @@ -49,18 +50,6 @@ public partial class InputEditor [Inject] public ILogger> Logger { get; set; } - /// - /// Gets or sets the - /// - [Inject] - public HttpClient HttpClient { get; set; } - - /// - /// Gets or sets the - /// - [Inject] - public IOptions Options { get; set; } - /// /// Gets or sets the /// @@ -84,27 +73,19 @@ public partial class InputEditor ///
[Parameter] public IEnumerable AvailableCategories { get; set; } - - /// - /// Sets if the component should show the name field - /// - private bool showName; - - /// - /// The name of the ShowName property on the configuration file - /// - private const string showNameConfigurationProperty = "ShowName"; - + /// - /// Sets if the component should show the shorname field + /// Gets or sets if the InputEditor should display the Name field /// - private bool showShortName; - + [Parameter] + public bool ShowName { get; set; } + /// - /// The name of the ShowShortName property on the configuration file + /// Gets or sets if the InputEditor should display the ShortName field /// - private const string showShortNameConfigurationProperty = "ShowShortName"; - + [Parameter] + public bool ShowShortName { get; set; } + /// /// Method invoked when the component is ready to start, having received its /// initial parameters from its parent in the render tree. @@ -117,46 +98,30 @@ protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); - var jsonFile = this.Options.Value.JsonConfigurationFile ?? "BookInputConfiguration.json"; - try { - var configurations = await this.GetBookInputConfigurationAsync(jsonFile); - - if (configurations.TryGetValue(showNameConfigurationProperty, out var showNameValue)) + if (this.Item is IOwnedThing ownedThing) { - this.showName = showNameValue; + ownedThing.Owner = this.SessionService.Session.ActivePerson.DefaultDomain; } - if (configurations.TryGetValue(showShortNameConfigurationProperty, out var showShortNameValue)) + //Name and ShortName are required fields on the SDK - setting these to - as default, as per request on the ticket. + if (this.Item is INamedThing namedThing && !this.ShowName) { - this.showShortName = showShortNameValue; + namedThing.Name = "-"; } - if (this.Item is IOwnedThing ownedThing) + if (this.Item is IShortNamedThing shortNamedThing && !this.ShowShortName) { - ownedThing.Owner = this.SessionService.Session.ActivePerson.DefaultDomain; + shortNamedThing.ShortName = "-"; } } - catch (Exception e) + catch (Exception ex) { - this.Logger.LogError(e, "Error while getting the configuration file."); + this.Logger.LogError(ex, "Exception while setting default values of the InputEditor."); } } - /// - /// Acquires the BookInput configurations - /// - /// The file name that contains the configurations - /// A KeyValuePair collection with each available configuration - private async Task> GetBookInputConfigurationAsync(string fileName) - { - var path = ContentPathBuilder.BuildPath(fileName); - var jsonContent = await this.HttpClient.GetStreamAsync(path); - var configurations = JsonSerializer.Deserialize>(jsonContent); - return configurations; - } - /// /// Handler for when the selected categories changed /// diff --git a/COMET.Web.Common/wwwroot/BookInputConfiguration.json b/COMET.Web.Common/wwwroot/BookInputConfiguration.json index a0c6a76d..e4781159 100644 --- a/COMET.Web.Common/wwwroot/BookInputConfiguration.json +++ b/COMET.Web.Common/wwwroot/BookInputConfiguration.json @@ -1,4 +1,4 @@ { - "ShowName": true, - "ShowShortName" : true + "ShowName": false, + "ShowShortName" : false } diff --git a/COMETwebapp/COMETwebapp.csproj b/COMETwebapp/COMETwebapp.csproj index 5c09b3c2..a77fd4e2 100644 --- a/COMETwebapp/COMETwebapp.csproj +++ b/COMETwebapp/COMETwebapp.csproj @@ -30,4 +30,10 @@ + + + Always + + + diff --git a/COMETwebapp/wwwroot/BookInputConfiguration.json b/COMETwebapp/wwwroot/BookInputConfiguration.json new file mode 100644 index 00000000..e4781159 --- /dev/null +++ b/COMETwebapp/wwwroot/BookInputConfiguration.json @@ -0,0 +1,4 @@ +{ + "ShowName": false, + "ShowShortName" : false +} From 39368b035723f996b09caedade6a2a798267b2cf Mon Sep 17 00:00:00 2001 From: Robbware Date: Wed, 4 Oct 2023 13:31:31 +0100 Subject: [PATCH 2/3] Cover the HttpClient mocking on the EditorPopup test instead of the InputEditor test --- .../Components/BookEditor/EditotPopupTestFixture.cs | 12 +++++++++++- .../Components/BookEditor/InputEditorTestFixture.cs | 9 --------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs index a0ff55e9..a69ee163 100644 --- a/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs +++ b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs @@ -47,6 +47,8 @@ namespace COMET.Web.Common.Tests.Components.BookEditor using NUnit.Framework; + using RichardSzalay.MockHttp; + using TestContext = Bunit.TestContext; [TestFixture] @@ -61,6 +63,8 @@ public class EditotPopupTestFixture private bool onCancelCalled; private bool onAcceptCalled; private Mock sessionService; + private MockHttpMessageHandler mockHttpMessageHandler; + private HttpClient httpClient; [SetUp] public void Setup() @@ -69,7 +73,13 @@ public void Setup() this.context.ConfigureDevExpressBlazor(); this.sessionService = new Mock(); 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.book = new Book(); this.activeDomains = new List diff --git a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs index 1eddac4b..34d64547 100644 --- a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs +++ b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs @@ -57,8 +57,6 @@ public class InputEditorTestFixture private List activeDomains; private List availableCategories; private Mock sessionService; - private MockHttpMessageHandler mockHttpMessageHandler; - private HttpClient httpClient; private const string BookName = "Book Example"; private const string BookShortName = "bookExample"; @@ -69,13 +67,6 @@ public void Setup() this.context.ConfigureDevExpressBlazor(); this.sessionService = new Mock(); 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.activeDomains = new List { From 48436c725bc689e3a9b849590e0bb376e2ad1506 Mon Sep 17 00:00:00 2001 From: Robbware Date: Thu, 5 Oct 2023 11:29:22 +0100 Subject: [PATCH 3/3] Remove unecessary usings and update the default BookInputConfiguration.json --- .../Components/BookEditor/EditotPopupTestFixture.cs | 1 - .../Components/BookEditor/InputEditorTestFixture.cs | 4 ---- COMET.Web.Common/wwwroot/BookInputConfiguration.json | 6 +++--- COMETwebapp/COMETwebapp.csproj | 6 ------ COMETwebapp/wwwroot/BookInputConfiguration.json | 4 ---- 5 files changed, 3 insertions(+), 18 deletions(-) delete mode 100644 COMETwebapp/wwwroot/BookInputConfiguration.json diff --git a/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs index a69ee163..35d27aa0 100644 --- a/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs +++ b/COMET.Web.Common.Tests/Components/BookEditor/EditotPopupTestFixture.cs @@ -36,7 +36,6 @@ namespace COMET.Web.Common.Tests.Components.BookEditor using COMET.Web.Common.ViewModels.Components.BookEditor; using DevExpress.Blazor; - using DevExpress.Blazor.Popup.Internal; using DynamicData; diff --git a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs index 34d64547..8d13aa7d 100644 --- a/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs +++ b/COMET.Web.Common.Tests/Components/BookEditor/InputEditorTestFixture.cs @@ -24,8 +24,6 @@ namespace COMET.Web.Common.Tests.Components.BookEditor { - using System.Text.Json; - using Bunit; using CDP4Common.ReportingData; @@ -44,8 +42,6 @@ namespace COMET.Web.Common.Tests.Components.BookEditor using NUnit.Framework; - using RichardSzalay.MockHttp; - using TestContext = Bunit.TestContext; [TestFixture] diff --git a/COMET.Web.Common/wwwroot/BookInputConfiguration.json b/COMET.Web.Common/wwwroot/BookInputConfiguration.json index e4781159..cd465b2a 100644 --- a/COMET.Web.Common/wwwroot/BookInputConfiguration.json +++ b/COMET.Web.Common/wwwroot/BookInputConfiguration.json @@ -1,4 +1,4 @@ { - "ShowName": false, - "ShowShortName" : false -} + "ShowName": true, + "ShowShortName" : true +} \ No newline at end of file diff --git a/COMETwebapp/COMETwebapp.csproj b/COMETwebapp/COMETwebapp.csproj index a77fd4e2..5c09b3c2 100644 --- a/COMETwebapp/COMETwebapp.csproj +++ b/COMETwebapp/COMETwebapp.csproj @@ -30,10 +30,4 @@ - - - Always - - - diff --git a/COMETwebapp/wwwroot/BookInputConfiguration.json b/COMETwebapp/wwwroot/BookInputConfiguration.json deleted file mode 100644 index e4781159..00000000 --- a/COMETwebapp/wwwroot/BookInputConfiguration.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "ShowName": false, - "ShowShortName" : false -}