public void Init() { _fakeRestrictionRepository = new Mock <IRestrictionRepository>(); _fakeSalesAreaRepository = new Mock <ISalesAreaRepository>(); _fakeClearanceRepository = new Mock <IClearanceRepository>(); _fakeProgrammeCategoryRepository = new Mock <IProgrammeCategoryHierarchyRepository>(); var salesAreaNames = new List <string> { "sa1" }; var salesArea = new SalesArea { Name = "sa1" }; var programmeCategory = new ProgrammeCategoryHierarchy { Id = 2, Name = "CHILDREN" }; _ = _fakeSalesAreaRepository.Setup(m => m.FindByNames(salesAreaNames)).Returns( new List <SalesArea> { salesArea } ); _ = _fakeProgrammeCategoryRepository.Setup(p => p.GetAll()).Returns( new List <ProgrammeCategoryHierarchy> { programmeCategory }); _fixture = new Fixture(); _mapper = AutoMapperInitializer.Initialize(Configuration.AutoMapperConfig.SetupAutoMapper); _controller = new RestrictionController( _fakeRestrictionRepository.Object, _fakeSalesAreaRepository.Object, _fakeClearanceRepository.Object, _fakeProgrammeCategoryRepository.Object, _mapper); _validCreateRestriction = CreateValidModel(); _validUpdateRestriction = UpdateValidModel(); _validCreateRestrictions = new List <CreateRestriction>(); }
private void ValidateInput(CreateRestriction command) { if (!string.IsNullOrWhiteSpace(command.ExternalIdentifier)) { var restriction = _restrictionRepository.Get(command.ExternalIdentifier); if (restriction != null) { var msg = $"Restriction with external identifier: '{command.ExternalIdentifier}' already exists"; throw new InvalidDataException(msg); } } ValidateRestrictionDatesAndDays(command.StartDate, command.EndDate, command.RestrictionDays); if (command.SalesAreas != null && command.SalesAreas.Any()) { _salesAreaRepository.ValidateSaleArea(command.SalesAreas); } CustomValidation(command); if (!string.IsNullOrWhiteSpace(command.ProgrammeCategory) && !_programmeCategoryRepository.IsValid( new List <string> { command.ProgrammeCategory }, out List <string> invalidList)) { var msg = string.Concat("Invalid programme category: ", invalidList != null ? invalidList.FirstOrDefault() : string.Empty); throw new InvalidDataException(msg); } if (!string.IsNullOrWhiteSpace(command.ClearanceCode)) { _clearanceRepository.ValidateClearanceCode(new List <string> { command.ClearanceCode }); } }
private static void AssignDefaultValue(ref CreateRestriction command) { command.StartDate = command.StartDate.Date; // removing time value command.EndDate = command.EndDate?.Date; // removing time value }
/// <summary> /// Validation based on restriction type and basis /// </summary> /// <param name="command"></param> private static void CustomValidation(CreateRestriction command) { IValidation validation; switch (command.RestrictionBasis) { case RestrictionBasis.Product: validation = new RequiredFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { FieldName = "Product Code", FieldToValidate = command.ProductCode } } }; validation.Execute(); validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "Clash code must be blank/empty if basis of restriction is product", FieldToValidate = command.ClashCode }, new ValidationInfo { ErrorMessage = "Clearance code must be blank/empty if basis of restriction is product", FieldToValidate = command.ClearanceCode } } }; validation.Execute(); break; case RestrictionBasis.Clash: validation = new RequiredFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { FieldName = "Clash Code", FieldToValidate = command.ClashCode } } }; validation.Execute(); validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "Product code must be 0 if basis of restriction is clash", FieldToValidate = command.ProductCode }, new ValidationInfo { ErrorMessage = "Clearance code must be blank/empty if basis of restriction is clash", FieldToValidate = command.ClearanceCode } } }; validation.Execute(); if (!string.IsNullOrWhiteSpace(command.ClockNumber) && (command.ClockNumber.Length > 1 || !int.TryParse(command.ClockNumber, out var clashBasedClockNumber) || clashBasedClockNumber != 0)) { throw new InvalidDataException("Clock number must be blank/empty if basis of restriction is clash"); } break; case RestrictionBasis.ClearanceCode: validation = new RequiredFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { FieldName = "Clearance Code", FieldToValidate = command.ClearanceCode } } }; validation.Execute(); validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "Product code must be 0 if basis of restriction is clearance", FieldToValidate = command.ProductCode }, new ValidationInfo { ErrorMessage = "Clash code must be blank/empty if basis of restriction is clearance", FieldToValidate = command.ClashCode } } }; validation.Execute(); if (!string.IsNullOrWhiteSpace(command.ClockNumber) && (command.ClockNumber.Length > 1 || !int.TryParse(command.ClockNumber, out var clearanceBasedClockNumber) || clearanceBasedClockNumber != 0)) { throw new InvalidDataException("Clock number must be 0 if basis of restriction is clearance"); } break; default: throw new ArgumentOutOfRangeException(); } switch (command.RestrictionType) { case RestrictionType.Time: validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "External programme reference must be blank/empty if restriction type is time", FieldToValidate = command.ExternalProgRef }, new ValidationInfo { ErrorMessage = "Programme category must be blank/empty if restriction type is time", FieldToValidate = command.ProgrammeCategory }, new ValidationInfo { ErrorMessage = "Programme classification must be blank/empty if restriction type is time", FieldToValidate = command.ProgrammeClassification }, new ValidationInfo { ErrorMessage = "Index type must be 0 if restriction type is time", FieldToValidate = command.IndexType }, new ValidationInfo { ErrorMessage = "Index threshold must be 0 if restriction type is time", FieldToValidate = command.IndexThreshold } } }; validation.Execute(); break; case RestrictionType.Programme: validation = new RequiredFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { FieldName = "External programme reference", FieldToValidate = command.ExternalProgRef } } }; validation.Execute(); validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "Programme category must be blank/empty if restriction type is programme", FieldToValidate = command.ProgrammeCategory }, new ValidationInfo { ErrorMessage = "Programme classification must be blank/empty if restriction type is programme", FieldToValidate = command.ProgrammeClassification }, new ValidationInfo { ErrorMessage = "Index type must be 0 if restriction type is programme", FieldToValidate = command.IndexType }, new ValidationInfo { ErrorMessage = "Index threshold must be 0 if restriction type is programme", FieldToValidate = command.IndexThreshold } } }; validation.Execute(); break; case RestrictionType.ProgrammeCategory: validation = new RequiredFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { FieldName = "Programme category", FieldToValidate = command.ProgrammeCategory } } }; validation.Execute(); validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "External programme reference must be blank/empty if restriction type is programme category", FieldToValidate = command.ExternalProgRef }, new ValidationInfo { ErrorMessage = "Index type must be 0 if restriction type is programme category", FieldToValidate = command.IndexType }, new ValidationInfo { ErrorMessage = "Index threshold must be 0 if restriction type is programme category", FieldToValidate = command.IndexThreshold }, new ValidationInfo { ErrorMessage = "Programme classification must be blank/empty if restriction type is programme category", FieldToValidate = command.ProgrammeClassification } } }; validation.Execute(); break; case RestrictionType.Index: validation = new RequiredFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { FieldName = "Index type", FieldToValidate = command.IndexType }, new ValidationInfo { FieldName = "Index threshold", FieldToValidate = command.IndexThreshold } } }; validation.Execute(); validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "External programme reference must be blank/empty if restriction type is index", FieldToValidate = command.ExternalProgRef }, new ValidationInfo { ErrorMessage = "Programme category must be blank/empty if restriction type is index", FieldToValidate = command.ProgrammeCategory }, new ValidationInfo { ErrorMessage = "Programme classification must be blank/empty if restriction type is index", FieldToValidate = command.ProgrammeClassification } } }; validation.Execute(); break; case RestrictionType.ProgrammeClassification: validation = new RequiredFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { FieldName = "Programme classification", FieldToValidate = command.ProgrammeClassification } } }; validation.Execute(); validation = new EmptyFieldValidation { Field = new List <ValidationInfo> { new ValidationInfo { ErrorMessage = "External programme reference must be blank/empty if restriction type is programme classification", FieldToValidate = command.ExternalProgRef }, new ValidationInfo { ErrorMessage = "Index type must be 0 if restriction type is programme classification", FieldToValidate = command.IndexType }, new ValidationInfo { ErrorMessage = "Index threshold must be 0 if restriction type is programme classification", FieldToValidate = command.IndexThreshold }, new ValidationInfo { ErrorMessage = "Programme category must be blank/empty if restriction type is programme classification", FieldToValidate = command.ProgrammeCategory } } }; validation.Execute(); break; default: throw new ArgumentOutOfRangeException(); } }