Skip to content

Commit

Permalink
Implement migration.json support when importing (#141)
Browse files Browse the repository at this point in the history
* WriteExtraFilesToZipFile initial implementation.

* Change WriteExtraFilesToZipFile.

* Add null parameter validation.

* Update test.

* Review Cozmin and Sam comments.
  • Loading branch information
adrianchivu authored Nov 5, 2020
1 parent 1bc5b98 commit 6dcbfe9
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 6 deletions.
128 changes: 125 additions & 3 deletions CDP4JsonFileDal.Tests/JsonFileDalTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,17 @@ namespace CDP4JsonFileDal.Tests
public class JsonFileDalTestFixture
{
/// <summary>
/// The instance of <see cref="JSONFileDal"/> that is being tested
/// AnnexC3 file archive
/// </summary>
private string annexC3File;

/// <summary>
/// Migration file that will be included
/// </summary>
private string migrationFile;

/// <summary>
/// The instance of <see cref="JsonFileDal"/> that is being tested
/// </summary>
private JsonFileDal dal;

Expand Down Expand Up @@ -99,12 +109,23 @@ public void OneTimeSetUp()
public void SetUp()
{
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "files", "LOFT_ECSS-E-TM-10-25_AnnexC.zip");
var migrationSourceFile = Path.Combine(TestContext.CurrentContext.TestDirectory, @"..\..\..\files", "migration.json");

this.annexC3File = Path.Combine(TestContext.CurrentContext.TestDirectory, "files", "AnnexC3.zip");
this.migrationFile = Path.Combine(TestContext.CurrentContext.TestDirectory, "files", "migration.json");

if (!File.Exists(this.migrationFile))
{
File.Copy(migrationSourceFile, this.migrationFile);
}

this.cancelationTokenSource = new CancellationTokenSource();
this.credentials = new Credentials("admin", "pass", new Uri(path));
this.session = new Mock<ISession>();
this.dal = new JsonFileDal();
this.dal.Session = this.session.Object;
this.dal = new JsonFileDal
{
Session = this.session.Object
};

this.siteDirectoryData = new SiteDirectory();
this.session.Setup(x => x.RetrieveSiteDirectory()).Returns(this.siteDirectoryData);
Expand All @@ -116,6 +137,16 @@ public void TearDown()
{
this.credentials = null;
this.dal = null;

if (File.Exists(this.annexC3File))
{
File.Delete(this.annexC3File);
}

if (File.Exists(this.migrationFile))
{
File.Delete(this.migrationFile);
}
}

[Test]
Expand Down Expand Up @@ -343,5 +374,96 @@ public void VerifyCtorWithVersionAndCopyright()
Assert.IsTrue(this.dal.FileHeader.Copyright == COPYRIGHT);
Assert.IsTrue(this.dal.FileHeader.Remark == REMARK);
}

[Test]
public void VerifyWritingWithoutMigrationFile()
{
var zipCredentials = new Credentials("admin", "pass", new Uri(this.annexC3File));
var zipSession = new Session(this.dal, zipCredentials);

var operationContainers = this.BuildOperationContainers();

Assert.DoesNotThrowAsync(async () => await Task.Run(() => this.dal.Write(operationContainers)));
}

[Test]
public void VerifyWritingMigrationFile()
{
var zipCredentials = new Credentials("admin", "pass", new Uri(this.annexC3File));
var zipSession = new Session(this.dal, zipCredentials);

var operationContainers = this.BuildOperationContainers();

Assert.DoesNotThrowAsync(async () => await Task.Run(() => this.dal.Write(operationContainers, new string[] { this.migrationFile })));
}

/// <summary>
/// Build operation containes structure that will be serialized
/// </summary>
/// <returns>
/// List of <see cref="OperationContainer"/>
/// </returns>
private IEnumerable<OperationContainer> BuildOperationContainers()
{
var cache = new ConcurrentDictionary<CacheKey, Lazy<Thing>>();

// DomainOfExpertise
var domain = new DomainOfExpertise(Guid.NewGuid(), cache, this.credentials.Uri) { ShortName = "SYS" };
this.siteDirectoryData.Domain.Add(domain);

// PersonRole
var role = new PersonRole(Guid.NewGuid(), null, null);
this.siteDirectoryData.PersonRole.Add(role);
this.siteDirectoryData.DefaultPersonRole = role;

// ParticipantRole
var participantRole = new ParticipantRole(Guid.Empty, null, null);
this.siteDirectoryData.ParticipantRole.Add(participantRole);
this.siteDirectoryData.DefaultParticipantRole = participantRole;

// Organization
var organization = new Organization(Guid.NewGuid(), null, null)
{
Container = this.siteDirectoryData
};

// Iteration
var iterationIid = new Guid("b58ea73d-350d-4520-b9d9-a52c75ac2b5d");
var iterationSetup = new IterationSetup(Guid.NewGuid(), 0);
var iterationSetupPoco = new CDP4Common.SiteDirectoryData.IterationSetup(iterationSetup.Iid, cache, this.credentials.Uri);

// EngineeringModel
var model = new EngineeringModel(Guid.NewGuid(), cache, this.credentials.Uri);
var modelSetup = new CDP4Common.SiteDirectoryData.EngineeringModelSetup();
modelSetup.ActiveDomain.Add(domain);

var requiredRdl = new ModelReferenceDataLibrary();

var person = new Person { ShortName = "admin", Organization = organization };
var participant = new Participant(Guid.NewGuid(), cache, this.credentials.Uri) { Person = person };
participant.Person.Role = role;
participant.Role = participantRole;
participant.Domain.Add(domain);
modelSetup.Participant.Add(participant);

var lazyPerson = new Lazy<Thing>(() => person);
var iterationPoco = new CDP4Common.EngineeringModelData.Iteration(iterationIid, cache, this.credentials.Uri) { IterationSetup = iterationSetupPoco };
model.Iteration.Add(iterationPoco);
var iteration = (Iteration)iterationPoco.ToDto();
model.EngineeringModelSetup = modelSetup;
this.siteDirectoryData.Model.Add(modelSetup);
modelSetup.RequiredRdl.Add(requiredRdl);
modelSetup.IterationSetup.Add(iterationSetupPoco);
cache.TryAdd(new CacheKey(person.Iid, this.siteDirectoryData.Iid), lazyPerson);
this.siteDirectoryData.Cache = cache;
iteration.IterationSetup = iterationSetup.Iid;
var iterationClone = iteration.DeepClone<Iteration>();

var operation = new Operation(iteration, iterationClone, OperationKind.Update);
var operationContainers = new[] { new OperationContainer("/EngineeringModel/" + model.Iid + "/iteration/" + iteration.Iid, 0) };
operationContainers.Single().AddOperation(operation);

return operationContainers;
}
}
}
13 changes: 13 additions & 0 deletions CDP4JsonFileDal.Tests/files/migration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"url": "https://username:password@server",
"credentials": {
"1c2466ed-ddb9-4546-b458-5b3dbcb58924": {
"password": "70d3491b7b5033ce7e4f77630058a2b78a8e5a817b7eb957a306490092d4dd83",
"salt": "8b747da1d2220d2deb9c153882177727edcf05151b9c575881eea57fce7ecceb"
},
"cc89eec4-d7ba-486d-854b-8f2407e154e2": {
"password": "70d3491b7b5033ce7e4f77630058a2b78a8e5a817b7eb957a306490092d4dd83",
"salt": "8b747da1d2220d2deb9c153882177727edcf05151b9c575881eea57fce7ecceb"
}
}
}
35 changes: 32 additions & 3 deletions CDP4JsonFileDal/JsonFileDal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public class JsonFileDal : Dal
/// </summary>
private const string EngineeringModelZipLocation = "EngineeringModels";

