From bb9e4c7eef7b09f794440508d42a73c0784d057c Mon Sep 17 00:00:00 2001 From: jarekrzdbk Date: Fri, 19 Aug 2016 15:27:46 +0300 Subject: [PATCH 1/2] refactored Validation in Write To Sql Server activity --- .../Activities/Write_To_Sql_Server_v1.cs | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/terminalAzure/Activities/Write_To_Sql_Server_v1.cs b/terminalAzure/Activities/Write_To_Sql_Server_v1.cs index b240b436c0..485926ec98 100644 --- a/terminalAzure/Activities/Write_To_Sql_Server_v1.cs +++ b/terminalAzure/Activities/Write_To_Sql_Server_v1.cs @@ -35,7 +35,7 @@ public class Write_To_Sql_Server_v1 : ExplicitTerminalActivity protected override ActivityTemplateDTO MyTemplate => ActivityTemplateDTO; - public Write_To_Sql_Server_v1(ICrateManager crateManager, IDbProvider dbProvider) + public Write_To_Sql_Server_v1(ICrateManager crateManager, IDbProvider dbProvider) : base(crateManager) { _dbProvider = dbProvider; @@ -52,8 +52,6 @@ public override Task Initialize() //if the user provides a connection string, this action attempts to connect to the sql server and get its columns and tables public override Task FollowUp() { - //Verify controls, make sure that TextBox with value exists - ValidateControls(); //In all followup calls, update data fields of the configuration store try { @@ -71,16 +69,16 @@ public override Task FollowUp() var textSourceControls = ConfigurationControls.Controls.OfType(); foreach (var control in textSourceControls.ToList()) { - RemoveControl(control.Name); + RemoveControl(control.Name); } var columns = GetTableColumns(dropDownControl.Value); foreach (var column in columns) { - if(identityColumn != column.ColumnName) + if (identityColumn != column.ColumnName) { var textSource = UiBuilder.CreateSpecificOrUpstreamValueChooser(column.ColumnName, column.ColumnName); - + if (column.isNullable == false) { textSource.Required = true; @@ -94,7 +92,7 @@ public override Task FollowUp() //this needs to be updated to hold Crates instead of FieldDefinitionDTO // Storage.Add("Sql Table Columns", new KeyValueListCM(contentsList.Select(col => new KeyValueDTO { Key = col, Value = col }))); } - catch(Exception e) + catch (Exception e) { AddErrorToControl(); } @@ -224,7 +222,7 @@ private void ListTableColumns(string connectionString, string tableName, Action< //public List GetFieldMappings() //{ // var connStringField = ConfigurationControls.Controls.First(); - + // return (List)_dbProvider.ConnectToSql(connStringField.Value, (command) => // { // command.CommandText = FieldMappingQuery; @@ -246,24 +244,44 @@ private void ListTableColumns(string connectionString, string tableName, Action< // }); //} - private void ValidateControls() + protected override Task Validate() { - if (Storage.Count == 0) + + var connStringField = ConfigurationControls.Controls.First(); + if (string.IsNullOrEmpty(connStringField?.Value)) { - throw new TerminalCodedException(TerminalErrorCode.SQL_SERVER_CONNECTION_STRING_MISSING); + ValidationManager.SetError("Connection string can't be empty", connStringField); + } + else + { + try + { + var columns = GetTables(); + } + catch (Exception e) + { + ValidationManager.SetError(e.Message, connStringField); + } } - if (ConfigurationControls == null) + var dropDownControl = ConfigurationControls.Controls.OfType().FirstOrDefault(); + if (string.IsNullOrEmpty(dropDownControl?.Value) && dropDownControl.ListItems.Count > 0) { - throw new TerminalCodedException(TerminalErrorCode.SQL_SERVER_CONNECTION_STRING_MISSING); + ValidationManager.SetError("Table must be selected", dropDownControl); } - var connStringField = ConfigurationControls.Controls.First(); - if (string.IsNullOrEmpty(connStringField?.Value)) + + var textSourceControls = ConfigurationControls.Controls.OfType(); + foreach (var control in textSourceControls) { - throw new TerminalCodedException(TerminalErrorCode.SQL_SERVER_CONNECTION_STRING_MISSING); + if (control.Required == true && string.IsNullOrEmpty(control.TextValue)) + { + ValidationManager.SetError("Column is not nullable", control); + } } + return Task.FromResult(0); } + private void AddErrorToControl() { var connStringTextBox = GetControl("connection_string"); @@ -361,7 +379,7 @@ private IEnumerable CreateTable() var values = new List(); foreach (var control in textSourceControls) { - if(!string.IsNullOrEmpty(control.TextValue)) + if (!string.IsNullOrEmpty(control.TextValue)) { values.Add(new FieldValue(control.Name, control.TextValue)); } From ad4f1357f4d6625afe47a6f7c91a9aa9d38d959c Mon Sep 17 00:00:00 2001 From: jarekrzdbk Date: Fri, 19 Aug 2016 16:53:32 +0300 Subject: [PATCH 2/2] fixed test --- .../Write_To_Sql_Server_v1_Tests.cs | 4 +-- .../Activities/Write_To_Sql_Server_v1.cs | 32 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Tests/terminalAzureTests/Integration/Write_To_Sql_Server_v1_Tests.cs b/Tests/terminalAzureTests/Integration/Write_To_Sql_Server_v1_Tests.cs index 35a3e1cadc..408bf01f55 100644 --- a/Tests/terminalAzureTests/Integration/Write_To_Sql_Server_v1_Tests.cs +++ b/Tests/terminalAzureTests/Integration/Write_To_Sql_Server_v1_Tests.cs @@ -169,13 +169,13 @@ await HttpPostAsync( Assert.NotNull(responseActionDTO.CrateStorage); var crateStorage = Crate.FromDto(responseActionDTO.CrateStorage); //There will be no DesignTimeCrate only Configuration crate - Assert.AreEqual(1, crateStorage.Count); + Assert.AreEqual(2, crateStorage.Count); Assert.AreEqual(1, crateStorage.CratesOfType().Count()); var controls = crateStorage.CrateContentsOfType().Single(); AssertConfigureControls(controls); //Check that Error message is shown var connStringTextBox = (TextBox)controls.Controls[0]; - Assert.AreEqual("Incorrect Connection String", connStringTextBox.Value); + Assert.AreEqual("This is incorrect database connection string!", connStringTextBox.Value); } /// diff --git a/terminalAzure/Activities/Write_To_Sql_Server_v1.cs b/terminalAzure/Activities/Write_To_Sql_Server_v1.cs index 485926ec98..56e6a845fd 100644 --- a/terminalAzure/Activities/Write_To_Sql_Server_v1.cs +++ b/terminalAzure/Activities/Write_To_Sql_Server_v1.cs @@ -247,30 +247,36 @@ private void ListTableColumns(string connectionString, string tableName, Action< protected override Task Validate() { - var connStringField = ConfigurationControls.Controls.First(); - if (string.IsNullOrEmpty(connStringField?.Value)) - { - ValidationManager.SetError("Connection string can't be empty", connStringField); - } - else + var connStringField = ConfigurationControls.Controls.FirstOrDefault(); + if(connStringField != null) { - try + if (string.IsNullOrEmpty(connStringField.Value)) { - var columns = GetTables(); + ValidationManager.SetError("Connection string can't be empty", connStringField); } - catch (Exception e) + else { - ValidationManager.SetError(e.Message, connStringField); + try + { + var columns = GetTables(); + } + catch (Exception e) + { + ValidationManager.SetError(e.Message, connStringField); + } } } + var dropDownControl = ConfigurationControls.Controls.OfType().FirstOrDefault(); - if (string.IsNullOrEmpty(dropDownControl?.Value) && dropDownControl.ListItems.Count > 0) + if(dropDownControl != null) { - ValidationManager.SetError("Table must be selected", dropDownControl); + if (string.IsNullOrEmpty(dropDownControl?.Value) && dropDownControl.ListItems.Count > 0) + { + ValidationManager.SetError("Table must be selected", dropDownControl); + } } - var textSourceControls = ConfigurationControls.Controls.OfType(); foreach (var control in textSourceControls) {