Skip to content

Commit

Permalink
Fix #1: Generic CSV import supported
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineatstariongroup committed Mar 14, 2024
1 parent 0b73a9e commit 0d9f4af
Show file tree
Hide file tree
Showing 14 changed files with 1,186 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,5 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

switcher.json
159 changes: 159 additions & 0 deletions DEH-CSV.Tests/CsvReaderTestFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// -------------------------------------------------------------------------------------------------
// <copyright file="CsvReaderTestFixture.cs" company="RHEA System S.A.">
//
// Copyright 2023 RHEA System S.A.
//
// 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 RHEAGROUP.DEHCSV.Tests
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

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

using CDP4Dal;
using CDP4Dal.DAL;

using CDP4ServicesDal;

using Microsoft.Extensions.Logging;

using NUnit.Framework;

using RHEAGROUP.DEHCSV.Mapping;

using File = System.IO.File;

[TestFixture]
public class CsvReaderTestFixture
{
private readonly Uri uri = new ("https://cdp4services-public.cdp4.org");
private Credentials credentials;
private CdpServicesDal dal;
private CDPMessageBus messageBus;
private Session session;
private CsvReader csvReader;
private JsonSerializerOptions options;

[SetUp]
public void Setup()
{
this.credentials = new Credentials("admin", "pass", this.uri);
this.dal = new CdpServicesDal();
this.messageBus = new CDPMessageBus();
this.session = new Session(this.dal, this.credentials, this.messageBus);
var loggerFactory = LoggerFactory.Create(x => x.AddConsole());

this.csvReader = new CsvReader(loggerFactory.CreateLogger<CsvReader>());

this.options = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Converters = { new JsonStringEnumConverter<ClassKind>() }
};
}

[TearDown]
public void Teardown()
{
this.messageBus.Dispose();
}

[Test]
public async Task VerifyCsvReaderImplementation()
{
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

var csvPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Data", "import-data.csv");
var mappingFunctionPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Data", "import-mapping.json");

var typeMaps = JsonSerializer.Deserialize<IEnumerable<TypeMap>>(await File.ReadAllTextAsync(mappingFunctionPath), this.options);

var csvStream = File.OpenRead(csvPath);
await this.session.Open();
var loftModel = this.session.RetrieveSiteDirectory().Model.Find(x => x.Name == "LOFT")!;
var iterationSetup = loftModel.IterationSetup.Single(x => x.FrozenOn == null);

var iteration = new Iteration()
{
Iid = iterationSetup.IterationIid,
IterationSetup = iterationSetup
};

var engineeringModel = new EngineeringModel()
{
Iid = loftModel.EngineeringModelIid,
EngineeringModelSetup = loftModel
};

engineeringModel.Iteration.Add(iteration);

var domain = loftModel.Participant.Single(x => x.Person == this.session.ActivePerson).SelectedDomain;
await this.session.Read(iteration, domain);
var mappedThings = (await this.csvReader.Read(csvStream, typeMaps.ToList(), this.session)).ToList();

Assert.Multiple(() =>
{
Assert.That(mappedThings, Is.Not.Empty);
Assert.That(mappedThings, Has.Count.EqualTo(315));
Assert.That(mappedThings.OfType<ElementDefinition>().ToImmutableList(), Has.Count.EqualTo(35));
Assert.That(mappedThings.OfType<Parameter>().ToImmutableList(), Has.Count.EqualTo(140));
Assert.That(mappedThings.OfType<ParameterValueSet>().ToImmutableList(), Has.Count.EqualTo(140));
});

var count = 0;

foreach (var elementDefinition in mappedThings.OfType<ElementDefinition>())
{
Assert.Multiple(() =>
{
Assert.That(elementDefinition.ShortName, Is.EqualTo($"shortName{count:0000}"));
Assert.That(elementDefinition.Name, Is.EqualTo($"Name {count:0000}"));
Assert.That(elementDefinition.Category[0].Name, Is.EqualTo("Subsystem"));
Assert.That(elementDefinition.Owner.Name, Is.EqualTo("System Engineering"));
Assert.That(elementDefinition.Parameter, Has.Count.EqualTo(4));
Assert.That(elementDefinition.Parameter[0].ParameterType.Name, Is.EqualTo("area"));
Assert.That(elementDefinition.Parameter[1].ParameterType.Name, Is.EqualTo("mass"));
Assert.That(elementDefinition.Parameter[2].ParameterType.Name, Is.EqualTo("dry mass"));
Assert.That(elementDefinition.Parameter[3].ParameterType.Name, Is.EqualTo("radius"));
Assert.That(elementDefinition.Parameter.Select(x => x.Owner).Distinct(), Is.EquivalentTo(new List<DomainOfExpertise>{elementDefinition.Owner}));
});

foreach (var parameter in elementDefinition.Parameter)
{
Assert.That(parameter.ValueSet, Has.Count.EqualTo(1));
}

Assert.That(int.Parse(elementDefinition.Parameter[0].ValueSet[0].Manual[0]), Is.EqualTo(count));
Assert.That(int.Parse(elementDefinition.Parameter[1].ValueSet[0].Manual[0]), Is.EqualTo(-count));
Assert.That(int.Parse(elementDefinition.Parameter[2].ValueSet[0].Manual[0]), Is.EqualTo(count+10));
Assert.That(int.Parse(elementDefinition.Parameter[3].ValueSet[0].Manual[0]), Is.EqualTo(count+100));

count++;
}
}
}
}
15 changes: 11 additions & 4 deletions DEH-CSV.Tests/CsvWriterTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class CsvWriterTestFixture
private Uri uri;

