using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using LCU.API.Collections.RepoApi.Models.Requests; using LCU.API.Collections.RepoApi.Services; using LCU.Shared.Collections.RepoApiLib.Exceptions; using LCU.Shared.Collections.RepoApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Repositories; using LCU.Shared.Collections.RepoApiLib.WebServices.AutoIMS; using Moq; using Xunit; using AuctionAssignment = LCU.Shared.Collections.RepoApiLib.Models.AuctionAssignment; namespace LCU.API.Collections.RepoApiTests.Services { public class AuctionAssignmentServiceTest { private readonly Mock mockUnitOfWork; private readonly Mock mockAuctionAssignmentRepository; private readonly Mock mockAutoImsWebService; private readonly Mock mockRepoAccountService; private readonly Mock mockEmailer; private readonly AuctionAssignmentService service; public AuctionAssignmentServiceTest() { mockUnitOfWork = new Mock(); mockAutoImsWebService = new Mock(); mockAuctionAssignmentRepository = new Mock(); mockRepoAccountService = new Mock(); mockEmailer = new Mock(); service = new AuctionAssignmentService( mockAutoImsWebService.Object, mockRepoAccountService.Object, mockAuctionAssignmentRepository.Object, mockUnitOfWork.Object, mockEmailer.Object); } [Fact] public async void Post_Success() { mockAuctionAssignmentRepository.Setup(repository => repository.AddAsync(It.IsAny())) .ReturnsAsync(new AuctionAssignment() { ID = 1914, AuctionId = "MMAA", AuctionCustomerNo = "1234-56" }); mockRepoAccountService.Setup(service => service.Get(It.IsAny())).ReturnsAsync(new RepoAccount() { Borrowers = new List { new Borrower() { OwnershipType = 6 } } }); var auctionAssignmentResponse = await service.SaveAsync(new AuctionAssignmentPostRequest() { AuctionAssignment = new AuctionAssignment() { RepoId = 1234 }, Collaterals = new List { new Collateral { CollateralId = 1 }, new Collateral() }, VehicleLocation = new VehicleLocation(), PrimaryCollateralId = 1, }); Assert.NotNull(auctionAssignmentResponse); Assert.NotNull(auctionAssignmentResponse.AuctionAssignment); Assert.NotNull(auctionAssignmentResponse.Collaterals); Assert.NotNull(auctionAssignmentResponse.VehicleLocation); Assert.Equal(1914, auctionAssignmentResponse.AuctionAssignment.ID); Assert.Equal("MMAA", auctionAssignmentResponse.AuctionAssignment.AuctionId); Assert.Equal("1234-56", auctionAssignmentResponse.AuctionAssignment.AuctionCustomerNo); Assert.Equal(2, auctionAssignmentResponse.Collaterals.Count()); } [Fact] public async void Post_Duplicate_Resource_Error() { // FindByAuctionCustomerNoAndVin() should NOT affect duplicate detection errors mockAuctionAssignmentRepository .Setup(repository => repository.FindByByAuctionCustomerNoAndVin(It.IsAny(), It.IsAny())) .ReturnsAsync(new AuctionAssignment()); // The Existance of MATCHING active collateral should mockAuctionAssignmentRepository.Setup(repository => repository.FindByRepoIdAsync(It.IsAny())) .ReturnsAsync( new List() { new AuctionAssignment() { AuctionAssignmentToCollaterals = new List() { new AuctionAssignmentToCollateral { CollateralId = 42 }, new AuctionAssignmentToCollateral { CollateralId = 5 } // Matching Collateral }, IsActive = true } }); mockAuctionAssignmentRepository.Setup(repository => repository.AddAsync(It.IsAny())) .ReturnsAsync(new AuctionAssignment()); mockRepoAccountService.Setup(service => service.Get(It.IsAny())).ReturnsAsync(new RepoAccount() { Borrowers = new List { new Borrower() { OwnershipType = 6 } } }); try { var auctionAssignmentResponse = await service.SaveAsync(new AuctionAssignmentPostRequest() { AuctionAssignment = new AuctionAssignment() { RepoId = 1234 }, Collaterals = new List { new Collateral { CollateralId = 1 }, new Collateral() { CollateralId = 5 } }, VehicleLocation = new VehicleLocation(), PrimaryCollateralId = 1, }); Assert.True(false, "An Exception should have been thrown."); } catch (DuplicateResourceException) { } catch (Exception) { Assert.True(false, "An DuplicateResourceException should have been thrown. Another exception was thrown instead."); } } [Fact] public async void GetAuctionCustomerNo_Success() { mockRepoAccountService.Setup(service => service.Get(It.IsAny())).ReturnsAsync(new RepoAccount() { LoanNo = 56, Borrowers = new List { new Borrower() { OwnershipType = 6, IndividualId = "1234" } }, Collaterals = new List { new Collateral() { RefCollateralId = "151834" } } }); var auctionCustomerNo = await service.GetAuctionCustomerNoAsync(1234, "151834"); Assert.Equal("1234-151834", auctionCustomerNo); } [Fact] public async void Update_No_Auction_Change() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 6 }; mockAuctionAssignmentRepository.Setup(x => x.FindByIdAsync(5)).ReturnsAsync(assignment); mockAuctionAssignmentRepository.Setup(x => x.Update(It.IsAny())).Verifiable(); mockUnitOfWork.Setup(x => x.CompleteAsync()).Verifiable(); mockAutoImsWebService.Setup(x => x.PostVehicleAsync(It.IsAny())) .Verifiable(); // act var result = await service.UpdateAsync(assignmentUpdated); // Assert Assert.NotNull(result); mockAuctionAssignmentRepository.Verify(x => x.FindByIdAsync(5), Times.Once); mockAutoImsWebService.Verify(x => x.PostVehicleAsync(It.IsAny()), Times.Never); } [Fact] public async void Update_Yes_Auction_Change() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 1000, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 6 }; mockAuctionAssignmentRepository.Setup(x => x.FindByIdAsync(5)).ReturnsAsync(assignment); mockAuctionAssignmentRepository.Setup(x => x.Update(It.IsAny())).Verifiable(); mockAutoImsWebService.Setup(x => x.PostVehicleAsync(It.IsAny())) .Verifiable(); mockAutoImsWebService.Setup(x => x.PostVehicleAsync(It.IsAny())) .ReturnsAsync((WebServiceResponse)null); // act var result = await service.UpdateAsync(assignmentUpdated); // Assert Assert.NotNull(result); mockAuctionAssignmentRepository.Verify(x => x.FindByIdAsync(5), Times.Once); mockAutoImsWebService.Verify(x => x.PostVehicleAsync(It.IsAny()), Times.Once); } [Fact] public void Floor_Price_Change_Manual_Success() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 1000, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 6 }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 1 } }; mockAutoImsWebService.Setup(x => x.PostVehicleAsync(It.IsAny())) .ReturnsAsync(response); mockEmailer.Setup(x => x.SendFloorPriceAcceptedEmail(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK)); // act service.UpdateAuctionFloor(assignmentUpdated, assignment).GetAwaiter().GetResult(); // Assert Assert.NotNull(assignment); Assert.Equal(1000, assignment.FloorPrice); mockAutoImsWebService.Verify(x => x.PostVehicleAsync(It.IsAny()), Times.Once); mockEmailer.Verify( x => x.SendFloorPriceAcceptedEmail(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Fact] public void Floor_Price_Change_Fail() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 1000, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5 }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 5, ValidationError = new WebServiceResponseStatusValidationError[] { new WebServiceResponseStatusValidationError() { Message = "test failure", Code = "5" } } } }; mockAutoImsWebService.Setup(x => x.PostVehicleAsync(It.IsAny())) .ReturnsAsync(response); // act service.UpdateAuctionFloor(assignmentUpdated, assignment).GetAwaiter().GetResult(); // Assert Assert.NotNull(assignment); Assert.Equal(0, assignment.FloorPrice); mockAutoImsWebService.Verify(x => x.PostVehicleAsync(It.IsAny()), Times.Once); } [Fact] public void Floor_Price_Change_Automatic_Success() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 1000, FloorPriceValuesUsed = new FloorPriceValuesUsed { MMRRegionalAdjustedPrice = 1000, NADAAverageAdjustedAuctionValue = 1000, PercentageOf = 100 }, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5 }; mockRepoAccountService.Setup(x => x.FindByAuctionAssignmentId(It.IsAny())) .ReturnsAsync(new RepoAccount { AccountNo = "1234567-98" }); WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 1 } }; mockAutoImsWebService.Setup(x => x.PostVehicleAsync(It.IsAny())) .ReturnsAsync(response); mockEmailer.Setup(x => x.SendFloorPriceAcceptedEmail(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new HttpResponseMessage(System.Net.HttpStatusCode.OK)); // act service.UpdateAuctionFloor(assignmentUpdated, assignment).GetAwaiter().GetResult(); // Assert Assert.NotNull(assignment); Assert.Equal(1000, assignment.FloorPrice); mockAutoImsWebService.Verify(x => x.PostVehicleAsync(It.IsAny()), Times.Once); mockEmailer.Verify( x => x.SendFloorPriceAcceptedEmail(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Fact] public void Title_Sent_Date_Change_Success() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, SentTitleDocsDate = null }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, SentTitleDocsDate = new System.DateTime(2017, 12, 21) }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 1 } }; mockAutoImsWebService.Setup(x => x.TitleSent(It.IsAny())).ReturnsAsync(response); // act service.UpdateTitleSentDate(assignmentUpdated, assignment).GetAwaiter().GetResult(); // assert Assert.NotNull(assignment); Assert.Equal(new System.DateTime(2017, 12, 21), assignment.SentTitleDocsDate); mockAutoImsWebService.Verify(x => x.TitleSent(It.IsAny()), Times.Once); } [Fact] public void Title_Sent_Date_Change_Fail() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, SentTitleDocsDate = new System.DateTime(2017, 12, 21) }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 5, ValidationError = new WebServiceResponseStatusValidationError[] { new WebServiceResponseStatusValidationError() { Message = "test failure", Code = "5" } } } }; mockAutoImsWebService.Setup(x => x.TitleSent(It.IsAny())).ReturnsAsync(response); // act service.UpdateTitleSentDate(assignmentUpdated, assignment).GetAwaiter().GetResult(); // assert Assert.NotNull(assignment); Assert.Null(assignment.SentTitleDocsDate); mockAutoImsWebService.Verify(x => x.TitleSent(It.IsAny()), Times.Once); } [Fact] public void Redeemed_Date_Change_Success() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, RedeemedDate = null }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, RedeemedDate = new System.DateTime(2017, 12, 21) }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 1 } }; mockAutoImsWebService.Setup(x => x.Redeem(It.IsAny())).ReturnsAsync(response); // act service.UpdateRedeemedDate(assignmentUpdated, assignment).GetAwaiter().GetResult(); // assert Assert.NotNull(assignment); Assert.Equal(new System.DateTime(2017, 12, 21), assignment.RedeemedDate); mockAutoImsWebService.Verify(x => x.Redeem(It.IsAny()), Times.Once); } [Fact] public void Redeemed_Date_Change_Fail() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, RedeemedDate = new System.DateTime(2017, 12, 21) }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 5, ValidationError = new WebServiceResponseStatusValidationError[] { new WebServiceResponseStatusValidationError() { Message = "test failure", Code = "5" } } } }; mockAutoImsWebService.Setup(x => x.Redeem(It.IsAny())).ReturnsAsync(response); // act service.UpdateRedeemedDate(assignmentUpdated, assignment).GetAwaiter().GetResult(); // assert Assert.NotNull(assignment); Assert.Null(assignment.RedeemedDate); mockAutoImsWebService.Verify(x => x.Redeem(It.IsAny()), Times.Once); } [Fact] public void Auction_Hold_Change_Success() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, HoldStatus = 3, HoldUntil = new DateTime(2017, 12, 21) }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 1 } }; mockAutoImsWebService.Setup(x => x.PlaceHold(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(response); // act service.UpdateHoldData(assignmentUpdated, assignment).GetAwaiter().GetResult(); // Assert Assert.NotNull(assignment); Assert.Equal(3, assignment.HoldStatus); Assert.Equal(new DateTime(2017, 12, 21), assignment.HoldUntil); mockAutoImsWebService.Verify(x => x.PlaceHold(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Fact] public async void Auction_Hold_Change_Fail() { // arrange var assignment = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, }; var assignmentUpdated = new AuctionAssignment() { ID = 5, FloorPrice = 0, AuctionCustomerNo = "123456-789", AuctionId = "Mannheim", CollateralVIN = "12345678901234567", VehicleType = 5, HoldStatus = 3 }; WebServiceResponse response = new WebServiceResponse() { AuctionCollateralVin = assignmentUpdated.CollateralVIN, Status = new WebServiceResponseStatus() { Description = "success", Code = 5, ValidationError = new WebServiceResponseStatusValidationError[] { new WebServiceResponseStatusValidationError() { Message = "test failure", Code = "5" } } } }; mockAutoImsWebService.Setup(x => x.PlaceHold(It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(response); // act await service.UpdateHoldData(assignmentUpdated, assignment); // Assert Assert.NotNull(assignment); Assert.Null(assignment.HoldStatus); Assert.Null(assignment.HoldUntil); mockAutoImsWebService.Verify(x => x.PlaceHold(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); } [Fact] public async void GetByRepoId_Ok() { // Arrange mockAuctionAssignmentRepository.Setup(repo => repo.FindByRepoIdAsync(It.IsAny())).ReturnsAsync( new List() { new AuctionAssignment(), new AuctionAssignment() }); // Act var auctionAssignments = await service.GetByRepoIdAsync(5); // Assert Assert.NotNull(auctionAssignments); Assert.Equal(2, auctionAssignments.Count()); } [Fact] public async void RemoveAuction_Ok() { // Arrange mockAuctionAssignmentRepository.Setup(x => x.FindByIdAsync(It.IsAny())).ReturnsAsync( new AuctionAssignment() { ID = 1, IsActive = true, CollateralVIN = "123456789", AuctionCustomerNo = "123-45" }); // Act var result = await service.RemoveAuction(1); // Assert Assert.NotNull(result); Assert.False(result.IsActive); mockAutoImsWebService.Verify(x => x.RemoveFromAuction(It.IsAny(), It.IsAny()), Times.Once()); mockAuctionAssignmentRepository.Verify(x => x.Update(It.IsAny()), Times.Once()); mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once()); } } }