public void PopulateCloudEvent_Valid() { var evt = new CloudEvent("type", new Uri("//source")); var data = new EventData { Text = "some text" }; CloudEventConverters.PopulateCloudEvent(evt, data); Assert.Equal("some text", evt.Data); }
public async Task NoRetry(PubsubMessage message) { var cloudEvent = new CloudEvent(MessagePublishedData.MessagePublishedCloudEventType, new Uri("//pubsub.googleapis.com")); var data = new MessagePublishedData { Message = message }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); await ExecuteCloudEventRequestAsync(cloudEvent); var logEntry = Assert.Single(GetFunctionLogEntries()); Assert.Equal(LogLevel.Information, logEntry.Level); Assert.Equal("Not retrying...", logEntry.Message); }
public async Task ObjectDeletedEvent() { var cloudEvent = new CloudEvent(StorageObjectData.DeletedCloudEventType, new Uri("//storage.googleapis.com")); var data = new StorageObjectData { Name = "new-file.txt" }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); await ExecuteCloudEventRequestAsync(cloudEvent); var logEntry = Assert.Single(GetFunctionLogEntries()); Assert.Equal($"Unsupported event type: {StorageObjectData.DeletedCloudEventType}", logEntry.Message); Assert.Equal(LogLevel.Warning, logEntry.Level); }
public async Task CloudEventInput() { var cloudEvent = new CloudEvent(StorageObjectData.FinalizedCloudEventType, new Uri("//storage.googleapis.com")); var data = new StorageObjectData { Name = "new-file.txt" }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); await ExecuteCloudEventRequestAsync(cloudEvent); var logEntry = Assert.Single(GetFunctionLogEntries()); Assert.Equal("File new-file.txt uploaded", logEntry.Message); Assert.Equal(LogLevel.Information, logEntry.Level); }
public async Task ExecuteCloudEventAsync_CloudEvent() { var cloudEvent = new CloudEvent( StorageObjectData.FinalizedCloudEventType, new Uri("//storage", UriKind.RelativeOrAbsolute)); var data = new StorageObjectData { Name = "test1" }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); using var test = new Test <StorageObjectFunction>(); await test.ExecuteCloudEventRequestAsync(cloudEvent); var log = Assert.Single(test.GetFunctionLogEntries()); Assert.Equal("Name: test1", log.Message); }
/// <summary> /// Convenience method to test CloudEvents by supplying only the most important aspects. /// This method simply constructs a CloudEvent from the parameters and delegates to /// <see cref="ExecuteCloudEventRequestAsync(CloudEvent)"/>. /// </summary> /// <param name="eventType">The CloudEvent type.</param> /// <param name="data">The data to populate the CloudEvent with. The data should be convertible /// via <see cref="CloudEventConverters"/>.</param> /// <param name="source">The source URI for the CloudEvent, or null to use a default of "//test-source".</param> /// <param name="subject">The subject of the CloudEvent, or null if no subject is required.</param> /// <typeparam name="T">The type of the event data. This is used to find the appropriate data converter.</typeparam> public Task ExecuteCloudEventRequestAsync <T>(string eventType, T?data, Uri?source = null, string?subject = null) where T : class { var cloudEvent = new CloudEvent( eventType, source ?? new Uri("//test-source", UriKind.RelativeOrAbsolute), id: Guid.NewGuid().ToString()); if (data is object) { CloudEventConverters.PopulateCloudEvent(cloudEvent, data); } if (subject is object) { cloudEvent.Subject = subject; } return(ExecuteCloudEventRequestAsync(cloudEvent)); }
public async Task RetryTrue() { var cloudEvent = new CloudEvent(MessagePublishedData.MessagePublishedCloudEventType, new Uri("//pubsub.googleapis.com")); var data = new MessagePublishedData { Message = new PubsubMessage { TextData = "{ \"retry\": true }" } }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); // The test server propagates the exception to the caller. The real server would respond // with a status code of 500. await Assert.ThrowsAsync <InvalidOperationException>(() => ExecuteCloudEventRequestAsync(cloudEvent)); Assert.Empty(GetFunctionLogEntries()); }
public async Task Processing(string textData, int ageInSeconds, string expectedLog) { var cloudEvent = new CloudEvent(MessagePublishedData.MessagePublishedCloudEventType, new Uri("//pubsub.googleapis.com"), "1234", DateTime.UtcNow.AddSeconds(-ageInSeconds)); var data = new MessagePublishedData { Message = new PubsubMessage { TextData = textData } }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); await ExecuteCloudEventRequestAsync(cloudEvent); var logEntry = Assert.Single(GetFunctionLogEntries()); Assert.Equal(LogLevel.Information, logEntry.Level); Assert.Equal(expectedLog, logEntry.Message); }
public async Task MessageWithoutTextData() { var cloudEvent = new CloudEvent(MessagePublishedData.MessagePublishedCloudEventType, new Uri("//pubsub.googleapis.com")); var data = new MessagePublishedData { Message = new PubsubMessage { Attributes = { { "key", "value" } } } }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); await ExecuteCloudEventRequestAsync(cloudEvent); var logEntry = Assert.Single(GetFunctionLogEntries()); Assert.Equal("Hello world", logEntry.Message); Assert.Equal(LogLevel.Information, logEntry.Level); }
public async Task FileNameIsLogged() { // Prepare the inputs var cloudEvent = new CloudEvent(StorageObjectData.FinalizedCloudEventType, new Uri("//storage.googleapis.com")); var data = new StorageObjectData { Name = "new-file.txt" }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); var logger = new MemoryLogger <HelloGcs.Function>(); // Execute the function var function = new HelloGcs.Function(logger); await function.HandleAsync(cloudEvent, data, CancellationToken.None); // Check the log results var logEntry = Assert.Single(logger.ListLogEntries()); Assert.Equal("File new-file.txt uploaded", logEntry.Message); Assert.Equal(LogLevel.Information, logEntry.Level); }
public async Task CloudEventIsLogged() { var client = Server.CreateClient(); var created = DateTimeOffset.UtcNow.AddMinutes(-5); var updated = created.AddMinutes(2); var cloudEvent = new CloudEvent(StorageObjectData.DeletedCloudEventType, new Uri("//storage.googleapis.com"), "1234"); var data = new StorageObjectData { Name = "new-file.txt", Bucket = "my-bucket", Metageneration = 23, TimeCreated = new DateTimeOffset(2020, 7, 9, 13, 0, 5, TimeSpan.Zero).ToTimestamp(), Updated = new DateTimeOffset(2020, 7, 9, 13, 23, 25, TimeSpan.Zero).ToTimestamp() }; CloudEventConverters.PopulateCloudEvent(cloudEvent, data); await ExecuteCloudEventRequestAsync(cloudEvent); var logs = GetFunctionLogEntries(); Assert.All(logs, entry => Assert.Equal(LogLevel.Information, entry.Level)); var actualMessages = logs.Select(entry => entry.Message).ToArray(); var expectedMessages = new[] { "Event: 1234", $"Event Type: {StorageObjectData.DeletedCloudEventType}", "Bucket: my-bucket", "File: new-file.txt", "Metageneration: 23", "Created: 2020-07-09T13:00:05", "Updated: 2020-07-09T13:23:25", }; Assert.Equal(expectedMessages, actualMessages); }