-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPublishedTest.cs
More file actions
312 lines (272 loc) · 10.4 KB
/
IPublishedTest.cs
File metadata and controls
312 lines (272 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using LabTracker;
using LabTracker.Mqtt;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
namespace LabTracker.Tests;
public class ClientStateTest
{
[Fact]
public void ClientState_ShouldInitializeWithDefaults()
{
// Act
var clientState = new ClientState();
// Assert
Assert.Equal(string.Empty, clientState.ClientId);
Assert.Null(clientState.ApHostname);
Assert.False(clientState.IsConnected);
Assert.Equal(default(DateTime), clientState.LastUpdated);
Assert.Null(clientState.LastPayload);
}
[Fact]
public void ClientState_ShouldSetProperties()
{
// Arrange
var clientId = "test-client";
var apHostname = "test-ap";
var isConnected = true;
var lastUpdated = DateTime.UtcNow;
var lastPayload = "online";
// Act
var clientState = new ClientState
{
ClientId = clientId,
ApHostname = apHostname,
IsConnected = isConnected,
LastUpdated = lastUpdated,
LastPayload = lastPayload
};
// Assert
Assert.Equal(clientId, clientState.ClientId);
Assert.Equal(apHostname, clientState.ApHostname);
Assert.Equal(isConnected, clientState.IsConnected);
Assert.Equal(lastUpdated, clientState.LastUpdated);
Assert.Equal(lastPayload, clientState.LastPayload);
}
[Theory]
[InlineData(true, "connected")]
[InlineData(false, "disconnected")]
public void ClientState_ShouldSupportDifferentConnectionStates(bool connected, string description)
{
// Arrange & Act
var clientState = new ClientState
{
ClientId = $"client-{description}",
IsConnected = connected
};
// Assert
Assert.Equal(connected, clientState.IsConnected);
Assert.Equal($"client-{description}", clientState.ClientId);
}
}
public class MqttPublishedReaderTest
{
private readonly Mock<ILogger<MqttPublishedReader>> _mockLogger;
private readonly Mock<IOptions<Options>> _mockOptions;
private readonly Options _options;
public MqttPublishedReaderTest()
{
_mockLogger = new Mock<ILogger<MqttPublishedReader>>();
_options = new Options
{
Mqtt = new MqttOptions
{
BrokerHost = "test-host",
BrokerPort = 8883,
TopicPrefix = "labtracker",
IncludeApInTopic = true,
ConnectedPayload = "online",
DisconnectedPayload = "offline",
UseTls = false
}
};
_mockOptions = new Mock<IOptions<Options>>();
_mockOptions.Setup(x => x.Value).Returns(_options);
}
[Fact]
public void Constructor_ShouldCreateMqttPublishedReader()
{
// Act
var reader = new MqttPublishedReader(_mockLogger.Object, _mockOptions.Object);
// Assert
Assert.NotNull(reader);
}
[Fact]
public void MqttPublishedReader_ShouldImplementIPublished()
{
// Act
var reader = new MqttPublishedReader(_mockLogger.Object, _mockOptions.Object);
// Assert
Assert.IsAssignableFrom<IPublished>(reader);
}
[Fact]
public async Task ReadCurrentStatesAsync_WhenConnectionFails_ShouldReturnEmptyDictionary()
{
// Arrange - Use invalid host to force connection failure
var failOptions = new Options
{
Mqtt = new MqttOptions
{
BrokerHost = "invalid-host-that-does-not-exist-12345",
BrokerPort = 9999,
TopicPrefix = "labtracker",
IncludeApInTopic = true,
ConnectedPayload = "online"
}
};
var mockFailOptions = new Mock<IOptions<Options>>();
mockFailOptions.Setup(x => x.Value).Returns(failOptions);
var reader = new MqttPublishedReader(_mockLogger.Object, mockFailOptions.Object);
// Act
var result = await reader.ReadCurrentStatesAsync();
// Assert
Assert.NotNull(result);
Assert.Empty(result);
// Verify error was logged
_mockLogger.Verify(
x => x.Log(
LogLevel.Error,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((v, t) => v.ToString()!.Contains("Failed to read current client states from MQTT")),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
}
}
// Test for the IPublished interface contract compliance
public class IPublishedContractTest
{
[Fact]
public void MqttPublishedReader_ShouldImplementIPublished()
{
// Arrange
var mockLogger = new Mock<ILogger<MqttPublishedReader>>();
var mockOptions = new Mock<IOptions<Options>>();
mockOptions.Setup(x => x.Value).Returns(new Options());
// Act
var reader = new MqttPublishedReader(mockLogger.Object, mockOptions.Object);
// Assert
Assert.IsAssignableFrom<IPublished>(reader);
}
[Fact]
public void IPublished_ReadCurrentStatesAsync_ShouldReturnDictionaryOfClientStates()
{
// Arrange
var mockLogger = new Mock<ILogger<MqttPublishedReader>>();
var mockOptions = new Mock<IOptions<Options>>();
mockOptions.Setup(x => x.Value).Returns(new Options());
IPublished publisher = new MqttPublishedReader(mockLogger.Object, mockOptions.Object);
// Act & Assert - Method should exist and be callable
var method = publisher.GetType().GetMethod("ReadCurrentStatesAsync");
Assert.NotNull(method);
Assert.Equal(typeof(Task<Dictionary<string, ClientState>>), method.ReturnType);
}
}
// Integration-style tests for parsing logic (using reflection to test private methods)
public class MqttPublishedReaderParsingTest
{
private readonly MqttPublishedReader _reader;
private readonly Options _options;
public MqttPublishedReaderParsingTest()
{
var mockLogger = new Mock<ILogger<MqttPublishedReader>>();
_options = new Options
{
Mqtt = new MqttOptions
{
TopicPrefix = "labtracker",
IncludeApInTopic = true,
ConnectedPayload = "online",
DisconnectedPayload = "offline"
}
};
var mockOptions = new Mock<IOptions<Options>>();
mockOptions.Setup(x => x.Value).Returns(_options);
_reader = new MqttPublishedReader(mockLogger.Object, mockOptions.Object);
}
[Theory]
[InlineData("labtracker/ap1/client1", "client1", "ap1")]
[InlineData("labtracker/office-ap/laptop", "laptop", "office-ap")]
[InlineData("labtracker/wifi-main/AA:BB:CC:DD:EE:FF", "AA:BB:CC:DD:EE:FF", "wifi-main")]
public void ParseTopic_WithApInTopic_ShouldParseCorrectly(string topic, string expectedClient, string expectedAp)
{
// Act
var parseTopicMethod = typeof(MqttPublishedReader).GetMethod("ParseTopic",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var result = parseTopicMethod?.Invoke(_reader, new object[] { topic });
// Assert
Assert.NotNull(result);
var (clientId, apHostname) = ((string?, string?))result;
Assert.Equal(expectedClient, clientId);
Assert.Equal(expectedAp, apHostname);
}
[Theory]
[InlineData("labtracker/client1", "client1")]
[InlineData("labtracker/laptop", "laptop")]
[InlineData("labtracker/AA:BB:CC:DD:EE:FF", "AA:BB:CC:DD:EE:FF")]
public void ParseTopic_WithoutApInTopic_ShouldParseCorrectly(string topic, string expectedClient)
{
// Arrange - Change options to not include AP in topic
_options.Mqtt.IncludeApInTopic = false;
// Act
var parseTopicMethod = typeof(MqttPublishedReader).GetMethod("ParseTopic",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var result = parseTopicMethod?.Invoke(_reader, new object[] { topic });
// Assert
Assert.NotNull(result);
var (clientId, apHostname) = ((string?, string?))result;
Assert.Equal(expectedClient, clientId);
Assert.Null(apHostname); // Should be null when not including AP in topic
}
[Theory]
[InlineData("wrong/ap1/client1")]
[InlineData("labtracker")]
[InlineData("labtracker/")]
[InlineData("")]
public void ParseTopic_WithInvalidTopic_ShouldReturnNulls(string topic)
{
// Act
var parseTopicMethod = typeof(MqttPublishedReader).GetMethod("ParseTopic",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var result = parseTopicMethod?.Invoke(_reader, new object[] { topic });
// Assert
Assert.NotNull(result);
var (clientId, apHostname) = ((string?, string?))result;
Assert.Null(clientId);
Assert.Null(apHostname);
}
[Theory]
[InlineData("online", true)]
[InlineData("ONLINE", true)]
[InlineData("OnLiNe", true)]
[InlineData("offline", false)]
[InlineData("OFFLINE", false)]
[InlineData("OfFlInE", false)]
[InlineData("disconnected", false)]
[InlineData("", false)]
public void IsConnectedPayload_ShouldIdentifyConnectionState(string payload, bool expected)
{
// Act
var isConnectedMethod = typeof(MqttPublishedReader).GetMethod("IsConnectedPayload",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var result = isConnectedMethod?.Invoke(_reader, new object[] { payload });
// Assert
Assert.NotNull(result);
Assert.Equal(expected, (bool)result);
}
[Theory]
[InlineData("client1", "ap1", "ap1:client1")]
[InlineData("laptop", "office", "office:laptop")]
[InlineData("AA:BB:CC:DD:EE:FF", "wifi-main", "wifi-main:AA:BB:CC:DD:EE:FF")]
[InlineData("client1", null, "unknown:client1")]
[InlineData("client1", Options.AllApsAggregate, Options.AllApsAggregate + ":client1")]
public void CreateClientStateKey_ShouldFormatCorrectly(string clientId, string? apHostname, string expected)
{
// Act
var result = PublishedUtils.CreateClientStateKey(clientId, apHostname);
// Assert
Assert.NotNull(result);
Assert.Equal(expected, result);
}
}