diff --git a/payment-service/src/main/java/com/example/paymentservice/model/response/PagedResult.java b/payment-service/src/main/java/com/example/paymentservice/model/response/PagedResult.java index 17aa537e..38ef821f 100644 --- a/payment-service/src/main/java/com/example/paymentservice/model/response/PagedResult.java +++ b/payment-service/src/main/java/com/example/paymentservice/model/response/PagedResult.java @@ -14,9 +14,10 @@ public record PagedResult( @JsonProperty("isLast") boolean isLast, @JsonProperty("hasNext") boolean hasNext, @JsonProperty("hasPrevious") boolean hasPrevious) { - public PagedResult(Page page) { + + public PagedResult(List data, Page page) { this( - page.getContent(), + data, page.getTotalElements(), page.getNumber() + 1, // for user page number starts from 1 page.getTotalPages(), diff --git a/payment-service/src/main/java/com/example/paymentservice/services/CustomerService.java b/payment-service/src/main/java/com/example/paymentservice/services/CustomerService.java index 2732b8f1..2fd427dc 100644 --- a/payment-service/src/main/java/com/example/paymentservice/services/CustomerService.java +++ b/payment-service/src/main/java/com/example/paymentservice/services/CustomerService.java @@ -24,14 +24,13 @@ @Slf4j @RequiredArgsConstructor @Service -@Transactional +@Transactional(readOnly = true) @Loggable public class CustomerService { private final CustomerRepository customerRepository; private final CustomerMapper customerMapper; - @Transactional(readOnly = true) public PagedResult findAllCustomers(FindCustomersQuery findCustomersQuery) { log.info( "Fetching findAllCustomers for pageNo {} with pageSize {}, sorting By {} {}", @@ -42,18 +41,10 @@ public PagedResult findAllCustomers(FindCustomersQuery findCus Pageable pageable = createPageable(findCustomersQuery); Page page = customerRepository.findAll(pageable); + List customerResponseList = customerMapper.toListResponse(page.getContent()); - - return new PagedResult<>( - customerResponseList, - page.getTotalElements(), - page.getNumber() + 1, // for user page number starts from 1 - page.getTotalPages(), - page.isFirst(), - page.isLast(), - page.hasNext(), - page.hasPrevious()); + return new PagedResult<>(customerResponseList, page); } private Pageable createPageable(FindCustomersQuery findCustomersQuery) { @@ -66,21 +57,21 @@ private Pageable createPageable(FindCustomersQuery findCustomersQuery) { return PageRequest.of(pageNo, findCustomersQuery.pageSize(), sort); } - @Transactional(readOnly = true) public Optional findCustomerById(Long id) { return customerRepository.findById(id).map(customerMapper::toResponse); } - @Transactional(readOnly = true) public Optional findCustomerByName(String name) { return customerRepository.findByName(name); } + @Transactional public CustomerResponse saveCustomer(CustomerRequest customerRequest) { Customer customer = customerMapper.toEntity(customerRequest); return customerMapper.toResponse(customerRepository.save(customer)); } + @Transactional public CustomerResponse updateCustomer(Long id, CustomerRequest customerRequest) { Customer customer = customerRepository @@ -97,6 +88,7 @@ public CustomerResponse updateCustomer(Long id, CustomerRequest customerRequest) return customerMapper.toResponse(updatedCustomer); } + @Transactional public void deleteCustomerById(Long id) { customerRepository.deleteById(id); } diff --git a/payment-service/src/main/java/com/example/paymentservice/services/PaymentOrderManageService.java b/payment-service/src/main/java/com/example/paymentservice/services/PaymentOrderManageService.java index 67e74f50..93361184 100644 --- a/payment-service/src/main/java/com/example/paymentservice/services/PaymentOrderManageService.java +++ b/payment-service/src/main/java/com/example/paymentservice/services/PaymentOrderManageService.java @@ -8,6 +8,7 @@ import com.example.paymentservice.exception.CustomerNotFoundException; import com.example.paymentservice.repositories.CustomerRepository; import com.example.paymentservice.utils.AppConstants; +import io.micrometer.core.annotation.Timed; import java.math.BigDecimal; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -23,6 +24,7 @@ public class PaymentOrderManageService { private final CustomerRepository customerRepository; private final KafkaTemplate kafkaTemplate; + @Timed(percentiles = 1.0) public void reserve(OrderDto orderDto) { log.debug( "Reserving Order with Id :{} in payment service with payload {}", @@ -55,6 +57,7 @@ public void reserve(OrderDto orderDto) { AppConstants.PAYMENT_ORDERS_TOPIC); } + @Timed(percentiles = 1.0) public void confirm(OrderDto orderDto) { log.debug( "Confirming Order with Id :{} in payment service with payload {}", diff --git a/payment-service/src/test/java/com/example/paymentservice/web/controllers/CustomerControllerTest.java b/payment-service/src/test/java/com/example/paymentservice/web/controllers/CustomerControllerTest.java index 7c5b912b..612ad70f 100644 --- a/payment-service/src/test/java/com/example/paymentservice/web/controllers/CustomerControllerTest.java +++ b/payment-service/src/test/java/com/example/paymentservice/web/controllers/CustomerControllerTest.java @@ -34,6 +34,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; @@ -68,8 +69,9 @@ void setUp() { @Test void shouldFetchAllCustomers() throws Exception { - Page page = new PageImpl<>(getCustomerResponseList()); - PagedResult postPagedResult = new PagedResult<>(page); + Page page = new PageImpl<>(customerList); + PagedResult postPagedResult = + new PagedResult<>(getCustomerResponseList(), page); FindCustomersQuery findCustomersQuery = new FindCustomersQuery(0, 10, "id", "desc"); given(customerService.findAllCustomers(findCustomersQuery)).willReturn(postPagedResult); @@ -124,7 +126,7 @@ void shouldReturn404WhenFetchingNonExistingCustomerById() throws Exception { .andExpect(status().isNotFound()) .andExpect( header().string( - "Content-Type", + HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found"))) .andExpect(jsonPath("$.title", is("Customer Not Found"))) @@ -143,7 +145,7 @@ void shouldReturn404WhenFetchingNonExistingCustomerByName() throws Exception { .andExpect(status().isNotFound()) .andExpect( header().string( - "Content-Type", + HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found"))) .andExpect(jsonPath("$.title", is("Customer Not Found"))) @@ -172,15 +174,18 @@ void shouldCreateNewCustomer() throws Exception { @Test void shouldReturn400WhenCreateNewCustomerWithoutNameAndEmail() throws Exception { - CustomerRequest customer = new CustomerRequest(null, null, null, 1); + CustomerRequest customerRequest = new CustomerRequest(null, null, null, 1); this.mockMvc .perform( post("/api/customers") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(customer))) + .content(objectMapper.writeValueAsString(customerRequest))) .andExpect(status().isBadRequest()) - .andExpect(header().string("Content-Type", is("application/problem+json"))) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect( jsonPath( "$.type", @@ -237,7 +242,7 @@ void shouldReturn404WhenUpdatingNonExistingCustomer() throws Exception { .andExpect(status().isNotFound()) .andExpect( header().string( - "Content-Type", + HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found"))) .andExpect(jsonPath("$.title", is("Customer Not Found"))) @@ -269,7 +274,7 @@ void shouldReturn404WhenDeletingNonExistingCustomer() throws Exception { .andExpect(status().isNotFound()) .andExpect( header().string( - "Content-Type", + HttpHeaders.CONTENT_TYPE, is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("https://api.customers.com/errors/not-found"))) .andExpect(jsonPath("$.title", is("Customer Not Found")))