Skip to content

Commit fb0cb1c

Browse files
[ADD] Support for creating ParameterOverrides; fixes #20 (#23)
* [ADD] Support for creating ParameterOverrides * [FIX] wrong ownership for ParameterOverride * Remove comment
1 parent 06d89f9 commit fb0cb1c

File tree

13 files changed

+290
-18
lines changed

13 files changed

+290
-18
lines changed

CDPBatchEditor.Tests/CDPBatchEditor.Tests.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
16-
<PackageReference Include="Moq" Version="4.20.70" />
17-
<PackageReference Include="NUnit" Version="4.1.0" />
18-
<PackageReference Include="NUnit.Console" Version="3.17.0" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
16+
<PackageReference Include="Moq" Version="4.20.72" />
17+
<PackageReference Include="NUnit" Version="4.3.2" />
18+
<PackageReference Include="NUnit.Console" Version="3.19.2" />
1919
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
2020
<PrivateAssets>all</PrivateAssets>
2121
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2222
</PackageReference>
23-
<PackageReference Include="coverlet.collector" Version="6.0.2">
23+
<PackageReference Include="coverlet.collector" Version="6.0.4">
2424
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2525
<PrivateAssets>all</PrivateAssets>
2626
</PackageReference>
27-
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
27+
<PackageReference Include="coverlet.msbuild" Version="6.0.4">
2828
<PrivateAssets>all</PrivateAssets>
2929
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3030
</PackageReference>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="OverrideCommandTestFixture.cs" company="Starion Group S.A.">
3+
// Copyright (c) 2015-2024 Starion Group S.A.
4+
//
5+
// Author: Nathanael Smiechowski, Alex Vorobiev, Alexander van Delft, Sam Gerené
6+
//
7+
// This file is part of CDP4-COMET Batch Editor.
8+
// The CDP4-COMET Batch Editor is a commandline application to perform batch operations on a
9+
// ECSS-E-TM-10-25 Annex A and Annex C data source
10+
//
11+
// The CDP4-COMET Batch Editor is free software; you can redistribute it and/or
12+
// modify it under the terms of the GNU Lesser 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 Batch Editor 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 Lesser General License version 3 for more details.
20+
//
21+
// You should have received a copy of the GNU Lesser General License
22+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
// </copyright>
24+
// --------------------------------------------------------------------------------------------------------------------
25+
26+
namespace CDPBatchEditor.Tests.Commands.Command
27+
{
28+
using System.Linq;
29+
30+
using CDP4Common.EngineeringModelData;
31+
32+
using CDPBatchEditor.CommandArguments;
33+
using CDPBatchEditor.Commands.Command;
34+
35+
using NUnit.Framework;
36+
37+
[TestFixture]
38+
public class OverrideCommandTestFixture : BaseCommandTestFixture
39+
{
40+
private OverrideCommand overrideCommand;
41+
42+
internal override void BuildAction(string action)
43+
{
44+
base.BuildAction(action);
45+
this.overrideCommand = new OverrideCommand(this.CommandArguments, this.SessionService.Object, this.FilterService.Object);
46+
}
47+
48+
[Test]
49+
public void VerifyOverrideParameters()
50+
{
51+
const string parameterUserFriendlyShortName = "testParameter2", elementDefinitionShortName = "testElementDefinition";
52+
this.BuildAction($"--action={CommandEnumeration.Override} -m TEST --parameters={parameterUserFriendlyShortName} --element-definition={elementDefinitionShortName} --included-owners={this.Domain.ShortName}");
53+
54+
Assert.That(this.Parameter2.ParameterSubscription.Any(), Is.False);
55+
56+
this.overrideCommand.Override();
57+
58+
Assert.That(this.Transactions.First().UpdatedThing.Count, Is.EqualTo(1));
59+
Assert.That(this.Transactions.First().AddedThing.Count(), Is.EqualTo(1));
60+
61+
Assert.That(this.Transactions.Any(t => t.AddedThing
62+
.Any(p =>
63+
p is ParameterOverride s
64+
&& s.Owner == this.Domain
65+
&& s.ParameterType.ShortName == parameterUserFriendlyShortName)),
66+
Is.True);
67+
68+
Assert.That(this.Transactions.Any(t => t.UpdatedThing
69+
.Any(u =>
70+
u.Value is ElementUsage p
71+
&& p.ParameterOverride.Any(s => s.Owner.ShortName == this.Domain.ShortName)
72+
)),
73+
Is.True);
74+
}
75+
}
76+
}

