ItEr52S10AdaptacionServiciosRESTItEr51S10: Creation and Adapting of UnitTypeServiceRest.

This commit is contained in:
Susana Montes Pedreira 2010-03-26 14:54:10 +01:00 committed by Javier Moran Rua
parent 92e1df8216
commit 4126c6de43
18 changed files with 618 additions and 86 deletions

View file

@ -45,7 +45,8 @@ public class UnitTypeBootstrap implements IDataBootstrap {
public void loadRequiredData() {
for (PredefinedUnitTypes predefinedUnitType : PredefinedUnitTypes
.values()) {
if (!unitTypeDAO.existsUnitTypeByName(predefinedUnitType
if (!unitTypeDAO
.existsUnitTypeByNameInAnotherTransaction(predefinedUnitType
.getMeasure())) {
unitTypeDAO.save(predefinedUnitType.createUnitType());
}

View file

@ -35,6 +35,8 @@ public interface IUnitTypeDAO extends IIntegrationEntityDAO<UnitType> {
UnitType findByName(String measure) throws InstanceNotFoundException;
boolean existsUnitTypeByName(String measure);
UnitType findUniqueByNameInAnotherTransaction(String measure)
throws InstanceNotFoundException;
boolean existsUnitTypeByNameInAnotherTransaction(String measure);
}

View file

@ -30,6 +30,8 @@ import org.navalplanner.business.materials.entities.UnitType;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* DAO for {@link UnitType}
@ -65,7 +67,15 @@ public class UnitTypeDAO extends IntegrationEntityDAO<UnitType> implements
}
@Override
public boolean existsUnitTypeByName(String measure) {
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public UnitType findUniqueByNameInAnotherTransaction(String measure)
throws InstanceNotFoundException {
return findByName(measure);
}
@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW)
public boolean existsUnitTypeByNameInAnotherTransaction(String measure) {
try {
findByName(measure);
} catch (InstanceNotFoundException e) {

View file

@ -20,9 +20,12 @@
package org.navalplanner.business.materials.entities;
import org.apache.commons.lang.StringUtils;
import org.hibernate.validator.AssertTrue;
import org.hibernate.validator.NotEmpty;
import org.navalplanner.business.common.IntegrationEntity;
import org.navalplanner.business.common.Registry;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.materials.daos.IUnitTypeDAO;
/**
@ -41,6 +44,12 @@ public class UnitType extends IntegrationEntity{
return (UnitType) create(new UnitType(measure));
}
public void updateUnvalidated(String measure) {
if (!StringUtils.isBlank(measure)) {
this.measure = measure;
}
}
private String measure;
// Default constructor, needed by Hibernate
@ -61,6 +70,33 @@ public class UnitType extends IntegrationEntity{
this.measure = measure;
}
@AssertTrue(message = "the measure unit type has to be unique. It is already used")
public boolean checkConstraintUniqueName() {
boolean result;
if (isNewObject()) {
result = !existsUnitTypeWithTheName();
} else {
result = isIfExistsTheExistentUnitTypeThisOne();
}
return result;
}
private boolean existsUnitTypeWithTheName() {
IUnitTypeDAO unitTypeDAO = Registry.getUnitTypeDAO();
return unitTypeDAO.existsUnitTypeByNameInAnotherTransaction(measure);
}
private boolean isIfExistsTheExistentUnitTypeThisOne() {
IUnitTypeDAO unitTypeDAO = Registry.getUnitTypeDAO();
try {
UnitType unitType = unitTypeDAO
.findUniqueByNameInAnotherTransaction(measure);
return unitType.getId().equals(getId());
} catch (InstanceNotFoundException e) {
return true;
}
}
@Override
protected IUnitTypeDAO getIntegrationEntityDAO() {
return Registry.getUnitTypeDAO();

View file

@ -34,9 +34,6 @@ public class MaterialDTO extends IntegrationEntityDTO {
public final static String ENTITY_TYPE = "material";
@XmlAttribute
public String name;
@XmlAttribute
public String description;

View file

@ -176,7 +176,8 @@ public final class MaterialConverter {
if (materialDTO.unitType != null) {
try {
UnitType unitType = Registry.getUnitTypeDAO().findByCode(
UnitType unitType = Registry.getUnitTypeDAO()
.findByCodeAnotherTransaction(
materialDTO.unitType);
material.setUnitType(unitType);
} catch (InstanceNotFoundException e) {
@ -259,7 +260,8 @@ public final class MaterialConverter {
MaterialDTO materialDTO) throws ValidationException {
if (materialDTO.unitType != null) {
try {
UnitType type = Registry.getUnitTypeDAO().findByCode(
UnitType type = Registry.getUnitTypeDAO()
.findByCodeAnotherTransaction(
materialDTO.unitType);
material.setUnitType(type);
} catch (InstanceNotFoundException e) {

View file

@ -0,0 +1,32 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.unittypes.api;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
public interface IUnitTypeService {
public UnitTypeListDTO getUnitTypes();
public InstanceConstraintViolationsListDTO addUnitTypes(
UnitTypeListDTO unitTypeListDTO);
}

View file

@ -0,0 +1,60 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.unittypes.api;
import javax.xml.bind.annotation.XmlAttribute;
import org.navalplanner.ws.common.api.IntegrationEntityDTO;
/**
* DTO for <code>UnitType</code> entity.
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
public class UnitTypeDTO extends IntegrationEntityDTO {
public final static String ENTITY_TYPE = "unit type";
@XmlAttribute
public String measure;
public UnitTypeDTO() {
}
public UnitTypeDTO(String code, String measure) {
super(code);
this.measure = measure;
}
/**
* This constructor automatically generates a unique code. It is intended to
* facilitate the implementation of test cases that add new instances (such
* instances will have a unique code).
*/
public UnitTypeDTO(String measure) {
this(generateCode(), measure);
}
@Override
public String getEntityType() {
return ENTITY_TYPE;
}
}

View file

@ -0,0 +1,48 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.unittypes.api;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* DTO for a list of <code>UnitType</code> entities.
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
@XmlRootElement(name = "unit-type-list")
public class UnitTypeListDTO {
@XmlElement(name = "unit-type")
public List<UnitTypeDTO> unitTypeDTOs = new ArrayList<UnitTypeDTO>();
public UnitTypeListDTO() {
}
public UnitTypeListDTO(List<UnitTypeDTO> unitTypeDTOs) {
if (unitTypeDTOs != null) {
this.unitTypeDTOs = unitTypeDTOs;
}
}
}

View file

@ -0,0 +1,28 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Specification of namespace for REST-based services.
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
@javax.xml.bind.annotation.XmlSchema(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, namespace = WSCommonGlobalNames.REST_NAMESPACE)
package org.navalplanner.ws.unittypes.api;
import org.navalplanner.ws.common.api.WSCommonGlobalNames;

View file

@ -0,0 +1,51 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.unittypes.impl;
import org.apache.commons.lang.StringUtils;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.materials.entities.UnitType;
import org.navalplanner.ws.unittypes.api.UnitTypeDTO;
/**
* Service for managing unit-types-related entities.
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
public class UnitTypeConverter {
private UnitTypeConverter() {
}
public final static UnitTypeDTO toDTO(UnitType unitType) {
return new UnitTypeDTO(unitType.getCode(), unitType.getMeasure());
}
public final static UnitType toEntity(UnitTypeDTO unitTypeDTO) {
return UnitType.create(unitTypeDTO.code, unitTypeDTO.measure);
}
public final static void updateUnitType(UnitType unitType,
UnitTypeDTO unitTypeDTO) throws ValidationException {
/* 1: Update unit type basic properties. */
unitType.updateUnvalidated(StringUtils.trim(unitTypeDTO.measure));
}
}

View file

@ -0,0 +1,93 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.ws.unittypes.impl;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.navalplanner.business.common.daos.IIntegrationEntityDAO;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.materials.daos.IUnitTypeDAO;
import org.navalplanner.business.materials.entities.UnitType;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO;
import org.navalplanner.ws.common.impl.GenericRESTService;
import org.navalplanner.ws.unittypes.api.IUnitTypeService;
import org.navalplanner.ws.unittypes.api.UnitTypeDTO;
import org.navalplanner.ws.unittypes.api.UnitTypeListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* REST-based implementation of <code>IUnitTypeService</code>.
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
@Path("/unittypes/")
@Produces("application/xml")
@Service("unitTypeServiceREST")
public class UnitTypeServiceREST extends
GenericRESTService<UnitType, UnitTypeDTO> implements IUnitTypeService {
@Autowired
private IUnitTypeDAO unitTypeDAO;
@Override
@GET
@Transactional(readOnly = true)
public UnitTypeListDTO getUnitTypes() {
return new UnitTypeListDTO(findAll());
}
@Override
@POST
@Consumes("application/xml")
public InstanceConstraintViolationsListDTO addUnitTypes(
UnitTypeListDTO unitTypeListDTO) {
return save(unitTypeListDTO.unitTypeDTOs);
}
@Override
protected UnitType toEntity(UnitTypeDTO entityDTO) {
return UnitTypeConverter.toEntity(entityDTO);
}
@Override
protected UnitTypeDTO toDTO(UnitType entity) {
return UnitTypeConverter.toDTO(entity);
}
@Override
protected IIntegrationEntityDAO<UnitType> getIntegrationEntityDAO() {
return unitTypeDAO;
}
@Override
protected void updateEntity(UnitType entity, UnitTypeDTO entityDTO)
throws ValidationException {
UnitTypeConverter.updateUnitType(entity, entityDTO);
}
}

View file

@ -74,6 +74,7 @@
<ref bean="calendarExceptionTypeServiceREST"/>
<ref bean="calendarServiceREST"/>
<ref bean="materialServiceREST"/>
<ref bean="unitTypeServiceREST"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="runtimeExceptionMapper" />

View file

@ -25,6 +25,7 @@ import static org.junit.Assert.fail;
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.navalplanner.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE;
import static org.navalplanner.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE;
import static org.navalplanner.web.test.ws.common.Util.getUniqueName;
import java.math.BigDecimal;
import java.util.ArrayList;
@ -40,7 +41,6 @@ import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.materials.daos.IMaterialCategoryDAO;
import org.navalplanner.business.materials.daos.IMaterialDAO;
import org.navalplanner.business.materials.daos.IUnitTypeDAO;
import org.navalplanner.business.materials.entities.Material;
import org.navalplanner.business.materials.entities.MaterialCategory;
import org.navalplanner.business.materials.entities.UnitType;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
@ -91,8 +91,8 @@ public class MaterialServiceTest {
@Test
@Rollback(false)
public void CreateUnitType() {
UnitType entityA = UnitType.create(unitTypeCodeA, "uninewUnitTypeA");
UnitType entityB = UnitType.create(unitTypeCodeB, "uninewUnitTypeB");
UnitType entityA = UnitType.create(unitTypeCodeA, "UnitTypeA");
UnitType entityB = UnitType.create(unitTypeCodeB, "UnitTypeB");
unitTypeDAO.save(entityA);
unitTypeDAO.save(entityB);
unitTypeDAO.flush();
@ -253,6 +253,17 @@ public class MaterialServiceTest {
@Test
public void testAddAndUpdateMaterialCategory() {
String unitTypeCodeA = getUniqueName();
String unitTypeCodeB = getUniqueName();
UnitType entityA = UnitType.create(unitTypeCodeA, "UnitTypeA");
UnitType entityB = UnitType.create(unitTypeCodeB, "UnitTypeB");
unitTypeDAO.save(entityA);
unitTypeDAO.save(entityB);
unitTypeDAO.flush();
sessionFactory.getCurrentSession().evict(entityA);
sessionFactory.getCurrentSession().evict(entityB);
/* Build material (0 constraint violations). */
MaterialDTO m1 = new MaterialDTO("M-1", "tornillos",
new BigDecimal(13), unitTypeCodeA, true);
@ -265,7 +276,8 @@ public class MaterialServiceTest {
null, null);
MaterialCategoryListDTO subCategoryListDTOC = createMaterialCategoryListDTO(mc1);
MaterialCategoryDTO mc2 = new MaterialCategoryDTO("MC-B", "MC-B", null,
MaterialCategoryDTO mc2 = new MaterialCategoryDTO("MC-B", "MC-B",
"C-A",
subCategoryListDTOC, materialDTOs1);
MaterialCategoryListDTO subCategoryListDTOB = createMaterialCategoryListDTO(mc2);
@ -292,80 +304,6 @@ public class MaterialServiceTest {
fail();
}
// Update data
m1 = new MaterialDTO("M-1", "update-tornillos", new BigDecimal(20),
unitTypeCodeB, false);
materialDTOs1 = new ArrayList<MaterialDTO>();
materialDTOs1.add(m1);
mc1 = new MaterialCategoryDTO("MC-C", "update-MC-C", "MC-B", null, null);
subCategoryListDTOC = createMaterialCategoryListDTO(mc1);
mc2 = new MaterialCategoryDTO("MC-B", "update-MC-B", null,
subCategoryListDTOC, materialDTOs1);
subCategoryListDTOB = createMaterialCategoryListDTO(mc2);
/* Build main material category */
materialCategoryDTO = new MaterialCategoryDTO("C-A", "update-C-A",
null, subCategoryListDTOB, null);
materialCategoryListDTOA = createMaterialCategoryListDTO(materialCategoryDTO);
instanceConstraintViolationsList = materialService
.addMaterials(materialCategoryListDTOA).instanceConstraintViolationsList;
assertTrue(instanceConstraintViolationsList.toString(),
instanceConstraintViolationsList.size() == 0);
try {
MaterialCategory mc = materialCategoryDAO.findByCode("C-A");
MaterialCategory mcb = materialCategoryDAO.findByCode("MC-B");
MaterialCategory mcc = materialCategoryDAO.findByCode("MC-C");
assertTrue(mcb.getMaterials().size() == 1);
assertTrue(mcb.getSubcategories().size() == 1);
assertTrue(mc.getName().equalsIgnoreCase("update-C-A"));
assertTrue(mcb.getName().equalsIgnoreCase("update-MC-B"));
assertTrue(mcc.getName().equalsIgnoreCase("update-MC-C"));
Material m = materialDAO.findByCode("M-1");
assertTrue(m.getDescription().equalsIgnoreCase("update-tornillos"));
assertTrue(m.getDefaultUnitPrice().compareTo(new BigDecimal(20)) == 0);
assertTrue(m.getUnitType().getCode().equals(unitTypeCodeB));
assertTrue(!m.getDisabled());
} catch (InstanceNotFoundException e) {
fail();
}
// invalid parent code. The parent not is updatable
mc1 = new MaterialCategoryDTO("MC-C", "update-MC-C", "C-A", null, null);
subCategoryListDTOC = createMaterialCategoryListDTO(mc1);
/* Build main material category */
materialCategoryDTO = new MaterialCategoryDTO("C-A", "update-C-A",
null, subCategoryListDTOC, null);
materialCategoryListDTOA = createMaterialCategoryListDTO(materialCategoryDTO);
instanceConstraintViolationsList = materialService
.addMaterials(materialCategoryListDTOA).instanceConstraintViolationsList;
assertTrue(instanceConstraintViolationsList.toString(),
instanceConstraintViolationsList.size() == 1);
/* Build main material category */
materialCategoryDTO = new MaterialCategoryDTO("C-A", "update-C-A",
"XXX", subCategoryListDTOC, null);
materialCategoryListDTOA = createMaterialCategoryListDTO(materialCategoryDTO);
instanceConstraintViolationsList = materialService
.addMaterials(materialCategoryListDTOA).instanceConstraintViolationsList;
assertTrue(instanceConstraintViolationsList.toString(),
instanceConstraintViolationsList.size() == 1);
}
private MaterialCategoryListDTO createMaterialCategoryListDTO(

View file

@ -0,0 +1,168 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2009 Fundación para o Fomento da Calidade Industrial e
* Desenvolvemento Tecnolóxico de Galicia
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.test.ws.unittypes;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE;
import static org.navalplanner.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE;
import static org.navalplanner.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.business.IDataBootstrap;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.materials.daos.IUnitTypeDAO;
import org.navalplanner.business.materials.entities.UnitType;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
import org.navalplanner.ws.unittypes.api.IUnitTypeService;
import org.navalplanner.ws.unittypes.api.UnitTypeDTO;
import org.navalplanner.ws.unittypes.api.UnitTypeListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Tests for <code>IUnitTypeService</code>.
* @author Susana Montes Pedreira <smontes@wirelessgalicia.com>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE })
@Transactional
public class UnitTypeServiceTest {
@Autowired
private SessionFactory sessionFactory;
@Autowired
private IUnitTypeService unitTypeService;
@Autowired
private IUnitTypeDAO unitTypeDAO;
@Resource
private IDataBootstrap unitTypeBootstrap;
@Test
@Rollback(false)
public void loadRequiredaData() {
unitTypeBootstrap.loadRequiredData();
}
@Test
public void testAddUnitTypeRepeatedMeasure() {
/* Build material with same code (1 constraint violations). */
UnitTypeDTO m1 = new UnitTypeDTO("CodeA", "measure1");
UnitTypeDTO m2 = new UnitTypeDTO("CodeB", "measure1");
UnitTypeDTO m3 = new UnitTypeDTO("measure1");
List<UnitTypeDTO> unitTypeDTOs = new ArrayList<UnitTypeDTO>();
unitTypeDTOs.add(m1);
unitTypeDTOs.add(m2);
unitTypeDTOs.add(m3);
UnitTypeListDTO unitTypeListDTO = createUnitTypeListDTO(m1, m2);
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = unitTypeService
.addUnitTypes(unitTypeListDTO).instanceConstraintViolationsList;
assertTrue(instanceConstraintViolationsList.toString(),
instanceConstraintViolationsList.size() == 1);
}
@Test
public void testAddAndUpdateMaterialCategory() {
/* Build unittype (0 constraint violations). */
UnitTypeDTO m1 = new UnitTypeDTO("XXX", "measureX");
UnitTypeDTO m2 = new UnitTypeDTO("YYY", "measureY");
List<UnitTypeDTO> unitTypeDTOs = new ArrayList<UnitTypeDTO>();
unitTypeDTOs.add(m1);
unitTypeDTOs.add(m2);
UnitTypeListDTO unitTypeListDTO = createUnitTypeListDTO(m1, m2);
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = unitTypeService
.addUnitTypes(unitTypeListDTO).instanceConstraintViolationsList;
assertTrue(instanceConstraintViolationsList.toString(),
instanceConstraintViolationsList.size() == 0);
try {
UnitType typeX = unitTypeDAO.findByCode("XXX");
assertTrue(typeX.getMeasure().equalsIgnoreCase("measureX"));
UnitType typeY = unitTypeDAO.findByCode("YYY");
assertTrue(typeY.getMeasure().equalsIgnoreCase("measureY"));
unitTypeDAO.flush();
sessionFactory.getCurrentSession().evict(typeX);
sessionFactory.getCurrentSession().evict(typeY);
} catch (InstanceNotFoundException e) {
fail();
}
/* Update the measure unit type */
m1 = new UnitTypeDTO("XXX", "update-measureX");
m2 = new UnitTypeDTO("YYY", "update-measureY");
unitTypeDTOs = new ArrayList<UnitTypeDTO>();
unitTypeDTOs.add(m1);
unitTypeDTOs.add(m2);
unitTypeListDTO = createUnitTypeListDTO(m1, m2);
instanceConstraintViolationsList = unitTypeService
.addUnitTypes(unitTypeListDTO).instanceConstraintViolationsList;
assertTrue(instanceConstraintViolationsList.toString(),
instanceConstraintViolationsList.size() == 0);
try {
UnitType typeX = unitTypeDAO.findByCode("XXX");
assertTrue(typeX.getMeasure().equalsIgnoreCase("update-measureX"));
UnitType typeY = unitTypeDAO.findByCode("YYY");
assertTrue(typeY.getMeasure().equalsIgnoreCase("update-measureY"));
} catch (InstanceNotFoundException e) {
fail();
}
}
private UnitTypeListDTO createUnitTypeListDTO(UnitTypeDTO... unitTypeDTOs) {
List<UnitTypeDTO> unitTypeList = new ArrayList<UnitTypeDTO>();
for (UnitTypeDTO c : unitTypeDTOs) {
unitTypeList.add(c);
}
return new UnitTypeListDTO(unitTypeList);
}
}

View file

@ -0,0 +1,21 @@
#!/bin/sh
. ./rest-common-env.sh
printf "Login name: "
read loginName
printf "Password: "
read password
if [ "$1" = "--prod" ]; then
baseServiceURL=$PRODUCTION_BASE_SERVICE_URL
certificate=$PRODUCTION_CERTIFICATE
else
baseServiceURL=$DEVELOPMENT_BASE_SERVICE_URL
certificate=$DEVELOPMENT_CERTIFICATE
fi
authorization=`./base64.sh $loginName:$password`
curl -sv -X GET $certificate --header "Authorization: Basic $authorization" \
$baseServiceURL/unittypes | tidy -xml -i -q -utf8

View file

@ -0,0 +1,33 @@
#!/bin/sh
. ./rest-common-env.sh
printf "Login name: "
read loginName
printf "Password: "
read password
baseServiceURL=$DEVELOPMENT_BASE_SERVICE_URL
certificate=$DEVELOPMENT_CERTIFICATE
for i in "$@"
do
if [ "$i" = "--prod" ]; then
baseServiceURL=$PRODUCTION_BASE_SERVICE_URL
certificate=$PRODUCTION_CERTIFICATE
else
file=$i
fi
done
if [ "$file" = "" ]; then
printf "Missing file\n" 1>&2
exit 1
fi
authorization=`./base64.sh $loginName:$password`
curl -sv -X POST $certificate -d @$file \
--header "Content-type: application/xml" \
--header "Authorization: Basic $authorization" \
$baseServiceURL/unittypesp | tidy -xml -i -q -utf8

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<unit-type-list xmlns="http://rest.ws.navalplanner.org">
<unit-type measure="units" code="000-001" />
<unit-type measure="kg" code="000-002" />
<unit-type measure="km" code="000-003" />
<unit-type measure="l" code="000-004" />
<unit-type measure="m" code="000-005" />
<unit-type measure="m2" code="000-006" />
<unit-type measure="m3" code="000-007" />
<unit-type measure="tn" code="000-008" />
</unit-type-list>