private static void AssertReceivedEvent(string eventId, string eventType, string eventSubject, string licensePlate, string receivedEvent) { Assert.NotEqual(string.Empty, receivedEvent); EventGridMessage <NewCarRegistered> deserializedEventGridMessage = EventGridParser.Parse <NewCarRegistered>(receivedEvent); Assert.NotNull(deserializedEventGridMessage); Assert.NotEmpty(deserializedEventGridMessage.SessionId); Assert.NotNull(deserializedEventGridMessage.Events); NewCarRegistered deserializedEvent = Assert.Single(deserializedEventGridMessage.Events); Assert.NotNull(deserializedEvent); Assert.Equal(eventId, deserializedEvent.Id); Assert.Equal(eventSubject, deserializedEvent.Subject); Assert.Equal(eventType, deserializedEvent.EventType); Assert.NotNull(deserializedEvent.Data); Assert.Equal(licensePlate, deserializedEvent.Data.LicensePlate); }
/// <summary> /// Parses a string to a EventGridMessage with typed data payload /// </summary> /// <param name="rawJsonBody">Raw JSON body</param> /// <param name="sessionId">Session id for event grid message</param> /// <returns>Typed EventGridMessage</returns> public static EventGridMessage <TEvent> Parse <TEvent>(string rawJsonBody, string sessionId) where TEvent : IEvent, new() { Guard.NotNullOrWhitespace(rawJsonBody, nameof(rawJsonBody)); Guard.NotNullOrWhitespace(sessionId, nameof(sessionId)); var array = JArray.Parse(rawJsonBody); var deserializedEvents = new List <TEvent>(); foreach (var eventObject in array.Children <JObject>()) { var rawEvent = eventObject.ToString(); var gridEvent = JsonConvert.DeserializeObject <TEvent>(rawEvent); deserializedEvents.Add(gridEvent); } var result = new EventGridMessage <TEvent>(sessionId, deserializedEvents); return(result); }