ItEr09S12CUCreacioRecursoTraballadorItEr07S05: It keeps use case state in the model instead of the controller.
This commit is contained in:
parent
802d60f312
commit
9891482fee
6 changed files with 170 additions and 83 deletions
|
|
@ -0,0 +1,43 @@
|
||||||
|
package org.navalplanner.business.common.exceptions;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
|
import org.hibernate.validator.InvalidValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates some validation failure <br />
|
||||||
|
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||||
|
*/
|
||||||
|
public class ValidationException extends Exception {
|
||||||
|
|
||||||
|
private InvalidValue[] invalidValues;
|
||||||
|
|
||||||
|
public InvalidValue[] getInvalidValues() {
|
||||||
|
return invalidValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValidationException(InvalidValue[] invalidValues) {
|
||||||
|
super();
|
||||||
|
Validate.noNullElements(invalidValues);
|
||||||
|
this.invalidValues = invalidValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValidationException(InvalidValue[] invalidValues, String message,
|
||||||
|
Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
Validate.noNullElements(invalidValues);
|
||||||
|
this.invalidValues = invalidValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValidationException(InvalidValue[] invalidValues, String message) {
|
||||||
|
super(message);
|
||||||
|
Validate.noNullElements(invalidValues);
|
||||||
|
this.invalidValues = invalidValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValidationException(InvalidValue[] invalidValues, Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
Validate.noNullElements(invalidValues);
|
||||||
|
this.invalidValues = invalidValues;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package org.navalplanner.web.resources;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||||
import org.navalplanner.business.resources.entities.Worker;
|
import org.navalplanner.business.resources.entities.Worker;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -10,10 +11,14 @@ import org.navalplanner.business.resources.entities.Worker;
|
||||||
*/
|
*/
|
||||||
public interface IWorkerModel {
|
public interface IWorkerModel {
|
||||||
|
|
||||||
Worker createNewInstance();
|
void save() throws ValidationException;
|
||||||
|
|
||||||
void save(Worker worker);
|
|
||||||
|
|
||||||
List<Worker> getWorkers();
|
List<Worker> getWorkers();
|
||||||
|
|
||||||
|
Worker getWorker();
|
||||||
|
|
||||||
|
void prepareForCreate();
|
||||||
|
|
||||||
|
void prepareEditFor(Worker worker);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -2,10 +2,8 @@ package org.navalplanner.web.resources;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.hibernate.validator.ClassValidator;
|
|
||||||
import org.hibernate.validator.InvalidValue;
|
import org.hibernate.validator.InvalidValue;
|
||||||
|
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||||
import org.navalplanner.business.resources.entities.Worker;
|
import org.navalplanner.business.resources.entities.Worker;
|
||||||
import org.navalplanner.web.common.IMessagesForUser;
|
import org.navalplanner.web.common.IMessagesForUser;
|
||||||
import org.navalplanner.web.common.Level;
|
import org.navalplanner.web.common.Level;
|
||||||
|
|
@ -22,12 +20,6 @@ import org.zkoss.zul.api.Window;
|
||||||
*/
|
*/
|
||||||
public class WorkerCRUDController extends GenericForwardComposer {
|
public class WorkerCRUDController extends GenericForwardComposer {
|
||||||
|
|
||||||
private static final Log LOG = LogFactory
|
|
||||||
.getLog(WorkerCRUDController.class);
|
|
||||||
|
|
||||||
private ClassValidator<Worker> workerValidator = new ClassValidator<Worker>(
|
|
||||||
Worker.class);
|
|
||||||
|
|
||||||
private Window createWindow;
|
private Window createWindow;
|
||||||
|
|
||||||
private Window listWindow;
|
private Window listWindow;
|
||||||
|
|
@ -36,8 +28,6 @@ public class WorkerCRUDController extends GenericForwardComposer {
|
||||||
|
|
||||||
private IWorkerModel workerModel;
|
private IWorkerModel workerModel;
|
||||||
|
|
||||||
private Worker worker;
|
|
||||||
|
|
||||||
private OnlyOneVisible visibility;
|
private OnlyOneVisible visibility;
|
||||||
|
|
||||||
private IMessagesForUser messages;
|
private IMessagesForUser messages;
|
||||||
|
|
@ -58,10 +48,7 @@ public class WorkerCRUDController extends GenericForwardComposer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Worker getWorker() {
|
public Worker getWorker() {
|
||||||
if (worker == null) {
|
return workerModel.getWorker();
|
||||||
worker = workerModel.createNewInstance();
|
|
||||||
}
|
|
||||||
return worker;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Worker> getWorkers() {
|
public List<Worker> getWorkers() {
|
||||||
|
|
@ -69,35 +56,30 @@ public class WorkerCRUDController extends GenericForwardComposer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save() {
|
public void save() {
|
||||||
InvalidValue[] invalidValues = workerValidator.getInvalidValues(worker);
|
try {
|
||||||
if (invalidValues.length > 0) {
|
workerModel.save();
|
||||||
for (InvalidValue invalidValue : invalidValues) {
|
getVisibility().showOnly(listWindow);
|
||||||
|
Util.reloadBindings(listWindow);
|
||||||
|
messages.showMessage(Level.INFO, "traballador gardado");
|
||||||
|
} catch (ValidationException e) {
|
||||||
|
for (InvalidValue invalidValue : e.getInvalidValues()) {
|
||||||
messages.invalidValue(invalidValue);
|
messages.invalidValue(invalidValue);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
workerModel.save(worker);
|
|
||||||
getVisibility().showOnly(listWindow);
|
|
||||||
Util.reloadBindings(listWindow);
|
|
||||||
messages.showMessage(Level.INFO, "traballador gardado");
|
|
||||||
worker = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancel() {
|
public void cancel() {
|
||||||
getVisibility().showOnly(listWindow);
|
getVisibility().showOnly(listWindow);
|
||||||
worker = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goToEditForm(Worker worker) {
|
public void goToEditForm(Worker worker) {
|
||||||
if (worker == null)
|
workerModel.prepareEditFor(worker);
|
||||||
throw new IllegalArgumentException("worker cannot be null");
|
|
||||||
this.worker = worker;
|
|
||||||
getVisibility().showOnly(editWindow);
|
getVisibility().showOnly(editWindow);
|
||||||
Util.reloadBindings(editWindow);
|
Util.reloadBindings(editWindow);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void goToCreateForm() {
|
public void goToCreateForm() {
|
||||||
worker = workerModel.createNewInstance();
|
workerModel.prepareForCreate();
|
||||||
getVisibility().showOnly(createWindow);
|
getVisibility().showOnly(createWindow);
|
||||||
Util.reloadBindings(createWindow);
|
Util.reloadBindings(createWindow);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,11 @@ package org.navalplanner.web.resources;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
|
import org.hibernate.validator.ClassValidator;
|
||||||
|
import org.hibernate.validator.InvalidValue;
|
||||||
|
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||||
|
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||||
import org.navalplanner.business.resources.entities.Worker;
|
import org.navalplanner.business.resources.entities.Worker;
|
||||||
import org.navalplanner.business.resources.services.ResourceService;
|
import org.navalplanner.business.resources.services.ResourceService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -13,21 +18,24 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
public class WorkerModel implements IWorkerModel {
|
public class WorkerModel implements IWorkerModel {
|
||||||
|
|
||||||
private final ResourceService resourceService;
|
private final ResourceService resourceService;
|
||||||
|
private Worker worker;
|
||||||
|
private ClassValidator<Worker> workerValidator;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public WorkerModel(ResourceService resourceService) {
|
public WorkerModel(ResourceService resourceService) {
|
||||||
if (resourceService == null)
|
if (resourceService == null)
|
||||||
throw new IllegalArgumentException("resourceService cannot be null");
|
throw new IllegalArgumentException("resourceService cannot be null");
|
||||||
this.resourceService = resourceService;
|
this.resourceService = resourceService;
|
||||||
|
this.workerValidator = new ClassValidator<Worker>(Worker.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Worker createNewInstance() {
|
public void save() throws ValidationException {
|
||||||
return new Worker();
|
InvalidValue[] invalidValues = workerValidator
|
||||||
}
|
.getInvalidValues(getWorker());
|
||||||
|
if (invalidValues.length > 0) {
|
||||||
@Override
|
throw new ValidationException(invalidValues);
|
||||||
public void save(Worker worker) {
|
}
|
||||||
resourceService.saveResource(worker);
|
resourceService.saveResource(worker);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,4 +44,24 @@ public class WorkerModel implements IWorkerModel {
|
||||||
return resourceService.getWorkers();
|
return resourceService.getWorkers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Worker getWorker() {
|
||||||
|
return worker;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareForCreate() {
|
||||||
|
worker = new Worker();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareEditFor(Worker worker) {
|
||||||
|
Validate.notNull(worker, "worker is not null");
|
||||||
|
try {
|
||||||
|
this.worker = (Worker) resourceService.findResource(worker.getId());
|
||||||
|
} catch (InstanceNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,24 @@
|
||||||
package org.navalplanner.web.resources;
|
package org.navalplanner.web.resources;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertEquals;
|
||||||
|
import static org.easymock.EasyMock.createMock;
|
||||||
|
import static org.easymock.EasyMock.createNiceMock;
|
||||||
|
import static org.easymock.EasyMock.expect;
|
||||||
|
import static org.easymock.EasyMock.isA;
|
||||||
|
import static org.easymock.EasyMock.replay;
|
||||||
|
import static org.easymock.EasyMock.same;
|
||||||
|
import static org.easymock.EasyMock.verify;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.hibernate.validator.ClassValidator;
|
|
||||||
import org.hibernate.validator.InvalidValue;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.navalplanner.business.resources.entities.Worker;
|
import org.navalplanner.business.resources.entities.Worker;
|
||||||
import org.navalplanner.web.common.IMessagesForUser;
|
import org.navalplanner.web.common.IMessagesForUser;
|
||||||
import org.navalplanner.web.common.Level;
|
import org.navalplanner.web.common.Level;
|
||||||
import org.zkoss.zul.api.Window;
|
import org.zkoss.zul.api.Window;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
|
||||||
import static junit.framework.Assert.assertFalse;
|
|
||||||
|
|
||||||
import static org.easymock.EasyMock.createMock;
|
|
||||||
import static org.easymock.EasyMock.createNiceMock;
|
|
||||||
import static org.easymock.EasyMock.expect;
|
|
||||||
import static org.easymock.EasyMock.expectLastCall;
|
|
||||||
import static org.easymock.EasyMock.isA;
|
|
||||||
import static org.easymock.EasyMock.replay;
|
|
||||||
import static org.easymock.EasyMock.same;
|
|
||||||
import static org.easymock.EasyMock.verify;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link WorkerCRUDController} <br />
|
* Tests for {@link WorkerCRUDController} <br />
|
||||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||||
|
|
@ -60,10 +55,10 @@ public class WorkerCRUDControllerTest {
|
||||||
workerModel, messagesForUser);
|
workerModel, messagesForUser);
|
||||||
replay(createWindow, listWindow, editWindow);
|
replay(createWindow, listWindow, editWindow);
|
||||||
// expectations
|
// expectations
|
||||||
expect(workerModel.createNewInstance()).andReturn(workerToReturn);
|
workerModel.prepareForCreate();
|
||||||
workerModel.save(workerToReturn);
|
expect(workerModel.getWorker()).andReturn(workerToReturn).anyTimes();
|
||||||
messagesForUser.showMessage(same(Level.INFO),
|
workerModel.save();
|
||||||
isA(String.class));
|
messagesForUser.showMessage(same(Level.INFO), isA(String.class));
|
||||||
replay(workerModel, messagesForUser);
|
replay(workerModel, messagesForUser);
|
||||||
// action
|
// action
|
||||||
workerCRUDController.goToCreateForm();
|
workerCRUDController.goToCreateForm();
|
||||||
|
|
@ -82,7 +77,8 @@ public class WorkerCRUDControllerTest {
|
||||||
Worker workerToReturn = new Worker();
|
Worker workerToReturn = new Worker();
|
||||||
// expectations
|
// expectations
|
||||||
WorkerCRUDController workerCRUDController = createControllerForModel(workerModel);
|
WorkerCRUDController workerCRUDController = createControllerForModel(workerModel);
|
||||||
expect(workerModel.createNewInstance()).andReturn(workerToReturn);
|
workerModel.prepareForCreate();
|
||||||
|
expect(workerModel.getWorker()).andReturn(workerToReturn).anyTimes();
|
||||||
expect(createWindow.setVisible(true)).andReturn(false);
|
expect(createWindow.setVisible(true)).andReturn(false);
|
||||||
expect(createWindow.setVisible(false)).andReturn(true);
|
expect(createWindow.setVisible(false)).andReturn(true);
|
||||||
expect(listWindow.setVisible(true)).andReturn(false);
|
expect(listWindow.setVisible(true)).andReturn(false);
|
||||||
|
|
@ -105,10 +101,12 @@ public class WorkerCRUDControllerTest {
|
||||||
"firstName", "surname", "nif", 4)));
|
"firstName", "surname", "nif", 4)));
|
||||||
// expectations
|
// expectations
|
||||||
expect(workerModel.getWorkers()).andReturn(workersToReturn);
|
expect(workerModel.getWorkers()).andReturn(workersToReturn);
|
||||||
|
workerModel.prepareEditFor(workersToReturn.get(0));
|
||||||
expect(editWindow.setVisible(true)).andReturn(false);
|
expect(editWindow.setVisible(true)).andReturn(false);
|
||||||
workerModel.save(workersToReturn.get(0));
|
expect(workerModel.getWorker()).andReturn(workersToReturn.get(0))
|
||||||
messagesForUser.showMessage(same(Level.INFO),
|
.anyTimes();
|
||||||
isA(String.class));
|
workerModel.save();
|
||||||
|
messagesForUser.showMessage(same(Level.INFO), isA(String.class));
|
||||||
replay(createWindow, listWindow, editWindow, workerModel,
|
replay(createWindow, listWindow, editWindow, workerModel,
|
||||||
messagesForUser);
|
messagesForUser);
|
||||||
// perform actions
|
// perform actions
|
||||||
|
|
@ -120,27 +118,4 @@ public class WorkerCRUDControllerTest {
|
||||||
verify(workerModel, editWindow, messagesForUser);
|
verify(workerModel, editWindow, messagesForUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testWorkerInvalid() {
|
|
||||||
IWorkerModel workerModel = createMock(IWorkerModel.class);
|
|
||||||
IMessagesForUser messages = createMock(IMessagesForUser.class);
|
|
||||||
WorkerCRUDController workerCRUDController = createControllerForModel(
|
|
||||||
workerModel, messages);
|
|
||||||
Worker workerToReturn = new Worker();
|
|
||||||
// expectations
|
|
||||||
expect(workerModel.createNewInstance()).andReturn(workerToReturn);
|
|
||||||
ClassValidator<Worker> workerValidator = new ClassValidator<Worker>(
|
|
||||||
Worker.class);
|
|
||||||
InvalidValue[] invalidValues = workerValidator
|
|
||||||
.getInvalidValues(workerToReturn);
|
|
||||||
assertFalse(invalidValues.length == 0);
|
|
||||||
messages.invalidValue(isA(InvalidValue.class));
|
|
||||||
expectLastCall().times(invalidValues.length);
|
|
||||||
replay(createWindow, listWindow, editWindow, workerModel, messages);
|
|
||||||
// perform actions
|
|
||||||
workerCRUDController.goToCreateForm();
|
|
||||||
workerCRUDController.save();
|
|
||||||
// verify
|
|
||||||
verify(messages);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,54 @@
|
||||||
|
package org.navalplanner.web.resources;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.createMock;
|
||||||
|
import static org.easymock.EasyMock.expect;
|
||||||
|
import static org.easymock.EasyMock.replay;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
|
||||||
|
import org.navalplanner.business.common.exceptions.ValidationException;
|
||||||
|
import org.navalplanner.business.resources.entities.Worker;
|
||||||
|
import org.navalplanner.business.resources.services.ResourceService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Some test cases for {@link WorkerModel}. <br />
|
||||||
|
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||||
|
*/
|
||||||
|
public class WorkerModelTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWorkerValid() throws ValidationException,
|
||||||
|
InstanceNotFoundException {
|
||||||
|
ResourceService resourceServiceMock = createMock(ResourceService.class);
|
||||||
|
WorkerModel workerModel = new WorkerModel(resourceServiceMock);
|
||||||
|
Worker workerToReturn = new Worker();
|
||||||
|
workerToReturn.setDailyHours(2);
|
||||||
|
workerToReturn.setFirstName("firstName");
|
||||||
|
workerToReturn.setSurname("surname");
|
||||||
|
workerToReturn.setNif("232344243");
|
||||||
|
// expectations
|
||||||
|
expect(resourceServiceMock.findResource(workerToReturn.getId()))
|
||||||
|
.andReturn(workerToReturn);
|
||||||
|
resourceServiceMock.saveResource(workerToReturn);
|
||||||
|
replay(resourceServiceMock);
|
||||||
|
// perform actions
|
||||||
|
workerModel.prepareEditFor(workerToReturn);
|
||||||
|
workerModel.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = ValidationException.class)
|
||||||
|
public void testWorkerInvalid() throws ValidationException,
|
||||||
|
InstanceNotFoundException {
|
||||||
|
ResourceService resourceServiceMock = createMock(ResourceService.class);
|
||||||
|
WorkerModel workerModel = new WorkerModel(resourceServiceMock);
|
||||||
|
Worker workerToReturn = new Worker();
|
||||||
|
// expectations
|
||||||
|
expect(resourceServiceMock.findResource(workerToReturn.getId()))
|
||||||
|
.andReturn(workerToReturn);
|
||||||
|
replay(resourceServiceMock);
|
||||||
|
// perform actions
|
||||||
|
workerModel.prepareEditFor(workerToReturn);
|
||||||
|
workerModel.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue