Example #1
0
        /// <summary>
        /// Retrieves and composes Alert propeties, for an Arm request Alert property.
        /// </summary>
        /// <param name="armProperty">Alert property of type ARM request to retrieve and compose</param>
        /// <param name="armClient">Support for ARM request</param>
        /// <returns>List of displayable properties from Arm request</returns>
        private async Task <List <DisplayableAlertProperty> > ComposeArmProperties(AzureResourceManagerRequestAlertProperty armProperty, IAzureResourceManagerClient armClient)
        {
            List <DisplayableAlertProperty> displayableArmProperties = new List <DisplayableAlertProperty>();

            try
            {
                List <JObject> response = await armClient.ExecuteArmQueryAsync(armProperty.AzureResourceManagerRequestUri, CancellationToken.None);

                foreach (IReferenceAlertProperty propertyRef in armProperty.PropertiesToDisplay.OfType <IReferenceAlertProperty>())
                {
                    JToken propertyVal = response[0].SelectToken(propertyRef.ReferencePath);

                    if (propertyVal == null)
                    {
                        displayableArmProperties.Add(this.CreateErrorProperty(propertyRef, $"Property {propertyRef.ReferencePath} doesn't exist in Response"));
                    }
                    else
                    {
                        switch (propertyRef)
                        {
                        case TextReferenceAlertProperty textRef:
                            TextAlertProperty displayText = new TextAlertProperty(textRef.PropertyName, textRef.DisplayName, textRef.Order, (string)propertyVal);
                            displayableArmProperties.Add(displayText);
                            break;

                        case LongTextReferenceAlertProperty longTextRef:
                            LongTextAlertProperty displayLongText = new LongTextAlertProperty(longTextRef.PropertyName, longTextRef.DisplayName, longTextRef.Order, (string)propertyVal);
                            displayableArmProperties.Add(displayLongText);
                            break;

                        case KeyValueReferenceAlertProperty keyValueRef:
                            IDictionary <string, string> keyValueField = new Dictionary <string, string> {
                                { keyValueRef.ReferencePath, (string)propertyVal }
                            };
                            KeyValueAlertProperty displayKeyValue = new KeyValueAlertProperty(keyValueRef.PropertyName, keyValueRef.DisplayName, keyValueRef.Order, keyValueField);
                            displayableArmProperties.Add(displayKeyValue);
                            break;

                        case TableReferenceAlertProperty tableRef:
                            JArray tableValue;
                            if (response.Count == 1)
                            {
                                tableValue = response[0].SelectToken(tableRef.ReferencePath) as JArray;
                            }
                            else
                            {
                                tableValue = (new JArray(response)).SelectToken(tableRef.ReferencePath) as JArray;
                            }

                            List <Dictionary <string, JToken> > values = tableValue
                                                                         .OfType <IDictionary <string, JToken> >()
                                                                         .Select(value => value.ToDictionary(item => item.Key, item => item.Value))
                                                                         .ToList();
                            TableAlertProperty <Dictionary <string, JToken> > displayTable = new TableAlertProperty <Dictionary <string, JToken> >(tableRef.PropertyName, tableRef.DisplayName, tableRef.Order, true, tableRef.Columns, values);
                            displayableArmProperties.Add(displayTable);
                            break;
                        }
                    }
                }
            }
            catch (HttpRequestException e)
            {
                string errorValue = $"Failed to get Arm Response, Error: {e.Message}";

                foreach (IReferenceAlertProperty propertyRef in armProperty.PropertiesToDisplay.OfType <IReferenceAlertProperty>())
                {
                    displayableArmProperties.Add(this.CreateErrorProperty(propertyRef, errorValue));
                }
            }

            return(displayableArmProperties);
        }
        private static void VerifyPresentationTestAlertDisplayedProperty(List <AlertProperty> properties, string propertyName, string displayName, byte order)
        {
            var property = properties.SingleOrDefault(p => p.PropertyName == propertyName);

            Assert.IsNotNull(property, $"Property {propertyName} not found");

            Assert.IsInstanceOfType(property, typeof(DisplayableAlertProperty));
            Assert.AreEqual(order, ((DisplayableAlertProperty)property).Order);
            Assert.AreEqual(displayName, ((DisplayableAlertProperty)property).DisplayName);

            if (propertyName == "LongTextPropertyName")
            {
                Assert.AreEqual("LongTextValue", ((LongTextAlertProprety)property).Value);
            }
            else if (propertyName == "UrlValue")
            {
                Assert.AreEqual("<a href=\"https://www.bing.com/\">LinkText1</a>", ((TextAlertProperty)property).Value);
            }
            else if (propertyName == "TextValue")
            {
                Assert.AreEqual("TextValue", ((TextAlertProperty)property).Value);
            }
            else if (propertyName == "KeyValue")
            {
                KeyValueAlertProperty alertProperty = (KeyValueAlertProperty)property;
                Assert.AreEqual("value1", alertProperty.Value["key1"]);
                Assert.AreEqual(false, alertProperty.ShowHeaders);
            }
            else if (propertyName == "KeyValueWithHeaders")
            {
                KeyValueAlertProperty alertProperty = (KeyValueAlertProperty)property;
                Assert.AreEqual("value1", alertProperty.Value["key1"]);
                Assert.AreEqual(true, alertProperty.ShowHeaders);
                Assert.AreEqual("Keys", alertProperty.KeyHeaderName);
                Assert.AreEqual("Values1", alertProperty.ValueHeaderName);
            }
            else if (propertyName == "DataPoints")
            {
                ChartAlertProperty alertProperty = (ChartAlertProperty)property;

                Assert.AreEqual(1, alertProperty.DataPoints.Count);
                Assert.AreEqual(new DateTime(2018, 7, 9, 14, 31, 0, DateTimeKind.Utc), alertProperty.DataPoints[0].X);
                Assert.AreEqual(5, alertProperty.DataPoints[0].Y);

                Assert.AreEqual(ContractsChartType.LineChart, alertProperty.ChartType);
                Assert.AreEqual(ContractsChartAxisType.Date, alertProperty.XAxisType);
                Assert.AreEqual(ContractsChartAxisType.Number, alertProperty.YAxisType);
            }
            else if (propertyName == "Table")
            {
                TableAlertProperty alertProperty = (TableAlertProperty)property;
                Assert.AreEqual(true, alertProperty.ShowHeaders);

                Assert.AreEqual(2, alertProperty.Values.Count);
                Assert.IsInstanceOfType(alertProperty.Values[0], typeof(TableData));
                Assert.AreEqual("p11", ((TableData)alertProperty.Values[0]).Prop1);
                Assert.AreEqual("p21", ((TableData)alertProperty.Values[0]).Prop2);
                Assert.AreEqual("NDP1", ((TableData)alertProperty.Values[0]).NonDisplayProp);

                Assert.IsInstanceOfType(alertProperty.Values[1], typeof(TableData));
                Assert.AreEqual("p12", ((TableData)alertProperty.Values[1]).Prop1);
                Assert.AreEqual("p22", ((TableData)alertProperty.Values[1]).Prop2);
                Assert.AreEqual("NDP2", ((TableData)alertProperty.Values[1]).NonDisplayProp);

                Assert.AreEqual(2, alertProperty.Columns.Count);
                Assert.AreEqual("prop1", alertProperty.Columns[0].PropertyName);
                Assert.AreEqual("First Prop", alertProperty.Columns[0].DisplayName);
                Assert.AreEqual("Prop2", alertProperty.Columns[1].PropertyName);
                Assert.AreEqual("Second Prop", alertProperty.Columns[1].DisplayName);
            }
            else if (propertyName == "SingleColumnTable")
            {
                TableAlertProperty alertProperty = (TableAlertProperty)property;
                Assert.AreEqual(false, alertProperty.ShowHeaders);

                Assert.AreEqual(3, alertProperty.Values.Count);
                Assert.AreEqual("value1", alertProperty.Values[0]);
                Assert.AreEqual("value2", alertProperty.Values[1]);
                Assert.AreEqual("value3", alertProperty.Values[2]);

                Assert.AreEqual(0, alertProperty.Columns.Count);
            }
            else
            {
                Assert.Fail($"Unknown property '{propertyName}'");
            }
        }