public async Task ReturnLogDTO_WhenLogIsFound() { var logId = 1; var description = "description"; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); Log mapInput = null; mappingProviderMocked.Setup(mpm => mpm.MapTo <LogDTO>(It.IsAny <Log>())) .Callback <object>(inputArg => mapInput = inputArg as Log); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); LogBookTestUtils.GetContextWithLogAndAuthorAndStatusAndCategory(nameof(ReturnLogDTO_WhenLogIsFound), logId, description); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ReturnLogDTO_WhenLogIsFound)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); var log = await assertContext.Logs.FirstOrDefaultAsync(l => l.Id == logId); await logbookService.FindLog(logId); Assert.AreEqual(log.Id, mapInput.Id); } }
public async Task ThrowException_WhenLogBookIsNotFound() { var logbookId = 1; var userId = "userId"; var description = "description"; var categoryId = 1; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); //LogBookTestUtils.GetContextWithLog(nameof(AddLog_ThrowException_WhenLogBookIsNotFound), logId); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ThrowException_WhenLogBookIsNotFound)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); await Assert.ThrowsExceptionAsync <ArgumentException>( async() => await logbookService.AddLog(logbookId, userId, description, categoryId)); } }
public async Task ThrowException_WhenUserIsNotInRole() { var userId = "userId"; var role = "manager"; var user = new User() { Id = userId }; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); //UserManager<User> userManager = null; userManagerWrapperMocked.Setup(umwm => umwm.IsInRoleAsync(user, role)); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); LogBookTestUtils.GetContextWithUser(nameof(ThrowException_WhenUserIsNotInRole), userId); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ThrowException_WhenUserIsNotInRole)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); await Assert.ThrowsExceptionAsync <ArgumentException>( async() => await logbookService.GetLogBooksAndCategories(userId)); } }
public async Task ReturnLogBooksAndCategories_WhenUserIsFoundAndInRole() { var userId = "userId"; var role = "manager"; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); userManagerWrapperMocked.Setup(um => um.IsInRoleAsync(It.IsAny <User>(), It.IsAny <string>())).ReturnsAsync(true); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); LogBookTestUtils.GetContextWithUserAndLogBookAndCategory(nameof(ReturnLogBooksAndCategories_WhenUserIsFoundAndInRole), userId); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ReturnLogBooksAndCategories_WhenUserIsFoundAndInRole)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); var logbooksAndCategories = await logbookService.GetLogBooksAndCategories(userId); Assert.AreEqual(logbooksAndCategories.LogBooks.Count, 1); Assert.AreEqual(logbooksAndCategories.Categories.Count, 1); } }
public async Task Return_WhenStatusIsChanged() { var logId = 1; var oldStatusId = 1; var newStatusId = 2; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); LogBookTestUtils.GetContextWithLogAndStatuses(nameof(Return_WhenStatusIsChanged), logId, oldStatusId, newStatusId); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(Return_WhenStatusIsChanged)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); var log = await assertContext.Logs.FirstOrDefaultAsync(l => l.StatusId == oldStatusId); await logbookService.ChangeStatusOfLogAsync(newStatusId, logId); Assert.AreEqual(newStatusId, log.StatusId); } }
public async Task <ActionResult> DeleteLivro([FromRoute] int Id) { LogBookService logBookService = new LogBookService(this._context); try { LivroServiceImpl livroService = new LivroServiceImpl(this._context); var livro = livroService.FindById(Id); if (livro != null) { livroService.Delete(livro); await _context.SaveChangesAsync(); logBookService.SalvarLog(livro, "Registro de livro excluído: " + livro.Titulo); _context.SaveChanges(); return(Ok("Livro deletado com sucesso!")); } else { return(Ok("Não foi possível remover o livro, título não encontrado")); } } catch (Exception e) { logBookService.SalvarLog("Erro ao excluir Livro"); return(StatusCode(404, "Erro ao excluir Livro " + e.Message)); } }
public async Task <ActionResult> SalvarLivro([FromBody] LivroDTO livroDTO) { LogBookService logBookService = new LogBookService(this._context); try { LivroServiceImpl livroService = new LivroServiceImpl(this._context); var resultado = livroService.ValidaSeTituloDeLivroEstaCadastrado(livroDTO.Titulo); if (resultado == false) { Livro livro = new Livro(livroDTO.Titulo); livroService.SalvarLivro(livro); await _context.SaveChangesAsync(); logBookService.SalvarLog(livro, "Foi registrado o livro: " + livro.Titulo); _context.SaveChanges(); return(Ok("Livro salvo com sucesso")); } return(Ok("Livro já cadastrado")); } catch (Exception e) { logBookService.SalvarLog("Erro ao salvar livro"); return(StatusCode(404, "Erro ao salvar Livro " + e.Message)); } }
public async Task <ActionResult> EditaLivro([FromRoute] int Id, [FromBody] LivroDTO livroDTO) { LogBookService logBookService = new LogBookService(this._context); try { LivroServiceImpl livroService = new LivroServiceImpl(this._context); var livro = livroService.FindById(Id); if (livro != null) { var livroEditado = livroService.EditaLivro(livro, livroDTO); await _context.SaveChangesAsync(); logBookService.SalvarLog(livro, "Atualização de Livro realizada: " + livro.Titulo); _context.SaveChanges(); return(Ok("Livro alterado com sucesso! " + livroEditado.Titulo)); } else { logBookService.SalvarLog("Erro ao editar o livro"); return(Ok("Não foi possível editar o livro")); } } catch (Exception e) { return(StatusCode(404, "Erro ao editar livro" + e.Message)); } }
public async Task ListAllLogsForManagerAsync_CallCreateAsyncOnce() { var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); //LogBookTestUtils.GetContextWithBusiness(nameof(ListAllLogsForManagerAsync_CallCreateAsyncOnce), businessId, businessName); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ListAllLogsForManagerAsync_CallCreateAsyncOnce)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); await logbookService.ListAllLogsForManagerAsync(It.IsAny <string>(), It.IsAny <int?>(), It.IsAny <int>(), It.IsAny <string>()); paginatedListMocked.Verify(pl => pl.CreateAsync(It.IsAny <IQueryable <LogDTO> >(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>()), Times.Once); } }
public async Task ReturnLogDTO_WhenAllParametersArePassed() { var logbookId = 1; var userId = "userId"; var description = "description"; var categoryId = 1; var username = "******"; var statusId = 1; var statusType = "todo"; var categoryName = "category"; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); Log mapInput = null; mappingProviderMocked.Setup(mpm => mpm.MapTo <LogDTO>(It.IsAny <Log>())) .Callback <object>(inputArg => mapInput = inputArg as Log); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); LogBookTestUtils.GetContextWithFullLogAndLogBookAndUserAndStatusAndCategory(nameof(ReturnLogDTO_WhenAllParametersArePassed), logbookId, userId, categoryId, description, username, statusId, statusType, categoryName); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ReturnLogDTO_WhenAllParametersArePassed)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); await logbookService.AddLog(logbookId, userId, description, categoryId); var log = await assertContext.Logs.FirstOrDefaultAsync(l => l.LogBookId == logbookId); Assert.AreEqual(log.Id, mapInput.Id); Assert.AreEqual(log.LogBookId, mapInput.LogBookId); Assert.AreEqual(log.Description, mapInput.Description); Assert.AreEqual(log.StatusId, mapInput.StatusId); Assert.AreEqual(log.CreatedOn, mapInput.CreatedOn); Assert.IsFalse(mapInput.IsDeleted); } }
public async Task ThrowException_WhenStatusIdIsLargerThanStatusesCount() { var logId = 1; var statusId = 5; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); LogBookTestUtils.GetContextWithLogAndStatuses(nameof(ThrowException_WhenStatusIdIsLargerThanStatusesCount), logId); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ThrowException_WhenStatusIdIsLargerThanStatusesCount)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); await Assert.ThrowsExceptionAsync <ArgumentException>( async() => await logbookService.ChangeStatusOfLogAsync(statusId, logId)); } }
public async Task ReturnAllStatuses() { var logId = 1; var statusId = 1; var statusId2 = 2; var mappingProviderMocked = new Mock <IMappingProvider>(); var paginatedListMocked = new Mock <IPaginatedList <LogDTO> >(); var dateTimeWrapperMocked = new Mock <IDateTimeWrapper>(); var userManagerWrapperMocked = new Mock <IUserManagerWrapper <User> >(); var loggerMocked = new Mock <ILogger <LogBookService> >(); LogBookTestUtils.ResetAutoMapper(); LogBookTestUtils.InitializeAutoMapper(); LogBookTestUtils.GetContextWithLogAndStatuses(nameof(ReturnAllStatuses), logId, statusId, statusId2); using (var assertContext = new AlphaHotelDbContext(LogBookTestUtils.GetOptions(nameof(ReturnAllStatuses)))) { var logbookService = new LogBookService(assertContext, mappingProviderMocked.Object, paginatedListMocked.Object, dateTimeWrapperMocked.Object, userManagerWrapperMocked.Object, loggerMocked.Object); var logbooks = await logbookService.ListAllStatusesAsync(); Assert.AreEqual(2, logbooks.Count); } }