Exemple #1
0
        public async Task PostBadRequest()
        {
            var serviceMock = new Mock <IContaService>();

            serviceMock.Setup(m => m.Post(It.IsAny <ContaDtoCreate>())).ReturnsAsync(
                new ContaDtoCreateResult
            {
                Id          = Guid.NewGuid(),
                Name        = "PV Conta",
                Description = "Conta Corrente",
                Balance     = 0,
                Status      = true,
                CreateAt    = DateTime.Now
            }
                );

            _controller = new ContasController(serviceMock.Object);
            _controller.ModelState.AddModelError("Id", "Formato Inválido");

            Mock <IUrlHelper> url = new Mock <IUrlHelper>();

            url.Setup(x => x.Link(It.IsAny <string>(), It.IsAny <object>())).Returns("http://localhost:5000");
            _controller.Url = url.Object;

            var contaDtoCreate = new ContaDtoCreate
            {
                Name        = "PV Conta",
                Description = "Conta Corrente",
                Status      = true
            };

            var result = await _controller.Post(contaDtoCreate);

            Assert.True(result is BadRequestObjectResult);
        }
Exemple #2
0
        public async Task <ContaDtoCreateResult> Post(ContaDtoCreate conta)
        {
            var model  = _mapper.Map <ContaModel>(conta);
            var entity = _mapper.Map <ContaEntity>(model);
            var result = await _repository.InsertAsync(entity);

            return(_mapper.Map <ContaDtoCreateResult>(result));
        }
        public async Task TestIntegrationLancamento()
        {
            var contaDtoCreate = new ContaDtoCreate()
            {
                Name        = Faker.Name.FullName(),
                Description = "Conta Corrente",
                Status      = true
            };

            //Post
            var response = await PostJsonAsync(contaDtoCreate, $"{hostApi}contas", client);

            var postResult = await response.Content.ReadAsStringAsync();

            var registroContaPost = JsonConvert.DeserializeObject <ContaDtoCreateResult>(postResult);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.Equal(contaDtoCreate.Name, registroContaPost.Name);
            Assert.Equal(contaDtoCreate.Description, registroContaPost.Description);
            Assert.Equal(contaDtoCreate.Status, registroContaPost.Status);
            Assert.True(registroContaPost.Id != default(Guid));

            var LancamentoDtoCreate = new LancamentoDtoCreate()
            {
                ContaId     = registroContaPost.Id,
                Description = "Lancamento de crédito",
                Value       = Faker.RandomNumber.Next(1, 1000)
            };

            //Post
            response = await PostJsonAsync(LancamentoDtoCreate, $"{hostApi}lancamentos", client);

            postResult = await response.Content.ReadAsStringAsync();

            var registroLancamentoPost = JsonConvert.DeserializeObject <LancamentoDtoCreateResult>(postResult);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.Equal(LancamentoDtoCreate.ContaId, registroLancamentoPost.ContaId);
            Assert.Equal(LancamentoDtoCreate.Description, registroLancamentoPost.Description);
            Assert.Equal(LancamentoDtoCreate.Value, registroLancamentoPost.Value);
            Assert.True(registroLancamentoPost.Id != default(Guid));



            //GET Id
            response = await client.GetAsync($"{hostApi}lancamentos/{registroLancamentoPost.Id}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var jsonResult = await response.Content.ReadAsStringAsync();

            var registroSelecionado = JsonConvert.DeserializeObject <LancamentoDto>(jsonResult);

            Assert.NotNull(registroSelecionado);
            Assert.Equal(registroLancamentoPost.Id, registroSelecionado.Id);
        }
        public async Task <ActionResult> Post([FromBody] ContaDtoCreate conta)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState)); // 400 bad request
                }

                var result = await _service.Post(conta);

                if (result != null)
                {
                    return(Created(new Uri(Url.Link("GetContaWithId", new { id = result.Id })), result));
                }

                return(BadRequest());
            }
            catch (ArgumentException ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Exemple #5
0
        public ContaTestes()
        {
            Id          = Guid.NewGuid();
            Name        = Faker.Name.FullName();
            Description = "Conta Corrente";
            Balance     = 0;
            Status      = true;

            for (int i = 0; i < 10; i++)
            {
                var dto = new ContaDto()
                {
                    Id          = Guid.NewGuid(),
                    Name        = Faker.Name.FullName(),
                    Description = "Conta Corrente",
                    Balance     = 0,
                    Status      = true
                };
                listaContaDto.Add(dto);
            }

            contaDto = new ContaDto
            {
                Id          = Id,
                Name        = Name,
                Description = Description,
                Balance     = Balance,
                Status      = Status
            };

            contaDtoCreate = new ContaDtoCreate
            {
                Name        = Name,
                Description = Description,
                Status      = Status
            };

            contaDtoCreateResult = new ContaDtoCreateResult
            {
                Id          = Id,
                Name        = Name,
                Description = Description,
                Status      = Status,
                Balance     = Balance,
                CreateAt    = DateTime.UtcNow
            };

            contaDtoUpdate = new ContaDtoUpdate
            {
                Id          = Id,
                Name        = Name,
                Description = Description,
                Status      = Status,
            };

            contaDtoUpdateResult = new ContaDtoUpdateResult
            {
                Id          = Id,
                Name        = Name,
                Description = Description,
                Status      = Status,
                UpdateAt    = DateTime.UtcNow
            };
        }
        public async Task TestIntegrationConta()
        {
            var contaDtoCreate = new ContaDtoCreate()
            {
                Name        = Faker.Name.FullName(),
                Description = "Conta Corrente",
                Status      = true
            };

            //Post
            var response = await PostJsonAsync(contaDtoCreate, $"{hostApi}contas", client);

            var postResult = await response.Content.ReadAsStringAsync();

            var registroContaPost = JsonConvert.DeserializeObject <ContaDtoCreateResult>(postResult);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.Equal(contaDtoCreate.Name, registroContaPost.Name);
            Assert.Equal(contaDtoCreate.Description, registroContaPost.Description);
            Assert.Equal(contaDtoCreate.Status, registroContaPost.Status);
            Assert.True(registroContaPost.Id != default(Guid));

            //Get All
            response = await client.GetAsync($"{hostApi}contas");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            var jsonResult = await response.Content.ReadAsStringAsync();

            var listaFromJson = JsonConvert.DeserializeObject <IEnumerable <ContaDto> >(jsonResult);

            Assert.NotNull(listaFromJson);
            Assert.True(listaFromJson.Count() > 0);
            Assert.True(listaFromJson.Where(r => r.Id == registroContaPost.Id).Count() == 1);

            var contaDtoUpdate = new ContaDtoUpdate()
            {
                Id          = registroContaPost.Id,
                Name        = Faker.Name.FullName(),
                Description = "Conta Corrente",
                Status      = true
            };

            //PUT
            var stringContent = new StringContent(JsonConvert.SerializeObject(contaDtoUpdate),
                                                  Encoding.UTF8, "application/json");

            response = await client.PutAsync($"{hostApi}contas", stringContent);

            jsonResult = await response.Content.ReadAsStringAsync();

            var registroAtualizado = JsonConvert.DeserializeObject <ContaDtoUpdateResult>(jsonResult);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(contaDtoUpdate.Name, registroAtualizado.Name);

            //GET Id
            response = await client.GetAsync($"{hostApi}contas/{registroAtualizado.Id}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            jsonResult = await response.Content.ReadAsStringAsync();

            var registroSelecionado = JsonConvert.DeserializeObject <ContaDto>(jsonResult);

            Assert.NotNull(registroSelecionado);
            Assert.Equal(contaDtoUpdate.Id, registroSelecionado.Id);

            //DELETE
            response = await client.DeleteAsync($"{hostApi}contas/{registroSelecionado.Id}");

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            //GET ID depois do DELETE
            response = await client.GetAsync($"{hostApi}contas/{registroSelecionado.Id}");

            Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
        }