ItEr33S08ValidacionEProbasFuncionaisItEr32S09: The class for validating must be retrieved from the entity, since it wouldn't work with a superclass
This commit is contained in:
parent
4065850b9e
commit
fd3b521f44
3 changed files with 33 additions and 15 deletions
|
|
@ -85,7 +85,9 @@ public class GenericDAOHibernate<E, PK extends Serializable> implements
|
|||
getSession().saveOrUpdate(entity);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void checkIsValid(E entity) throws ValidationException {
|
||||
Class<E> entityClass = (Class<E>) entity.getClass();
|
||||
ClassValidator<E> classValidator = new ClassValidator<E>(entityClass);
|
||||
InvalidValue[] invalidValues = classValidator.getInvalidValues(entity);
|
||||
if (invalidValues.length > 0) {
|
||||
|
|
|
|||
|
|
@ -33,8 +33,6 @@ import java.util.Map;
|
|||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.hibernate.validator.ClassValidator;
|
||||
import org.hibernate.validator.InvalidValue;
|
||||
import org.navalplanner.business.calendars.daos.IBaseCalendarDAO;
|
||||
import org.navalplanner.business.calendars.entities.BaseCalendar;
|
||||
import org.navalplanner.business.calendars.entities.CalendarData;
|
||||
|
|
@ -79,15 +77,11 @@ public class WorkerModel implements IWorkerModel {
|
|||
PredefinedCriterionTypes.LEAVE,
|
||||
PredefinedCriterionTypes.WORK_RELATIONSHIP };
|
||||
private Worker worker;
|
||||
private ClassValidator<Worker> workerValidator;
|
||||
|
||||
private final ICriterionDAO criterionDAO;
|
||||
|
||||
private IMultipleCriterionActiveAssigner localizationsAssigner;
|
||||
|
||||
private ClassValidator<BaseCalendar> baseCalendarValidator = new ClassValidator<BaseCalendar>(
|
||||
BaseCalendar.class);
|
||||
|
||||
@Autowired
|
||||
@Qualifier("subclass")
|
||||
private IBaseCalendarModel baseCalendarModel;
|
||||
|
|
@ -101,7 +95,6 @@ public class WorkerModel implements IWorkerModel {
|
|||
Validate.notNull(resourceDAO);
|
||||
Validate.notNull(criterionDAO);
|
||||
this.resourceDAO = resourceDAO;
|
||||
this.workerValidator = new ClassValidator<Worker>(Worker.class);
|
||||
this.criterionDAO = criterionDAO;
|
||||
}
|
||||
|
||||
|
|
@ -111,11 +104,6 @@ public class WorkerModel implements IWorkerModel {
|
|||
if (worker.getCalendar() != null) {
|
||||
baseCalendarModel.checkInvalidValuesCalendar(worker.getCalendar());
|
||||
}
|
||||
InvalidValue[] invalidValues = workerValidator
|
||||
.getInvalidValues(getWorker());
|
||||
if (invalidValues.length > 0) {
|
||||
throw new ValidationException(invalidValues);
|
||||
}
|
||||
getLocalizationsAssigner().applyChanges();
|
||||
if(assignedCriterionsModel != null){
|
||||
assignedCriterionsModel.confirm();
|
||||
|
|
|
|||
|
|
@ -2,27 +2,46 @@ package org.navalplanner.web.resources;
|
|||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.expectLastCall;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
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 org.easymock.EasyMock;
|
||||
import org.easymock.IAnswer;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||
import org.navalplanner.business.resources.daos.ICriterionDAO;
|
||||
import org.navalplanner.business.resources.daos.IResourceDAO;
|
||||
import org.navalplanner.business.resources.entities.Criterion;
|
||||
import org.navalplanner.business.resources.entities.PredefinedCriterionTypes;
|
||||
import org.navalplanner.business.resources.entities.Resource;
|
||||
import org.navalplanner.business.resources.entities.Worker;
|
||||
import org.navalplanner.web.resources.worker.WorkerModel;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Some test cases for {@link WorkerModel}. <br />
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE,
|
||||
WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE })
|
||||
@Transactional
|
||||
public class WorkerModelTest {
|
||||
|
||||
@Autowired
|
||||
private IResourceDAO resourceDAO;
|
||||
|
||||
@Test
|
||||
public void testWorkerValid() throws ValidationException,
|
||||
InstanceNotFoundException {
|
||||
|
|
@ -58,15 +77,24 @@ public class WorkerModelTest {
|
|||
|
||||
IResourceDAO resourceDAOMock = createMock(IResourceDAO.class);
|
||||
ICriterionDAO criterionServiceMock = createMock(ICriterionDAO.class);
|
||||
Worker workerToReturn = Worker.create();
|
||||
final Worker workerToReturn = Worker.create();
|
||||
// expectations
|
||||
List<Criterion> criterions = new ArrayList<Criterion>();
|
||||
expect(
|
||||
criterionServiceMock
|
||||
.findByType(PredefinedCriterionTypes.LOCATION_GROUP))
|
||||
.andReturn(criterions).anyTimes();
|
||||
expect(resourceDAOMock.find(workerToReturn.getId()))
|
||||
.andReturn(workerToReturn);
|
||||
expect(resourceDAOMock.find(workerToReturn.getId())).andReturn(
|
||||
workerToReturn);
|
||||
resourceDAOMock.save(workerToReturn);
|
||||
expectLastCall().andAnswer(new IAnswer<Object>() {
|
||||
@Override
|
||||
public Object answer() throws Throwable {
|
||||
Resource argument = (Resource) EasyMock.getCurrentArguments()[0];
|
||||
resourceDAO.save(argument);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
replay(resourceDAOMock, criterionServiceMock);
|
||||
// perform actions
|
||||
WorkerModel workerModel = new WorkerModel(resourceDAOMock,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue