diff --git a/web-interface/src/main/java/org/reparationservice/rest/Application.java b/web-interface/src/main/java/org/reparationservice/rest/Application.java index 4b2efe4..c059c1d 100644 --- a/web-interface/src/main/java/org/reparationservice/rest/Application.java +++ b/web-interface/src/main/java/org/reparationservice/rest/Application.java @@ -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; @@ -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) { + } } } 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 c0d8ea8..61d1689 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 @@ -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; @@ -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); 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 4a9f770..59d6bf9 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 @@ -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; @@ -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 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> getWorkers() { - UseCaseActivator interactor = intFactory.makeGetAllWorkersInteractor(workers); + @RequestMapping(method = RequestMethod.GET) ResponseEntity> getWorkers() { UseCaseRequest request = requestBuilder.buildGetAllWorkersRequest(this); interactor.execute(request); Resources res = new Resources<>(this.workerList); 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 c64da67..1ebab10 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 @@ -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; @@ -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 wGwArg = ArgumentCaptor.forClass(WorkerGateway.class); - verify(addWorkerIF, times(1)).makeAddWorkerInteractor(wGwArg.capture() - ); - - assertThat(wGwArg.getValue()).isSameAs(workerGW); - } - @Test public void interactorActionWasExecuted() throws Exception { - ArgumentCaptor requestArg = ArgumentCaptor.forClass(UseCaseRequest.class); + ArgumentCaptor requestArg = ArgumentCaptor.forClass(UseCaseRequest.class); verify(interactor).execute(requestArg.capture()); } @Test public void moreThanOneRequest() throws Exception { sendWorkerPostRequestFor(WORKER_USERNAME_2); - + ArgumentCaptor usernameArg = ArgumentCaptor.forClass(String.class); - ArgumentCaptor responderArg = ArgumentCaptor.forClass(AddWorkerResponder.class); - verify(addWorkerRB, times(2)).buildAddWorkerRequest(usernameArg.capture(), responderArg.capture()); - + ArgumentCaptor responderArg = ArgumentCaptor + .forClass(AddWorkerResponder.class); + verify(addWorkerRB, times(2)) + .buildAddWorkerRequest(usernameArg.capture(), responderArg.capture()); + List usernameList = usernameArg.getAllValues(); assertThat(usernameList.get(0)).isEqualTo(WORKER_USERNAME_1); assertThat(usernameList.get(1)).isEqualTo(WORKER_USERNAME_2);