|
| 1 | +using System.Text.Json; |
1 | 2 | using AzureEventGridSimulator.Domain.Entities; |
2 | 3 | using Shouldly; |
3 | 4 | using Xunit; |
@@ -258,36 +259,55 @@ string metadataVersion |
258 | 259 | public void GivenEventWithNonNullTopic_WhenValidated_ThenExceptionThrown() |
259 | 260 | { |
260 | 261 | // Topic should be null/empty when publishing - it's set by the service |
261 | | - var eventGridEvent = new EventGridEvent |
262 | | - { |
263 | | - Id = "test-id-123", |
264 | | - Subject = "/test/subject", |
265 | | - EventType = "Test.EventType", |
266 | | - EventTime = "2025-01-15T10:30:00Z", |
267 | | - Topic = "/subscriptions/some/topic", |
268 | | - }; |
269 | | - |
270 | | - var exception = Should.Throw<InvalidOperationException>(() => eventGridEvent.Validate()); |
| 262 | + // Use JSON deserialization to simulate a publisher sending a Topic value |
| 263 | + var json = """ |
| 264 | + { |
| 265 | + "id": "test-id-123", |
| 266 | + "subject": "/test/subject", |
| 267 | + "eventType": "Test.EventType", |
| 268 | + "eventTime": "2025-01-15T10:30:00Z", |
| 269 | + "topic": "/subscriptions/some/topic" |
| 270 | + } |
| 271 | + """; |
| 272 | + var eventGridEvent = JsonSerializer.Deserialize<EventGridEvent>(json); |
| 273 | + |
| 274 | + var exception = Should.Throw<InvalidOperationException>(() => eventGridEvent!.Validate()); |
271 | 275 | exception.Message.ShouldContain("Topic"); |
272 | 276 | } |
273 | 277 |
|
274 | | - [Theory] |
275 | | - [InlineData(null)] |
276 | | - [InlineData("")] |
277 | | - public void GivenEventWithNullOrEmptyTopic_WhenValidated_ThenNoExceptionThrown(string topic) |
| 278 | + [Fact] |
| 279 | + public void GivenEventWithNullTopic_WhenValidated_ThenNoExceptionThrown() |
278 | 280 | { |
| 281 | + // Topic defaults to null when not set |
279 | 282 | var eventGridEvent = new EventGridEvent |
280 | 283 | { |
281 | 284 | Id = "test-id-123", |
282 | 285 | Subject = "/test/subject", |
283 | 286 | EventType = "Test.EventType", |
284 | 287 | EventTime = "2025-01-15T10:30:00Z", |
285 | | - Topic = topic, |
286 | 288 | }; |
287 | 289 |
|
288 | 290 | Should.NotThrow(() => eventGridEvent.Validate()); |
289 | 291 | } |
290 | 292 |
|
| 293 | + [Fact] |
| 294 | + public void GivenEventWithEmptyTopic_WhenValidated_ThenNoExceptionThrown() |
| 295 | + { |
| 296 | + // Use JSON deserialization to set empty Topic |
| 297 | + var json = """ |
| 298 | + { |
| 299 | + "id": "test-id-123", |
| 300 | + "subject": "/test/subject", |
| 301 | + "eventType": "Test.EventType", |
| 302 | + "eventTime": "2025-01-15T10:30:00Z", |
| 303 | + "topic": "" |
| 304 | + } |
| 305 | + """; |
| 306 | + var eventGridEvent = JsonSerializer.Deserialize<EventGridEvent>(json); |
| 307 | + |
| 308 | + Should.NotThrow(() => eventGridEvent!.Validate()); |
| 309 | + } |
| 310 | + |
291 | 311 | [Fact] |
292 | 312 | public void GivenEventWithNullData_WhenValidated_ThenNoExceptionThrown() |
293 | 313 | { |
|
0 commit comments