private async Task <List <RecordEntity> > UpdateRecord(DomainEntity domain, List <RecordEntity> records) { DnsResponse response = await _dnsRecordClient.GetRecord(domain.Name); if (IsSuccess(response.ResponseCode)) { //find old records List <RecordEntity> oldRecords = records.Where(existingRecord => response.Records.All(responseRecord => !responseRecord.Equals(existingRecord.RecordInfo))) .Select(_ => new RecordEntity(_.Id, _.Domain, _.RecordInfo, response.ResponseCode, _.FailureCount, DateTime.UtcNow)) .ToList(); //find unchanged records List <RecordEntity> unchangedRecords = records.Where(existingRecord => response.Records.Any(responseRecord => responseRecord.Equals(existingRecord.RecordInfo))).ToList(); //find new records List <RecordEntity> newRecords = response.Records.Where(responseRecords => records.All(exisingRecord => !exisingRecord.RecordInfo.Equals(responseRecords))) .Select(_ => new RecordEntity(null, domain, _, response.ResponseCode, 0)) .ToList(); List <RecordEntity> updatedSuccessfulRecords = oldRecords .Concat(unchangedRecords) .Concat(newRecords) .ToList(); //create placeholder if no records if (updatedSuccessfulRecords.All(_ => _.EndDate != null)) { updatedSuccessfulRecords.Add(new RecordEntity(null, domain, null, response.ResponseCode, 0)); } return(updatedSuccessfulRecords); } //Once placeholder in play keep returning placeholder on failure RecordEntity recordEntity = records.SingleOrDefault(_ => _.FailureCount == -1); if (recordEntity != null) { return(records); } //less that 3 failures increment failure count if (records.Any() && records.Max(_ => _.FailureCount) < 3) { return(records.Select(_ => new RecordEntity(_.Id, domain, _.RecordInfo, response.ResponseCode, _.FailureCount + 1)).ToList()); } //otherwise expire records and add placeholder return(records.Select(_ => new RecordEntity(_.Id, domain, _.RecordInfo, response.ResponseCode, _.FailureCount, DateTime.UtcNow)). Concat(new List <RecordEntity> { new RecordEntity(null, domain, null, response.ResponseCode, -1) }).ToList()); }
public async Task OldRecordsCorrectlyUpdated() { string domain = "a.b.com"; DomainEntity domainEntity = new DomainEntity(1, domain); DmarcRecordInfo existingRecord = new DmarcRecordInfo("existing record"); Dictionary <DomainEntity, List <RecordEntity> > records = new Dictionary <DomainEntity, List <RecordEntity> > { { domainEntity, new List <RecordEntity> { new RecordEntity(1, domainEntity, existingRecord, RCode.NoError, 0) } } }; A.CallTo(() => _dnsRecordClient.GetRecord(domain)).Returns(Task.FromResult(new DnsResponse(new List <RecordInfo> { DmarcRecordInfo.EmptyRecordInfo }, RCode.NoError))); List <RecordEntity> updatedRecords = await _dnsRecordsUpdater.UpdateRecord(records); Assert.That(updatedRecords.Count, Is.EqualTo(2)); Assert.That(updatedRecords[0].EndDate, Is.Not.Null); Assert.That(updatedRecords[0].RecordInfo, Is.EqualTo(existingRecord)); }