public void ConvertToTableEntityWithFewKeys()
        {
            // Arrange
            var converter = new TableEntityConverter <LogEntry>();
            var entry     = new LogEntry
            {
                Id          = Guid.NewGuid().ToString("N"),
                ETag        = "MyETag",
                Timestamp   = DateTime.UtcNow,
                Message     = "My message",
                PrivateData = new byte[] { 0xaa, 0xbb, 0xcc }
            };

            var context = new OperationContext();

            // Act
            ITableEntity tableEntity = converter.GetEntity(entry);
            IDictionary <string, EntityProperty> properties = tableEntity.WriteEntity(context);

            // Assert
            Assert.Equal(entry.ETag, tableEntity.ETag);
            Assert.Equal(entry.Id, tableEntity.PartitionKey);
            Assert.Equal(string.Empty, tableEntity.RowKey);
            Assert.Equal(default(DateTimeOffset), tableEntity.Timestamp);
            Assert.Equal(entry.Message, properties["OldMessage"].StringValue);
            Assert.DoesNotContain("PrivateData", properties.Keys);
        }
        public void ConvertToEntityWithSerializeAttribute()
        {
            // Arrange
            var serializer = SerializationSettings.Instance.DefaultSerializer;

            var converter   = new TableEntityConverter <EntityWithSerializeAttribute>();
            var tableEntity = new DynamicTableEntity
            {
                PartitionKey = "Pk",
                Properties   = new Dictionary <string, EntityProperty>
                {
                    { "NestedSerialized", new EntityProperty("{DecimalValue: 10}") }
                }
            };

            // Act
            var entity = converter.GetEntity(tableEntity);

            // Assert
            Assert.Equal(entity.Pk, tableEntity.PartitionKey);
            Assert.Null(tableEntity.RowKey);
            Assert.Equal(
                entity.Nested.DecimalValue,
                serializer.Deserialize <EntityWithSerializeAttribute.NestedEntity>(
                    tableEntity.Properties["NestedSerialized"].StringValue).DecimalValue);
        }
        /// <summary>
        ///     Creates a TableQueryProvider instance.
        /// </summary>
        /// <returns>TableQueryProvider.</returns>
        private TableQueryProvider <Country> GetTableQueryProvider()
        {
            var tableEntityConverter     = new TableEntityConverter <Country>();
            CloudTableClient tableClient = GenerateCloudTableClient();
            CloudTable       cloudTable  = tableClient.GetTableReference(TableName);

            return(new TableQueryProvider <Country>(new CloudTableWrapper(cloudTable), tableEntityConverter));
        }
        /// <summary>
        ///     Creates a TableQueryProvider instance.
        /// </summary>
        /// <returns>TableQueryProvider.</returns>
        private TableQueryProvider<Country> GetTableQueryProvider()
        {
            var tableEntityConverter = new TableEntityConverter<Country>();
            CloudTableClient tableClient = GenerateCloudTableClient();
            CloudTable cloudTable = tableClient.GetTableReference(TableName);

            return new TableQueryProvider<Country>(new CloudTableWrapper(cloudTable), tableEntityConverter);
        }
        public void ExecuteAsyncWithNullExpression()
        {
            // Arrange
            Mock <ICloudTable> cloudTableMock = MocksFactory.GetCloudTableMock();
            var converter = new TableEntityConverter <Country>();
            var provider  = new TableQueryProvider <Country>(cloudTableMock.Object, converter);

            // Act && Assert
            Assert.Throws <ArgumentNullException>(() => provider.ExecuteAsync(null));
        }
        public void ExecuteAsyncWithNullExpression()
        {
            // Arrange
            Mock<ICloudTable> cloudTableMock = MocksFactory.GetCloudTableMock();
            var converter = new TableEntityConverter<Country>();
            var provider = new TableQueryProvider<Country>(cloudTableMock.Object, converter);

            // Act && Assert
            Assert.Throws<ArgumentNullException>(() => provider.ExecuteAsync(null));
        }
Beispiel #7
0
        private IEnumerable <Orchestration> BuildOrchestrationList(Pageable <OrchestrationTableEntity> tableEntities)
        {
            List <Orchestration> orchestrations = new List <Orchestration>();

            foreach (var orchestration in tableEntities)
            {
                orchestrations.Add(TableEntityConverter.ConvertToBaseType <Orchestration>(orchestration));
            }

            return(orchestrations);
        }
        public void CreateConverter()
        {
            // Arrange & Act
            var converter = new TableEntityConverter <Country>();

            // Assert
            Assert.NotNull(converter.NameChanges);
            Assert.Equal(2, converter.NameChanges.Count);
            Assert.Equal("PartitionKey", converter.NameChanges["Continent"]);
            Assert.Equal("RowKey", converter.NameChanges["Name"]);
        }
Beispiel #9
0
        private IEnumerable <OrchestrationEvent> BuildEventList(Pageable <OrchestrationEventTableEntity> tableEntities)
        {
            List <OrchestrationEvent> events = new List <OrchestrationEvent>();

            foreach (var item in tableEntities)
            {
                events.Add(TableEntityConverter.ConvertToBaseType <OrchestrationEvent>(item));
            }

            return(events);
        }
        public void CreateConverter()
        {
            // Arrange & Act
            var converter = new TableEntityConverter<Country>();

            // Assert
            Assert.NotNull(converter.NameChanges);
            Assert.Equal(2, converter.NameChanges.Count);
            Assert.Equal("PartitionKey", converter.NameChanges["Continent"]);
            Assert.Equal("RowKey", converter.NameChanges["Name"]);
        }
        public void CreateDynamicTableEntityFromProperties_WhenPropertyValueIsNull_DoesNotAddItToTheEntityProperties()
        {
            var c = new TableEntityConverter();

            var entity = c.CreateDynamicTableEntityFromProperties(new Dictionary <string, object>
            {
                { "MyProp", null }
            });

            Assert.False(entity.Properties.ContainsKey("MyProp"));
        }
Beispiel #12
0
        public async Task <IDictionary <IScaleMonitor, IList <ScaleMetrics> > > ReadMetricsAsync(IEnumerable <IScaleMonitor> monitors)
        {
            try
            {
                var monitorMetrics = new Dictionary <IScaleMonitor, IList <ScaleMetrics> >();
                var metricsTable   = GetMetricsTable();
                if (metricsTable == null || !monitors.Any())
                {
                    return(monitorMetrics);
                }

                var recentMetrics = await ReadRecentMetrics(metricsTable);

                var entitiesByMonitorId = recentMetrics.ToLookup(p => p.Properties[MonitorIdPropertyName].StringValue, StringComparer.OrdinalIgnoreCase);
                foreach (var monitor in monitors)
                {
                    var currMonitorMetrics = new List <ScaleMetrics>();
                    monitorMetrics[monitor] = currMonitorMetrics;

                    Type metricsType = GetMonitorScaleMetricsTypeOrNull(monitor);
                    if (metricsType != null)
                    {
                        var monitorEntities = entitiesByMonitorId[monitor.Descriptor.Id];
                        foreach (var entity in monitorEntities)
                        {
                            var convertedMetrics = (ScaleMetrics)TableEntityConverter.ToObject(metricsType, entity);
                            if (entity.Properties.TryGetValue(SampleTimestampPropertyName, out EntityProperty value) && value.DateTime.HasValue)
                            {
                                convertedMetrics.Timestamp = value.DateTime.Value;
                            }
                            currMonitorMetrics.Add(convertedMetrics);
                        }

                        // entities are stored in the table recent to oldest, so we reverse
                        // to give consumers a list in ascending time order
                        currMonitorMetrics.Reverse();
                    }
                }

                return(monitorMetrics);
            }
            catch (StorageException e)
            {
                LogStorageException(e);
                throw;
            }
        }
Beispiel #13
0
        internal static TableOperation CreateMetricsInsertOperation(ScaleMetrics metrics, string hostId, ScaleMonitorDescriptor descriptor, DateTime?now = null)
        {
            now = now ?? DateTime.UtcNow;

            // Use an inverted ticks rowkey to order the table in descending order, allowing us to easily
            // query for latest logs. Adding a guid as part of the key to ensure uniqueness.
            string rowKey = TableStorageHelpers.GetRowKey(now.Value);

            var entity = TableEntityConverter.ToEntity(metrics, hostId, rowKey, metrics.Timestamp);

            entity.Properties.Add(MonitorIdPropertyName, EntityProperty.GeneratePropertyForString(descriptor.Id));

            // We map the sample timestamp to its own column so it doesn't conflict with the built in column.
            // We want to ensure that timestamp values for returned metrics are precise and monotonically
            // increasing when ordered results are returned. The built in timestamp doesn't guarantee this.
            entity.Properties.Add(SampleTimestampPropertyName, EntityProperty.GeneratePropertyForDateTimeOffset(metrics.Timestamp));

            return(TableOperation.Insert(entity));
        }
        public void DynamicToPocoMeasurement()
        {
            // Arrange
            var converter = new TableEntityConverter <Country>();
            DynamicTableEntity dynamicTableEntity = ObjectsFactory.GetCountryDynamicTableEntity();
            Stopwatch          stopWatch          = Stopwatch.StartNew();
            Country            entity             = null;

            // Act
            for (int i = 0; i < IteractionsCount; i++)
            {
                entity = converter.GetEntity(dynamicTableEntity);
            }

            stopWatch.Stop();

            Assert.NotNull(entity);

            Console.WriteLine(ResultFormat, IteractionsCount, stopWatch.ElapsedMilliseconds);
        }
