|
| 1 | +// -------------------------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="BulkParticipantCreationDialogViewModelTestFixture.cs" company="Starion Group S.A."> |
| 3 | +// Copyright (c) 2015-2026 Starion Group S.A. |
| 4 | +// |
| 5 | +// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Rowan de Voogt |
| 6 | +// |
| 7 | +// This file is part of CDP4-COMET IME Community Edition. |
| 8 | +// The CDP4-COMET IME Community Edition is the Starion Concurrent Design Desktop Application and Excel Integration |
| 9 | +// compliant with ECSS-E-TM-10-25 Annex A and Annex C. |
| 10 | +// |
| 11 | +// The CDP4-COMET IME Community Edition is free software; you can redistribute it and/or |
| 12 | +// modify it under the terms of the GNU Affero General Public |
| 13 | +// License as published by the Free Software Foundation; either |
| 14 | +// version 3 of the License, or any later version. |
| 15 | +// |
| 16 | +// The CDP4-COMET IME Community Edition is distributed in the hope that it will be useful, |
| 17 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +// GNU Affero General Public License for more details. |
| 20 | +// |
| 21 | +// You should have received a copy of the GNU Affero General Public License |
| 22 | +// along with this program. If not, see http://www.gnu.org/licenses/. |
| 23 | +// </copyright> |
| 24 | +// -------------------------------------------------------------------------------------------------------------------- |
| 25 | + |
| 26 | +namespace CDP4SiteDirectory.Tests.Dialogs |
| 27 | +{ |
| 28 | + using System; |
| 29 | + using System.Collections.Concurrent; |
| 30 | + using System.Linq; |
| 31 | + using System.Reactive.Concurrency; |
| 32 | + using System.Reactive.Linq; |
| 33 | + using System.Threading.Tasks; |
| 34 | + using System.Windows.Input; |
| 35 | + |
| 36 | + using CDP4Common.CommonData; |
| 37 | + using CDP4Common.SiteDirectoryData; |
| 38 | + using CDP4Common.Types; |
| 39 | + |
| 40 | + using CDP4Composition.Navigation; |
| 41 | + |
| 42 | + using CDP4SiteDirectory.ViewModels; |
| 43 | + |
| 44 | + using NUnit.Framework; |
| 45 | + |
| 46 | + using ReactiveUI; |
| 47 | + |
| 48 | + [TestFixture] |
| 49 | + internal class BulkParticipantCreationDialogViewModelTestFixture |
| 50 | + { |
| 51 | + private Uri uri; |
| 52 | + private ConcurrentDictionary<CacheKey, Lazy<Thing>> cache; |
| 53 | + private DomainOfExpertise thermal; |
| 54 | + private DomainOfExpertise systems; |
| 55 | + private ParticipantRole role; |
| 56 | + private Person john; |
| 57 | + private Person jane; |
| 58 | + private Person personWithoutDefaultDomain; |
| 59 | + |
| 60 | + [SetUp] |
| 61 | + public void Setup() |
| 62 | + { |
| 63 | + RxApp.MainThreadScheduler = Scheduler.CurrentThread; |
| 64 | + this.uri = new Uri("https://www.stariongroup.eu"); |
| 65 | + this.cache = new ConcurrentDictionary<CacheKey, Lazy<Thing>>(); |
| 66 | + |
| 67 | + this.thermal = new DomainOfExpertise(Guid.NewGuid(), this.cache, this.uri) { Name = "Thermal" }; |
| 68 | + this.systems = new DomainOfExpertise(Guid.NewGuid(), this.cache, this.uri) { Name = "Systems" }; |
| 69 | + |
| 70 | + this.role = new ParticipantRole(Guid.NewGuid(), this.cache, this.uri) { Name = "Team Member" }; |
| 71 | + |
| 72 | + this.john = new Person(Guid.NewGuid(), this.cache, this.uri) { GivenName = "John", Surname = "Doe", DefaultDomain = this.thermal }; |
| 73 | + this.jane = new Person(Guid.NewGuid(), this.cache, this.uri) { GivenName = "Jane", Surname = "Roe", DefaultDomain = this.systems }; |
| 74 | + this.personWithoutDefaultDomain = new Person(Guid.NewGuid(), this.cache, this.uri) { GivenName = "No", Surname = "Domain" }; |
| 75 | + } |
| 76 | + |
| 77 | + private BulkParticipantCreationDialogViewModel CreateDialogViewModel() |
| 78 | + { |
| 79 | + return new BulkParticipantCreationDialogViewModel( |
| 80 | + new[] { this.john, this.jane, this.personWithoutDefaultDomain }, |
| 81 | + new[] { this.role }, |
| 82 | + new[] { this.thermal, this.systems }); |
| 83 | + } |
| 84 | + |
| 85 | + private BulkParticipantRowViewModel RowFor(BulkParticipantCreationDialogViewModel viewModel, Person person) |
| 86 | + { |
| 87 | + return viewModel.Participants.Single(x => x.Person == person); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void VerifyThatArgumentNullExceptionsAreThrown() |
| 92 | + { |
| 93 | + Assert.Throws<ArgumentNullException>(() => new BulkParticipantCreationDialogViewModel(null, new[] { this.role }, new[] { this.thermal })); |
| 94 | + Assert.Throws<ArgumentNullException>(() => new BulkParticipantCreationDialogViewModel(new[] { this.john }, null, new[] { this.thermal })); |
| 95 | + Assert.Throws<ArgumentNullException>(() => new BulkParticipantCreationDialogViewModel(new[] { this.john }, new[] { this.role }, null)); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + public void VerifyThatRowsArePopulatedWithDefaults() |
| 100 | + { |
| 101 | + var viewModel = this.CreateDialogViewModel(); |
| 102 | + |
| 103 | + Assert.That(viewModel.Participants.Count, Is.EqualTo(3)); |
| 104 | + Assert.That(viewModel.Participants.All(x => x.IsSelected), Is.True); |
| 105 | + Assert.That(viewModel.IsActive, Is.True); |
| 106 | + |
| 107 | + Assert.That(this.RowFor(viewModel, this.john).SelectedDomain, Is.EqualTo(this.thermal)); |
| 108 | + Assert.That(this.RowFor(viewModel, this.jane).SelectedDomain, Is.EqualTo(this.systems)); |
| 109 | + Assert.That(this.RowFor(viewModel, this.personWithoutDefaultDomain).SelectedDomain, Is.Null); |
| 110 | + |
| 111 | + Assert.That(this.RowFor(viewModel, this.john).PossibleDomain, Is.EquivalentTo(new[] { this.thermal, this.systems })); |
| 112 | + } |
| 113 | + |
| 114 | + [Test] |
| 115 | + public void VerifyThatOkCanExecuteRequiresRoleAndDomainPerSelectedRow() |
| 116 | + { |
| 117 | + var viewModel = this.CreateDialogViewModel(); |
| 118 | + |
| 119 | + Assert.That(viewModel.OkCanExecute, Is.False); |
| 120 | + |
| 121 | + viewModel.SelectedRole = this.role; |
| 122 | + |
| 123 | + // the person without a default domain has no domain selected yet |
| 124 | + Assert.That(viewModel.OkCanExecute, Is.False); |
| 125 | + |
| 126 | + this.RowFor(viewModel, this.personWithoutDefaultDomain).SelectedDomain = this.thermal; |
| 127 | + |
| 128 | + Assert.That(viewModel.OkCanExecute, Is.True); |
| 129 | + |
| 130 | + foreach (var row in viewModel.Participants) |
| 131 | + { |
| 132 | + row.IsSelected = false; |
| 133 | + } |
| 134 | + |
| 135 | + Assert.That(viewModel.OkCanExecute, Is.False); |
| 136 | + } |
| 137 | + |
| 138 | + [Test] |
| 139 | + public void VerifyThatDeselectingPersonWithoutDomainAllowsOk() |
| 140 | + { |
| 141 | + var viewModel = this.CreateDialogViewModel(); |
| 142 | + |
| 143 | + viewModel.SelectedRole = this.role; |
| 144 | + |
| 145 | + // exclude the person without a default domain; the remaining persons resolve to their own default domain |
| 146 | + this.RowFor(viewModel, this.personWithoutDefaultDomain).IsSelected = false; |
| 147 | + |
| 148 | + Assert.That(viewModel.OkCanExecute, Is.True); |
| 149 | + } |
| 150 | + |
| 151 | + [Test] |
| 152 | + public async Task VerifyThatOkResultAppliesBatchValuesToSelectedRows() |
| 153 | + { |
| 154 | + var viewModel = this.CreateDialogViewModel(); |
| 155 | + |
| 156 | + viewModel.SelectedRole = this.role; |
| 157 | + viewModel.IsActive = false; |
| 158 | + this.RowFor(viewModel, this.personWithoutDefaultDomain).SelectedDomain = this.thermal; |
| 159 | + |
| 160 | + await viewModel.OkCommand.Execute(); |
| 161 | + |
| 162 | + var result = viewModel.DialogResult as BulkParticipantCreationResult; |
| 163 | + |
| 164 | + Assert.That(result, Is.Not.Null); |
| 165 | + Assert.That(result.Result, Is.True); |
| 166 | + Assert.That(result.Participants.Count(), Is.EqualTo(3)); |
| 167 | + Assert.That(result.Participants.All(x => x.SelectedRole == this.role), Is.True); |
| 168 | + Assert.That(result.Participants.All(x => !x.IsActive), Is.True); |
| 169 | + |
| 170 | + Assert.That(result.Participants.Single(x => x.Person == this.john).SelectedDomain, Is.EqualTo(this.thermal)); |
| 171 | + Assert.That(result.Participants.Single(x => x.Person == this.jane).SelectedDomain, Is.EqualTo(this.systems)); |
| 172 | + Assert.That(result.Participants.Single(x => x.Person == this.personWithoutDefaultDomain).SelectedDomain, Is.EqualTo(this.thermal)); |
| 173 | + } |
| 174 | + |
| 175 | + [Test] |
| 176 | + public async Task VerifyThatOkResultContainsOnlySelectedParticipants() |
| 177 | + { |
| 178 | + var viewModel = this.CreateDialogViewModel(); |
| 179 | + |
| 180 | + viewModel.SelectedRole = this.role; |
| 181 | + this.RowFor(viewModel, this.personWithoutDefaultDomain).IsSelected = false; |
| 182 | + |
| 183 | + await viewModel.OkCommand.Execute(); |
| 184 | + |
| 185 | + var result = viewModel.DialogResult as BulkParticipantCreationResult; |
| 186 | + |
| 187 | + Assert.That(result, Is.Not.Null); |
| 188 | + Assert.That(result.Participants.Count(), Is.EqualTo(2)); |
| 189 | + Assert.That(result.Participants.Any(x => x.Person == this.personWithoutDefaultDomain), Is.False); |
| 190 | + } |
| 191 | + |
| 192 | + [Test] |
| 193 | + public async Task VerifyThatCancelSetsNegativeResult() |
| 194 | + { |
| 195 | + var viewModel = this.CreateDialogViewModel(); |
| 196 | + |
| 197 | + await viewModel.CancelCommand.Execute(); |
| 198 | + |
| 199 | + Assert.That(viewModel.DialogResult, Is.Not.Null); |
| 200 | + Assert.That(viewModel.DialogResult.Result, Is.False); |
| 201 | + } |
| 202 | + |
| 203 | + [Test] |
| 204 | + public void VerifyThatOkCommandCanExecuteIsGatedByOkCanExecute() |
| 205 | + { |
| 206 | + var viewModel = this.CreateDialogViewModel(); |
| 207 | + |
| 208 | + Assert.That(((ICommand)viewModel.OkCommand).CanExecute(null), Is.False); |
| 209 | + |
| 210 | + viewModel.SelectedRole = this.role; |
| 211 | + this.RowFor(viewModel, this.personWithoutDefaultDomain).SelectedDomain = this.thermal; |
| 212 | + |
| 213 | + Assert.That(((ICommand)viewModel.OkCommand).CanExecute(null), Is.True); |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments