public void ReturnsEmptyCollectionForEqualValidationContexts() { var fieldValidationResults1 = new List <IFieldValidationResult>(); fieldValidationResults1.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning")); fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError")); var businessRuleValidationResults1 = new List <IBusinessRuleValidationResult>(); businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning")); businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError")); var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1); var fieldValidationResults2 = new List <IFieldValidationResult>(); fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning")); fieldValidationResults2.Add(FieldValidationResult.CreateError("MyProperty", "FieldError")); var businessRuleValidationResults2 = new List <IBusinessRuleValidationResult>(); businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning")); businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError")); var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2); var changes = ValidationContextHelper.GetChanges(context1, context2); Assert.AreEqual(0, changes.Count); }
public void ReturnsChangesForAddedFieldWarning() { var fieldValidationResults1 = new List <IFieldValidationResult>(); fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError")); var businessRuleValidationResults1 = new List <IBusinessRuleValidationResult>(); businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning")); businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError")); var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1); var fieldValidationResults2 = new List <IFieldValidationResult>(); fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning")); fieldValidationResults2.Add(FieldValidationResult.CreateError("MyProperty", "FieldError")); var businessRuleValidationResults2 = new List <IBusinessRuleValidationResult>(); businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning")); businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError")); var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2); var changes = ValidationContextHelper.GetChanges(context1, context2); Assert.AreEqual(1, changes.Count); Assert.AreEqual("MyProperty", ((IFieldValidationResult)changes[0].ValidationResult).PropertyName); Assert.AreEqual("FieldWarning", changes[0].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Warning, changes[0].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Added, changes[0].ChangeType); }
public void SetsValuesCorrectlyUsingNormalMessage() { var validationResult = BusinessRuleValidationResult.CreateWarning("my message"); Assert.AreEqual(ValidationResultType.Warning, validationResult.ValidationResultType); Assert.AreEqual("my message", validationResult.Message); }
public void SetsValuesCorrectlyUsingFormattedMessage() { var validationResult = BusinessRuleValidationResult.CreateWarning("my message with {0}", "format"); Assert.AreEqual(ValidationResultType.Warning, validationResult.ValidationResultType); Assert.AreEqual("my message with format", validationResult.Message); }
protected override void ValidateBusinessRules(List <IBusinessRuleValidationResult> validationResults) { if (string.IsNullOrEmpty(FirstName) || string.IsNullOrEmpty(LastName)) { validationResults.Add(BusinessRuleValidationResult.CreateError("Both first and last name are required")); } if (string.IsNullOrEmpty(MiddleName)) { validationResults.Add(BusinessRuleValidationResult.CreateWarning("No middle name")); } }
public IValidationContext Validate(string machineIdToValidate) { Argument.IsNotNullOrWhitespace(() => machineIdToValidate); var validationContext = new ValidationContext(); Log.Debug("Retrieving machine id"); var machineId = _identificationService.GetMachineId(); Log.Debug("Validating machine id '{0}' against expected machine id '{1}'", machineId, machineIdToValidate); var machineSplitted = machineId.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None); var expectedSplitter = machineIdToValidate.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None); if (machineSplitted.Length != expectedSplitter.Length) { var error = "The number of items inside the license differ too much, assuming machine ids do not match"; Log.Error(error); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(error)); return(validationContext); } var invalidEntries = 0; for (var i = 0; i < machineSplitted.Length; i++) { if (!string.Equals(expectedSplitter[i], machineSplitted[i], StringComparison.OrdinalIgnoreCase)) { invalidEntries++; } } if (invalidEntries > Threshold) { var error = string.Format("{0} values are not equal, not accepting the machine id, maximum threshold is '{1}'", invalidEntries, Threshold); Log.Error(error); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(error)); return(validationContext); } if (invalidEntries > 0) { var warning = string.Format("One of the values is not equal, but we have a threshold of {0} so accepting machine id", Threshold); Log.Warning(warning); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(warning)); } return(validationContext); }
protected override void ValidateBusinessRules(List <IBusinessRuleValidationResult> validationResults) { if (string.IsNullOrEmpty(BusinessRuleErrorWhenEmpty)) { validationResults.Add(BusinessRuleValidationResult.CreateError("BusinessRuleErrorWhenEmpty should not be empty")); } if (string.IsNullOrEmpty(BusinessRuleWarningWhenEmpty)) { validationResults.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarningWhenEmpty should not be empty")); } }
/// <summary> /// Validates the business rules. /// </summary> protected override void ValidateBusinessRules(List <IBusinessRuleValidationResult> validationResults) { if (ValueToValidate == ValueThatCausesBusinessWarning) { validationResults.Add(BusinessRuleValidationResult.CreateWarning("Business rule warning")); } if (ValueToValidate == ValueThatCausesBusinessError) { validationResults.Add(BusinessRuleValidationResult.CreateError("Business rule error")); } }
/// <summary> /// Called when the object is validating the business rules. /// </summary> /// <param name="validationContext">The validation context.</param> protected override void OnValidatingBusinessRules(IValidationContext validationContext) { base.OnValidatingBusinessRules(validationContext); lock (_modelLock) { foreach (var modelObject in _modelObjects) { if (!_modelObjectsInfo[modelObject.Key].SupportValidation) { continue; } // IDataErrorInfo var dataErrorInfo = modelObject.Value as IDataErrorInfo; if (dataErrorInfo != null && !string.IsNullOrEmpty(dataErrorInfo.Error)) { validationContext.Add(BusinessRuleValidationResult.CreateError(dataErrorInfo.Error)); } // IDataWarningInfo var dataWarningInfo = modelObject.Value as IDataWarningInfo; if (dataWarningInfo != null && !string.IsNullOrEmpty(dataWarningInfo.Warning)) { validationContext.Add(BusinessRuleValidationResult.CreateWarning(dataWarningInfo.Warning)); } // INotifyDataErrorInfo & INotifyDataWarningInfo ModelErrorInfo modelErrorInfo; if (_modelErrorInfo.TryGetValue(modelObject.Key, out modelErrorInfo)) { foreach (var error in modelErrorInfo.GetErrors(string.Empty)) { validationContext.Add(BusinessRuleValidationResult.CreateError(error)); } foreach (var warning in modelErrorInfo.GetWarnings(string.Empty)) { validationContext.Add(BusinessRuleValidationResult.CreateWarning(warning)); } } } } }
/// <summary> /// Called when the object is validating the business rules. /// </summary> protected override void OnValidatingBusinessRules() { base.OnValidatingBusinessRules(); lock (_modelObjects) { foreach (KeyValuePair <string, object> modelObject in _modelObjects) { // IDataErrorInfo var dataErrorInfo = modelObject.Value as IDataErrorInfo; if ((dataErrorInfo != null) && !string.IsNullOrEmpty(dataErrorInfo.Error)) { SetBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(dataErrorInfo.Error)); } // IDataWarningInfo var dataWarningInfo = modelObject.Value as IDataWarningInfo; if ((dataWarningInfo != null) && !string.IsNullOrEmpty(dataWarningInfo.Warning)) { SetBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(dataWarningInfo.Warning)); } // INotifyDataErrorInfo & INotifyDataWarningInfo if (_modelErrorInfo.ContainsKey(modelObject.Key)) { var modelErrorInfo = _modelErrorInfo[modelObject.Key]; foreach (string error in modelErrorInfo.GetErrors(string.Empty)) { SetBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(error)); } foreach (string warning in modelErrorInfo.GetWarnings(string.Empty)) { SetBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(warning)); } } } } }
public void ReturnsChangesForComplexScenario() { var fieldValidationResults1 = new List <IFieldValidationResult>(); fieldValidationResults1.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarning")); fieldValidationResults1.Add(FieldValidationResult.CreateError("MyProperty", "FieldError")); var businessRuleValidationResults1 = new List <IBusinessRuleValidationResult>(); businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarning")); businessRuleValidationResults1.Add(BusinessRuleValidationResult.CreateError("BusinessRuleError")); var context1 = new ValidationContext(fieldValidationResults1, businessRuleValidationResults1); var fieldValidationResults2 = new List <IFieldValidationResult>(); fieldValidationResults2.Add(FieldValidationResult.CreateWarning("MyProperty", "FieldWarningTextHasChanged")); fieldValidationResults2.Add(FieldValidationResult.CreateError("NewProperty", "FieldErrorForNewProperty")); var businessRuleValidationResults2 = new List <IBusinessRuleValidationResult>(); businessRuleValidationResults2.Add(BusinessRuleValidationResult.CreateWarning("BusinessRuleWarningTextHasChanged")); var context2 = new ValidationContext(fieldValidationResults2, businessRuleValidationResults2); var changes = ValidationContextHelper.GetChanges(context1, context2); Assert.AreEqual(7, changes.Count); int counter; // Field warning text has changed, thus removed counter = 0; Assert.AreEqual("MyProperty", ((IFieldValidationResult)changes[counter].ValidationResult).PropertyName); Assert.AreEqual("FieldWarning", changes[counter].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType); // Field error has been removed counter++; Assert.AreEqual("MyProperty", ((IFieldValidationResult)changes[counter].ValidationResult).PropertyName); Assert.AreEqual("FieldError", changes[counter].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Error, changes[counter].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType); // Field warning text has changed, thus added counter++; Assert.AreEqual("MyProperty", ((IFieldValidationResult)changes[counter].ValidationResult).PropertyName); Assert.AreEqual("FieldWarningTextHasChanged", changes[counter].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Added, changes[counter].ChangeType); // Field error added counter++; Assert.AreEqual("NewProperty", ((IFieldValidationResult)changes[counter].ValidationResult).PropertyName); Assert.AreEqual("FieldErrorForNewProperty", changes[counter].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Error, changes[counter].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Added, changes[counter].ChangeType); // Business rule text has changed, thus removed counter++; Assert.AreEqual("BusinessRuleWarning", changes[counter].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType); // Business rule error has been removed counter++; Assert.AreEqual("BusinessRuleError", changes[counter].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Error, changes[counter].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Removed, changes[counter].ChangeType); // Business rule text has changed, thus added counter++; Assert.AreEqual("BusinessRuleWarningTextHasChanged", changes[counter].ValidationResult.Message); Assert.AreEqual(ValidationResultType.Warning, changes[counter].ValidationResult.ValidationResultType); Assert.AreEqual(ValidationContextChangeType.Added, changes[counter].ChangeType); }
public IValidationContext Parse(List <string> commandLineArguments, IContext targetContext) { var validationContext = new ValidationContext(); var quoteSplitCharacters = targetContext.QuoteSplitCharacters.ToArray(); targetContext.OriginalCommandLine = string.Join(" ", commandLineArguments); var isHelp = commandLineArguments.Any(commandLineArgument => commandLineArgument.IsHelp(quoteSplitCharacters)); if (isHelp) { targetContext.IsHelp = true; return(validationContext); } var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext); var handledOptions = new HashSet <string>(StringComparer.OrdinalIgnoreCase); Log.Debug("Parsing command line"); for (var i = 0; i < commandLineArguments.Count; i++) { var commandLineArgument = commandLineArguments[i]; try { // Allow the first one to be a non-switch if (i == 0 && !commandLineArguments[i].IsSwitch(quoteSplitCharacters)) { var emptyOptionDefinition = (from x in optionDefinitions where !x.HasSwitch() select x).FirstOrDefault(); if (emptyOptionDefinition == null) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoEmptySwitch"), commandLineArgument); Log.Debug(message); validationContext.Add(BusinessRuleValidationResult.CreateError(message)); continue; } UpdateContext(targetContext, emptyOptionDefinition, commandLineArgument); handledOptions.Add(emptyOptionDefinition.ShortName); continue; } if (!commandLineArgument.IsSwitch(quoteSplitCharacters)) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoSwitch"), commandLineArgument); Log.Debug(message); validationContext.Add(BusinessRuleValidationResult.CreateWarning(message)); continue; } var value = string.Empty; var optionDefinition = (from x in optionDefinitions where x.IsSwitch(commandLineArgument, quoteSplitCharacters) select x).FirstOrDefault(); var isKnownDefinition = (optionDefinition != null); if (!isKnownDefinition) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseSwitchNotRecognized"), commandLineArgument); Log.Debug(message); validationContext.Add(BusinessRuleValidationResult.CreateWarning(message)); // Try to read the next value, but keep in mind that some options might // not have a value passed into it var potentialValue = (i < commandLineArguments.Count - 1) ? commandLineArguments[i + 1] : string.Empty; if (!string.IsNullOrWhiteSpace(potentialValue) && potentialValue.IsSwitch(quoteSplitCharacters)) { potentialValue = string.Empty; } value = potentialValue; } targetContext.RawValues[commandLineArgument.TrimSwitchPrefix()] = value; if (!isKnownDefinition) { continue; } if (!optionDefinition.AcceptsValue) { // Assume boolean switch value = "true"; } else { if (commandLineArguments.Count <= i + 1) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseValueMissing"), commandLineArgument); Log.Info(message); validationContext.Add(BusinessRuleValidationResult.CreateWarning(message)); continue; } value = commandLineArguments[++i]; } UpdateContext(targetContext, optionDefinition, value); handledOptions.Add(optionDefinition.ShortName); } catch (Exception ex) { validationContext.Add(BusinessRuleValidationResult.CreateError(_languageService.GetString("CommandLine_CannotParseExceptionOccurred"), commandLineArgument, ex.Message)); } } ValidateMandatorySwitches(validationContext, optionDefinitions, handledOptions); Log.Debug("Finishing the context"); targetContext.Finish(); return(validationContext); }
public IValidationContext Parse(List <string> commandLineArguments, IContext targetContext) { var validationContext = new ValidationContext(); targetContext.OriginalCommandLine = string.Join(" ", commandLineArguments); var isHelp = commandLineArguments.Any(commandLineArgument => commandLineArgument.IsHelp()); if (isHelp) { targetContext.IsHelp = true; return(validationContext); } var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext); var handledOptions = new HashSet <char>(); Log.Debug("Parsing command line"); for (var i = 0; i < commandLineArguments.Count; i++) { var commandLineArgument = commandLineArguments[i]; try { // Allow the first one to be a non-switch if (i == 0) { if (!commandLineArguments[i].IsSwitch()) { var emptyOptionDefinition = (from x in optionDefinitions where !x.HasSwitch() select x).FirstOrDefault(); if (emptyOptionDefinition == null) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoEmptySwitch"), commandLineArgument); Log.Error(message); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(message)); continue; } UpdateContext(targetContext, emptyOptionDefinition, commandLineArgument); handledOptions.Add(emptyOptionDefinition.ShortName); continue; } } if (!commandLineArgument.IsSwitch()) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoSwitch"), commandLineArgument); Log.Warning(message); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message)); continue; } var optionDefinition = (from x in optionDefinitions where x.IsSwitch(commandLineArgument) select x).FirstOrDefault(); if (optionDefinition == null) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseSwitchNotRecognized"), commandLineArgument); Log.Warning(message); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message)); continue; } var value = string.Empty; if (!optionDefinition.AcceptsValue) { // Assume boolean switch value = "true"; } else { if (commandLineArguments.Count <= i + 1) { var message = string.Format(_languageService.GetString("CommandLine_CannotParseValueMissing"), commandLineArgument); Log.Warning(message); validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateWarning(message)); continue; } value = commandLineArguments[++i]; } UpdateContext(targetContext, optionDefinition, value); handledOptions.Add(optionDefinition.ShortName); } catch (Exception ex) { validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError(_languageService.GetString("CommandLine_CannotParseExceptionOccurred"), commandLineArgument, ex.Message)); } } Log.Debug("Checking if all required options are specified"); foreach (var optionDefinition in optionDefinitions) { if (optionDefinition.IsMandatory) { if (!handledOptions.Contains(optionDefinition.ShortName)) { var message = string.Format(_languageService.GetString("CommandLine_RequiredSwitchNotSpecified"), optionDefinition); Log.Error(message); validationContext.AddFieldValidationResult(FieldValidationResult.CreateError(optionDefinition.GetSwitchDisplay(), message)); } } } Log.Debug("Finishing the context"); targetContext.Finish(); return(validationContext); }