namespace PS.IPAM.Tests.Cmdlets;
using System.Net;
using FluentAssertions;
using PS.IPAM;
using PS.IPAM.Cmdlets;
using PS.IPAM.Helpers;
using PS.IPAM.Tests.Mocks;
using Xunit;
///
/// Tests for the GetAddressCmdlet.
/// Note: Full cmdlet testing with parameter sets requires a PowerShell runspace.
/// These tests focus on verifying the cmdlet structure and the underlying RequestHelper functionality.
///
[Collection("Sequential")]
public class GetAddressCmdletTests : IDisposable
{
private MockHttpMessageHandler? _mockHandler;
public GetAddressCmdletTests()
{
// Clean state before each test
SessionManager.CloseSession();
RequestHelper.TestHttpHandler = null;
}
public void Dispose()
{
// Clean up after each test
SessionManager.CloseSession();
RequestHelper.TestHttpHandler = null;
_mockHandler?.ForceDispose();
}
private void SetupSession()
{
SessionManager.CreateSessionWithToken("https://ipam.example.com", "testapp", "test-token");
}
private MockHttpMessageHandler SetupMockHandler()
{
_mockHandler = new MockHttpMessageHandler();
RequestHelper.TestHttpHandler = _mockHandler;
return _mockHandler;
}
[Fact]
public void GetAddressCmdlet_Exists()
{
// Verify the cmdlet class exists and can be instantiated
var cmdlet = new GetAddressCmdlet();
cmdlet.Should().NotBeNull();
}
[Fact]
public void GetAddressCmdlet_IdProperty_Exists()
{
var cmdlet = new GetAddressCmdlet();
cmdlet.Id = 42;
cmdlet.Id.Should().Be(42);
}
[Fact]
public void GetAddressCmdlet_IPProperty_Exists()
{
var cmdlet = new GetAddressCmdlet();
cmdlet.IP = IPAddress.Parse("192.168.1.100");
cmdlet.IP.Should().Be(IPAddress.Parse("192.168.1.100"));
}
[Fact]
public void GetAddressCmdlet_HostNameProperty_Exists()
{
var cmdlet = new GetAddressCmdlet();
cmdlet.HostName = "server01.example.com";
cmdlet.HostName.Should().Be("server01.example.com");
}
[Fact]
public void GetAddressCmdlet_HostBaseProperty_Exists()
{
var cmdlet = new GetAddressCmdlet();
cmdlet.HostBase = "server";
cmdlet.HostBase.Should().Be("server");
}
[Fact]
public void GetAddressCmdlet_TagIdProperty_Exists()
{
var cmdlet = new GetAddressCmdlet();
cmdlet.TagId = 2;
cmdlet.TagId.Should().Be(2);
}
[Fact]
public void GetAddressCmdlet_SubnetIdProperty_Exists()
{
var cmdlet = new GetAddressCmdlet();
cmdlet.SubnetId = 10;
cmdlet.SubnetId.Should().Be(10);
}
[Fact]
public void GetAddressCmdlet_SubnetCIDRProperty_Exists()
{
var cmdlet = new GetAddressCmdlet();
cmdlet.SubnetCIDR = "192.168.1.0/24";
cmdlet.SubnetCIDR.Should().Be("192.168.1.0/24");
}
// Test the underlying RequestHelper functionality that the cmdlet uses
[Fact]
public async Task RequestHelper_GetAddressById_ReturnsAddress()
{
// Arrange
SetupSession();
var handler = SetupMockHandler();
var addressJson = @"{
""id"": 1,
""subnetId"": 10,
""ip"": ""192.168.1.100"",
""hostname"": ""server01"",
""description"": ""Test server""
}";
handler.WithSuccessResponse(addressJson);
// Act
var result = await RequestHelper.InvokeRequest(
"GET",
controllers.addresses,
types.Address,
null,
null,
new[] { "1" }
);
// Assert
result.Should().BeOfType