public void CreateResults_WithProducerConsecutivelyRemovedInNetUploadsbutLatesOnAdded_ReturnsEeeDataHistoryWithOneBlankDataRowforProducer() { // Arrange IWeeeAuthorization authorization = new AuthorizationBuilder().AllowInternalAreaAccess().Build(); var context = A.Fake <WeeeContext>(); var csvWriterFactory = A.Fake <CsvWriterFactory>(); GetProducerEeeDataHistoryCsvHandler handler = new GetProducerEeeDataHistoryCsvHandler( authorization, context, csvWriterFactory); //first upload ProducerEeeHistoryCsvData results = new ProducerEeeHistoryCsvData(); ProducerEeeHistoryCsvData.ProducerInReturnsResult upload1 = CreateEeeDataForProducer("WEE/MM0841AA", "WEE/FA0000KE/SCH", "Test Scheme", 2007, 1, new DateTime(2016, 1, 1), 100, "No"); results.ProducerReturnsHistoryData.Add(upload1); //producer removed in second upload ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult removedUpload = CreateRemovedProducerResults("WEE/FA0000KE/SCH", 2007, new DateTime(2016, 1, 2), 1); results.ProducerRemovedFromReturnsData.Add(removedUpload); //producer removed in third upload ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult removedUpload1 = CreateRemovedProducerResults("WEE/FA0000KE/SCH", 2007, new DateTime(2016, 1, 3), 1); results.ProducerRemovedFromReturnsData.Add(removedUpload1); //In this upload producer added back again with same data as first upload ProducerEeeHistoryCsvData.ProducerInReturnsResult upload2 = CreateEeeDataForProducer("WEE/MM0841AA", "WEE/FA0000KE/SCH", "Test Scheme", 2007, 1, new DateTime(2016, 1, 4), 200, "Yes"); results.ProducerReturnsHistoryData.Add(upload2); //Act IEnumerable <GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult> csvResults = new List <GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult>(); csvResults = handler.CreateResults(results); //Assert Assert.NotNull(csvResults); Assert.Equal(3, csvResults.Count()); Assert.Collection(csvResults, (r1) => Assert.Equal(new DateTime(2016, 1, 4), r1.SubmittedDate), (r2) => Assert.Equal(new DateTime(2016, 1, 2), r2.SubmittedDate), (r3) => Assert.Equal(new DateTime(2016, 1, 1), r3.SubmittedDate)); Assert.Collection(csvResults, (r1) => Assert.Equal("Yes", r1.LatestData), (r2) => Assert.Equal("No", r2.LatestData), (r3) => Assert.Equal("No", r3.LatestData)); Assert.Collection(csvResults, (r1) => Assert.Equal(200, r1.Cat1B2B), (r2) => Assert.Equal(null, r2.Cat1B2B), (r3) => Assert.Equal(100, r3.Cat1B2B)); }
public void CreateResults_WithProducerRemovedAfterFirstUpload_ReturnsEeeDataHistorywithLatestAsBlankDataforProducer() { // Arrange IWeeeAuthorization authorization = new AuthorizationBuilder().AllowInternalAreaAccess().Build(); var context = A.Fake <WeeeContext>(); var csvWriterFactory = A.Fake <CsvWriterFactory>(); GetProducerEeeDataHistoryCsvHandler handler = new GetProducerEeeDataHistoryCsvHandler( authorization, context, csvWriterFactory); ProducerEeeHistoryCsvData results = new ProducerEeeHistoryCsvData(); results.ProducerReturnsHistoryData.Add(CreateEeeDataForProducer("WEE/MM0841AA", "WEE/FA0000KE/SCH", "schemeB2B", 2007, 1, new DateTime(2016, 1, 1), 100, "Yes")); results.ProducerRemovedFromReturnsData.Add(CreateRemovedProducerResults("WEE/FA0000KE/SCH", 2007, new DateTime(2016, 1, 2), 1)); //Act IEnumerable <GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult> csvResults = new List <GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult>(); csvResults = handler.CreateResults(results); //Assert Assert.NotNull(csvResults); Assert.Equal(2, csvResults.Count()); Assert.Collection(csvResults, (r1) => Assert.Equal(new DateTime(2016, 1, 2), r1.SubmittedDate), (r2) => Assert.Equal(new DateTime(2016, 1, 1), r2.SubmittedDate)); Assert.Collection(csvResults, (r1) => Assert.Equal("Yes", r1.LatestData), (r2) => Assert.Equal("No", r2.LatestData)); Assert.Collection(csvResults, (r1) => Assert.Equal(null, r1.Cat1B2B), (r2) => Assert.Equal(100, r2.Cat1B2B)); }
public void CreateResults_WithProducerRemovedAfterFirstUpload_ReturnsEeeDataHistorywithLatestAsBlankDataforProducer() { // Arrange IWeeeAuthorization authorization = new AuthorizationBuilder().AllowInternalAreaAccess().Build(); var context = A.Fake<WeeeContext>(); var csvWriterFactory = A.Fake<CsvWriterFactory>(); GetProducerEeeDataHistoryCsvHandler handler = new GetProducerEeeDataHistoryCsvHandler( authorization, context, csvWriterFactory); ProducerEeeHistoryCsvData results = new ProducerEeeHistoryCsvData(); results.ProducerReturnsHistoryData.Add(CreateEeeDataForProducer("WEE/MM0841AA", "WEE/FA0000KE/SCH", "schemeB2B", 2007, 1, new DateTime(2016, 1, 1), 100, "Yes")); results.ProducerRemovedFromReturnsData.Add(CreateRemovedProducerResults("WEE/FA0000KE/SCH", 2007, new DateTime(2016, 1, 2), 1)); //Act IEnumerable<GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult> csvResults = new List<GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult>(); csvResults = handler.CreateResults(results); //Assert Assert.NotNull(csvResults); Assert.Equal(2, csvResults.Count()); Assert.Collection(csvResults, (r1) => Assert.Equal(new DateTime(2016, 1, 2), r1.SubmittedDate), (r2) => Assert.Equal(new DateTime(2016, 1, 1), r2.SubmittedDate)); Assert.Collection(csvResults, (r1) => Assert.Equal("Yes", r1.LatestData), (r2) => Assert.Equal("No", r2.LatestData)); Assert.Collection(csvResults, (r1) => Assert.Equal(null, r1.Cat1B2B), (r2) => Assert.Equal(100, r2.Cat1B2B)); }
public void CreateResults_WithProducerConsecutivelyRemovedInNetUploadsbutLatesOnAdded_ReturnsEeeDataHistoryWithOneBlankDataRowforProducer() { // Arrange IWeeeAuthorization authorization = new AuthorizationBuilder().AllowInternalAreaAccess().Build(); var context = A.Fake<WeeeContext>(); var csvWriterFactory = A.Fake<CsvWriterFactory>(); GetProducerEeeDataHistoryCsvHandler handler = new GetProducerEeeDataHistoryCsvHandler( authorization, context, csvWriterFactory); //first upload ProducerEeeHistoryCsvData results = new ProducerEeeHistoryCsvData(); ProducerEeeHistoryCsvData.ProducerInReturnsResult upload1 = CreateEeeDataForProducer("WEE/MM0841AA", "WEE/FA0000KE/SCH", "Test Scheme", 2007, 1, new DateTime(2016, 1, 1), 100, "No"); results.ProducerReturnsHistoryData.Add(upload1); //producer removed in second upload ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult removedUpload = CreateRemovedProducerResults("WEE/FA0000KE/SCH", 2007, new DateTime(2016, 1, 2), 1); results.ProducerRemovedFromReturnsData.Add(removedUpload); //producer removed in third upload ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult removedUpload1 = CreateRemovedProducerResults("WEE/FA0000KE/SCH", 2007, new DateTime(2016, 1, 3), 1); results.ProducerRemovedFromReturnsData.Add(removedUpload1); //In this upload producer added back again with same data as first upload ProducerEeeHistoryCsvData.ProducerInReturnsResult upload2 = CreateEeeDataForProducer("WEE/MM0841AA", "WEE/FA0000KE/SCH", "Test Scheme", 2007, 1, new DateTime(2016, 1, 4), 200, "Yes"); results.ProducerReturnsHistoryData.Add(upload2); //Act IEnumerable<GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult> csvResults = new List<GetProducerEeeDataHistoryCsvHandler.EeeHistoryCsvResult>(); csvResults = handler.CreateResults(results); //Assert Assert.NotNull(csvResults); Assert.Equal(3, csvResults.Count()); Assert.Collection(csvResults, (r1) => Assert.Equal(new DateTime(2016, 1, 4), r1.SubmittedDate), (r2) => Assert.Equal(new DateTime(2016, 1, 2), r2.SubmittedDate), (r3) => Assert.Equal(new DateTime(2016, 1, 1), r3.SubmittedDate)); Assert.Collection(csvResults, (r1) => Assert.Equal("Yes", r1.LatestData), (r2) => Assert.Equal("No", r2.LatestData), (r3) => Assert.Equal("No", r3.LatestData)); Assert.Collection(csvResults, (r1) => Assert.Equal(200, r1.Cat1B2B), (r2) => Assert.Equal(null, r2.Cat1B2B), (r3) => Assert.Equal(100, r3.Cat1B2B)); }
public IEnumerable <EeeHistoryCsvResult> CreateResults(ProducerEeeHistoryCsvData results) { List <EeeHistoryCsvResult> csvResults = new List <EeeHistoryCsvResult>(); var uniqueSetsforHistory = results.ProducerReturnsHistoryData.GroupBy(p => new { p.ApprovalNumber, p.ComplianceYear, p.Quarter, p.SchemeName, p.PRN }) .Select(x => new { x.Key.ApprovalNumber, x.Key.ComplianceYear, x.Key.Quarter, x.Key.SchemeName, x.Key.PRN, count = x.Count() }); var uniqueSetsforRemovedHistory = results.ProducerRemovedFromReturnsData.GroupBy(p => new { p.ApprovalNumber, p.ComplianceYear, p.Quarter }) .Select(x => new { x.Key.ApprovalNumber, x.Key.ComplianceYear, x.Key.Quarter, count = x.Count() }); foreach (var set in uniqueSetsforHistory) { string prn = set.PRN; string schemeName = set.SchemeName; List <ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult> removedList = new List <ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult>(); //for each (scheme, year and quarter) combination in ProducerEEEHisory resultset //get the earliest date, discards the results from removed producers result set which are less than earliest set var resultforHistoryDataSet = results.ProducerReturnsHistoryData.Where(p => (p.ApprovalNumber == set.ApprovalNumber && p.Quarter == set.Quarter && p.ComplianceYear == set.ComplianceYear)); var resultforRemovedDataSet = results.ProducerRemovedFromReturnsData.Where(p => (p.ApprovalNumber == set.ApprovalNumber && p.Quarter == set.Quarter && p.ComplianceYear == set.ComplianceYear)); var earliestDateForSet = resultforHistoryDataSet.Min(d => d.SubmittedDate); var maxDateForSet = resultforHistoryDataSet.Max(d => d.SubmittedDate); if (resultforRemovedDataSet.Any()) { //discard for all records from removedproducer result set where date is less than earliestDate for this set var newRemovedResultSet = resultforRemovedDataSet.Where(r => (r.SubmittedDate > earliestDateForSet)); //removed duplicate submissions which happened after the producer was removed if (newRemovedResultSet.Any()) { var newRemovedProducerResultSet = newRemovedResultSet.Where(p => p.SubmittedDate > maxDateForSet); if (newRemovedProducerResultSet.Any()) { removedList.Add(newRemovedProducerResultSet.First()); removedList.AddRange(newRemovedResultSet.Where(d => d.SubmittedDate < newRemovedProducerResultSet.First().SubmittedDate)); } else { removedList.AddRange(newRemovedResultSet); } } } List <ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult> duplicateItemsToRemove = new List <ProducerEeeHistoryCsvData.ProducerRemovedFromReturnsResult>(); for (int i = 0; i < resultforHistoryDataSet.Count(); i++) { var item = resultforHistoryDataSet.ElementAt(i); var nextItem = resultforHistoryDataSet.ElementAtOrDefault(i + 1); //remove duplicate submission between 2 submission in case where producer was removed and and then added back after few submissions. if (removedList.Any()) { if (removedList.Count > 1 && item != null && nextItem != null) { var duplicates = removedList.Where(p => p.SubmittedDate > item.SubmittedDate && p.SubmittedDate < nextItem.SubmittedDate).Skip(1); if (duplicates.Any()) { foreach (var it in duplicates) { duplicateItemsToRemove.Add(it); } } } } EeeHistoryCsvResult row = new EeeHistoryCsvResult(item.PRN, item.ApprovalNumber, item.SchemeName, item.ComplianceYear, item.Quarter, item.SubmittedDate, item.LatestData, item.Cat1B2C, item.Cat2B2C, item.Cat3B2C, item.Cat4B2C, item.Cat5B2C, item.Cat6B2C, item.Cat7B2C, item.Cat8B2C, item.Cat9B2C, item.Cat10B2C, item.Cat11B2C, item.Cat12B2C, item.Cat13B2C, item.Cat14B2C, item.Cat1B2B, item.Cat2B2B, item.Cat3B2B, item.Cat4B2B, item.Cat5B2B, item.Cat6B2B, item.Cat7B2B, item.Cat8B2B, item.Cat9B2B, item.Cat10B2B, item.Cat11B2B, item.Cat12B2B, item.Cat13B2B, item.Cat14B2B); csvResults.Add(row); } //remove consecutive items from removed list foreach (var duplicateItem in duplicateItemsToRemove) { removedList.Remove(duplicateItem); } //add all the records for the returns in which producer was removed. foreach (var item in removedList) { string latestData = (item.SubmittedDate > maxDateForSet) ? "Yes" : "No"; if (latestData == "Yes") { //Set "no" for the max date data from producer history result set csvResults.Single(s => s.SubmittedDate == maxDateForSet).LatestData = "No"; } EeeHistoryCsvResult row = new EeeHistoryCsvResult(prn, item.ApprovalNumber, schemeName, item.ComplianceYear, item.Quarter, item.SubmittedDate, latestData); csvResults.Add(row); } } return(csvResults.OrderByDescending(r => r.ComplianceYear).ThenByDescending(r => r.SubmittedDate)); }