public async Task E_Possivel_Invocar_a_Controller_Update() { var serviceMock = new Mock <ICepService>(); serviceMock.Setup(m => m.Put(It.IsAny <CepDtoUpdate>())).ReturnsAsync( new CepDtoUpdateResult { Id = Guid.NewGuid(), Logradouro = "Teste de Rua", UpdateAt = DateTime.UtcNow } ); _controller = new CepsController(serviceMock.Object); _controller.ModelState.AddModelError("Nome", "É um Campo Obrigatório"); var cepDtoUpdate = new CepDtoUpdate { Logradouro = "Teste de Rua", Cep = "10333444" }; var result = await _controller.Put(cepDtoUpdate); Assert.True(result is BadRequestObjectResult); }
public async Task E_Possivel_Invocar_a_Controller_Create() { var serviceMock = new Mock <ICepService>(); serviceMock.Setup(m => m.Put(It.IsAny <CepDtoUpdate>())).ReturnsAsync( new CepDtoUpdateResult { Id = Guid.NewGuid(), Logradouro = "Teste de Rua", Cep = "10333444", UpdateAt = DateTime.UtcNow } ); _controller = new CepsController(serviceMock.Object); var cepDtoUpdate = new CepDtoUpdate { Logradouro = "Teste de Rua", Cep = "10333444" }; var result = await _controller.Put(cepDtoUpdate); Assert.True(result is OkObjectResult); }
public async Task <ActionResult> Put([FromBody] CepDtoUpdate update) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var result = await _service.Put(update); if (result != null) { return(Ok(result)); } else { return(BadRequest()); } } catch (ArgumentException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }
public async Task <CepDtoUpdateResult> Put(CepDtoUpdate cep) { var model = _mapper.Map <CepModel>(cep); var entity = _mapper.Map <CepEntity>(model); var result = await _repository.UpdateAsync(entity); return(_mapper.Map <CepDtoUpdateResult>(result)); }
public async Task E_Possivel_Invocar_a_Controller_update() { var serviceMock = new Mock <ICepService>(); var Cep = Faker.RandomNumber.Next(1, 10000).ToString(); var Logradouro = Faker.Address.StreetName(); var Numero = Faker.RandomNumber.Next(1, 10000).ToString(); var MunicipioId = Guid.NewGuid(); serviceMock.Setup(c => c.Put(It.IsAny <CepDtoUpdate>())).ReturnsAsync( new CepDtoUpdateResult { Id = Guid.NewGuid(), Cep = Cep, Logradouro = Logradouro, Numero = Numero, MunicipioId = MunicipioId, UpdateAt = DateTime.UtcNow } ); _controller = new CepsController(serviceMock.Object); _controller.ModelState.AddModelError("Name", "É um campo Obrigatório"); 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 cepDtoUpdate = new CepDtoUpdate { Cep = Cep, Logradouro = Logradouro, Numero = Numero, MunicipioId = MunicipioId }; var result = await _controller.Put(cepDtoUpdate); Assert.True(result is BadRequestObjectResult); }
public CepTestes() { IdMunicipio = Guid.NewGuid(); IdCep = Guid.NewGuid(); CepOriginal = Faker.RandomNumber.Next(10000, 99999).ToString(); NumeroOriginal = Faker.RandomNumber.Next(1, 1000).ToString(); LogradouroOriginal = Faker.Address.StreetName(); CepAlterado = Faker.RandomNumber.Next(10000, 99999).ToString(); NumeroAlterado = Faker.RandomNumber.Next(1, 1000).ToString(); LogradouroAlterado = Faker.Address.StreetName(); for (int i = 0; i < 10; i++) { var dto = new CepDto() { Id = Guid.NewGuid(), Cep = Faker.RandomNumber.Next(10000, 99999).ToString(), Logradouro = Faker.Address.StreetName(), Numero = Faker.RandomNumber.Next(1, 1000).ToString(), MunicipioId = Guid.NewGuid(), Municipio = new MunicipioDtoComplete { Id = IdMunicipio, Nome = Faker.Address.City(), CodIbge = Faker.RandomNumber.Next(1, 10000), UfId = Guid.NewGuid(), uf = new UfDto { Id = Guid.NewGuid(), Nome = Faker.Address.UsState(), Sigla = Faker.Address.UsState().Substring(1, 3) } } }; listaDto.Add(dto); } cepDto = new CepDto { Id = IdCep, Cep = CepOriginal, Logradouro = LogradouroOriginal, Numero = NumeroOriginal, MunicipioId = IdMunicipio, Municipio = new MunicipioDtoComplete { Id = IdMunicipio, Nome = Faker.Address.City(), CodIbge = Faker.RandomNumber.Next(1, 10000), UfId = Guid.NewGuid(), uf = new UfDto { Id = Guid.NewGuid(), Nome = Faker.Address.UsState(), Sigla = Faker.Address.UsState().Substring(1, 3) } } }; cepDtoCreate = new CepDtoCreate { Cep = CepOriginal, Logradouro = LogradouroOriginal, Numero = NumeroOriginal, MunicipioId = IdMunicipio, }; cepDtoCreateResult = new CepDtoCreateResult { Id = IdCep, Cep = CepOriginal, Logradouro = LogradouroOriginal, Numero = NumeroOriginal, MunicipioId = IdMunicipio, CreateAt = DateTime.UtcNow }; cepDtoUpdate = new CepDtoUpdate { Id = IdCep, Cep = CepAlterado, Logradouro = LogradouroAlterado, Numero = NumeroAlterado, MunicipioId = IdMunicipio }; cepDtoUpdateResult = new CepDtoUpdateResult { Id = IdMunicipio, Cep = CepAlterado, Logradouro = LogradouroAlterado, Numero = NumeroAlterado, MunicipioId = IdMunicipio, UpdateAt = DateTime.UtcNow }; }
public async Task E_Possivel_Realizar_Crud_CEP() { await AdicionarToken(); var municipioDto = new MunicipioDtoCreate() { Nome = "São Paulo", CodIBGE = 3550308, UfId = new Guid("e7e416de-477c-4fa3-a541-b5af5f35ccf6") }; //Post var response = await PostJsonAsync(municipioDto, $"{hostApi}municipios", client); var postResult = await response.Content.ReadAsStringAsync(); var registroPost = JsonConvert.DeserializeObject <MunicipioDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("São Paulo", registroPost.Nome); Assert.Equal(3550308, registroPost.CodIBGE); Assert.True(registroPost.Id != default(Guid)); var cepDto = new CepDtoCreate() { Cep = "13480180", Logradouro = "Rua Boa Morte", Numero = "até nº 200", MunicipioId = registroPost.Id }; //Post response = await PostJsonAsync(cepDto, $"{hostApi}ceps", client); postResult = await response.Content.ReadAsStringAsync(); var registroCepPost = JsonConvert.DeserializeObject <CepDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(cepDto.Cep, registroCepPost.Cep); Assert.Equal(cepDto.Logradouro, registroCepPost.Logradouro); Assert.Equal(cepDto.Numero, registroCepPost.Numero); Assert.True(registroCepPost.Id != default(Guid)); var cepMunicipioDto = new CepDtoUpdate() { Id = registroCepPost.Id, Cep = "13480180", Logradouro = "Rua da Liberdade", Numero = "até nº 200", MunicipioId = registroPost.Id }; //PUT var stringContent = new StringContent(JsonConvert.SerializeObject(cepMunicipioDto), Encoding.UTF8, "application/json"); response = await client.PutAsync($"{hostApi}ceps", stringContent); var jsonResult = await response.Content.ReadAsStringAsync(); var registroAtualizado = JsonConvert.DeserializeObject <CepDtoUpdateResult>(jsonResult); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(cepMunicipioDto.Logradouro, registroAtualizado.Logradouro); //GET Id response = await client.GetAsync($"{hostApi}ceps/{registroAtualizado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); var registroSelecionado = JsonConvert.DeserializeObject <CepDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(cepMunicipioDto.Logradouro, registroSelecionado.Logradouro); //GET Cep response = await client.GetAsync($"{hostApi}ceps/byCep/{registroAtualizado.Cep}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); registroSelecionado = JsonConvert.DeserializeObject <CepDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(cepMunicipioDto.Logradouro, registroSelecionado.Logradouro); //DELETE response = await client.DeleteAsync($"{hostApi}ceps/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); //GET ID depois do DELETE response = await client.GetAsync($"{hostApi}ceps/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }
public async Task RequestCepTest() { await AdicionarToken(); var municipioDto = new MunicipioDtoCreate() { Nome = "Sinop", CodIbge = 3550308, UfId = new Guid("22ffbd18-cdb9-45cc-97b0-51e97700bf71") }; //Post var response = await PostJsonAsync(municipioDto, $"{hostApi}municipios", client); var postResult = await response.Content.ReadAsStringAsync(); var registroPost = JsonConvert.DeserializeObject <MunicipioDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("Sinop", registroPost.Nome); Assert.Equal(3550308, registroPost.CodIbge); Assert.True(registroPost.Id != default(Guid)); var cepDto = new CepDtoCreate() { Cep = "13480180", Logradouro = "Rua Das Samambaias", Numero = "até nº 200", MunicipioId = registroPost.Id }; //Post response = await PostJsonAsync(cepDto, $"{hostApi}ceps", client); postResult = await response.Content.ReadAsStringAsync(); var registroCepPost = JsonConvert.DeserializeObject <CepDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(cepDto.Cep, registroCepPost.Cep); Assert.Equal(cepDto.Logradouro, registroCepPost.Logradouro); Assert.Equal(cepDto.Numero, registroCepPost.Numero); Assert.True(registroCepPost.Id != default(Guid)); var cepMunicipioDto = new CepDtoUpdate() { Id = registroCepPost.Id, Cep = "13480180", Logradouro = "Rua das Samambaias", Numero = "até nº 200", MunicipioId = registroPost.Id }; //PUT var stringContent = new StringContent(JsonConvert.SerializeObject(cepMunicipioDto), Encoding.UTF8, "application/json"); response = await client.PutAsync($"{hostApi}ceps", stringContent); var jsonResult = await response.Content.ReadAsStringAsync(); var registroAtualizado = JsonConvert.DeserializeObject <CepDtoUpdateResult>(jsonResult); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(cepMunicipioDto.Logradouro, registroAtualizado.Logradouro); //GET Id response = await client.GetAsync($"{hostApi}ceps/{registroAtualizado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); var registroSelecionado = JsonConvert.DeserializeObject <CepDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(cepMunicipioDto.Logradouro, registroSelecionado.Logradouro); //GET Cep response = await client.GetAsync($"{hostApi}ceps/byCep/{registroAtualizado.Cep}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); registroSelecionado = JsonConvert.DeserializeObject <CepDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(cepMunicipioDto.Logradouro, registroSelecionado.Logradouro); //DELETE response = await client.DeleteAsync($"{hostApi}ceps/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); //GET ID depois do DELETE response = await client.GetAsync($"{hostApi}ceps/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }
public async Task E_Possivel_Realizar_Crud_Cep() { await AdicionarToken(); var municipioDto = new MunicipioDtoCreate { Nome = "Guaíba", CodIBGE = 123456, UfId = new Guid("88970a32-3a2a-4a95-8a18-2087b65f59d1") }; //Insere Municipio pra usar o ID Gerado no cadastro do CEP var response = await PostJsonAsync(municipioDto, $"{hostApi}Municipios", client); var postResult = await response.Content.ReadAsStringAsync(); var registroPostMunicipio = JsonConvert.DeserializeObject <MunicipioDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("Guaíba", registroPostMunicipio.Nome); Assert.Equal(123456, registroPostMunicipio.CodIBGE); Assert.True(registroPostMunicipio.Id != default(Guid)); var cepDto = new CepDtoCreate { Cep = "92714-630", Logradouro = "Rua Leopoldo Rassier", Numero = "242", MunicipioId = registroPostMunicipio.Id }; //Post response = await PostJsonAsync(cepDto, $"{hostApi}Ceps", client); postResult = await response.Content.ReadAsStringAsync(); var registroPost = JsonConvert.DeserializeObject <CepDtoCreateResult>(postResult); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("92714-630", registroPost.Cep); Assert.Equal("Rua Leopoldo Rassier", registroPost.Logradouro); Assert.True(registroPost.Id != default(Guid)); //Put var cepDtoAtualizado = new CepDtoUpdate { Id = registroPost.Id, Cep = "92714-630", Logradouro = "Rua Leopoldo Rassier (casa de esquina)", Numero = "242", MunicipioId = registroPostMunicipio.Id }; var stringContent = new StringContent(JsonConvert.SerializeObject(cepDtoAtualizado), Encoding.UTF8, "application/json"); response = await client.PutAsync($"{hostApi}Ceps", stringContent); var jsonResult = await response.Content.ReadAsStringAsync(); var registroAtualizado = JsonConvert.DeserializeObject <CepDtoUpdateResult>(jsonResult); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("Rua Leopoldo Rassier (casa de esquina)", registroAtualizado.Logradouro); Assert.Equal(registroPost.Id, registroAtualizado.Id); //Get por ID response = await client.GetAsync($"{hostApi}Ceps/{registroAtualizado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); var registroSelecionado = JsonConvert.DeserializeObject <CepDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(registroSelecionado.Id, registroAtualizado.Id); Assert.Equal(registroSelecionado.Cep, registroAtualizado.Cep); Assert.Equal(registroSelecionado.Logradouro, registroAtualizado.Logradouro); //Get por Cep response = await client.GetAsync($"{hostApi}Ceps/byCep/{registroAtualizado.Cep}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); jsonResult = await response.Content.ReadAsStringAsync(); registroSelecionado = JsonConvert.DeserializeObject <CepDto>(jsonResult); Assert.NotNull(registroSelecionado); Assert.Equal(registroSelecionado.Cep, registroAtualizado.Cep); Assert.Equal(registroSelecionado.Logradouro, registroAtualizado.Logradouro); //Delete response = await client.DeleteAsync($"{hostApi}Ceps/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); //Get ID depois do Delete response = await client.GetAsync($"{hostApi}Ceps/{registroSelecionado.Id}"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }