using Microsoft.EntityFrameworkCore; using Moq; using ReserveManagementSystemApi.Services; using ReserveManagementSystemLib.Models; using ReserveManagementSystemLib.Persistence; using ReserveManagementSystemLib.Repositories; using System; using System.Collections.Generic; using System.Linq; using TechTalk.SpecFlow; using TechTalk.SpecFlow.Assist; using TechTalk.SpecFlow.Assist.ValueRetrievers; using Xunit; using ReserveManagementSystemLib.Enums; namespace ReserveManagementSystemApi.Tests.ServiceTests { [Binding] public sealed class ImportServiceSteps { // For additional details on SpecFlow step definitions see https://go.specflow.org/doc-stepdef private readonly ScenarioContext _scenarioContext; private ImportService _importService; private AppDbContext _appDbContext; private IEnumerable _importsResult; private Import _importResult; private Exception _exception; public ImportServiceSteps(ScenarioContext scenarioContext) { _scenarioContext = scenarioContext; Service.Instance.ValueRetrievers.Register(new NullValueRetriever("")); } [BeforeScenario] public void CreateDatabase() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; _appDbContext = new AppDbContext(options); _importService = new ImportService(new ImportRepository(_appDbContext)); } [AfterScenario] public void DisposeAppDbContext() { _appDbContext.Database.EnsureDeleted(); _appDbContext.Dispose(); } [Given(@"the Import table has the following imports")] public void GivenTheImportTableHasTheFollowingImports(Table table) { var imports = table.CreateSet(); _appDbContext.Imports.AddRange(imports); _appDbContext.SaveChanges(); } [Given(@"the Import Repository throws an exception in ListAll")] public void GivenTheImportRepositoryThrowsAnExceptionInListAll() { var mockImportRepo = new Mock(); mockImportRepo.Setup(a => a.GetAll()).Throws(new Exception()); _importService = new ImportService(mockImportRepo.Object); } [Given(@"the Import Repository throws an exception in FindByIdAsync")] public void GivenTheImportRepositoryThrowsAnExceptionInFindByIdAsync() { var mockImportRepo = new Mock(); mockImportRepo.Setup(a => a.FindByIdAsync(It.IsAny())).Throws(new Exception()); _importService = new ImportService(mockImportRepo.Object); } [When(@"I get the list of all imports with the import service")] public async void WhenIGetTheListOfAllImportsWithTheImportService() { try { _importsResult = await _importService.GetAllAsync(); } catch (Exception ex) { _exception = ex; } } [When(@"I get the list of the top (.*) imports with the import service")] public async void WhenIGetTheListOfAllImportsWithTheImportService(int numberOfImports) { try { _importsResult = await _importService.GetRecentAsync(numberOfImports); } catch (Exception ex) { _exception = ex; } } [When(@"I get the import with the importId (.*) from the import service")] public async void WhenIGetTheImportWithTheImportIdFromTheImportService(int importId) { try { _importResult = await _importService.GetByIdAsync(importId); } catch (Exception ex) { _exception = ex; } } [When(@"I get the last loan import of type ""(.*)""")] public async void WhenIGetTheLastLoanImportOfType(string importType) { var type = (ImportType)Enum.Parse(typeof(ImportType), importType, true); _importResult = await _importService.GetLastLoanImportByType(type); } [Then(@"the result from the import service should have (.*) imports")] public void ThenTheResultFromTheImportServiceShouldHaveImports(int importCount) { Assert.Equal(importCount, _importsResult.Count()); } [Then(@"the first import from the import service has ImportId (.*)")] public void ThenTheFirstImportFromTheImportServiceHasImportId(int importId) { Assert.Equal(importId, _importsResult.First().ImportId); } [Then(@"the Import Service throws an exception with the message ""(.*)""")] public void ThenTheImportServiceThrowsAnExceptionWithTheMessage(string message) { Assert.NotNull(_exception); Assert.Equal(message, _exception.Message); } [Then(@"the import from the import service has ImportId (.*)")] public void ThenTheImportFromTheImportServiceHasImportId(int importId) { Assert.Equal(importId, _importResult.ImportId); } } }