Skip to content

Commit

Permalink
AddCustomerInteractor receive Request on execute method
Browse files Browse the repository at this point in the history
  add not null exceptions
  • Loading branch information
palpa committed Mar 11, 2015
1 parent 0acd16e commit f70fd17
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit f70fd17

Please sign in to comment.