-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into xabpe/1-Refactor-ArchivedMessages-to-clean-a…
…rchitecture
- Loading branch information
Showing
9 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
source/ArchivedMessages.Domain/Exceptions/InvalidEnumMappingException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// 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. | ||
|
||
namespace Energinet.DataHub.EDI.ArchivedMessages.Domain.Exceptions; | ||
|
||
public class InvalidEnumMappingException : Exception | ||
{ | ||
public InvalidEnumMappingException() | ||
{ | ||
} | ||
|
||
public InvalidEnumMappingException(string message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
public InvalidEnumMappingException(string message, Exception innerException) | ||
: base(message, innerException) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
source/ArchivedMessages.IntegrationTests/ArchivedMessagesMappingTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// 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. | ||
|
||
using Energinet.DataHub.EDI.ArchivedMessages.Domain.Models; | ||
using Energinet.DataHub.EDI.ArchivedMessages.Interfaces.Models; | ||
using Energinet.DataHub.EDI.BuildingBlocks.Domain.Validation; | ||
using Xunit; | ||
|
||
namespace Energinet.DataHub.EDI.ArchivedMessages.IntegrationTests; | ||
|
||
public class ArchivedMessagesMappingTests | ||
{ | ||
private enum MismatchedEnum | ||
{ | ||
A = 1, | ||
B = 2, | ||
D = 4, | ||
} | ||
|
||
[Fact] | ||
public void Given_EnumsAreLogicallyCompatible_When_Mapping_Then_ReturnTrueWhenEnumsMatch() | ||
{ | ||
// Act | ||
var result = EnumCompatibilityChecker.AreEnumsCompatible<ArchivedMessageType, ArchivedMessageTypeDto>(); | ||
|
||
// Assert | ||
Assert.True(result, "The enums should be logically compatible."); | ||
} | ||
|
||
[Fact] | ||
public void Given_EnumsAreNotLogicallyCompatible_When_Mapping_Then_ReturnFalseWhenEnumsDoNotMatch() | ||
{ | ||
// Act | ||
var result = EnumCompatibilityChecker.AreEnumsCompatible<ArchivedMessageType, MismatchedEnum>(); | ||
|
||
// Assert | ||
Assert.False(result, "The enums should not be logically compatible."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
source/BuildingBlocks.Domain/Validation/EnumCompatibilityChecker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2020 Energinet DataHub A/S | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License2"); | ||
// 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. | ||
|
||
namespace Energinet.DataHub.EDI.BuildingBlocks.Domain.Validation; | ||
|
||
public static class EnumCompatibilityChecker | ||
{ | ||
public static bool AreEnumsCompatible<T1, T2>() | ||
where T1 : Enum | ||
where T2 : Enum | ||
{ | ||
var firstEnum = Enum.GetValues(typeof(T1)).Cast<Enum>().ToDictionary(e => e.ToString(), Convert.ToInt32); | ||
var secondEnum = Enum.GetValues(typeof(T2)).Cast<Enum>().ToDictionary(e => e.ToString(), Convert.ToInt32); | ||
|
||
return firstEnum.All(e => secondEnum.ContainsKey(e.Key) && secondEnum[e.Key] == e.Value); | ||
} | ||
} |