From 3119f9c86d757edba25178b5c43cc663c5c1c63a Mon Sep 17 00:00:00 2001 From: Damian Palpacelli Date: Sat, 14 Mar 2015 22:22:30 -0300 Subject: [PATCH] Refactor AddReparationInteractorTest (+1 squashed commits) Squashed commits: [156093a] Refactor AddReparationInteractorTest --- .../inmemory/InMemoryConfigurator.java | 31 ---- .../inmemory/ReparationServiceImpl.java | 11 +- .../entities/customer/CustomerImpl.java | 5 +- .../entities/customer/DeviceImpl.java | 5 +- .../entities/customer/ReparationImpl.java | 4 +- .../entities/customer/CustomerTest.java | 5 +- .../entities/customer/DeviceTest.java | 92 ++++++----- .../entities/customer/ReparationTest.java | 29 ---- .../entities/devicetype/DeviceTypeTest.java | 3 +- .../inmemory/entities/worker/WorkerTest.java | 3 +- .../entities/customer/Reparation.java | 4 +- .../entities/customer/ReparationDTO.java | 18 +-- .../entities/customer/ReparationTest.java | 22 +-- .../add/AddReparationInteractor.java | 5 +- .../reparation/add/AddReparationRequest.java | 17 +- .../doubles/CustomerGatewaySpy.java | 150 ++++++++++-------- .../doubles/CustomerGwWithDeviceSpy.java | 10 ++ .../doubles/CustomerWithDeviceSpy.java | 10 -- .../add/AddCustomerInteractorTest.java | 10 +- .../add/AddReparationInteractorTest.java | 36 ++--- .../add/AddReparationRequestTest.java | 6 +- 21 files changed, 199 insertions(+), 277 deletions(-) delete mode 100644 core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/ReparationTest.java create mode 100644 core/usecases/src/test/java/org/reparationservice/doubles/CustomerGwWithDeviceSpy.java delete mode 100644 core/usecases/src/test/java/org/reparationservice/doubles/CustomerWithDeviceSpy.java diff --git a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/InMemoryConfigurator.java b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/InMemoryConfigurator.java index 926677b..7ae9189 100644 --- a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/InMemoryConfigurator.java +++ b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/InMemoryConfigurator.java @@ -1,19 +1,7 @@ package org.reparationservice.inmemory; -import org.reparationservice.inmemory.entities.devicetype.DeviceTypeImpl; -import org.reparationservice.inmemory.entities.worker.WorkerImpl; -import org.reparationservice.inmemory.entities.customer.CustomerImpl; -import org.reparationservice.inmemory.entities.customer.DeviceImpl; -import org.reparationservice.inmemory.entities.customer.ReparationImpl; -import org.reparationservice.entities.customer.Customer; import org.reparationservice.entities.customer.CustomerGateway; -import org.reparationservice.entities.customer.Device; -import org.reparationservice.entities.customer.Reparation; -import org.reparationservice.entities.customer.ReparationDTO; -import org.reparationservice.entities.devicetype.DeviceType; import org.reparationservice.entities.devicetype.DeviceTypeGateway; -import org.reparationservice.entities.worker.Worker; -import org.reparationservice.entities.worker.WorkerDTO; import org.reparationservice.entities.worker.WorkerGateway; public final class InMemoryConfigurator { @@ -33,23 +21,4 @@ public static WorkerGateway getWorkerGateway() { return new ReparationServiceImpl(); } - public static Customer getNewCustomer(long customerId) { - return new CustomerImpl(customerId); - } - - public static Device getNewDevice(long deviceSerialNumber) { - return new DeviceImpl(deviceSerialNumber); - } - - public static DeviceType getNewDeviceType(String deviceTypeDescription) { - return new DeviceTypeImpl(deviceTypeDescription); - } - - public static Reparation getNewReparation(ReparationDTO reparationDTO) { - return new ReparationImpl(reparationDTO); - } - - public static Worker getNewWorker(WorkerDTO workerDTO) { - return new WorkerImpl(workerDTO); - } } diff --git a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/ReparationServiceImpl.java b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/ReparationServiceImpl.java index f539254..0c76e09 100644 --- a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/ReparationServiceImpl.java +++ b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/ReparationServiceImpl.java @@ -9,6 +9,9 @@ import org.reparationservice.entities.devicetype.DeviceType; import org.reparationservice.entities.worker.Worker; import org.reparationservice.entities.worker.WorkerDTO; +import org.reparationservice.inmemory.entities.customer.CustomerImpl; +import org.reparationservice.inmemory.entities.devicetype.DeviceTypeImpl; +import org.reparationservice.inmemory.entities.worker.WorkerImpl; public class ReparationServiceImpl implements ReparationService { @@ -18,7 +21,7 @@ public class ReparationServiceImpl implements @Override public void addWorker(WorkerDTO workerDTO) { - Worker worker = InMemoryConfigurator.getNewWorker(workerDTO); + Worker worker = new WorkerImpl(workerDTO); workers.put(workerDTO.getUserName(), worker); } @@ -36,8 +39,8 @@ public Worker getWorkerByUserName(String workerUserName) { @Override public void addDeviceType(String deviceTypeDescription) { - this.deviceTypes.put(deviceTypeDescription, - InMemoryConfigurator.getNewDeviceType(deviceTypeDescription)); + this.deviceTypes.put(deviceTypeDescription, + new DeviceTypeImpl(deviceTypeDescription)); } @Override @@ -56,6 +59,6 @@ public Customer getCustomerById(long customerId) { @Override public void addCustomer(long customerId) { - customers.put(customerId, InMemoryConfigurator.getNewCustomer(customerId)); + customers.put(customerId, new CustomerImpl(customerId)); } } diff --git a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/CustomerImpl.java b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/CustomerImpl.java index df430d9..af212df 100644 --- a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/CustomerImpl.java +++ b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/CustomerImpl.java @@ -2,7 +2,6 @@ import org.reparationservice.entities.customer.Customer; import org.reparationservice.entities.customer.Device; -import org.reparationservice.inmemory.InMemoryConfigurator; import java.util.HashMap; import java.util.Map; @@ -29,7 +28,7 @@ public Device getDevice(long deviceSerialNumber) { @Override public void addDevice(long deviceSerialNumber) { - devices.put(deviceSerialNumber, - InMemoryConfigurator.getNewDevice(deviceSerialNumber)); + devices.put(deviceSerialNumber, + new DeviceImpl(deviceSerialNumber)); } } diff --git a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/DeviceImpl.java b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/DeviceImpl.java index 3fdfffd..4e26a05 100644 --- a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/DeviceImpl.java +++ b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/DeviceImpl.java @@ -5,7 +5,6 @@ import org.joda.time.DateTime; -import org.reparationservice.inmemory.InMemoryConfigurator; import org.reparationservice.entities.customer.Device; import org.reparationservice.entities.customer.Reparation; import org.reparationservice.entities.customer.ReparationDTO; @@ -30,7 +29,7 @@ public Reparation getReparation(DateTime creationDate) { @Override public void addReparation(ReparationDTO reparationDTO) { - reparations.put(reparationDTO.getCreationDate(), - InMemoryConfigurator.getNewReparation(reparationDTO)); + reparations.put(reparationDTO.getCreationDate(), + new ReparationImpl(reparationDTO)); } } diff --git a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/ReparationImpl.java b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/ReparationImpl.java index 6f52794..c57b148 100644 --- a/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/ReparationImpl.java +++ b/core-impl/inmemory-entities/src/main/java/org/reparationservice/inmemory/entities/customer/ReparationImpl.java @@ -11,7 +11,7 @@ public final class ReparationImpl extends Reparation { public ReparationImpl(ReparationDTO reparationDTO) { this.creationDate = reparationDTO.getCreationDate(); - this.failure = reparationDTO.getFailure(); + this.failure = reparationDTO.getDeviceFailure(); } @Override @@ -20,7 +20,7 @@ public DateTime getCreationDate() { } @Override - public String getFailure() { + public String getDeviceFailure() { return failure; } } diff --git a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/CustomerTest.java b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/CustomerTest.java index 40e903a..0d4af19 100644 --- a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/CustomerTest.java +++ b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/CustomerTest.java @@ -4,7 +4,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.reparationservice.inmemory.InMemoryConfigurator; import org.reparationservice.entities.customer.Customer; import org.reparationservice.entities.customer.Device; @@ -59,6 +58,6 @@ private void assertDevice(Device device, long deviceSerialNumber) { } private Customer createCustomer(long customerId) { - return InMemoryConfigurator.getNewCustomer(customerId); - } + return new CustomerImpl(customerId); + } } diff --git a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/DeviceTest.java b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/DeviceTest.java index 07ab8c6..b988ddc 100644 --- a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/DeviceTest.java +++ b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/DeviceTest.java @@ -2,58 +2,62 @@ import de.bechte.junit.runners.context.HierarchicalContextRunner; import org.joda.time.DateTime; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.reparationservice.entities.customer.Device; import org.reparationservice.entities.customer.Reparation; import org.reparationservice.entities.customer.ReparationDTO; -import org.reparationservice.inmemory.InMemoryConfigurator; import static org.assertj.core.api.Assertions.assertThat; @RunWith(HierarchicalContextRunner.class) public class DeviceTest { - private static final DateTime REP_CREATION_DATE_1 = DateTime.now(); - private static final DateTime REP_CREATION_DATE_2 = REP_CREATION_DATE_1 - .plusDays(1); - private Device device; - private static final int SERIAL_NUMBER = 150; - - @Before - public void givenDevice() { - device = InMemoryConfigurator.getNewDevice(SERIAL_NUMBER); - } - - @Test - public void creation() { - assertThat(device).isNotNull(); - assertThat(device.getSerialNumber()).isEqualTo(SERIAL_NUMBER); - } - - public class ReparationOps - { - private static final String REP_FAILURE = "failure"; - - @Test - public void getReparation() { - device.addReparation(newRepDTO(REP_CREATION_DATE_1)); - device.addReparation(newRepDTO(REP_CREATION_DATE_2)); - - Reparation reparation1 = device - .getReparation(REP_CREATION_DATE_1); - assertThat(reparation1).isNotNull(); - assertThat(reparation1.getCreationDate()).isEqualTo( - REP_CREATION_DATE_1); - Reparation reparation2 = device - .getReparation(REP_CREATION_DATE_2); - assertThat(reparation2).isNotNull(); - assertThat(reparation2.getCreationDate()).isEqualTo( - REP_CREATION_DATE_2); - } - - private ReparationDTO newRepDTO(DateTime creationDate) { - return new ReparationDTO(creationDate, REP_FAILURE); - } - } + private static final int SERIAL_NUMBER = 150; + private static final DateTime REP_CREATION_DATE_1 = DateTime.now(); + private static final DateTime REP_CREATION_DATE_2 = REP_CREATION_DATE_1 + .plusDays(1); + private static final String REP_FAILURE = "failure"; + + @Test + public void addGetReparation() { + Device device = new DeviceImpl((long) SERIAL_NUMBER); + device.addReparation(newRepDTO(REP_CREATION_DATE_1)); + device.addReparation(newRepDTO(REP_CREATION_DATE_2)); + + Reparation reparation1 = device + .getReparation(REP_CREATION_DATE_1); + assertThat(reparation1).isNotNull(); + assertThat(reparation1.getCreationDate()).isEqualTo( + REP_CREATION_DATE_1); + assertThat(reparation1.getDeviceFailure()).isEqualTo( + REP_FAILURE); + + Reparation reparation2 = device + .getReparation(REP_CREATION_DATE_2); + assertThat(reparation2).isNotNull(); + assertThat(reparation2.getCreationDate()).isEqualTo( + REP_CREATION_DATE_2); + assertThat(reparation2.getDeviceFailure()).isEqualTo( + REP_FAILURE); + } + + private ReparationDTO newRepDTO(final DateTime creationDate) { + return new ReparationDTO() { + @Override public DateTime getCreationDate() { + return creationDate; + } + + @Override public String getDeviceFailure() { + return REP_FAILURE; + } + + @Override public String getUrgency() { + return null; + } + + @Override public String getObservations() { + return null; + } + }; + } } diff --git a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/ReparationTest.java b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/ReparationTest.java deleted file mode 100644 index 2a60699..0000000 --- a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/customer/ReparationTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.reparationservice.inmemory.entities.customer; - -import org.joda.time.DateTime; -import org.junit.Before; -import org.junit.Test; -import org.reparationservice.entities.customer.Reparation; -import org.reparationservice.entities.customer.ReparationDTO; -import org.reparationservice.inmemory.InMemoryConfigurator; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ReparationTest { - private static final String FAILURE = "failure"; - private static final DateTime CREATION_DATE = DateTime.now(); - private ReparationDTO reparationDTO; - - @Before - public void givenReparationDTO() { - reparationDTO = new ReparationDTO(CREATION_DATE, FAILURE); - } - - @Test - public void createReparation() { - Reparation rep = InMemoryConfigurator.getNewReparation(reparationDTO); - assertThat(rep).isNotNull(); - assertThat(rep.getCreationDate()).isEqualTo(CREATION_DATE); - assertThat(rep.getFailure()).isEqualTo(FAILURE); - } -} diff --git a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/devicetype/DeviceTypeTest.java b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/devicetype/DeviceTypeTest.java index 26cc4af..28432b3 100644 --- a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/devicetype/DeviceTypeTest.java +++ b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/devicetype/DeviceTypeTest.java @@ -2,7 +2,6 @@ import org.junit.Test; import org.reparationservice.entities.devicetype.DeviceType; -import org.reparationservice.inmemory.InMemoryConfigurator; import static org.assertj.core.api.Assertions.assertThat; @@ -11,7 +10,7 @@ public class DeviceTypeTest { @Test public void testDeviceTypeImpl() { - DeviceType device = InMemoryConfigurator.getNewDeviceType(DEVICE_DESCRIPTION); + DeviceType device = new DeviceTypeImpl(DEVICE_DESCRIPTION); assertThat(device).isNotNull(); assertThat(device.getDescription()).isEqualTo(DEVICE_DESCRIPTION); } diff --git a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/worker/WorkerTest.java b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/worker/WorkerTest.java index 149a495..fc9e7df 100644 --- a/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/worker/WorkerTest.java +++ b/core-impl/inmemory-entities/src/test/java/org/reparationservice/inmemory/entities/worker/WorkerTest.java @@ -3,7 +3,6 @@ import org.junit.Test; import org.reparationservice.entities.worker.Worker; import org.reparationservice.entities.worker.WorkerDTO; -import org.reparationservice.inmemory.InMemoryConfigurator; import static org.assertj.core.api.Assertions.assertThat; @@ -12,7 +11,7 @@ public class WorkerTest { @Test public void createWorker() { - Worker worker = InMemoryConfigurator.getNewWorker(new WorkerDTO(USER_NAME)); + Worker worker = new WorkerImpl(new WorkerDTO(USER_NAME)); assertThat(worker).isNotNull(); assertThat(worker.getUserName()).isEqualTo(USER_NAME); } diff --git a/core/entities/src/main/java/org/reparationservice/entities/customer/Reparation.java b/core/entities/src/main/java/org/reparationservice/entities/customer/Reparation.java index c207cee..f19553b 100644 --- a/core/entities/src/main/java/org/reparationservice/entities/customer/Reparation.java +++ b/core/entities/src/main/java/org/reparationservice/entities/customer/Reparation.java @@ -5,7 +5,7 @@ public abstract class Reparation { public static final Reparation NULL = new Reparation() { @Override - public String getFailure() { + public String getDeviceFailure() { return ""; } @@ -17,5 +17,5 @@ public DateTime getCreationDate() { public abstract DateTime getCreationDate(); - public abstract String getFailure(); + public abstract String getDeviceFailure(); } diff --git a/core/entities/src/main/java/org/reparationservice/entities/customer/ReparationDTO.java b/core/entities/src/main/java/org/reparationservice/entities/customer/ReparationDTO.java index ca0e7e0..7a6333a 100644 --- a/core/entities/src/main/java/org/reparationservice/entities/customer/ReparationDTO.java +++ b/core/entities/src/main/java/org/reparationservice/entities/customer/ReparationDTO.java @@ -2,20 +2,12 @@ import org.joda.time.DateTime; -public final class ReparationDTO { - private final DateTime creationDate; - private final String failure; +public interface ReparationDTO { + DateTime getCreationDate(); - public ReparationDTO(DateTime creationDate, String failure) { - this.creationDate = creationDate; - this.failure = failure; - } + String getDeviceFailure(); - public DateTime getCreationDate() { - return creationDate; - } + String getUrgency(); - public String getFailure() { - return failure; - } + String getObservations(); } diff --git a/core/entities/src/test/java/org/reparationservice/entities/customer/ReparationTest.java b/core/entities/src/test/java/org/reparationservice/entities/customer/ReparationTest.java index 41306ae..5abdb54 100644 --- a/core/entities/src/test/java/org/reparationservice/entities/customer/ReparationTest.java +++ b/core/entities/src/test/java/org/reparationservice/entities/customer/ReparationTest.java @@ -3,33 +3,17 @@ import static org.assertj.core.api.Assertions.assertThat; import org.joda.time.DateTime; -import org.junit.Before; import org.junit.Test; public class ReparationTest { - private static final String FAILURE = "failure"; - private static final DateTime CREATION_DATE = DateTime.now(); - private static final Object INVALID_CREATION_DATE = null; - private static final Object EMPTY_FAILURE = ""; - private ReparationDTO reparationDTO; - - @Before - public void givenReparationDTO() { - reparationDTO = new ReparationDTO(CREATION_DATE, FAILURE); - } - - @Test - public void testReparationDTO() { - assertThat(reparationDTO).isNotNull(); - assertThat(reparationDTO.getCreationDate()).isEqualTo(CREATION_DATE); - assertThat(reparationDTO.getFailure()).isEqualTo(FAILURE); - } + private static final DateTime INVALID_CREATION_DATE = null; + private static final String EMPTY_FAILURE = ""; @Test public void testSpecialCase() { Reparation rep = Reparation.NULL; assertThat(rep).isNotNull(); assertThat(rep.getCreationDate()).isEqualTo(INVALID_CREATION_DATE); - assertThat(rep.getFailure()).isEqualTo(EMPTY_FAILURE); + assertThat(rep.getDeviceFailure()).isEqualTo(EMPTY_FAILURE); } } diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationInteractor.java b/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationInteractor.java index d7e8f64..18a2920 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationInteractor.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationInteractor.java @@ -3,7 +3,6 @@ import org.reparationservice.entities.customer.Customer; import org.reparationservice.entities.customer.CustomerGateway; import org.reparationservice.entities.customer.Device; -import org.reparationservice.entities.customer.ReparationDTO; import org.reparationservice.requestor.UseCaseActivator; import org.reparationservice.requestor.UseCaseRequest; @@ -28,10 +27,8 @@ public void execute(UseCaseRequest request) { } private void addReparation(AddReparationRequest repReq) { - ReparationDTO reparationDTO = new ReparationDTO( - repReq.getCreationDate(), repReq.getDeviceFailure()); customers.getCustomerById(repReq.getCustomerId()) - .getDevice(repReq.getDeviceSerialNumber()).addReparation(reparationDTO); + .getDevice(repReq.getDeviceSerialNumber()).addReparation(repReq); } private boolean deviceIsFound(AddReparationRequest repReq) { diff --git a/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationRequest.java b/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationRequest.java index 7b1da3b..ea29340 100644 --- a/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationRequest.java +++ b/core/usecases/src/main/java/org/reparationservice/usecases/reparation/add/AddReparationRequest.java @@ -2,9 +2,10 @@ import org.joda.time.DateTime; +import org.reparationservice.entities.customer.ReparationDTO; import org.reparationservice.requestor.UseCaseRequest; -public class AddReparationRequest implements UseCaseRequest, AddReparationResponder { +public class AddReparationRequest implements UseCaseRequest, AddReparationResponder, ReparationDTO { private final long customerId; private final DateTime creationDate; private final long deviceSerialNumber; @@ -29,23 +30,23 @@ public long getCustomerId() { return customerId; } - public DateTime getCreationDate() { - return creationDate; - } - public long getDeviceSerialNumber() { return deviceSerialNumber; } - public String getDeviceFailure() { + @Override public DateTime getCreationDate() { + return creationDate; + } + + @Override public String getDeviceFailure() { return failure; } - public String getUrgency() { + @Override public String getUrgency() { return urgency; } - public String getObservations() { + @Override public String getObservations() { return observations; } diff --git a/core/usecases/src/test/java/org/reparationservice/doubles/CustomerGatewaySpy.java b/core/usecases/src/test/java/org/reparationservice/doubles/CustomerGatewaySpy.java index d86a7fe..ddc8b3b 100644 --- a/core/usecases/src/test/java/org/reparationservice/doubles/CustomerGatewaySpy.java +++ b/core/usecases/src/test/java/org/reparationservice/doubles/CustomerGatewaySpy.java @@ -4,73 +4,85 @@ import org.reparationservice.entities.customer.*; public class CustomerGatewaySpy implements CustomerGateway { - private Customer customer; - - public CustomerGatewaySpy() { - customer = Customer.NULL; - } - - @Override - public Customer getCustomerById(long customerId) { - return customer; - } - - @Override - public void addCustomer(final long customerId) { - customer = new Customer() { - Device device = Device.NULL; - @Override - public long getId() { - return customerId; - } - - @Override - public Device getDevice(long deviceSerialNumber) { - return device; - } - - @Override - public void addDevice(final long deviceSerialNumber) { - device = new Device() { - Reparation reparation = Reparation.NULL; - @Override - public long getSerialNumber() { - return deviceSerialNumber; - } - - @Override - public Reparation getReparation(DateTime creationDate) { - return reparation; - } - - @Override - public void addReparation(final ReparationDTO reparationDTO) { - reparation = new Reparation() { - @Override - public DateTime getCreationDate() { - return reparationDTO.getCreationDate(); - } - - @Override - public String getFailure() { - return reparationDTO.getFailure(); - } - }; - } - }; - } - }; - } - - protected void addDevice (long deviceSerialNumber){ - customer.addDevice(deviceSerialNumber); - } - - public boolean addCustomerWasCalled() { - return customer != Customer.NULL; - } - - public Customer getCustomer() { - return customer; - } + private Customer customer = Customer.NULL; + private int addCustomerTimesCalled = 0; + private int addReparationCalledTimes = 0; + private ReparationDTO repDTO; + + @Override + public Customer getCustomerById(long customerId) { + return customer; + } + + @Override + public void addCustomer(final long customerId) { + addCustomerTimesCalled++; + customer = new Customer() { + Device device = Device.NULL; + + @Override + public long getId() { + return customerId; + } + + @Override + public Device getDevice(long deviceSerialNumber) { + return device; + } + + @Override + public void addDevice(final long deviceSerialNumber) { + device = new Device() { + Reparation reparation = Reparation.NULL; + + @Override + public long getSerialNumber() { + return deviceSerialNumber; + } + + @Override + public Reparation getReparation(DateTime creationDate) { + return reparation; + } + + @Override + public void addReparation(final ReparationDTO reparationDTO) { + addReparationCalledTimes++; + repDTO = reparationDTO; + reparation = new Reparation() { + @Override + public DateTime getCreationDate() { + return reparationDTO.getCreationDate(); + } + + @Override + public String getDeviceFailure() { + return reparationDTO.getDeviceFailure(); + } + }; + } + }; + } + }; + } + + protected Customer getCustomer() { + return customer; + } + + public int addCustomerTimesCalled() { + return addCustomerTimesCalled; + } + + public boolean addCustomerWasCalledWith(long customerId) { + return customer.getId() == customerId; + } + + public int addReparationCalledTimes() { + return addReparationCalledTimes; + } + + public boolean addReparationWasCalledWith(ReparationDTO reparationDTO) { + return repDTO != null && (repDTO.equals(reparationDTO)); + } } diff --git a/core/usecases/src/test/java/org/reparationservice/doubles/CustomerGwWithDeviceSpy.java b/core/usecases/src/test/java/org/reparationservice/doubles/CustomerGwWithDeviceSpy.java new file mode 100644 index 0000000..5176aa5 --- /dev/null +++ b/core/usecases/src/test/java/org/reparationservice/doubles/CustomerGwWithDeviceSpy.java @@ -0,0 +1,10 @@ +package org.reparationservice.doubles; + +public class CustomerGwWithDeviceSpy extends NotEmptyCustomerSpy { + public static final long DEVICE_SERIAL_NUMBER = 10; + + public CustomerGwWithDeviceSpy() { + super(); + this.getCustomer().addDevice(DEVICE_SERIAL_NUMBER); + } +} diff --git a/core/usecases/src/test/java/org/reparationservice/doubles/CustomerWithDeviceSpy.java b/core/usecases/src/test/java/org/reparationservice/doubles/CustomerWithDeviceSpy.java deleted file mode 100644 index b916280..0000000 --- a/core/usecases/src/test/java/org/reparationservice/doubles/CustomerWithDeviceSpy.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.reparationservice.doubles; - -public class CustomerWithDeviceSpy extends NotEmptyCustomerSpy { - public static final long DEVICE_SERIAL_NUMBER = 10; - - public CustomerWithDeviceSpy() { - super(); - this.addDevice(DEVICE_SERIAL_NUMBER); - } -} 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 d3bde3d..6021fc0 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 @@ -8,7 +8,6 @@ import org.junit.runner.RunWith; import org.reparationservice.doubles.CustomerGatewaySpy; -import org.reparationservice.entities.customer.Customer; import org.reparationservice.requestor.UseCaseActivator; @RunWith(HierarchicalContextRunner.class) @@ -23,7 +22,7 @@ public void setUp() { @Test public void gatewayWasNotCalledWhenInteractorNotYetExecuted() { - assertThat(customersSpy.addCustomerWasCalled()).isFalse(); + assertThat(customersSpy.addCustomerTimesCalled()).isEqualTo(0); } @Test(expected = AddCustomerInteractor.CustomerGatewayCannotBeNull.class) @@ -49,10 +48,9 @@ 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); + assertThat(customersSpy.addCustomerTimesCalled()).isEqualTo(1); + assertThat(customersSpy.addCustomerWasCalledWith(CUSTOMER_ID)).isTrue(); + } } } diff --git a/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationInteractorTest.java b/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationInteractorTest.java index 7bdd12c..e238f3b 100644 --- a/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationInteractorTest.java +++ b/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationInteractorTest.java @@ -9,10 +9,10 @@ import de.bechte.junit.runners.context.HierarchicalContextRunner; import org.reparationservice.doubles.CustomerGatewaySpy; -import org.reparationservice.doubles.CustomerWithDeviceSpy; +import org.reparationservice.doubles.CustomerGwWithDeviceSpy; import org.reparationservice.doubles.NotEmptyCustomerSpy; import org.reparationservice.entities.customer.CustomerGateway; -import org.reparationservice.entities.customer.Reparation; +import org.reparationservice.entities.customer.ReparationDTO; import org.reparationservice.requestor.UseCaseActivator; import org.reparationservice.requestor.UseCaseRequest; @@ -21,7 +21,7 @@ public class AddReparationInteractorTest { private static final int CUSTOMER_ID = NotEmptyCustomerSpy.CUSTOMER_ID; private static final String DEVICE_FAILURE = "failure"; private static final DateTime CREATION_DATE = DateTime.now(); - private static final long DEVICE_SERIAL_NUMBER = CustomerWithDeviceSpy.DEVICE_SERIAL_NUMBER; + private static final long DEVICE_SERIAL_NUMBER = CustomerGwWithDeviceSpy.DEVICE_SERIAL_NUMBER; private static final String REPARATION_OBSERVATIONS = "observations"; private static final String REPARATION_URGENCY = "urgency"; @@ -45,12 +45,16 @@ public void callCustomerNotFoundWhenProvidedCustomerIdDoesNotMatchWithAnyCustome assertThat(((AddReparationResponderSpy) responder).customerNotFoundWasCalledTimes()) .isEqualTo(1); + assertThat(((CustomerGatewaySpy) emptyCustomersSpy).addReparationCalledTimes()). + isEqualTo(0); } public class CustomerIsFound { + private CustomerGateway notEmptyCustomerSpy; + @Before public void givenCustomer() { - CustomerGateway notEmptyCustomerSpy = new NotEmptyCustomerSpy(); + notEmptyCustomerSpy = new NotEmptyCustomerSpy(); addReparation = new AddReparationInteractor(notEmptyCustomerSpy); } @@ -59,33 +63,27 @@ public void callDeviceNotFoundWhenDeviceSerialNumberNotFound() { addReparation.execute(request); assertThat(((AddReparationResponderSpy) responder).deviceNotFoundWasCalledTimes()) .isEqualTo(1); + assertThat(((NotEmptyCustomerSpy) notEmptyCustomerSpy).addReparationCalledTimes()). + isEqualTo(0); } public class DeviceIsFound { - private CustomerGatewaySpy customerWithDeviceSpy; + private CustomerGateway customerGwWithDevice; @Before public void givenDevice() { - customerWithDeviceSpy = new CustomerWithDeviceSpy(); - addReparation = new AddReparationInteractor(customerWithDeviceSpy); + customerGwWithDevice = new CustomerGwWithDeviceSpy(); + addReparation = new AddReparationInteractor(customerGwWithDevice); } @Test public void addReparation() { addReparation.execute(request); - Reparation reparation = getReparation(DEVICE_SERIAL_NUMBER, - CREATION_DATE, customerWithDeviceSpy); - assertThat(reparation).isNotNull(); - assertThat(reparation.getCreationDate()).isEqualTo( - CREATION_DATE); - } - - private Reparation getReparation(long deviceSerialNumber, - DateTime creationDate, CustomerGatewaySpy customerGatewaySpy) { - return customerGatewaySpy.getCustomer() - .getDevice(deviceSerialNumber) - .getReparation(creationDate); + CustomerGwWithDeviceSpy customerGwWithDeviceSpy = (CustomerGwWithDeviceSpy) customerGwWithDevice; + assertThat(customerGwWithDeviceSpy.addReparationCalledTimes()).isEqualTo(1); + ReparationDTO reparationDTO = (AddReparationRequest)request; + assertThat(customerGwWithDeviceSpy.addReparationWasCalledWith(reparationDTO)).isTrue(); } } } diff --git a/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationRequestTest.java b/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationRequestTest.java index f1477cf..bfdc505 100644 --- a/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationRequestTest.java +++ b/core/usecases/src/test/java/org/reparationservice/usecases/reparation/add/AddReparationRequestTest.java @@ -3,16 +3,14 @@ import org.joda.time.DateTime; import org.junit.Before; import org.junit.Test; -import org.reparationservice.doubles.CustomerWithDeviceSpy; -import org.reparationservice.doubles.NotEmptyCustomerSpy; import static org.assertj.core.api.Assertions.assertThat; public class AddReparationRequestTest { - private static final int CUSTOMER_ID = NotEmptyCustomerSpy.CUSTOMER_ID; + private static final int CUSTOMER_ID = 1; private static final String DEVICE_FAILURE = "failure"; private static final DateTime CREATION_DATE = DateTime.now(); - private static final long DEVICE_SERIAL_NUMBER = CustomerWithDeviceSpy.DEVICE_SERIAL_NUMBER; + private static final long DEVICE_SERIAL_NUMBER = 1; private static final String REPARATION_OBSERVATIONS = "observations"; private static final String REPARATION_URGENCY = "urgency"; private AddReparationResponder responder;