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 PartyRoleService(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 PartyRoleMapping
            {
                System       = system,
                MappingValue = "A"
            };
            var details = new PartyRoleDetails
            {
                Name     = "PartyRole 1",
                Validity = new DateRange(start, finish)
            };
            var party = new PartyRole
            {
                Id    = 1,
                Party = new Party {
                    Id = 999
                }
            };

            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.PartyRoleDetails
            {
                Name = "PartyRole 1"
            };

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

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

            repository.Setup(x => x.Queryable <PartyRoleMapping>()).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
            repository.Verify(x => x.Queryable <PartyRoleMapping>());

            Assert.IsTrue(response.IsValid);
            Assert.IsNotNull(candidate, "Contract null");

            mappingEngine.Verify(x => x.Map <PartyRoleMapping, EnergyTrading.Mdm.Contracts.MdmId>(mapping));
            mappingEngine.Verify(x => x.Map <PartyRoleDetails, EnergyTrading.MDM.Contracts.Sample.PartyRoleDetails>(details));

            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 PartyRoleService(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 PartyRoleMapping
            {
                System       = system,
                MappingValue = "A"
            };
            var details = new PartyRoleDetails
            {
                Name     = "PartyRole 1",
                Validity = new DateRange(start, finish)
            };
            var party = new PartyRole
            {
                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.PartyRoleDetails
            {
                Name = "PartyRole 1"
            };

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

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

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

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