Beispiel #15
0
        /// <summary>
        ///     Constructor.
        /// </summary>
        /// <param name="cloudTableClient">Cloud table client.</param>
        /// <param name="tableName">Table name.</param>
        public TableSet(CloudTableClient cloudTableClient, string tableName)
        {
            if (cloudTableClient == null)
            {
                throw new ArgumentNullException(nameof(cloudTableClient));
            }

            if (string.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }

            _cloudTable = cloudTableClient.GetTableReference(tableName);
            var cloudTableWrapper = new CloudTableWrapper(_cloudTable);
            var entityConverter   = new TableEntityConverter <TEntity>();

            RequestExecutorFactory = new TableRequestExecutorFactory <TEntity>(cloudTableWrapper, entityConverter);
            Provider        = new TableQueryProvider <TEntity>(cloudTableWrapper, entityConverter);
            RequestExecutor = RequestExecutorFactory.Create(_executionMode);
        }
        public void ConvertToEntityWithNameMapping()
        {
            // Arrange
            var converter   = new TableEntityConverter <LogEntry>();
            var tableEntity = new DynamicTableEntity
            {
                PartitionKey = "My partiton key",
                Properties   = new Dictionary <string, EntityProperty>
                {
                    { "OldMessage", new EntityProperty("My message") }
                }
            };

            // Act
            LogEntry entity = converter.GetEntity(tableEntity);

            // Assert
            Assert.Equal(entity.Id, tableEntity.PartitionKey);
            Assert.Null(tableEntity.RowKey);
            Assert.Equal(entity.Message, tableEntity.Properties["OldMessage"].StringValue);
        }
        public void ConvertToTableEntityWithNameMapping()
        {
            // Arrange
            var converter = new TableEntityConverter <LogEntry>();
            var entity    = new LogEntry
            {
                Id      = "My id",
                Message = "My message"
            };

            var context = new OperationContext();

            // Act
            ITableEntity tableEntity = converter.GetEntity(entity);
            IDictionary <string, EntityProperty> properties = tableEntity.WriteEntity(context);

            // Assert
            Assert.Equal(null, tableEntity.ETag);
            Assert.Equal(entity.Id, tableEntity.PartitionKey);
            Assert.Equal(string.Empty, tableEntity.RowKey);
            Assert.Equal(entity.Message, properties["OldMessage"].StringValue);
        }
        public void ConvertToEntity()
        {
            // Arrange
            var converter = new TableEntityConverter<Country>();
            var tableEntity = new DynamicTableEntity
                {
                    PartitionKey = "Europe",
                    RowKey = "Spain",
                    Properties = new Dictionary<string, EntityProperty>
                        {
                            {"Area", new EntityProperty(505992d)},
                            {
                                "TopSecretKey",
                                new EntityProperty(new byte[] {0xaa, 0xbb, 0xcc})
                            },
                            {"Formed", new EntityProperty(new DateTime(1812, 1, 1))},
                            {"Id", new EntityProperty(Guid.NewGuid())},
                            {"IsExists", new EntityProperty(true)},
                            {"Population", new EntityProperty(47190493L)},
                            {"PresidentsCount", new EntityProperty(8)},
                        }
                };

            // Act
            Country country = converter.GetEntity(tableEntity);

            // Assert
            Assert.Equal(country.Continent, tableEntity.PartitionKey);
            Assert.Equal(country.Name, tableEntity.RowKey);
            Assert.Equal(country.Area, tableEntity.Properties["Area"].DoubleValue);
            Assert.Equal(country.TopSecretKey, tableEntity.Properties["TopSecretKey"].BinaryValue);
            Assert.True(tableEntity.Properties["Formed"].DateTimeOffsetValue.HasValue);
            Assert.Equal(country.Formed, tableEntity.Properties["Formed"].DateTimeOffsetValue.Value.DateTime);
            Assert.Equal(country.Id, tableEntity.Properties["Id"].GuidValue);
            Assert.Equal(country.IsExists, tableEntity.Properties["IsExists"].BooleanValue);
            Assert.Equal(country.Population, tableEntity.Properties["Population"].Int64Value);
            Assert.Equal(country.PresidentsCount, tableEntity.Properties["PresidentsCount"].Int32Value);
        }
        public void ConvertToEntity()
        {
            // Arrange
            var converter   = new TableEntityConverter <Country>();
            var tableEntity = new DynamicTableEntity
            {
                PartitionKey = "Europe",
                RowKey       = "Spain",
                Properties   = new Dictionary <string, EntityProperty>
                {
                    { "Area", new EntityProperty(505992d) },
                    {
                        "TopSecretKey",
                        new EntityProperty(new byte[] { 0xaa, 0xbb, 0xcc })
                    },
                    { "Formed", new EntityProperty(new DateTime(1812, 1, 1)) },
                    { "Id", new EntityProperty(Guid.NewGuid()) },
                    { "IsExists", new EntityProperty(true) },
                    { "Population", new EntityProperty(47190493L) },
                    { "PresidentsCount", new EntityProperty(8) },
                }
            };

            // Act
            Country country = converter.GetEntity(tableEntity);

            // Assert
            Assert.Equal(country.Continent, tableEntity.PartitionKey);
            Assert.Equal(country.Name, tableEntity.RowKey);
            Assert.Equal(country.Area, tableEntity.Properties["Area"].DoubleValue);
            Assert.Equal(country.TopSecretKey, tableEntity.Properties["TopSecretKey"].BinaryValue);
            Assert.True(tableEntity.Properties["Formed"].DateTimeOffsetValue.HasValue);
            Assert.Equal(country.Formed, tableEntity.Properties["Formed"].DateTimeOffsetValue.Value.DateTime);
            Assert.Equal(country.Id, tableEntity.Properties["Id"].GuidValue);
            Assert.Equal(country.IsExists, tableEntity.Properties["IsExists"].BooleanValue);
            Assert.Equal(country.Population, tableEntity.Properties["Population"].Int64Value);
            Assert.Equal(country.PresidentsCount, tableEntity.Properties["PresidentsCount"].Int32Value);
        }
        public void ConvertFromEntityWithSerializeAttribute()
        {
            // Arrange
            var serializer = SerializationSettings.Instance.DefaultSerializer;

            var converter = new TableEntityConverter <EntityWithSerializeAttribute>();
            var entity    = new EntityWithSerializeAttribute
            {
                Nested = new EntityWithSerializeAttribute.NestedEntity
                {
                    DecimalValue = 10,
                },
            };

            // Act
            var tableEntity  = converter.GetEntity(entity);
            var properties   = tableEntity.WriteEntity(new OperationContext());
            var deserialized = serializer.Deserialize <EntityWithSerializeAttribute.NestedEntity>(properties["NestedSerialized"].StringValue);

            // Assert
            Assert.NotNull(properties["NestedSerialized"].StringValue);
            Assert.Equal(entity.Nested.DecimalValue, deserialized.DecimalValue);
        }
        public void ConvertToTableEntity()
        {
            // Arrange
            var converter = new TableEntityConverter<Country>();
            var country = new Country
                {
                    Area = 505992,
                    Continent = "Europe",
                    TopSecretKey = new byte[] {0xaa, 0xbb, 0xcc},
                    Formed = new DateTime(1812, 1, 1),
                    Id = Guid.NewGuid(),
                    IsExists = true,
                    Name = "Spain",
                    Population = 47190493,
                    PresidentsCount = 8
                };

            var context = new OperationContext();

            // Act
            ITableEntity tableEntity = converter.GetEntity(country);
            IDictionary<string, EntityProperty> properties = tableEntity.WriteEntity(context);

            // Assert
            Assert.Equal("*", tableEntity.ETag);
            Assert.Equal(country.Continent, tableEntity.PartitionKey);
            Assert.Equal(country.Name, tableEntity.RowKey);
            Assert.Equal(country.Area, properties["Area"].DoubleValue);
            Assert.Equal(country.TopSecretKey, properties["TopSecretKey"].BinaryValue);
            Assert.True(properties["Formed"].DateTimeOffsetValue.HasValue);
            Assert.Equal(country.Formed, properties["Formed"].DateTimeOffsetValue.Value.DateTime);
            Assert.Equal(country.Id, properties["Id"].GuidValue);
            Assert.Equal(country.IsExists, properties["IsExists"].BooleanValue);
            Assert.Equal(country.Population, properties["Population"].Int64Value);
            Assert.Equal(country.PresidentsCount, properties["PresidentsCount"].Int32Value);
        }
        public void ConvertToTableEntity()
        {
            // Arrange
            var converter = new TableEntityConverter <Country>();
            var country   = new Country
            {
                Area            = 505992,
                Continent       = "Europe",
                TopSecretKey    = new byte[] { 0xaa, 0xbb, 0xcc },
                Formed          = new DateTime(1812, 1, 1),
                Id              = Guid.NewGuid(),
                IsExists        = true,
                Name            = "Spain",
                Population      = 47190493,
                PresidentsCount = 8
            };

            var context = new OperationContext();

            // Act
            ITableEntity tableEntity = converter.GetEntity(country);
            IDictionary <string, EntityProperty> properties = tableEntity.WriteEntity(context);

            // Assert
            Assert.Equal("*", tableEntity.ETag);
            Assert.Equal(country.Continent, tableEntity.PartitionKey);
            Assert.Equal(country.Name, tableEntity.RowKey);
            Assert.Equal(country.Area, properties["Area"].DoubleValue);
            Assert.Equal(country.TopSecretKey, properties["TopSecretKey"].BinaryValue);
            Assert.True(properties["Formed"].DateTimeOffsetValue.HasValue);
            Assert.Equal(country.Formed, properties["Formed"].DateTimeOffsetValue.Value.DateTime);
            Assert.Equal(country.Id, properties["Id"].GuidValue);
            Assert.Equal(country.IsExists, properties["IsExists"].BooleanValue);
            Assert.Equal(country.Population, properties["Population"].Int64Value);
            Assert.Equal(country.PresidentsCount, properties["PresidentsCount"].Int32Value);
        }
        public void ConvertToEntityWithNameMapping()
        {
            // Arrange
            var converter = new TableEntityConverter<LogEntry>();
            var tableEntity = new DynamicTableEntity
                {
                    PartitionKey = "My partiton key",
                    Properties = new Dictionary<string, EntityProperty>
                        {
                            {"OldMessage", new EntityProperty("My message")}
                        }
                };

            // Act
            LogEntry entity = converter.GetEntity(tableEntity);

            // Assert
            Assert.Equal(entity.Id, tableEntity.PartitionKey);
            Assert.Null(tableEntity.RowKey);
            Assert.Equal(entity.Message, tableEntity.Properties["OldMessage"].StringValue);
        }
        public void ConvertToTableEntityWithNameMapping()
        {
            // Arrange
            var converter = new TableEntityConverter<LogEntry>();
            var entity = new LogEntry
                {
                    Id = "My id",
                    Message = "My message"
                };

            var context = new OperationContext();

            // Act
            ITableEntity tableEntity = converter.GetEntity(entity);
            IDictionary<string, EntityProperty> properties = tableEntity.WriteEntity(context);

            // Assert
            Assert.Equal(null, tableEntity.ETag);
            Assert.Equal(entity.Id, tableEntity.PartitionKey);
            Assert.Equal(string.Empty, tableEntity.RowKey);
            Assert.Equal(entity.Message, properties["OldMessage"].StringValue);
        }
        public void DynamicToPocoMeasurement()
        {
            // Arrange
            var converter = new TableEntityConverter<Country>();
            DynamicTableEntity dynamicTableEntity = ObjectsFactory.GetCountryDynamicTableEntity();
            Stopwatch stopWatch = Stopwatch.StartNew();
            Country entity = null;

            // Act
            for (int i = 0; i < IteractionsCount; i++)
            {
                entity = converter.GetEntity(dynamicTableEntity);
            }

            stopWatch.Stop();

            Assert.NotNull(entity);

            Console.WriteLine(ResultFormat, IteractionsCount, stopWatch.ElapsedMilliseconds);
        }
        public void ConvertToTableEntityWithFewKeys()
        {
            // Arrange
            var converter = new TableEntityConverter<LogEntry>();
            var entry = new LogEntry
                {
                    Id = Guid.NewGuid().ToString("N"),
                    ETag = "MyETag",
                    Timestamp = DateTime.UtcNow,
                    Message = "My message",
                    PrivateData = new byte[] {0xaa, 0xbb, 0xcc}
                };

            var context = new OperationContext();

            // Act
            ITableEntity tableEntity = converter.GetEntity(entry);
            IDictionary<string, EntityProperty> properties = tableEntity.WriteEntity(context);

            // Assert
            Assert.Equal(entry.ETag, tableEntity.ETag);
            Assert.Equal(entry.Id, tableEntity.PartitionKey);
            Assert.Equal(string.Empty, tableEntity.RowKey);
            Assert.Equal(default(DateTimeOffset), tableEntity.Timestamp);
            Assert.Equal(entry.Message, properties["OldMessage"].StringValue);
            Assert.DoesNotContain("PrivateData", properties.Keys);
        }