-
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 #11 from alvinmarshall/4-endpoint-to-fetch-customer
chore: add fetch customers endpoint
- Loading branch information
Showing
11 changed files
with
220 additions
and
22 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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/cheise_proj/auditing/CustomerExceptionAdviser.java
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.cheise_proj.auditing; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ProblemDetail; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
class CustomerExceptionAdviser { | ||
|
||
@ExceptionHandler(value = {EntityNotFoundException.class}) | ||
@ResponseStatus(value = HttpStatus.NOT_FOUND) | ||
public ProblemDetail resourceNotFoundException(EntityNotFoundException ex) { | ||
return ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage()); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/cheise_proj/auditing/CustomerRepository.java
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 |
---|---|---|
@@ -1,6 +1,12 @@ | ||
package com.cheise_proj.auditing; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.EntityGraph; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
interface CustomerRepository extends JpaRepository<Customer, Long> { | ||
@EntityGraph(attributePaths = "addresses") | ||
@Override | ||
Page<Customer> findAll(Pageable pageable); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
spring: | ||
application: | ||
name: auditing | ||
|
||
jpa: | ||
open-in-view: false |
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
22 changes: 22 additions & 0 deletions
22
src/test/java/com/cheise_proj/auditing/CustomerExceptionAdviserTest.java
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.cheise_proj.auditing; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.http.ProblemDetail; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class CustomerExceptionAdviserTest { | ||
@InjectMocks | ||
private CustomerExceptionAdviser sut; | ||
|
||
@Test | ||
void resourceNotFoundException() { | ||
ProblemDetail notFound = sut.resourceNotFoundException(new EntityNotFoundException("not found")); | ||
assertNotNull(notFound); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,19 @@ | ||
package com.cheise_proj.auditing; | ||
|
||
import jakarta.persistence.EntityNotFoundException; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.*; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
@@ -37,7 +44,7 @@ void createCustomer() { | |
.emailAddress("[email protected]") | ||
.build(); | ||
sut.createCustomer(customerDto); | ||
Mockito.verify(customerRepository,Mockito.atMostOnce()).save(customerArgumentCaptor.capture()); | ||
Mockito.verify(customerRepository, Mockito.atMostOnce()).save(customerArgumentCaptor.capture()); | ||
Customer customer = customerArgumentCaptor.getValue(); | ||
assertNotNull(customer); | ||
assertEquals("Claribel", customer.getFirstName()); | ||
|
@@ -52,17 +59,45 @@ void createCustomerWithAddress() { | |
.lastName("Zieme") | ||
.emailAddress("[email protected]") | ||
.customerAddress(Set.of(CustomerDto.CustomerAddress.builder() | ||
.city("Risaberg") | ||
.country("USA") | ||
.streetAddress("942 Walker Street") | ||
.stateCode("WV") | ||
.zipCode("88742") | ||
.city("Risaberg") | ||
.country("USA") | ||
.streetAddress("942 Walker Street") | ||
.stateCode("WV") | ||
.zipCode("88742") | ||
.build())) | ||
.build(); | ||
sut.createCustomer(customerDto); | ||
Mockito.verify(customerRepository,Mockito.atMostOnce()).save(customerArgumentCaptor.capture()); | ||
Mockito.verify(customerRepository, Mockito.atMostOnce()).save(customerArgumentCaptor.capture()); | ||
Customer customer = customerArgumentCaptor.getValue(); | ||
assertNotNull(customer); | ||
assertEquals(1,customer.getAddresses().size()); | ||
assertEquals(1, customer.getAddresses().size()); | ||
} | ||
|
||
@Test | ||
void getCustomers() { | ||
List<Customer> customerList = List.of(Customer.builder().id(1L).build()); | ||
Mockito.when(customerRepository.findAll(ArgumentMatchers.any(Pageable.class))) | ||
.thenReturn(new PageImpl<>(customerList)); | ||
Page<Customer> customerPage = sut.getCustomers(Pageable.ofSize(1)); | ||
assertNotNull(customerPage); | ||
assertEquals(1, customerPage.getTotalElements()); | ||
assertEquals(1, customerPage.getContent().size()); | ||
} | ||
|
||
@Test | ||
void getCustomer() { | ||
Mockito.when(customerRepository.findById(ArgumentMatchers.anyLong())) | ||
.thenReturn(Optional.of(Customer.builder().id(1L).build())); | ||
Customer customer = sut.getCustomer(1L); | ||
assertNotNull(customer); | ||
} | ||
|
||
@Test | ||
void getCustomer_throw_if_customer_not_found() { | ||
EntityNotFoundException exception = Assertions.assertThrows( | ||
EntityNotFoundException.class, | ||
() -> sut.getCustomer(1L) | ||
); | ||
assertEquals("Customer with id 1 not found", exception.getMessage()); | ||
} | ||
} |