public async Task CloudEventIsLogged()
        {
            var client = Server.CreateClient();

            var created = DateTimeOffset.UtcNow.AddMinutes(-5);
            var updated = created.AddMinutes(2);
            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()
            };

            await ExecuteFunctionAsync(StorageObjectData.DeletedCloudEventType, data);

            var logs = GetFunctionLogEntries();

            Assert.All(logs, entry => Assert.Equal(LogLevel.Information, entry.Level));

            var actualMessages   = logs.Select(entry => entry.Message).ToList();
            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);
        }
Esempio n. 2
0
        public async Task FileNameIsLogged()
        {
            // Prepare the inputs
            var data = new StorageObjectData {
                Name = "new-file.txt"
            };
            var cloudEvent = new CloudEvent
            {
                Type   = StorageObjectData.FinalizedCloudEventType,
                Source = new Uri("//storage.googleapis.com", UriKind.RelativeOrAbsolute),
                Id     = "1234",
                Data   = 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 - just the entry starting with "File:".
            var logEntry = Assert.Single(logger.ListLogEntries(), entry => entry.Message.StartsWith("File:"));

            Assert.Equal("File: new-file.txt", logEntry.Message);
            Assert.Equal(LogLevel.Information, logEntry.Level);
        }
        public void PopulateCloudEvent_DefaultToJson()
        {
            var data = new StorageObjectData {
                Bucket = "my-bucket"
            };
            var evt       = new CloudEvent("type", new Uri("//source"));
            var converter = new ProtobufCloudEventDataConverter <StorageObjectData>();

            converter.PopulateCloudEvent(evt, data);
            Assert.Equal("{ \"bucket\": \"my-bucket\" }", evt.Data);
            Assert.Equal(JsonContentType, evt.DataContentType);
        }
Esempio n. 4
0
        public async Task ObjectDeletedEvent()
        {
            var data = new StorageObjectData {
                Name = "new-file.txt"
            };

            await ExecuteFunctionAsync(StorageObjectData.DeletedCloudEventType, data);

            var logEntry = Assert.Single(GetFunctionLogEntries());

            Assert.Equal($"Unsupported event type: {StorageObjectData.DeletedCloudEventType}", logEntry.Message);
            Assert.Equal(LogLevel.Warning, logEntry.Level);
        }
Esempio n. 5
0
        public async Task CloudEventInput()
        {
            var data = new StorageObjectData {
                Name = "new-file.txt"
            };

            await ExecuteFunctionAsync(StorageObjectData.FinalizedCloudEventType, data);

            var logEntry = Assert.Single(GetFunctionLogEntries());

            Assert.Equal("File new-file.txt uploaded", logEntry.Message);
            Assert.Equal(LogLevel.Information, logEntry.Level);
        }
        public void PopulateCloudEvent_UnknownDataContentType()
        {
            var data = new StorageObjectData {
                Bucket = "my-bucket"
            };
            var evt = new CloudEvent("type", new Uri("//source"))
            {
                DataContentType = new ContentType("application/unknown")
            };
            var converter = new ProtobufCloudEventDataConverter <StorageObjectData>();

            Assert.Throws <ArgumentException>(() => converter.PopulateCloudEvent(evt, data));
        }
        public void ConvertEventData_Json()
        {
            var original = new StorageObjectData {
                Bucket = "my-bucket"
            };
            var evt = new CloudEvent("type", new Uri("//source"))
            {
                Data = "{ \"bucket\": \"my-bucket\" }"
            };
            var converter = new ProtobufCloudEventDataConverter <StorageObjectData>();
            var result    = converter.ConvertEventData(evt);

            Assert.Equal(original, result);
        }
        public async Task CloudEventIsLogged()
        {
            var client = Server.CreateClient();

            var created = DateTimeOffset.UtcNow.AddMinutes(-5);
            var updated = created.AddMinutes(2);
            var data    = new StorageObjectData
            {
                Name           = "new-file.txt",
                Bucket         = "my-bucket",
                Metageneration = 23,
                TimeCreated    = new DateTimeOffset(2020, 7, 9, 13, 0, 5, TimeSpan.Zero),
                Updated        = new DateTimeOffset(2020, 7, 9, 13, 23, 25, TimeSpan.Zero)
            };
            var request = new HttpRequestMessage
            {
                RequestUri = new Uri("uri", UriKind.Relative),
                // CloudEvent headers
                Headers =
                {
                    { "ce-type",        StorageObjectData.DeletedCloudEventType },
                    { "ce-id",          "1234"                                  },
                    { "ce-source",      "//storage.googleapis.com/"             },
                    { "ce-specversion", "1.0"                                   }
                },
                Content = new StringContent(JsonSerializer.Serialize(data)),
                Method  = HttpMethod.Post
            };
            var response = await client.SendAsync(request);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var logs = Server.GetLogEntries(typeof(HelloGcsGeneric.Function));

            Assert.All(logs, entry => Assert.Equal(LogLevel.Information, entry.Level));

            var actualMessages   = logs.Select(entry => entry.Message).ToList();
            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);
        }
        public void PopulateCloudEvent_ExplicitlyBinary()
        {
            var data = new StorageObjectData {
                Bucket = "my-bucket"
            };
            var evt = new CloudEvent("type", new Uri("//source"))
            {
                DataContentType = ProtobufContentType
            };
            var converter = new ProtobufCloudEventDataConverter <StorageObjectData>();

            converter.PopulateCloudEvent(evt, data);
            Assert.Equal(data.ToByteArray(), evt.Data);
            Assert.Equal(ProtobufContentType, evt.DataContentType);
        }
        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);
        }
        public async Task CloudEventIsLogged()
        {
            var client = Server.CreateClient();

            var created = DateTimeOffset.UtcNow.AddMinutes(-5);
            var updated = created.AddMinutes(2);
            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()
            };
            var cloudEvent = new CloudEvent
            {
                Type   = StorageObjectData.DeletedCloudEventType,
                Source = new Uri("//storage.googleapis.com", UriKind.RelativeOrAbsolute),
                Id     = "1234",
                Data   = 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);
        }
        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 Task HandleAsync(CloudEvent cloudEvent, StorageObjectData data, CancellationToken cancellationToken)
 {
     _logger.LogInformation($"Name: {data.Name}");
     return(Task.CompletedTask);
 }
Esempio n. 16
0
 public void SetUpFromStorageObjectData(StorageObjectData Data)
 {
     maxItemSize = Data.maxItemSize;
     maxSlots    = Data.maxSlots;
 }