public void SuccessMatch()
        {
            // Arrange
            var validatorFactory = new Mock<IValidatorEngine>();
            var mappingEngine = new Mock<IMappingEngine>();
            var repository = new Mock<IRepository>();
            var searchCache = new Mock<ISearchCache>();

            var service = new SourceSystemService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Domain details
            var start = new DateTime(1999, 12, 31);
            var finish = new DateTime(2020, 1, 1);
            var system = new SourceSystem { Name = "Endur" };
            var mapping = new SourceSystemMapping
            {
                System = system,
                MappingValue = "A"
            };
            var sourcesystem = new SourceSystem
            {
                Id = 1,
                Validity = new DateRange(start, finish)
            };
            //SourceSystem.AddDetails(details);
            sourcesystem.ProcessMapping(mapping);

            // Contract details
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Endur",
                Identifier = "A"
            };
            var cDetails = new EnergyTrading.Mdm.Contracts.SourceSystemDetails();

            mappingEngine.Setup(x => x.Map<SourceSystemMapping, EnergyTrading.Mdm.Contracts.MdmId>(mapping)).Returns(identifier);
            mappingEngine.Setup(x => x.Map<SourceSystem, EnergyTrading.Mdm.Contracts.SourceSystemDetails>(sourcesystem)).Returns(cDetails);
            validatorFactory.Setup(x => x.IsValid(It.IsAny<MappingRequest>(), It.IsAny<IList<IRule>>())).Returns(true);

            var list = new List<SourceSystemMapping> { mapping };
            repository.Setup(x => x.Queryable<SourceSystemMapping>()).Returns(list.AsQueryable());

            var request = new MappingRequest { SystemName = "Endur", Identifier = "A", ValidAt = SystemTime.UtcNow(), Version = 1 };

            // Act
            var response = service.Map(request);
            var candidate = response.Contract;

            // Assert
            mappingEngine.Verify(x => x.Map<SourceSystemMapping, EnergyTrading.Mdm.Contracts.MdmId>(mapping));
            mappingEngine.Verify(x => x.Map<SourceSystem, EnergyTrading.Mdm.Contracts.SourceSystemDetails>(sourcesystem));
            repository.Verify(x => x.Queryable<SourceSystemMapping>());
            Assert.IsNotNull(candidate, "Contract null");
            Assert.AreEqual(2, candidate.Identifiers.Count, "Identifier count incorrect");
            // NB This is order dependent
            Assert.AreSame(identifier, candidate.Identifiers[1], "Different identifier assigned");
            Assert.AreSame(cDetails, candidate.Details, "Different details assigned");
            Assert.AreEqual(start, candidate.MdmSystemData.StartDate, "Start date differs");
            Assert.AreEqual(finish, candidate.MdmSystemData.EndDate, "End date differs");
        }
        public void Map()
        {
            // Arrange
            var start = new DateTime(2010, 1, 1);
            var end = DateUtility.MaxDate;
            var range = new DateRange(start, end);

            var id = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "A" };
            var contractDetails = new EnergyTrading.Mdm.Contracts.SourceSystemDetails();
            var contract = new EnergyTrading.Mdm.Contracts.SourceSystem
            {
                Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { id },
                Details = contractDetails,
                MdmSystemData = new EnergyTrading.Mdm.Contracts.SystemData { StartDate = start, EndDate = end }
            };

            // NB Don't assign validity here, want to prove SUT sets it
            var details = new SourceSystem();

            var mapping = new SourceSystemMapping();

            var mappingEngine = new Mock<IMappingEngine>();
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.MdmId, SourceSystemMapping>(id)).Returns(mapping);
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.SourceSystemDetails, SourceSystem>(contractDetails)).Returns(details);

            var mapper = new SourceSystemMapper(mappingEngine.Object);

            // Act
            var candidate = mapper.Map(contract);

            // Assert
            //Assert.AreEqual(1, candidate.Details.Count, "Detail count differs");
            Assert.AreEqual(1, candidate.Mappings.Count, "Mapping count differs");
            Check(range, details.Validity, "Validity differs");
        }
        public void Map()
        {
            // Arrange
            var system = new SourceSystem { Name = "Test" };
            var start = new DateTime(2010, 1, 1);
            var end = new DateTime(2012, 12, 31);

            var source = new PartyMapping
                {
                    Id = 100,
                    System = system,
                    MappingValue = "1",
                    IsMaster = true,
                    Validity = new DateRange(start, end)
                };
            var expected = new EnergyTrading.Mdm.Contracts.MdmId
                {
                    MappingId = 100,
                    SystemName = "Test",
                    Identifier = "1",
                    SourceSystemOriginated = true,
                    StartDate = start,
                    EndDate = end
                };

            var mapper = new PartyMappingMapper();

            // Act
            var candidate = mapper.Map(source);

            // Assert
            Check(expected, candidate);
        }
        public void ValidContractAdded()
        {
            // Arrange
            var validatorFactory = new Mock<IValidatorEngine>();
            var mappingEngine = new Mock<IMappingEngine>();
            var repository = new Mock<IRepository>();
            var searchCache = new Mock<ISearchCache>();

            var service = new SourceSystemService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "A" };
            var message = new CreateMappingRequest
            {
                EntityId = 12,
                Mapping = identifier
            };

            var system = new Mdm.SourceSystem { Name = "Test" };
            var mapping = new SourceSystemMapping { System = system, MappingValue = "A" };
            validatorFactory.Setup(x => x.IsValid(It.IsAny<CreateMappingRequest>(), It.IsAny<IList<IRule>>())).Returns(true);
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.MdmId, SourceSystemMapping>(identifier)).Returns(mapping);

            var sourcesystem = new Mdm.SourceSystem();
            repository.Setup(x => x.FindOne<Mdm.SourceSystem>(12)).Returns(sourcesystem);

            // Act
            var candidate = (SourceSystemMapping)service.CreateMapping(message);

            // Assert
            Assert.AreSame(mapping, candidate);
            repository.Verify(x => x.Save(sourcesystem));
            repository.Verify(x => x.Flush());
        }
        public void SystemPresentPasses()
        {
            // Arrange
            var system = new SourceSystem {
                Name = "Test"
            };
            var systemList = new System.Collections.Generic.List <SourceSystem> {
                system
            };
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.Queryable <SourceSystem>()).Returns(systemList.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId {
                SystemName = "Test", Identifier = "1"
            };

            var rule = new MdmIdSystemExistsRule(repository.Object);

            // Act
            var result = rule.IsValid(identifier);

            // Assert
            repository.Verify(x => x.Queryable <SourceSystem>());
            Assert.IsTrue(result, "Rule failed");
        }
        public void OverlappingIdentifierFails()
        {
            // Assert
            var start = new DateTime(1999, 1, 1);
            var finish = new DateTime(2020, 12, 31);
            var validity = new DateRange(start, finish);
            var system = new SourceSystem { Name = "Test" };
            var expected = new SourceSystemMapping { System = system, MappingValue = "1", Validity = validity };
            var list = new System.Collections.Generic.List<SourceSystemMapping> { expected };
            var repository = new Mock<IRepository>();
            repository.Setup(x => x.Queryable<SourceSystemMapping>()).Returns(list.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate = start.AddHours(5),
                EndDate = start.AddHours(10)
            };

            var request = new AmendMappingRequest() { EntityId = 1, Mapping = identifier, MappingId = 1 };

            var rule = new AmendMappingNoOverlappingRule<SourceSystemMapping>(repository.Object);

            // Act
            var result = rule.IsValid(request);

            // Assert
            repository.Verify(x => x.Queryable<SourceSystemMapping>());
            Assert.IsFalse(result, "Rule failed");
        }
Beispiel #7
0
        public void Map()
        {
            // Arrange
            var system = new SourceSystem {
                Name = "Test"
            };
            var start = new DateTime(2010, 1, 1);
            var end   = new DateTime(2012, 12, 31);

            var source = new PartyMapping
            {
                Id           = 100,
                System       = system,
                MappingValue = "1",
                IsMaster     = true,
                Validity     = new DateRange(start, end)
            };
            var expected = new EnergyTrading.Mdm.Contracts.MdmId
            {
                MappingId              = 100,
                SystemName             = "Test",
                Identifier             = "1",
                SourceSystemOriginated = true,
                StartDate              = start,
                EndDate = end
            };

            var mapper = new PartyMappingMapper();

            // Act
            var candidate = mapper.Map(source);

            // Assert
            Check(expected, candidate);
        }
        public void NoOverlappingIndentifierShouldPass()
        {
            // Assert
            var start = new DateTime(1999, 1, 1);
            var finish = new DateTime(2020, 12, 31);
            var validity = new DateRange(start, finish);
            var system = new SourceSystem { Name = "Test" };
            var expectedPartyRole = new PartyRole() { PartyRoleType = "PartyRole" };
            var expected = new PartyRoleMapping { System = system, MappingValue = "1", Validity = validity, PartyRole = expectedPartyRole };
            var list = new List<PartyRoleMapping> { expected };
            var repository = new Mock<IRepository>();
            repository.Setup(x => x.Queryable<PartyRoleMapping>()).Returns(list.AsQueryable());
            repository.Setup(x => x.FindOne<PartyRole>(It.IsAny<int>())).Returns(expectedPartyRole);

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate = start.AddHours(-10),
                EndDate = start.AddHours(-5)
            };

            var request = new CreateMappingRequest() { EntityId = 1, Mapping = identifier };
            var rule = new PartyRoleCreateMappingdNoOverlappingRule<PartyRole, PartyRoleMapping>(repository.Object);

            // Act
            var result = rule.IsValid(request);

            // Assert
            repository.Verify(x => x.FindOne<PartyRole>(It.IsAny<int>()));
            Assert.IsTrue(result, "Rule failed");
        }
Beispiel #9
0
 public override void Map(EnergyTrading.Mdm.Contracts.MdmId source, TMapping destination)
 {
     destination.MappingId    = (int)(source.MappingId.HasValue ? source.MappingId.Value : 0L);
     destination.System       = this.repository.SystemByName(source.SystemName);
     destination.MappingValue = source.Identifier;
     destination.IsMaster     = source.SourceSystemOriginated;
     destination.IsDefault    = source.DefaultReverseInd.HasValue && source.DefaultReverseInd.Value;
     destination.Validity     = new DateRange(source.StartDate, source.EndDate);
 }
Beispiel #10
0
        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 legalentityMapping = new PartyRoleMapping {
                System = system, MappingValue = "1", Validity = validity
            };

            var list = new List <PartyRoleMapping> {
                legalentityMapping
            };
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.Queryable <PartyRoleMapping>()).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 <PartyRoleMapping>(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 LegalEntityValidator(validatorEngine.Object, repository.Object);

            var legalentity = new LegalEntity {
                Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    overlapsRangeIdentifier
                }
            };

            // Act
            var violations = new List <IRule>();
            var result     = validator.IsValid(legalentity, violations);

            // Assert
            Assert.IsFalse(result, "Validator succeeded");
        }
        public void BadSystemFails()
        {
            // Assert
            var start    = new DateTime(1999, 1, 1);
            var finish   = new DateTime(2020, 12, 31);
            var validity = new DateRange(start, finish);
            var system   = new SourceSystem {
                Name = "Test"
            };
            var personMapping = new PersonMapping {
                System = system, MappingValue = "1", Validity = validity
            };

            var list = new List <PersonMapping> {
                personMapping
            };
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.Queryable <PersonMapping>()).Returns(list.AsQueryable());

            var badSystemIdentifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Jim",
                Identifier = "1",
                StartDate  = start.AddHours(-10),
                EndDate    = start.AddHours(-5)
            };

            var identifierValidator = new NexusIdValidator <PersonMapping>(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 PersonValidator(validatorEngine.Object, null);

            var person = new EnergyTrading.MDM.Contracts.Sample.Person {
                Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    badSystemIdentifier
                }
            };

            // Act
            var violations = new List <IRule>();
            var result     = validator.IsValid(person, violations);

            // Assert
            Assert.IsFalse(result, "Validator succeeded");
        }
