using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Moq; using NUnit.Framework; using LCU.Api.Internal.CoreApiWrapper.Controllers; using LCU.Api.Internal.CoreApiWrapper.Services; using LCU.Library.Internal.CoreApiWrapperModel.LcuModels.Loans; using LCU.Library.Internal.CoreApiWrapperModel.LcuModels.Collaterals; using LCU.Library.Internal.CoreApiWrapperModel.CoreModels.Properties; using System.Linq; namespace LCU.Api.Internal.CoreApiWrapperTest.Controllers { public class LoanControllerTest { private LoanController _loanController; private Mock _mockLoanService; [SetUp] public void SetUp() { _mockLoanService = new Mock(); _loanController = new LoanController(_mockLoanService.Object); } [Test] public async Task GetLoanDetails_OK() { _mockLoanService.Setup(x => x.GetLoanDetail(123334234324)).ReturnsAsync(new LoanDetail(){ LoanId = 1}).Verifiable(); var results = await _loanController.GetLoanDetails(123334234324); Assert.NotNull(results); var okResult = results as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult.StatusCode); var result = okResult.Value as LoanDetail; Assert.NotNull(result); Assert.AreEqual(1, result.LoanId); _mockLoanService.Verify(); } [Test] public async Task GetLoanDetails_Exception() { _mockLoanService.Setup(x => x.GetLoanDetail(It.IsAny())).Throws(new Exception("Oops")); var results = await _loanController.GetLoanDetails(1234555); Assert.NotNull(results); var badRes = results as StatusCodeResult; Assert.NotNull(badRes); Assert.AreEqual(500, badRes.StatusCode); } [Test] public async Task GetLoanCollateral_OK() { var collateralResponse = new List() { new CollateralDetail() { CollateralId = 1 }, new CollateralDetail() { CollateralId = 2 } }; _mockLoanService.Setup(x => x.GetCollateralDetails(123334234324)).ReturnsAsync(collateralResponse).Verifiable(); var results = await _loanController.GetLoanCollateral(123334234324); Assert.NotNull(results); var okResult = results as OkObjectResult; Assert.NotNull(okResult); Assert.AreEqual(200, okResult.StatusCode); var resultsValue = okResult.Value as List; Assert.NotNull(resultsValue); Assert.AreEqual(2, resultsValue.Count); _mockLoanService.Verify(); } [Test] public async Task GetLoanCollateral_Exception() { _mockLoanService.Setup(x => x.GetCollateralDetails(It.IsAny())).Throws(new Exception("Oops")); var results = await _loanController.GetLoanCollateral(1234555); Assert.NotNull(results); var badRes = results as ObjectResult; Assert.NotNull(badRes); Assert.AreEqual(500, badRes.StatusCode); } [Test] public async Task GetPropertyInformation_Exception() { _mockLoanService.Setup(x => x.GetPropertyInformation(It.IsAny())).Throws(new Exception("Oops")); var results = await _loanController.GetPropertyInformation(1234555); Assert.NotNull(results); var result = results as ObjectResult; Assert.NotNull(result); Assert.AreEqual(500, result.StatusCode); } [Test] public async Task GetPropertyInformation_Ok() { Property property = new Property() { PropertyNumber = 1000 }; IEnumerable properties = new List() { property }; _mockLoanService.Setup(x => x.GetPropertyInformation(1000)).ReturnsAsync(properties); var results = await _loanController.GetPropertyInformation(1000); Assert.NotNull(results); var goodRes = results as OkObjectResult; Assert.NotNull(goodRes); Assert.AreEqual(200, goodRes.StatusCode); } } }