Skip to content

Commit

Permalink
Fixes #74. (#75)
Browse files Browse the repository at this point in the history
Registration interface improved.
- RegisterPostProcessings removed
- Naming unified (RegisterCopyAttributeWithSourceFilter renamed to RegisterCopyAttribute, RegisterCreateToManyGeneric renamed to RegisterCreateToMany)
- Overloads for RegisterCreateToOneWithRelation and RegisterCreateToManyWithRelation with target argument in SourveValue Function.
- RegisterCreateToManyWithSourceFilterAndReverseRelation removed (RegisterCreateToManyWithRelation can be used instead).

Co-authored-by: Stefan Lindegger <lis@bbtsoftware.ch>
  • Loading branch information
2 people authored and Elias Oehen committed Feb 25, 2021
1 parent 8155c54 commit d05fa61
Show file tree
Hide file tree
Showing 31 changed files with 602 additions and 232 deletions.
150 changes: 132 additions & 18 deletions src/BBT.StructureTools.Tests/Convert/ConverterIntTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ public void Convert_WholeStructure_ConvertAll()
AssertRoot(sourceRoot, targetRoot);
}

/// <summary>
/// Tests the convert from root to leaf into target structure.
/// </summary>
[Fact]
public void Convert_CopyAttribute_Successful()
{
// Arrange
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CopyTreeAttributeRegistrations>();

var converter = GetConverter<SourceTree, TargetTree>();

var sourceTree = new SourceTree()
{
TreeName = "TreeName",
};

var targetTree = new TargetTree()
{
MasterDataId = Guid.NewGuid(),
};

// Act
var processings = new List<IBaseAdditionalProcessing>();
converter.Convert(sourceTree, targetTree, processings);

// Assert
targetTree.TreeName.Should().Be(sourceTree.TreeName);
targetTree.TemporalDataOriginId.Should().Be(targetTree.MasterDataId);
}