private string mappingPath;
private CDPMessageBus messageBus;

[SetUp]
public void SetUp()
Expand All @@ -66,10 +67,16 @@ public void SetUp()
this.mappingProvider = new MappingProvider(this.loggerFactory);

this.iterationReader = new IterationReader(this.loggerFactory);

this.messageBus = new CDPMessageBus();
this.csvWriter = new CsvWriter(this.loggerFactory);
}

[TearDown]
public void Teardown()
{
this.messageBus.Dispose();
}

[Test]
public async Task Verify_that_demosat_model_can_be_written_to_CSV_file()
{
Expand All @@ -80,8 +87,8 @@ public async Task Verify_that_demosat_model_can_be_written_to_CSV_file()
this.uri = new Uri(path);

var credentials = new Credentials("admin", "pass", uri);

var session = new Session(jsonFileDal, credentials);
var session = new Session(jsonFileDal, credentials, this.messageBus);

await session.Open(false);

Expand All @@ -106,7 +113,7 @@ public async Task Verify_that_when_ValuePrefix_is_set_CSV_File_is_Written_with_p

var credentials = new Credentials("admin", "pass", uri);

var session = new Session(jsonFileDal, credentials);
var session = new Session(jsonFileDal, credentials, this.messageBus);

await session.Open(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class ThingTimeStampedCSVWriterTestFixture

private string mappingPath;

private CDPMessageBus messageBus;

[SetUp]
public void SetUp()
{
Expand All @@ -64,6 +66,14 @@ public void SetUp()
this.iterationReader = new IterationReader();

this.thingTimeStampedCsvWriter = new ThingTimeStampedCSVWriter();

this.messageBus = new CDPMessageBus();
}

[TearDown]
public void Teardown()
{
this.messageBus.Dispose();
}

[Test]
Expand All @@ -77,7 +87,7 @@ public async Task Verify_that_demosat_model_can_be_written_to_CSV_file()

var credentials = new Credentials("admin", "pass", uri);

var session = new Session(jsonFileDal, credentials);
var session = new Session(jsonFileDal, credentials, this.messageBus);

await session.Open(false);

Expand Down
6 changes: 6 additions & 0 deletions DEH-CSV.Tests/DEH-CSV.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<None Update="Data\demo-space.zip">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\import-data.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\import-mapping.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\mapping.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
36 changes: 36 additions & 0 deletions DEH-CSV.Tests/Data/import-data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Category;Short name;Name;area;mass;dry mass;radius;Ownership
Subsystem;shortName0000;Name 0000;0;0;10;100;System Engineering
Subsystem;shortName0001;Name 0001;1;-1;11;101;System Engineering
Subsystem;shortName0002;Name 0002;2;-2;12;102;System Engineering
Subsystem;shortName0003;Name 0003;3;-3;13;103;System Engineering
Subsystem;shortName0004;Name 0004;4;-4;14;104;System Engineering
Subsystem;shortName0005;Name 0005;5;-5;15;105;System Engineering
Subsystem;shortName0006;Name 0006;6;-6;16;106;System Engineering
Subsystem;shortName0007;Name 0007;7;-7;17;107;System Engineering
Subsystem;shortName0008;Name 0008;8;-8;18;108;System Engineering
Subsystem;shortName0009;Name 0009;9;-9;19;109;System Engineering
Subsystem;shortName0010;Name 0010;10;-10;20;110;System Engineering
Subsystem;shortName0011;Name 0011;11;-11;21;111;System Engineering
Subsystem;shortName0012;Name 0012;12;-12;22;112;System Engineering
Subsystem;shortName0013;Name 0013;13;-13;23;113;System Engineering
Subsystem;shortName0014;Name 0014;14;-14;24;114;System Engineering
Subsystem;shortName0015;Name 0015;15;-15;25;115;System Engineering
Subsystem;shortName0016;Name 0016;16;-16;26;116;System Engineering
Subsystem;shortName0017;Name 0017;17;-17;27;117;System Engineering
Subsystem;shortName0018;Name 0018;18;-18;28;118;System Engineering
Subsystem;shortName0019;Name 0019;19;-19;29;119;System Engineering
Subsystem;shortName0020;Name 0020;20;-20;30;120;System Engineering
Subsystem;shortName0021;Name 0021;21;-21;31;121;System Engineering
Subsystem;shortName0022;Name 0022;22;-22;32;122;System Engineering
Subsystem;shortName0023;Name 0023;23;-23;33;123;System Engineering
Subsystem;shortName0024;Name 0024;24;-24;34;124;System Engineering
Subsystem;shortName0025;Name 0025;25;-25;35;125;System Engineering
Subsystem;shortName0026;Name 0026;26;-26;36;126;System Engineering
Subsystem;shortName0027;Name 0027;27;-27;37;127;System Engineering
Subsystem;shortName0028;Name 0028;28;-28;38;128;System Engineering
Subsystem;shortName0029;Name 0029;29;-29;39;129;System Engineering
Subsystem;shortName0030;Name 0030;30;-30;40;130;System Engineering
Subsystem;shortName0031;Name 0031;31;-31;41;131;System Engineering
Subsystem;shortName0032;Name 0032;32;-32;42;132;System Engineering
Subsystem;shortName0033;Name 0033;33;-33;43;133;System Engineering
Subsystem;shortName0034;Name 0034;34;-34;44;134;System Engineering
Loading

0 comments on commit 0d9f4af

Please sign in to comment.