ItEr60S04ValidacionEProbasFuncionaisItEr59S04 : Fixing bug in CostCategory Service.

it showsthe error messages when the importation is incorrect.
This commit is contained in:
Susana Montes Pedreira 2010-06-30 12:54:42 +02:00 committed by Javier Moran Rua
parent f43b3f1c80
commit bf3f952de7
4 changed files with 61 additions and 21 deletions

View file

@ -142,6 +142,9 @@ public class HourCost extends IntegrationEntity {
@AssertTrue(message="The end date cannot be before the init date")
public boolean checkPositiveTimeInterval() {
if (initDate == null) {
return true;
}
if (endDate == null) {
return true;
}

View file

@ -1,9 +1,9 @@
package org.navalplanner.ws.costcategories.api;
import java.math.BigDecimal;
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.datatype.XMLGregorianCalendar;
import org.navalplanner.business.costcategories.entities.HourCost;
import org.navalplanner.ws.common.api.IntegrationEntityDTO;
@ -20,10 +20,10 @@ public class HourCostDTO extends IntegrationEntityDTO {
public BigDecimal priceCost;
@XmlAttribute
public Date initDate;
public XMLGregorianCalendar initDate;
@XmlAttribute
public Date endDate;
public XMLGregorianCalendar endDate;
@XmlAttribute(name = "work-hours-type")
public String type;
@ -31,8 +31,10 @@ public class HourCostDTO extends IntegrationEntityDTO {
public HourCostDTO() {
}
public HourCostDTO(String code, BigDecimal priceCost, Date initDate,
Date endDate, String type) {
public HourCostDTO(String code, BigDecimal priceCost,
XMLGregorianCalendar initDate,
XMLGregorianCalendar endDate,
String type) {
super(code);
this.initDate = initDate;
@ -46,7 +48,8 @@ public class HourCostDTO extends IntegrationEntityDTO {
* facilitate the implementation of test cases that add new instances (such
* instances will have a unique code).
*/
public HourCostDTO(BigDecimal priceCost, Date initDate, Date endDate,
public HourCostDTO(BigDecimal priceCost, XMLGregorianCalendar initDate,
XMLGregorianCalendar endDate,
String type) {
this(generateCode(), priceCost, initDate, endDate, type);

View file

@ -22,10 +22,11 @@ package org.navalplanner.ws.costcategories.impl;
import static org.navalplanner.web.I18nHelper._;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.xml.datatype.XMLGregorianCalendar;
import org.apache.commons.lang.StringUtils;
import org.joda.time.LocalDate;
import org.navalplanner.business.common.Registry;
@ -34,6 +35,7 @@ import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.costcategories.entities.CostCategory;
import org.navalplanner.business.costcategories.entities.HourCost;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
import org.navalplanner.ws.common.impl.DateConverter;
import org.navalplanner.ws.costcategories.api.CostCategoryDTO;
import org.navalplanner.ws.costcategories.api.HourCostDTO;
@ -65,15 +67,16 @@ public class CostCategoryConverter {
public final static HourCostDTO toDTO(HourCost hourCost) {
Date initDate = null;
XMLGregorianCalendar initDate = null;
if (hourCost.getInitDate() != null) {
initDate = hourCost.getInitDate().toDateTimeAtCurrentTime()
.toDate();
initDate = DateConverter.toXMLGregorianCalendar(hourCost
.getInitDate());
}
Date endDate = null;
XMLGregorianCalendar endDate = null;
if (hourCost.getEndDate() != null) {
endDate = hourCost.getEndDate().toDateTimeAtCurrentTime().toDate();
endDate = DateConverter.toXMLGregorianCalendar(hourCost
.getEndDate());
}
String type = null;
@ -108,15 +111,16 @@ public class CostCategoryConverter {
// Mandatory properties
LocalDate initDate = null;
if(hourCostDTO.initDate != null){
initDate = LocalDate.fromDateFields(hourCostDTO.initDate);
initDate = DateConverter.toLocalDate(hourCostDTO.initDate);
}
//Create new hour cost
HourCost hourCost = HourCost.createUnvalidated(hourCostDTO.code, hourCostDTO.priceCost, initDate);
HourCost hourCost = HourCost.createUnvalidated(hourCostDTO.code,
hourCostDTO.priceCost, initDate);
// optional properties
if (hourCostDTO.endDate != null) {
hourCost.setEndDate(LocalDate.fromDateFields(hourCostDTO.endDate));
hourCost.setEndDate(DateConverter.toLocalDate(hourCostDTO.endDate));
}
if (hourCostDTO.type != null) {
@ -168,7 +172,7 @@ public class CostCategoryConverter {
// Mandatory properties
LocalDate initDate = null;
if (hourCostDTO.initDate != null) {
initDate = LocalDate.fromDateFields(hourCostDTO.initDate);
initDate = DateConverter.toLocalDate(hourCostDTO.initDate);
}
// Create new hour cost
@ -176,7 +180,7 @@ public class CostCategoryConverter {
// optional properties
if (hourCostDTO.endDate != null) {
hourCost.setEndDate(LocalDate.fromDateFields(hourCostDTO.endDate));
hourCost.setEndDate(DateConverter.toLocalDate(hourCostDTO.endDate));
}
if (hourCostDTO.type != null) {

View file

@ -37,6 +37,8 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.xml.datatype.XMLGregorianCalendar;
import org.hibernate.SessionFactory;
import org.joda.time.LocalDate;
import org.junit.Test;
@ -49,6 +51,7 @@ import org.navalplanner.business.costcategories.entities.CostCategory;
import org.navalplanner.business.costcategories.entities.HourCost;
import org.navalplanner.business.costcategories.entities.TypeOfWorkHours;
import org.navalplanner.ws.common.api.InstanceConstraintViolationsDTO;
import org.navalplanner.ws.common.impl.DateConverter;
import org.navalplanner.ws.costcategories.api.CostCategoryDTO;
import org.navalplanner.ws.costcategories.api.CostCategoryListDTO;
import org.navalplanner.ws.costcategories.api.HourCostDTO;
@ -124,8 +127,13 @@ public class CostCategoryServiceTest {
// Valid cost category DTO with a hour cost
Set<HourCostDTO> cc3_HourCostDTOs = new HashSet<HourCostDTO>();
XMLGregorianCalendar initDate = DateConverter
.toXMLGregorianCalendar(new Date());
XMLGregorianCalendar endDate = initDate;
HourCostDTO cc3_1_HourCostDTO = new HourCostDTO(new BigDecimal(3),
new Date(), new Date(), typeOfWorkHoursCodeA);
initDate, endDate, typeOfWorkHoursCodeA);
cc3_HourCostDTOs.add(cc3_1_HourCostDTO);
CostCategoryDTO cc3 = new CostCategoryDTO("cc3", true, cc3_HourCostDTOs);
@ -177,8 +185,13 @@ public class CostCategoryServiceTest {
final String hourCostCode = "code-HC";
Set<HourCostDTO> cc1_HourCostDTOs = new HashSet<HourCostDTO>();
XMLGregorianCalendar initDate = DateConverter
.toXMLGregorianCalendar(new Date());
XMLGregorianCalendar endDate = initDate;
HourCostDTO cc1_1_HourCostDTO = new HourCostDTO(hourCostCode,new BigDecimal(3),
new Date(), new Date(), typeOfWorkHoursCodeA);
initDate, endDate, typeOfWorkHoursCodeA);
cc1_HourCostDTOs.add(cc1_1_HourCostDTO);
CostCategoryDTO cc1 = new CostCategoryDTO(costCategoryCode,"newCC1", true, cc1_HourCostDTOs);
@ -190,6 +203,7 @@ public class CostCategoryServiceTest {
hourCostDAO.flush();
/* Test. */
assertTrue(instanceConstraintViolationsList.toString(),
instanceConstraintViolationsList.size() == 0);
assertTrue(costCategoryDAO.existsByCode(cc1.code));
@ -197,6 +211,14 @@ public class CostCategoryServiceTest {
try {
costCategory = costCategoryDAO.findByCode(cc1.code);
assertTrue(costCategory.getHourCosts().size() == 1);
HourCost hourCost = hourCostDAO.findByCode(hourCostCode);
LocalDate currentDate = LocalDate.fromDateFields(new Date());
assertTrue(hourCost.getInitDate().compareTo(currentDate) == 0);
assertFalse(hourCost.getEndDate() == null);
assertTrue(hourCost.getEndDate().compareTo(hourCost.getInitDate()) == 0);
assertTrue(hourCost.getPriceCost().compareTo(new BigDecimal(3)) == 0);
assertTrue(hourCost.getType().getCode().equalsIgnoreCase(
typeOfWorkHoursCodeA));
} catch (InstanceNotFoundException e) {
assertTrue(false);
}
@ -206,8 +228,14 @@ public class CostCategoryServiceTest {
// Update the previous cost category
Set<HourCostDTO> cc2_HourCostDTOs = new HashSet<HourCostDTO>();
XMLGregorianCalendar initDate2 = DateConverter
.toXMLGregorianCalendar(new Date());
XMLGregorianCalendar endDate2 = DateConverter
.toXMLGregorianCalendar(getNextMonthDate());
HourCostDTO cc2_1_HourCostDTO = new HourCostDTO(hourCostCode,
new BigDecimal(100), getNextMonthDate(), getNextMonthDate(),
new BigDecimal(100), initDate2, endDate2,
typeOfWorkHoursCodeB);
cc2_HourCostDTOs.add(cc2_1_HourCostDTO);
CostCategoryDTO cc2 = new CostCategoryDTO(costCategoryCode,"updateCC1", false, cc2_HourCostDTOs);
@ -235,8 +263,10 @@ public class CostCategoryServiceTest {
HourCost hourCost = hourCostDAO.findByCode(cc1_1_HourCostDTO.code);
LocalDate nextMonthDate = LocalDate
.fromDateFields(getNextMonthDate());
assertTrue(hourCost.getInitDate().compareTo(nextMonthDate) == 0);
assertTrue(hourCost.getInitDate().compareTo(
LocalDate.fromDateFields(new Date())) == 0);
assertFalse(hourCost.getEndDate() == null);
assertTrue(hourCost.getEndDate().compareTo(nextMonthDate) == 0);
assertTrue(hourCost.getPriceCost().compareTo(new BigDecimal(100)) == 0);
assertTrue(hourCost.getType().getCode().equalsIgnoreCase(
typeOfWorkHoursCodeB));