Skip to content

Commit

Permalink
Move Interactor creation to Spring Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
palpa committed Mar 11, 2015
1 parent 4ffe61c commit 05e141c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import org.reparationservice.inmemory.InMemoryConfigurator;
import org.reparationservice.requestor.InteractorFactory;
import org.reparationservice.requestor.RequestBuilder;
import org.reparationservice.requestor.UseCaseActivator;
import org.reparationservice.requestor.impl.InteractorFactoryImpl;
import org.reparationservice.requestor.impl.RequestBuilderImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter;
Expand All @@ -22,24 +24,30 @@ public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
WorkerGateway getWorkerGW() {
@Bean WorkerGateway getWorkerGW() {
return InMemoryConfigurator.getWorkerGateway();
}

@Bean
InteractorFactory getInteractorFactory() {

@Bean InteractorFactory getInteractorFactory() {
return new InteractorFactoryImpl();
}

@Bean
RequestBuilder getRequestBuilder() {

@Bean RequestBuilder getRequestBuilder() {
return new RequestBuilderImpl();
}


@Bean @Qualifier("AddWorkerInteractor") UseCaseActivator getAddWorkerInteractor() {
return getInteractorFactory().makeAddWorkerInteractor(getWorkerGW());
}

@Bean @Qualifier("GetAllWorkersInteractor") UseCaseActivator getGetAllWorkersInteractor() {
return getInteractorFactory().makeGetAllWorkersInteractor(getWorkerGW());
}

@Configuration
public static class DisableStaticResourceConfiguration extends WebMvcAutoConfigurationAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {}
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.reparationservice.rest.controllers;

import org.reparationservice.entities.worker.WorkerGateway;
import org.reparationservice.requestor.UseCaseActivator;
import org.reparationservice.requestor.UseCaseRequest;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.hateoas.VndErrors;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,24 +17,18 @@

@RestController
public class AddWorkerController {
private final AddWorkerInteractorFactory intFactory;
private WorkerGateway workers;
private final UseCaseActivator interactor;
private final AddWorkerRequestBuilder requestBuilder;

@Autowired
public AddWorkerController(AddWorkerInteractorFactory intFactory,
WorkerGateway workers,
public AddWorkerController(@Qualifier("AddWorkerInteractor") UseCaseActivator interactor,
AddWorkerRequestBuilder requestBuilder) {
this.workers = workers;
this.intFactory = intFactory;
this.interactor = interactor;
this.requestBuilder = requestBuilder;
}

@RequestMapping(value = "/workers", method = RequestMethod.POST)
ResponseEntity<?> addWorker(@RequestBody AddWorkerJsonRequest workerReq) {

UseCaseActivator interactor = intFactory.makeAddWorkerInteractor(workers);

@RequestMapping(value = "/workers", method = RequestMethod.POST) ResponseEntity<?> addWorker(
@RequestBody AddWorkerJsonRequest workerReq) {
AddWorkerResponder responder = new AddWorkerPresenter();
UseCaseRequest request = requestBuilder
.buildAddWorkerRequest(workerReq.getUsername(), responder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
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.beans.factory.annotation.Qualifier;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -21,22 +20,19 @@
@Controller
@RequestMapping(value = "/workers", produces = "application/hal+json")
public final class GetAllWorkersController implements GetAllWorkersResponder {
private final WorkerGateway workers;
private final GetAllWorkersInteractorFactory intFactory;
private final UseCaseActivator interactor;
private final GetAllWorkersRequestBuilder requestBuilder;

private Iterable<WorkerDTO> workerList = Collections.emptyList();

@Autowired
public GetAllWorkersController(GetAllWorkersInteractorFactory intFactory
, WorkerGateway workers, GetAllWorkersRequestBuilder requestBuilder) {
this.workers = workers;
this.intFactory = intFactory;
public GetAllWorkersController(@Qualifier("GetAllWorkersInteractor") UseCaseActivator interactor,
GetAllWorkersRequestBuilder requestBuilder) {
this.interactor = interactor;
this.requestBuilder = requestBuilder;
}

@RequestMapping(method = RequestMethod.GET)
ResponseEntity<Resources<WorkerDTO>> getWorkers() {
UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workers);
@RequestMapping(method = RequestMethod.GET) ResponseEntity<Resources<WorkerDTO>> getWorkers() {
UseCaseRequest request = requestBuilder.buildGetAllWorkersRequest(this);
interactor.execute(request);
Resources<WorkerDTO> res = new Resources<>(this.workerList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.reparationservice.entities.worker.WorkerGateway;
import org.reparationservice.requestor.UseCaseActivator;
import org.reparationservice.requestor.UseCaseRequest;
import org.reparationservice.rest.controllers.AddWorkerController;
import org.reparationservice.rest.controllers.standalone.helpers.TestHelper;
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;
Expand All @@ -34,49 +32,36 @@ public class AddWorkerCtrlTest {
private static final String WORKER_USERNAME_2 = "any username 2";
private static final MediaType JSON_HAL_CONTENT_TYPE = TestHelper.JSON_HAL_CONTENT_TYPE;
private MockMvc mockMvc;
@Mock
private AddWorkerInteractorFactory addWorkerIF;

@Mock
private AddWorkerRequestBuilder addWorkerRB;
@Mock
private WorkerGateway workerGW;
@Mock
private UseCaseActivator interactor;

@Before
public void setup() throws Exception {
when(addWorkerIF.makeAddWorkerInteractor(any(WorkerGateway.class)))
.thenReturn(interactor);

AddWorkerController addWorkerCtrl = new AddWorkerController(addWorkerIF, workerGW, addWorkerRB);
AddWorkerController addWorkerCtrl = new AddWorkerController(interactor, addWorkerRB);
mockMvc = MockMvcBuilders.standaloneSetup(addWorkerCtrl).build();

sendWorkerPostRequestFor(WORKER_USERNAME_1);
}

@Test
public void callToAddWorkerInteractorWithGivenGW() throws Exception {
ArgumentCaptor<WorkerGateway> wGwArg = ArgumentCaptor.forClass(WorkerGateway.class);
verify(addWorkerIF, times(1)).makeAddWorkerInteractor(wGwArg.capture()
);

assertThat(wGwArg.getValue()).isSameAs(workerGW);
}

@Test
public void interactorActionWasExecuted() throws Exception {
ArgumentCaptor<UseCaseRequest> requestArg = ArgumentCaptor.forClass(UseCaseRequest.class);
ArgumentCaptor<UseCaseRequest> requestArg = ArgumentCaptor.forClass(UseCaseRequest.class);
verify(interactor).execute(requestArg.capture());
}

@Test
public void moreThanOneRequest() throws Exception {
sendWorkerPostRequestFor(WORKER_USERNAME_2);

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

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

List<String> usernameList = usernameArg.getAllValues();
assertThat(usernameList.get(0)).isEqualTo(WORKER_USERNAME_1);
assertThat(usernameList.get(1)).isEqualTo(WORKER_USERNAME_2);
Expand Down

0 comments on commit 05e141c

Please sign in to comment.