Skip to content

Commit

Permalink
feat : persist phoneNumber and make endpoint as saveOrRetrieve (#765)
Browse files Browse the repository at this point in the history
* feat : persist phoneNumber and make endpoint as saveOrRetrieve

* fix : issue with Integrationtests

* fix : Integration Tests Issue

* feat : send phone number as response

* adds negative test case
  • Loading branch information
rajadilipkolli authored Jun 17, 2024
1 parent d351233 commit 0755a56
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void run(String... args) {
.setName(faker.name().fullName())
.setEmail(faker.name().lastName() + "@gmail.com")
.setAddress(faker.address().fullAddress())
.setPhone(faker.phoneNumber().phoneNumber())
.setAmountAvailable(count)
.setAmountReserved(0);
customerList.add(customer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class Customer {

private String address;

private String phone;

private int amountAvailable;

private int amountReserved;
Expand Down Expand Up @@ -61,6 +63,15 @@ public Customer setAddress(final String address) {
return this;
}

public String getPhone() {
return phone;
}

public Customer setPhone(String phone) {
this.phone = phone;
return this;
}

public Customer setAmountAvailable(final int amountAvailable) {
this.amountAvailable = amountAvailable;
return this;
Expand All @@ -78,6 +89,8 @@ public String toString() {
+ this.getName()
+ ", email="
+ this.getEmail()
+ ", phone="
+ this.getPhone()
+ ", address="
+ this.getAddress()
+ ", amountAvailable="
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public Customer toEntity(CustomerRequest customerRequest) {
customer.setName(customerRequest.name());
customer.setEmail(customerRequest.email());
customer.setAddress(customerRequest.address());
customer.setPhone(customerRequest.phone());
customer.setAmountAvailable(customerRequest.amountAvailable());
return customer;
}
Expand All @@ -24,6 +25,7 @@ public CustomerResponse toResponse(Customer customer) {
customer.getId(),
customer.getName(),
customer.getEmail(),
customer.getPhone(),
customer.getAddress(),
customer.getAmountAvailable());
}
Expand All @@ -33,6 +35,7 @@ public void mapCustomerWithRequest(Customer customer, CustomerRequest customerRe
customer.setName(customerRequest.name());
customer.setAddress(customerRequest.address());
customer.setEmail(customerRequest.email());
customer.setPhone(customerRequest.phone());
}

public List<CustomerResponse> toListResponse(List<Customer> customerList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public record CustomerRequest(
@NotBlank(message = "Name cannot be Blank") String name,
@NotBlank(message = "Email cannot be Blank") @Email(message = "supplied email is not valid")
String email,
@NotBlank(message = "Customer Phone number is required") String phone,
String address,
@Positive(message = "AmountAvailable must be greater than 0") int amountAvailable) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
package com.example.paymentservice.model.response;

public record CustomerResponse(
Long customerId, String name, String email, String address, int amountAvailable) {}
Long customerId,
String name,
String email,
String phone,
String address,
int amountAvailable) {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface CustomerRepository {

Optional<Customer> findById(Long customerId);

Optional<Customer> findByEmail(String email);

Customer save(Customer customer);

void deleteById(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public Optional<CustomerResponse> findByName(String name) {
CUSTOMERS.ID,
CUSTOMERS.NAME,
CUSTOMERS.EMAIL,
CUSTOMERS.PHONE,
CUSTOMERS.ADDRESS,
CUSTOMERS.AMOUNT_AVAILABLE)
.from(CUSTOMERS)
Expand All @@ -68,6 +69,13 @@ public Optional<Customer> findById(Long customerId) {
.map(r -> r.into(Customer.class));
}

@Override
public Optional<Customer> findByEmail(String customerEmail) {
return dslContext
.fetchOptional(CUSTOMERS, CUSTOMERS.EMAIL.eq(customerEmail))
.map(r -> r.into(Customer.class));
}

@Override
@Transactional
public Customer save(Customer customer) {
Expand All @@ -86,6 +94,7 @@ public Customer save(Customer customer) {
.set(CUSTOMERS.ADDRESS, customer.getAddress())
.set(CUSTOMERS.NAME, customer.getName())
.set(CUSTOMERS.EMAIL, customer.getEmail())
.set(CUSTOMERS.PHONE, customer.getPhone())
.where(CUSTOMERS.ID.eq(customer.getId()))
.returningResult()
.fetchOneInto(Customer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@ public Optional<CustomerResponse> findCustomerByName(String name) {

@Transactional
public CustomerResponse saveCustomer(CustomerRequest customerRequest) {
Customer customer = customerMapper.toEntity(customerRequest);
return customerMapper.toResponse(customerRepository.save(customer));
Optional<Customer> customerByEmail =
customerRepository.findByEmail(customerRequest.email());
Customer customer;
if (customerByEmail.isPresent()) {
customer = customerByEmail.get();
} else {
Customer requestCustomer = customerMapper.toEntity(customerRequest);
customer = customerRepository.save(requestCustomer);
}
return customerMapper.toResponse(customer);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<column name="email" type="${string.type}">
<constraints nullable="false"/>
</column>
<column name="phone" type="${string.type}">
<constraints nullable="false"/>
</column>
<column name="address" type="${string.type}"/>
<column name="amount_available" type="int"/>
<column name="amount_reserved" type="int"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void setUp() {
new Customer()
.setName("First Customer")
.setEmail("[email protected]")
.setPhone("1234567890")
.setAddress("First Address")
.setAmountAvailable(100)
.setAmountReserved(10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,21 @@ void setUp() {
new Customer()
.setName("First Customer")
.setEmail("[email protected]")
.setPhone("9876543210")
.setAddress("First Address")
.setAmountAvailable(100)
.setAmountReserved(0),
new Customer()
.setName("Second Customer")
.setEmail("[email protected]")
.setPhone("9876543210")
.setAddress("Second Address")
.setAmountAvailable(100)
.setAmountReserved(0),
new Customer()
.setName("Third Customer")
.setEmail("[email protected]")
.setPhone("9876543210")
.setAddress("Third Address")
.setAmountAvailable(100)
.setAmountReserved(0));
Expand Down Expand Up @@ -83,6 +86,7 @@ void shouldFindCustomerById() throws Exception {
.andExpect(jsonPath("$.customerId", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.email", is(customer.getEmail())))
.andExpect(jsonPath("$.phone", is(customer.getPhone())))
.andExpect(jsonPath("$.address", is(customer.getAddress())))
.andExpect(jsonPath("$.amountAvailable", is(customer.getAmountAvailable())));
}
Expand Down Expand Up @@ -116,6 +120,7 @@ void shouldFindCustomerByName() throws Exception {
.andExpect(jsonPath("$.customerId", is(customer.getId()), Long.class))
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.email", is(customer.getEmail())))
.andExpect(jsonPath("$.phone", is(customer.getPhone())))
.andExpect(jsonPath("$.address", is(customer.getAddress())))
.andExpect(jsonPath("$.amountAvailable", is(customer.getAmountAvailable())));
}
Expand All @@ -124,7 +129,11 @@ void shouldFindCustomerByName() throws Exception {
void shouldCreateNewCustomer() throws Exception {
CustomerRequest customerRequest =
new CustomerRequest(
"New Customer", "[email protected]", "First Address", 10_000);
"New Customer",
"[email protected]",
"1234567890",
"First Address",
10_000);
this.mockMvc
.perform(
post("/api/customers")
Expand All @@ -135,13 +144,39 @@ void shouldCreateNewCustomer() throws Exception {
.andExpect(jsonPath("$.customerId", notNullValue(Long.class)))
.andExpect(jsonPath("$.name", is(customerRequest.name())))
.andExpect(jsonPath("$.email", is(customerRequest.email())))
.andExpect(jsonPath("$.phone", is(customerRequest.phone())))
.andExpect(jsonPath("$.address", is(customerRequest.address())))
.andExpect(jsonPath("$.amountAvailable", is(customerRequest.amountAvailable())));
}

@Test
void shouldReturnWithNoErrorCreatingExistingCustomer() throws Exception {
Customer customer = customerList.getFirst();
CustomerRequest customerRequest =
new CustomerRequest(
customer.getName(),
customer.getEmail(),
customer.getPhone(),
customer.getAddress(),
customer.getAmountAvailable());
this.mockMvc
.perform(
post("/api/customers")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(customerRequest)))
.andExpect(status().isCreated())
.andExpect(header().exists(HttpHeaders.LOCATION))
.andExpect(jsonPath("$.customerId", notNullValue(Long.class)))
.andExpect(jsonPath("$.name", is(customerRequest.name())))
.andExpect(jsonPath("$.email", is(customerRequest.email())))
.andExpect(jsonPath("$.phone", is(customerRequest.phone())))
.andExpect(jsonPath("$.address", is(customerRequest.address())))
.andExpect(jsonPath("$.amountAvailable", is(customerRequest.amountAvailable())));
}

@Test
void shouldReturn400WhenCreateNewCustomerWithoutNameAndEmail() throws Exception {
CustomerRequest customer = new CustomerRequest(null, null, null, 0);
CustomerRequest customer = new CustomerRequest(null, null, null, null, 0);

this.mockMvc
.perform(
Expand All @@ -159,7 +194,7 @@ void shouldReturn400WhenCreateNewCustomerWithoutNameAndEmail() throws Exception
is("https://zalando.github.io/problem/constraint-violation")))
.andExpect(jsonPath("$.title", is("Constraint Violation")))
.andExpect(jsonPath("$.status", is(400)))
.andExpect(jsonPath("$.violations", hasSize(3)))
.andExpect(jsonPath("$.violations", hasSize(4)))
.andExpect(jsonPath("$.violations[0].field", is("amountAvailable")))
.andExpect(
jsonPath(
Expand All @@ -176,7 +211,8 @@ void shouldReturn400WhenCreateNewCustomerWithoutNameAndEmail() throws Exception
void shouldUpdateCustomer() throws Exception {
Long customerId = customerList.getFirst().getId();
CustomerRequest customerRequest =
new CustomerRequest("Updated text", "[email protected]", "First Address", 500);
new CustomerRequest(
"Updated text", "[email protected]", "1234567890", "First Address", 500);

this.mockMvc
.perform(
Expand All @@ -188,6 +224,7 @@ void shouldUpdateCustomer() throws Exception {
.andExpect(jsonPath("$.name", is(customerRequest.name())))
.andExpect(jsonPath("$.email", is(customerRequest.email())))
.andExpect(jsonPath("$.address", is(customerRequest.address())))
.andExpect(jsonPath("$.phone", is(customerRequest.phone())))
.andExpect(jsonPath("$.amountAvailable", is(customerRequest.amountAvailable())));
}

Expand All @@ -196,7 +233,11 @@ void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception {
long customerId = customerList.getFirst().getId() + 99_999;
CustomerRequest customerRequest =
new CustomerRequest(
"Updated text", "[email protected]", "First Address", 10_000);
"Updated text",
"[email protected]",
"1234567890",
"First Address",
10_000);

this.mockMvc
.perform(
Expand Down Expand Up @@ -227,6 +268,7 @@ void shouldDeleteCustomer() throws Exception {
.andExpect(jsonPath("$.name", is(customer.getName())))
.andExpect(jsonPath("$.email", is(customer.getEmail())))
.andExpect(jsonPath("$.address", is(customer.getAddress())))
.andExpect(jsonPath("$.phone", is(customer.getPhone())))
.andExpect(jsonPath("$.amountAvailable", is(customer.getAmountAvailable())));
}

Expand Down
Loading

0 comments on commit 0755a56

Please sign in to comment.