Skip to content

Commit

Permalink
Change exception by responder to inform validation error
Browse files Browse the repository at this point in the history
  error: worker already exists
  • Loading branch information
palpa committed Mar 10, 2015
1 parent 98322af commit 70abea2
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import org.reparationservice.requestor.UseCaseRequest;

public interface AddWorkerRequestBuilder {
UseCaseRequest buildAddWorkerRequest(String username);
UseCaseRequest buildAddWorkerRequest(String username, AddWorkerResponder responder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.reparationservice.usecases.worker.add;

public interface AddWorkerResponder {
void workerAlreadyExists();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
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";

@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -72,11 +73,12 @@ public void moreThanOneRequest() throws Exception {
sendWorkerPostRequestFor(WORKER_USERNAME_2);

ArgumentCaptor<String> usernameArg = ArgumentCaptor.forClass(String.class);
verify(addWorkerRB, times(2)).buildAddWorkerRequest(usernameArg.capture());
ArgumentCaptor<AddWorkerResponder> responderArg = ArgumentCaptor.forClass(AddWorkerResponder.class);
verify(addWorkerRB, times(2)).buildAddWorkerRequest(usernameArg.capture(), responderArg.capture());

List<String> unernames = usernameArg.getAllValues();
assertThat(unernames.get(0)).isEqualTo(WORKER_USERNAME_1);
assertThat(unernames.get(1)).isEqualTo(WORKER_USERNAME_2);
List<String> 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 {
Expand Down

0 comments on commit 70abea2

Please sign in to comment.