CDPBatchEditor.Tests/Commands/CommandDispatcherTestFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void Setup()
4747
this.commandArguments = new Mock<ICommandArguments>();
4848
this.parameterCommand = new Mock<IParameterCommand>();
4949
this.subscriptionCommand = new Mock<ISubscriptionCommand>();
50+
this.overrideCommand = new Mock<IOverrideCommand>();
5051
this.optionCommand = new Mock<IOptionCommand>();
5152
this.scaleCommand = new Mock<IScaleCommand>();
5253
this.stateCommand = new Mock<IStateCommand>();
@@ -57,7 +58,7 @@ public void Setup()
5758
this.commandArguments.Setup(x => x.Report).Returns(true);
5859

5960
this.commandDispatcher = new CommandDispatcher(
60-
this.commandArguments.Object, this.parameterCommand.Object, this.subscriptionCommand.Object, this.optionCommand.Object,
61+
this.commandArguments.Object, this.parameterCommand.Object, this.subscriptionCommand.Object, this.overrideCommand.Object, this.optionCommand.Object,
6162
this.scaleCommand.Object, this.stateCommand.Object, this.domainCommand.Object, this.valueSetCommand.Object, this.reportGenerator.Object);
6263
}
6364

@@ -70,6 +71,7 @@ public void Setup()
7071
private Mock<IDomainCommand> domainCommand;
7172
private Mock<IValueSetCommand> valueSetCommand;
7273
private Mock<IReportGenerator> reportGenerator;
74+
private Mock<IOverrideCommand> overrideCommand;
7375
private CommandDispatcher commandDispatcher;
7476

7577
[Test]
@@ -100,6 +102,7 @@ public void VerifyInvoke()
100102
this.optionCommand.Verify(x => x.ApplyOrRemoveOptionDependency(It.IsAny<bool>()), Times.Exactly(2));
101103
this.subscriptionCommand.Verify(x => x.Subscribe(), Times.Once);
102104
this.subscriptionCommand.Verify(x => x.SetParameterSubscriptionsSwitch(), Times.Once);
105+
this.overrideCommand.Verify(x => x.Override(), Times.Once);
103106
this.parameterCommand.Verify(x => x.Add(), Times.Once);
104107
this.parameterCommand.Verify(x => x.Remove(), Times.Once);
105108
this.scaleCommand.Verify(x => x.AssignMeasurementScale(), Times.Once);

CDPBatchEditor/AppContainer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static void BuildContainer(ICommandArguments commandArguments)
6666
containerBuilder.RegisterType<ParameterCommand>().As<IParameterCommand>();
6767
containerBuilder.RegisterType<StateCommand>().As<IStateCommand>();
6868
containerBuilder.RegisterType<SubscriptionCommand>().As<ISubscriptionCommand>();
69+
containerBuilder.RegisterType<OverrideCommand>().As<IOverrideCommand>();
6970
containerBuilder.RegisterType<ValueSetCommand>().As<IValueSetCommand>();
7071
containerBuilder.RegisterType<ScaleCommand>().As<IScaleCommand>();
7172
containerBuilder.RegisterType<OptionCommand>().As<IOptionCommand>();

CDPBatchEditor/CDPBatchEditor.csproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@
4646
</EmbeddedResource>
4747
</ItemGroup>
4848
<ItemGroup>
49-
<PackageReference Include="CDP4Common-CE" Version="26.6.2" />
50-
<PackageReference Include="CDP4Dal-CE" Version="26.6.2" />
51-
<PackageReference Include="CDP4JsonFileDal-CE" Version="26.6.2" />
52-
<PackageReference Include="CDP4JsonSerializer-CE" Version="26.6.2" />
53-
<PackageReference Include="CDP4RequirementsVerification-CE" Version="26.6.2" />
54-
<PackageReference Include="CDP4Rules-CE" Version="26.6.2" />
55-
<PackageReference Include="CDP4ServicesDal-CE" Version="26.6.2" />
56-
<PackageReference Include="CDP4WspDal-CE" Version="26.6.2" />
49+
<PackageReference Include="CDP4Common-CE" Version="28.1.0" />
50+
<PackageReference Include="CDP4Dal-CE" Version="28.1.0" />
51+
<PackageReference Include="CDP4JsonFileDal-CE" Version="28.1.0" />
52+
<PackageReference Include="CDP4JsonSerializer-CE" Version="28.1.0" />
53+
<PackageReference Include="CDP4RequirementsVerification-CE" Version="28.1.0" />
54+
<PackageReference Include="CDP4Rules-CE" Version="28.1.0" />
55+
<PackageReference Include="CDP4ServicesDal-CE" Version="28.1.0" />
56+
<PackageReference Include="CDP4WspDal-CE" Version="27.4.2" />
5757
<PackageReference Include="CommandLineParser" Version="2.9.1" />
58-
<PackageReference Include="Autofac" Version="7.0.1" />
58+
<PackageReference Include="Autofac" Version="8.2.0" />
5959
</ItemGroup>
6060
</Project>

CDPBatchEditor/CommandArguments/Arguments.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class Arguments : ConnectionArguments, ICommandArguments
6868
+ "ApplyStateDependence,"
6969
+ "ChangeParameterOwnership,"
7070
+ "ChangeDomain,"
71+
+ "Override,"
7172
+ "RemoveOptionDependence,"
7273
+ "RemoveStateDependence,"
7374
+ "SetGenericOwners,"
@@ -86,7 +87,7 @@ public class Arguments : ConnectionArguments, ICommandArguments
8687
Required = false,
8788
HelpText = "Comma-separated list of short names of parameters. "
8889
+ "Use in conjunction with --action=add-parameters | remove-parameters | change-parameter-ownership | subscribe "
89-
+ "| apply-state-dependence | remove-state-dependence | apply-option-dependence | remove-option-dependence.")]
90+
+ "| override | apply-state-dependence | remove-state-dependence | apply-option-dependence | remove-option-dependence.")]
9091
public IReadOnlyList<string> SelectedParameters { get; set; }
9192

9293
/// <summary>

CDPBatchEditor/CommandArguments/CommandEnumeration.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public enum CommandEnumeration
7070
/// </summary>
7171
ChangeDomain,
7272

73+
/// <summary>
74+
/// Override parameters #
75+
/// </summary>
76+
Override,
77+
7378
/// <summary>
7479
/// Remove option dependency #
7580
/// </summary>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="IOverrideCommand.cs" company="Starion Group S.A.">
3+
// Copyright (c) 2015-2024 Starion Group S.A.
4+
//
5+
// Author: Nathanael Smiechowski, Alex Vorobiev, Alexander van Delft, Sam Gerené
6+
//
7+
// This file is part of CDP4-COMET Batch Editor.
8+
// The CDP4-COMET Batch Editor is a commandline application to perform batch operations on a
9+
// ECSS-E-TM-10-25 Annex A and Annex C data source
10+
//
11+
// The CDP4-COMET Batch Editor is free software; you can redistribute it and/or
12+
// modify it under the terms of the GNU Lesser 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 Batch Editor 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 Lesser General License version 3 for more details.
20+
//
21+
// You should have received a copy of the GNU Lesser General License
22+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
// </copyright>
24+
// --------------------------------------------------------------------------------------------------------------------
25+
26+
namespace CDPBatchEditor.Commands.Command.Interface
27+
{
28+
/// <summary>
29+
/// Defines an <see cref="IOverrideCommand" /> that provides actions that are
30+
/// <see cref="CDP4Common.EngineeringModelData.ParameterOverride" /> related
31+
/// </summary>
32+
public interface IOverrideCommand
33+
{
34+
/// <summary>
35+
/// Override parameters with given short names.
36+
/// </summary>
37+
void Override();
38+
}
39+
}

