-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from alvinmarshall/5-endpoint-to-update-customer
chore: update customer endpoint
- Loading branch information
Showing
8 changed files
with
207 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,29 @@ static String createCustomerWithAddress(ObjectMapper mapper) throws JsonProcessi | |
.build(); | ||
return mapper.writeValueAsString(customerDto); | ||
} | ||
|
||
static String updateCustomer(ObjectMapper mapper) throws JsonProcessingException { | ||
CustomerDto.CreateCustomer customerDto = CustomerDto.CreateCustomer.builder() | ||
.firstName("Theresia") | ||
.lastName("Macejkovic") | ||
.emailAddress("[email protected]") | ||
.build(); | ||
return mapper.writeValueAsString(customerDto); | ||
} | ||
|
||
static String updateCustomerWithAddress(ObjectMapper mapper) throws JsonProcessingException { | ||
CustomerDto.CreateCustomer customerDto = CustomerDto.CreateCustomer.builder() | ||
.firstName("Vito") | ||
.lastName("Kovacek") | ||
.emailAddress("[email protected]") | ||
.customerAddress(Set.of(CustomerDto.CustomerAddress.builder() | ||
.city("Phillipshire") | ||
.country("USA") | ||
.streetAddress("68578 Champlin Mill") | ||
.stateCode("NJ") | ||
.zipCode("28711") | ||
.build())) | ||
.build(); | ||
return mapper.writeValueAsString(customerDto); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,4 +100,83 @@ void getCustomer_throw_if_customer_not_found() { | |
); | ||
assertEquals("Customer with id 1 not found", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void updateCustomer() { | ||
CustomerDto.UpdateCustomer updateCustomerDto = CustomerDto.UpdateCustomer.builder() | ||
.firstName("Update Claribel") | ||
.lastName("Update Zieme") | ||
.emailAddress("Update [email protected]") | ||
.build(); | ||
|
||
Mockito.when(customerRepository.findByIdWithAddress(ArgumentMatchers.anyLong())) | ||
.thenReturn(Optional.of(Customer.builder().id(1L) | ||
.firstName("Claribel") | ||
.lastName("Zieme") | ||
.emailAddress("[email protected]") | ||
.build())); | ||
|
||
sut.updateCustomer(1L, updateCustomerDto); | ||
Mockito.verify(customerRepository, Mockito.atMostOnce()).save(customerArgumentCaptor.capture()); | ||
Customer customer = customerArgumentCaptor.getValue(); | ||
assertNotNull(customer); | ||
assertEquals("Update Claribel", customer.getFirstName()); | ||
assertEquals("Update Zieme", customer.getLastName()); | ||
assertEquals("Update [email protected]", customer.getEmailAddress()); | ||
} | ||
|
||
@Test | ||
void updateCustomerWithAddress() { | ||
CustomerDto.UpdateCustomer updateCustomerDto = CustomerDto.UpdateCustomer.builder() | ||
.firstName("Update Claribel") | ||
.lastName("Update Zieme") | ||
.emailAddress("Update [email protected]") | ||
.customerAddress(Set.of(CustomerDto.CustomerAddress.builder() | ||
.city("Emmerichmouth") | ||
.country("USA") | ||
.streetAddress("Suite 290 6898 King Village") | ||
.stateCode("PA") | ||
.zipCode("29665") | ||
.build())) | ||
.build(); | ||
|
||
Mockito.when(customerRepository.findByIdWithAddress(ArgumentMatchers.anyLong())) | ||
.thenReturn(Optional.of(Customer.builder().id(1L) | ||
.firstName("Claribel") | ||
.lastName("Zieme") | ||
.emailAddress("[email protected]") | ||
.addresses(Set.of(Address.builder() | ||
.city("Risaberg") | ||
.country("USA") | ||
.streetAddress("942 Walker Street") | ||
.stateCode("WV") | ||
.zipCode("88742") | ||
.build())) | ||
.build())); | ||
|
||
sut.updateCustomer(1L, updateCustomerDto); | ||
Mockito.verify(customerRepository, Mockito.atMostOnce()).save(customerArgumentCaptor.capture()); | ||
Customer customer = customerArgumentCaptor.getValue(); | ||
assertNotNull(customer); | ||
assertEquals("Update Claribel", customer.getFirstName()); | ||
assertEquals("Update Zieme", customer.getLastName()); | ||
assertEquals("Update [email protected]", customer.getEmailAddress()); | ||
assertEquals(2, customer.getAddresses().size()); | ||
} | ||
|
||
@Test | ||
void updateCustomer_throw_if_customer_not_found() { | ||
CustomerDto.UpdateCustomer updateCustomerDto = CustomerDto.UpdateCustomer.builder() | ||
.firstName("Update Claribel") | ||
.lastName("Update Zieme") | ||
.emailAddress("Update [email protected]") | ||
.build(); | ||
|
||
EntityNotFoundException exception = Assertions.assertThrows( | ||
EntityNotFoundException.class, | ||
() -> sut.updateCustomer(1L, updateCustomerDto) | ||
); | ||
assertEquals("Customer with id 1 not found", exception.getMessage()); | ||
} | ||
|
||
} |