/// <summary>
/// Tests the convert from root to leaf into target structure.
/// </summary>
Expand Down Expand Up @@ -119,10 +149,10 @@ public void Convert_CreateToManyWithGenericReverseRelation_Successful()
/// Tests the convert from root to leaf into target structure.
/// </summary>
[Fact]
public void Convert_RegisterCreateToManyWithSourceFilterAndReverseRelation_Successful()
public void Convert_CreateToMany_Successful()
{
// Arrange
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CreateToManyWithSourceFilterAndReverseRelationRegistrations>();
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CreateToManyGenericRegistrations>();
container.Bind<IConvertRegistrations<SourceTreeLeaf, TargetTreeLeaf, IForTest>>().To<CopyLeafAttributeRegistrations>();

var converter = GetConverter<SourceTree, TargetTree>();
Expand All @@ -144,7 +174,7 @@ public void Convert_RegisterCreateToManyWithSourceFilterAndReverseRelation_Succe
/// Tests the convert from root to leaf into target structure.
/// </summary>
[Fact]
public void Convert_CreateToManyGeneric_Successful()
public void Convert_Processings_Successful()
{
// Arrange
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CreateToManyGenericRegistrations>();
Expand All @@ -156,13 +186,28 @@ public void Convert_CreateToManyGeneric_Successful()
sourceTree.Leafs.Add(new SourceTreeLeaf() { Tree = sourceTree });
sourceTree.Leafs.Add(new SourceTreeLeaf() { Tree = sourceTree });
var targetTree = new TargetTree();
var targetIds = new List<Guid>();
var sourceIds = new List<Guid>();
var preProcHits = 0;

// Act
var processings = new List<IBaseAdditionalProcessing>();
processings.Add(new GenericConvertPostProcessing<SourceTreeLeaf, TargetTreeLeaf>((x, y) => targetIds.Add(y.Id)));
processings.Add(new GenericContinueConvertInterception<SourceTreeLeaf, TargetTreeLeaf>(
x =>
{
sourceIds.Add(x.Id);
return true;
}));
processings.Add(new GenericConvertPreProcessing<SourceTreeLeaf, TargetTreeLeaf>((x, y) => preProcHits++));

converter.Convert(sourceTree, targetTree, processings);

// Assert
sourceTree.Leafs.Count.Should().Be(targetTree.TargetLeafs.Count);
targetIds.Should().BeEquivalentTo(targetTree.TargetLeafs.Select(x => x.Id).ToList());
sourceIds.Should().BeEquivalentTo(sourceTree.Leafs.Select(x => x.Id).ToList());
preProcHits.Should().Be(2);
}

/// <summary>
Expand Down Expand Up @@ -236,45 +281,49 @@ public void Convert_CreateToManyFromGenericStrategyWithReverseRelation_Successfu
/// Tests the convert from root to leaf into target structure.
/// </summary>
[Fact]
public void Convert_RegisterPostProcessings_Successful()
public void Convert_RegisterCreateToOneWithRelation_Successful()
{
// Arrange
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CreateToManyGenericRegistrations>();
container.Bind<IConvertRegistrations<SourceTreeLeaf, TargetTreeLeaf, IForTest>>().To<LeafPostProcessingsRegistrations>();
container.Bind<IConvertRegistrations<SourceRoot, TargetRoot, IForTest>>().To<CreateToOneWithRelationRegistrations>();
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CopyTreeAttributeRegistrations>();

var converter = GetConverter<SourceTree, TargetTree>();
var converter = GetConverter<SourceRoot, TargetRoot>();

var sourceTree = new SourceTree();
sourceTree.Leafs.Add(new SourceTreeLeaf() { Tree = sourceTree });
sourceTree.Leafs.Add(new SourceTreeLeaf() { Tree = sourceTree });
var targetTree = new TargetTree();
var sourceRoot = new SourceRoot()
{
Tree = new SourceTree(),
};
var targetRoot = new TargetRoot();

// Act
var processings = new List<IBaseAdditionalProcessing>();
converter.Convert(sourceTree, targetTree, processings);
converter.Convert(sourceRoot, targetRoot, processings);

// Assert
targetTree.TargetLeafs.Count.Should().Be(sourceTree.Leafs.Count);
targetTree.TargetLeafs.All(x => x.LeafName == "PostProcessingExpected").Should().BeTrue();
targetRoot.TargetTree.Should().NotBeNull();
targetRoot.TargetTree.RelationOnTarget.Should().Be(targetRoot.RelationOnTarget);
}

/// <summary>
/// Tests the convert from root to leaf into target structure.
/// </summary>
[Fact]
public void Convert_RegisterCreateToOneWithRelation_Successful()
public void Convert_RegisterCreateToOneWithRelationAndTarget_Successful()
{
// Arrange
container.Bind<IConvertRegistrations<SourceRoot, TargetRoot, IForTest>>().To<CreateToOneWithRelationRegistrations>();
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CopyTreeAttributeRegistrations>();
container.Bind<IConvertRegistrations<SourceRoot, TargetRoot, IForTest>>().To<CreateToOneWithRelationAndTargetRegistrations>();
container.Bind<IConvertRegistrations<IdDto, TargetTree, IForTest>>().To<CopyIdDtoToTargetTreeRegistrations>();

var converter = GetConverter<SourceRoot, TargetRoot>();

var sourceRoot = new SourceRoot()
{
Tree = new SourceTree(),
};
var targetRoot = new TargetRoot();
var targetRoot = new TargetRoot()
{
Id = Guid.NewGuid(),
};

// Act
var processings = new List<IBaseAdditionalProcessing>();
Expand All @@ -283,6 +332,7 @@ public void Convert_RegisterCreateToOneWithRelation_Successful()
// Assert
targetRoot.TargetTree.Should().NotBeNull();
targetRoot.TargetTree.RelationOnTarget.Should().Be(targetRoot.RelationOnTarget);
targetRoot.TargetTree.TemporalDataOriginId.Should().Be(targetRoot.Id);
}

/// <summary>
Expand Down Expand Up @@ -318,6 +368,70 @@ public void Convert_RegisterCreateToManyWithRelation_Successful()
targetLeaf.RelationOnTarget.Should().Be(targetTree.RelationOnTarget);
}

