|
| 1 | +using AzureEventGridSimulator.Domain.Entities; |
| 2 | +using AzureEventGridSimulator.Domain.Entities.Dashboard; |
| 3 | +using AzureEventGridSimulator.Domain.Services.Dashboard; |
| 4 | +using AzureEventGridSimulator.Infrastructure.Settings; |
| 5 | +using AzureEventGridSimulator.Infrastructure.Settings.Subscribers; |
| 6 | +using NSubstitute; |
| 7 | +using Shouldly; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace AzureEventGridSimulator.Tests.Domain.Services.Dashboard; |
| 11 | + |
| 12 | +[Trait("Category", "unit")] |
| 13 | +public class EventHistoryServiceTests |
| 14 | +{ |
| 15 | + private readonly ILogger<EventHistoryService> _logger; |
| 16 | + private readonly EventHistoryService _service; |
| 17 | + private readonly SimulatorSettings _settings; |
| 18 | + private readonly EventHistoryStore _store; |
| 19 | + |
| 20 | + public EventHistoryServiceTests() |
| 21 | + { |
| 22 | + _store = new EventHistoryStore(); |
| 23 | + _settings = new SimulatorSettings |
| 24 | + { |
| 25 | + Topics = |
| 26 | + [ |
| 27 | + new TopicSettings |
| 28 | + { |
| 29 | + Name = "topic-1", |
| 30 | + Port = 60101, |
| 31 | + Key = "key1", |
| 32 | + }, |
| 33 | + new TopicSettings |
| 34 | + { |
| 35 | + Name = "topic-2", |
| 36 | + Port = 60102, |
| 37 | + Key = "key2", |
| 38 | + Disabled = true, |
| 39 | + }, |
| 40 | + ], |
| 41 | + }; |
| 42 | + _logger = Substitute.For<ILogger<EventHistoryService>>(); |
| 43 | + _service = new EventHistoryService(_store, _settings, _logger); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void RecordEventReceived_AddsEventToStore() |
| 48 | + { |
| 49 | + var evt = CreateTestEvent("event-1"); |
| 50 | + var topic = new TopicSettings |
| 51 | + { |
| 52 | + Name = "test-topic", |
| 53 | + Port = 60101, |
| 54 | + Key = "key", |
| 55 | + }; |
| 56 | + |
| 57 | + _service.RecordEventReceived(evt, topic, EventSchema.EventGridSchema); |
| 58 | + |
| 59 | + _store.Count.ShouldBe(1); |
| 60 | + var recorded = _store.Get("event-1"); |
| 61 | + recorded.ShouldNotBeNull(); |
| 62 | + recorded.TopicName.ShouldBe("test-topic"); |
| 63 | + recorded.TopicPort.ShouldBe(60101); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void RecordDeliveryQueued_AddsDeliveryToEvent() |
| 68 | + { |
| 69 | + var evt = CreateTestEvent("event-1"); |
| 70 | + var topic = new TopicSettings |
| 71 | + { |
| 72 | + Name = "test-topic", |
| 73 | + Port = 60101, |
| 74 | + Key = "key", |
| 75 | + }; |
| 76 | + _service.RecordEventReceived(evt, topic, EventSchema.EventGridSchema); |
| 77 | + |
| 78 | + var subscriber = new HttpSubscriberSettings |
| 79 | + { |
| 80 | + Name = "http-subscriber", |
| 81 | + Endpoint = "https://example.com/webhook", |
| 82 | + }; |
| 83 | + |
| 84 | + _service.RecordDeliveryQueued("event-1", subscriber); |
| 85 | + |
| 86 | + var recorded = _store.Get("event-1"); |
| 87 | + recorded.ShouldNotBeNull(); |
| 88 | + var deliveries = recorded.GetDeliveries(); |
| 89 | + deliveries.Count.ShouldBe(1); |
| 90 | + deliveries[0].SubscriberName.ShouldBe("http-subscriber"); |
| 91 | + deliveries[0].Status.ShouldBe(DeliveryStatus.Pending); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void RecordDeliveryAttempt_UpdatesDeliveryStatus() |
| 96 | + { |
| 97 | + // Setup |
| 98 | + var evt = CreateTestEvent("event-1"); |
| 99 | + var topic = new TopicSettings |
| 100 | + { |
| 101 | + Name = "test-topic", |
| 102 | + Port = 60101, |
| 103 | + Key = "key", |
| 104 | + }; |
| 105 | + _service.RecordEventReceived(evt, topic, EventSchema.EventGridSchema); |
| 106 | + |
| 107 | + var subscriber = new HttpSubscriberSettings |
| 108 | + { |
| 109 | + Name = "http-subscriber", |
| 110 | + Endpoint = "https://example.com/webhook", |
| 111 | + }; |
| 112 | + _service.RecordDeliveryQueued("event-1", subscriber); |
| 113 | + |
| 114 | + // Act |
| 115 | + var attempt = new DeliveryAttempt |
| 116 | + { |
| 117 | + AttemptNumber = 1, |
| 118 | + AttemptTime = DateTime.UtcNow, |
| 119 | + Outcome = DeliveryOutcome.Success, |
| 120 | + HttpStatusCode = 200, |
| 121 | + }; |
| 122 | + _service.RecordDeliveryAttempt("event-1", "http-subscriber", attempt); |
| 123 | + |
| 124 | + // Assert |
| 125 | + var recorded = _store.Get("event-1"); |
| 126 | + recorded.ShouldNotBeNull(); |
| 127 | + var deliveries = recorded.GetDeliveries(); |
| 128 | + deliveries[0].Status.ShouldBe(DeliveryStatus.Delivered); |
| 129 | + deliveries[0].Attempts.Count.ShouldBe(1); |
| 130 | + deliveries[0].Attempts[0].HttpStatusCode.ShouldBe(200); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void RecordDeliveryAttempt_NonExistingEvent_DoesNotThrow() |
| 135 | + { |
| 136 | + var attempt = new DeliveryAttempt |
| 137 | + { |
| 138 | + AttemptNumber = 1, |
| 139 | + AttemptTime = DateTime.UtcNow, |
| 140 | + Outcome = DeliveryOutcome.Success, |
| 141 | + }; |
| 142 | + |
| 143 | + Should.NotThrow(() => |
| 144 | + _service.RecordDeliveryAttempt("non-existing", "subscriber", attempt) |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + public void RecordDeliveryCompleted_UpdatesStatusAndCompletedTime() |
| 150 | + { |
| 151 | + // Setup |
| 152 | + var evt = CreateTestEvent("event-1"); |
| 153 | + var topic = new TopicSettings |
| 154 | + { |
| 155 | + Name = "test-topic", |
| 156 | + Port = 60101, |
| 157 | + Key = "key", |
| 158 | + }; |
| 159 | + _service.RecordEventReceived(evt, topic, EventSchema.EventGridSchema); |
| 160 | + |
| 161 | + var subscriber = new HttpSubscriberSettings |
| 162 | + { |
| 163 | + Name = "http-subscriber", |
| 164 | + Endpoint = "https://example.com/webhook", |
| 165 | + }; |
| 166 | + _service.RecordDeliveryQueued("event-1", subscriber); |
| 167 | + |
| 168 | + // Act |
| 169 | + var completedAt = DateTimeOffset.UtcNow; |
| 170 | + _service.RecordDeliveryCompleted( |
| 171 | + "event-1", |
| 172 | + "http-subscriber", |
| 173 | + DeliveryStatus.DeadLettered, |
| 174 | + completedAt |
| 175 | + ); |
| 176 | + |
| 177 | + // Assert |
| 178 | + var recorded = _store.Get("event-1"); |
| 179 | + recorded.ShouldNotBeNull(); |
| 180 | + var deliveries = recorded.GetDeliveries(); |
| 181 | + deliveries[0].Status.ShouldBe(DeliveryStatus.DeadLettered); |
| 182 | + deliveries[0].CompletedAt.ShouldBe(completedAt); |
| 183 | + } |
| 184 | + |
| 185 | + [Fact] |
| 186 | + public void GetRecentEvents_ReturnsAllEvents() |
| 187 | + { |
| 188 | + var evt1 = CreateTestEvent("event-1"); |
| 189 | + var evt2 = CreateTestEvent("event-2"); |
| 190 | + var topic = new TopicSettings |
| 191 | + { |
| 192 | + Name = "test-topic", |
| 193 | + Port = 60101, |
| 194 | + Key = "key", |
| 195 | + }; |
| 196 | + |
| 197 | + _service.RecordEventReceived(evt1, topic, EventSchema.EventGridSchema); |
| 198 | + _service.RecordEventReceived(evt2, topic, EventSchema.EventGridSchema); |
| 199 | + |
| 200 | + var recent = _service.GetRecentEvents(); |
| 201 | + |
| 202 | + recent.Count.ShouldBe(2); |
| 203 | + } |
| 204 | + |
| 205 | + [Fact] |
| 206 | + public void GetRecentEvents_WithTopicFilter_ReturnsFilteredEvents() |
| 207 | + { |
| 208 | + var evt1 = CreateTestEvent("event-1"); |
| 209 | + var evt2 = CreateTestEvent("event-2"); |
| 210 | + var topic1 = new TopicSettings |
| 211 | + { |
| 212 | + Name = "topic-a", |
| 213 | + Port = 60101, |
| 214 | + Key = "key", |
| 215 | + }; |
| 216 | + var topic2 = new TopicSettings |
| 217 | + { |
| 218 | + Name = "topic-b", |
| 219 | + Port = 60102, |
| 220 | + Key = "key", |
| 221 | + }; |
| 222 | + |
| 223 | + _service.RecordEventReceived(evt1, topic1, EventSchema.EventGridSchema); |
| 224 | + _service.RecordEventReceived(evt2, topic2, EventSchema.EventGridSchema); |
| 225 | + |
| 226 | + var filtered = _service.GetRecentEvents("topic-a"); |
| 227 | + |
| 228 | + filtered.Count.ShouldBe(1); |
| 229 | + filtered[0].TopicName.ShouldBe("topic-a"); |
| 230 | + } |
| 231 | + |
| 232 | + [Fact] |
| 233 | + public void GetEvent_ExistingEvent_ReturnsEvent() |
| 234 | + { |
| 235 | + var evt = CreateTestEvent("event-1"); |
| 236 | + var topic = new TopicSettings |
| 237 | + { |
| 238 | + Name = "test-topic", |
| 239 | + Port = 60101, |
| 240 | + Key = "key", |
| 241 | + }; |
| 242 | + _service.RecordEventReceived(evt, topic, EventSchema.EventGridSchema); |
| 243 | + |
| 244 | + var result = _service.GetEvent("event-1"); |
| 245 | + |
| 246 | + result.ShouldNotBeNull(); |
| 247 | + result.Id.ShouldBe("event-1"); |
| 248 | + } |
| 249 | + |
| 250 | + [Fact] |
| 251 | + public void GetEvent_NonExistingEvent_ReturnsNull() |
| 252 | + { |
| 253 | + var result = _service.GetEvent("non-existing"); |
| 254 | + |
| 255 | + result.ShouldBeNull(); |
| 256 | + } |
| 257 | + |
| 258 | + [Fact] |
| 259 | + public void GetStats_ReturnsStatsWithActiveTopicsCount() |
| 260 | + { |
| 261 | + var evt = CreateTestEvent("event-1"); |
| 262 | + var topic = new TopicSettings |
| 263 | + { |
| 264 | + Name = "test-topic", |
| 265 | + Port = 60101, |
| 266 | + Key = "key", |
| 267 | + }; |
| 268 | + _service.RecordEventReceived(evt, topic, EventSchema.EventGridSchema); |
| 269 | + |
| 270 | + var stats = _service.GetStats(); |
| 271 | + |
| 272 | + stats.TotalEventsReceived.ShouldBe(1); |
| 273 | + stats.EventsInHistory.ShouldBe(1); |
| 274 | + // One topic is enabled, one is disabled |
| 275 | + stats.TopicsActive.ShouldBe(1); |
| 276 | + } |
| 277 | + |
| 278 | + private static SimulatorEvent CreateTestEvent(string id) |
| 279 | + { |
| 280 | + return SimulatorEvent.FromEventGridEvent( |
| 281 | + new EventGridEvent |
| 282 | + { |
| 283 | + Id = id, |
| 284 | + Subject = "/test/subject", |
| 285 | + EventType = "Test.EventType", |
| 286 | + EventTime = DateTime.UtcNow.ToString("o"), |
| 287 | + DataVersion = "1.0", |
| 288 | + Data = new { Property = "Value" }, |
| 289 | + } |
| 290 | + ); |
| 291 | + } |
| 292 | +} |
0 commit comments