Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIxed issue with Entity Equality #4025

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Ginger/GingerCoreCommon/Actions/Act.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,7 @@ public virtual void DoNewActionSetup()

public override void PrepareItemToBeCopied()
{
this.IsSharedRepositoryInstance = TargetFrameworkHelper.Helper.IsSharedRepositoryItem(this);
this.IsSharedRepositoryInstance = TargetFrameworkHelper.Helper?.IsSharedRepositoryItem(this) ?? false;
}

public override string GetItemType()
Expand Down Expand Up @@ -2116,7 +2116,7 @@ public bool AreEqual(object obj)
return false;
}

return Equals(obj as Act);
return AreEqual(obj as Act);
}

}
Expand Down
4 changes: 3 additions & 1 deletion Ginger/GingerCoreCommon/Actions/ActInputValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ public bool AreEqual(ActInputValue other)
}

return this.Param == other.Param &&
this.Value == other.Value;
((this.Value == null && string.IsNullOrEmpty(other.Value))
|| this.Value == other.Value);

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ public virtual string ActivityType

public override void PrepareItemToBeCopied()
{
this.IsSharedRepositoryInstance = TargetFrameworkHelper.Helper.IsSharedRepositoryItem(this);
this.IsSharedRepositoryInstance = TargetFrameworkHelper.Helper?.IsSharedRepositoryItem(this) ?? false;
}

public override string GetItemType()
Expand Down Expand Up @@ -1290,8 +1290,8 @@ public bool AreEqual(Activity other)
return false;
}

if (this.ActivityName != other.ActivityName || this.TargetApplication != other.TargetApplication ||
this.Type != other.Type || this.ActivitiesGroupID != other.ActivitiesGroupID)
if (ActivityName != other.ActivityName || TargetApplication != other.TargetApplication ||
Type != other.Type || (other.ActivitiesGroupID != null && ActivitiesGroupID != other.ActivitiesGroupID))
{
return false;
}
Expand Down
14 changes: 3 additions & 11 deletions Ginger/GingerCoreCommon/Repository/BusinessFlowLib/BusinessFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2050,21 +2050,11 @@ public bool AreEqual(BusinessFlow other)
{
if (other == null || this.Name != other.Name
|| this.Activities.Count != other.Activities.Count
|| this.Variables.Count != other.Variables.Count
|| this.TargetApplications.Count != other.TargetApplications.Count)
|| this.Variables.Count != other.Variables.Count)
{
return false;
}

for (int i = 0; i < this.TargetApplications.Count; i++)
{
if (!other.TargetApplications.Any(f => f.Name.Equals(this.TargetApplications[i].Name)
&& f.Guid.Equals(this.TargetApplications[i].Guid)))
{
return false;
}
}

for (int i = 0; i < this.Variables.Count; i++)
{
if (!this.Variables[i].AreEqual(other.Variables[i]))
Expand All @@ -2073,6 +2063,8 @@ public bool AreEqual(BusinessFlow other)
}
}

LoadLinkedActivities();

for (int i = 0; i < this.Activities.Count; i++)
{
if (!this.Activities[i].AreEqual(other.Activities[i]))
Expand Down
3 changes: 2 additions & 1 deletion Ginger/GingerCoreCommon/Repository/SolutionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,13 @@ public string CreateRepositoryItemFileName(RepositoryItemBase repositoryItemBase
filefullPath = filefullPath.Replace(Nameex, "");
}
counter++;

Nameex = "~" + counter;
filefullPath = filefullPath.Replace(ext, Nameex + ext);

if (counter > 100)
{
throw new Exception("cannot find unique file after 100 tries");
counter = Random.Shared.Next(minValue: 101, maxValue: 10000);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Ginger/GingerCoreCommon/VariablesLib/VariableBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ public bool AreEqual(object obj)
return false;
}

return Equals(obj as VariableBase);
return AreEqual(obj as VariableBase);
}
}
}
125 changes: 125 additions & 0 deletions Ginger/GingerCoreCommonTest/EqualityTests/ActivityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using Amdocs.Ginger.Repository;
using GingerCore;
using GingerCore.Actions;
using GingerCore.Variables;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GingerCoreCommonTest.Repository;
[TestClass]
public class ActivityEqualityTests
{
[TestMethod]
public void AreEqual_SameProperties_ReturnsTrue()
{
// Arrange
var activity1 = new Activity
{
ActivityName = "Search and Buy iPhone 15 Device",
TargetApplication = "Brain_3UK_Self Service",
Type = eSharedItemType.Regular,
ActivitiesGroupID = null,
Acts = [new ActDummy()],
Variables = [new VariableString()]
};

var activity2 = new Activity
{
ActivityName = "Search and Buy iPhone 15 Device",
TargetApplication = "Brain_3UK_Self Service",
Type = eSharedItemType.Regular,
ActivitiesGroupID = null,
Acts = [new ActDummy()],
Variables = [new VariableString()]
};

// Act
var result = activity1.AreEqual(activity2);

// Assert
Assert.IsTrue(result);
}

[TestMethod]
public void AreEqual_DifferentActivityName_ReturnsFalse()
{
// Arrange
var activity1 = new Activity { ActivityName = "TestActivity1" };
var activity2 = new Activity { ActivityName = "TestActivity2" };

// Act
var result = activity1.AreEqual(activity2);

// Assert
Assert.IsFalse(result);
}

[TestMethod]
public void AreEqual_DifferentTargetApplication_ReturnsFalse()
{
// Arrange
var activity1 = new Activity { TargetApplication = "TestApp1" };
var activity2 = new Activity { TargetApplication = "TestApp2" };

// Act
var result = activity1.AreEqual(activity2);

// Assert
Assert.IsFalse(result);
}

[TestMethod]
public void AreEqual_DifferentType_ReturnsFalse()
{
// Arrange
var activity1 = new Activity { Type = eSharedItemType.Regular };
var activity2 = new Activity { Type = eSharedItemType.Link };

// Act
var result = activity1.AreEqual(activity2);

// Assert
Assert.IsFalse(result);
}

[TestMethod]
public void AreEqual_DifferentActivitiesGroupID_ReturnsFalse()
{
// Arrange
var activity1 = new Activity { ActivitiesGroupID = "Group1" };
var activity2 = new Activity { ActivitiesGroupID = "Group2" };

// Act
var result = activity1.AreEqual(activity2);

// Assert
Assert.IsFalse(result);
}

[TestMethod]
public void AreEqual_DifferentActsCount_ReturnsFalse()
{
// Arrange
var activity1 = new Activity { Acts = [new ActDummy()] };
var activity2 = new Activity { Acts = [new ActDummy(), new ActDummy()] };

// Act
var result = activity1.AreEqual(activity2);

// Assert
Assert.IsFalse(result);
}
Comment on lines +98 to +110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance collection equality tests

The current tests only verify collection counts. Consider adding tests for:

  1. Actual equality of Acts and Variables
  2. Different ordering of collection elements
  3. Null vs empty collection scenarios

Example:

[TestMethod]
public void AreEqual_SameActsDifferentOrder_ReturnsTrue()
{
    // Arrange
    var act1 = new ActDummy { Name = "Act1" };
    var act2 = new ActDummy { Name = "Act2" };
    
    var activity1 = new Activity { Acts = [act1, act2] };
    var activity2 = new Activity { Acts = [act2, act1] };

    // Act
    var result = activity1.AreEqual(activity2);

    // Assert
    Assert.IsTrue(result);
}

Also applies to: 112-124


[TestMethod]
public void AreEqual_DifferentVariablesCount_ReturnsFalse()
{
// Arrange
var activity1 = new Activity { Variables = [new VariableString()] };
var activity2 = new Activity { Variables = [new VariableString(), new VariableString()] };

// Act
var result = activity1.AreEqual(activity2);

// Assert
Assert.IsFalse(result);
}
}
63 changes: 63 additions & 0 deletions Ginger/GingerCoreCommonTest/EqualityTests/BusinessFlowTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using GingerCore;
using GingerCore.Platforms;
using GingerCore.Variables;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace GingerCoreCommonTest.Repository
{
[TestClass]
public class BusinessFlowEqualityTests
{
[TestMethod]
public void ArAeEqual_ShouldReturnTrue_WhenBusinessFlowsAreEqual()
{
// Arrange
var businessFlow1 = CreateBusinessFlow("TestFlow");
var businessFlow2 = CreateBusinessFlow("TestFlow");

// Act
var result = businessFlow1.AreEqual(businessFlow2);

// Assert
Assert.IsTrue(result);
}
Comment on lines +12 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage for BusinessFlow equality

The current tests only verify equality based on the Name property. Consider adding tests for:

  • Equality of child collections (Activities, Variables, TargetApplications)
  • Edge cases (null values, empty collections)
  • Different ordering of child elements

Example test cases to add:

[TestMethod]
public void AreEqual_DifferentActivityOrder_ReturnsTrue()
{
    // Arrange
    var flow1 = CreateBusinessFlow("TestFlow");
    var flow2 = CreateBusinessFlow("TestFlow");
    flow2.Activities.Reverse();

    // Act
    var result = flow1.AreEqual(flow2);

    // Assert
    Assert.IsTrue(result);
}

[TestMethod]
public void AreEqual_NullActivities_ReturnsFalse()
{
    // Arrange
    var flow1 = CreateBusinessFlow("TestFlow");
    var flow2 = CreateBusinessFlow("TestFlow");
    flow2.Activities = null;

    // Act
    var result = flow1.AreEqual(flow2);

    // Assert
    Assert.IsFalse(result);
}

Also applies to: 26-38


[TestMethod]
public void ArAeEqual_ShouldReturnFalse_WhenBusinessFlowsAreNotEqual()
{
// Arrange
var businessFlow1 = CreateBusinessFlow("TestFlow1");
var businessFlow2 = CreateBusinessFlow("TestFlow2");

// Act
var result = businessFlow1.AreEqual(businessFlow2);

// Assert
Assert.IsFalse(result);
}

private BusinessFlow CreateBusinessFlow(string name)
{
return new BusinessFlow
{
Name = name,
Activities =
[
new Activity { ActivityName = "Activity1" },
new Activity { ActivityName = "Activity2" }
],
Variables =
[
new VariableString { Name = "Variable1" },
new VariableString { Name = "Variable2" }
],
TargetApplications =
[
new TargetApplication { AppName = "Target1", Guid = Guid.NewGuid() },
new TargetApplication { AppName = "Target2", Guid = Guid.NewGuid() }
]
};
}
}
}
1 change: 1 addition & 0 deletions Ginger/GingerCoreCommonTest/GingerCoreCommonTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="EqualityTests\" />
<Folder Include="TestResources\" />
</ItemGroup>

Expand Down
Loading