Example #1
0
        public ActionResult <Envelope> Get(int id)
        {
            Envelope envelope = new Envelope();

            try
            {
                // carregar e retornar um contato
                BusContato bContato = new BusContato();

                // retorno da API
                envelope.Colecao.Add(bContato.Carregar(id));
            }
            catch (Exception e)
            {
                envelope.AddExcecao("Erro ao CARREGAR contato", e);
            }
            return(Ok(envelope));
        }
Example #2
0
        public ActionResult <IActionResult> Get()
        {
            Envelope envelope = new Envelope();

            try
            {
                // retornar a lista de contatos
                BusContato bContato = new BusContato();

                // retorno da API
                envelope.Colecao = bContato.Listar().ToList <object>();
            }
            catch (Exception e)
            {
                envelope.AddExcecao("Erro ao CARREGAR contato.", e);
            }
            return(Ok(envelope));
        }
Example #3
0
        public void IdadeAbaixoMinima_Excecao()
        {
            // novo contato com o mesmo nome que já existe antes.
            Contato oContatoMOQ = new Contato
            {
                Nome           = "Fabio Mahfoud Cerdeira " + DateTime.Now.Ticks.ToString(),
                Sexo           = "M",
                DataNascimento = DateTime.Now
            };

            /*
             * usando ExpectedException para falhar ou passar no teste:
             */

            // tentar carregar
            BusContato bContato = new BusContato();

            bContato.Salvar(oContatoMOQ);
        }
Example #4
0
        public ActionResult <IActionResult> Delete(int id)
        {
            Envelope envelope = new Envelope();

            try
            {
                // apagar
                BusContato bContato = new BusContato();
                bContato.Apagar(id);

                // retorno da API
                envelope.Infos.Add("Sucesso ao APAGAR contato");
            }
            catch (Exception e)
            {
                envelope.AddExcecao("Erro ao APAGAR contato", e);
            }
            return(Ok(envelope));
        }
Example #5
0
        public void SalvarComMesmoNome()
        {
            // novo contato com o mesmo nome que já existe antes.
            Contato oContatoMOQ = new Contato
            {
                // Nome = "Fabio Mahfoud Cerdeira " + DateTime.Now.Ticks.ToString(), // nesse nome NÃO da FALHA no teste.
                Nome           = "Fabio Mahfoud Cerdeira",
                Sexo           = "M",
                DataNascimento = new System.DateTime(1977, 4, 13)
            };

            /*
             * usando ExpectedException para falhar ou passar no teste:
             */

            // tentar carregar
            BusContato bContato = new BusContato();

            bContato.Salvar(oContatoMOQ);
        }
Example #6
0
        public void CriarUm()
        {
            Contato oContatoMOQ = new Contato
            {
                Nome           = "Teste Criar Um " + DateTime.Now.Ticks.ToString(),
                Sexo           = "M",
                DataNascimento = DateTime.Now.AddYears(-6)
            };

            try
            {
                BusContato bContato = new BusContato();
                bContato.Salvar(oContatoMOQ);

                // recuperar o id do novo registro no banco.
                int novoId = oContatoMOQ.IdContato;

                // carregar do banco o registro recem criado.
                Contato oContato = bContato.Carregar(novoId);

                // comparação do objeto com o outro objeto, em todas as suas propriedades.
                Assert.AreEqual(oContatoMOQ, oContato);

                /*
                 * // comparar por propriedade.
                 * Assert.AreEqual(oContatoMOQ.IdContato, oContato.IdContato);
                 * Assert.AreEqual(oContatoMOQ.Nome, oContato.Nome);
                 * Assert.AreEqual(oContatoMOQ.Sexo, oContato.Sexo);
                 * Assert.AreEqual(oContatoMOQ.Idade, oContato.Idade);
                 */
            }
            catch (MyException e)
            {
                Assert.Fail(e.Infos[0]);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
Example #7
0
        public ActionResult <IActionResult> Post([FromBody] Contato value)
        {
            Envelope envelope = new Envelope();

            try
            {
                // criar ou editar (salvar)
                BusContato bContato = new BusContato();
                bContato.Salvar(value);

                // retorno da API
                envelope.Infos.Add("Sucesso ao GRAVAR contato");

                // retorno o mesmo objeto para cliente poder ter campos atualizados (pk e idade);
                envelope.Colecao.Add(value);
            }
            catch (Exception e)
            {
                envelope.AddExcecao("Erro ao GRAVAR contato", e);
            }
            return(Ok(envelope));
        }
Example #8
0
        public void CriarUmApagarOmesmo()
        {
            Contato oContatoMOQ = new Contato
            {
                Nome           = "Teste Criar Apagar " + DateTime.Now.Ticks.ToString(),
                Sexo           = "X",
                DataNascimento = DateTime.Now.AddYears(-6)
            };

            BusContato bContato = new BusContato();

            bContato.Salvar(oContatoMOQ);

            // recuperar o id do novo registro no banco.
            int novoId = oContatoMOQ.IdContato;

            // apagar
            bContato = new BusContato();
            bContato.Apagar(novoId);

            /*
             * usando TRY CATCH para falhar ou passar no teste:
             */

            try
            {
                // tentar carregar
                Contato contato = bContato.Carregar(novoId);
                if (contato != null)
                {
                    Assert.Fail("Objeto não foi apagado");
                }
            }
            catch (InvalidOperationException)
            {
                Assert.IsTrue(true);
            }
        }
Example #9
0
        public void CarregarUm()
        {
            Contato oContatoMOQ = new Contato
            {
                IdContato      = 1,
                Nome           = "Fabio Mahfoud Cerdeira",
                Sexo           = "M",
                DataNascimento = new System.DateTime(1977, 4, 13)
            };


            BusContato bContato = new BusContato();
            Contato    oContato = bContato.Carregar(1);

            // como o AreEqual não compara objetos fiz a comparação por propriedades.
            // se eu tivesse mais tempo faria um metodo usando a interface IComparable
            // Assert.AreEqual(oContatoMOQ, oContato);

            Assert.AreEqual(oContatoMOQ.IdContato, oContato.IdContato);
            Assert.AreEqual(oContatoMOQ.Nome, oContato.Nome);
            Assert.AreEqual(oContatoMOQ.Sexo, oContato.Sexo);
            Assert.AreEqual(oContatoMOQ.Idade, oContato.Idade);
        }