Skip to content

Commit 757646a

Browse files
committed
updated tests
1 parent 5534029 commit 757646a

File tree

3 files changed

+71
-67
lines changed

3 files changed

+71
-67
lines changed

ThingConnect.Pulse.Tests/CidrExpansionTests.cs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using Microsoft.Extensions.Logging;
2-
using NUnit.Framework;
31
using System.Net;
42
using System.Text.RegularExpressions;
3+
using NUnit.Framework;
54

65
namespace ThingConnect.Pulse.Tests;
76

@@ -60,33 +59,33 @@ public void TestCidrExpansion_With24Subnet_ShouldReturn254IPs()
6059
{
6160
// Arrange
6261
string cidr = "10.18.8.0/24";
63-
62+
6463
// Act
6564
var expandedIPs = ExpandCidr(cidr).ToList();
66-
65+
6766
// Assert
6867
Console.WriteLine($"Testing CIDR expansion for: {cidr}");
6968
Console.WriteLine($"Expected: 254 IP addresses from 10.18.8.1 to 10.18.8.254");
7069
Console.WriteLine($"Actual count: {expandedIPs.Count}");
71-
70+
7271
Assert.That(expandedIPs.Count, Is.EqualTo(254), $"CIDR /24 should expand to 254 IPs but got {expandedIPs.Count}");
73-
72+
7473
if (expandedIPs.Count > 0)
7574
{
7675
Console.WriteLine($"First IP: {expandedIPs.First()}");
7776
Console.WriteLine($"Last IP: {expandedIPs.Last()}");
78-
77+
7978
Assert.That(expandedIPs.First(), Is.EqualTo("10.18.8.1"), "First IP should be .1");
8079
Assert.That(expandedIPs.Last(), Is.EqualTo("10.18.8.254"), "Last IP should be .254");
81-
80+
8281
// Print first and last 5 IPs for debugging
8382
Console.WriteLine("First 5 IPs:");
84-
foreach (var ip in expandedIPs.Take(5))
83+
foreach (string? ip in expandedIPs.Take(5))
8584
{
8685
Console.WriteLine($" {ip}");
8786
}
8887
Console.WriteLine("Last 5 IPs:");
89-
foreach (var ip in expandedIPs.TakeLast(5))
88+
foreach (string? ip in expandedIPs.TakeLast(5))
9089
{
9190
Console.WriteLine($" {ip}");
9291
}
@@ -98,20 +97,20 @@ public void TestCidrExpansion_With30Subnet_ShouldReturn2IPs()
9897
{
9998
// Arrange
10099
string cidr = "192.168.1.0/30";
101-
100+
102101
// Act
103102
var expandedIPs = ExpandCidr(cidr).ToList();
104-
103+
105104
// Assert
106105
Console.WriteLine($"Testing CIDR expansion for: {cidr}");
107106
Console.WriteLine($"Actual count: {expandedIPs.Count}");
108-
107+
109108
Assert.That(expandedIPs.Count, Is.EqualTo(2), "CIDR /30 should expand to 2 IPs");
110109
Assert.That(expandedIPs[0], Is.EqualTo("192.168.1.1"));
111110
Assert.That(expandedIPs[1], Is.EqualTo("192.168.1.2"));
112-
111+
113112
Console.WriteLine("All IPs:");
114-
foreach (var ip in expandedIPs)
113+
foreach (string? ip in expandedIPs)
115114
{
116115
Console.WriteLine($" {ip}");
117116
}
@@ -122,14 +121,14 @@ public void TestCidrExpansion_WithInvalidFormat_ShouldReturnEmpty()
122121
{
123122
// Arrange
124123
string invalidCidr = "invalid-cidr";
125-
124+
126125
// Act
127126
var expandedIPs = ExpandCidr(invalidCidr).ToList();
128-
127+
129128
// Assert
130129
Console.WriteLine($"Testing invalid CIDR: {invalidCidr}");
131130
Console.WriteLine($"Actual count: {expandedIPs.Count}");
132-
131+
133132
Assert.That(expandedIPs.Count, Is.EqualTo(0), "Invalid CIDR should return empty list");
134133
}
135134

@@ -138,33 +137,33 @@ public void TestCidrExpansion_UserScenario_10_18_8_0_24()
138137
{
139138
// Arrange - This is the exact CIDR from the user's config
140139
string cidr = "10.18.8.0/24";
141-
140+
142141
// Act
143142
var expandedIPs = ExpandCidr(cidr).ToList();
144-
143+
145144
// Assert
146145
Console.WriteLine($"Testing USER'S CIDR: {cidr}");
147146
Console.WriteLine($"Expected: 254 IP addresses from 10.18.8.1 to 10.18.8.254");
148147
Console.WriteLine($"Actual count: {expandedIPs.Count}");
149-
148+
150149
Assert.That(expandedIPs.Count, Is.EqualTo(254), $"User's CIDR {cidr} should expand to 254 IPs but got {expandedIPs.Count}");
151-
150+
152151
if (expandedIPs.Count > 0)
153152
{
154153
Console.WriteLine($"First IP: {expandedIPs.First()}");
155154
Console.WriteLine($"Last IP: {expandedIPs.Last()}");
156-
155+
157156
Assert.That(expandedIPs.First(), Is.EqualTo("10.18.8.1"), "First IP should be .1");
158157
Assert.That(expandedIPs.Last(), Is.EqualTo("10.18.8.254"), "Last IP should be .254");
159-
158+
160159
// Print first and last 3 IPs for debugging
161160
Console.WriteLine("First 3 IPs:");
162-
foreach (var ip in expandedIPs.Take(3))
161+
foreach (string? ip in expandedIPs.Take(3))
163162
{
164163
Console.WriteLine($" {ip}");
165164
}
166165
Console.WriteLine("Last 3 IPs:");
167-
foreach (var ip in expandedIPs.TakeLast(3))
166+
foreach (string? ip in expandedIPs.TakeLast(3))
168167
{
169168
Console.WriteLine($" {ip}");
170169
}
Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System;
2-
using System.IO;
3-
using System.Threading.Tasks;
4-
using NUnit.Framework;
51
using NJsonSchema;
2+
using NUnit.Framework;
63
using YamlDotNet.Serialization;
74
using YamlDotNet.Serialization.NamingConventions;
85

@@ -21,15 +18,15 @@ public async Task OneTimeSetUp()
2118
// Load schema once for all tests
2219
string schemaPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "config.schema.json");
2320
Assert.That(File.Exists(schemaPath), Is.True, $"Schema file not found at: {schemaPath}");
24-
21+
2522
string schemaJson = await File.ReadAllTextAsync(schemaPath);
2623
_schema = await JsonSchema.FromJsonAsync(schemaJson);
27-
24+
2825
// Setup YAML processors
2926
_yamlDeserializer = new DeserializerBuilder()
3027
.WithNamingConvention(UnderscoredNamingConvention.Instance)
3128
.Build();
32-
29+
3330
_yamlSerializer = new SerializerBuilder()
3431
.WithNamingConvention(UnderscoredNamingConvention.Instance)
3532
.JsonCompatible()
@@ -42,16 +39,16 @@ public async Task TestConfigurationValidation_WithValidYaml_ShouldPassValidation
4239
// Arrange
4340
string yamlPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "test-config.yaml");
4441
Assert.That(File.Exists(yamlPath), Is.True, $"Test YAML file not found at: {yamlPath}");
45-
42+
4643
string yamlContent = await File.ReadAllTextAsync(yamlPath);
47-
44+
4845
// Act
49-
var config = _yamlDeserializer.Deserialize<object>(yamlContent);
46+
object config = _yamlDeserializer.Deserialize<object>(yamlContent);
5047
string configJson = _yamlSerializer.Serialize(config);
51-
var validationResults = _schema!.Validate(configJson);
52-
48+
ICollection<NJsonSchema.Validation.ValidationError> validationResults = _schema!.Validate(configJson);
49+
5350
// Assert
54-
Assert.That(validationResults.Count, Is.EqualTo(0),
51+
Assert.That(validationResults.Count, Is.EqualTo(0),
5552
$"Validation should pass but found {validationResults.Count} errors: {string.Join(", ", validationResults)}");
5653
}
5754

@@ -60,10 +57,10 @@ public void TestCidrExpansion_With24Subnet_ShouldReturn254IPs()
6057
{
6158
// Arrange
6259
string cidr = "10.18.8.0/24";
63-
60+
6461
// Act
65-
var expandedIPs = ExpandCidrForTesting(cidr);
66-
62+
List<string> expandedIPs = ExpandCidrForTesting(cidr);
63+
6764
// Assert
6865
Assert.That(expandedIPs.Count, Is.EqualTo(254), "CIDR /24 should expand to 254 IPs (skip .0 and .255)");
6966
Assert.That(expandedIPs[0], Is.EqualTo("10.18.8.1"), "First IP should be .1");
@@ -75,10 +72,10 @@ public void TestCidrExpansion_With30Subnet_ShouldReturn2IPs()
7572
{
7673
// Arrange
7774
string cidr = "192.168.1.0/30";
78-
75+
7976
// Act
80-
var expandedIPs = ExpandCidrForTesting(cidr);
81-
77+
List<string> expandedIPs = ExpandCidrForTesting(cidr);
78+
8279
// Assert
8380
Assert.That(expandedIPs.Count, Is.EqualTo(2), "CIDR /30 should expand to 2 IPs");
8481
Assert.That(expandedIPs[0], Is.EqualTo("192.168.1.1"));
@@ -90,9 +87,9 @@ public void TestCidrExpansion_WithInvalidFormat_ShouldReturnEmpty()
9087
{
9188
// Arrange
9289
string invalidCidr = "invalid-cidr";
93-
90+
9491
// Act & Assert
95-
var expandedIPs = ExpandCidrForTesting(invalidCidr);
92+
List<string> expandedIPs = ExpandCidrForTesting(invalidCidr);
9693
Assert.That(expandedIPs.Count, Is.EqualTo(0), "Invalid CIDR should return empty list");
9794
}
9895

@@ -103,21 +100,21 @@ public async Task TestConfigurationValidation_WithMinimalYaml_ShouldFailWithOldS
103100
// Arrange
104101
string yamlPath = Path.Combine(TestContext.CurrentContext.TestDirectory, "test-config-minimal.yaml");
105102
Assert.That(File.Exists(yamlPath), Is.True, $"Minimal test YAML file not found at: {yamlPath}");
106-
103+
107104
string yamlContent = await File.ReadAllTextAsync(yamlPath);
108-
105+
109106
// Act
110-
var config = _yamlDeserializer.Deserialize<object>(yamlContent);
107+
object config = _yamlDeserializer.Deserialize<object>(yamlContent);
111108
string configJson = _yamlSerializer.Serialize(config);
112-
109+
113110
// Show what the JSON looks like with null values
114111
TestContext.WriteLine($"Generated JSON with nulls: {configJson}");
115-
116-
var validationResults = _schema!.Validate(configJson);
117-
112+
113+
ICollection<NJsonSchema.Validation.ValidationError> validationResults = _schema!.Validate(configJson);
114+
118115
// Assert - With the current fixed schema, this should now pass
119116
// But originally this would have failed due to null values
120-
Assert.That(validationResults.Count, Is.EqualTo(0),
117+
Assert.That(validationResults.Count, Is.EqualTo(0),
121118
"With fixed schema, even minimal config with nulls should validate");
122119
}
123120

@@ -128,30 +125,30 @@ public async Task TestConfigurationValidation_WithMinimalYaml_ShouldFailWithOldS
128125
private List<string> ExpandCidrForTesting(string cidr)
129126
{
130127
var result = new List<string>();
131-
128+
132129
try
133130
{
134-
var parts = cidr.Split('/');
131+
string[] parts = cidr.Split('/');
135132
if (parts.Length != 2) return result;
136-
133+
137134
string baseIp = parts[0];
138135
if (!int.TryParse(parts[1], out int prefixLength)) return result;
139-
136+
140137
if (prefixLength < 0 || prefixLength > 32) return result;
141-
142-
if (!System.Net.IPAddress.TryParse(baseIp, out var ipAddress)) return result;
143-
138+
139+
if (!System.Net.IPAddress.TryParse(baseIp, out System.Net.IPAddress? ipAddress)) return result;
140+
144141
byte[] addressBytes = ipAddress.GetAddressBytes();
145142
uint addressInt = BitConverter.ToUInt32(addressBytes.Reverse().ToArray(), 0);
146-
143+
147144
int hostBits = 32 - prefixLength;
148145
uint hostCount = (uint)(1 << hostBits);
149146
uint networkAddress = addressInt & (0xFFFFFFFF << hostBits);
150-
147+
151148
// Skip network and broadcast addresses for practical use
152149
uint startAddress = networkAddress + 1;
153150
uint endAddress = networkAddress + hostCount - 1;
154-
151+
155152
for (uint address = startAddress; address < endAddress && address > networkAddress; address++)
156153
{
157154
byte[] bytes = BitConverter.GetBytes(address).Reverse().ToArray();
@@ -163,7 +160,7 @@ private List<string> ExpandCidrForTesting(string cidr)
163160
{
164161
// Return empty list on any error
165162
}
166-
163+
167164
return result;
168165
}
169166
}

thingconnect.pulse.client/src/components/config/ConfigurationEditor.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,15 @@ export function ConfigurationEditor({ onConfigurationApplied }: ConfigurationEdi
370370
/>
371371
)}
372372
{applyResult && (
373-
<Alert flex='1' status='success' title='Configuration applied successfully' />
373+
<Alert
374+
flex='1'
375+
status='success'
376+
title={
377+
applyResult.warnings?.length ?
378+
applyResult.warnings[0] :
379+
`Configuration applied: ${applyResult.added} added, ${applyResult.updated} updated, ${applyResult.removed} removed`
380+
}
381+
/>
374382
)}
375383
</HStack>
376384
<HStack gap={2} mb='2'>

0 commit comments

Comments
 (0)