From 70abea2b64f38aed43b2b9967b70ca23bf6b93c7 Mon Sep 17 00:00:00 2001 From: Damian Palpacelli Date: Tue, 10 Mar 2015 10:47:04 -0300 Subject: [PATCH] Change exception by responder to inform validation error error: worker already exists --- .../requestor/impl/RequestBuilderImpl.java | 5 +-- .../worker/add/AddWorkerInteractor.java | 32 ++++++++--------- .../usecases/worker/add/AddWorkerRequest.java | 22 +++++++----- .../worker/add/AddWorkerRequestBuilder.java | 2 +- .../worker/add/AddWorkerResponder.java | 5 +++ .../requestor/RequestBuilderTest.java | 10 ++++-- .../worker/add/AddWorkerInteractorTest.java | 7 ++-- .../worker/add/AddWorkerResponderSpy.java | 15 ++++++++ .../rest/controllers/AddWorkerController.java | 34 ++++++++++--------- .../standalone/AddWorkerCtrlTest.java | 10 +++--- 10 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerResponder.java create mode 100644 core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerResponderSpy.java diff --git a/core/usecases/src/main/java/org/reparationservice/requestor/impl/RequestBuilderImpl.java b/core/usecases/src/main/java/org/reparationservice/requestor/impl/RequestBuilderImpl.java index 46e23ae..ec71e97 100644 --- a/core/usecases/src/main/java/org/reparationservice/requestor/impl/RequestBuilderImpl.java +++ b/core/usecases/src/main/java/org/reparationservice/requestor/impl/RequestBuilderImpl.java @@ -3,10 +3,11 @@ import org.reparationservice.requestor.RequestBuilder; import org.reparationservice.requestor.UseCaseRequest; import org.reparationservice.usecases.worker.add.AddWorkerRequest; +import org.reparationservice.usecases.worker.add.AddWorkerResponder; public class RequestBuilderImpl implements RequestBuilder { @Override - public UseCaseRequest buildAddWorkerRequest(String username) { - return new AddWorkerRequest(username); + public UseCaseRequest buildAddWorkerRequest(String username, AddWorkerResponder responder) { + return new AddWorkerRequest(username, responder); } } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerInteractor.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerInteractor.java index d9b9270..5f5fcaf 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerInteractor.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerInteractor.java @@ -7,25 +7,21 @@ import org.reparationservice.requestor.UseCaseRequest; public final class AddWorkerInteractor implements UseCaseActivator { - private final WorkerGateway workers; - private final UseCaseRequest request; + private final WorkerGateway workers; + private final AddWorkerRequest request; - public AddWorkerInteractor(WorkerGateway workers, UseCaseRequest request) { - this.workers = workers; - this.request = request; - } + public AddWorkerInteractor(WorkerGateway workers, UseCaseRequest request) { + this.workers = workers; + this.request = (AddWorkerRequest) request; + } - @Override - public void execute() { - String workerUserName = ((AddWorkerRequest) this.request).getUserName(); - - if (workers.getWorkerByUserName(workerUserName) != Worker.NULL) - throw new WorkerAlreadyExists(); - - workers.addWorker(new WorkerDTO(workerUserName)); - } + @Override + public void execute() { + String workerUserName = request.getUserName(); - public class WorkerAlreadyExists extends RuntimeException { - private static final long serialVersionUID = 3471396866818354971L; - } + if (workers.getWorkerByUserName(workerUserName) != Worker.NULL) + request.workerAlreadyExists(); + else + workers.addWorker(new WorkerDTO(workerUserName)); + } } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequest.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequest.java index 096f61d..d377950 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequest.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequest.java @@ -2,14 +2,20 @@ import org.reparationservice.requestor.UseCaseRequest; -public class AddWorkerRequest extends UseCaseRequest { - private String username; +public class AddWorkerRequest extends UseCaseRequest implements AddWorkerResponder { + private final AddWorkerResponder responder; + private final String username; - public AddWorkerRequest(String username) { - this.username = username; - } + public AddWorkerRequest(String username, AddWorkerResponder responder) { + this.username = username; + this.responder = responder; + } - public String getUserName() { - return username; - } + public String getUserName() { + return username; + } + + @Override public void workerAlreadyExists() { + responder.workerAlreadyExists(); + } } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequestBuilder.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequestBuilder.java index 460d127..7651087 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequestBuilder.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerRequestBuilder.java @@ -3,5 +3,5 @@ import org.reparationservice.requestor.UseCaseRequest; public interface AddWorkerRequestBuilder { - UseCaseRequest buildAddWorkerRequest(String username); + UseCaseRequest buildAddWorkerRequest(String username, AddWorkerResponder responder); } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerResponder.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerResponder.java new file mode 100644 index 0000000..ce0bdd4 --- /dev/null +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/add/AddWorkerResponder.java @@ -0,0 +1,5 @@ +package org.reparationservice.usecases.worker.add; + +public interface AddWorkerResponder { + void workerAlreadyExists(); +} diff --git a/core/usecases/src/test/java/org/reparationservice/requestor/RequestBuilderTest.java b/core/usecases/src/test/java/org/reparationservice/requestor/RequestBuilderTest.java index 2e68d1d..e8d72d6 100644 --- a/core/usecases/src/test/java/org/reparationservice/requestor/RequestBuilderTest.java +++ b/core/usecases/src/test/java/org/reparationservice/requestor/RequestBuilderTest.java @@ -7,6 +7,8 @@ import org.reparationservice.requestor.impl.RequestBuilderImpl; import org.reparationservice.usecases.worker.add.AddWorkerRequest; import org.reparationservice.usecases.worker.add.AddWorkerRequestBuilder; +import org.reparationservice.usecases.worker.add.AddWorkerResponder; +import org.reparationservice.usecases.worker.add.AddWorkerResponderSpy; public class RequestBuilderTest { private static final String WORKER_USERNAME = "username"; @@ -14,8 +16,12 @@ public class RequestBuilderTest { @Test public void buildAddWorkerRequest() { AddWorkerRequestBuilder builder = new RequestBuilderImpl(); - UseCaseRequest request = builder.buildAddWorkerRequest(WORKER_USERNAME); + AddWorkerResponder responder = new AddWorkerResponderSpy(); + UseCaseRequest request = builder.buildAddWorkerRequest(WORKER_USERNAME, responder); assertThat(request).isInstanceOf(AddWorkerRequest.class); - assertThat(((AddWorkerRequest) request).getUserName()).isEqualTo(WORKER_USERNAME); + AddWorkerRequest addWorkerReq = (AddWorkerRequest) request; + assertThat(addWorkerReq.getUserName()).isEqualTo(WORKER_USERNAME); + addWorkerReq.workerAlreadyExists(); + assertThat(((AddWorkerResponderSpy) responder).workerAlreadyExistsWasCalled()).isTrue(); } } diff --git a/core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerInteractorTest.java b/core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerInteractorTest.java index 0fb1674..b78988b 100644 --- a/core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerInteractorTest.java +++ b/core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerInteractorTest.java @@ -14,11 +14,12 @@ public class AddWorkerInteractorTest { private static final String WORKER_USERNAME = "username"; private UseCaseActivator addWorker; private WorkerGatewaySpy workersSpy; + private AddWorkerResponder responder = new AddWorkerResponderSpy(); @Before public void givenAddWorkerInteractor() { workersSpy = new WorkerGatewaySpy(); - UseCaseRequest request = new AddWorkerRequest(WORKER_USERNAME); + UseCaseRequest request = new AddWorkerRequest(WORKER_USERNAME, responder); addWorker = new AddWorkerInteractor(workersSpy, request); } @@ -35,9 +36,11 @@ public void executeAddOperation() { assertThat(worker.getUserName()).isEqualTo(WORKER_USERNAME); } - @Test(expected = AddWorkerInteractor.WorkerAlreadyExists.class) + @Test//(expected = AddWorkerInteractor.WorkerAlreadyExists.class) public void throwWorkerAlreadyExistsWhenTwoWorkersWithSameUserNameAdded() { addWorker.execute(); addWorker.execute(); + assertThat(((AddWorkerResponderSpy) responder).workerAlreadyExistsWasCalled()).isTrue(); } + } diff --git a/core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerResponderSpy.java b/core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerResponderSpy.java new file mode 100644 index 0000000..7682d89 --- /dev/null +++ b/core/usecases/src/test/java/org/reparationservice/usecases/worker/add/AddWorkerResponderSpy.java @@ -0,0 +1,15 @@ +package org.reparationservice.usecases.worker.add; + +public class AddWorkerResponderSpy implements AddWorkerResponder { + + private boolean wasCalled = false; + + @Override + public void workerAlreadyExists() { + wasCalled = true; + } + + public boolean workerAlreadyExistsWasCalled() { + return wasCalled; + } +} diff --git a/web-interface/src/main/java/org/reparationservice/rest/controllers/AddWorkerController.java b/web-interface/src/main/java/org/reparationservice/rest/controllers/AddWorkerController.java index 3cb5295..69d2e0c 100644 --- a/web-interface/src/main/java/org/reparationservice/rest/controllers/AddWorkerController.java +++ b/web-interface/src/main/java/org/reparationservice/rest/controllers/AddWorkerController.java @@ -4,20 +4,16 @@ import org.reparationservice.requestor.UseCaseActivator; import org.reparationservice.requestor.UseCaseRequest; import org.reparationservice.rest.requests.AddWorkerJsonRequest; -import org.reparationservice.usecases.worker.add.AddWorkerInteractor; import org.reparationservice.usecases.worker.add.AddWorkerInteractorFactory; import org.reparationservice.usecases.worker.add.AddWorkerRequestBuilder; +import org.reparationservice.usecases.worker.add.AddWorkerResponder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.VndErrors; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @RestController @@ -37,22 +33,28 @@ public AddWorkerController(AddWorkerInteractorFactory intFactory, @RequestMapping(value = "/workers", method = RequestMethod.POST) ResponseEntity addWorker(@RequestBody AddWorkerJsonRequest workerReq) { + + AddWorkerResponder responder = new AddWorkerPresenter(); UseCaseRequest request = requestBuilder - .buildAddWorkerRequest(workerReq.getUsername()); + .buildAddWorkerRequest(workerReq.getUsername(), responder); + UseCaseActivator interactor = intFactory.makeAddWorkerInteractor(workers, request); interactor.execute(); - return new ResponseEntity<>(HttpStatus.CREATED); + + return ((AddWorkerPresenter) responder).getResponse(); } - @ControllerAdvice - public static class AddWorkerControllerAdvice { - @ResponseBody - @ExceptionHandler(AddWorkerInteractor.WorkerAlreadyExists.class) - @ResponseStatus(HttpStatus.CONFLICT) - VndErrors workerAlreadyExistsExceptionHandler(AddWorkerInteractor.WorkerAlreadyExists ex) { - String message = (ex.getMessage() == null || ex.getMessage().isEmpty()) ? - "Worker Already Exists" : ex.getMessage(); - return new VndErrors("error", message); + private class AddWorkerPresenter implements AddWorkerResponder { + + private ResponseEntity response = new ResponseEntity<>(HttpStatus.CREATED); + + @Override public void workerAlreadyExists() { + response = new ResponseEntity<>(new VndErrors("error", "Worker Already Exists"), + HttpStatus.CONFLICT); + } + + public ResponseEntity getResponse() { + return response; } } } diff --git a/web-interface/src/test/java/org/reparationservice/rest/controllers/standalone/AddWorkerCtrlTest.java b/web-interface/src/test/java/org/reparationservice/rest/controllers/standalone/AddWorkerCtrlTest.java index 968e567..848f352 100644 --- a/web-interface/src/test/java/org/reparationservice/rest/controllers/standalone/AddWorkerCtrlTest.java +++ b/web-interface/src/test/java/org/reparationservice/rest/controllers/standalone/AddWorkerCtrlTest.java @@ -23,6 +23,7 @@ import org.reparationservice.rest.requests.AddWorkerJsonRequest; import org.reparationservice.usecases.worker.add.AddWorkerInteractorFactory; import org.reparationservice.usecases.worker.add.AddWorkerRequestBuilder; +import org.reparationservice.usecases.worker.add.AddWorkerResponder; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; @@ -72,11 +73,12 @@ public void moreThanOneRequest() throws Exception { sendWorkerPostRequestFor(WORKER_USERNAME_2); ArgumentCaptor usernameArg = ArgumentCaptor.forClass(String.class); - verify(addWorkerRB, times(2)).buildAddWorkerRequest(usernameArg.capture()); + ArgumentCaptor responderArg = ArgumentCaptor.forClass(AddWorkerResponder.class); + verify(addWorkerRB, times(2)).buildAddWorkerRequest(usernameArg.capture(), responderArg.capture()); - List unernames = usernameArg.getAllValues(); - assertThat(unernames.get(0)).isEqualTo(WORKER_USERNAME_1); - assertThat(unernames.get(1)).isEqualTo(WORKER_USERNAME_2); + List usernameList = usernameArg.getAllValues(); + assertThat(usernameList.get(0)).isEqualTo(WORKER_USERNAME_1); + assertThat(usernameList.get(1)).isEqualTo(WORKER_USERNAME_2); } private void sendWorkerPostRequestFor(String username) throws Exception {