Beispiel #12
0
        public void ValidPartyRolePasses()
        {
            // Assert
            var start  = new DateTime(1999, 1, 1);
            var system = new SourceSystem {
                Name = "Test"
            };

            var systemList = new List <SourceSystem> {
                system
            };
            var systemRepository = new Mock <IRepository>();
            var repository       = new StubValidatorRepository();

            systemRepository.Setup(x => x.Queryable <MDM.SourceSystem>()).Returns(systemList.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate  = start.AddHours(-10),
                EndDate    = start.AddHours(-5)
            };

            var validatorEngine = new Mock <IValidatorEngine>();
            var validator       = new PartyRoleValidator(validatorEngine.Object, repository);

            var party = new PartyRole
            {
                PartyRoleType = "SomepartyRole",
                Details       = new EnergyTrading.MDM.Contracts.Sample.PartyRoleDetails {
                    Name = "Test"
                }, Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    identifier
                }
            };

            this.AddRelatedEntities(party);

            // Act
            var violations = new List <IRule>();
            var result     = validator.IsValid(party, violations);

            // Assert
            Assert.IsTrue(result, "Validator failed");
            Assert.AreEqual(0, violations.Count, "Violation count differs");
        }
        public void Map()
        {
            // Arrange
            var start = new DateTime(2010, 1, 1);
            var end   = DateUtility.MaxDate;
            var range = new DateRange(start, end);

            var id = new EnergyTrading.Mdm.Contracts.MdmId {
                SystemName = "Test", Identifier = "A"
            };
            var contractDetails = new EnergyTrading.MDM.Contracts.Sample.ExchangeDetails();
            var contract        = new EnergyTrading.MDM.Contracts.Sample.Exchange
            {
                Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    id
                },
                Details       = contractDetails,
                MdmSystemData = new EnergyTrading.Mdm.Contracts.SystemData {
                    StartDate = start, EndDate = end
                },
                Party = ObjectMother.Create <Party>().CreateNexusEntityId(() => "")
            };

            // NB Don't assign validity here, want to prove SUT sets it
            var details = new ExchangeDetails();

            var mapping = new PartyRoleMapping();

            var mappingEngine = new Mock <IMappingEngine>();
            var repository    = new Mock <IRepository>();

            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.MdmId, PartyRoleMapping>(id)).Returns(mapping);
            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.MdmId, PartyRoleMapping>(id)).Returns(mapping);
            mappingEngine.Setup(x => x.Map <EnergyTrading.MDM.Contracts.Sample.ExchangeDetails, ExchangeDetails>(contractDetails)).Returns(details);
            repository.Setup(x => x.FindOne <Party>(int.Parse(contract.Party.Identifier.Identifier))).Returns(ObjectMother.Create <Party>());

            var mapper = new ExchangeMapper(mappingEngine.Object, repository.Object);

            // Act
            var candidate = mapper.Map(contract);

            // Assert
            //Assert.AreEqual(1, candidate.Details.Count, "Detail count differs");
            Assert.AreEqual(1, candidate.Mappings.Count, "Mapping count differs");
            Assert.AreEqual("Exchange", candidate.PartyRoleType);
            Check(range, details.Validity, "Validity differs");
        }
        public void OverlappingIndentifierShouldFail()
        {
            // Assert
            var start    = new DateTime(1999, 1, 1);
            var finish   = new DateTime(2020, 12, 31);
            var validity = new DateRange(start, finish);
            var system   = new SourceSystem {
                Name = "Test"
            };
            var expectedPartyRole = new PartyRole()
            {
                PartyRoleType = "PartyRole"
            };
            var expected = new PartyRoleMapping {
                System = system, MappingValue = "1", Validity = validity, PartyRole = expectedPartyRole
            };
            var list = new List <PartyRoleMapping> {
                expected
            };
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.Queryable <PartyRoleMapping>()).Returns(list.AsQueryable());
            repository.Setup(x => x.FindOne <PartyRole>(It.IsAny <int>())).Returns(expectedPartyRole);

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate  = start.AddHours(5),
                EndDate    = start.AddHours(10)
            };

            var request = new AmendMappingRequest()
            {
                EntityId = 1, Mapping = identifier, MappingId = 1
            };
            var rule = new PartyRoleAmendMappingNoOverlappingRule <PartyRole, PartyRoleMapping>(repository.Object);

            // Act
            var result = rule.IsValid(request);

            // Assert
            repository.Verify(x => x.FindOne <PartyRole>(It.IsAny <int>()));
            Assert.IsFalse(result, "Rule failed");
        }
        public void SystemMissingFails()
        {
            // Assert
            var systemList = new System.Collections.Generic.List<SourceSystem>();
            var repository = new Mock<IRepository>();
            repository.Setup(x => x.Queryable<SourceSystem>()).Returns(systemList.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "1" };

            var rule = new MdmIdSystemExistsRule(repository.Object);

            // Act
            var result = rule.IsValid(identifier);

            // Assert
            repository.Verify(x => x.Queryable<SourceSystem>());
            Assert.IsFalse(result, "Rule failed");
        }
Beispiel #16
0
        protected void ConvertToNexusId <TMapping>()
            where TMapping : EntityMapping, new()
        {
            var container = new UnityContainer();

            // Self-register and set up service location
            container.RegisterInstance <IUnityContainer>(container);
            var locator = new UnityServiceLocator(container);

            Global.ServiceLocator = locator;

            var task = new SimpleMappingEngineConfiguration(container);

            task.Configure();

            var start    = new DateTime(2000, 12, 31);
            var expected = new EnergyTrading.Mdm.Contracts.MdmId
            {
                MappingId              = 34,
                SystemName             = "Test",
                Identifier             = "1",
                SourceSystemOriginated = true,
                StartDate              = start,
                EndDate = DateUtility.MaxDate
            };
            var s1 = new SourceSystem {
                Name = "Test"
            };
            var m1 = new TMapping
            {
                Id           = 34,
                System       = s1,
                MappingValue = "1",
                IsMaster     = true,
                Validity     = new DateRange(start, DateUtility.MaxDate)
            };

            var mappingEngine = container.Resolve <IMappingEngine>();

            var candidate = mappingEngine.Map <IEntityMapping, EnergyTrading.Mdm.Contracts.MdmId>(m1);

            Check(expected, candidate);
        }
