Ejemplo n.º 1
0
            public IEnumerator <object[]> GetEnumerator()
            {
                var searchDto = new TestSearchDto();

                yield return(new object[]
                {
                    new SearchRequest <TestSearchDto>
                    {
                        Search = searchDto,
                        Page = 0,
                        PageSize = 10
                    }
                });

                yield return(new object[]
                {
                    new SearchRequest <TestSearchDto>
                    {
                        Search = searchDto,
                        Page = 2,
                        PageSize = 10
                    }
                });
            }
        public async Task Deve_Buscar_Registro_Por_Id_Pela_Controller()
        {
            //Given
            var services = new ServiceCollection();
            var entity   = new TestEntity(
                new NomeValueObject("Weslley_1", "Carneiro"),
                new EmailValueObject("*****@*****.**")
                );
            var entity2 = new TestEntity(
                new NomeValueObject("Weslley_2", "Carneiro"),
                new EmailValueObject("*****@*****.**")
                );
            var entity3 = new TestEntity(
                new NomeValueObject("Weslley_3", "Carneiro"),
                new EmailValueObject("*****@*****.**")
                );

            var searchDto = new TestSearchDto();

            services.AddLogging();
            services.AddAutoMapper(typeof(TestViewModel));
            services.AddDomainNotifications();
            services.AddContext <Context>(options =>
            {
                options
                .EnabledInMemory()
                .EnabledLogging();
            });
            services.AddRepository <ITestReadRepository, TestReadRepository>("Optsol.Components.Test.Utils");
            services.AddApplications <IBaseServiceApplication <TestEntity>, TestServiceApplication>("Optsol.Components.Test.Utils");
            services.AddServices();

            var provider = services.BuildServiceProvider();
            IApiControllerBase <TestEntity, TestViewModel, TestViewModel, InsertTestViewModel, UpdateTestViewModel, TestSearchDto> controllerBase =
                new ApiControllerBase <TestEntity, TestViewModel, TestViewModel, InsertTestViewModel, UpdateTestViewModel, TestSearchDto>(
                    provider.GetRequiredService <ILoggerFactory>(),
                    provider.GetRequiredService <IBaseServiceApplication <TestEntity> >(),
                    provider.GetRequiredService <IResponseFactory>());

            var unitOfWork = provider.GetRequiredService <IUnitOfWork>();
            var repository = provider.GetRequiredService <ITestWriteRepository>();

            await repository.InsertAsync(entity);

            await repository.InsertAsync(entity2);

            await repository.InsertAsync(entity3);

            await unitOfWork.CommitAsync();

            //When
            var actionResult = await controllerBase.GetByIdAsync(entity.Id);

            //Then
            ((OkObjectResult)actionResult).StatusCode.Should().NotBeNull();
            ((OkObjectResult)actionResult).StatusCode.Should().Be((int)HttpStatusCode.OK);

            var resultObj = JsonConvert.DeserializeObject <Response <TestViewModel> >(((OkObjectResult)actionResult).Value.ToJson());

            resultObj.Should().NotBeNull();
            resultObj.Success.Should().BeTrue();
            resultObj.Failure.Should().BeFalse();
            resultObj.Messages.Should().BeEmpty();
            resultObj.Data.Should().NotBeNull();
            resultObj.Data.Nome.Should().Be(entity.Nome.ToString());
            resultObj.Data.Contato.Should().Be(entity.Email.ToString());
            resultObj.Data.Ativo.Should().Be("Inativo");
        }
