Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TablePropertyControlViewModel{T}"/> class.
        /// </summary>
        /// <param name="tableAlertProperty">The table alert property that should be displayed.</param>
        public TablePropertyControlViewModel(TableAlertProperty <T> tableAlertProperty)
        {
            // Generate a table for the given table alert property
            var table = new DataTable();

            foreach (var column in tableAlertProperty.Columns)
            {
                var dataColumn = new DataColumn(column.DisplayName);
                table.Columns.Add(dataColumn);
            }

            foreach (object value in tableAlertProperty.Values)
            {
                var newRow = table.NewRow();
                if (value is IDictionary dictionaryValue)
                {
                    foreach (var tableColumn in tableAlertProperty.Columns)
                    {
                        newRow[tableColumn.DisplayName] = dictionaryValue[tableColumn.PropertyName].ToString();
                    }
                }
                else
                {
                    foreach (var tableColumn in tableAlertProperty.Columns)
                    {
                        newRow[tableColumn.DisplayName] = value.GetType().GetProperty(tableColumn.PropertyName)?.GetValue(value, null)?.ToString();
                    }
                }

                table.Rows.Add(newRow);
            }

            this.Table = table;
        }
Ejemplo n.º 2
0
        public void WhenCreatingContractsAlertThenTablePropertiesAreConvertedCorrectly()
        {
            ContractsAlert contractsAlert = CreateContractsAlert <TestAlert>();

            Assert.AreEqual(2, contractsAlert.AlertProperties.Count);

            int propertyIndex = 0;
            TableAlertProperty <string> singleColumnTableAlertProperty = (TableAlertProperty <string>)contractsAlert.AlertProperties[propertyIndex];

            Assert.AreEqual("SingleColumnTable", singleColumnTableAlertProperty.PropertyName);
            Assert.AreEqual(AlertPropertyType.Table, singleColumnTableAlertProperty.Type);
            Assert.AreEqual("SingleColumnTableDisplayName", singleColumnTableAlertProperty.DisplayName);
            Assert.AreEqual(0, singleColumnTableAlertProperty.Order);
            Assert.AreEqual(false, singleColumnTableAlertProperty.ShowHeaders);

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

            Assert.AreEqual(0, singleColumnTableAlertProperty.Columns.Count);

            propertyIndex++;
            TableAlertProperty <Dictionary <string, string> > multiColumnTableAlertProperty = (TableAlertProperty <Dictionary <string, string> >)contractsAlert.AlertProperties[propertyIndex];

            Assert.AreEqual("Table", multiColumnTableAlertProperty.PropertyName);
            Assert.AreEqual(AlertPropertyType.Table, multiColumnTableAlertProperty.Type);
            Assert.AreEqual("TableDisplayName", multiColumnTableAlertProperty.DisplayName);
            Assert.AreEqual(1, multiColumnTableAlertProperty.Order);
            Assert.AreEqual(true, multiColumnTableAlertProperty.ShowHeaders);

            Assert.AreEqual(2, multiColumnTableAlertProperty.Values.Count);
            Assert.AreEqual(4, multiColumnTableAlertProperty.Values[0].Count);
            Assert.AreEqual("p11", multiColumnTableAlertProperty.Values[0]["Prop1"]);
            Assert.AreEqual("The value of Prop2 is p21", multiColumnTableAlertProperty.Values[0]["Prop2"]);
            Assert.AreEqual("p31", multiColumnTableAlertProperty.Values[0]["Prop3"]);
            Assert.AreEqual("<a href=\"http://microsoft.com/\">Link for NDP1</a>", multiColumnTableAlertProperty.Values[0]["UriProp"]);

            Assert.AreEqual(4, multiColumnTableAlertProperty.Values[1].Count);
            Assert.AreEqual("p12", multiColumnTableAlertProperty.Values[1]["Prop1"]);
            Assert.AreEqual("The value of Prop2 is p22", multiColumnTableAlertProperty.Values[1]["Prop2"]);
            Assert.AreEqual("p32", multiColumnTableAlertProperty.Values[1]["Prop3"]);
            Assert.AreEqual("<a href=\"http://contoso.com/\">Link for NDP2</a>", multiColumnTableAlertProperty.Values[1]["UriProp"]);

            Assert.AreEqual(4, multiColumnTableAlertProperty.Columns.Count);
            Assert.AreEqual("Prop1", multiColumnTableAlertProperty.Columns[0].PropertyName);
            Assert.AreEqual("First Prop", multiColumnTableAlertProperty.Columns[0].DisplayName);
            Assert.AreEqual("Prop2", multiColumnTableAlertProperty.Columns[1].PropertyName);
            Assert.AreEqual("Second Prop", multiColumnTableAlertProperty.Columns[1].DisplayName);
            Assert.AreEqual("UriProp", multiColumnTableAlertProperty.Columns[2].PropertyName);
            Assert.AreEqual("Uri Prop", multiColumnTableAlertProperty.Columns[2].DisplayName);
            Assert.AreEqual("Prop3", multiColumnTableAlertProperty.Columns[3].PropertyName);
            Assert.AreEqual("Third Prop, without order", multiColumnTableAlertProperty.Columns[3].DisplayName);
        }