Beispiel #17
0
        public void Map()
        {
            // Arrange
            var start = new DateTime(2010, 1, 1);
            var end   = DateUtility.MaxDate;
            var range = new DateRange(start, end);

            var id = new EnergyTrading.Mdm.Contracts.MdmId {
                SystemName = "Test", Identifier = "A"
            };
            var contractDetails = new EnergyTrading.MDM.Contracts.Sample.PartyDetails();
            var contract        = new EnergyTrading.MDM.Contracts.Sample.Party
            {
                Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    id
                },
                Details       = contractDetails,
                MdmSystemData = new EnergyTrading.Mdm.Contracts.SystemData {
                    StartDate = start, EndDate = end
                }
            };

            // NB Don't assign validity here, want to prove SUT sets it
            var partyDetails = new PartyDetails();

            var mapping = new PartyMapping();

            var mappingEngine = new Mock <IMappingEngine>();

            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.MdmId, PartyMapping>(id)).Returns(mapping);
            mappingEngine.Setup(x => x.Map <EnergyTrading.MDM.Contracts.Sample.PartyDetails, PartyDetails>(contractDetails)).Returns(partyDetails);

            var mapper = new PartyMapper(mappingEngine.Object);

            // Act
            var candidate = mapper.Map(contract);

            // Assert
            Assert.AreEqual(1, candidate.Details.Count, "Detail count differs");
            Assert.AreEqual(1, candidate.Mappings.Count, "Mapping count differs");
            Check(range, partyDetails.Validity, "Validity differs");
        }
        public void ValidPersonPasses()
        {
            // Assert
            var start  = new DateTime(1999, 1, 1);
            var system = new SourceSystem {
                Name = "Test"
            };

            var systemList = new List <SourceSystem> {
                system
            };
            var systemRepository = new Mock <IRepository>();

            systemRepository.Setup(x => x.Queryable <MDM.SourceSystem>()).Returns(systemList.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate  = start.AddHours(-10),
                EndDate    = start.AddHours(-5)
            };

            var validatorEngine = new Mock <IValidatorEngine>();
            var validator       = new PersonValidator(validatorEngine.Object, null);

            var person = new EnergyTrading.MDM.Contracts.Sample.Person {
                Details = new EnergyTrading.MDM.Contracts.Sample.PersonDetails {
                    Surname = "Test"
                }, Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList {
                    identifier
                }
            };

            // Act
            var violations = new List <IRule>();
            var result     = validator.IsValid(person, violations);

            // Assert
            Assert.IsTrue(result, "Validator failed");
            Assert.AreEqual(0, violations.Count, "Violation count differs");
        }
Beispiel #19
0
        public void ValidDetailsSaved()
        {
            const int MappingId = 12;

            // Arrange
            var validatorFactory = new Mock<IValidatorEngine>();
            var mappingEngine = new Mock<IMappingEngine>();
            var repository = new Mock<IRepository>();
            var searchCache = new Mock<ISearchCache>();

            var service = new SourceSystemService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // NB Don't put mappingId here - service assigns it
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "A" };
            var message = new AmendMappingRequest { MappingId = MappingId, Mapping = identifier, Version = 34UL };

            var start = new DateTime(2000, 12, 31);
            var finish = DateUtility.Round(SystemTime.UtcNow().AddDays(5));
            var s1 = new SourceSystem { Name = "Test" };
            var m1 = new SourceSystemMapping { Id = MappingId, System = s1, MappingValue = "1", Version = 34UL.GetVersionByteArray(), Validity = new DateRange(start, DateUtility.MaxDate) };
            var m2 = new SourceSystemMapping { Id = MappingId, System = s1, MappingValue = "1", Validity = new DateRange(start, finish) };

            // NB We deliberately bypasses the business logic
            var entity = new Mdm.SourceSystem();
            m1.SourceSystem = entity;
            entity.Mappings.Add(m1);

            validatorFactory.Setup(x => x.IsValid(It.IsAny<AmendMappingRequest>(), It.IsAny<IList<IRule>>())).Returns(true);
            repository.Setup(x => x.FindOne<SourceSystemMapping>(MappingId)).Returns(m1);
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.MdmId, SourceSystemMapping>(identifier)).Returns(m2);

            // Act
            service.UpdateMapping(message);

            // Assert
            Assert.AreEqual(MappingId, identifier.MappingId, "Mapping identifier differs");
            // Proves we have an update not an add
            Assert.AreEqual(1, entity.Mappings.Count, "Mapping count differs"); 
            // NB Don't verify result of Update - already covered by SourceSystemMappingFixture
            repository.Verify(x => x.Save(entity));
            repository.Verify(x => x.Flush());
        }
