Refactor IPAM model classes to use records for Address, Subnetwork, Vlan, Vrf, Section, Tag, Domain, Nameserver, and Session; enhance documentation and implement value equality for records.

This commit is contained in:
2026-01-19 17:25:18 +03:00
parent 694822f0d6
commit f56784f2aa
44 changed files with 1601 additions and 1905 deletions

View File

@@ -55,7 +55,7 @@ public class AddressTests
address.Note.Should().Be(note);
address.LastSeen.Should().Be(lastSeen);
address.ExcludePing.Should().Be(excludePing);
address.PTRignore.Should().Be(ptrIgnore);
address.PTRIgnore.Should().Be(ptrIgnore);
address.PTR.Should().Be(ptr);
address.FirewallAddressObject.Should().Be(firewallObject);
address.EditDate.Should().Be(editDate);
@@ -113,4 +113,24 @@ public class AddressTests
// Act & Assert
address.ToString().Should().Be(expectedIp);
}
[Fact]
public void Record_Equality_WorksCorrectly()
{
// Arrange
var address1 = new Address(
1, 10, "192.168.1.1", false, "Test", "host", "", "",
0, 0, "", "", "", null, false,
false, 0, "", null, 0, null
);
var address2 = new Address(
1, 10, "192.168.1.1", false, "Test", "host", "", "",
0, 0, "", "", "", null, false,
false, 0, "", null, 0, null
);
// Assert
address1.Should().Be(address2);
}
}

View File

@@ -57,7 +57,7 @@ public class NameserverTests
}
[Fact]
public void Constructor_WithEmptyNameservers_ReturnsArrayWithEmptyString()
public void Constructor_WithEmptyNameservers_ReturnsEmptyArray()
{
// Arrange
var nameServersString = "";
@@ -65,9 +65,8 @@ public class NameserverTests
// Act
var nameserver = new Nameserver(1, "Test", nameServersString, "", "", null);
// Assert
nameserver.NameServers.Should().HaveCount(1);
nameserver.NameServers[0].Should().BeEmpty();
// Assert - Empty entries are excluded
nameserver.NameServers.Should().BeEmpty();
}
[Fact]
@@ -79,4 +78,27 @@ public class NameserverTests
// Assert
nameserver.EditDate.Should().BeNull();
}
[Fact]
public void Constructor_WithSemicolonOnlyString_ReturnsEmptyArray()
{
// Arrange - edge case with just semicolons
var nameServersString = ";;;";
// Act
var nameserver = new Nameserver(1, "Test", nameServersString, "", "", null);
// Assert
nameserver.NameServers.Should().BeEmpty();
}
[Fact]
public void ToString_ReturnsName()
{
// Arrange
var nameserver = new Nameserver(1, "Google DNS", "8.8.8.8", "", "", null);
// Act & Assert
nameserver.ToString().Should().Be("Google DNS");
}
}

View File

@@ -10,7 +10,7 @@ public class SessionTests
public void Constructor_WithCredentialsAuth_SetsAllProperties()
{
// Arrange
var authType = AuthType.credentials;
var authType = AuthType.Credentials;
var token = "test-token-123";
var appId = "myApp";
var url = "https://ipam.example.com";
@@ -21,7 +21,7 @@ public class SessionTests
var session = new Session(authType, token, appId, url, expires, credentials);
// Assert
session.AuthType.Should().Be(AuthType.credentials);
session.AuthType.Should().Be(AuthType.Credentials);
session.Token.Should().Be(token);
session.AppID.Should().Be(appId);
session.URL.Should().Be(url);
@@ -33,7 +33,7 @@ public class SessionTests
public void Constructor_WithTokenAuth_SetsAllProperties()
{
// Arrange
var authType = AuthType.token;
var authType = AuthType.Token;
var token = "static-api-token";
var appId = "apiApp";
var url = "https://ipam.test.com";
@@ -42,7 +42,7 @@ public class SessionTests
var session = new Session(authType, token, appId, url, null, null);
// Assert
session.AuthType.Should().Be(AuthType.token);
session.AuthType.Should().Be(AuthType.Token);
session.Token.Should().Be(token);
session.AppID.Should().Be(appId);
session.URL.Should().Be(url);
@@ -54,7 +54,7 @@ public class SessionTests
public void Token_CanBeModified()
{
// Arrange
var session = new Session(AuthType.credentials, "old-token", "app", "https://test.com", null, null);
var session = new Session(AuthType.Credentials, "old-token", "app", "https://test.com", null, null);
// Act
session.Token = "new-token";
@@ -67,7 +67,7 @@ public class SessionTests
public void Expires_CanBeModified()
{
// Arrange
var session = new Session(AuthType.credentials, "token", "app", "https://test.com", null, null);
var session = new Session(AuthType.Credentials, "token", "app", "https://test.com", null, null);
var newExpiry = new DateTime(2027, 1, 1);
// Act
@@ -78,15 +78,13 @@ public class SessionTests
}
[Fact]
public void ToString_ReturnsDefaultObjectString()
public void Record_Equality_WorksForSameValues()
{
// Arrange
var session = new Session(AuthType.token, "token", "app", "https://test.com", null, null);
var session1 = new Session(AuthType.Token, "token", "app", "https://test.com", null, null);
var session2 = new Session(AuthType.Token, "token", "app", "https://test.com", null, null);
// Act
var result = session.ToString();
// Assert
result.Should().Contain("PS.IPAM.Session");
// Assert - Records use value equality
session1.Should().Be(session2);
}
}

View File

@@ -93,13 +93,13 @@ public class SubnetworkTests
[InlineData("10.0.0.0", 8, "10.0.0.0/8")]
[InlineData("172.16.0.0", 16, "172.16.0.0/16")]
[InlineData("192.168.100.0", 30, "192.168.100.0/30")]
public void GetCIDR_ReturnsCidrNotation(string subnet, int mask, string expectedCidr)
public void CIDR_ReturnsCidrNotation(string subnet, int mask, string expectedCidr)
{
// Arrange
var subnetwork = CreateSubnetwork(subnet, mask);
// Act
var result = subnetwork.GetCIDR();
var result = subnetwork.CIDR;
// Assert
result.Should().Be(expectedCidr);
@@ -119,16 +119,31 @@ public class SubnetworkTests
}
[Fact]
public void ToString_EqualsGetCIDR()
public void ToString_EqualsCIDRProperty()
{
// Arrange
var subnetwork = CreateSubnetwork("172.20.0.0", 12);
// Act & Assert
subnetwork.ToString().Should().Be(subnetwork.GetCIDR());
subnetwork.ToString().Should().Be(subnetwork.CIDR);
}
private static Subnetwork CreateSubnetwork(string subnet, int mask)
[Fact]
public void Record_Equality_WorksCorrectly()
{
// Arrange - use same calculation object for equality comparison
var calculation = new { maxhosts = 254 };
var subnet1 = CreateSubnetwork("192.168.1.0", 24, calculation);
var subnet2 = CreateSubnetwork("192.168.1.0", 24, calculation);
// Assert - records use value equality (except for the Calculation object reference)
subnet1.Id.Should().Be(subnet2.Id);
subnet1.Subnet.Should().Be(subnet2.Subnet);
subnet1.Mask.Should().Be(subnet2.Mask);
subnet1.CIDR.Should().Be(subnet2.CIDR);
}
private static Subnetwork CreateSubnetwork(string subnet, int mask, object? calculation = null)
{
return new Subnetwork(
1, subnet, mask, 1, "", "", "",
@@ -136,7 +151,7 @@ public class SubnetworkTests
"", false, false, false, false,
false, 0, false, false, false, false,
0, 0, 0, null, null, null,
new object(), null
calculation ?? new object(), null
);
}
}

View File

@@ -26,28 +26,13 @@ public class TagTests
tag.Id.Should().Be(id);
tag.Type.Should().Be(type);
tag.ShowTag.Should().Be(showTag);
tag.BGColor.Should().Be(bgColor);
tag.FGColor.Should().Be(fgColor);
tag.Compress.Should().BeTrue();
tag.Locked.Should().BeFalse();
tag.BackgroundColor.Should().Be(bgColor);
tag.ForegroundColor.Should().Be(fgColor);
tag.Compress.Should().Be(compress);
tag.Locked.Should().Be(locked);
tag.UpdateTag.Should().Be(updateTag);
}
[Theory]
[InlineData("Yes", true)]
[InlineData("No", false)]
[InlineData("", false)]
[InlineData("yes", false)] // Case sensitive
[InlineData("true", false)] // Only "Yes" is true
public void StringToBool_ConvertsCorrectly(string input, bool expected)
{
// The StringToBool is private, so we test through the constructor
// Using Compress field which uses StringToBool
var tag = new Tag(1, "Test", false, "", "", input, "No", false);
tag.Compress.Should().Be(expected);
}
[Theory]
[InlineData("Used")]
[InlineData("Available")]
@@ -66,12 +51,13 @@ public class TagTests
}
[Fact]
public void Locked_WithYes_IsTrue()
public void Record_Equality_WorksCorrectly()
{
// Arrange & Act
var tag = new Tag(1, "Test", false, "", "", "No", "Yes", false);
// Arrange
var tag1 = new Tag(1, "Used", true, "#fff", "#000", "Yes", "No", false);
var tag2 = new Tag(1, "Used", true, "#fff", "#000", "Yes", "No", false);
// Assert
tag.Locked.Should().BeTrue();
tag1.Should().Be(tag2);
}
}