diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/customer/add/AddCustomerInteractor.java b/core/usecases/src/main/java/org/reparationservice/usecases/customer/add/AddCustomerInteractor.java index 088c90e..ac21e95 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/customer/add/AddCustomerInteractor.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/customer/add/AddCustomerInteractor.java @@ -5,17 +5,28 @@ import org.reparationservice.requestor.UseCaseRequest; public final class AddCustomerInteractor implements UseCaseActivator { - private final CustomerGateway customers; - private final UseCaseRequest request; + private final CustomerGateway customers; - public AddCustomerInteractor(CustomerGateway customers, UseCaseRequest request) { - this.customers = customers; - this.request = request; - } + public AddCustomerInteractor(CustomerGateway customers) { + if (customers == null) + throw new CustomerGatewayCannotBeNull(); + this.customers = customers; + } - @Override - public void execute(UseCaseRequest request) { - long customerId = ((AddCustomerRequest) this.request).getId(); - customers.addCustomer(customerId); - } + @Override + public void execute(UseCaseRequest request) { + if (request == null) + throw new RequestCannotBeNull(); + + long customerId = ((AddCustomerRequest) request).getId(); + customers.addCustomer(customerId); + } + + class RequestCannotBeNull extends RuntimeException { + private static final long serialVersionUID = -8424684392205119848L; + } + + class CustomerGatewayCannotBeNull extends RuntimeException { + private static final long serialVersionUID = 6821771280959420623L; + } } diff --git a/core/usecases/src/test/java/org/reparationservice/usecases/customer/add/AddCustomerInteractorTest.java b/core/usecases/src/test/java/org/reparationservice/usecases/customer/add/AddCustomerInteractorTest.java index 419ce9a..d3bde3d 100644 --- a/core/usecases/src/test/java/org/reparationservice/usecases/customer/add/AddCustomerInteractorTest.java +++ b/core/usecases/src/test/java/org/reparationservice/usecases/customer/add/AddCustomerInteractorTest.java @@ -2,37 +2,57 @@ 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.CustomerGatewaySpy; import org.reparationservice.entities.customer.Customer; import org.reparationservice.requestor.UseCaseActivator; +@RunWith(HierarchicalContextRunner.class) public class AddCustomerInteractorTest { - private static final long CUSTOMER_ID = 1; - private CustomerGatewaySpy customersSpy; - - @Before - public void setUp() { - customersSpy = new CustomerGatewaySpy(); - } - - @Test - public void gatewayWasNotCalledWhenInteractorNotYetExecuted() { - assertThat(customersSpy.addCustomerWasCalled()).isFalse(); - } - - @Test - public void executeOperation() { - AddCustomerRequest request = new AddCustomerRequest(CUSTOMER_ID); - UseCaseActivator addCustomer = new AddCustomerInteractor(customersSpy, request); - - addCustomer.execute(request); - - assertThat(customersSpy.addCustomerWasCalled()).isTrue(); - Customer addedCustomer = customersSpy.getCustomer(); - assertThat(addedCustomer).isNotNull(); - assertThat(addedCustomer.getId()).isEqualTo(CUSTOMER_ID); - } + private static final long CUSTOMER_ID = 1; + private CustomerGatewaySpy customersSpy; + + @Before + public void setUp() { + customersSpy = new CustomerGatewaySpy(); + } + + @Test + public void gatewayWasNotCalledWhenInteractorNotYetExecuted() { + assertThat(customersSpy.addCustomerWasCalled()).isFalse(); + } + + @Test(expected = AddCustomerInteractor.CustomerGatewayCannotBeNull.class) + public void customerGatewayCannotBeNull() { + new AddCustomerInteractor(null); + } + + public class InteractorCreated { + private UseCaseActivator addCustomer; + + @Before + public void givenInteractor(){ + addCustomer = new AddCustomerInteractor(customersSpy); + } + + @Test(expected = AddCustomerInteractor.RequestCannotBeNull.class) + public void requestCannotBeNull() { + addCustomer.execute(null); + } + + @Test + public void executeOperation() { + AddCustomerRequest request = new AddCustomerRequest(CUSTOMER_ID); + addCustomer.execute(request); + + assertThat(customersSpy.addCustomerWasCalled()).isTrue(); + Customer addedCustomer = customersSpy.getCustomer(); + assertThat(addedCustomer).isNotNull(); + assertThat(addedCustomer.getId()).isEqualTo(CUSTOMER_ID); + } + } }