Skip to content

Commit

Permalink
GetAllWorkersInteractor receive Request on execute method
Browse files Browse the repository at this point in the history
  • Loading branch information
palpa committed Mar 10, 2015
1 parent 20c2d99 commit 0acd16e
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<WorkerDTO> workerList = convertToDTOCollection(workerGateway.getAllWorkers());
allWorkersReq.bindModel(workerList);
}

private Collection<WorkerDTO> convertToDTOCollection(Iterable<Worker> workers) {
Expand All @@ -36,11 +37,11 @@ private Collection<WorkerDTO> convertToDTOCollection(Iterable<Worker> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
import org.reparationservice.requestor.UseCaseActivator;

public interface GetAllWorkersInteractorFactory {
UseCaseActivator makeGetAllWorkersInteractor(WorkerGateway workers,
GetAllWorkersResponder getAllWorkersResponder);
UseCaseActivator makeGetAllWorkersInteractor(WorkerGateway workers);
}
Original file line number Diff line number Diff line change
@@ -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<WorkerDTO> workerList) {
responder.bindModel(workerList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.reparationservice.usecases.worker.getall;

import org.reparationservice.requestor.UseCaseRequest;

public interface GetAllWorkersRequestBuilder {
UseCaseRequest buildGetAllWorkersRequest(GetAllWorkersResponder responder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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<WorkerDTO> workerList = null;
request.bindModel(workerList);

GetAllWorkersResponderSpy respSpy = (GetAllWorkersResponderSpy) responder;
assertThat(respSpy.bindModelWasCalled()).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,19 +23,21 @@
public final class GetAllWorkersController implements GetAllWorkersResponder {
private final WorkerGateway workers;
private final GetAllWorkersInteractorFactory intFactory;
private Iterable<WorkerDTO> workerList;
private final GetAllWorkersRequestBuilder requestBuilder;
private Iterable<WorkerDTO> 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<Resources<WorkerDTO>> getWorkers() {
UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workers, this);
UseCaseRequest request = null;
UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workers);
UseCaseRequest request = requestBuilder.buildGetAllWorkersRequest(this);
interactor.execute(request);
Resources<WorkerDTO> res = new Resources<>(this.workerList);
return new ResponseEntity<>(res, HttpStatus.OK);
Expand Down

0 comments on commit 0acd16e

Please sign in to comment.