private static IObjectToTypeConverter <TableEntityContext> CreateConverter(IStorageTableClient client,
                                                                            IBindableTableEntityPath path)
 {
     return(new CompositeObjectToTypeConverter <TableEntityContext>(
                new EntityOutputConverter <TableEntityContext>(new IdentityConverter <TableEntityContext>()),
                new EntityOutputConverter <string>(new StringToTableEntityContextConverter(client, path))));
 }
Exemple #2
0
        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);
        }
Exemple #3
0
 private static IObjectToTypeConverter <IStorageTable> CreateConverter(IStorageTableClient client, IBindableTablePath path)
 {
     return(new CompositeObjectToTypeConverter <IStorageTable>(
                new OutputConverter <IStorageTable>(new IdentityConverter <IStorageTable>()),
                new OutputConverter <CloudTable>(new CloudTableToStorageTableConverter()),
                new OutputConverter <string>(new StringToStorageTableConverter(client, path))));
 }
Exemple #4
0
        public void PropertyHasBeenAdded()
        {
            // Arrange
            IStorageTableClient            client = CreateTableClient();
            IStorageTable                  table  = client.GetTableReference("table");
            PocoEntityWriter <SimpleClass> writer = new PocoEntityWriter <SimpleClass>(table);

            writer.TableEntityWriter = new StubTableEntityWriter();
            Type valueType = typeof(PocoEntityWriter <SimpleClass>);
            PocoEntityCollectorBinder <SimpleClass> product = new PocoEntityCollectorBinder <SimpleClass>(table, writer, valueType);

            SimpleClass value = new SimpleClass
            {
                PartitionKey = "PK",
                RowKey       = "RK"
            };

            writer.Add(value);

            // Act
            var parameterLog = product.GetStatus() as TableParameterLog;

            // Assert
            Assert.Equal(1, parameterLog.EntitiesWritten);
        }
Exemple #5
0
        public void PropertyHasBeenAdded()
        {
            // Arrange
            IStorageTableClient client = CreateTableClient();
            IStorageTable       table  = client.GetTableReference("table");
            StubTableEntityWriter <DynamicTableEntity> writer = new StubTableEntityWriter <DynamicTableEntity>();
            Type valueType = typeof(TableEntityWriter <DynamicTableEntity>);
            TableEntityCollectorBinder <DynamicTableEntity> product = new TableEntityCollectorBinder <DynamicTableEntity>(table, writer, valueType);

            DynamicTableEntity value = new DynamicTableEntity
            {
                PartitionKey = "PK",
                RowKey       = "RK",
                Properties   = new Dictionary <string, EntityProperty> {
                    { "Item", new EntityProperty("Foo") }
                }
            };

            writer.Add(value);

            // Act
            var parameterLog = product.GetStatus() as TableParameterLog;

            // Assert
            Assert.Equal(1, parameterLog.EntitiesWritten);
            Assert.Equal(0, writer.TimesPartitionFlushed);
        }
 private static IObjectToTypeConverter<IStorageTable> CreateConverter(IStorageTableClient client, IBindableTablePath path)
 {
     return new CompositeObjectToTypeConverter<IStorageTable>(
         new OutputConverter<IStorageTable>(new IdentityConverter<IStorageTable>()),
         new OutputConverter<CloudTable>(new CloudTableToStorageTableConverter()),
         new OutputConverter<string>(new StringToStorageTableConverter(client, path)));
 }
Exemple #7
0
 public ResolvedTableAttribute(TableAttribute inner, IStorageTableClient client)
     : base(inner.TableName, inner.PartitionKey, inner.RowKey)
 {
     this.Take   = inner.Take;
     this.Filter = inner.Filter;
     this.Client = client;
 }
        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);
        }
Exemple #9
0
        private static IStorageTable CreateTable(IStorageAccount account, string tableName)
        {
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(tableName);

            table.CreateIfNotExists();
            return(table);
        }
Exemple #10
0
        public async Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            ParameterInfo  parameter      = context.Parameter;
            TableAttribute tableAttribute = parameter.GetCustomAttribute <TableAttribute>(inherit: false);

            if (tableAttribute == null)
            {
                return(null);
            }

            string          tableName = Resolve(tableAttribute.TableName);
            IStorageAccount account   = await _accountProvider.GetStorageAccountAsync(context.Parameter, context.CancellationToken, _nameResolver);

            // requires storage account with table support
            account.AssertTypeOneOf(StorageAccountType.GeneralPurpose);

            StorageClientFactoryContext clientFactoryContext = new StorageClientFactoryContext
            {
                Parameter = context.Parameter
            };
            IStorageTableClient client = account.CreateTableClient(clientFactoryContext);

            bool     bindsToEntireTable = tableAttribute.RowKey == null;
            IBinding binding;

            if (bindsToEntireTable)
            {
                IBindableTablePath path = BindableTablePath.Create(tableName);
                path.ValidateContractCompatibility(context.BindingDataContract);

                IStorageTableArgumentBinding argumentBinding = _tableBindingProvider.TryCreate(parameter);

                if (argumentBinding == null)
                {
                    throw new InvalidOperationException("Can't bind Table to type '" + parameter.ParameterType + "'.");
                }

                binding = new TableBinding(parameter.Name, argumentBinding, client, path);
            }
            else
            {
                string partitionKey           = Resolve(tableAttribute.PartitionKey);
                string rowKey                 = Resolve(tableAttribute.RowKey);
                IBindableTableEntityPath path = BindableTableEntityPath.Create(tableName, partitionKey, rowKey);
                path.ValidateContractCompatibility(context.BindingDataContract);

                IArgumentBinding <TableEntityContext> argumentBinding = _entityBindingProvider.TryCreate(parameter);

                if (argumentBinding == null)
                {
                    throw new InvalidOperationException("Can't bind Table entity to type '" + parameter.ParameterType + "'.");
                }

                binding = new TableEntityBinding(parameter.Name, argumentBinding, client, path);
            }

            return(binding);
        }
 public TableBinding(string parameterName, ITableArgumentBinding argumentBinding, IStorageTableClient client, IBindableTablePath path)
 {
     _parameterName = parameterName;
     _argumentBinding = argumentBinding;
     _client = client;
     _accountName = TableClient.GetAccountName(client);
     _path = path;
     _converter = CreateConverter(client, path);
 }
Exemple #12
0
 public TableBinding(string parameterName, IStorageTableArgumentBinding argumentBinding, IStorageTableClient client, IBindableTablePath path)
 {
     _parameterName   = parameterName;
     _argumentBinding = argumentBinding;
     _client          = client;
     _accountName     = TableClient.GetAccountName(client);
     _path            = path;
     _converter       = CreateConverter(client, path);
 }
Exemple #13
0
        public static string GetAccountName(IStorageTableClient client)
        {
            if (client == null)
            {
                return(null);
            }

            return(StorageClient.GetAccountName(client.Credentials));
        }
Exemple #14
0
        // [Table] has some pre-existing behavior where the storage account can be specified outside of the [Table] attribute.
        // The storage account is pulled from the ParameterInfo (which could pull in a [Storage] attribute on the container class)
        // Resolve everything back down to a single attribute so we can use the binding helpers.
        // This pattern should be rare since other extensions can just keep everything directly on the primary attribute.
        private async Task <TableAttribute> CollectAttributeInfo(TableAttribute attrResolved, ParameterInfo parameter, INameResolver nameResolver)
        {
            // Look for [Storage] attribute and squirrel over
            IStorageAccount account = await _accountProvider.GetStorageAccountAsync(parameter, CancellationToken.None, nameResolver);

            StorageClientFactoryContext clientFactoryContext = new StorageClientFactoryContext
            {
                Parameter = parameter
            };
            IStorageTableClient client = account.CreateTableClient(clientFactoryContext);

            return(new ResolvedTableAttribute(attrResolved, client));
        }
Exemple #15
0
        public void ValueHasNotChanged()
        {
            // Arrange
            IStorageTableClient client = CreateTableClient();
            IStorageTable       table  = client.GetTableReference("table");
            StubTableEntityWriter <DynamicTableEntity> writer = new StubTableEntityWriter <DynamicTableEntity>();
            Type valueType = typeof(TableEntityWriter <DynamicTableEntity>);
            TableEntityCollectorBinder <DynamicTableEntity> product = new TableEntityCollectorBinder <DynamicTableEntity>(table, writer, valueType);

            // Act
            var parameterLog = product.GetStatus() as TableParameterLog;

            // Assert
            Assert.Null(parameterLog);
        }
        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 Table_IfBoundToIQueryableDynamicTableEntityAndDoesNotExist_BindsAndDoesNotCreateTable()
        {
            // Arrange
            IStorageAccount account = CreateFakeStorageAccount();
            IStorageQueue triggerQueue = CreateQueue(account, TriggerQueueName);
            triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore"));

            // Act
            IQueryable<DynamicTableEntity> result = RunTrigger<IQueryable<DynamicTableEntity>>(account,
                typeof(BindToIQueryableDynamicTableEntityProgram),
                (s) => BindToIQueryableDynamicTableEntityProgram.TaskSource = s);

            // Assert
            Assert.NotNull(result);
            Assert.Empty(result);
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable table = client.GetTableReference(TableName);
            Assert.False(table.Exists());
        }
        public void Table_IfBoundToIQueryableDynamicTableEntityAndExists_Binds()
        {
            // Arrange
            Guid            expectedValue = Guid.NewGuid();
            IStorageAccount account       = CreateFakeStorageAccount();
            IStorageQueue   triggerQueue  = CreateQueue(account, TriggerQueueName);

            triggerQueue.AddMessage(triggerQueue.CreateMessage("ignore"));
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(TableName);

            table.CreateIfNotExists();
            Dictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty>
            {
                { PropertyName, new EntityProperty(expectedValue) }
            };

            table.Insert(new DynamicTableEntity(PartitionKey, RowKey, etag: null, properties: properties));

            // Act
            IQueryable <DynamicTableEntity> result = RunTrigger <IQueryable <DynamicTableEntity> >(account,
                                                                                                   typeof(BindToIQueryableDynamicTableEntityProgram),
                                                                                                   (s) => BindToIQueryableDynamicTableEntityProgram.TaskSource = s);

            // Assert
            Assert.NotNull(result);
            DynamicTableEntity[] entities = result.ToArray();
            Assert.Equal(1, entities.Length);
            DynamicTableEntity entity = entities[0];

            Assert.NotNull(entity);
            Assert.Equal(PartitionKey, entity.PartitionKey);
            Assert.Equal(RowKey, entity.RowKey);
            Assert.NotNull(entity.Properties);
            Assert.True(entity.Properties.ContainsKey(PropertyName));
            EntityProperty property = entity.Properties[PropertyName];

            Assert.NotNull(property);
            Assert.Equal(EdmType.Guid, property.PropertyType);
            Assert.Equal(expectedValue, property.GuidValue);
        }
