Files
monorepo/LCU.API.Collections.RepoApi-old/LCU.API.Collections.RepoApiTests/Services/AuctionAssignmentServiceTest.cs
T
2026-04-20 14:47:24 -05:00

765 lines
29 KiB
C#

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<IUnitOfWork> mockUnitOfWork;
private readonly Mock<IAuctionAssignmentRepository> mockAuctionAssignmentRepository;
private readonly Mock<IAutoImsWebService> mockAutoImsWebService;
private readonly Mock<IRepoAccountService> mockRepoAccountService;
private readonly Mock<IEmailer> mockEmailer;
private readonly AuctionAssignmentService service;
public AuctionAssignmentServiceTest()
{
mockUnitOfWork = new Mock<IUnitOfWork>();
mockAutoImsWebService = new Mock<IAutoImsWebService>();
mockAuctionAssignmentRepository = new Mock<IAuctionAssignmentRepository>();
mockRepoAccountService = new Mock<IRepoAccountService>();
mockEmailer = new Mock<IEmailer>();
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<AuctionAssignment>()))
.ReturnsAsync(new AuctionAssignment()
{
ID = 1914,
AuctionId = "MMAA",
AuctionCustomerNo = "1234-56"
});
mockRepoAccountService.Setup(service => service.Get(It.IsAny<int>())).ReturnsAsync(new RepoAccount()
{
Borrowers = new List<Borrower>
{
new Borrower()
{
OwnershipType = 6
}
}
});
var auctionAssignmentResponse = await service.SaveAsync(new AuctionAssignmentPostRequest()
{
AuctionAssignment = new AuctionAssignment()
{
RepoId = 1234
},
Collaterals = new List<Collateral>
{
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<string>(), It.IsAny<string>()))
.ReturnsAsync(new AuctionAssignment());
// The Existance of MATCHING active collateral should
mockAuctionAssignmentRepository.Setup(repository => repository.FindByRepoIdAsync(It.IsAny<int>()))
.ReturnsAsync(
new List<AuctionAssignment>()
{
new AuctionAssignment()
{
AuctionAssignmentToCollaterals = new List<AuctionAssignmentToCollateral>()
{
new AuctionAssignmentToCollateral { CollateralId = 42 },
new AuctionAssignmentToCollateral { CollateralId = 5 } // Matching Collateral
},
IsActive = true
}
});
mockAuctionAssignmentRepository.Setup(repository => repository.AddAsync(It.IsAny<AuctionAssignment>()))
.ReturnsAsync(new AuctionAssignment());
mockRepoAccountService.Setup(service => service.Get(It.IsAny<int>())).ReturnsAsync(new RepoAccount()
{
Borrowers = new List<Borrower>
{
new Borrower()
{
OwnershipType = 6
}
}
});
try
{
var auctionAssignmentResponse = await service.SaveAsync(new AuctionAssignmentPostRequest()
{
AuctionAssignment = new AuctionAssignment()
{
RepoId = 1234
},
Collaterals = new List<Collateral>
{
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<int>())).ReturnsAsync(new RepoAccount()
{
LoanNo = 56,
Borrowers = new List<Borrower>
{
new Borrower()
{
OwnershipType = 6,
IndividualId = "1234"
}
},
Collaterals = new List<Collateral>
{
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<AuctionAssignment>())).Verifiable();
mockUnitOfWork.Setup(x => x.CompleteAsync()).Verifiable();
mockAutoImsWebService.Setup(x => x.PostVehicleAsync<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()))
.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<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()),
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<AuctionAssignment>())).Verifiable();
mockAutoImsWebService.Setup(x => x.PostVehicleAsync<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()))
.Verifiable();
mockAutoImsWebService.Setup(x => x.PostVehicleAsync<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()))
.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<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()),
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<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()))
.ReturnsAsync(response);
mockEmailer.Setup(x =>
x.SendFloorPriceAcceptedEmail(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<decimal>(),
It.IsAny<decimal>())).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<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()),
Times.Once);
mockEmailer.Verify(
x => x.SendFloorPriceAcceptedEmail(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<decimal>(),
It.IsAny<decimal>()), 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<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()))
.ReturnsAsync(response);
// act
service.UpdateAuctionFloor(assignmentUpdated, assignment).GetAwaiter().GetResult();
// Assert
Assert.NotNull(assignment);
Assert.Equal(0, assignment.FloorPrice);
mockAutoImsWebService.Verify(x => x.PostVehicleAsync<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()),
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<int>()))
.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<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()))
.ReturnsAsync(response);
mockEmailer.Setup(x =>
x.SendFloorPriceAcceptedEmail(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<decimal>(),
It.IsAny<decimal>())).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<FloorPriceVehicle>(It.IsAny<FloorPriceVehicle>()),
Times.Once);
mockEmailer.Verify(
x => x.SendFloorPriceAcceptedEmail(It.IsAny<bool>(), It.IsAny<string>(), It.IsAny<decimal>(),
It.IsAny<decimal>()), 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<string>())).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<string>()), 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<string>())).ReturnsAsync(response);
// act
service.UpdateTitleSentDate(assignmentUpdated, assignment).GetAwaiter().GetResult();
// assert
Assert.NotNull(assignment);
Assert.Null(assignment.SentTitleDocsDate);
mockAutoImsWebService.Verify(x => x.TitleSent(It.IsAny<string>()), 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<string>())).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<string>()), 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<string>())).ReturnsAsync(response);
// act
service.UpdateRedeemedDate(assignmentUpdated, assignment).GetAwaiter().GetResult();
// assert
Assert.NotNull(assignment);
Assert.Null(assignment.RedeemedDate);
mockAutoImsWebService.Verify(x => x.Redeem(It.IsAny<string>()), 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<string>(), It.IsAny<int>(), It.IsAny<DateTime>()))
.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<string>(), It.IsAny<int>(), It.IsAny<DateTime>()),
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<string>(), It.IsAny<int>(), It.IsAny<DateTime>()))
.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<string>(), It.IsAny<int>(), It.IsAny<DateTime>()),
Times.Once);
}
[Fact]
public async void GetByRepoId_Ok()
{
// Arrange
mockAuctionAssignmentRepository.Setup(repo => repo.FindByRepoIdAsync(It.IsAny<int>())).ReturnsAsync(
new List<AuctionAssignment>()
{
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<int>())).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<string>(), It.IsAny<string>()),
Times.Once());
mockAuctionAssignmentRepository.Verify(x => x.Update(It.IsAny<AuctionAssignment>()), Times.Once());
mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once());
}
}
}