From c04295acd9f5262804130bf49c62eb3562ca7a21 Mon Sep 17 00:00:00 2001 From: "re.vega11" Date: Wed, 17 May 2017 12:19:09 -0500 Subject: [PATCH] Entrega 5 ciclo 3 --- .../paseos/test/logic/OfertaLogicTest.java | 235 ++++++++++++++++++ .../paseos/test/logic/UsuarioLogicTest.java | 18 +- .../persistence/OfertasPersistenceTest.java | 2 - .../csw/paseos/resources/UsuarioResource.java | 14 +- .../csw/paseos/resources/VisitaResource.java | 4 +- .../ofertasTest.postman_collection.json | 122 +++++++++ 6 files changed, 375 insertions(+), 20 deletions(-) create mode 100644 paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/OfertaLogicTest.java create mode 100644 paseos-01-web/src/test/resources/postmanTests/ofertasTest.postman_collection.json diff --git a/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/OfertaLogicTest.java b/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/OfertaLogicTest.java new file mode 100644 index 0000000..f48d498 --- /dev/null +++ b/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/OfertaLogicTest.java @@ -0,0 +1,235 @@ +package co.edu.uniandes.csw.paseos.test.logic; + +import co.edu.uniandes.csw.paseos.ejbs.OfertaLogic; +import co.edu.uniandes.csw.paseos.entities.OfertaEntity; +import co.edu.uniandes.csw.paseos.exceptions.BusinessLogicException; +import co.edu.uniandes.csw.paseos.persistence.OfertaPersistence; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.transaction.UserTransaction; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import uk.co.jemos.podam.api.PodamFactory; +import uk.co.jemos.podam.api.PodamFactoryImpl; + + +@RunWith(Arquillian.class) +public class OfertaLogicTest{ + public static final String DEPLOY = "PruebaOfertaLogic"; + + @Deployment + public static WebArchive createDeployment() { + return ShrinkWrap.create(WebArchive.class, DEPLOY + ".war") + .addPackage(OfertaEntity.class.getPackage()) + .addPackage(OfertaPersistence.class.getPackage()) + .addPackage(OfertaLogic.class.getPackage()) + .addAsResource("META-INF/persistence.xml", "META-INF/persistence.xml") + .addAsWebInfResource("META-INF/beans.xml", "beans.xml"); + } + + + + @Inject + private OfertaLogic ofertaLogic; + + @PersistenceContext(unitName = "paseosPU") + private EntityManager em; + + @Inject + UserTransaction utx; + + /** + * Configuración inicial de la prueba. + * + * @generated + */ + @Before + public void configTest() { + try { + utx.begin(); + clearData(); + insertData(); + utx.commit(); + } catch (Exception e) { + e.printStackTrace(); + try { + utx.rollback(); + } catch (Exception e1) { + e1.printStackTrace(); + } + } + } + + /** + * Limpia las tablas que están implicadas en la prueba. + * + * @generated + */ + private void clearData() { + em.createQuery("delete from OfertaEntity").executeUpdate(); + } + + /** + * @generated + */ + private List data = new ArrayList(); + + /** + * Inserta los datos iniciales para el correcto funcionamiento de las + * pruebas. + * + * @generated + */ + private void insertData() { + for (int i = 0; i < 3; i++) { + PodamFactory factory = new PodamFactoryImpl(); + OfertaEntity entity = factory.manufacturePojo(OfertaEntity.class); + + em.persist(entity); + data.add(entity); + } + } + + /** + * Prueba para crear una oferta + */ + @Test + public void createOfertaTest() throws BusinessLogicException { + PodamFactory factory = new PodamFactoryImpl(); + OfertaEntity entity = factory.manufacturePojo(OfertaEntity.class); + OfertaEntity result =null; + entity.setFecha(agregaDias(hoy,5)); + result = ofertaLogic.createOferta(entity); + + Assert.assertNotNull(result); + Assert.assertEquals(result.getFecha().getDay(), entity.getFecha().getDay()); + Assert.assertEquals(result.getFecha().getMonth(), entity.getFecha().getMonth()); + Assert.assertEquals(result.getFecha().getYear(), entity.getFecha().getYear()); + Assert.assertEquals(result.getGuia(), entity.getGuia()); + Assert.assertEquals(result.getPaseo(), entity.getPaseo()); + } + /** + * Prueba para consultar una Oferta + */ + @Test + public void getOfertaTest() { + OfertaEntity entity = data.get(0); + OfertaEntity resultEntity= null ; + try { + resultEntity = ofertaLogic.getOferta(entity.getId()); + } catch (BusinessLogicException ex) { + Assert.fail("No deberia generar excepción"); + } + Assert.assertEquals(resultEntity.getFecha().getDay(), entity.getFecha().getDay()); + Assert.assertEquals(resultEntity.getFecha().getMonth(), entity.getFecha().getMonth()); + Assert.assertEquals(resultEntity.getFecha().getYear(), entity.getFecha().getYear()); + Assert.assertEquals(resultEntity.getGuia(), entity.getGuia()); + Assert.assertEquals(resultEntity.getPaseo(), entity.getPaseo()); + } + /** + * Prueba encontrar un usuario que no existe + */ + @Test + public void getOfertaTest1() { + OfertaEntity entity = data.get(0); + OfertaEntity resultEntity= null ; + try { + resultEntity = ofertaLogic.getOferta(100L); + } catch (BusinessLogicException ex) { + Assert.assertEquals(1, 1); + } + + } + + /** + * Prueba para consultar la lista de ofertas + */ + @Test + public void getOfertasTest() { + List list = ofertaLogic.getOfertas(); + Assert.assertEquals(data.size(), list.size()); + for (OfertaEntity entity : list) { + boolean found = false; + for (OfertaEntity storedEntity : data) { + if (entity.getId().equals(storedEntity.getId())) { + found = true; + } + } + Assert.assertTrue(found); + } + } + + /** + * Prueba para eliminar una oferta + */ + @Test + public void deleteOfertaTest() { + OfertaEntity entity = data.get(0); + entity.setFecha(agregaDias(hoy,5)); + em.persist(entity); + try { + ofertaLogic.deleteOferta(entity.getId()); + } catch (BusinessLogicException ex) { + Assert.fail("No deberia generar excepción"); + } + OfertaEntity deleted = em.find(OfertaEntity.class, entity.getId()); + Assert.assertNull(deleted); + } + + /** + * Prueba eliminar un usuario que no existe + */ + @Test + public void deleteOfertaTest1() { + OfertaEntity entity = data.get(0); + try { + ofertaLogic.deleteOferta(100L); + } catch (BusinessLogicException ex) { + Assert.assertEquals(1, 1); + } + + } + + /** + * Prueba para actualizar una oferta + */ + @Test + public void updateOfertaTest() throws BusinessLogicException { + OfertaEntity entity = data.get(0); + PodamFactory factory = new PodamFactoryImpl(); + OfertaEntity pojoEntity = factory.manufacturePojo(OfertaEntity.class); + pojoEntity.setId(entity.getId()); + pojoEntity.setFecha(agregaDias(hoy,5)); + ofertaLogic.updateOferta(pojoEntity); + + OfertaEntity resp = em.find(OfertaEntity.class, entity.getId()); + + Assert.assertEquals(pojoEntity.getFecha().getDay(), resp.getFecha().getDay()); + Assert.assertEquals(pojoEntity.getFecha().getMonth(), resp.getFecha().getMonth()); + Assert.assertEquals(pojoEntity.getFecha().getYear(), resp.getFecha().getYear()); + Assert.assertEquals(pojoEntity.getGuia(), resp.getGuia()); + Assert.assertEquals(pojoEntity.getPaseo(), resp.getPaseo()); + } + + private Date agregaDias(Date fecha, int dias) { + Calendar calendario = Calendar.getInstance(); + calendario.setTime(fecha); + calendario.add(Calendar.DATE, dias); + return calendario.getTime(); + } + Date hoy = new Date(); +} diff --git a/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/UsuarioLogicTest.java b/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/UsuarioLogicTest.java index 6aa9c24..d3bdb6d 100644 --- a/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/UsuarioLogicTest.java +++ b/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/logic/UsuarioLogicTest.java @@ -77,7 +77,7 @@ public static WebArchive createDeployment() { * @generated */ @Inject - private UsuarioLogic usuarioLogic; + private UsuarioLogic ofertaLogic; /** * @generated @@ -153,7 +153,7 @@ public void createUsuarioTest() throws BusinessLogicException { PodamFactory factory = new PodamFactoryImpl(); UsuarioEntity entity = factory.manufacturePojo(UsuarioEntity.class); UsuarioEntity result =null; - result = usuarioLogic.createUsuario(entity); + result = ofertaLogic.createUsuario(entity); Assert.assertNotNull(result); Assert.assertEquals(result.getNombres(), entity.getNombres()); @@ -176,7 +176,7 @@ public void createUsuarioTest1() { UsuarioEntity entity = factory.manufacturePojo(UsuarioEntity.class); UsuarioEntity result =null; entity.setLogin(data.get(0).getLogin()); - result = usuarioLogic.createUsuario(entity); + result = ofertaLogic.createUsuario(entity); } catch (BusinessLogicException ex) { @@ -194,7 +194,7 @@ public void getUsuarioTest() { UsuarioEntity entity = data.get(0); UsuarioEntity resultEntity= null ; try { - resultEntity = usuarioLogic.getUsuario(entity.getId()); + resultEntity = ofertaLogic.getUsuario(entity.getId()); } catch (BusinessLogicException ex) { Assert.fail("No deberia generar excepción"); } @@ -211,7 +211,7 @@ public void getUsuarioTest1() { UsuarioEntity entity = data.get(0); UsuarioEntity resultEntity= null ; try { - resultEntity = usuarioLogic.getUsuario(100L); + resultEntity = ofertaLogic.getUsuario(100L); } catch (BusinessLogicException ex) { Assert.assertEquals(1, 1); } @@ -225,7 +225,7 @@ public void getUsuarioTest1() { */ @Test public void getUsuariosTest() { - List list = usuarioLogic.getUsuarios(); + List list = ofertaLogic.getUsuarios(); Assert.assertEquals(data.size(), list.size()); for (UsuarioEntity entity : list) { boolean found = false; @@ -247,7 +247,7 @@ public void getUsuariosTest() { public void deleteUsuarioTest() { UsuarioEntity entity = data.get(0); try { - usuarioLogic.deleteUsuario(entity.getId()); + ofertaLogic.deleteUsuario(entity.getId()); } catch (BusinessLogicException ex) { Assert.fail("No deberia generar excepción"); } @@ -261,7 +261,7 @@ public void deleteUsuarioTest() { public void deleteUsuarioTest1() { UsuarioEntity entity = data.get(0); try { - usuarioLogic.deleteUsuario(100L); + ofertaLogic.deleteUsuario(100L); } catch (BusinessLogicException ex) { Assert.assertEquals(1, 1); } @@ -280,7 +280,7 @@ public void updateUsuarioTest() throws BusinessLogicException { UsuarioEntity pojoEntity = factory.manufacturePojo(UsuarioEntity.class); pojoEntity.setId(entity.getId()); - usuarioLogic.updateUsuario(pojoEntity); + ofertaLogic.updateUsuario(pojoEntity); UsuarioEntity resp = em.find(UsuarioEntity.class, entity.getId()); diff --git a/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/persistence/OfertasPersistenceTest.java b/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/persistence/OfertasPersistenceTest.java index 66fc98b..9831313 100644 --- a/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/persistence/OfertasPersistenceTest.java +++ b/paseos-01-logic/src/test/java/co/edu/uniandes/csw/paseos/test/persistence/OfertasPersistenceTest.java @@ -135,8 +135,6 @@ public void createOfertaTest() { /** * Prueba para consultar la lista de Ofertas. - * - * @generated */ @Test public void getOfertasTest() { diff --git a/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/UsuarioResource.java b/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/UsuarioResource.java index a563933..c0ad8b6 100644 --- a/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/UsuarioResource.java +++ b/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/UsuarioResource.java @@ -63,7 +63,7 @@ public UsuarioResource() } @Inject - private UsuarioLogic usuarioLogic; + private UsuarioLogic ofertaLogic; /** * Convierte una lista de EmployeeEntity a una lista de EmployeeDetailDTO. @@ -92,7 +92,7 @@ private List listEntity2DTO(List entityList){ public List getUsuarios() { - return listEntity2DTO(usuarioLogic.getUsuarios()); + return listEntity2DTO(ofertaLogic.getUsuarios()); } **/ @@ -107,7 +107,7 @@ public List getUsuarios() { @Path("/usuarios/{id: \\d+}") // TODO: retornar una excepción / código 404 si no existe public UsuarioDetailDTO getUsuario(@PathParam("id") Long id) throws BusinessLogicException { - return new UsuarioDetailDTO(usuarioLogic.getUsuario(id)); + return new UsuarioDetailDTO(ofertaLogic.getUsuario(id)); } /** @@ -120,7 +120,7 @@ public UsuarioDetailDTO getUsuario(@PathParam("id") Long id) throws BusinessLogi @POST @Path("usuarios") public UsuarioDTO createUsuario(UsuarioDTO dto) throws BusinessLogicException { - return new UsuarioDTO(usuarioLogic.createUsuario(dto.toEntity())); + return new UsuarioDTO(ofertaLogic.createUsuario(dto.toEntity())); } /** @@ -136,7 +136,7 @@ public UsuarioDTO createUsuario(UsuarioDTO dto) throws BusinessLogicException { public UsuarioDetailDTO updateUsuario(@PathParam("id") Long id, UsuarioDetailDTO dto) throws BusinessLogicException { UsuarioEntity entity = dto.toEntity(); entity.setId(id); - return new UsuarioDetailDTO(usuarioLogic.updateUsuario(entity)); + return new UsuarioDetailDTO(ofertaLogic.updateUsuario(entity)); } /** @@ -150,14 +150,14 @@ public UsuarioDetailDTO updateUsuario(@PathParam("id") Long id, UsuarioDetailDTO @Path("usuarios/{id: \\d+}") public void deleteUsuario(@PathParam("id") Long id) throws BusinessLogicException { - usuarioLogic.deleteUsuario(id); + ofertaLogic.deleteUsuario(id); } @GET @Path("usuarios") public List getUsuariosGuias(@QueryParam("guias")int g ){ List lista = new ArrayList(); - List lista1 = listEntity2DTO(usuarioLogic.getUsuarios()); + List lista1 = listEntity2DTO(ofertaLogic.getUsuarios()); for (UsuarioDetailDTO usuario : lista1 ) { if (usuario.getGuia()!=null) diff --git a/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/VisitaResource.java b/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/VisitaResource.java index 0734123..7b24f1d 100644 --- a/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/VisitaResource.java +++ b/paseos-01-web/src/main/java/co/edu/uniandes/csw/paseos/resources/VisitaResource.java @@ -54,7 +54,7 @@ public class VisitaResource { @Inject private OfertaLogic ofertaLogic; - @Inject private UsuarioLogic usuarioLogic; + @Inject private UsuarioLogic ofertaLogic; private List listEntity2DTO(List entityList){ List list = new ArrayList<>(); @@ -96,7 +96,7 @@ public VisitaDTO createVisita(@PathParam("idOferta") Long idOferta, @PathParam(" VisitaDTO visita = new VisitaDTO(visitaLogic.createVisita(entity)); OfertaEntity oferta = ofertaLogic.getOferta(idOferta); entity.setOferta(oferta); - UsuarioEntity usuario = usuarioLogic.getUsuario(idUsuario); + UsuarioEntity usuario = ofertaLogic.getUsuario(idUsuario); entity.setUsuario(usuario); visitaLogic.setUsuarioYOferta(entity); return visita; diff --git a/paseos-01-web/src/test/resources/postmanTests/ofertasTest.postman_collection.json b/paseos-01-web/src/test/resources/postmanTests/ofertasTest.postman_collection.json new file mode 100644 index 0000000..74af591 --- /dev/null +++ b/paseos-01-web/src/test/resources/postmanTests/ofertasTest.postman_collection.json @@ -0,0 +1,122 @@ +{ + "id": "07ebc323-0309-e3b2-4a3a-67f6c6e9a66f", + "name": "ofertasTest", + "description": "", + "order": [ + "6c8322b5-983f-c1cb-7d0d-5abf69755792", + "702964c7-e0a3-91ac-efc5-d4e164787c22", + "0e81bfd1-4538-d798-f201-4b3383e967db", + "ee51b0d9-622b-7190-1c6e-0ef7141a6f28", + "3f709d1c-9d19-b610-a02d-2a517e62cf73" + ], + "folders": [], + "timestamp": 1495040445754, + "owner": 0, + "public": false, + "requests": [ + { + "id": "0e81bfd1-4538-d798-f201-4b3383e967db", + "headers": [], + "url": "http://localhost:8080/paseos-01-web/api/ofertas/123", + "pathVariables": {}, + "preRequestScript": null, + "method": "GET", + "collectionId": "07ebc323-0309-e3b2-4a3a-67f6c6e9a66f", + "data": [], + "dataMode": "raw", + "name": "GET OFERTA", + "description": "", + "descriptionFormat": "html", + "time": 1495040713678, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "rawModeData": "\t{\n\t\t\"fecha\": \"2018-09-06T00:00:00-05:00\",\n \"inscritos\": 0,\n \"guia\": {\n \"id\": 103\n },\n \"paseo\": {\n \"id\": 102\n },\n \"visitas\": []\n }" + }, + { + "id": "3f709d1c-9d19-b610-a02d-2a517e62cf73", + "headers": "Content-Type: application/json\n", + "url": "http://localhost:8080/paseos-01-web/api/ofertas/123", + "pathVariables": {}, + "preRequestScript": null, + "method": "DELETE", + "collectionId": "07ebc323-0309-e3b2-4a3a-67f6c6e9a66f", + "data": [], + "dataMode": "raw", + "name": "DELETE OFERTA", + "description": "", + "descriptionFormat": "html", + "time": 1495041221036, + "version": 2, + "responses": [], + "tests": "tests[\"Status code is 200\"] = responseCode.code === 200;\r\n", + "currentHelper": "normal", + "helperAttributes": {}, + "rawModeData": "" + }, + { + "id": "6c8322b5-983f-c1cb-7d0d-5abf69755792", + "headers": "Content-Type: application/json\n", + "url": "http://localhost:8080/paseos-01-web/api/ofertas", + "preRequestScript": null, + "pathVariables": {}, + "method": "POST", + "data": [], + "dataMode": "raw", + "version": 2, + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "time": 1495040595152, + "name": "POST OFERTA", + "description": "", + "collectionId": "07ebc323-0309-e3b2-4a3a-67f6c6e9a66f", + "responses": [], + "rawModeData": "\t{\n\t\t\"fecha\": \"2020-09-06T00:00:00-05:00\",\n \"id\": 123,\n \"inscritos\": 0,\n \"guia\": {\n \"id\": 103\n },\n \"paseo\": {\n \"id\": 102\n },\n \"visitas\": []\n }" + }, + { + "id": "702964c7-e0a3-91ac-efc5-d4e164787c22", + "headers": "Content-Type: application/json\n", + "url": "http://localhost:8080/paseos-01-web/api/ofertas/123", + "pathVariables": {}, + "preRequestScript": null, + "method": "PUT", + "collectionId": "07ebc323-0309-e3b2-4a3a-67f6c6e9a66f", + "data": [], + "dataMode": "raw", + "name": "PUT OFERTA", + "description": "", + "descriptionFormat": "html", + "time": 1495040692910, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "rawModeData": "\t{\n\t\t\"fecha\": \"2018-09-06T00:00:00-05:00\",\n \"inscritos\": 0,\n \"guia\": {\n \"id\": 103\n },\n \"paseo\": {\n \"id\": 102\n },\n \"visitas\": []\n }" + }, + { + "id": "ee51b0d9-622b-7190-1c6e-0ef7141a6f28", + "headers": [], + "url": "http://localhost:8080/paseos-01-web/api/ofertas", + "pathVariables": {}, + "preRequestScript": null, + "method": "GET", + "collectionId": "07ebc323-0309-e3b2-4a3a-67f6c6e9a66f", + "data": [], + "dataMode": "raw", + "name": "GET OFERTAS", + "description": "", + "descriptionFormat": "html", + "time": 1495040735064, + "version": 2, + "responses": [], + "tests": null, + "currentHelper": "normal", + "helperAttributes": {}, + "rawModeData": "\t{\n\t\t\"fecha\": \"2018-09-06T00:00:00-05:00\",\n \"inscritos\": 0,\n \"guia\": {\n \"id\": 103\n },\n \"paseo\": {\n \"id\": 102\n },\n \"visitas\": []\n }" + } + ] +} \ No newline at end of file