using System; using System.Collections.Generic; using System.Linq; using LCU.Shared.Collections.RepoApiLib.Exceptions; using LCU.Shared.Collections.RepoApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Persistence; using LCU.Shared.Collections.RepoApiLib.Repositories; using Microsoft.AspNetCore.Mvc.ModelBinding; using Xunit; namespace LCU.API.Collections.RepoApiTests.Repository { public class EventAlertRepositoryTest : DbContextFromJson { private Shared.Collections.RepoApiLib.Repositories.EventAlertRepository _repository; private readonly AppDbContext _dbContext; public EventAlertRepositoryTest() { _dbContext = GetSqliteContextWithData("event_alert_repository.json"); _repository = new EventAlertRepository(_dbContext); } [Fact] public async void ListAllAsync_Test() { var resultsBasic = await _repository.ListAllAsync(); Assert.NotNull(resultsBasic); Assert.NotEmpty(resultsBasic); Assert.Equal("Unit Test", resultsBasic.First().CreatedBy); } [Fact] public async void ListUnreadEventsAsync_Test() { var resultsBasic = await _repository.ListUnreadAsync(); Assert.NotNull(resultsBasic); Assert.NotEmpty(resultsBasic); Assert.Equal(1, resultsBasic.Count()); } [Fact] public async void ListO4R_No_Company_Test() { var resultsBasic = await _repository.ListO4rNoCompanyAsync(); Assert.NotNull(resultsBasic); Assert.NotEmpty(resultsBasic); Assert.Single(resultsBasic); } [Fact] public async void ListRepo_No_Company_Test() { var resultsBasic = await _repository.ListRepoNoCompanyAsync(); Assert.NotNull(resultsBasic); Assert.NotEmpty(resultsBasic); Assert.Equal(1, resultsBasic.Count()); } [Fact] public async void ListRepo_No_Auction_Test() { var resultsBasic = await _repository.ListRepoNoAuctionAsync(); Assert.NotNull(resultsBasic); // There is 1, but it is closed. Assert.Empty(resultsBasic); } [Fact] public async void ListReadyForRetitleAsync_Test() { var resultsBasic = await _repository.ListAllAsync(); Assert.NotNull(resultsBasic); Assert.NotEmpty(resultsBasic); IEnumerable results = await _repository.ListReadyForRetitleAsync(); Assert.NotNull(results); Assert.NotEmpty(results); Assert.Equal(1, results.Count()); } [Fact] public async void AddAsync_DoesNotThrowWhenAlertShouldRepeat_Test() { var alert = new EventAlert { RepoId = 11688, AccountNo = "6829620143", Message = "test", CreatedBy = "Unit Test", DateAdded = DateTime.Parse("2019-06-29T00:00:00") }; await _repository.AddAsync(alert); await _dbContext.SaveChangesAsync(); } [Fact] public async void AddAsync_Invalid_Test() { var alert = new EventAlert { RepoId = 11688, AccountNo = "6829620143", Message = "test", CreatedBy = "012345678901234567890123456789", DateAdded = DateTime.Parse("2019-06-29T00:00:00") }; await _repository.AddAsync(alert); await _dbContext.SaveChangesAsync(); } [Fact] public async void AddAsync_ThrowsWhenAlertShouldNotRepeat_Test() { var alert = new EventAlert() { RepoId = 11688, AccountNo = "6829620143", Message = "RTC Letter Not Found", CreatedBy = "Unit Test", DateAdded = DateTime.Parse("2019-06-29T00:00:00") }; await Assert.ThrowsAsync(() => _repository.AddAsync(alert)); } [Fact] public async void AddAsync_Empty_Test() { await _repository.AddRangeAsync(new List()); await _dbContext.SaveChangesAsync(); } [Theory] [InlineData(14883, "9987149147", "test", true)] //This Data Already Exists in event_alert_repository.json [InlineData(00001, "0000000001", "test", false)] [InlineData(14883, "9987149147", "not test", false)] public void IsDuplicateAlert_Ok(int RepoID, string AccountNo, string Message, bool expected) { var alert = new EventAlert { RepoId = RepoID, AccountNo = AccountNo, Message = Message, CreatedBy = "Unit Test", DateAdded = DateTime.Parse("2019-06-27T00:00:00"), }; Assert.Equal(_repository.IsDuplicateAlert(alert), expected); } [Fact] public void IsDuplicateAlert_NonRepeatingAlerts_OK() { var alert = new EventAlert { RepoId = 11688, AccountNo = "6829620143", Message = "Payment Received", CreatedBy = "Unit Test", DateAdded = DateTime.Parse("2019-06-27T00:00:00"), MarkedReadBy = "tester" }; Assert.False(_repository.IsDuplicateAlert(alert)); } [Fact] public async void AddRangeAsync_Ignores_Duplicates_OK() { List eventAlerts = new List { new EventAlert { RepoId = 11688, AccountNo = "6829620143", Message = "Payment Received", CreatedBy = "Unit Test", DateAdded = DateTime.Parse("2019-06-27T00:00:00"), MarkedReadBy = "tester" }, new EventAlert() { RepoId = 1, AccountNo = "6829627143", CreatedBy = "Unit Test", Message = "Payment Received", DateAdded = DateTime.Parse("2019-06-27T00:00:00") }, new EventAlert() { RepoId = 2, AccountNo = "6829628143", CreatedBy = "Unit Test", Message = "Payment Received", DateAdded = DateTime.Parse("2019-06-27T00:00:00") }, }; var results = await _repository.AddRangeAsync(eventAlerts); await _dbContext.SaveChangesAsync(); Assert.Equal(3,results.Count()); Assert.True(results.ToList().Exists(x => x.RepoId == 11688)); } [Fact] public async void ListUnreadByAccountNoAsync_Ok() { var results = await _repository.ListUnreadByAccountNoAsync("9987149147"); Assert.Single(results); } } }