Beispiel #20
0
        public void OverlappingIdentifierFails()
        {
            // Assert
            var start    = new DateTime(1999, 1, 1);
            var finish   = new DateTime(2020, 12, 31);
            var validity = new DateRange(start, finish);
            var system   = new SourceSystem {
                Name = "Test"
            };
            var expected = new SourceSystemMapping {
                System = system, MappingValue = "1", Validity = validity
            };
            var list = new System.Collections.Generic.List <SourceSystemMapping> {
                expected
            };
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.Queryable <SourceSystemMapping>()).Returns(list.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate  = start.AddHours(5),
                EndDate    = start.AddHours(10)
            };

            var request = new AmendMappingRequest()
            {
                EntityId = 1, Mapping = identifier, MappingId = 1
            };

            var rule = new AmendMappingNoOverlappingRule <SourceSystemMapping>(repository.Object);

            // Act
            var result = rule.IsValid(request);

            // Assert
            repository.Verify(x => x.Queryable <SourceSystemMapping>());
            Assert.IsFalse(result, "Rule failed");
        }
        public void ValidContractAdded()
        {
            // Arrange
            var validatorFactory = new Mock <IValidatorEngine>();
            var mappingEngine    = new Mock <IMappingEngine>();
            var repository       = new Mock <IRepository>();
            var searchCache      = new Mock <ISearchCache>();

            var service    = new SourceSystemService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId {
                SystemName = "Test", Identifier = "A"
            };
            var message = new CreateMappingRequest
            {
                EntityId = 12,
                Mapping  = identifier
            };

            var system = new Mdm.SourceSystem {
                Name = "Test"
            };
            var mapping = new SourceSystemMapping {
                System = system, MappingValue = "A"
            };

            validatorFactory.Setup(x => x.IsValid(It.IsAny <CreateMappingRequest>(), It.IsAny <IList <IRule> >())).Returns(true);
            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.MdmId, SourceSystemMapping>(identifier)).Returns(mapping);

            var sourcesystem = new Mdm.SourceSystem();

            repository.Setup(x => x.FindOne <Mdm.SourceSystem>(12)).Returns(sourcesystem);

            // Act
            var candidate = (SourceSystemMapping)service.CreateMapping(message);

            // Assert
            Assert.AreSame(mapping, candidate);
            repository.Verify(x => x.Save(sourcesystem));
            repository.Verify(x => x.Flush());
        }
        public void Map()
        {
            // Arrange
            var start = new DateTime(2010, 1, 1);
            var end = DateUtility.MaxDate;
            var range = new DateRange(start, end);

            var id = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "A" };
            var contractDetails = new EnergyTrading.MDM.Contracts.Sample.BrokerDetails();
            var contract = new EnergyTrading.MDM.Contracts.Sample.Broker
            {
                Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { id },
                Details = contractDetails,
                MdmSystemData = new EnergyTrading.Mdm.Contracts.SystemData { StartDate = start, EndDate = end },
                Party = ObjectMother.Create<Party>().CreateNexusEntityId(() => "")
            };

            // NB Don't assign validity here, want to prove SUT sets it
            var details = new BrokerDetails();

            var mapping = new PartyRoleMapping();

            var mappingEngine = new Mock<IMappingEngine>();
            var repository = new Mock<IRepository>();
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.MdmId, PartyRoleMapping>(id)).Returns(mapping);
            mappingEngine.Setup(x => x.Map<EnergyTrading.MDM.Contracts.Sample.BrokerDetails, BrokerDetails>(contractDetails)).Returns(details);
            repository.Setup(x => x.FindOne<Party>(int.Parse(contract.Party.Identifier.Identifier))).Returns(ObjectMother.Create<Party>());

            var mapper = new BrokerMapper(mappingEngine.Object, repository.Object);

            // Act
            var candidate = mapper.Map(contract);

            // Assert
            //Assert.AreEqual(1, candidate.Details.Count, "Detail count differs");
            Assert.AreEqual(1, candidate.Mappings.Count, "Mapping count differs");
            Assert.AreEqual("Broker", candidate.PartyRoleType);
            Check(range, details.Validity, "Validity differs");
        }
Beispiel #23
0
        public void Map <TMapping>()
            where TMapping : class, IEntityMapping, new()
        {
            // Arrange
            var system = new SourceSystem {
                Name = "Test"
            };
            var systemList = new System.Collections.Generic.List <SourceSystem> {
                system
            };
            var repository = new Mock <IRepository>();

            repository.Setup(x => x.Queryable <SourceSystem>()).Returns(systemList.AsQueryable());

            var start  = new DateTime(2010, 1, 1);
            var end    = new DateTime(2012, 12, 31);
            var source = new EnergyTrading.Mdm.Contracts.MdmId {
                MappingId = 1, SystemName = "Test", Identifier = "1", StartDate = start, EndDate = end
            };

            var expected = new TMapping
            {
                System       = system,
                MappingValue = "1",
                Validity     = new DateRange(start, end)
            };

            expected.MappingId = 1;

            var mapper = new MappingMapper <TMapping>(repository.Object);

            // Act
            var candidate = mapper.Map(source);

            // Assert
            Check(expected, candidate);
        }
        public void BadSystemFails()
        {
            // 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 counterpartyMapping = new PartyRoleMapping { System = system, MappingValue = "1", Validity = validity };

            var list = new List<PartyRoleMapping> { counterpartyMapping };
            var repository = new Mock<IRepository>();
            repository.Setup(x => x.Queryable<PartyRoleMapping>()).Returns(list.AsQueryable());

            var badSystemIdentifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Jim",
                Identifier = "1",
                StartDate = start.AddHours(-10),
                EndDate = start.AddHours(-5)
            };

            var identifierValidator = new NexusIdValidator<PartyRoleMapping>(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 CounterpartyValidator(validatorEngine.Object, repository.Object);

            var counterparty = new Counterparty { Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { badSystemIdentifier } };

            // Act
            var violations = new List<IRule>();
            var result = validator.IsValid(counterparty, violations);

            // Assert
            Assert.IsFalse(result, "Validator succeeded");
        }
        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 SourceSystem { Name = "Test" };
            var personMapping = new PersonMapping { System = system, MappingValue = "1", Validity = validity };

            var list = new List<PersonMapping> { personMapping };
            var repository = new Mock<IRepository>();
            repository.Setup(x => x.Queryable<PersonMapping>()).Returns(list.AsQueryable());

            var systemList = new List<SourceSystem>();
            repository.Setup(x => x.Queryable<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<PersonMapping>(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 PersonValidator(validatorEngine.Object, null);

            var person = new EnergyTrading.MDM.Contracts.Sample.Person { Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { overlapsRangeIdentifier } };

            // Act
            var violations = new List<IRule>();
            var result = validator.IsValid(person, violations);

            // Assert
            Assert.IsFalse(result, "Validator succeeded");
        }
        public void ValidDetailsSaved()
        {
            var validatorFactory = new Mock <IValidatorEngine>();
            var mappingEngine    = new Mock <IMappingEngine>();
            var repository       = new Mock <IRepository>();
            var searchCache      = new Mock <ISearchCache>();

            // Contract
            var cd    = new EnergyTrading.Mdm.Contracts.SourceSystemDetails();
            var nexus = new EnergyTrading.Mdm.Contracts.SystemData {
                StartDate = new DateTime(2012, 1, 1)
            };
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId {
                SystemName = "Test", Identifier = "A"
            };
            var contract = new EnergyTrading.Mdm.Contracts.SourceSystem {
                Details = cd, MdmSystemData = nexus
            };

            contract.Identifiers.Add(identifier);

            // Domain
            var system = new SourceSystem {
                Name = "Test"
            };
            var mapping = new SourceSystemMapping {
                System = system, MappingValue = "A"
            };
            var d1 = ObjectMother.Create <SourceSystem>();

            d1.Id        = 1;
            d1.Timestamp = 74UL.GetVersionByteArray();
            var entity = ObjectMother.Create <SourceSystem>();

            entity.Timestamp = 74UL.GetVersionByteArray();
            entity.AddDetails(d1);

            var d2    = ObjectMother.Create <SourceSystem>();
            var range = new DateRange(new DateTime(2012, 1, 1), DateTime.MaxValue);

            validatorFactory.Setup(x => x.IsValid(It.IsAny <CreateMappingRequest>(), It.IsAny <IList <IRule> >())).Returns(true);
            validatorFactory.Setup(x => x.IsValid(It.IsAny <EnergyTrading.Mdm.Contracts.SourceSystem>(), It.IsAny <IList <IRule> >())).Returns(true);

            repository.Setup(x => x.FindOne <SourceSystem>(1)).Returns(entity);

            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.SourceSystemDetails, SourceSystem>(cd)).Returns(d2);
            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.SystemData, DateRange>(nexus)).Returns(range);
            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.MdmId, SourceSystemMapping>(identifier)).Returns(mapping);

            var service = new SourceSystemService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Act
            service.Update(1, 74, contract);

            // Assert
            Assert.AreEqual(0, d2.Mappings.Count, "Created entity mappings count differs");
            Assert.AreEqual(0, d2.Id, "Created entity id differs");

            Assert.AreEqual(1, entity.Mappings.Count, "Mapping count differs");
            repository.Verify(x => x.Save(entity));
            repository.Verify(x => x.Flush());

            // Ok, hack the created one to align it
            d2.Id = entity.Id;
            foreach (var m in entity.Mappings)
            {
                d2.Mappings.Add(m);
            }

            // Should now be the same - avoid exposing what properties we have here
            Check(d2, entity);
        }
