public void SaveHomeAddress_WithNonCachedAddress_ShouldCallActualLaunderer() { var empl = new Employee() { LOSOrgId = 10, AnsatForhold = "1", EkstraCiffer = 1, Leder = true, Stillingsbetegnelse = "Udvikler", AnsaettelsesDato = new DateTime(2015, 4, 28), Adresse = "Jens Baggesens Vej 44", PostNr = 8210, By = "Aarhus V" }; _actualLaundererMock.Launder(new CachedAddress()).ReturnsForAnyArgs(new CachedAddress { IsDirty = false, StreetName = "Jens Baggesens Vej", StreetNumber = "44", ZipCode = 8210, Town = "Aarhus V", DirtyString = "Jens Baggesens Vej 44, 8210 Aarhus V", }); _uut = new UpdateService(_emplRepoMock, _orgUnitRepoMock, _personRepoMock, _cachedAddressRepoMock, _personalAddressRepoMock, _actualLaundererMock, _coordinates, _dataProvider, _mailServiceMock, NSubstitute.Substitute.For <IAddressHistoryService>(), _reportRepo, _driveService, _subservice, _subRepo); _uut.UpdateHomeAddress(empl, 1); _actualLaundererMock.ReceivedWithAnyArgs().Launder(new Address()); }
public void LaunderAddress_SplittetStreetName_Good() { //Arrange Address address = new Address { StreetName = "Ny Adelgade", StreetNumber = "10", ZipCode = 1104 }; //Act Address result = uut.Launder(address); //Assert Assert.AreEqual(address, result); }
/// <summary> /// Get coordinates for a given addresses. Use this method for single calls outside the service class. /// </summary> /// <param name="address"></param> /// <param name="correctAddresses">Set this to use address laundering prior to each request, the corrected values will be replaced. Default: false</param> /// <exception cref="AddressCoordinatesException">Thrown if address has critical spelling errors(see inner exception) or if no address coordinates correspond to the entered address.</exception> /// <returns></returns> public Address GetAddressCoordinates(Address address, bool correctAddresses = false) { Address correctedAddress = address; try { if (address.Latitude != null && address.Longitude != null && address.Latitude != "" && address.Longitude != "") { return(correctedAddress); } correctedAddress = _addressLaunderer.Launder(address); return(correctedAddress); } catch (AddressLaunderingException e) { throw new AddressCoordinatesException("En valgt adresse kunne ikke vaskes.", e); } //var request = CreateCoordRequest(correctedAddress.StreetName, correctedAddress.StreetNumber, correctedAddress.ZipCode.ToString()); //string addressesString = ExecuteAndRead(request); //var addresses = ParseJson(addressesString); //if (!addresses.Any()) //{ // request = CreateCoordRequest(address.StreetName, null, address.ZipCode.ToString()); // addressesString = ExecuteAndRead(request); // addresses = ParseJson(addressesString); //} //if (!addresses.Any()) //{ // throw new AddressCoordinatesException("No coordinates returned."); //} //if (addresses[0].adgangsadresse.vejstykke.navn == address.StreetName // && addresses[0].adgangsadresse.postnummer.nr == address.ZipCode.ToString()) //{ // correctedAddress.Longitude = addresses[0].adgangsadresse.adgangspunkt.koordinater[0].ToString().Replace(",", "."); // correctedAddress.Latitude = addresses[0].adgangsadresse.adgangspunkt.koordinater[1].ToString().Replace(",", "."); // correctedAddress.Longitude = correctedAddress.Longitude.Remove(correctedAddress.Longitude.IndexOf('.') + 1 + CoordDecimals); // correctedAddress.Latitude = correctedAddress.Latitude.Remove(correctedAddress.Latitude.IndexOf('.') + 1 + CoordDecimals); //} //else //{ // throw new AddressCoordinatesException("The addresses returned differ highly from the original, streetname does not exist in zipcode area."); //} //return correctedAddress; }
public void CleanAddress_Clean_ShouldNotBeCalled() { _laundryMock.Launder(new Address()).ReturnsForAnyArgs(x => x.Arg <CachedAddress>()); var testAddr = new Address() { StreetName = "Katrinebjergvej", StreetNumber = "93b", ZipCode = 8200, Town = "Aarhus N", }; _uut.Launder(testAddr); _laundryMock.DidNotReceiveWithAnyArgs().Launder(new Address()); }
/// <summary> /// Attempts to launder an Address. /// If the address has previously been successfully laundered, then the result of that launder will be returned. /// Otherwise an actual lookup will be performed. /// </summary> /// <param name="inputAddress"></param> /// <returns>An Address</returns> public Address Launder(Address inputAddress) { var inputAddressString = inputAddress.StreetName + " " + inputAddress.StreetNumber + ", " + inputAddress.ZipCode + " " + inputAddress.Town; var cachedAddress = _repo.AsQueryable() .FirstOrDefault(addr => addr.DirtyString.Equals(inputAddressString)); // Return CachedAddress if one exists that is not dirty. if (cachedAddress != null && !cachedAddress.IsDirty) { return(cachedAddress); } var newCachedAddress = false; if (cachedAddress == null) { cachedAddress = new CachedAddress(inputAddress); newCachedAddress = true; } var isDirty = false; try { cachedAddress = (CachedAddress)_actualLaunderer.Launder(cachedAddress); } catch (AddressLaunderingException e) { //Logger.Error("Fejl ved adressevask", e); isDirty = true; } if (cachedAddress.Latitude == null || cachedAddress.Latitude.Equals("0")) { try { _coordinates.GetAddressCoordinates(cachedAddress, true); } catch (AddressCoordinatesException e) { //Logger.Error("Fejl ved opslag af adressekoordinater", e); isDirty = true; cachedAddress.Latitude = "0"; cachedAddress.Longitude = "0"; } } cachedAddress.IsDirty = isDirty; if (newCachedAddress) { cachedAddress = _repo.Insert(cachedAddress); } else { _repo.Update(cachedAddress); } _repo.Save(); return(cachedAddress); }
/// <summary> /// Get coordinates for a given addresses. Use this method for single calls outside the service class. /// </summary> /// <param name="address"></param> /// <param name="correctAddresses">Set this to use address laundering prior to each request, the corrected values will be replaced. Default: false</param> /// <exception cref="AddressCoordinatesException">Thrown if address has critical spelling errors(see inner exception) or if no address coordinates correspond to the entered address.</exception> /// <returns></returns> public Address GetAddressCoordinates(Address address, bool correctAddresses = false) { Address correctedAddress = address; try { if (address.Latitude != null && address.Longitude != null && address.Latitude != "" && address.Longitude != "") { return(correctedAddress); } correctedAddress = _addressLaunderer.Launder(address); return(correctedAddress); } catch (AddressLaunderingException e) { throw new AddressCoordinatesException("En valgt adresse kunne ikke vaskes.", e); } }
public IHttpActionResult AttemptCleanCachedAddress(Address input) { try { var cleanAddress = _launderer.Launder(input); cleanAddress = _coordinates.GetAddressCoordinates(cleanAddress, true); var cachedAddr = _cachedAddressRepo.AsQueryable().Single(x => x.Id.Equals(input.Id)); cachedAddr.Latitude = cleanAddress.Latitude; cachedAddr.Longitude = cleanAddress.Longitude; cachedAddr.StreetName = cleanAddress.StreetName; cachedAddr.StreetNumber = cleanAddress.StreetNumber; cachedAddr.ZipCode = cleanAddress.ZipCode; cachedAddr.Town = cleanAddress.Town; cachedAddr.IsDirty = false; _cachedAddressRepo.Save(); return(Ok()); } catch (Exception e) { return(StatusCode(HttpStatusCode.BadRequest)); } }
public void SetUp() { var orgList = new List <OrgUnit>(); var orgIdCount = 0; var cachedAddressList = new List <CachedAddress>(); var cachedIdCount = 0; _emplRepoMock = NSubstitute.Substitute.For <IGenericRepository <Employment> >(); _orgUnitRepoMock = NSubstitute.Substitute.For <IGenericRepository <OrgUnit> >(); _personRepoMock = NSubstitute.Substitute.For <IGenericRepository <Person> >(); _cachedAddressRepoMock = NSubstitute.Substitute.For <IGenericRepository <CachedAddress> >(); _personalAddressRepoMock = NSubstitute.Substitute.For <IGenericRepository <PersonalAddress> >(); _actualLaunderer = NSubstitute.Substitute.For <IAddressLaunderer>(); _coordinates = NSubstitute.Substitute.For <IAddressCoordinates>(); _dataProvider = NSubstitute.Substitute.For <IDbUpdaterDataProvider>(); _mailSender = NSubstitute.Substitute.For <IMailSender>(); _vacationBalanceRepo = NSubstitute.Substitute.For <IGenericRepository <Core.DomainModel.VacationBalance> >(); _orgUnitRepoMock.AsQueryable().Returns(orgList.AsQueryable()); _orgUnitRepoMock.Insert(new OrgUnit()).ReturnsForAnyArgs(x => x.Arg <OrgUnit>()).AndDoes(x => orgList.Add(x.Arg <OrgUnit>())).AndDoes(x => x.Arg <OrgUnit>().Id = orgIdCount).AndDoes(x => orgIdCount++); _cachedAddressRepoMock.Insert(new CachedAddress()).ReturnsForAnyArgs(x => x.Arg <CachedAddress>()).AndDoes(x => cachedAddressList.Add(x.Arg <CachedAddress>())).AndDoes(x => x.Arg <CachedAddress>().Id = cachedIdCount).AndDoes(x => cachedIdCount++); _cachedAddressRepoMock.AsQueryable().Returns(cachedAddressList.AsQueryable()); _subRepo = NSubstitute.Substitute.For <IGenericRepository <Core.DomainModel.Substitute> >(); _reportRepo = NSubstitute.Substitute.For <IGenericRepository <Report> >(); _repotService = NSubstitute.Substitute.For <IReportService <Report> >(); _subservice = NSubstitute.Substitute.For <ISubstituteService>(); _actualLaunderer.Launder(new Address()).ReturnsForAnyArgs(x => x.Arg <CachedAddress>()); _uut = new UpdateService(_emplRepoMock, _orgUnitRepoMock, _personRepoMock, _cachedAddressRepoMock, _personalAddressRepoMock, _actualLaunderer, _coordinates, _dataProvider, _mailSender, NSubstitute.Substitute.For <IAddressHistoryService>(), _reportRepo, _repotService, _subservice, _subRepo, _vacationBalanceRepo); }
public void SetUp() { var personList = new List <Person>(); var emplList = new List <Employment>(); var emplIdCount = 0; var personIdCount = 0; var cachedAddressList = new List <CachedAddress>(); var cachedIdCount = 0; var personalAddressList = new List <PersonalAddress>(); var personalIdCount = 0; _emplRepoMock = NSubstitute.Substitute.For <IGenericRepository <Employment> >(); _orgUnitRepoMock = NSubstitute.Substitute.For <IGenericRepository <OrgUnit> >(); _personRepoMock = NSubstitute.Substitute.For <IGenericRepository <Person> >(); _cachedAddressRepoMock = NSubstitute.Substitute.For <IGenericRepository <CachedAddress> >(); _personalAddressRepoMock = NSubstitute.Substitute.For <IGenericRepository <PersonalAddress> >(); _actualLaundererMock = NSubstitute.Substitute.For <IAddressLaunderer>(); _coordinatesMock = NSubstitute.Substitute.For <IAddressCoordinates>(); _dataProviderMock = NSubstitute.Substitute.For <IDbUpdaterDataProvider>(); _workAddressRepoMock = NSubstitute.Substitute.For <IGenericRepository <WorkAddress> >(); _mailServiceMock = NSubstitute.Substitute.For <IMailService>(); _actualLaunderer = NSubstitute.Substitute.For <IAddressLaunderer>(); _coordinates = NSubstitute.Substitute.For <IAddressCoordinates>(); _dataProvider = NSubstitute.Substitute.For <IDbUpdaterDataProvider>(); _subRepo = NSubstitute.Substitute.For <IGenericRepository <Core.DomainModel.Substitute> >(); _reportRepo = NSubstitute.Substitute.For <IGenericRepository <DriveReport> >(); _driveService = NSubstitute.Substitute.For <IDriveReportService>(); _subservice = NSubstitute.Substitute.For <ISubstituteService>(); _personRepoMock.AsQueryable().Returns(personList.AsQueryable()); _personRepoMock.Insert(new Person()).ReturnsForAnyArgs(x => x.Arg <Person>()).AndDoes(x => personList.Add(x.Arg <Person>())).AndDoes(x => x.Arg <Person>().Id = personIdCount).AndDoes(x => personIdCount++); _emplRepoMock.AsQueryable().Returns(emplList.AsQueryable()); _emplRepoMock.Insert(new Employment()).ReturnsForAnyArgs(x => x.Arg <Employment>()).AndDoes(x => emplList.Add(x.Arg <Employment>())).AndDoes(x => x.Arg <Employment>().Id = emplIdCount).AndDoes(x => emplIdCount++); _cachedAddressRepoMock.Insert(new CachedAddress()).ReturnsForAnyArgs(x => x.Arg <CachedAddress>()).AndDoes(x => cachedAddressList.Add(x.Arg <CachedAddress>())).AndDoes(x => x.Arg <CachedAddress>().Id = cachedIdCount).AndDoes(x => cachedIdCount++); cachedAddressList.Add(new CachedAddress() { Id = 999, StreetName = "Katrinebjergvej", StreetNumber = "93B", ZipCode = 8200, Town = "Aarhus N", DirtyString = "Katrinebjergvej 93B, 8200 Aarhus N", }); _cachedAddressRepoMock.AsQueryable().Returns(cachedAddressList.AsQueryable()); _personalAddressRepoMock.Insert(new PersonalAddress()).ReturnsForAnyArgs(x => x.Arg <PersonalAddress>()).AndDoes(x => personalAddressList.Add(x.Arg <PersonalAddress>())).AndDoes(x => x.Arg <PersonalAddress>().Id = personalIdCount).AndDoes(x => personalIdCount++); _personalAddressRepoMock.AsQueryable().Returns(personalAddressList.AsQueryable()); _actualLaundererMock.Launder(new Address()).ReturnsForAnyArgs(x => x.Arg <CachedAddress>()); _uut = new UpdateService(_emplRepoMock, _orgUnitRepoMock, _personRepoMock, _cachedAddressRepoMock, _personalAddressRepoMock, _actualLaunderer, _coordinates, _dataProvider, _mailServiceMock, NSubstitute.Substitute.For <IAddressHistoryService>(), _reportRepo, _driveService, _subservice, _subRepo); _orgUnitRepoMock.AsQueryable().ReturnsForAnyArgs(new List <OrgUnit>() { new OrgUnit() { Id = 1, OrgId = 1, ShortDescription = "ITM", LongDescription = "IT Minds, Aarhus", Level = 0, HasAccessToFourKmRule = false, }, new OrgUnit() { Id = 2, OrgId = 2, ShortDescription = "ITMB", LongDescription = "IT Minds, Aarhus child", Level = 1, ParentId = 1, HasAccessToFourKmRule = false, } }.AsQueryable()); personList.Add(new Person() { Id = 1, }); }