/*
* The MIT License
*
* Copyright 2012 J. David Mendoza <jdmendoza@um.edu.mx>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package mx.edu.um.mateo.inventario.web;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import mx.edu.um.mateo.general.model.Empresa;
import mx.edu.um.mateo.general.model.Organizacion;
import mx.edu.um.mateo.general.model.Rol;
import mx.edu.um.mateo.general.model.Usuario;
import mx.edu.um.mateo.general.test.BaseControllerTest;
import mx.edu.um.mateo.general.test.GenericWebXmlContextLoader;
import mx.edu.um.mateo.inventario.model.Almacen;
import mx.edu.um.mateo.inventario.model.Producto;
import mx.edu.um.mateo.inventario.model.TipoProducto;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.springframework.test.web.server.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.*;
import org.springframework.transaction.annotation.Transactional;
/**
*
* @author J. David Mendoza <jdmendoza@um.edu.mx>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = GenericWebXmlContextLoader.class, locations = {
"classpath:mateo.xml",
"classpath:security.xml",
"classpath:dispatcher-servlet.xml"
})
@Transactional
public class ProductoControllerTest extends BaseControllerTest {
@Test
public void debieraMostrarListaDeTiposDeCliente() throws Exception {
log.debug("Debiera mostrar lista de productos");
this.mockMvc.perform(
get("/inventario/producto"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/jsp/inventario/producto/lista.jsp"))
.andExpect(model().attributeExists("productos"))
.andExpect(model().attributeExists("paginacion"))
.andExpect(model().attributeExists("paginas"))
.andExpect(model().attributeExists("pagina"));
}
@Test
public void debieraMostrarProducto() throws Exception {
log.debug("Debiera mostrar producto");
Organizacion organizacion = new Organizacion("tst-01", "test-01", "test-01");
currentSession().save(organizacion);
Empresa empresa = new Empresa("tst-01", "test-01", "test-01", "000000000001", organizacion);
currentSession().save(empresa);
Almacen almacen = new Almacen("TST", "test-01", empresa);
currentSession().save(almacen);
TipoProducto tipoProducto = new TipoProducto("TEST-01", "TEST-01", almacen);
currentSession().save(tipoProducto);
Producto producto = new Producto("tst-01", "test-01", "tst-01", "tst-01", tipoProducto, almacen);
currentSession().save(producto);
Long id = producto.getId();
this.mockMvc.perform(get("/inventario/producto/ver/" + id))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/jsp/inventario/producto/ver.jsp"))
.andExpect(model().attributeExists("producto"));
}
@Test
public void debieraCrearProducto() throws Exception {
Organizacion organizacion = new Organizacion("tst-01", "test-01", "test-01");
currentSession().save(organizacion);
Empresa empresa = new Empresa("tst-01", "test-01", "test-01", "000000000001", organizacion);
currentSession().save(empresa);
Rol rol = new Rol("ROLE_TEST");
currentSession().save(rol);
Set<Rol> roles = new HashSet<>();
roles.add(rol);
Almacen almacen = new Almacen("TST", "TEST", empresa);
currentSession().save(almacen);
TipoProducto tipoProducto = new TipoProducto("TEST-01", "TEST-01", almacen);
currentSession().save(tipoProducto);
Usuario usuario = new Usuario("bugs@um.edu.mx", "apPaterno","apMaterno", "TEST-01", "TEST-01");
usuario.setEmpresa(empresa);
usuario.setAlmacen(almacen);
usuario.setRoles(roles);
currentSession().save(usuario);
Long id = usuario.getId();
assertNotNull(id);
this.authenticate(usuario, usuario.getPassword(), new ArrayList<GrantedAuthority>(usuario.getRoles()));
this.mockMvc.perform(fileUpload("/inventario/producto/crea")
.param("codigo", "TST-01")
.param("sku", "TEST--01")
.param("nombre", "TEST--01")
.param("descripcion", "TEST--01")
.param("tipoProducto.id", tipoProducto.getId().toString())
)
.andExpect(status().isOk())
.andExpect(flash().attributeExists("message"))
.andExpect(flash().attribute("message", "producto.creado.message"));
}
}