ItEr39S17CUImportacionOrganizacionsTraballo: Adding properly messages and checks about mandatory attributes to Order.

This commit is contained in:
Manuel Rego Casasnovas 2009-12-17 19:09:35 +01:00 committed by Javier Moran Rua
parent 2660c57c1e
commit e2241b40ca
7 changed files with 207 additions and 10 deletions

View file

@ -22,6 +22,7 @@ package org.navalplanner.business.common;
import org.navalplanner.business.advance.daos.IAdvanceTypeDAO;
import org.navalplanner.business.calendars.daos.IBaseCalendarDAO;
import org.navalplanner.business.common.daos.IConfigurationDAO;
import org.navalplanner.business.costcategories.daos.ITypeOfWorkHoursDAO;
import org.navalplanner.business.labels.daos.ILabelDAO;
import org.navalplanner.business.labels.daos.ILabelTypeDAO;
@ -88,6 +89,9 @@ public class Registry {
@Autowired
private ILabelTypeDAO labelTypeDAO;
@Autowired
private IConfigurationDAO configurationDAO;
private Registry() {
}
@ -143,4 +147,8 @@ public class Registry {
return getInstance().labelTypeDAO;
}
public static IConfigurationDAO getConfigurationDAO() {
return getInstance().configurationDAO;
}
}

View file