/// <summary>
/// Tests the convert from root to leaf into target structure.
/// </summary>
[Fact]
public void Convert_RegisterCreateToManyWithRelationAndTarget_Successful()
{
// Arrange
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CreateToManyWithRelationAndTargetRegistrations>();
container.Bind<IConvertRegistrations<IdDto, TargetTreeLeaf, IForTest>>().To<CopyIdDtoToTargetTreeLeafRegistrations>();

var converter = GetConverter<SourceTree, TargetTree>();

var sourceTree = new SourceTree()
{
};

sourceTree.Leafs.Add(new SourceTreeLeaf());

var targetTree = new TargetTree()
{
RelationOnTarget = new MasterData(),
};

// Act
var processings = new List<IBaseAdditionalProcessing>();
converter.Convert(sourceTree, targetTree, processings);

// Assert
targetTree.TargetLeafs.Count.Should().Be(1);
var targetLeaf = targetTree.TargetLeafs.Single();
targetLeaf.RelationOnTarget.Should().Be(targetTree.RelationOnTarget);
targetLeaf.OriginId.Should().Be(targetTree.Id);
}

/// <summary>
/// Tests the convert from root to leaf into target structure.
/// </summary>
[Fact]
public void Convert_RegisterCreateToManyWithRelation_ReverseRelation_Successful()
{
// Arrange
container.Bind<IConvertRegistrations<SourceTree, TargetTree, IForTest>>().To<CreateToManyWithRelationReverseRelationRegistrations>();
container.Bind<IConvertRegistrations<SourceTreeLeaf, TargetTreeLeaf, IForTest>>().To<CopyLeafAttributeRegistrations>();

var converter = GetConverter<SourceTree, TargetTree>();

var sourceTree = new SourceTree();
sourceTree.Leafs.Add(new SourceTreeLeaf() { Tree = sourceTree });
sourceTree.Leafs.Add(new SourceTreeLeaf() { Tree = sourceTree });
var targetTree = new TargetTree();

// Act
var processings = new List<IBaseAdditionalProcessing>();
converter.Convert(sourceTree, targetTree, processings);

// Assert
sourceTree.Leafs.Count.Should().Be(targetTree.TargetLeafs.Count);
for (var i = 0; i < sourceTree.Leafs.Count; i++)
{
targetTree.TargetLeafs[i].TargetTree.Should().Be(targetTree);
targetTree.TargetLeafs[i].OriginId.Should().Be(sourceTree.Leafs.ElementAt(i).Id);
}
}

#endregion