Exemple #19
0
        public void FlushAfterAdd_PersistsEntity()
        {
            // Arrange
            IStorageAccount     account = CreateFakeStorageAccount();
            IStorageTableClient client  = account.CreateTableClient();
            IStorageTable       table   = client.GetTableReference("Table");

            TableEntityWriter <ITableEntity> product = new TableEntityWriter <ITableEntity>(table);
            const string       partitionKey          = "PK";
            const string       rowKey = "RK";
            DynamicTableEntity entity = new DynamicTableEntity(partitionKey, rowKey);

            product.Add(entity);

            // Act
            product.FlushAsync().GetAwaiter().GetResult();

            // Assert
            DynamicTableEntity persisted = table.Retrieve <DynamicTableEntity>(partitionKey, rowKey);

            Assert.NotNull(persisted);
        }
        // Assert the given table has the given entity with PropertyName=ExpectedValue
        void AssertStringProperty(
            IStorageAccount account,
            string propertyName,
            string expectedValue,
            string tableName    = TableName,
            string partitionKey = PartitionKey,
            string rowKey       = RowKey)
        {
            // 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 StringToTableEntityContextConverter(IStorageTableClient client, IBindableTableEntityPath defaultPath)
 {
     _client      = client;
     _defaultPath = defaultPath;
 }
 private static IObjectToTypeConverter<TableEntityContext> CreateConverter(IStorageTableClient client, IBindableTableEntityPath path)
 {
     return new CompositeObjectToTypeConverter<TableEntityContext>(
         new EntityOutputConverter<TableEntityContext>(new IdentityConverter<TableEntityContext>()),
         new EntityOutputConverter<string>(new StringToTableEntityContextConverter(client, path)));
 }
Exemple #23
0
 public StringToStorageTableConverter(IStorageTableClient client, IBindableTablePath defaultPath)
 {
     _client      = client;
     _defaultPath = defaultPath;
 }
        public void Table_IfBoundToICollectorPoco_AddInsertsUsingNativeTableTypes()
        {
            // Arrange
            PocoWithAllTypes expected = new PocoWithAllTypes
            {
                PartitionKey                   = PartitionKey,
                RowKey                         = RowKey,
                BooleanProperty                = true,
                NullableBooleanProperty        = null,
                ByteArrayProperty              = new byte[] { 0x12, 0x34 },
                DateTimeProperty               = DateTime.Now,
                NullableDateTimeProperty       = null,
                DateTimeOffsetProperty         = DateTimeOffset.MaxValue,
                NullableDateTimeOffsetProperty = null,
                DoubleProperty                 = 3.14,
                NullableDoubleProperty         = null,
                GuidProperty                   = Guid.NewGuid(),
                NullableGuidProperty           = null,
                Int32Property                  = 123,
                NullableInt32Property          = null,
                Int64Property                  = 456,
                NullableInt64Property          = null,
                StringProperty                 = "abc",
                PocoProperty                   = new Poco
                {
                    PartitionKey = "def",
                    RowKey       = "ghi",
                    Property     = "jkl"
                }
            };
            IStorageAccount account      = CreateFakeStorageAccount();
            IStorageQueue   triggerQueue = CreateQueue(account, TriggerQueueName);

            triggerQueue.AddMessage(triggerQueue.CreateMessage(JsonConvert.SerializeObject(expected)));

            // Act
            RunTrigger(account, typeof(BindToICollectorPocoWithAllTypesProgram));

            // Assert
            IStorageTableClient client = account.CreateTableClient();
            IStorageTable       table  = client.GetTableReference(TableName);

            Assert.True(table.Exists());
            DynamicTableEntity entity = table.Retrieve <DynamicTableEntity>(PartitionKey, RowKey);

            Assert.Equal(expected.PartitionKey, entity.PartitionKey);
            Assert.Equal(expected.RowKey, entity.RowKey);
            IDictionary <string, EntityProperty> properties = entity.Properties;

            AssertNullablePropertyEqual(expected.BooleanProperty, EdmType.Boolean, properties, "BooleanProperty",
                                        (p) => p.BooleanValue);
            AssertPropertyNull(EdmType.Boolean, properties, "NullableBooleanProperty", (p) => p.BooleanValue);
            AssertPropertyEqual(expected.ByteArrayProperty, EdmType.Binary, properties, "ByteArrayProperty",
                                (p) => p.BinaryValue);
            AssertNullablePropertyEqual(expected.DateTimeProperty, EdmType.DateTime, properties, "DateTimeProperty",
                                        (p) => p.DateTime);
            AssertPropertyNull(EdmType.DateTime, properties, "NullableDateTimeProperty", (p) => p.DateTime);
            AssertNullablePropertyEqual(expected.DateTimeOffsetProperty, EdmType.DateTime, properties,
                                        "DateTimeOffsetProperty", (p) => p.DateTime);
            AssertPropertyNull(EdmType.DateTime, properties, "NullableDateTimeOffsetProperty",
                               (p) => p.DateTimeOffsetValue);
            AssertNullablePropertyEqual(expected.DoubleProperty, EdmType.Double, properties, "DoubleProperty",
                                        (p) => p.DoubleValue);
            AssertPropertyNull(EdmType.Double, properties, "NullableDoubleProperty", (p) => p.DoubleValue);
            AssertNullablePropertyEqual(expected.GuidProperty, EdmType.Guid, properties, "GuidProperty",
                                        (p) => p.GuidValue);
            AssertPropertyNull(EdmType.Guid, properties, "NullableGuidProperty", (p) => p.GuidValue);
            AssertNullablePropertyEqual(expected.Int32Property, EdmType.Int32, properties, "Int32Property",
                                        (p) => p.Int32Value);
            AssertPropertyNull(EdmType.Int32, properties, "NullableInt32Property", (p) => p.Int32Value);
            AssertNullablePropertyEqual(expected.Int64Property, EdmType.Int64, properties, "Int64Property",
                                        (p) => p.Int64Value);
            AssertPropertyNull(EdmType.Int64, properties, "NullableInt64Property", (p) => p.Int64Value);
            AssertPropertyEqual(expected.StringProperty, EdmType.String, properties, "StringProperty",
                                (p) => p.StringValue);
            AssertPropertyEqual(JsonConvert.SerializeObject(expected.PocoProperty, Formatting.Indented), EdmType.String,
                                properties, "PocoProperty", (p) => p.StringValue);
        }