@ -63,7 +63,7 @@ public class Order extends OrderLineGroup {
private Boolean dependenciesConstraintsHavePriority;
@NotNull
@NotNull(message = "order calendar not specified")
private BaseCalendar calendar;
public String getResponsible() {
@ -118,19 +118,19 @@ public class Order extends OrderLineGroup {
@SuppressWarnings("unused")
@AssertTrue(message = "the order must have a init date")
private boolean theOrderMustHaveStartDate() {
private boolean checkConstraintOrderMustHaveStartDate() {
return getInitDate() != null;
}
@SuppressWarnings("unused")
@AssertTrue(message = "deadline must be after start date")
private boolean theDeadlineMustBeAfterStart() {
private boolean checkConstraintDeadlineMustBeAfterStart() {
return !this.isDeadlineBeforeStart();
}
@SuppressWarnings("unused")
@AssertTrue(message = "At least one HoursGroup is needed for each OrderElement")
private boolean atLeastOneHoursGroupForEachOrderElement() {
private boolean checkConstraintAtLeastOneHoursGroupForEachOrderElement() {
for (OrderElement orderElement : this.getOrderElements()) {
if (!orderElement.checkAtLeastOneHoursGroup()) {
return false;

View file

@ -54,7 +54,7 @@ import org.navalplanner.business.requirements.entities.IndirectCriterionRequirem
import org.navalplanner.business.resources.entities.Criterion;
public abstract class OrderElement extends BaseEntity {
@NotEmpty(message = "name not specified")
private String name;
private Date initDate;
@ -73,6 +73,7 @@ public abstract class OrderElement extends BaseEntity {
private Set<Label> labels = new HashSet<Label>();
@NotEmpty(message = "code not specified")
private String code;
private Set<CriterionRequirement> criterionRequirements = new HashSet<CriterionRequirement>();
@ -243,7 +244,6 @@ public abstract class OrderElement extends BaseEntity {
public abstract List<HoursGroup> getHoursGroups();
@NotEmpty
public String getName() {
return name;
}
@ -334,7 +334,6 @@ public abstract class OrderElement extends BaseEntity {
this.code = code;
}
@NotEmpty
public String getCode() {
return code;
}

View file

@ -21,6 +21,7 @@
package org.navalplanner.ws.orders.api;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.xml.bind.annotation.XmlElement;
@ -39,7 +40,7 @@ public class OrderLineDTO extends OrderElementDTO {
@XmlElementWrapper(name = "hours-groups")
@XmlElement(name = "hours-group")
public Set<HoursGroupDTO> hoursGroups;
public Set<HoursGroupDTO> hoursGroups = new HashSet<HoursGroupDTO>();
public OrderLineDTO() {
super();

View file

@ -20,6 +20,7 @@
package org.navalplanner.ws.orders.api;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
@ -44,7 +45,7 @@ public class OrderLineGroupDTO extends OrderElementDTO {
@XmlElement(name = "order-line", type = OrderLineDTO.class),
@XmlElement(name = "order-line-group", type = OrderLineGroupDTO.class),
@XmlElement(name = "order", type = OrderDTO.class) })
public List<OrderElementDTO> children;
public List<OrderElementDTO> children = new ArrayList<OrderElementDTO>();
public OrderLineGroupDTO() {
super();

View file

@ -153,11 +153,17 @@ public final class OrderElementConverter {
((Order) orderElement)
.setDependenciesConstraintsHavePriority(((OrderDTO) orderElementDTO).dependenciesConstraintsHavePriority);
List<BaseCalendar> calendars = Registry.getBaseCalendarDAO()
.findByName(((OrderDTO) orderElementDTO).calendarName);
BaseCalendar calendar;
if ((calendars != null) && (calendars.size() == 1)) {
((Order) orderElement).setCalendar(calendars.get(0));
calendar = calendars.get(0);
} else {
calendar = Registry.getConfigurationDAO()
.getConfiguration().getDefaultCalendar();
}
((Order) orderElement).setCalendar(calendar);
} else { // orderElementDTO instanceof OrderLineGroupDTO
orderElement = OrderLineGroup.create();
}

View file

@ -0,0 +1,182 @@
/*
* This file is part of ###PROJECT_NAME###
*
* 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.orders;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
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.Date;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.navalplanner.business.IDataBootstrap;
import org.navalplanner.business.orders.daos.IOrderDAO;
import org.navalplanner.business.orders.daos.IOrderElementDAO;
import org.navalplanner.ws.common.api.ConstraintViolationDTO;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
import org.navalplanner.ws.orders.api.IOrderElementService;
import org.navalplanner.ws.orders.api.OrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Tests for {@link IOrderElementService}.
*
* @author Manuel Rego Casasnovas <mrego@igalia.com>
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE })
@Transactional
public class OrderElementServiceTest {
@Resource
private IDataBootstrap defaultAdvanceTypesBootstrapListener;
@Resource
private IDataBootstrap configurationBootstrap;
@Before
public void loadRequiredaData() {
defaultAdvanceTypesBootstrapListener.loadRequiredData();
configurationBootstrap.loadRequiredData();
}
@Autowired
private IOrderElementService orderElementService;
@Autowired
private IOrderDAO orderDAO;
@Autowired
private IOrderElementDAO orderElementDAO;
@Test
public void invalidOrderWithoutAttributes() {
int previous = orderDAO.getOrders().size();
OrderDTO orderDTO = new OrderDTO();
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = orderElementService
.addOrder(orderDTO).instanceConstraintViolationsList;
assertThat(instanceConstraintViolationsList.size(), equalTo(1));
List<ConstraintViolationDTO> constraintViolations = instanceConstraintViolationsList
.get(0).constraintViolations;
// Mandatory fields: code, name. Check constraints:
// checkConstraintOrderMustHaveStartDate
assertThat(constraintViolations.size(), equalTo(3));
assertThat(orderDAO.getOrders().size(), equalTo(previous));
}
@Test
public void invalidOrderWithoutNameAndInitDate() {
int previous = orderDAO.getOrders().size();
OrderDTO orderDTO = new OrderDTO();
orderDTO.code = "order-code";
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = orderElementService
.addOrder(orderDTO).instanceConstraintViolationsList;
assertThat(instanceConstraintViolationsList.size(), equalTo(1));
List<ConstraintViolationDTO> constraintViolations = instanceConstraintViolationsList
.get(0).constraintViolations;
// Mandatory fields: name. Check constraints:
// checkConstraintOrderMustHaveStartDate
assertThat(constraintViolations.size(), equalTo(2));
assertThat(orderDAO.getOrders().size(), equalTo(previous));
}
@Test
public void invalidOrderWithoutCodeAndInitDate() {
int previous = orderDAO.getOrders().size();
OrderDTO orderDTO = new OrderDTO();
orderDTO.name = "Order name";
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = orderElementService
.addOrder(orderDTO).instanceConstraintViolationsList;
assertThat(instanceConstraintViolationsList.size(), equalTo(1));
List<ConstraintViolationDTO> constraintViolations = instanceConstraintViolationsList
.get(0).constraintViolations;
// Mandatory fields: code. Check constraints:
// checkConstraintOrderMustHaveStartDate
assertThat(constraintViolations.size(), equalTo(2));
assertThat(orderDAO.getOrders().size(), equalTo(previous));
}
@Test
public void invalidOrderWithoutCodeAndName() {
int previous = orderDAO.getOrders().size();
OrderDTO orderDTO = new OrderDTO();
orderDTO.initDate = new Date();
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = orderElementService
.addOrder(orderDTO).instanceConstraintViolationsList;
assertThat(instanceConstraintViolationsList.size(), equalTo(1));
List<ConstraintViolationDTO> constraintViolations = instanceConstraintViolationsList
.get(0).constraintViolations;
// Mandatory fields: code, name
assertThat(constraintViolations.size(), equalTo(2));
for (ConstraintViolationDTO constraintViolationDTO : constraintViolations) {
assertThat(constraintViolationDTO.fieldName, anyOf(
equalTo("Order::code"), equalTo("Order::name")));
}
assertThat(orderDAO.getOrders().size(), equalTo(previous));
}
@Test
public void validOrder() {
String code = "order-code";
int previous = orderElementDAO.findByCode(code).size();
OrderDTO orderDTO = new OrderDTO();
orderDTO.name = "Order name";
orderDTO.code = code;
orderDTO.initDate = new Date();
List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList = orderElementService
.addOrder(orderDTO).instanceConstraintViolationsList;
assertThat(instanceConstraintViolationsList.size(), equalTo(0));
assertThat(orderElementDAO.findByCode(code).size(),
equalTo(previous + 1));
}
}