CDPBatchEditor/Commands/Command/Interface/ISubscriptionCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
namespace CDPBatchEditor.Commands.Command.Interface
2727
{
28+
using CDP4Common.EngineeringModelData;
29+
2830
/// <summary>
2931
/// Defines an <see cref="IOptionCommand" /> that provides actions that are
3032
/// <see cref="CDP4Common.EngineeringModelData.Option" /> related
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="OverrideCommand.cs" company="Starion Group S.A.">
3+
// Copyright (c) 2015-2024 Starion Group S.A.
4+
//
5+
// Author: Nathanael Smiechowski, Alex Vorobiev, Alexander van Delft, Sam Gerené
6+
//
7+
// This file is part of CDP4-COMET Batch Editor.
8+
// The CDP4-COMET Batch Editor is a commandline application to perform batch operations on a
9+
// ECSS-E-TM-10-25 Annex A and Annex C data source
10+
//
11+
// The CDP4-COMET Batch Editor is free software; you can redistribute it and/or
12+
// modify it under the terms of the GNU Lesser 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 Batch Editor 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 Lesser General License version 3 for more details.
20+
//
21+
// You should have received a copy of the GNU Lesser General License
22+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
// </copyright>
24+
// --------------------------------------------------------------------------------------------------------------------
25+
26+
namespace CDPBatchEditor.Commands.Command
27+
{
28+
using System;
29+
using System.Linq;
30+
31+
using CDP4Common.EngineeringModelData;
32+
33+
using CDP4Dal.Operations;
34+
35+
using CDPBatchEditor.CommandArguments.Interface;
36+
using CDPBatchEditor.Commands.Command.Interface;
37+
using CDPBatchEditor.Services.Interfaces;
38+
39+
/// <summary>
40+
/// Defines an <see cref="OverrideCommand" /> that provides actions that are
41+
/// <see cref="CDP4Common.EngineeringModelData.ParameterOverride" /> related
42+
/// </summary>
43+
public class OverrideCommand : IOverrideCommand
44+
{
45+
/// <summary>
46+
/// Gets the injected <see cref="ICommandArguments" /> instance
47+
/// </summary>
48+
private readonly ICommandArguments commandArguments;
49+
50+
/// <summary>
51+
/// Gets the injected <see cref="IFilterService" /> instance
52+
/// </summary>
53+
private readonly IFilterService filterService;
54+
55+
/// <summary>
56+
/// Gets the injected <see cref="ISessionService" /> instance
57+
/// </summary>
58+
private readonly ISessionService sessionService;
59+
60+
/// <summary>
61+
/// Initialise a new <see cref="OverrideCommand" />
62+
/// </summary>
63+
/// <param name="commandArguments">the <see cref="ICommandArguments" /> arguments instance</param>
64+
/// <param name="sessionService">
65+
/// the <see cref="ISessionService" /> providing the <see cref="CDP4Dal.ISession" /> for the
66+
/// application
67+
/// </param>
68+
/// <param name="filterService">the <see cref="IFilterService" /></param>
69+
public OverrideCommand(ICommandArguments commandArguments, ISessionService sessionService, IFilterService filterService)
70+
{
71+
this.commandArguments = commandArguments;
72+
this.sessionService = sessionService;
73+
this.filterService = filterService;
74+
}
75+
76+
/// <summary>
77+
/// Override parameters with given short names, possibly filtered on Ownership (Included Owner) or Category.
78+
/// </summary>
79+
public void Override()
80+
{
81+
if (!this.commandArguments.SelectedParameters.Any())
82+
{
83+
Console.WriteLine("No --parameters given. Override parameters skipped.");
84+
return;
85+
}
86+
87+
var allElementUsages = this.sessionService.Iteration.Element.SelectMany(x => x.ContainedElement).ToArray();
88+
89+
foreach (var elementDefinition in this.sessionService.Iteration.Element
90+
.Where(e => this.filterService.IsFilteredIn(e))
91+
.OrderBy(x => x.ShortName).ToArray())
92+
{
93+
foreach (var elementDefinitionParameter in elementDefinition.Parameter)
94+
{
95+
var overrider = elementDefinitionParameter.Owner;
96+
97+
if (this.commandArguments.SelectedParameters.Contains(elementDefinitionParameter.ParameterType.ShortName))
98+
{
99+
var specificElementUsages = allElementUsages.Where(x => x.ElementDefinition == elementDefinition).ToArray();
100+
101+
foreach (var elementUsage in specificElementUsages.OrderBy(x => x.ShortName))
102+
{
103+
if (elementUsage.ParameterOverride.All(x => x.Parameter != elementDefinitionParameter))
104+
{
105+
var elementUsageClone = elementUsage.Clone(true);
106+
107+
this.sessionService.Transactions.Add(new ThingTransaction(TransactionContextResolver.ResolveContext(elementUsageClone), elementUsageClone));
108+
109+
var parameterOverride = new ParameterOverride(Guid.NewGuid(), this.sessionService.Cache, this.commandArguments.ServerUri)
110+
{
111+
Owner = overrider,
112+
Parameter = elementDefinitionParameter
113+
};
114+
115+
this.sessionService.Transactions.Last().Create(parameterOverride, elementUsageClone);
116+
117+
Console.WriteLine($"Parameter Override created on {parameterOverride.UserFriendlyShortName}");
118+
}
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)