diff --git a/core/usecases/src/main/java/org/reparationservice/requestor/impl/InteractorFactoryImpl.java b/core/usecases/src/main/java/org/reparationservice/requestor/impl/InteractorFactoryImpl.java index eb6c2c6..27c6c8c 100644 --- a/core/usecases/src/main/java/org/reparationservice/requestor/impl/InteractorFactoryImpl.java +++ b/core/usecases/src/main/java/org/reparationservice/requestor/impl/InteractorFactoryImpl.java @@ -5,7 +5,6 @@ import org.reparationservice.entities.worker.WorkerGateway; import org.reparationservice.usecases.worker.add.AddWorkerInteractor; import org.reparationservice.usecases.worker.getall.GetAllWorkerInteractor; -import org.reparationservice.usecases.worker.getall.GetAllWorkersResponder; public class InteractorFactoryImpl implements InteractorFactory { @Override @@ -14,8 +13,7 @@ public UseCaseActivator makeAddWorkerInteractor(WorkerGateway workers) { } @Override - public UseCaseActivator makeGetAllWorkersInteractor(WorkerGateway workers, - GetAllWorkersResponder getAllWorkersResponder) { - return new GetAllWorkerInteractor(workers, getAllWorkersResponder); + public UseCaseActivator makeGetAllWorkersInteractor(WorkerGateway workers) { + return new GetAllWorkerInteractor(workers); } } 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 ec71e97..26b106f 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 @@ -4,10 +4,17 @@ import org.reparationservice.requestor.UseCaseRequest; import org.reparationservice.usecases.worker.add.AddWorkerRequest; import org.reparationservice.usecases.worker.add.AddWorkerResponder; +import org.reparationservice.usecases.worker.getall.GetAllWorkersRequest; +import org.reparationservice.usecases.worker.getall.GetAllWorkersRequestBuilder; +import org.reparationservice.usecases.worker.getall.GetAllWorkersResponder; -public class RequestBuilderImpl implements RequestBuilder { +public class RequestBuilderImpl implements RequestBuilder, GetAllWorkersRequestBuilder { @Override public UseCaseRequest buildAddWorkerRequest(String username, AddWorkerResponder responder) { return new AddWorkerRequest(username, responder); } + + @Override public UseCaseRequest buildGetAllWorkersRequest(GetAllWorkersResponder responder) { + return new GetAllWorkersRequest(responder); + } } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkerInteractor.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkerInteractor.java index aef5b76..1feed89 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkerInteractor.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkerInteractor.java @@ -10,22 +10,23 @@ import org.reparationservice.requestor.UseCaseRequest; public class GetAllWorkerInteractor implements UseCaseActivator { - private final GetAllWorkersResponder allWorkersPresenter; private final WorkerGateway workerGateway; - public GetAllWorkerInteractor(WorkerGateway workerGateway, - GetAllWorkersResponder allWorkersPresenter) { + public GetAllWorkerInteractor(WorkerGateway workerGateway) { if (workerGateway == null) - throw new WorkerGatewayNotNull(); - if (allWorkersPresenter == null) - throw new PresenterNotNull(); + throw new WorkerGatewayCannotBeNull(); + this.workerGateway = workerGateway; - this.allWorkersPresenter = allWorkersPresenter; } @Override public void execute(UseCaseRequest request) { - allWorkersPresenter.bindModel(convertToDTOCollection(workerGateway.getAllWorkers()) ); + if (request == null) + throw new GetAllWorkersRequestCannotBeNull(); + GetAllWorkersRequest allWorkersReq = (GetAllWorkersRequest) request; + + Collection workerList = convertToDTOCollection(workerGateway.getAllWorkers()); + allWorkersReq.bindModel(workerList); } private Collection convertToDTOCollection(Iterable workers) { @@ -36,11 +37,11 @@ private Collection convertToDTOCollection(Iterable workers) { return workerDTOCollection; } - public class WorkerGatewayNotNull extends RuntimeException { + class WorkerGatewayCannotBeNull extends RuntimeException { private static final long serialVersionUID = -2219845456935531878L; } - public class PresenterNotNull extends RuntimeException { - private static final long serialVersionUID = 2553548905696488214L; + class GetAllWorkersRequestCannotBeNull extends RuntimeException { + private static final long serialVersionUID = -7825871272211962752L; } } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorFactory.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorFactory.java index b4f1b07..333754a 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorFactory.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorFactory.java @@ -4,6 +4,5 @@ import org.reparationservice.requestor.UseCaseActivator; public interface GetAllWorkersInteractorFactory { - UseCaseActivator makeGetAllWorkersInteractor(WorkerGateway workers, - GetAllWorkersResponder getAllWorkersResponder); + UseCaseActivator makeGetAllWorkersInteractor(WorkerGateway workers); } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequest.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequest.java new file mode 100644 index 0000000..e73d04b --- /dev/null +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequest.java @@ -0,0 +1,18 @@ +package org.reparationservice.usecases.worker.getall; + +import org.reparationservice.entities.worker.WorkerDTO; +import org.reparationservice.requestor.UseCaseRequest; + +import java.util.Collection; + +public class GetAllWorkersRequest extends UseCaseRequest implements GetAllWorkersResponder { + private final GetAllWorkersResponder responder; + + public GetAllWorkersRequest(GetAllWorkersResponder responder) { + this.responder = responder; + } + + @Override public void bindModel(Collection workerList) { + responder.bindModel(workerList); + } +} diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequestBuilder.java b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequestBuilder.java new file mode 100644 index 0000000..2e4c62d --- /dev/null +++ b/core/usecases/src/main/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequestBuilder.java @@ -0,0 +1,7 @@ +package org.reparationservice.usecases.worker.getall; + +import org.reparationservice.requestor.UseCaseRequest; + +public interface GetAllWorkersRequestBuilder { + UseCaseRequest buildGetAllWorkersRequest(GetAllWorkersResponder responder); +} diff --git a/core/usecases/src/test/java/org/reparationservice/requestor/InteractorFactoryTest.java b/core/usecases/src/test/java/org/reparationservice/requestor/InteractorFactoryTest.java index 8097874..f081661 100644 --- a/core/usecases/src/test/java/org/reparationservice/requestor/InteractorFactoryTest.java +++ b/core/usecases/src/test/java/org/reparationservice/requestor/InteractorFactoryTest.java @@ -8,7 +8,6 @@ import org.reparationservice.doubles.WorkerGatewaySpy; import org.reparationservice.entities.worker.WorkerGateway; import org.reparationservice.requestor.impl.InteractorFactoryImpl; -import org.reparationservice.doubles.GetAllWorkersResponderSpy; import org.reparationservice.usecases.worker.add.AddWorkerInteractor; import org.reparationservice.usecases.worker.getall.GetAllWorkerInteractor; @@ -31,8 +30,7 @@ public void makeAddWorkerInteractor() { @Test public void makeGetAllWorkersInteractor() { - UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workerGateway, - new GetAllWorkersResponderSpy()); + UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workerGateway); assertThat(interactor).isInstanceOf(GetAllWorkerInteractor.class); } } 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 e8d72d6..1424aca 100644 --- a/core/usecases/src/test/java/org/reparationservice/requestor/RequestBuilderTest.java +++ b/core/usecases/src/test/java/org/reparationservice/requestor/RequestBuilderTest.java @@ -9,6 +9,9 @@ import org.reparationservice.usecases.worker.add.AddWorkerRequestBuilder; import org.reparationservice.usecases.worker.add.AddWorkerResponder; import org.reparationservice.usecases.worker.add.AddWorkerResponderSpy; +import org.reparationservice.usecases.worker.getall.GetAllWorkersRequest; +import org.reparationservice.usecases.worker.getall.GetAllWorkersRequestBuilder; +import org.reparationservice.usecases.worker.getall.GetAllWorkersResponder; public class RequestBuilderTest { private static final String WORKER_USERNAME = "username"; @@ -24,4 +27,12 @@ public void buildAddWorkerRequest() { addWorkerReq.workerAlreadyExists(); assertThat(((AddWorkerResponderSpy) responder).workerAlreadyExistsWasCalled()).isTrue(); } + + @Test + public void buildGetAllWorkersRequest() { + GetAllWorkersRequestBuilder builder = new RequestBuilderImpl(); + GetAllWorkersResponder responder = null; + UseCaseRequest request = builder.buildGetAllWorkersRequest(responder); + assertThat(request).isInstanceOf(GetAllWorkersRequest.class); + } } diff --git a/core/usecases/src/test/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorTest.java b/core/usecases/src/test/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorTest.java index 7b7f8c1..1ba2a8e 100644 --- a/core/usecases/src/test/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorTest.java +++ b/core/usecases/src/test/java/org/reparationservice/usecases/worker/getall/GetAllWorkersInteractorTest.java @@ -1,39 +1,54 @@ package org.reparationservice.usecases.worker.getall; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; +import de.bechte.junit.runners.context.HierarchicalContextRunner; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.reparationservice.doubles.GetAllWorkersGatewaySpy; import org.reparationservice.doubles.GetAllWorkersResponderSpy; import org.reparationservice.entities.worker.WorkerGateway; import org.reparationservice.requestor.UseCaseActivator; -import org.reparationservice.requestor.UseCaseRequest; +@RunWith(HierarchicalContextRunner.class) public class GetAllWorkersInteractorTest { - - @Test(expected = GetAllWorkerInteractor.WorkerGatewayNotNull.class) + + @Test(expected = GetAllWorkerInteractor.WorkerGatewayCannotBeNull.class) public void throwExceptionWhenNullGatewayPassed() { - new GetAllWorkerInteractor(null, new GetAllWorkersResponderSpy()); - } - - @Test(expected = GetAllWorkerInteractor.PresenterNotNull.class) - public void throwExceptionWhenNullPresenterPassed() { - new GetAllWorkerInteractor(new GetAllWorkersGatewaySpy(), null); + new GetAllWorkerInteractor(null); } - - @Test - public void returnsAllWorkersWhenExecuteIsCalled() { - GetAllWorkersResponder allWorkersPresenter = new GetAllWorkersResponderSpy(); - WorkerGateway workerGateway = new GetAllWorkersGatewaySpy(); - - UseCaseActivator getAllWorkers = new GetAllWorkerInteractor(workerGateway, allWorkersPresenter); - UseCaseRequest request = null; - getAllWorkers.execute(request); - - GetAllWorkersGatewaySpy workerGatewaySpy = (GetAllWorkersGatewaySpy) workerGateway; - assertTrue(workerGatewaySpy.getAllWorkersWasCalled()); - assertTrue(((GetAllWorkersResponderSpy) allWorkersPresenter).bindModelWasCalled()); - assertEquals(GetAllWorkersGatewaySpy.workerListSize, ((GetAllWorkersResponderSpy) allWorkersPresenter).getWorkerCollection().size()); + + public class InteractorCreated { + private WorkerGateway workerGateway; + private UseCaseActivator getAllWorkers; + + @Before + public void givenInteractor() { + workerGateway = new GetAllWorkersGatewaySpy(); + getAllWorkers = new GetAllWorkerInteractor(workerGateway); + } + + @Test(expected = GetAllWorkerInteractor.GetAllWorkersRequestCannotBeNull.class) + public void throwExceptionWhenNullRequestReceived() { + getAllWorkers.execute(null); + } + + @Test + public void returnsAllWorkersWhenExecuteIsCalled() { + GetAllWorkersResponder responder = new GetAllWorkersResponderSpy(); + GetAllWorkersRequest request = new GetAllWorkersRequest(responder); + + getAllWorkers.execute(request); + + GetAllWorkersGatewaySpy workerGatewaySpy = (GetAllWorkersGatewaySpy) workerGateway; + assertThat(workerGatewaySpy.getAllWorkersWasCalled()).isTrue(); + + GetAllWorkersResponderSpy respSpy = (GetAllWorkersResponderSpy) responder; + assertThat(respSpy.bindModelWasCalled()).isTrue(); + assertThat(GetAllWorkersGatewaySpy.workerListSize) + .isEqualTo(respSpy.getWorkerCollection().size()); + } } } diff --git a/core/usecases/src/test/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequestTest.java b/core/usecases/src/test/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequestTest.java new file mode 100644 index 0000000..d57adcc --- /dev/null +++ b/core/usecases/src/test/java/org/reparationservice/usecases/worker/getall/GetAllWorkersRequestTest.java @@ -0,0 +1,32 @@ +package org.reparationservice.usecases.worker.getall; + +import org.junit.Test; +import org.reparationservice.doubles.GetAllWorkersResponderSpy; +import org.reparationservice.entities.worker.WorkerDTO; + +import java.util.Collection; + +import static org.assertj.core.api.Assertions.assertThat; + +public class GetAllWorkersRequestTest { + @Test + public void requestIsInstanceOfGetAllWorkersResponder() { + GetAllWorkersResponder responder = new GetAllWorkersResponderSpy(); + GetAllWorkersRequest request = new GetAllWorkersRequest(responder); + + assertThat(request).isInstanceOf(GetAllWorkersResponder.class); + } + + @Test + public void bindModelWasCalled() { + GetAllWorkersResponder responder = new GetAllWorkersResponderSpy(); + GetAllWorkersRequest request = new GetAllWorkersRequest(responder); + + Collection workerList = null; + request.bindModel(workerList); + + GetAllWorkersResponderSpy respSpy = (GetAllWorkersResponderSpy) responder; + assertThat(respSpy.bindModelWasCalled()).isTrue(); + } + +} diff --git a/web-interface/src/main/java/org/reparationservice/rest/controllers/GetAllWorkersController.java b/web-interface/src/main/java/org/reparationservice/rest/controllers/GetAllWorkersController.java index 3f91b8f..4a9f770 100644 --- a/web-interface/src/main/java/org/reparationservice/rest/controllers/GetAllWorkersController.java +++ b/web-interface/src/main/java/org/reparationservice/rest/controllers/GetAllWorkersController.java @@ -1,12 +1,14 @@ package org.reparationservice.rest.controllers; import java.util.Collection; +import java.util.Collections; import org.reparationservice.entities.worker.WorkerDTO; import org.reparationservice.entities.worker.WorkerGateway; import org.reparationservice.requestor.UseCaseActivator; import org.reparationservice.requestor.UseCaseRequest; import org.reparationservice.usecases.worker.getall.GetAllWorkersInteractorFactory; +import org.reparationservice.usecases.worker.getall.GetAllWorkersRequestBuilder; import org.reparationservice.usecases.worker.getall.GetAllWorkersResponder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.hateoas.Resources; @@ -21,19 +23,21 @@ public final class GetAllWorkersController implements GetAllWorkersResponder { private final WorkerGateway workers; private final GetAllWorkersInteractorFactory intFactory; - private Iterable workerList; + private final GetAllWorkersRequestBuilder requestBuilder; + private Iterable workerList = Collections.emptyList(); @Autowired public GetAllWorkersController(GetAllWorkersInteractorFactory intFactory - , WorkerGateway workers) { + , WorkerGateway workers, GetAllWorkersRequestBuilder requestBuilder) { this.workers = workers; this.intFactory = intFactory; + this.requestBuilder = requestBuilder; } @RequestMapping(method = RequestMethod.GET) ResponseEntity> getWorkers() { - UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workers, this); - UseCaseRequest request = null; + UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workers); + UseCaseRequest request = requestBuilder.buildGetAllWorkersRequest(this); interactor.execute(request); Resources res = new Resources<>(this.workerList); return new ResponseEntity<>(res, HttpStatus.OK);