#region Asserts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public class CopyHistAttributeRegistrations : IConvertRegistrations<SourceTreeHi
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<SourceTreeHist, TargetTreeHist> aRegistrations)
public void DoRegistrations(IConvertRegistration<SourceTreeHist, TargetTreeHist> registrations)
{
aRegistrations.NotNull(nameof(aRegistrations));
registrations.NotNull(nameof(registrations));

aRegistrations
registrations
.RegisterCopyAttribute(x => x.Id, x => x.OriginId)
.RegisterCopyAttribute(x => x.From, x => x.From)
.RegisterCopyAttribute(x => x.To, x => x.To);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright © BBT Software AG. All rights reserved.

namespace BBT.StructureTools.Tests.Convert.Registrations
{
using BBT.StructureTools.Convert;
using BBT.StructureTools.Extension;
using BBT.StructureTools.Tests.Convert.TestData;

/// <summary>
/// Registrations for test purposes.
/// </summary>
public class CopyIdDtoToTargetTreeLeafRegistrations : IConvertRegistrations<IdDto, TargetTreeLeaf, IForTest>
{
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<IdDto, TargetTreeLeaf> registrations)
{
registrations.NotNull(nameof(registrations));

registrations.RegisterCopyAttribute(
x => x.Id,
x => x.OriginId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright © BBT Software AG. All rights reserved.

namespace BBT.StructureTools.Tests.Convert.Registrations
{
using BBT.StructureTools.Convert;
using BBT.StructureTools.Extension;
using BBT.StructureTools.Tests.Convert.TestData;

/// <summary>
/// Registrations for test purposes.
/// </summary>
public class CopyIdDtoToTargetTreeRegistrations : IConvertRegistrations<IdDto, TargetTree, IForTest>
{
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<IdDto, TargetTree> registrations)
{
registrations.NotNull(nameof(registrations));

registrations.RegisterCopyAttribute(
x => x.Id,
x => x.TemporalDataOriginId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class CopyLeafAttributeRegistrations : IConvertRegistrations<SourceTreeLe
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<SourceTreeLeaf, TargetTreeLeaf> aRegistrations)
public void DoRegistrations(IConvertRegistration<SourceTreeLeaf, TargetTreeLeaf> registrations)
{
aRegistrations.NotNull(nameof(aRegistrations));
registrations.NotNull(nameof(registrations));

aRegistrations.RegisterCopyAttribute(
registrations.RegisterCopyAttribute(
x => x.Id,
x => x.OriginId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public class CopyTreeAttributeRegistrations : IConvertRegistrations<SourceTree,
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<SourceTree, TargetTree> aRegistrations)
public void DoRegistrations(IConvertRegistration<SourceTree, TargetTree> registrations)
{
aRegistrations.NotNull(nameof(aRegistrations));
registrations.NotNull(nameof(registrations));

registrations
.RegisterCopyAttribute(x => x.TreeName, x => x.TreeName)
.RegisterCopyAttribute((x, y) => y.MasterDataId, x => x.TemporalDataOriginId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class CreateToManyFromGenericStrategyWithReverseRelationRegistrations : I
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<SourceRoot, TargetRoot> aRegistrations)
public void DoRegistrations(IConvertRegistration<SourceRoot, TargetRoot> registrations)
{
aRegistrations.NotNull(nameof(aRegistrations));
registrations.NotNull(nameof(registrations));

aRegistrations.RegisterCreateToManyFromGenericStrategyWithReverseRelation<SourceBaseLeaf, TargetBaseLeaf, IForTest>(
registrations.RegisterCreateToManyFromGenericStrategyWithReverseRelation<SourceBaseLeaf, TargetBaseLeaf, IForTest>(
x => x.Leafs,
x => x.TargetLeafs,
x => x.TargetRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public CreateToManyGenericRegistrations(
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<SourceTree, TargetTree> aRegistrations)
public void DoRegistrations(IConvertRegistration<SourceTree, TargetTree> registrations)
{
aRegistrations.NotNull(nameof(aRegistrations));
registrations.NotNull(nameof(registrations));

aRegistrations
.RegisterCreateToManyGeneric(
registrations
.RegisterCreateToMany(
x => x.Leafs,
x => x.TargetLeafs,
this.convertHelperFactory.GetConvertHelper());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public CreateToManyGenericWithReverseRelationRegistrations(
/// <summary>
/// See <see cref="IConvertRegistrations{TSource, TTarget, TConvertIntention}.DoRegistrations"/>.
/// </summary>
public void DoRegistrations(IConvertRegistration<SourceTree, TargetTree> aRegistrations)
public void DoRegistrations(IConvertRegistration<SourceTree, TargetTree> registrations)
{
aRegistrations.NotNull(nameof(aRegistrations));
registrations.NotNull(nameof(registrations));

aRegistrations.RegisterCreateToManyWithReverseRelation(
registrations.RegisterCreateToManyWithReverseRelation(
x => x.Leafs,
x => x.TargetLeafs,
this.convertHelperFactory.GetConvertHelper(x => x.TargetTree));
Expand Down
Loading

0 comments on commit d05fa61

Please sign in to comment.