From 4126c6de43188855b2a88afccdd6e857c4e31ede Mon Sep 17 00:00:00 2001 From: Susana Montes Pedreira Date: Fri, 26 Mar 2010 14:54:10 +0100 Subject: [PATCH] ItEr52S10AdaptacionServiciosRESTItEr51S10: Creation and Adapting of UnitTypeServiceRest. --- .../bootstrap/UnitTypeBootstrap.java | 3 +- .../business/materials/daos/IUnitTypeDAO.java | 4 +- .../business/materials/daos/UnitTypeDAO.java | 12 +- .../business/materials/entities/UnitType.java | 36 ++++ .../ws/materials/api/MaterialDTO.java | 3 - .../ws/materials/impl/MaterialConverter.java | 6 +- .../ws/unittypes/api/IUnitTypeService.java | 32 ++++ .../ws/unittypes/api/UnitTypeDTO.java | 60 +++++++ .../ws/unittypes/api/UnitTypeListDTO.java | 48 +++++ .../ws/unittypes/api/package-info.java | 28 +++ .../ws/unittypes/impl/UnitTypeConverter.java | 51 ++++++ .../unittypes/impl/UnitTypeServiceREST.java | 93 ++++++++++ .../navalplanner-webapp-spring-config.xml | 1 + .../ws/materials/MaterialServiceTest.java | 94 ++-------- .../ws/unittypes/UnitTypeServiceTest.java | 168 ++++++++++++++++++ scripts/rest-clients/export-unit-types.sh | 21 +++ scripts/rest-clients/import-unit-types.sh | 33 ++++ scripts/rest-clients/unit-types-sample.xml | 11 ++ 18 files changed, 618 insertions(+), 86 deletions(-) create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/IUnitTypeService.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeDTO.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeListDTO.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/package-info.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeConverter.java create mode 100644 navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeServiceREST.java create mode 100644 navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/unittypes/UnitTypeServiceTest.java create mode 100755 scripts/rest-clients/export-unit-types.sh create mode 100755 scripts/rest-clients/import-unit-types.sh create mode 100644 scripts/rest-clients/unit-types-sample.xml diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/materials/bootstrap/UnitTypeBootstrap.java b/navalplanner-business/src/main/java/org/navalplanner/business/materials/bootstrap/UnitTypeBootstrap.java index 7efd79b06..cca633b92 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/materials/bootstrap/UnitTypeBootstrap.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/materials/bootstrap/UnitTypeBootstrap.java @@ -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()); } diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/IUnitTypeDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/IUnitTypeDAO.java index 1bed2bdfd..f1474c0af 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/IUnitTypeDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/IUnitTypeDAO.java @@ -35,6 +35,8 @@ public interface IUnitTypeDAO extends IIntegrationEntityDAO { UnitType findByName(String measure) throws InstanceNotFoundException; - boolean existsUnitTypeByName(String measure); + UnitType findUniqueByNameInAnotherTransaction(String measure) + throws InstanceNotFoundException; + boolean existsUnitTypeByNameInAnotherTransaction(String measure); } diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/UnitTypeDAO.java b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/UnitTypeDAO.java index 96219409b..8ba4b5d82 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/UnitTypeDAO.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/materials/daos/UnitTypeDAO.java @@ -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 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) { diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/materials/entities/UnitType.java b/navalplanner-business/src/main/java/org/navalplanner/business/materials/entities/UnitType.java index 5ebcbe67e..daf011d8d 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/materials/entities/UnitType.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/materials/entities/UnitType.java @@ -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(); diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/api/MaterialDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/api/MaterialDTO.java index b9b618a32..e994fcb3b 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/api/MaterialDTO.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/api/MaterialDTO.java @@ -34,9 +34,6 @@ public class MaterialDTO extends IntegrationEntityDTO { public final static String ENTITY_TYPE = "material"; - @XmlAttribute - public String name; - @XmlAttribute public String description; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/impl/MaterialConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/impl/MaterialConverter.java index ae171ee34..80f89754d 100644 --- a/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/impl/MaterialConverter.java +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/materials/impl/MaterialConverter.java @@ -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) { diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/IUnitTypeService.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/IUnitTypeService.java new file mode 100644 index 000000000..09aa04c98 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/IUnitTypeService.java @@ -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 . + */ + +package org.navalplanner.ws.unittypes.api; + +import org.navalplanner.ws.common.api.InstanceConstraintViolationsListDTO; + +public interface IUnitTypeService { + + public UnitTypeListDTO getUnitTypes(); + + public InstanceConstraintViolationsListDTO addUnitTypes( + UnitTypeListDTO unitTypeListDTO); + +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeDTO.java new file mode 100644 index 000000000..16ba34fb8 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeDTO.java @@ -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 . + */ + +package org.navalplanner.ws.unittypes.api; + +import javax.xml.bind.annotation.XmlAttribute; + +import org.navalplanner.ws.common.api.IntegrationEntityDTO; + +/** + * DTO for UnitType entity. + * @author Susana Montes Pedreira + */ +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; + } + +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeListDTO.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeListDTO.java new file mode 100644 index 000000000..8623f7cc0 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/UnitTypeListDTO.java @@ -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 . + */ + +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 UnitType entities. + * @author Susana Montes Pedreira + */ +@XmlRootElement(name = "unit-type-list") +public class UnitTypeListDTO { + + @XmlElement(name = "unit-type") + public List unitTypeDTOs = new ArrayList(); + + public UnitTypeListDTO() { + } + + public UnitTypeListDTO(List unitTypeDTOs) { + if (unitTypeDTOs != null) { + this.unitTypeDTOs = unitTypeDTOs; + } + } + +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/package-info.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/package-info.java new file mode 100644 index 000000000..6190da8ee --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/api/package-info.java @@ -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 . + */ + +/** + * Specification of namespace for REST-based services. + * @author Susana Montes Pedreira + */ +@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; diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeConverter.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeConverter.java new file mode 100644 index 000000000..a7485b5f4 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeConverter.java @@ -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 . + */ + +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 + */ +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)); + } + +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeServiceREST.java b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeServiceREST.java new file mode 100644 index 000000000..a3c69bcc7 --- /dev/null +++ b/navalplanner-webapp/src/main/java/org/navalplanner/ws/unittypes/impl/UnitTypeServiceREST.java @@ -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 . + */ + +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 IUnitTypeService. + * @author Susana Montes Pedreira + */ +@Path("/unittypes/") +@Produces("application/xml") +@Service("unitTypeServiceREST") +public class UnitTypeServiceREST extends + GenericRESTService 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 getIntegrationEntityDAO() { + return unitTypeDAO; + } + + @Override + protected void updateEntity(UnitType entity, UnitTypeDTO entityDTO) + throws ValidationException { + + UnitTypeConverter.updateUnitType(entity, entityDTO); + + } + +} \ No newline at end of file diff --git a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml index d182fef5d..64a7c732c 100644 --- a/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml +++ b/navalplanner-webapp/src/main/resources/navalplanner-webapp-spring-config.xml @@ -74,6 +74,7 @@ + diff --git a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/materials/MaterialServiceTest.java b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/materials/MaterialServiceTest.java index 19abdc1c8..7124871d8 100644 --- a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/materials/MaterialServiceTest.java +++ b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/materials/MaterialServiceTest.java @@ -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(); - 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( diff --git a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/unittypes/UnitTypeServiceTest.java b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/unittypes/UnitTypeServiceTest.java new file mode 100644 index 000000000..2161d4989 --- /dev/null +++ b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/unittypes/UnitTypeServiceTest.java @@ -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 . + */ + +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 IUnitTypeService. + * @author Susana Montes Pedreira + */ +@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 unitTypeDTOs = new ArrayList(); + unitTypeDTOs.add(m1); + unitTypeDTOs.add(m2); + unitTypeDTOs.add(m3); + + UnitTypeListDTO unitTypeListDTO = createUnitTypeListDTO(m1, m2); + + List 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 unitTypeDTOs = new ArrayList(); + unitTypeDTOs.add(m1); + unitTypeDTOs.add(m2); + + UnitTypeListDTO unitTypeListDTO = createUnitTypeListDTO(m1, m2); + + List 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(); + 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 unitTypeList = new ArrayList(); + + for (UnitTypeDTO c : unitTypeDTOs) { + unitTypeList.add(c); + } + + return new UnitTypeListDTO(unitTypeList); + + } + +} \ No newline at end of file diff --git a/scripts/rest-clients/export-unit-types.sh b/scripts/rest-clients/export-unit-types.sh new file mode 100755 index 000000000..b721d9b57 --- /dev/null +++ b/scripts/rest-clients/export-unit-types.sh @@ -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 diff --git a/scripts/rest-clients/import-unit-types.sh b/scripts/rest-clients/import-unit-types.sh new file mode 100755 index 000000000..786767e3a --- /dev/null +++ b/scripts/rest-clients/import-unit-types.sh @@ -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 diff --git a/scripts/rest-clients/unit-types-sample.xml b/scripts/rest-clients/unit-types-sample.xml new file mode 100644 index 000000000..8d35bfc4b --- /dev/null +++ b/scripts/rest-clients/unit-types-sample.xml @@ -0,0 +1,11 @@ + + + + + + + + + + +