DescribeSwitchUseCaseTests.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using NSubstitute;
  2. using RackPeek.Domain.Helpers;
  3. using RackPeek.Domain.Resources.Hardware;
  4. using RackPeek.Domain.Resources.Hardware.Switches;
  5. using RackPeek.Domain.Resources.Models;
  6. namespace Tests.HardwareResources.Switches;
  7. public class DescribeSwitchUseCaseTests
  8. {
  9. [Fact]
  10. public async Task ExecuteAsync_Returns_null_when_switch_not_found()
  11. {
  12. // Arrange
  13. var repo = Substitute.For<IHardwareRepository>();
  14. repo.GetByNameAsync("sw01").Returns((Hardware?)null);
  15. var sut = new DescribeSwitchUseCase(repo);
  16. // Act
  17. await Assert.ThrowsAsync<NotFoundException>(() => sut.ExecuteAsync("sw01"));
  18. }
  19. [Fact]
  20. public async Task ExecuteAsync_Returns_defaults_when_switch_has_no_ports()
  21. {
  22. // Arrange
  23. var repo = Substitute.For<IHardwareRepository>();
  24. repo.GetByNameAsync("sw01").Returns(new Switch
  25. {
  26. Name = "sw01",
  27. Model = "TestModel",
  28. Managed = true,
  29. Poe = false,
  30. Ports = null
  31. });
  32. var sut = new DescribeSwitchUseCase(repo);
  33. // Act
  34. var result = await sut.ExecuteAsync("sw01");
  35. // Assert
  36. Assert.NotNull(result);
  37. Assert.Equal("sw01", result.Name);
  38. Assert.Equal("TestModel", result.Model);
  39. Assert.True(result.Managed);
  40. Assert.False(result.Poe);
  41. Assert.Equal(0, result.TotalPorts);
  42. Assert.Equal(0, result.TotalSpeedGb);
  43. Assert.Equal(string.Empty, result.PortSummary);
  44. }
  45. [Fact]
  46. public async Task ExecuteAsync_Calculates_totals_and_summary_from_ports()
  47. {
  48. // Arrange
  49. var repo = Substitute.For<IHardwareRepository>();
  50. repo.GetByNameAsync("sw01").Returns(new Switch
  51. {
  52. Name = "sw01",
  53. Model = "CoreSwitch",
  54. Managed = true,
  55. Poe = true,
  56. Ports = new List<Port>
  57. {
  58. new() { Type = "RJ45", Speed = 1, Count = 24 },
  59. new() { Type = "SFP+", Speed = 10, Count = 4 }
  60. }
  61. });
  62. var sut = new DescribeSwitchUseCase(repo);
  63. // Act
  64. var result = await sut.ExecuteAsync("sw01");
  65. // Assert
  66. Assert.NotNull(result);
  67. Assert.Equal("sw01", result.Name);
  68. Assert.Equal("CoreSwitch", result.Model);
  69. Assert.True(result.Managed);
  70. Assert.True(result.Poe);
  71. Assert.Equal(28, result.TotalPorts); // 24 + 4
  72. Assert.Equal(64, result.TotalSpeedGb); // (24 * 1) + (4 * 10)
  73. Assert.Equal(
  74. "RJ45: 24 ports (24 Gb total), SFP+: 4 ports (40 Gb total)",
  75. result.PortSummary
  76. );
  77. }
  78. }