/// <summary>
/// The application-dependent (extensions) files zip location.
/// </summary>
private const string ExtensionsZipLocation = "Extensions";

/// <summary>
/// The iteration zip location.
/// </summary>
Expand Down Expand Up @@ -157,13 +162,13 @@ public override bool IsReadOnly
/// <param name="operationContainers">
/// The provided <see cref="OperationContainer"/> to write
/// </param>
/// <param name="files">
/// The path to the files that need to be uploaded. If <paramref name="files"/> is null, then no files are to be uploaded
/// <param name="extensionFiles">
/// The path to the files that need to be uploaded. If <paramref name="extensionFiles"/> is null, then no files are to be uploaded
/// </param>
/// <returns>
/// A list of <see cref="Thing"/>s that has been created or updated since the last Read or Write operation.
/// </returns>
public override Task<IEnumerable<Thing>> Write(IEnumerable<OperationContainer> operationContainers, IEnumerable<string> files = null)
public override Task<IEnumerable<Thing>> Write(IEnumerable<OperationContainer> operationContainers, IEnumerable<string> extensionFiles = null)
{
this.ValidateOperationContainers(operationContainers);

Expand Down Expand Up @@ -243,6 +248,8 @@ public override Task<IEnumerable<Thing>> Write(IEnumerable<OperationContainer> o
this.WriteModelReferenceDataLibraryToZipFile(modelReferenceDataLibraries, zipFile, path);

this.WriteIterationsToZipFile(iterations, zipFile, path);

this.WriteExtensionFilesToZipFile(extensionFiles, zipFile, path);
}

Logger.Info("Successfully exported the open session {1} to {0}.", path, this.Session.Credentials.Uri);
Expand Down Expand Up @@ -806,6 +813,28 @@ private void WriteIterationsToZipFile(IEnumerable<CDP4Common.EngineeringModelDat
}
}

/// <summary>
/// Writes the application dependend files inside specific folder to the <see cref="ZipFile"/>
/// </summary>
/// <param name="extraFilesPath">The files list that will be written</param>
/// <param name="zipFile">The target <see cref="ZipFile"/></param>
/// <param name="filePath">The file path of the target <see cref="ZipFile"/></param>
private void WriteExtensionFilesToZipFile(IEnumerable<string> extraFilesPath, ZipFile zipFile, string filePath)
{
if (extraFilesPath is null)
{
return;
}

foreach (var extraFile in extraFilesPath)
{
var zipEntry = zipFile.AddFile(extraFile, ExtensionsZipLocation);
zipEntry.Comment = $"The {extraFile} file";
}

zipFile.Save(filePath);
}

/// <summary>
/// Reads SiteDirectory data from the archive
/// </summary>
Expand Down

0 comments on commit 6dcbfe9

Please sign in to comment.