Ejemplo n.º 3
0
        public void WhenCreatingNewViewModelThenItWasInitializedCorrectly()
        {
            var columns = new List <TableColumn>()
            {
                new TableColumn(nameof(TestTableAlertPropertyValue.FirstName), "First Name"),
                new TableColumn(nameof(TestTableAlertPropertyValue.LastName), "Last Name"),
                new TableColumn(nameof(TestTableAlertPropertyValue.Goals), "Goals avg")
            };

            var rows = new List <TestTableAlertPropertyValue>()
            {
                new TestTableAlertPropertyValue()
                {
                    FirstName = "Edinson", LastName = "Cavani", Goals = 4.67
                },
                new TestTableAlertPropertyValue()
                {
                    FirstName = "Fernando", LastName = "Torres", Goals = 1.7
                }
            };

            var tableAlertProperty = new TableAlertProperty <TestTableAlertPropertyValue>("propertyName", "displayName", 5, true, columns, rows);

            var tablePropertyControlViewModel = new TablePropertyControlViewModel <TestTableAlertPropertyValue>(tableAlertProperty);

            DataTable generateDataTable = tablePropertyControlViewModel.Table;

            Assert.IsNotNull(generateDataTable);

            // Verify generated table coloumns
            Assert.AreEqual(columns.Count, generateDataTable.Columns.Count, "The generated table has unexpected number of columns");
            for (int i = 0; i < columns.Count; i++)
            {
                Assert.AreEqual(columns[i].DisplayName, generateDataTable.Columns[i].ColumnName, $"The generated table's column in index {i} has unexpected name");
            }

            // Verify generated table rows values
            for (int i = 0; i < rows.Count; i++)
            {
                Assert.AreEqual(rows[i].FirstName, generateDataTable.Rows[i]["First Name"], $"Unexpected value in row: {i}, column: 'First Name'");
                Assert.AreEqual(rows[i].LastName, generateDataTable.Rows[i]["Last Name"], $"Unexpected value in row: {i}, column: 'Last Name'");
                Assert.AreEqual(rows[i].Goals.ToString("G", CultureInfo.InvariantCulture), generateDataTable.Rows[i]["Goals avg"], $"Unexpected value in row: {i}, column: 'Goals avg'");
            }
        }
Ejemplo n.º 4
0
        public void WhenConvertingTablePropertyThenResultIsTablePropertyControlViewModel()
        {
            var columns = new List <TableColumn>()
            {
                new TableColumn(nameof(TestTableAlertPropertyValue.FirstName), "First Name"),
            };

            var rows = new List <TestTableAlertPropertyValue>()
            {
                new TestTableAlertPropertyValue()
                {
                    FirstName = "Edinson", LastName = "Cavani", Goals = 4.67
                },
            };

            var tableAlertProperty = new TableAlertProperty <TestTableAlertPropertyValue>("propertyName", "displayName", 5, true, columns, rows);
            var converter          = new TablePropertyToTablePropertyControlViewModelConverter();

            object result = converter.Convert(tableAlertProperty, typeof(TablePropertyControlViewModel <TestTableAlertPropertyValue>), null, new CultureInfo("en-us"));

            Assert.IsInstanceOfType(result, typeof(TablePropertyControlViewModel <TestTableAlertPropertyValue>));
        }
Ejemplo n.º 5
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}'");
            }
        }