Beispiel #27
0
        public void ValidDetailsSaved()
        {
            var validatorFactory = new Mock <IValidatorEngine>();
            var mappingEngine    = new Mock <IMappingEngine>();
            var repository       = new Mock <IRepository>();
            var searchCache      = new Mock <ISearchCache>();

            // Contract
            var cd = new EnergyTrading.MDM.Contracts.Sample.BrokerDetails {
                Name = "Test"
            };
            var nexus = new EnergyTrading.Mdm.Contracts.SystemData {
                StartDate = new DateTime(2012, 1, 1)
            };
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId {
                SystemName = "Test", Identifier = "A"
            };
            var contract = new EnergyTrading.MDM.Contracts.Sample.Broker {
                Details = cd, MdmSystemData = nexus
            };

            contract.Identifiers.Add(identifier);

            // Domain
            var system = new SourceSystem {
                Name = "Test"
            };
            var mapping = new PartyRoleMapping {
                System = system, MappingValue = "A"
            };
            var d1 = new BrokerDetails {
                Id = 1, Name = "Test", Timestamp = 74UL.GetVersionByteArray()
            };
            var entity = new Broker()
            {
                Party = new Party()
                {
                    Id = 999
                }
            };

            entity.AddDetails(d1);

            var d2 = new BrokerDetails {
                Name = "Test"
            };
            var range = new DateRange(new DateTime(2012, 1, 1), DateTime.MaxValue);

            validatorFactory.Setup(x => x.IsValid(It.IsAny <CreateMappingRequest>(), It.IsAny <IList <IRule> >())).Returns(true);
            validatorFactory.Setup(x => x.IsValid(It.IsAny <EnergyTrading.MDM.Contracts.Sample.Broker>(), It.IsAny <IList <IRule> >())).Returns(true);

            repository.Setup(x => x.FindOne <Broker>(1)).Returns(entity);

            mappingEngine.Setup(x => x.Map <EnergyTrading.MDM.Contracts.Sample.BrokerDetails, BrokerDetails>(cd)).Returns(d2);
            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.SystemData, DateRange>(nexus)).Returns(range);
            mappingEngine.Setup(x => x.Map <EnergyTrading.Mdm.Contracts.MdmId, PartyRoleMapping>(identifier)).Returns(mapping);

            var service = new BrokerService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Act
            service.Update(1, 74, contract);

            // Assert
            Assert.AreEqual(2, entity.Details.Count, "Details count differs");
            Assert.AreEqual(1, entity.Mappings.Count, "Mapping count differs");
            repository.Verify(x => x.Save(entity));
            repository.Verify(x => x.Flush());
        }
        public void ValidDetailsSaved()
        {
            var validatorFactory = new Mock<IValidatorEngine>();
            var mappingEngine = new Mock<IMappingEngine>();
            var repository = new Mock<IRepository>();
            var searchCache = new Mock<ISearchCache>();

            // Contract
            var cd = new EnergyTrading.Mdm.Contracts.SourceSystemDetails();
            var nexus = new EnergyTrading.Mdm.Contracts.SystemData { StartDate = new DateTime(2012, 1, 1) };
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "A" };
            var contract = new EnergyTrading.Mdm.Contracts.SourceSystem { Details = cd, MdmSystemData = nexus };
            contract.Identifiers.Add(identifier);

            // Domain
            var system = new SourceSystem { Name = "Test" };
            var mapping = new SourceSystemMapping { System = system, MappingValue = "A" };
            var d1 = ObjectMother.Create<SourceSystem>();
            d1.Id = 1;
            d1.Timestamp = 74UL.GetVersionByteArray();
            var entity = ObjectMother.Create<SourceSystem>();
            entity.Timestamp = 74UL.GetVersionByteArray();
            entity.AddDetails(d1);

            var d2 = ObjectMother.Create<SourceSystem>();
            var range = new DateRange(new DateTime(2012, 1, 1), DateTime.MaxValue);

            validatorFactory.Setup(x => x.IsValid(It.IsAny<CreateMappingRequest>(), It.IsAny<IList<IRule>>())).Returns(true);
            validatorFactory.Setup(x => x.IsValid(It.IsAny<EnergyTrading.Mdm.Contracts.SourceSystem>(), It.IsAny<IList<IRule>>())).Returns(true);

            repository.Setup(x => x.FindOne<SourceSystem>(1)).Returns(entity);

            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.SourceSystemDetails, SourceSystem>(cd)).Returns(d2);
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.SystemData, DateRange>(nexus)).Returns(range);
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.MdmId, SourceSystemMapping>(identifier)).Returns(mapping);

            var service = new SourceSystemService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Act
            service.Update(1, 74, contract);

            // Assert
            Assert.AreEqual(0, d2.Mappings.Count, "Created entity mappings count differs");
            Assert.AreEqual(0, d2.Id, "Created entity id differs");

            Assert.AreEqual(1, entity.Mappings.Count, "Mapping count differs");
            repository.Verify(x => x.Save(entity));
            repository.Verify(x => x.Flush());

            // Ok, hack the created one to align it
            d2.Id = entity.Id;
            foreach (var m in entity.Mappings)
            {
                d2.Mappings.Add(m);
            }

            // Should now be the same - avoid exposing what properties we have here
            Check(d2, entity);
        }
        public void ValidCounterpartyPasses()
        {
            // Assert
            var start = new DateTime(1999, 1, 1);
            var system = new MDM.SourceSystem { Name = "Test" };

            var systemList = new List<MDM.SourceSystem> { system };
            var systemRepository = new Mock<IRepository>();
            var repository = new StubValidatorRepository();

            systemRepository.Setup(x => x.Queryable<MDM.SourceSystem>()).Returns(systemList.AsQueryable());

            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Test",
                Identifier = "1",
                StartDate = start.AddHours(-10),
                EndDate = start.AddHours(-5)
            };

            var validatorEngine = new Mock<IValidatorEngine>();
            var validator = new CounterpartyValidator(validatorEngine.Object, repository);

            var counterparty = new Counterparty { Details = new EnergyTrading.MDM.Contracts.Sample.CounterpartyDetails{Name = "Test"}, Identifiers = new EnergyTrading.Mdm.Contracts.MdmIdList { identifier } };
            this.AddRelatedEntities(counterparty);

            // Act
            var violations = new List<IRule>();
            var result = validator.IsValid(counterparty, violations);

            // Assert
            Assert.IsTrue(result, "Validator failed");
            Assert.AreEqual(0, violations.Count, "Violation count differs");
        }
        public void ValidDetailsSaved()
        {
            var validatorFactory = new Mock<IValidatorEngine>();
            var mappingEngine = new Mock<IMappingEngine>();
            var repository = new Mock<IRepository>();
            var searchCache = new Mock<ISearchCache>();

            // Contract
            var cd = new EnergyTrading.MDM.Contracts.Sample.CounterpartyDetails { Name = "Test" };
            var nexus = new EnergyTrading.Mdm.Contracts.SystemData { StartDate = new DateTime(2012, 1, 1) };
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId { SystemName = "Test", Identifier = "A" };
            var contract = new EnergyTrading.MDM.Contracts.Sample.Counterparty { Details = cd, MdmSystemData = nexus };
            contract.Identifiers.Add(identifier);

            // Domain
            var system = new SourceSystem { Name = "Test" };
            var mapping = new PartyRoleMapping { System = system, MappingValue = "A" };
            var d1 = new CounterpartyDetails { Id = 1, Name = "Test", Timestamp = 74UL.GetVersionByteArray() };
            var entity = new Counterparty();
            entity.Party = new Party() { Id = 1};
            entity.AddDetails(d1);

            var d2 = new CounterpartyDetails { Name = "Test" };
            var range = new DateRange(new DateTime(2012, 1, 1), DateTime.MaxValue);

            validatorFactory.Setup(x => x.IsValid(It.IsAny<CreateMappingRequest>(), It.IsAny<IList<IRule>>())).Returns(true);
            validatorFactory.Setup(x => x.IsValid(It.IsAny<EnergyTrading.MDM.Contracts.Sample.Counterparty>(), It.IsAny<IList<IRule>>())).Returns(true);

            repository.Setup(x => x.FindOne<Counterparty>(1)).Returns(entity);

            mappingEngine.Setup(x => x.Map<EnergyTrading.MDM.Contracts.Sample.CounterpartyDetails, CounterpartyDetails>(cd)).Returns(d2);
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.SystemData, DateRange>(nexus)).Returns(range);
            mappingEngine.Setup(x => x.Map<EnergyTrading.Mdm.Contracts.MdmId, PartyRoleMapping>(identifier)).Returns(mapping);

            var service = new CounterpartyService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Act
            service.Update(1, 74, contract);

            // Assert
            Assert.AreEqual(2, entity.Details.Count, "Details count differs");
            Assert.AreEqual(1, entity.Mappings.Count, "Mapping count differs");
            repository.Verify(x => x.Save(entity));
            repository.Verify(x => x.Flush());
        }
        public void SuccessMatch()
        {
            // Arrange
            var validatorFactory = new Mock <IValidatorEngine>();
            var mappingEngine    = new Mock <IMappingEngine>();
            var repository       = new Mock <IRepository>();
            var searchCache      = new Mock <ISearchCache>();

            var service = new LocationService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Domain details
            var start  = new DateTime(1999, 12, 31);
            var finish = new DateTime(2020, 1, 1);
            var system = new SourceSystem {
                Name = "Endur"
            };
            var mapping = new LocationMapping
            {
                System       = system,
                MappingValue = "A"
            };
            var location = new Location
            {
                Id       = 1,
                Validity = new DateRange(start, finish)
            };

            //Location.AddDetails(details);
            location.ProcessMapping(mapping);

            // Contract details
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Endur",
                Identifier = "A"
            };
            var cDetails = new EnergyTrading.MDM.Contracts.Sample.LocationDetails();

            mappingEngine.Setup(x => x.Map <LocationMapping, EnergyTrading.Mdm.Contracts.MdmId>(mapping)).Returns(identifier);
            mappingEngine.Setup(x => x.Map <Location, EnergyTrading.MDM.Contracts.Sample.LocationDetails>(location)).Returns(cDetails);
            validatorFactory.Setup(x => x.IsValid(It.IsAny <MappingRequest>(), It.IsAny <IList <IRule> >())).Returns(true);

            var list = new List <LocationMapping> {
                mapping
            };

            repository.Setup(x => x.Queryable <LocationMapping>()).Returns(list.AsQueryable());

            var request = new MappingRequest {
                SystemName = "Endur", Identifier = "A", ValidAt = SystemTime.UtcNow(), Version = 1
            };

            // Act
            var response  = service.Map(request);
            var candidate = response.Contract;

            // Assert
            mappingEngine.Verify(x => x.Map <LocationMapping, EnergyTrading.Mdm.Contracts.MdmId>(mapping));
            mappingEngine.Verify(x => x.Map <Location, EnergyTrading.MDM.Contracts.Sample.LocationDetails>(location));
            repository.Verify(x => x.Queryable <LocationMapping>());
            Assert.IsNotNull(candidate, "Contract null");
            Assert.AreEqual(2, candidate.Identifiers.Count, "Identifier count incorrect");
            // NB This is order dependent
            Assert.AreSame(identifier, candidate.Identifiers[1], "Different identifier assigned");
            Assert.AreSame(cDetails, candidate.Details, "Different details assigned");
            Assert.AreEqual(start, candidate.MdmSystemData.StartDate, "Start date differs");
            Assert.AreEqual(finish, candidate.MdmSystemData.EndDate, "End date differs");
        }
        public void SuccessMatchCurrentVersion()
        {
            // Arrange
            var validatorFactory = new Mock <IValidatorEngine>();
            var mappingEngine    = new Mock <IMappingEngine>();
            var repository       = new Mock <IRepository>();
            var searchCache      = new Mock <ISearchCache>();

            var service = new PartyService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Domain details
            var start  = new DateTime(1999, 12, 31);
            var finish = new DateTime(2020, 1, 1);
            var system = new SourceSystem {
                Name = "Endur"
            };
            var mapping = new PartyMapping
            {
                System       = system,
                MappingValue = "A"
            };
            var details = new PartyDetails
            {
                Name     = "Party 1",
                Validity = new DateRange(start, finish)
            };
            var party = new Party
            {
                Id = 1
            };

            party.AddDetails(details);
            party.ProcessMapping(mapping);

            // Contract details
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Endur",
                Identifier = "A"
            };
            var cDetails = new EnergyTrading.MDM.Contracts.Sample.PartyDetails
            {
                Name = "Party 1"
            };

            mappingEngine.Setup(x => x.Map <PartyMapping, EnergyTrading.Mdm.Contracts.MdmId>(mapping)).Returns(identifier);
            mappingEngine.Setup(x => x.Map <PartyDetails, EnergyTrading.MDM.Contracts.Sample.PartyDetails>(details)).Returns(cDetails);
            validatorFactory.Setup(x => x.IsValid(It.IsAny <MappingRequest>(), It.IsAny <IList <IRule> >())).Returns(true);

            var list = new List <PartyMapping> {
                mapping
            };

            repository.Setup(x => x.Queryable <PartyMapping>()).Returns(list.AsQueryable());

            var request = new MappingRequest
            {
                SystemName = "Endur",
                Identifier = "A",
                ValidAt    = SystemTime.UtcNow(),
                Version    = 0
            };

            // Act
            var response = service.Map(request);

            // Assert
            repository.Verify(x => x.Queryable <PartyMapping>());

            Assert.IsNull(response.Contract, "Contract not null");
            Assert.IsTrue(response.IsValid);
            Assert.AreEqual(0UL, response.Version);
        }
        public void SuccessMatchCurrentVersion()
        {
            // Arrange
            var validatorFactory = new Mock<IValidatorEngine>();
            var mappingEngine = new Mock<IMappingEngine>();
            var repository = new Mock<IRepository>();
            var searchCache = new Mock<ISearchCache>();

            var service = new PartyService(validatorFactory.Object, mappingEngine.Object, repository.Object, searchCache.Object);

            // Domain details
            var start = new DateTime(1999, 12, 31);
            var finish = new DateTime(2020, 1, 1);
            var system = new SourceSystem { Name = "Endur" };
            var mapping = new PartyMapping
            {
                System = system,
                MappingValue = "A"
            };
            var details = new PartyDetails
            {
                Name = "Party 1",
                Validity = new DateRange(start, finish)
            };
            var party = new Party
            {
                Id = 1
            };
            party.AddDetails(details);
            party.ProcessMapping(mapping);

            // Contract details
            var identifier = new EnergyTrading.Mdm.Contracts.MdmId
            {
                SystemName = "Endur",
                Identifier = "A"
            };
            var cDetails = new EnergyTrading.MDM.Contracts.Sample.PartyDetails
            {
                Name = "Party 1"
            };

            mappingEngine.Setup(x => x.Map<PartyMapping, EnergyTrading.Mdm.Contracts.MdmId>(mapping)).Returns(identifier);
            mappingEngine.Setup(x => x.Map<PartyDetails, EnergyTrading.MDM.Contracts.Sample.PartyDetails>(details)).Returns(cDetails);
            validatorFactory.Setup(x => x.IsValid(It.IsAny<MappingRequest>(), It.IsAny<IList<IRule>>())).Returns(true);

            var list = new List<PartyMapping> { mapping };
            repository.Setup(x => x.Queryable<PartyMapping>()).Returns(list.AsQueryable());

            var request = new MappingRequest
            {
                SystemName = "Endur",
                Identifier = "A",
                ValidAt = SystemTime.UtcNow(),
                Version = 0
            };

            // Act
            var response = service.Map(request);

            // Assert
            repository.Verify(x => x.Queryable<PartyMapping>());

            Assert.IsNull(response.Contract, "Contract not null");
            Assert.IsTrue(response.IsValid);
            Assert.AreEqual(0UL, response.Version);
        }