Ejemplo n.º 1
0
        public async Task TimeSeriesInsightsTypes_Lifecycle()
        {
            // Arrange
            TimeSeriesInsightsClient client = GetClient();
            var timeSeriesTypes             = new List <TimeSeriesType>();
            var tsiTypeNamePrefix           = "type";
            int numOfTypesCreated           = 0;
            var timeSeriesTypesProperties   = new Dictionary <string, string>
            {
                { Recording.GenerateAlphaNumericId(tsiTypeNamePrefix), Recording.GenerateId() },
                { Recording.GenerateAlphaNumericId(tsiTypeNamePrefix), Recording.GenerateId() }
            };

            // Build aggregate variable
            var countExpression    = new TimeSeriesExpression("count()");
            var aggregateVariable  = new AggregateVariable(countExpression);
            var variables          = new Dictionary <string, TimeSeriesVariable>();
            var variableNamePrefix = "aggregateVariable";

            variables.Add(Recording.GenerateAlphaNumericId(variableNamePrefix), aggregateVariable);

            foreach (var property in timeSeriesTypesProperties)
            {
                var type = new TimeSeriesType(property.Key, variables);
                type.Id = property.Value;
                timeSeriesTypes.Add(type);
                numOfTypesCreated++;
            }

            // Act and assert
            try
            {
                // Get all Time Series types in the environment
                AsyncPageable <TimeSeriesType> getAllTypesResponse = client.GetTimeSeriesTypesAsync();

                await foreach (TimeSeriesType tsiType in getAllTypesResponse)
                {
                    tsiType.Should().NotBeNull();
                }

                // Create Time Series types
                Response <TimeSeriesOperationError[]> createTypesResult = await client
                                                                          .CreateOrReplaceTimeSeriesTypesAsync(timeSeriesTypes)
                                                                          .ConfigureAwait(false);

                // Assert that the result error array does not contain any object that is set
                createTypesResult.Value.Should().OnlyContain((errorResult) => errorResult == null);
                Response <TimeSeriesTypeOperationResult[]> getTypesByNamesResult;

                // This retry logic was added as the TSI types are not immediately available after creation
                await TestRetryHelper.RetryAsync <Response <TimeSeriesTypeOperationResult[]> >(async() =>
                {
                    // Get the created types by names
                    getTypesByNamesResult = await client
                                            .GetTimeSeriesTypesByNamesAsync(timeSeriesTypesProperties.Keys)
                                            .ConfigureAwait(false);

                    getTypesByNamesResult.Value.Should().OnlyContain((errorResult) => errorResult.Error == null);
                    getTypesByNamesResult.Value.Length.Should().Be(timeSeriesTypes.Count);
                    foreach (TimeSeriesTypeOperationResult typesResult in getTypesByNamesResult.Value)
                    {
                        typesResult.Error.Should().BeNull();
                        typesResult.TimeSeriesType.Should().NotBeNull();
                        typesResult.TimeSeriesType.Id.Should().NotBeNullOrEmpty();
                        typesResult.TimeSeriesType.Variables.Count.Should().Be(1);
                        typesResult.TimeSeriesType.Variables.IsSameOrEqualTo(variables);
                    }
                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);

                // Update variables with adding a new variable
                foreach (var type in timeSeriesTypes)
                {
                    type.Description = "Description";
                }

                Response <TimeSeriesOperationError[]> updateTypesResult = await client
                                                                          .CreateOrReplaceTimeSeriesTypesAsync(timeSeriesTypes)
                                                                          .ConfigureAwait(false);

                updateTypesResult.Value.Should().OnlyContain((errorResult) => errorResult == null);
                updateTypesResult.Value.Length.Should().Be(timeSeriesTypes.Count);

                // This retry logic was added as the TSI types are not immediately available after creation
                await TestRetryHelper.RetryAsync <Response <TimeSeriesTypeOperationResult[]> >(async() =>
                {
                    // Get type by Id
                    Response <TimeSeriesTypeOperationResult[]> getTypeByIdResult = await client
                                                                                   .GetTimeSeriesTypesByIdAsync(timeSeriesTypesProperties.Values)
                                                                                   .ConfigureAwait(false);

                    getTypeByIdResult.Value.Length.Should().Be(numOfTypesCreated);
                    foreach (TimeSeriesTypeOperationResult typeOperationResult in getTypeByIdResult.Value)
                    {
                        typeOperationResult.TimeSeriesType.Should().NotBeNull();
                        typeOperationResult.Error.Should().BeNull();
                        typeOperationResult.TimeSeriesType.Name.Should().StartWith(tsiTypeNamePrefix);
                        typeOperationResult.TimeSeriesType.Id.Should().NotBeNull();
                    }

                    return(null);
                }, MaxNumberOfRetries, s_retryDelay);
            }
            finally
            {
                // clean up
                try
                {
                    Response <TimeSeriesOperationError[]> deleteTypesResponse = await client
                                                                                .DeleteTimeSeriesTypesbyIdAsync(timeSeriesTypesProperties.Values)
                                                                                .ConfigureAwait(false);

                    // Assert that the response array does not have any error object set
                    deleteTypesResponse.Value.Should().OnlyContain((errorResult) => errorResult == null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Test clean up failed: {ex.Message}");
                    throw;
                }
            }
        }