Ejemplo n.º 3
0
        public async Task Deve_Registrar_Logs_No_ApiController()
        {
            //Given
            var model           = new TestViewModel();
            var insertViewModel = new InsertTestViewModel();
            var updateViewModel = new UpdateTestViewModel();

            var testSearchDto = new TestSearchDto();

            testSearchDto.Nome      = "Nome";
            testSearchDto.SobreNome = "Sobrenome";
            var searchDto = new SearchRequest <TestSearchDto>();

            searchDto.Search = testSearchDto;

            var entity = new TestEntity(
                new NomeValueObject("Weslley", "Carneiro"),
                new EmailValueObject("*****@*****.**"));

            model.Id = entity.Id;

            var logger = new XunitLogger <
                ApiControllerBase <TestEntity
                                   , TestViewModel
                                   , TestViewModel
                                   , InsertTestViewModel
                                   , UpdateTestViewModel
                                   , TestSearchDto> >();
            var loggerFactoryMock = new Mock <ILoggerFactory>();

            loggerFactoryMock.Setup(setup => setup.CreateLogger(It.IsAny <string>())).Returns(logger);

            Mock <IMapper> mapperMock = new Mock <IMapper>();

            mapperMock.Setup(mapper => mapper.Map <TestViewModel>(It.IsAny <TestEntity>())).Returns(model);
            mapperMock.Setup(mapper => mapper.Map <TestEntity>(It.IsAny <TestViewModel>())).Returns(entity);

            var mockApplicationService = new Mock <ITestServiceApplication>();

            mockApplicationService.Setup(setup => setup.InsertAsync <InsertTestViewModel, InsertTestViewModel>(It.IsAny <InsertTestViewModel>())).ReturnsAsync(insertViewModel);
            mockApplicationService.Setup(setup => setup.UpdateAsync <UpdateTestViewModel, UpdateTestViewModel>(It.IsAny <UpdateTestViewModel>())).ReturnsAsync(updateViewModel);

            var mockResponseFactory = new Mock <IResponseFactory>();

            mockResponseFactory.Setup(setup => setup.Create()).Returns(new Response());
            mockResponseFactory.Setup(setup => setup.Create(It.IsAny <TestViewModel>())).Returns(new Response <TestViewModel>(model, true));
            mockResponseFactory.Setup(setup => setup.Create(It.IsAny <IEnumerable <TestViewModel> >())).Returns(new ResponseList <TestViewModel>(new[] { model }, true));
            mockResponseFactory.Setup(setup => setup.Create(It.IsAny <SearchResult <TestViewModel> >())).Returns(new ResponseSearch <TestViewModel>(new SearchResult <TestViewModel>(1, 10)
            {
                Items = new[] { model }
            }, true));
            mockResponseFactory.Setup(setup => setup.Create(It.IsAny <InsertTestViewModel>())).Returns(new Response <InsertTestViewModel>(insertViewModel, true));
            mockResponseFactory.Setup(setup => setup.Create(It.IsAny <UpdateTestViewModel>())).Returns(new Response <UpdateTestViewModel>(updateViewModel, true));


            var controller = new TestController(loggerFactoryMock.Object, mockApplicationService.Object, mockResponseFactory.Object);

            //When
            await controller.GetAllAsync();

            await controller.GetAllAsync(searchDto);

            await controller.GetByIdAsync(model.Id);

            await controller.InsertAsync(insertViewModel);

            await controller.UpdateAsync(updateViewModel);

            await controller.DeleteAsync(model.Id);

            //Then
            var msgContructor           = "Inicializando Controller Base<TestEntity, Guid>";
            var msgGetById              = $"Método: GetByIdAsync({{ id:{ entity.Id } }})";
            var msgGetAllAsync          = "Método: GetAllAsync() Retorno: IActionResult";
            var msgGetAllPaginatedAsync = $"Método: GetAllAsync({ searchDto.ToJson() }) Retorno: IActionResult";
            var msgInsertAsync          = $"Método: InsertAsync({{ viewModel:{ insertViewModel.ToJson() } }})";
            var msgUpdateAsync          = $"Método: UpdateAsync({{ viewModel:{ updateViewModel.ToJson() } }})";
            var msgDeleteAsync          = $"Método: DeleteAsync({{ id:{ entity.Id } }})";

            logger.Logs.Should().HaveCount(7);
            logger.Logs.Any(a => a.Equals(msgGetById)).Should().BeTrue();
            logger.Logs.Any(a => a.Equals(msgContructor)).Should().BeTrue();
            logger.Logs.Any(a => a.Equals(msgGetAllAsync)).Should().BeTrue();
            logger.Logs.Any(a => a.Equals(msgGetAllPaginatedAsync)).Should().BeTrue();
            logger.Logs.Any(a => a.Equals(msgInsertAsync)).Should().BeTrue();
            logger.Logs.Any(a => a.Equals(msgUpdateAsync)).Should().BeTrue();
            logger.Logs.Any(a => a.Equals(msgDeleteAsync)).Should().BeTrue();
        }