Skip to content

Commit

Permalink
6.0.0-RC3 merge (#146)
Browse files Browse the repository at this point in the history
* add sonarcloud support (#140)

* Autogen excludes

* Update appveyor.yml

* Update appveyor.yml

* Fixes #143 required rdl lookup (#144)

* Fix required rdl lookup
* Fix docs
* Added unit tests

Co-authored-by: Mike Unitskyi <[email protected]>
Co-authored-by: Alexander van Delft <[email protected]>
  • Loading branch information
3 people authored Jul 27, 2020
1 parent 8fa9207 commit 2e9727e
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 32 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ packages/
#Test files

CDP4.v12.suo

/CodeGenerator/*

/CodeGenerator/*
.idea
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ModelReferenceDataLibraryServiceTestFixture.cs" company="RHEA System S.A.">
// Copyright (c) 2015-2020 RHEA System S.A.
//
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Kamil Wojnowski, Nathanael Smiechowski
//
// This file is part of CDP4 Web Services Community Edition.
// The CDP4 Web Services Community Edition is the RHEA implementation of ECSS-E-TM-10-25 Annex A and Annex C.
// This is an auto-generated class. Any manual changes to this file will be overwritten!
//
// The CDP4 Web Services 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 CDP4 Web Services 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
// Lesser 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 CDP4WebServices.API.Tests.Services.Supplemental
{
using System;
using System.Collections.Generic;

using CDP4Common.DTO;

using CDP4Orm.Dao;

using CDP4WebServices.API.Helpers;
using CDP4WebServices.API.Services;

using Moq;

using Npgsql;

using NUnit.Framework;

[TestFixture]
public class ModelReferenceDataLibraryServiceTestFixture
{
private Mock<ISiteReferenceDataLibraryDao> siteReferenceDataLibraryDao;
private Mock<IEngineeringModelSetupDao> engineeringModelSetupDao;
private Mock<IModelReferenceDataLibraryDao> modelReferenceDataLibraryDao;

private Mock<ICdp4TransactionManager> transactionManager;
private ModelReferenceDataLibrary modelReferenceDataLibrary;
private SiteReferenceDataLibrary siteReferenceDataLibrary1;
private SiteReferenceDataLibrary siteReferenceDataLibrary2;
private Iteration iteration;
private IterationSetup iterationSetup;
private EngineeringModelSetup engineeringModelSetup;

private ModelReferenceDataLibraryService modelReferenceDataLibraryService;

[SetUp]
public void Setup()
{
this.siteReferenceDataLibraryDao = new Mock<ISiteReferenceDataLibraryDao>();
this.engineeringModelSetupDao = new Mock<IEngineeringModelSetupDao>();
this.modelReferenceDataLibraryDao = new Mock<IModelReferenceDataLibraryDao>();
this.transactionManager = new Mock<ICdp4TransactionManager>();

this.transactionManager.Setup(x => x.IsFullAccessEnabled()).Returns(true);

this.modelReferenceDataLibraryService = new ModelReferenceDataLibraryService
{
EngineeringModelSetupDao = this.engineeringModelSetupDao.Object,
SiteReferenceDataLibraryDato = this.siteReferenceDataLibraryDao.Object,
ModelReferenceDataLibraryDao = this.modelReferenceDataLibraryDao.Object,
TransactionManager = this.transactionManager.Object
};

this.siteReferenceDataLibrary1 = new SiteReferenceDataLibrary(Guid.NewGuid(), 0);
this.siteReferenceDataLibrary2 = new SiteReferenceDataLibrary(Guid.NewGuid(), 0);
this.modelReferenceDataLibrary = new ModelReferenceDataLibrary(Guid.NewGuid(), 0);

this.iteration = new Iteration(Guid.NewGuid(), 0);
this.iterationSetup = new IterationSetup(Guid.NewGuid(), 0);
this.engineeringModelSetup = new EngineeringModelSetup(Guid.NewGuid(), 0);

this.iteration.IterationSetup = this.iterationSetup.Iid;

this.engineeringModelSetup.IterationSetup.Add(this.iterationSetup.Iid);
}

[Test]
public void VerifyQueryReferenceDataLibraryThrowsOnUnknownEngineeringModelSetup()
{
this.engineeringModelSetupDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
It.IsAny<IEnumerable<Guid>>(),
It.IsAny<bool>()))
.Returns(new List<EngineeringModelSetup>());

Assert.Throws<InvalidOperationException>(
() => this.modelReferenceDataLibraryService.QueryReferenceDataLibrary(null, this.iteration)
);
}

[Test]
public void VerifyQueryReferenceDataLibraryThrowsOnNotHavingModelReferenceDataLibrary()
{
this.engineeringModelSetupDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
It.IsAny<IEnumerable<Guid>>(),
It.IsAny<bool>()))
.Returns(new List<EngineeringModelSetup> {this.engineeringModelSetup});

Assert.Throws<InvalidOperationException>(
() => this.modelReferenceDataLibraryService.QueryReferenceDataLibrary(null, this.iteration)
);
}

[Test]
public void VerifyQueryReferenceDataLibraryWorksForModelReferenceDataLibrary()
{
this.engineeringModelSetupDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
It.IsAny<IEnumerable<Guid>>(),
It.IsAny<bool>()))
.Returns(new List<EngineeringModelSetup> { this.engineeringModelSetup });

this.modelReferenceDataLibraryDao.Setup( x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
It.IsAny<IEnumerable<Guid>>(),
It.IsAny<bool>()))
.Returns(new List<ModelReferenceDataLibrary> { this.modelReferenceDataLibrary});

var result = this.modelReferenceDataLibraryService.QueryReferenceDataLibrary(null, this.iteration);

CollectionAssert.AreEqual(new List<ReferenceDataLibrary> { this.modelReferenceDataLibrary }, result);
}

[Test]
public void VerifyQueryReferenceDataLibraryWorksForModelReferenceDataLibraryWithSingleSiteReferenceLibraryInChainOfRDLs()
{
this.engineeringModelSetupDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
It.IsAny<IEnumerable<Guid>>(),
It.IsAny<bool>()))
.Returns(new List<EngineeringModelSetup> { this.engineeringModelSetup });

this.engineeringModelSetup.RequiredRdl.Add(this.modelReferenceDataLibrary.Iid);

this.modelReferenceDataLibraryDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
new [] { this.modelReferenceDataLibrary.Iid},
It.IsAny<bool>()))
.Returns(new List<ModelReferenceDataLibrary> { this.modelReferenceDataLibrary });

this.siteReferenceDataLibraryDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
new[] { this.siteReferenceDataLibrary1.Iid },
It.IsAny<bool>()))
.Returns(new List<SiteReferenceDataLibrary> { this.siteReferenceDataLibrary1 });

this.modelReferenceDataLibrary.RequiredRdl = this.siteReferenceDataLibrary1.Iid;

var result = this.modelReferenceDataLibraryService.QueryReferenceDataLibrary(null, this.iteration);

CollectionAssert.AreEqual(new List<ReferenceDataLibrary> { this.modelReferenceDataLibrary, this.siteReferenceDataLibrary1 }, result);
}

[Test]
public void VerifyQueryReferenceDataLibraryWorksForModelReferenceDataLibraryWithMultipleSiteReferenceLibrariesInChainOfRDLs()
{
this.engineeringModelSetupDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
It.IsAny<IEnumerable<Guid>>(),
It.IsAny<bool>()))
.Returns(new List<EngineeringModelSetup> { this.engineeringModelSetup });

this.engineeringModelSetup.RequiredRdl.Add(this.modelReferenceDataLibrary.Iid);

this.modelReferenceDataLibraryDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
new[] { this.modelReferenceDataLibrary.Iid },
It.IsAny<bool>()))
.Returns(new List<ModelReferenceDataLibrary> { this.modelReferenceDataLibrary });

this.siteReferenceDataLibraryDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
new[] { this.siteReferenceDataLibrary1.Iid },
It.IsAny<bool>()))
.Returns(new List<SiteReferenceDataLibrary> { this.siteReferenceDataLibrary1 });

this.siteReferenceDataLibraryDao.Setup(x => x.Read(
It.IsAny<NpgsqlTransaction>(),
It.IsAny<string>(),
new[] { this.siteReferenceDataLibrary2.Iid },
It.IsAny<bool>()))
.Returns(new List<SiteReferenceDataLibrary> { this.siteReferenceDataLibrary2 });

this.modelReferenceDataLibrary.RequiredRdl = this.siteReferenceDataLibrary1.Iid;
this.siteReferenceDataLibrary1.RequiredRdl = this.siteReferenceDataLibrary2.Iid;

var result = this.modelReferenceDataLibraryService.QueryReferenceDataLibrary(null, this.iteration);

CollectionAssert.AreEqual(new List<ReferenceDataLibrary> { this.modelReferenceDataLibrary, this.siteReferenceDataLibrary1, this.siteReferenceDataLibrary2 }, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ModelReferenceDataLibraryService.cs" company="RHEA System S.A.">
// Copyright (c) 2016-2019 RHEA System S.A.
// Copyright (c) 2015-2020 RHEA System S.A.
//
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Kamil Wojnowski, Nathanael Smiechowski
//
// This file is part of CDP4 Web Services Community Edition.
// The CDP4 Web Services Community Edition is the RHEA implementation of ECSS-E-TM-10-25 Annex A and Annex C.
// This is an auto-generated class. Any manual changes to this file will be overwritten!
//
// The CDP4 Web Services 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 CDP4 Web Services 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
// Lesser 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>
// --------------------------------------------------------------------------------------------------------------------

Expand All @@ -9,8 +28,11 @@ namespace CDP4WebServices.API.Services
using System;
using System.Collections.Generic;
using System.Linq;

using CDP4Common.DTO;

using CDP4Orm.Dao;

using Npgsql;

/// <summary>
Expand All @@ -36,21 +58,24 @@ public partial class ModelReferenceDataLibraryService
/// <returns>The <see cref="ReferenceDataLibrary"/></returns>
public IEnumerable<ReferenceDataLibrary> QueryReferenceDataLibrary(NpgsqlTransaction transaction, Iteration iteration)
{
var engineeringModelSetup = this.EngineeringModelSetupDao.Read(transaction, typeof(SiteDirectory).Name).FirstOrDefault(ems => ems.IterationSetup.Contains(iteration.IterationSetup));
var engineeringModelSetup = this.EngineeringModelSetupDao.Read(transaction, nameof(SiteDirectory)).FirstOrDefault(ems => ems.IterationSetup.Contains(iteration.IterationSetup));

if (engineeringModelSetup == null)
{
throw new InvalidOperationException($"Could not find the associated engineering-modem-setup for iteration {iteration.Iid}");
}

var mrdl = this.ModelReferenceDataLibraryDao.Read(transaction, typeof(SiteDirectory).Name, engineeringModelSetup.RequiredRdl).FirstOrDefault();
var mrdl = this.ModelReferenceDataLibraryDao.Read(transaction, nameof(SiteDirectory), engineeringModelSetup.RequiredRdl).FirstOrDefault();

if (mrdl == null)
{
throw new InvalidOperationException($"Could not find the associated reference-data-library for iteration {iteration.Iid}");
}

var list = new List<ReferenceDataLibrary> {mrdl};
list.AddRange(this.GetRequiredRdl(transaction, mrdl));
return list;
var requiredRdls = new List<ReferenceDataLibrary> { mrdl };
this.TryCopyToRequiredRdls(this.GetRequiredRdl(transaction, mrdl), requiredRdls);

return requiredRdls;
}

/// <summary>
Expand All @@ -68,22 +93,33 @@ private IReadOnlyList<ReferenceDataLibrary> GetRequiredRdl(NpgsqlTransaction tra
return requiredRdls;
}

var requiredRdl = (ReferenceDataLibrary)this.ModelReferenceDataLibraryDao.Read(transaction, typeof(SiteDirectory).Name, new [] { rdl.RequiredRdl.Value }).FirstOrDefault();
var requiredRdl =
this.ModelReferenceDataLibraryDao.Read(transaction, nameof(SiteDirectory), new[] { rdl.RequiredRdl.Value }).FirstOrDefault() as ReferenceDataLibrary
?? this.SiteReferenceDataLibraryDato.Read(transaction, nameof(SiteDirectory), new[] { rdl.RequiredRdl.Value }).FirstOrDefault();

if (requiredRdl != null)
{
requiredRdls.Add(requiredRdl);
requiredRdls.AddRange(this.GetRequiredRdl(transaction, requiredRdl));
this.TryCopyToRequiredRdls(new[] { requiredRdl }, requiredRdls);
this.TryCopyToRequiredRdls(this.GetRequiredRdl(transaction, requiredRdl), requiredRdls);
}
else

return requiredRdls;
}

/// <summary>
/// Tries to copy inidividual <see cref="ReferenceDataLibrary"/>s from an <see cref="IEnumerable{ReferenceDataLibrary}"/> to an <see cref="ICollection{ReferenceDataLibrary}"/>
/// </summary>
/// <param name="source">The source <see cref="IEnumerable{ReferenceDataLibrary}"/></param>
/// <param name="target">The target <see cref="ICollection{ReferenceDataLibrary}"/></param>
private void TryCopyToRequiredRdls(IEnumerable<ReferenceDataLibrary> source, ICollection<ReferenceDataLibrary> target)
{
foreach (var possibleAdditionalRdl in source)
{
requiredRdl = this.SiteReferenceDataLibraryDato.Read(transaction, typeof(SiteDirectory).Name, new[] { rdl.RequiredRdl.Value }).FirstOrDefault();
if (requiredRdl != null)
if (target.All(x => x.Iid != possibleAdditionalRdl.Iid))
{
requiredRdls.Add(requiredRdl);
target.Add(possibleAdditionalRdl);
}
}

return requiredRdls;
}
}
}
Loading

0 comments on commit 2e9727e

Please sign in to comment.