From 56337460f9999857649e1b7b57f741aa0c873ebe Mon Sep 17 00:00:00 2001 From: Fernando Bellas Permuy Date: Wed, 13 Jan 2010 20:02:17 +0100 Subject: [PATCH] ItEr43S08CUImportacionRecursosProductivosItEr42S12: checkConstraintPositiveTimeInterval added to CriterionSatisfaction. This constraint checks that interval of a CriterionSatisfaction is positive. A test case has also been provided. --- .../entities/CriterionSatisfaction.java | 31 +++++++++++++++++-- .../ws/resources/api/ResourceServiceTest.java | 23 +++++++++++++- scripts/rest-clients/resources-sample.xml | 11 +++++++ 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/navalplanner-business/src/main/java/org/navalplanner/business/resources/entities/CriterionSatisfaction.java b/navalplanner-business/src/main/java/org/navalplanner/business/resources/entities/CriterionSatisfaction.java index 64344904a..d540fc3e5 100644 --- a/navalplanner-business/src/main/java/org/navalplanner/business/resources/entities/CriterionSatisfaction.java +++ b/navalplanner-business/src/main/java/org/navalplanner/business/resources/entities/CriterionSatisfaction.java @@ -28,6 +28,7 @@ import java.util.Date; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.Validate; import org.apache.commons.lang.builder.ToStringBuilder; +import org.hibernate.validator.AssertTrue; import org.hibernate.validator.NotNull; import org.navalplanner.business.common.BaseEntity; import org.navalplanner.business.common.Registry; @@ -274,17 +275,41 @@ public class CriterionSatisfaction extends BaseEntity { } } + /* + * IMPORTANT: This method currently redefines BaseEntity::validate avoiding + * Hibernate Validator to run. This should be fixed in a future. + */ public void validate(){ Validate.notNull(resource); Validate.notNull(startDate); Validate.notNull(criterion); - Validate.isTrue(finishDate == null || finishDate.after(startDate)); - Validate.isTrue(finishDate == null || startDate.equals(finishDate) - || startDate.before(finishDate)); + Validate.isTrue(checkConstraintPositiveTimeInterval()); } public ResourceEnum getResourceType() { return criterion.getType().getResource(); } + @AssertTrue(message="criterion satisfaction with end date less than start " + + "date") + public boolean checkConstraintPositiveTimeInterval() { + + /* Check if it makes sense to check the constraint .*/ + if (!isStartDateSpecified()) { + return true; + } + + /* Check the constraint. */ + if (finishDate == null) { + return true; + } + + return (finishDate.after(startDate) || startDate.equals(finishDate)); + + } + + public boolean isStartDateSpecified() { + return startDate != null; + } + } diff --git a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/resources/api/ResourceServiceTest.java b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/resources/api/ResourceServiceTest.java index d1cda2924..633100b08 100644 --- a/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/resources/api/ResourceServiceTest.java +++ b/navalplanner-webapp/src/test/java/org/navalplanner/web/test/ws/resources/api/ResourceServiceTest.java @@ -291,7 +291,7 @@ public class ResourceServiceTest { @Test @NotTransactional - public void testAddResourceWithCriterionSatisfactionsWithoutStartDate() { + public void testAddResourceWithCriterionSatisfactionWithoutStartDate() { /* Create a criterion type. */ CriterionType ct = createCriterionType(); @@ -310,6 +310,27 @@ public class ResourceServiceTest { } + @Test + @NotTransactional + public void testAddResourceWithCriterionSatisfactionWithNegativeInterval() { + + /* Create a criterion type. */ + CriterionType ct = createCriterionType(); + + /* Create a machine DTO. */ + MachineDTO machineDTO = new MachineDTO(getUniqueName(), "name", "desc"); + machineDTO.criterionSatisfactions.add( + new CriterionSatisfactionDTO(ct.getName() , "c1", + getDate(2000, 2, 1), getDate(2000, 1, 1))); + + /* Test. */ + assertOneConstraintViolation( + resourceService.addResources(createResourceListDTO(machineDTO))); + assertFalse(machineDAO.existsMachineWithCodeInAnotherTransaction( + machineDTO.code)); + + } + @Test @NotTransactional public void testAddResourcesWithCriterionSatisfactionsWithIncorrectType() { diff --git a/scripts/rest-clients/resources-sample.xml b/scripts/rest-clients/resources-sample.xml index dba584c89..9d17305f8 100644 --- a/scripts/rest-clients/resources-sample.xml +++ b/scripts/rest-clients/resources-sample.xml @@ -186,4 +186,15 @@ + + + + + + +