Skip to content

Commit

Permalink
[ADD] Save and reuse data in open tab component; fixes #724
Browse files Browse the repository at this point in the history
* [ADD] Save and reuse data while opening tabs
* Add unit test and fix bugs
* Fix unit tests
* Fix unit tests, missing ICacheService
* Changes according to SQ
* Add new unit tests
  • Loading branch information
lxatstariongroup authored Dec 9, 2024
1 parent a200985 commit 208d88b
Show file tree
Hide file tree
Showing 18 changed files with 451 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace COMET.Web.Common.Tests.Components.Applications
using COMET.Web.Common.Components.Applications;
using COMET.Web.Common.Components.Selectors;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Services.Cache;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.Services.StringTableService;
Expand Down Expand Up @@ -70,6 +71,7 @@ public void Setup()
this.viewModel = new Mock<ISingleEngineeringModelApplicationTemplateViewModel>();

this.openEngineeringModels = [];
var cacheService = new Mock<ICacheService>();
var sessionService = new Mock<ISessionService>();
sessionService.Setup(x => x.OpenEngineeringModels).Returns(this.openEngineeringModels);
sessionService.Setup(x => x.OpenIterations).Returns(new SourceList<Iteration>());
Expand All @@ -84,6 +86,7 @@ public void Setup()
this.context.Services.AddSingleton(mockConfigurationService.Object);
this.context.Services.AddSingleton(new Mock<IStringTableService>().Object);
this.context.Services.AddSingleton(sessionService.Object);
this.context.Services.AddSingleton(cacheService.Object);
this.context.ConfigureDevExpressBlazor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace COMET.Web.Common.Tests.Components.Applications
using COMET.Web.Common.Components.Selectors;
using COMET.Web.Common.Extensions;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Services.Cache;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.Services.StringTableService;
Expand All @@ -61,6 +62,7 @@ namespace COMET.Web.Common.Tests.Components.Applications
public class SingleIterationApplicationTemplateTestFixture
{
private Mock<ISingleIterationApplicationTemplateViewModel> viewModel;
private Mock<ICacheService> cacheService;
private SourceList<Iteration> openIterations;
private TestContext context;
private ICDPMessageBus messageBus;
Expand All @@ -72,6 +74,7 @@ public void Setup()
this.context = new TestContext();
this.openIterations = new SourceList<Iteration>();
this.viewModel = new Mock<ISingleIterationApplicationTemplateViewModel>();
this.cacheService = new Mock<ICacheService>();
var sessionService = new Mock<ISessionService>();
sessionService.Setup(x => x.OpenIterations).Returns(this.openIterations);
var session = new Mock<ISession>();
Expand All @@ -86,6 +89,7 @@ public void Setup()
this.context.Services.AddSingleton(mockConfigurationService.Object);
this.context.Services.AddSingleton(new Mock<IStringTableService>().Object);
this.context.Services.AddSingleton(sessionService.Object);
this.context.Services.AddSingleton(this.cacheService.Object);
this.context.Services.AddSingleton(this.messageBus);
this.context.ConfigureDevExpressBlazor();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace COMET.Web.Common.Tests.Components
using COMET.Web.Common.Extensions;
using COMET.Web.Common.Model;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Services.Cache;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.RegistrationService;
using COMET.Web.Common.Services.SessionManagement;
Expand All @@ -64,6 +65,7 @@ public class IndexComponentTestFixture
{
private IIndexViewModel viewModel;
private TestContext context;
private Mock<ICacheService> cacheService;
private Mock<IVersionService> versionService;
private Mock<ISessionService> sessionService;
private Mock<IConfigurationService> serverConnectionService;
Expand All @@ -80,6 +82,7 @@ public void Setup()
this.versionService = new Mock<IVersionService>();
this.sessionService = new Mock<ISessionService>();
this.serverConnectionService = new Mock<IConfigurationService>();
this.cacheService = new Mock<ICacheService>();
var serverConfiguration = new ServerConfiguration { FullTrustConfiguration = new FullTrustConfiguration() };
this.serverConnectionService.Setup(x => x.ServerConfiguration).Returns(serverConfiguration);
this.sourceList = new SourceList<Iteration>();
Expand All @@ -100,6 +103,7 @@ public void Setup()
this.context.Services.AddSingleton<ILoginViewModel, LoginViewModel>();
this.context.Services.AddSingleton<IOpenModelViewModel, OpenModelViewModel>();
this.context.Services.AddSingleton(this.registrationService.Object);
this.context.Services.AddSingleton(this.cacheService.Object);
this.context.ConfigureDevExpressBlazor();
this.authorization = this.context.AddTestAuthorization();

Expand Down
5 changes: 4 additions & 1 deletion COMET.Web.Common.Tests/Components/OpenModelTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace COMET.Web.Common.Tests.Components
using CDP4Common.SiteDirectoryData;

using COMET.Web.Common.Components;
using COMET.Web.Common.Services.Cache;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.Services.StringTableService;
Expand All @@ -57,14 +58,16 @@ public class OpenModelTestFixture
private TestContext context;
private Mock<ISessionService> sessionService;
private Mock<IConfigurationService> configurationService;
private Mock<ICacheService> cacheService;

[SetUp]
public void Setup()
{
this.context = new TestContext();
this.sessionService = new Mock<ISessionService>();
this.configurationService = new Mock<IConfigurationService>();
this.viewModel = new OpenModelViewModel(this.sessionService.Object, this.configurationService.Object);
this.cacheService = new Mock<ICacheService>();
this.viewModel = new OpenModelViewModel(this.sessionService.Object, this.configurationService.Object, this.cacheService.Object);
this.context.ConfigureDevExpressBlazor();
this.context.Services.AddSingleton<IOpenModelViewModel>(this.viewModel);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CacheServiceTestFixture.cs" company="Starion Group S.A.">
// Copyright (c) 2023-2024 Starion Group S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, Nabil Abbar
//
// This file is part of CDP4-COMET WEB Community Edition
// The CDP4-COMET WEB Community Edition is the Starion 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.CacheService
{
using CDP4Common.EngineeringModelData;

using COMET.Web.Common.Enumerations;
using COMET.Web.Common.Services.Cache;

using NUnit.Framework;

[TestFixture]
public class CacheServiceTestFixture
{
private CacheService cacheService;

[SetUp]
public void Setup()
{
this.cacheService = new CacheService();
}

[Test]
public void VerifyAddNewKey()
{
var engineeringModel = new EngineeringModel();
var browserSessionSettingKey = BrowserSessionSettingKey.LastUsedEngineeringModel;

Assert.That(() => this.cacheService.AddOrUpdateBrowserSessionSetting(browserSessionSettingKey, engineeringModel), Throws.Nothing);

Assert.That(this.cacheService.TryGetBrowserSessionSetting(browserSessionSettingKey, out var result), Is.True);

Assert.That(result, Is.EqualTo(engineeringModel));
}

[Test]
public void VerifyKeyDoesNotExistWorksAsExpected()
{
var browserSessionSettingKey = BrowserSessionSettingKey.LastUsedEngineeringModel;

Assert.That(this.cacheService.TryGetBrowserSessionSetting(browserSessionSettingKey, out var result), Is.False);

Assert.That(result, Is.Null);
}

[Test]
public void VerifyOverwriteExistingKey()
{
var engineeringModel1 = new EngineeringModel();
var browserSessionSettingKey = BrowserSessionSettingKey.LastUsedEngineeringModel;

Assert.That(() => this.cacheService.AddOrUpdateBrowserSessionSetting(browserSessionSettingKey, engineeringModel1), Throws.Nothing);

Assert.That(this.cacheService.TryGetBrowserSessionSetting(browserSessionSettingKey, out var result), Is.True);

Assert.That(result, Is.EqualTo(engineeringModel1));

var engineeringModel2 = new EngineeringModel();

Assert.That(() => this.cacheService.AddOrUpdateBrowserSessionSetting(browserSessionSettingKey, engineeringModel2), Throws.Nothing);

Assert.That(this.cacheService.TryGetBrowserSessionSetting(browserSessionSettingKey, out var result2), Is.True);

Assert.That(result2, Is.EqualTo(engineeringModel2));
}

[Test]
public void VerifyGetOrAddKey()
{
var engineeringModel1 = new EngineeringModel();
var browserSessionSettingKey = BrowserSessionSettingKey.LastUsedEngineeringModel;

Assert.That(this.cacheService.TryGetOrAddBrowserSessionSetting(browserSessionSettingKey, engineeringModel1, out var result), Is.True);

Assert.That(result, Is.EqualTo(engineeringModel1));

Assert.That(this.cacheService.TryGetBrowserSessionSetting(browserSessionSettingKey, out var result2), Is.True);

Assert.That(result2, Is.EqualTo(engineeringModel1));

var engineeringModel2 = new EngineeringModel();

Assert.That(this.cacheService.TryGetOrAddBrowserSessionSetting(browserSessionSettingKey, engineeringModel2, out var result3), Is.True);

Assert.That(result3, Is.EqualTo(engineeringModel1));

Assert.That(this.cacheService.TryGetBrowserSessionSetting(browserSessionSettingKey, out var result4), Is.True);

Assert.That(result4, Is.EqualTo(engineeringModel1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ namespace COMET.Web.Common.Tests.ViewModels.Components
using CDP4Common.EngineeringModelData;
using CDP4Common.SiteDirectoryData;

using COMET.Web.Common.Enumerations;
using COMET.Web.Common.Model;
using COMET.Web.Common.Model.Configuration;
using COMET.Web.Common.Services.Cache;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.SessionManagement;
using COMET.Web.Common.ViewModels.Components;
Expand All @@ -45,6 +48,7 @@ public class OpenModelViewModelTestFixture
private OpenModelViewModel viewModel;
private Mock<IConfigurationService> configurationService;
private Mock<ISessionService> sessionService;
private Mock<ICacheService> cacheService;
private const string RdlShortName = "filterRdl";
private List<EngineeringModelSetup> models;

Expand All @@ -53,12 +57,20 @@ public void Setup()
{
this.configurationService = new Mock<IConfigurationService>();
this.sessionService = new Mock<ISessionService>();
this.cacheService = new Mock<ICacheService>();
var iterations = new SourceList<Iteration>();
this.sessionService.Setup(x => x.OpenIterations).Returns(iterations);

this.viewModel = new OpenModelViewModel(this.sessionService.Object, this.configurationService.Object);
this.viewModel = new OpenModelViewModel(this.sessionService.Object, this.configurationService.Object, this.cacheService.Object);
this.models = CreateData().ToList();
this.sessionService.Setup(x => x.GetParticipantModels()).Returns(this.models);

object result;
this.cacheService.Setup(x => x.TryGetBrowserSessionSetting(BrowserSessionSettingKey.LastUsedDomainOfExpertise, out result)).Returns(false);

this.cacheService.Setup(x => x.TryGetBrowserSessionSetting(BrowserSessionSettingKey.LastUsedEngineeringModel, out result)).Returns(false);

this.cacheService.Setup(x => x.TryGetBrowserSessionSetting(BrowserSessionSettingKey.LastUsedIterationData, out result)).Returns(false);
}

[Test]
Expand Down Expand Up @@ -98,7 +110,32 @@ public void VerifyInitializeViewModel()
serverConfiguration.RdlFilter.Kinds = new[] { EngineeringModelKind.STUDY_MODEL };
serverConfiguration.RdlFilter.RdlShortNames = Enumerable.Empty<string>();
this.viewModel.InitializesProperties();
Assert.That(this.viewModel.AvailableEngineeringModelSetups.Count(), Is.EqualTo(4));
Assert.That(this.viewModel.AvailableEngineeringModelSetups.Count(), Is.EqualTo(4));
}

[Test]
public void VerifyInitializeViewModelWithBrowserSessionSettings()
{
//Initialize without server configuration
this.viewModel.InitializesProperties();
Assert.That(this.viewModel.AvailableEngineeringModelSetups.Count(), Is.EqualTo(5));

var preselectedEngineeringModel = this.viewModel.AvailableEngineeringModelSetups.First();

object domainOfExpertise = new DomainOfExpertise(Guid.NewGuid(), null, null);
object engineeringModelSetup = preselectedEngineeringModel;
object iterationData = new IterationData(preselectedEngineeringModel.IterationSetup.First());

this.cacheService.Setup(x => x.TryGetBrowserSessionSetting(BrowserSessionSettingKey.LastUsedDomainOfExpertise, out domainOfExpertise)).Returns(true);

this.cacheService.Setup(x => x.TryGetBrowserSessionSetting(BrowserSessionSettingKey.LastUsedEngineeringModel, out engineeringModelSetup)).Returns(true);

this.cacheService.Setup(x => x.TryGetBrowserSessionSetting(BrowserSessionSettingKey.LastUsedIterationData, out iterationData)).Returns(true);

this.viewModel.InitializesProperties();
Assert.That(this.viewModel.SelectedEngineeringModel, Is.EqualTo(engineeringModelSetup));
Assert.That(this.viewModel.SelectedIterationSetup, Is.EqualTo(iterationData));
Assert.That(this.viewModel.SelectedDomainOfExpertise, Is.EqualTo(domainOfExpertise));
}

private static List<EngineeringModelSetup> CreateData()
Expand Down
50 changes: 50 additions & 0 deletions COMET.Web.Common/Enumerations/BrowserSessionSettingKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BrowserSessionSettings.cs" company="Starion Group S.A.">
// Copyright (c) 2024 Starion Group S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, João Rua
//
// This file is part of COMET WEB Community Edition
// The COMET WEB Community Edition is the Starion Group 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.Enumerations
{
using CDP4Common.EngineeringModelData;
using CDP4Common.SiteDirectoryData;

/// <summary>
/// En enumeration of possible keys to be used to store and retrieve cached BrowserSessionSettings
/// </summary>
public enum BrowserSessionSettingKey
{
/// <summary>
/// Key to handle the last selected <see cref="EngineeringModel"/>
/// </summary>
LastUsedEngineeringModel,

/// <summary>
/// Key to handle the last selected <see cref="IterationSetup"/>
/// </summary>
LastUsedIterationData,

/// <summary>
/// Key to handle the last selected <see cref="DomainOfExpertise"/>
/// </summary>
LastUsedDomainOfExpertise
}
}
2 changes: 2 additions & 0 deletions COMET.Web.Common/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace COMET.Web.Common.Extensions
using COMET.Web.Common.Model;
using COMET.Web.Common.Server.Services.ConfigurationService;
using COMET.Web.Common.Server.Services.StringTableService;
using COMET.Web.Common.Services.Cache;
using COMET.Web.Common.Services.ConfigurationService;
using COMET.Web.Common.Services.NotificationService;
using COMET.Web.Common.Services.RegistrationService;
Expand Down Expand Up @@ -87,6 +88,7 @@ public static void RegisterCdp4CometCommonServices(this IServiceCollection servi
serviceProvider.AddScoped<INotificationService, NotificationService>();
serviceProvider.AddScoped<ICDPMessageBus, CDPMessageBus>();
serviceProvider.AddSingleton<IValidationService, ValidationService>();
serviceProvider.AddScoped<ICacheService, CacheService>();
serviceProvider.AddAuthorizationCore();
serviceProvider.AddDevExpressBlazor(configure => configure.SizeMode = SizeMode.Medium);
serviceProvider.RegisterCommonViewModels();
Expand Down
Loading

0 comments on commit 208d88b

Please sign in to comment.