public override bool IsValid(CreateMappingRequest request) { var mapping = request.Mapping; var range = new EnergyTrading.DateRange(mapping.StartDate, mapping.EndDate); var entity = this.repository.FindOne <TEntity>(request.EntityId) as MDM.PartyRole; if (entity == null) { this.Message = string.Format(EntityNotFoundMessageTemplate, typeof(TEntity).Name, request.EntityId); return(false); } var count = this.repository.FindPartyRoleOverlappingMappingCount <TMapping>( mapping.SystemName, mapping.Identifier, range, entity.PartyRoleType); if (count > 0) { this.Message = string.Format( MessageTemplate, mapping.Identifier, mapping.SystemName, range.Start, range.Finish); return(false); } return(true); }
/// <summary> /// Does the target validity overlap the range. /// <para> /// There are six cases /// <list type=""> /// <li>Range is entirely before the validity period</li> /// <li>Range starts before validity start and ends before validity.finish</li> /// <li>Range starts after validity start and ends before validity.finish</li> /// <li>Range starts after validity start and ends after validity.finish</li> /// <li>Range starts after validity finish</li> /// <li>Range starts before validity start and ends after validity.finish</li> /// </list> /// So what we need to check is whether the range falls into case 1 or 5 /// As DateRange good temporal semantics i.e. Start < Finish we can use a simple inequality /// check; is the range finish before the validity start or the range.start after the validity.finish /// and then negate this. /// </para> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="queryable"></param> /// <param name="range"></param> /// <returns></returns> public static IQueryable <T> Overlaps <T>(this IQueryable <T> queryable, EnergyTrading.DateRange range) where T : IRanged { return(queryable .Where(x => !(x.Validity.Start >= range.Finish || x.Validity.Finish <= range.Start))); }
public void OverlapsRangeFails() { // Assert var start = new DateTime(1999, 1, 1); var finish = new DateTime(2020, 12, 31); var validity = new DateRange(start, finish); var system = new Mdm.SourceSystem { Name = "Test" }; var sourcesystemMapping = new SourceSystemMapping { System = system, MappingValue = "1", Validity = validity }; var list = new List <SourceSystemMapping> { sourcesystemMapping }; var repository = new Mock <IRepository>(); repository.Setup(x => x.Queryable <SourceSystemMapping>()).Returns(list.AsQueryable()); var systemList = new List <Mdm.SourceSystem>(); var systemRepository = new Mock <IRepository>(); systemRepository.Setup(x => x.Queryable <Mdm.SourceSystem>()).Returns(systemList.AsQueryable()); var overlapsRangeIdentifier = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "1", StartDate = start.AddHours(10), EndDate = start.AddHours(15) }; var identifierValidator = new NexusIdValidator <SourceSystemMapping>(repository.Object); var validatorEngine = new Mock <IValidatorEngine>(); validatorEngine.Setup(x => x.IsValid(It.IsAny <EnergyTrading.Mdm.Contracts.MdmId>(), It.IsAny <IList <IRule> >())) .Returns((EnergyTrading.Mdm.Contracts.MdmId x, IList <IRule> y) => identifierValidator.IsValid(x, y)); var validator = new SourceSystemValidator(validatorEngine.Object, repository.Object); var sourcesystem = new SourceSystem { Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { overlapsRangeIdentifier } }; // Act var violations = new List <IRule>(); var result = validator.IsValid(sourcesystem, violations); // Assert Assert.IsFalse(result, "Validator succeeded"); }
public override bool IsValid(MdmId entity) { var range = new EnergyTrading.DateRange(entity.StartDate, entity.EndDate); var count = this.repository.FindOverlappingMappingCount <T>(entity.SystemName, entity.Identifier, range); if (count > 0) { Message = string.Format(MessageTemplate, entity.Identifier, entity.SystemName, range.Start, range.Finish); return(false); } return(true); }
public override bool IsValid(AmendMappingRequest amendMappingRequest) { var mapping = amendMappingRequest.Mapping; var range = new EnergyTrading.DateRange(mapping.StartDate, mapping.EndDate); var count = this.repository.FindOverlappingMappingCount <T>(mapping.SystemName, mapping.Identifier, range, amendMappingRequest.MappingId); if (count > 0) { this.Message = string.Format(MessageTemplate, mapping.Identifier, mapping.SystemName, range.Start, range.Finish); return(false); } return(true); }
public void OverlapsRangeFails() { // Assert var start = new DateTime(1999, 1, 1); var finish = new DateTime(2020, 12, 31); var validity = new DateRange(start, finish); var system = new Mdm.SourceSystem { Name = "Test" }; var sourcesystemMapping = new SourceSystemMapping { System = system, MappingValue = "1", Validity = validity }; var list = new List<SourceSystemMapping> { sourcesystemMapping }; var repository = new Mock<IRepository>(); repository.Setup(x => x.Queryable<SourceSystemMapping>()).Returns(list.AsQueryable()); var systemList = new List<Mdm.SourceSystem>(); var systemRepository = new Mock<IRepository>(); systemRepository.Setup(x => x.Queryable<Mdm.SourceSystem>()).Returns(systemList.AsQueryable()); var overlapsRangeIdentifier = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "1", StartDate = start.AddHours(10), EndDate = start.AddHours(15) }; var identifierValidator = new NexusIdValidator<SourceSystemMapping>(repository.Object); var validatorEngine = new Mock<IValidatorEngine>(); validatorEngine.Setup(x => x.IsValid(It.IsAny<EnergyTrading.Mdm.Contracts.MdmId>(), It.IsAny<IList<IRule>>())) .Returns((EnergyTrading.Mdm.Contracts.MdmId x, IList<IRule> y) => identifierValidator.IsValid(x, y)); var validator = new SourceSystemValidator(validatorEngine.Object, repository.Object); var sourcesystem = new SourceSystem { Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { overlapsRangeIdentifier } }; // Act var violations = new List<IRule>(); var result = validator.IsValid(sourcesystem, violations); // Assert Assert.IsFalse(result, "Validator succeeded"); }
public static IQueryable <TMapping> FindOverlappingMappings <TMapping>(this IRepository repository, string sourceSystem, string mapping, EnergyTrading.DateRange range, int mappingId) where TMapping : class, IEntityMapping { var query = repository.Queryable <TMapping>() .Where(x => x.System.Name == sourceSystem && x.MappingValue == mapping // Explicit Overlaps as LINQ to Entity can't cope with interfaces && !(x.Validity.Start >= range.Finish || x.Validity.Finish <= range.Start)); if (mappingId != 0) { query = query.Where(x => (int)x.Id != mappingId); } return(query); }
public static int FindOverlappingMappingCount <TMapping>(this IRepository repository, string sourceSystem, string mapping, EnergyTrading.DateRange range, int mappingId) where TMapping : class, IEntityMapping { return(repository.FindOverlappingMappings <TMapping>(sourceSystem, mapping, range, mappingId).Count()); }
public static EnergyTrading.Mdm.Contracts.DateRange ToContract(this DateRange value) { return(value == null ? new EnergyTrading.Mdm.Contracts.DateRange() : new EnergyTrading.Mdm.Contracts.DateRange { StartDate = value.Start, EndDate = value.Finish }); }