public void ConfigurationSaveResults_AddErrors_ErrorsAddedFailureState() { ConfigurationSaveResults results = new ConfigurationSaveResults(); results.AddError("Test", DataStorageError.AlreadyExists); Assert.IsFalse(results.Successful, "Should fail due to error"); Assert.AreEqual(results.Errors.Count, 1, "Should only have one errors added"); results.Errors[0].WithDeepEqual(new ErrorMap() { Code = DataStorageError.AlreadyExists, Message = "Test" }).Assert(); }
public void SaveToDataStore(IEnumerable <SectionMigrated> sections) { try { using (ConfigurationStorage storage = new ConfigurationStorage(this._connectionString, new DefaultConsoleLogger())) { Database.SetInitializer(new CreateDatabaseIfNotExists <InAnotherCastleContext>()); foreach (var section in sections) { ConfigurationSaveResults currentResults = null; var addResults = currentResults = storage.AddConfigurationSection(new ConfigurationNew() { JSONSchema = section.JsonDetails.Schema, Name = section.SectionName, SystemName = null, //TODO, XML = section.XmlDetails.RawData, XMLSchema = section.XmlDetails.Schema }); if (addResults.Successful) { if (addResults.Record.JSON != section.JsonDetails.RawData) { var updateResults = currentResults = storage.UpdateConfigurationSectionJson(new ConfigurationUpdate() { Id = addResults.Record.Id, Value = section.JsonDetails.RawData }); } } if (!currentResults.Successful) { throw new MigrationException( "Validation issues occurred while put the migrated configuration into the data store: " + String.Join("\n", currentResults.Errors.Select(e => e.Code.ToString() + ":" + e.Message) ) , null); } } } } catch (SqlException sqlException) { throw new MigrationException("Sql exception occurred while put the migrated configuration into the data store", sqlException); } }
public ConfigurationSaveResults UpdateConfigurationSectionJson(ConfigurationUpdate update) { var results = new ConfigurationSaveResults(); var id = update.Id; var json = update.Value; ConfigurationValue record = Context.ConfigurationValues.Find(id); if (record != null) { if (ValidateJson(json, record.JSONSchema)) { var xmlSchema = GetSchema(record.XMLSchema); //TODO some checks on the type throw exception string xml = JsonConvert.DeserializeXmlNode(json).OuterXml; if (ValidateXml(xml, xmlSchema)) { record.XML = xml; record.JSON = json; Context.SetModified(record); var changes = Context.SaveChanges(); if (changes == 0) results.AddError("No records were removed ensure the id is correct.", DataStorageError.RecordsUnchanged); else results.Record = record; } else { results.AddError("The Xml does not match the schema", DataStorageError.DoesNotMatchXMlSchema); } } else { results.AddError("The Json does not match the schema", DataStorageError.DoesNotMatchJsonSchema); } } else { results.AddError("Record not found.", DataStorageError.RecordNotFound); } return results; }
public ConfigurationSaveResults UpdateConfigurationSectionXml(ConfigurationUpdate update) { var results = new ConfigurationSaveResults(); var id = update.Id; var xml = update.Value; ConfigurationValue record = Context.ConfigurationValues.Find(id); if (record != null) { var xmlSchema = GetSchema(record.XMLSchema); XmlDocument document = null; if (ValidateXml(xml, xmlSchema, out document)) { this.AssignJsonArrayAttributes(document, document.ChildNodes); string json = JsonConvert.SerializeXmlNode(document, Newtonsoft.Json.Formatting.Indented, false); if (ValidateJson(json, record.JSONSchema)) { record.XML = xml; record.JSON = json; Context.SetModified(record); var changes = Context.SaveChanges(); if (changes == 0) results.AddError("No records were removed ensure the id is correct.", DataStorageError.RecordsUnchanged); else results.Record = record; } else { results.AddError("The Json does not match the schema", DataStorageError.DoesNotMatchJsonSchema); } } else { results.AddError("The Xml does not match the schema", DataStorageError.DoesNotMatchXMlSchema); } } else { results.AddError("Record not found.", DataStorageError.RecordNotFound); } return results; }
public void ConfigurationSaveResults_AddErrorsChained_ErrorsAddedFailureState() { ConfigurationSaveResults results = new ConfigurationSaveResults(); results.AddError("Test1", DataStorageError.AlreadyExists).AddError("Test2", DataStorageError.RecordNotFound); Assert.IsFalse(results.Successful, "Should fail due to errors"); Assert.AreEqual(results.Errors.Count, 2, "Should only have two errors added"); results.Errors.OrderBy(r => r.Message).WithDeepEqual(new List <ErrorMap>() { new ErrorMap() { Code = DataStorageError.AlreadyExists, Message = "Test1" }, new ErrorMap() { Code = DataStorageError.RecordNotFound, Message = "Test2" } }.OrderBy(r => r.Message)).Assert(); }
public ConfigurationSaveResults RemoveConfigurationSection(int id) { var results = new ConfigurationSaveResults(); //If instance exists locally us it, or else this will cause an error. ConfigurationValue record = Context.ConfigurationValues.Local.FirstOrDefault(c => c.Id == id) ?? new ConfigurationValue() { Id = id }; Context.ConfigurationValues.Attach(record); record.Deleted = true; Context.SetModified(record); var changes = Context.SaveChanges(); if (changes == 0) { results.AddError("No records were removed ensure the id is correct.", DataStorageError.RecordsUnchanged); } else results.Record = record; return results; }
public void ConfigurationSaveResults_NoErrors_SuccessfulState() { ConfigurationSaveResults results = new ConfigurationSaveResults(); Assert.IsTrue(results.Successful, "Should pass due to no errorsw"); }
private ConfigurationSaveResults AddUpdateConfigurationSection(int? id, ConfigurationNew newConfiguration) { var results = new ConfigurationSaveResults() { }; if (string.IsNullOrWhiteSpace(newConfiguration.Name)) { results.AddError("The Name field cannot be empty", DataStorageError.MissingNameField); } else { ConfigurationValue record = new ConfigurationValue(); var existingRecordByNames = GetConfigurationSection(newConfiguration.Name, newConfiguration.SystemName); if (id.HasValue) { record = GetConfigurationSection(id.Value); if (record == null) { results.AddError("Record not found for the requested id", DataStorageError.RecordNotFound); return results; } else if (existingRecordByNames != null && record.Id == existingRecordByNames.Id) { //This means the existing record by this name matches, otherwise the logic below will return the existing name error existingRecordByNames = null; } } //Previously deleted, will be brought back, it history should be maintained in the history tables. if (existingRecordByNames != null && existingRecordByNames.Deleted) { record = existingRecordByNames; existingRecordByNames = null; } if (existingRecordByNames == null) { record.JSONSchema = newConfiguration.JSONSchema; record.Name = newConfiguration.Name; record.SystemName = newConfiguration.SystemName; record.XML = newConfiguration.XML; record.XMLSchema = newConfiguration.XMLSchema; record.Deleted = false; //Tags taken care of below var xmlSchema = GetSchema(record.XMLSchema); if (xmlSchema != null) { XmlDocument document = null; if (ValidateXml(record.XML, xmlSchema, out document)) { this.AssignJsonArrayAttributes(document, document.ChildNodes); string json = JsonConvert.SerializeXmlNode(document, Newtonsoft.Json.Formatting.Indented, false); if (ValidateJson(json, record.JSONSchema)) { record.JSON = json; if (id.HasValue) { Context.SetModified(record); } else { Context.ConfigurationValues.Add(record); Context.SetAdded(record); } var tagErrors = this.AddUpdateTagsConfigurationTags(record, newConfiguration.Tags); if (tagErrors == null) { var changes = Context.SaveChanges(); if (changes == 0) results.AddError("No records were removed ensure the id is correct.", DataStorageError.RecordsUnchanged); else results.Record = record; } else { results.Errors.Add(tagErrors); } } else { results.AddError("The Json does not match the schema", DataStorageError.DoesNotMatchJsonSchema); } } else { results.AddError("The Xml does not match the schema", DataStorageError.DoesNotMatchXMlSchema); } } else { results.AddError("The Xml Schema is invalid", DataStorageError.InvalidXmlSchema); } } else { results.AddError("Name and System Name already exist", DataStorageError.AlreadyExists); } } if(!results.Successful) { Context.ClearChanges(); } return results; }