public async Task Estourar_Excesao_VerificarSeEmailJaExiste_Update() { var idAluno = 1; var alunoRequest = new AlunoRequestModel() { NomeAluno = "Mario", Email = "*****@*****.**", CPF = "099.536.159-26", Telefone = "47991085945", Id = idAluno }; var aluno = new AlunoBuilderTest() .ComId(idAluno) .ComNomeAluno("Arthur") .ComEmail("*****@*****.**") .ComCpf("099.536.159-26") .ComTelefone("47991085945") .Construir(); _alunoRepository.GetById(idAluno).Returns(aluno); _alunoRepository.VerificarSeEmailJaExiste(aluno.Email, aluno.Id).Returns(true); var ex = await Record.ExceptionAsync(() => _alunoService.Update(idAluno, alunoRequest)); ex.Should().BeOfType <ArgumentException>(); ex.Message.Should().Be("Email já existente."); }
public async Task Estourar_Excecao_IdNulo_Update() { var idAluno = 1; var alunoRequest = new AlunoRequestModel() { NomeAluno = "Mario", Email = "*****@*****.**", CPF = "099.536.159-26", Telefone = "47991085945", Id = idAluno }; var aluno = new AlunoBuilderTest() .ComId(idAluno) .ComNomeAluno("Arthur") .ComEmail("*****@*****.**") .ComCpf("099.536.159-26") .ComTelefone("47991085945") .Construir(); var ex = await Record.ExceptionAsync(() => _alunoService.Update(idAluno, alunoRequest)); ex.Should().BeOfType <ArgumentException>(); ex.Message.Should().Be("Id do aluno inválido."); }
public void Validar_Telefone_Caracteres(int?qtdCaracteres) { string telefone = ""; if (qtdCaracteres != null) { telefone = new string('t', Convert.ToInt32(qtdCaracteres)); } var aluno = new AlunoRequestModel() { NomeAluno = "Mario", Id = 0, CPF = "099.536.159-26", Email = "*****@*****.**", Telefone = telefone }; var ex = Assert.ThrowsAsync <ValidationException>(async() => await _alunoService.Create(aluno)); if (qtdCaracteres == null) { ex.Result.Message.Should().Contain("Numero de telefone está nulo."); } else if (qtdCaracteres == 9) { ex.Result.Message.Should().Contain("Telefone deve conter exatamente 10 caracteres."); } else if (qtdCaracteres == 12) { ex.Result.Message.Should().Contain("Telefone deve conter exatamente 11 caracteres."); } }
public async Task Atualizar_Update() { var idAluno = 1; var alunoRequest = new AlunoRequestModel() { NomeAluno = "Mario", Email = "*****@*****.**", CPF = "099.536.159-26", Telefone = "47991085945", Id = idAluno }; var aluno = new AlunoBuilderTest() .ComId(idAluno) .ComNomeAluno("Arthur") .ComEmail("*****@*****.**") .ComCpf("099.536.159-26") .ComTelefone("47991085945") .Construir(); _alunoRepository.GetById(idAluno).Returns(aluno); _alunoRepository.VerificarSeEmailJaExiste(aluno.Email, aluno.Id).Returns(false); var alunoRetornado = await _alunoService.Update(idAluno, alunoRequest); await _alunoRepository.Received(1).Update(Arg.Is <AlunoEntity>(args => args.NomeAluno == alunoRequest.NomeAluno && args.Email == alunoRequest.Email && args.Id == alunoRequest.Id && args.CPF == alunoRequest.CPF && args.Telefone == alunoRequest.Telefone )); }
public void Validar_CPF_Caracteres(string cpfTeste) { var cpf = ""; if (cpfTeste != "x") { cpf = cpfTeste; } var aluno = new AlunoRequestModel() { NomeAluno = "Mario", Id = 0, CPF = cpf, Email = "*****@*****.**", Telefone = "47991085345" }; var ex = Assert.ThrowsAsync <ValidationException>(async() => await _alunoService.Create(aluno)); if (cpf == "x") { ex.Result.Message.Should().Contain("CPF não pode estar vazio."); } else if (cpf == "00000/0001-17") { ex.Result.Message.Should().Contain("CPF incorreto."); } }
public void Validar_NomeAluno_Caracteres(int?qtdCaracteres) { string nomeAluno = ""; if (qtdCaracteres != null) { nomeAluno = new string('a', Convert.ToInt32(qtdCaracteres)); } var aluno = new AlunoRequestModel() { NomeAluno = nomeAluno, Id = 0, CPF = "099.536.159-26", Email = "*****@*****.**", Telefone = "47991085345" }; var ex = Assert.ThrowsAsync <ValidationException>(async() => await _alunoService.Create(aluno)); if (qtdCaracteres == null) { ex.Result.Message.Should().Contain("Nome de aluno está nulo."); } else if (qtdCaracteres == 51) { ex.Result.Message.Should().Contain("Nome do aluno deve conter no máximo 50 caracteres."); } else if (qtdCaracteres == 2) { ex.Result.Message.Should().Contain("Nome do aluno precisa no minimo 3 caracteres."); } }
public void Validar_Email_Caracteres(string emailTeste) { string email = ""; if (emailTeste != "x") { email = emailTeste; } var aluno = new AlunoRequestModel() { NomeAluno = "Mario", Id = 0, CPF = "099.536.159-26", Email = email, Telefone = "47991085945" }; var ex = Assert.ThrowsAsync <ValidationException>(async() => await _alunoService.Create(aluno)); if (emailTeste == "x") { ex.Result.Message.Should().Contain("Email não pode ser nulo."); } else if (emailTeste == "mabelksouza.com") { ex.Result.Message.Should().Contain("Email incorreto. EX:[email protected]."); } }
public async Task Salvar_Create() { var alunoRequest = new AlunoRequestModel() { NomeAluno = "Mario", Email = "*****@*****.**", CPF = "099.536.159-26", Telefone = "47991085945", Id = 1 }; await _alunoService.Create(alunoRequest); await _alunoRepository.Received(1).Create(Arg.Any <AlunoEntity>()); }
public async Task <int> Create(AlunoRequestModel request) { var aluno = new AlunoEntity(request.NomeAluno, request.Email, request.CPF, request.Telefone); var alunoJaExiste = await _repository.VerificarSeJaExisteAluno(aluno.CPF); if (alunoJaExiste) { throw new ArgumentException("Aluno já existe."); } aluno.Validate(); await _repository.Create(aluno); return(aluno.Id); }
public async Task Estourar_Excecao_GetById() { var idAluno = 1; var alunoRequest = new AlunoRequestModel() { NomeAluno = "Mario", Email = "*****@*****.**", CPF = "099.536.159-26", Telefone = "47991085945", Id = idAluno }; var ex = await Record.ExceptionAsync(() => _alunoService.GetById(idAluno)); ex.Should().BeOfType <ArgumentException>(); ex.Message.Should().Be("Id de aluno inexistente."); }
public async Task Estourar_Excecao_VerificarSeJaExisteAluno_Create() { var alunoRequest = new AlunoRequestModel() { NomeAluno = "Mario", Email = "*****@*****.**", CPF = "099.536.159-26", Telefone = "47991085945", Id = 1 }; _alunoRepository.VerificarSeJaExisteAluno(alunoRequest.CPF).Returns(true); var ex = await Record.ExceptionAsync(() => _alunoService.Create(alunoRequest)); ex.Should().BeOfType <ArgumentException>(); ex.Message.Should().Be("Aluno já existe."); }
public async Task <IActionResult> Update([FromRoute] int id, [FromBody] AlunoRequestModel model) { try { await _alunoService.Update(id, model); return(Ok("Aluno atualizado com sucesso.")); } catch (AlunoException ex) { return(BadRequest(ex.Errors)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public async Task <IActionResult> Create([FromBody] AlunoRequestModel alunoRequest) { try { int id = await _alunoService.Create(alunoRequest); return(CreatedAtRoute(alunoRequest, id)); } catch (AlunoException ex) { return(BadRequest(ex.Errors)); } catch (DbUpdateException ex) { return(StatusCode(500, ex.Message)); } catch (Exception ex) { return(StatusCode(400, ex.Message)); } }
public async Task <AlunoEntity> Update(int id, AlunoRequestModel request) { var aluno = await _repository.GetById(id); if (aluno == null) { throw new ArgumentException("Id do aluno inválido."); } aluno.Update(request.NomeAluno, request.Email, request.CPF, request.Telefone); aluno.Validate(); var emailJaExiste = await _repository.VerificarSeEmailJaExiste(aluno.Email, aluno.Id); if (emailJaExiste) { throw new ArgumentException("Email já existente."); } await _repository.Update(aluno); return(aluno); }