diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
index d6bfb24..712ab9d 100644
--- a/.idea/jarRepositories.xml
+++ b/.idea/jarRepositories.xml
@@ -11,16 +11,6 @@
-
-
-
-
-
-
-
-
-
-
diff --git a/.idea/misc.xml b/.idea/misc.xml
index a04c3e5..4dcb8a2 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -7,5 +7,5 @@
-
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 94a25f7..35eb1dd 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/src/main/java/com/amigoscode/testing/customer/CustomerRegistrationService.java b/src/main/java/com/amigoscode/testing/customer/CustomerRegistrationService.java
index 451f60b..640bd9f 100644
--- a/src/main/java/com/amigoscode/testing/customer/CustomerRegistrationService.java
+++ b/src/main/java/com/amigoscode/testing/customer/CustomerRegistrationService.java
@@ -1,5 +1,6 @@
package com.amigoscode.testing.customer;
+import com.amigoscode.testing.utils.PhoneNumberValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -10,15 +11,23 @@
public class CustomerRegistrationService {
private final CustomerRepository customerRepository;
+ private final PhoneNumberValidator phoneNumberValidator;
@Autowired
- public CustomerRegistrationService(CustomerRepository customerRepository) {
+ public CustomerRegistrationService(CustomerRepository customerRepository, PhoneNumberValidator phoneNumberValidator) {
this.customerRepository = customerRepository;
+
+ this.phoneNumberValidator = phoneNumberValidator;
}
public void registerNewCustomer(CustomerRegistrationRequest request) {
String phoneNumber = request.getCustomer().getPhoneNumber();
+
+ //TODO: Validate that phone number is valid
+ if (!phoneNumberValidator.test(phoneNumber)){
+ throw new IllegalStateException("Phone Number " + phoneNumber + " is not valid");
+ }
Optional customerOptional = customerRepository
.selectCustomerByPhoneNumber(phoneNumber);
diff --git a/src/main/java/com/amigoscode/testing/utils/PhoneNumberValidator.java b/src/main/java/com/amigoscode/testing/utils/PhoneNumberValidator.java
new file mode 100644
index 0000000..465d252
--- /dev/null
+++ b/src/main/java/com/amigoscode/testing/utils/PhoneNumberValidator.java
@@ -0,0 +1,14 @@
+package com.amigoscode.testing.utils;
+
+import java.util.function.Predicate;
+import org.springframework.stereotype.Service;
+
+@Service
+public class PhoneNumberValidator implements Predicate {
+
+ @Override
+ public boolean test(String phoneNumber) {
+ return phoneNumber.startsWith("+44") &&
+ phoneNumber.length() == 13;
+ }
+}
diff --git a/src/test/java/com/amigoscode/testing/customer/CustomerRegistrationServiceTest.java b/src/test/java/com/amigoscode/testing/customer/CustomerRegistrationServiceTest.java
index 0d48bcf..0ad3814 100644
--- a/src/test/java/com/amigoscode/testing/customer/CustomerRegistrationServiceTest.java
+++ b/src/test/java/com/amigoscode/testing/customer/CustomerRegistrationServiceTest.java
@@ -1,5 +1,6 @@
package com.amigoscode.testing.customer;
+import com.amigoscode.testing.utils.PhoneNumberValidator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -22,6 +23,9 @@ class CustomerRegistrationServiceTest {
@Mock
private CustomerRepository customerRepository;
+ @Mock
+ private PhoneNumberValidator phoneNumberValidator;
+
@Captor
private ArgumentCaptor customerArgumentCaptor;
@@ -30,7 +34,7 @@ class CustomerRegistrationServiceTest {
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
- underTest = new CustomerRegistrationService(customerRepository);
+ underTest = new CustomerRegistrationService(customerRepository, phoneNumberValidator);
}
@Test
@@ -46,6 +50,9 @@ void itShouldSaveNewCustomer() {
given(customerRepository.selectCustomerByPhoneNumber(phoneNumber))
.willReturn(Optional.empty());
+ //...Valid phone number
+ given(phoneNumberValidator.test(phoneNumber)).willReturn(true);
+
// When
underTest.registerNewCustomer(request);
@@ -56,10 +63,10 @@ void itShouldSaveNewCustomer() {
}
@Test
- void itShouldSaveNewCustomerWhenIdIsNull() {
+ void itShouldNotSaveNewCustomerWhenPhoneNumberIsInvalid() {
// Given a phone number and a customer
String phoneNumber = "000099";
- Customer customer = new Customer(null, "Maryam", phoneNumber);
+ Customer customer = new Customer(UUID.randomUUID(), "Maryam", phoneNumber);
// ... a request
CustomerRegistrationRequest request = new CustomerRegistrationRequest(customer);
@@ -68,6 +75,30 @@ void itShouldSaveNewCustomerWhenIdIsNull() {
given(customerRepository.selectCustomerByPhoneNumber(phoneNumber))
.willReturn(Optional.empty());
+ //...Valid phone number
+ given(phoneNumberValidator.test(phoneNumber)).willReturn(false);
+
+ // When
+ assertThatThrownBy(() -> underTest.registerNewCustomer(request))
+ .isInstanceOf(IllegalStateException.class)
+ .hasMessageContaining("Phone Number " + phoneNumber + " is not valid");
+
+ // Then
+ then(customerRepository).shouldHaveNoInteractions();
+ }
+
+ @Test
+ void itShouldSaveNewCustomerWhenIdIsNull() {
+ // Given a phone number and a customer
+ String phoneNumber = "000099";
+ Customer customer = new Customer(null, "Maryam", phoneNumber);
+
+ // ... a request
+ CustomerRegistrationRequest request = new CustomerRegistrationRequest(customer);
+
+ given(phoneNumberValidator.test(phoneNumber)).willReturn(true);
+
+
// When
underTest.registerNewCustomer(request);
@@ -92,6 +123,9 @@ void itShouldNotSaveCustomerWhenCustomerExists() {
given(customerRepository.selectCustomerByPhoneNumber(phoneNumber))
.willReturn(Optional.of(customer));
+ given(phoneNumberValidator.test(phoneNumber)).willReturn(true);
+
+
// When
underTest.registerNewCustomer(request);
@@ -113,6 +147,9 @@ void itShouldThrowWhenPhoneNumberIsTaken() {
given(customerRepository.selectCustomerByPhoneNumber(phoneNumber))
.willReturn(Optional.of(customerTwo));
+ given(phoneNumberValidator.test(phoneNumber)).willReturn(true);
+
+
// When
// Then
assertThatThrownBy(() -> underTest.registerNewCustomer(request))
diff --git a/src/test/java/com/amigoscode/testing/payment/PaymentIntegrationTest.java b/src/test/java/com/amigoscode/testing/payment/PaymentIntegrationTest.java
index 309c823..4f6f961 100644
--- a/src/test/java/com/amigoscode/testing/payment/PaymentIntegrationTest.java
+++ b/src/test/java/com/amigoscode/testing/payment/PaymentIntegrationTest.java
@@ -36,7 +36,7 @@ class PaymentIntegrationTest {
void itShouldCreatePaymentSuccessfully() throws Exception {
// Given a customer
UUID customerId = UUID.randomUUID();
- Customer customer = new Customer(customerId, "James", "0000000");
+ Customer customer = new Customer(customerId, "James", "+447000000000");
CustomerRegistrationRequest customerRegistrationRequest = new CustomerRegistrationRequest(customer);
diff --git a/src/test/java/com/amigoscode/testing/utils/PhoneNumberValidatorTest.java b/src/test/java/com/amigoscode/testing/utils/PhoneNumberValidatorTest.java
new file mode 100644
index 0000000..2761af3
--- /dev/null
+++ b/src/test/java/com/amigoscode/testing/utils/PhoneNumberValidatorTest.java
@@ -0,0 +1,31 @@
+package com.amigoscode.testing.utils;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import static org.assertj.core.api.Assertions.assertThat;
+
+class PhoneNumberValidatorTest {
+ private PhoneNumberValidator underTest;
+ @BeforeEach
+ void setUp() {
+ underTest = new PhoneNumberValidator();
+ }
+
+ @ParameterizedTest
+ @CsvSource({
+ "+447000000000, true",
+ "+447000000000878, false",
+ "447000000000, false"
+ })
+ void itShouldValidatePhoneNumber(String phoneNumber, boolean expected){
+ //When
+ boolean isValid = underTest.test(phoneNumber);
+
+ //Then
+ assertThat(isValid).isEqualTo(expected);
+ }
+
+}
diff --git a/testing.iml b/testing.iml
deleted file mode 100644
index 1a35667..0000000
--- a/testing.iml
+++ /dev/null
@@ -1,105 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file