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

Fix the BookInputConfiguration failing upon validations being run. #466

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,6 +46,8 @@ namespace COMET.Web.Common.Tests.Components.BookEditor

using NUnit.Framework;

using RichardSzalay.MockHttp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this dependency comming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used for MockHttpMessageHandler (also on other fixtures in the project). Either way, one of the tests I modified had this using but it wasn't in use so I've removed it.


using TestContext = Bunit.TestContext;

[TestFixture]
Expand All @@ -61,6 +62,8 @@ public class EditotPopupTestFixture
private bool onCancelCalled;
private bool onAcceptCalled;
private Mock<ISessionService> sessionService;
private MockHttpMessageHandler mockHttpMessageHandler;
private HttpClient httpClient;

[SetUp]
public void Setup()
Expand All @@ -69,7 +72,13 @@ public void Setup()
this.context.ConfigureDevExpressBlazor();
this.sessionService = new Mock<ISessionService>();
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<DomainOfExpertise>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

namespace COMET.Web.Common.Tests.Components.BookEditor
{
using System.Text.Json;

using Bunit;

using CDP4Common.ReportingData;
Expand All @@ -44,8 +42,6 @@ namespace COMET.Web.Common.Tests.Components.BookEditor

using NUnit.Framework;

using RichardSzalay.MockHttp;

using TestContext = Bunit.TestContext;

[TestFixture]
Expand All @@ -57,8 +53,6 @@ public class InputEditorTestFixture
private List<DomainOfExpertise> activeDomains;
private List<Category> availableCategories;
private Mock<ISessionService> sessionService;
private MockHttpMessageHandler mockHttpMessageHandler;
private HttpClient httpClient;
private const string BookName = "Book Example";
private const string BookShortName = "bookExample";

Expand All @@ -69,13 +63,6 @@ public void Setup()
this.context.ConfigureDevExpressBlazor();
this.sessionService = new Mock<ISessionService>();
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<DomainOfExpertise>
{
Expand All @@ -100,6 +87,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);
});
}

Expand All @@ -119,6 +108,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();
Expand Down
2 changes: 1 addition & 1 deletion COMET.Web.Common/Components/BookEditor/EditorPopup.razor
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<DxPopup @bind-Visible="@this.ViewModel.IsVisible" CloseOnOutsideClick="false" ShowCloseButton="true" ShowFooter="true" HeaderText="@this.ViewModel.HeaderText" Closed="@(()=> this.ViewModel.OnCancelClick.InvokeAsync())">
<Content>
<InputEditor Item="@this.ViewModel.Item" ActiveDomains="@this.ViewModel.ActiveDomains" AvailableCategories="@this.ViewModel.AvailableCategories" />
<InputEditor Item="@this.ViewModel.Item" ActiveDomains="@this.ViewModel.ActiveDomains" AvailableCategories="@this.ViewModel.AvailableCategories" ShowName="@this.showName" ShowShortName="@this.showShortName" />
<div style="display:flex; flex-direction:column">
@foreach (var validationError in this.ViewModel.ValidationErrors.Items)
{
Expand Down
85 changes: 80 additions & 5 deletions COMET.Web.Common/Components/BookEditor/EditorPopup.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,6 +53,41 @@ public partial class EditorPopup
[Parameter]
public IEditorPopupViewModel ViewModel { get; set; }

/// <summary>
/// Gets or sets the <see cref="HttpClient"/>
/// </summary>
[Inject]
public HttpClient HttpClient { get; set; }

/// <summary>
/// Gets or sets the <see cref="IOptions{TOptions}"/>
/// </summary>
[Inject]
public IOptions<GlobalOptions> Options { get; set; }

[Inject]
public ILogger<EditorPopup> Logger { get; set; }

/// <summary>
/// Sets if the component should show the name field
/// </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 @@ -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.");
}
}

/// <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;
}

/// <summary>
Expand All @@ -68,13 +143,13 @@ private async Task OnConfirmClick()
{
var validationErrors = new List<string>();

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);
Expand Down
4 changes: 2 additions & 2 deletions COMET.Web.Common/Components/BookEditor/InputEditor.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
@typeparam TItem

<div class="input-content">
@if (this.Item is INamedThing namedThing && this.showName)
@if (this.Item is INamedThing namedThing && this.ShowName)
{
<div class="w-100 editor-row">
<p>Name:</p>
<DxTextBox CssClass="w-100" @bind-Text="@namedThing.Name" />
</div>
}

@if (this.Item is IShortNamedThing shortNamedThing && this.showShortName)
@if (this.Item is IShortNamedThing shortNamedThing && this.ShowShortName)
{
<div class="w-100 editor-row">
<p>ShortName:</p>
Expand Down
73 changes: 19 additions & 54 deletions COMET.Web.Common/Components/BookEditor/InputEditor.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace COMET.Web.Common.Components.BookEditor
{
using System.Text.Json;

using CDP4Common.CommonData;
using CDP4Common.EngineeringModelData;
using CDP4Common.SiteDirectoryData;

Expand All @@ -49,18 +50,6 @@ public partial class InputEditor<TItem>
[Inject]
public ILogger<InputEditor<TItem>> Logger { get; set; }

/// <summary>
/// Gets or sets the <see cref="HttpClient"/>
/// </summary>
[Inject]
public HttpClient HttpClient { get; set; }

/// <summary>
/// Gets or sets the <see cref="IOptions{GlobalOptions}"/>
/// </summary>
[Inject]
public IOptions<GlobalOptions> Options { get; set; }

/// <summary>
/// Gets or sets the <see cref="ISessionService"/>
/// </summary>
Expand All @@ -84,27 +73,19 @@ public partial class InputEditor<TItem>
/// </summary>
[Parameter]
public IEnumerable<Category> AvailableCategories { get; set; }

/// <summary>
/// Sets if the component should show the name field
/// </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
/// Gets or sets if the InputEditor should display the Name field
/// </summary>
private bool showShortName;

[Parameter]
public bool ShowName { get; set; }

/// <summary>
/// The name of the ShowShortName property on the configuration file
/// Gets or sets if the InputEditor should display the ShortName field
/// </summary>
private const string showShortNameConfigurationProperty = "ShowShortName";

[Parameter]
public bool ShowShortName { get; set; }

/// <summary>
/// Method invoked when the component is ready to start, having received its
/// initial parameters from its parent in the render tree.
Expand All @@ -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.");
}
}

/// <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;
}

/// <summary>
/// Handler for when the selected categories changed
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion COMET.Web.Common/wwwroot/BookInputConfiguration.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"ShowName": true,
"ShowShortName" : true
}
}
Loading