public void QueueTrigger_IfBoundToPocoAndMessageIsIncompatibleJson_DoesNotBind() { // Arrange const string content = "123"; // A JSON int rather than a JSON object. IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage(content); queue.AddMessage(message); // Act Exception exception = RunTriggerFailure <Poco>(account, typeof(BindToPocoProgram), (s) => BindToPocoProgram.TaskSource = s); // Assert Assert.IsType <InvalidOperationException>(exception); Assert.Equal("Exception binding parameter 'message'", exception.Message); Exception innerException = exception.InnerException; Assert.IsType <InvalidOperationException>(innerException); string expectedInnerMessage = "Binding parameters to complex objects (such as 'Poco') uses Json.NET " + "serialization. \r\n1. Bind the parameter type as 'string' instead of 'Poco' to get the raw values " + "and avoid JSON deserialization, or\r\n2. Change the queue payload to be valid json. The JSON parser " + "failed: Error converting value 123 to type '" + typeof(Poco).FullName + "'. Path '', line 1, " + "position 3.\r\n"; Assert.Equal(expectedInnerMessage, innerException.Message); }
public void QueueTrigger_IfDequeueCountReachesMaxDequeueCount_MovesToPoisonQueue() { try { // Arrange const string expectedContents = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(queue.CreateMessage(expectedContents)); // Act RunTrigger <object>(account, typeof(MaxDequeueCountProgram), (s) => MaxDequeueCountProgram.TaskSource = s, new string[] { typeof(MaxDequeueCountProgram).FullName + ".PutInPoisonQueue" }); // Assert IStorageAccountProvider storageAccountProvider = new FakeStorageAccountProvider() { StorageAccount = new FakeStorageAccount() }; Assert.Equal(new FakeQueueConfiguration(storageAccountProvider).MaxDequeueCount, MaxDequeueCountProgram.DequeueCount); } finally { MaxDequeueCountProgram.DequeueCount = 0; } }
public void QueueTrigger_ProvidesPocoComplexPropertyBindingData() { // Arrange Poco expectedChild = new Poco { Value = "abc", Int32Value = 123 }; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); Poco value = new Poco { Child = expectedChild }; string content = JsonConvert.SerializeObject(value, typeof(Poco), settings: null); IStorageQueueMessage message = queue.CreateMessage(content); queue.AddMessage(message); // Act Poco result = RunTrigger <Poco>(account, typeof(BindToPocoComplexPropertyBindingDataProgram), (s) => BindToPocoComplexPropertyBindingDataProgram.TaskSource = s); // Assert AssertEqual(expectedChild, result); }
public void TableEntity_IfUpdatesPoco_Persists() { // Arrange const string originalValue = "abc"; const string expectedValue = "def"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue)); IStorageTable table = CreateTable(account, TableName); Dictionary <string, EntityProperty> originalProperties = new Dictionary <string, EntityProperty> { { "Value", new EntityProperty(originalValue) } }; table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: originalProperties)); // Act RunTrigger(account, typeof(UpdatePocoProgram)); // Assert DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey); Assert.NotNull(entity); IDictionary <string, EntityProperty> properties = entity.Properties; Assert.NotNull(properties); Assert.True(properties.ContainsKey("Value")); EntityProperty property = properties["Value"]; Assert.NotNull(property); Assert.Equal(EdmType.String, property.PropertyType); Assert.Equal(expectedValue, property.StringValue); }
public void Table_IfBoundToICollectorJObject_AddInsertsEntity() { // Arrange const string expectedValue = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue)); // Act RunTrigger(account, typeof(BindToICollectorJObjectProgram)); // Assert IStorageTableClient client = account.CreateTableClient(); IStorageTable table = client.GetTableReference(TableName); Assert.True(table.Exists()); DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey); Assert.NotNull(entity); Assert.NotNull(entity.Properties); AssertPropertyValue(entity, "ValueStr", "abcdef"); AssertPropertyValue(entity, "ValueNum", 123); }
public void TableEntity_IfBoundToExistingDynamicTableEntity_Binds() { // Arrange const string expectedKey = "abc"; const int expectedValue = 123; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore")); IStorageTable table = CreateTable(account, TableName); Dictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty> { { expectedKey, new EntityProperty(expectedValue) } }; table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: properties)); // Act DynamicTableEntity result = RunTrigger <DynamicTableEntity>(account, typeof(BindToDynamicTableEntityProgram), (s) => BindToDynamicTableEntityProgram.TaskSource = s); // Assert Assert.NotNull(result); Assert.Equal(PartitionKey, result.PartitionKey); Assert.Equal(RowKey, result.RowKey); Assert.NotNull(result.Properties); Assert.True(result.Properties.ContainsKey(expectedKey)); EntityProperty property = result.Properties[expectedKey]; Assert.NotNull(property); Assert.Equal(EdmType.Int32, property.PropertyType); Assert.Equal(expectedValue, property.Int32Value); }
public void TableEntity_IfBoundToExistingPoco_Binds() { // Arrange const string expectedValue = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore")); IStorageTable table = CreateTable(account, TableName); Dictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty> { { "Value", new EntityProperty(expectedValue) } }; table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: properties)); // Act Poco result = RunTrigger <Poco>(account, typeof(BindToPocoProgram), (s) => BindToPocoProgram.TaskSource = s); // Assert Assert.NotNull(result); Assert.Equal(expectedValue, result.Value); }
public void TableEntity_IfUpdatesRowKey_Throws() { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore")); IStorageTable table = CreateTable(account, TableName); table.Insert(new DynamicTableEntity(PartitionKey, RowKey)); // Act Exception exception = RunTriggerFailure(account, typeof(UpdatePocoRowKeyProgram)); // Assert Assert.NotNull(exception); Assert.IsType <InvalidOperationException>(exception); Assert.Equal("Error while handling parameter entity after function returned:", exception.Message); Exception innerException = exception.InnerException; Assert.NotNull(innerException); Assert.IsType <InvalidOperationException>(innerException); Assert.Equal("When binding to a table entity, the row key must not be changed.", innerException.Message); }
public void TableEntity_IfUpdatesPoco_PersistsUsingNativeTableTypes() { // Arrange byte[] originalValue = new byte[] { 0x12, 0x34 }; byte[] expectedValue = new byte[] { 0x56, 0x78 }; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue)); IStorageTable table = CreateTable(account, TableName); Dictionary <string, EntityProperty> originalProperties = new Dictionary <string, EntityProperty> { { "Value", new EntityProperty(originalValue) } }; table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: originalProperties)); // Act RunTrigger(account, typeof(UpdatePocoWithByteArrayValueProgram)); // Assert DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey); Assert.NotNull(entity); IDictionary <string, EntityProperty> properties = entity.Properties; Assert.NotNull(properties); Assert.True(properties.ContainsKey("Value")); EntityProperty property = properties["Value"]; Assert.NotNull(property); Assert.Equal(EdmType.Binary, property.PropertyType); Assert.Equal(expectedValue, property.BinaryValue); }
public void Trigger_IfClassConstructorHasDependencies_CanUseCustomJobActivator() { // Arrange const string expectedResult = "abc"; Mock <IFactory <string> > resultFactoryMock = new Mock <IFactory <string> >(MockBehavior.Strict); resultFactoryMock.Setup(f => f.Create()).Returns(expectedResult); IFactory <string> resultFactory = resultFactoryMock.Object; Mock <IJobActivator> activatorMock = new Mock <IJobActivator>(MockBehavior.Strict); activatorMock.Setup(a => a.CreateInstance <InstanceCustomActivatorProgram>()) .Returns(() => new InstanceCustomActivatorProgram(resultFactory)); IJobActivator activator = activatorMock.Object; CloudQueueMessage message = new CloudQueueMessage("ignore"); IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(new FakeStorageQueueMessage(message)); // Act string result = RunTrigger <string>(account, typeof(InstanceCustomActivatorProgram), (s) => InstanceCustomActivatorProgram.TaskSource = s, activator); // Assert Assert.Same(expectedResult, result); }
public void QueueTrigger_IfBoundToPocoAndMessageIsNotJson_DoesNotBind() { // Arrange const string content = "not json"; // Not a valid JSON byte sequence. IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage(content); queue.AddMessage(message); // Act Exception exception = RunTriggerFailure <Poco>(account, typeof(BindToPocoProgram), (s) => BindToPocoProgram.TaskSource = s); // Assert Assert.IsType <InvalidOperationException>(exception); Assert.Equal("Exception binding parameter 'message'", exception.Message); Exception innerException = exception.InnerException; Assert.IsType <InvalidOperationException>(innerException); const string expectedInnerMessage = "Binding parameters to complex objects (such as 'Poco') uses " + "Json.NET serialization. \r\n1. Bind the parameter type as 'string' instead of 'Poco' to get the raw " + "values and avoid JSON deserialization, or\r\n2. Change the queue payload to be valid json. The JSON " + "parser failed: Unexpected character encountered while parsing value: n. Path '', line 0, position " + "0.\r\n"; Assert.Equal(expectedInnerMessage, innerException.Message); }
public void Table_IfBoundToICollectorPoco_AddInsertsEntity() { // Arrange const string expectedValue = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue)); // Act RunTrigger(account, typeof(BindToICollectorPocoProgram)); // Assert IStorageTableClient client = account.CreateTableClient(); IStorageTable table = client.GetTableReference(TableName); Assert.True(table.Exists()); DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey); Assert.NotNull(entity); Assert.NotNull(entity.Properties); Assert.True(entity.Properties.ContainsKey(PropertyName)); EntityProperty property = entity.Properties[PropertyName]; Assert.NotNull(property); Assert.Equal(EdmType.String, property.PropertyType); Assert.Equal(expectedValue, property.StringValue); }
public void Trigger_IfClassIsDisposable_Disposes() { // Arrange CloudQueueMessage expectedMessage = new CloudQueueMessage("ignore"); IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(new FakeStorageQueueMessage(expectedMessage)); // Act & Assert RunTrigger <object>(account, typeof(DisposeInstanceProgram), (s) => DisposeInstanceProgram.TaskSource = s); }
public void Table_IfBoundToICollectorPoco_AddInsertsEntity() { // Arrange const string expectedValue = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage(expectedValue)); // Act RunTrigger(account, typeof(BindToICollectorPocoProgram)); // Assert AssertStringProperty(account, PropertyName, expectedValue); }
private static void TestBindToByteArray(byte[] expectedContent) { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage(expectedContent); queue.AddMessage(message); // Act byte[] result = RunTrigger <byte[]>(account, typeof(BindToByteArrayProgram), (s) => BindToByteArrayProgram.TaskSource = s); // Assert Assert.Equal(expectedContent, result); }
private static void TestBindToPoco(Poco expectedContent) { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); string content = JsonConvert.SerializeObject(expectedContent, typeof(Poco), settings: null); IStorageQueueMessage message = queue.CreateMessage(content); queue.AddMessage(message); // Act Poco result = RunTrigger <Poco>(account, typeof(BindToPocoProgram), (s) => BindToPocoProgram.TaskSource = s); // Assert AssertEqual(expectedContent, result); }
private static void TestBindToString(string expectedContent) { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage(expectedContent); queue.AddMessage(message); // Act string result = RunTrigger <string>(account, typeof(BindToStringProgram), (s) => BindToStringProgram.TaskSource = s); // Assert Assert.Same(expectedContent, result); }
public void Trigger_CanBeAsyncInstanceMethod() { // Arrange CloudQueueMessage expectedMessage = new CloudQueueMessage("ignore"); IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(new FakeStorageQueueMessage(expectedMessage)); // Act CloudQueueMessage result = RunTrigger <CloudQueueMessage>(account, typeof(InstanceAsyncProgram), (s) => InstanceAsyncProgram.TaskSource = s); // Assert Assert.Same(expectedMessage, result); }
public void QueueTrigger_IfBoundToCloudQueueMessage_Binds() { // Arrange CloudQueueMessage expectedMessage = new CloudQueueMessage("ignore"); IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(new FakeStorageQueueMessage(expectedMessage)); // Act CloudQueueMessage result = RunTrigger <CloudQueueMessage>(account, typeof(BindToCloudQueueMessageProgram), (s) => BindToCloudQueueMessageProgram.TaskSource = s); // Assert Assert.Same(expectedMessage, result); }
public void Trigger_CanBeAsyncInstanceMethod() { // Arrange string expectedGuid = Guid.NewGuid().ToString(); CloudQueueMessage expectedMessage = new CloudQueueMessage(expectedGuid); IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(new FakeStorageQueueMessage(expectedMessage)); // Act CloudQueueMessage result = RunTrigger <CloudQueueMessage>(account, typeof(InstanceAsyncProgram), (s) => InstanceAsyncProgram.TaskSource = s); // Assert Assert.Equal(expectedGuid, result.AsString); }
public void QueueTrigger_IfBoundToCloudQueueMessage_Binds() { // Arrange string expectedGuid = Guid.NewGuid().ToString(); CloudQueueMessage expectedMessage = new CloudQueueMessage(expectedGuid); IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(new FakeStorageQueueMessage(expectedMessage)); // Act CloudQueueMessage result = RunTrigger <CloudQueueMessage>(account, typeof(BindToCloudQueueMessageProgram), (s) => BindToCloudQueueMessageProgram.TaskSource = s); // Assert Assert.Equal(expectedMessage.AsString, result.AsString); }
public void Trigger_ViaIBinder_CannotBind() { // Arrange const string expectedContents = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); FakeStorageQueueMessage message = (FakeStorageQueueMessage)queue.CreateMessage(expectedContents); queue.AddMessage(message); // Act Exception exception = RunTriggerFailure <string>(account, typeof(BindToQueueTriggerViaIBinderProgram), (s) => BindToQueueTriggerViaIBinderProgram.TaskSource = s); // Assert Assert.Equal("No binding found for attribute 'Microsoft.Azure.WebJobs.QueueTriggerAttribute'.", exception.Message); }
public void QueueTrigger_IfBindingAlwaysFails_MovesToPoisonQueue() { // Arrange const string expectedContents = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); queue.AddMessage(queue.CreateMessage(expectedContents)); // Act string result = RunTrigger <string>(account, typeof(PoisonQueueProgram), (s) => PoisonQueueProgram.TaskSource = s, new string[] { typeof(PoisonQueueProgram).FullName + ".PutInPoisonQueue" }); // Assert Assert.Equal(expectedContents, result); }
public void QueueTrigger_ProvidesNextVisibleTimeBindingData() { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage("ignore"); // message.NextVisibleTime is provided by FakeStorageAccount when the message is retrieved. queue.AddMessage(message); // Act DateTimeOffset result = RunTrigger <DateTimeOffset>(account, typeof(BindToNextVisibleTimeBindingDataProgram), (s) => BindToNextVisibleTimeBindingDataProgram.TaskSource = s); // Assert Assert.Equal(0, (int)DateTimeOffset.Now.Subtract(result).TotalHours); }
public void QueueTrigger_ProvidesDequeueCountBindingData() { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage("ignore"); // message.DequeueCount is provided by FakeStorageAccount when the message is retrieved. queue.AddMessage(message); // Act int result = RunTrigger <int>(account, typeof(BindToDequeueCountBindingDataProgram), (s) => BindToDequeueCountBindingDataProgram.TaskSource = s); // Assert Assert.Equal(1, result); }
public void QueueTrigger_IfMessageIsString_ProvidesQueueTriggerBindingData() { // Arrange const string expectedQueueTrigger = "abc"; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage(expectedQueueTrigger); queue.AddMessage(message); // Act string result = RunTrigger <string>(account, typeof(BindToQueueTriggerBindingDataProgram), (s) => BindToQueueTriggerBindingDataProgram.TaskSource = s); // Assert Assert.Same(expectedQueueTrigger, result); }
public void QueueTrigger_IfBoundToPocoStruct_Binds() { // Arrange const int expectedContent = 123; IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); string content = JsonConvert.SerializeObject(expectedContent, typeof(int), settings: null); IStorageQueueMessage message = queue.CreateMessage(content); queue.AddMessage(message); // Act int result = RunTrigger <int>(account, typeof(BindToPocoStructProgram), (s) => BindToPocoStructProgram.TaskSource = s); // Assert Assert.Equal(expectedContent, result); }
public void Table_IfBoundToCloudTable_BindsAndCreatesTable() { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName); triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore")); // Act CloudTable result = RunTrigger<CloudTable>(account, typeof(BindToCloudTableProgram), (s) => BindToCloudTableProgram.TaskSource = s); // Assert Assert.NotNull(result); Assert.Equal(TableName, result.Name); IStorageTableClient client = account.CreateTableClient(); IStorageTable table = client.GetTableReference(TableName); Assert.True(table.Exists()); }
public void QueueTrigger_ProvidesPopReceiptBindingData() { // Arrange IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage("ignore"); // message.PopReceipt is provided by FakeStorageAccount when the message is retrieved. queue.AddMessage(message); // Act string result = RunTrigger <string>(account, typeof(BindToPopReceiptBindingDataProgram), (s) => BindToPopReceiptBindingDataProgram.TaskSource = s); // Assert Assert.NotNull(result); Assert.NotEmpty(result); }
public void QueueTrigger_IfMessageIsUtf8ByteArray_ProvidesQueueTriggerBindingData() { // Arrange const string expectedQueueTrigger = "abc"; byte[] content = StrictEncodings.Utf8.GetBytes(expectedQueueTrigger); IStorageAccount account = CreateFakeStorageAccount(); IStorageQueue queue = CreateQueue(account, QueueName); IStorageQueueMessage message = queue.CreateMessage(content); queue.AddMessage(message); // Act string result = RunTrigger <string>(account, typeof(BindToQueueTriggerBindingDataProgram), (s) => BindToQueueTriggerBindingDataProgram.TaskSource = s); // Assert Assert.Equal(expectedQueueTrigger, result); }