-
Notifications
You must be signed in to change notification settings - Fork 0
Inventory customers tests #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7db0d94
test: create the controller tests for the inventory-analytics & custo…
malekelkssas 0c0e320
test: create the services tests for the inventory-analytics & custome…
malekelkssas 77716c1
fix: lint
malekelkssas 31b36cc
fix: query syntax
malekelkssas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
221 changes: 221 additions & 0 deletions
221
src/test/java/com/Podzilla/analytics/controllers/CustomerReportControllerTest.java
This file contains hidden or 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,221 @@ | ||
| package com.Podzilla.analytics.controllers; | ||
|
|
||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.hamcrest.Matchers.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.UUID; | ||
|
|
||
| import jakarta.persistence.EntityManager; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.test.context.ActiveProfiles; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import com.Podzilla.analytics.models.Customer; | ||
| import com.Podzilla.analytics.models.Order; | ||
| import com.Podzilla.analytics.models.Region; | ||
| import com.Podzilla.analytics.repositories.CustomerRepository; | ||
| import com.Podzilla.analytics.repositories.OrderRepository; | ||
| import com.Podzilla.analytics.repositories.RegionRepository; | ||
|
|
||
| @SpringBootTest | ||
| @AutoConfigureMockMvc | ||
| @Transactional | ||
| @ActiveProfiles("test") | ||
| class CustomerReportControllerTest { | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @Autowired | ||
| private CustomerRepository customerRepository; | ||
|
|
||
| @Autowired | ||
| private OrderRepository orderRepository; | ||
|
|
||
| @Autowired | ||
| private RegionRepository regionRepository; | ||
|
|
||
| @Autowired | ||
| private EntityManager entityManager; | ||
|
|
||
| private static final DateTimeFormatter ISO_LOCAL_DATE = DateTimeFormatter.ISO_LOCAL_DATE; | ||
|
|
||
| private Customer customer1; | ||
| private Customer customer2; | ||
| private Region region1; | ||
| private Order order1; | ||
| private Order order2; | ||
| private Order order3; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| orderRepository.deleteAll(); | ||
| customerRepository.deleteAll(); | ||
| regionRepository.deleteAll(); | ||
| entityManager.flush(); | ||
| entityManager.clear(); | ||
|
|
||
| // Create test region | ||
| region1 = regionRepository.save(Region.builder() | ||
| .city("Sample City") | ||
| .state("Sample State") | ||
| .country("Sample Country") | ||
| .postalCode("12345") | ||
| .build()); | ||
|
|
||
| // Create test customers | ||
| customer1 = customerRepository.save(Customer.builder() | ||
| .id(UUID.randomUUID()) | ||
| .name("John Doe") | ||
| .build()); | ||
|
|
||
| customer2 = customerRepository.save(Customer.builder() | ||
| .id(UUID.randomUUID()) | ||
| .name("Jane Smith") | ||
| .build()); | ||
|
|
||
| // Create test orders | ||
| order1 = orderRepository.save(Order.builder() | ||
| .id(UUID.randomUUID()) | ||
| .totalAmount(new BigDecimal("1000.00")) | ||
| .finalStatusTimestamp(LocalDateTime.now().minusDays(2)) | ||
| .status(Order.OrderStatus.DELIVERED) | ||
| .customer(customer1) | ||
| .region(region1) | ||
| .build()); | ||
|
|
||
| order2 = orderRepository.save(Order.builder() | ||
| .id(UUID.randomUUID()) | ||
| .totalAmount(new BigDecimal("500.00")) | ||
| .finalStatusTimestamp(LocalDateTime.now().minusDays(1)) | ||
| .status(Order.OrderStatus.DELIVERED) | ||
| .customer(customer1) | ||
| .region(region1) | ||
| .build()); | ||
|
|
||
| order3 = orderRepository.save(Order.builder() | ||
| .id(UUID.randomUUID()) | ||
| .totalAmount(new BigDecimal("2000.00")) | ||
| .finalStatusTimestamp(LocalDateTime.now().minusDays(3)) | ||
| .status(Order.OrderStatus.DELIVERED) | ||
| .customer(customer2) | ||
| .region(region1) | ||
| .build()); | ||
|
|
||
| entityManager.flush(); | ||
| entityManager.clear(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| orderRepository.deleteAll(); | ||
| customerRepository.deleteAll(); | ||
| regionRepository.deleteAll(); | ||
| entityManager.flush(); | ||
| entityManager.clear(); | ||
| } | ||
|
|
||
| @Test | ||
| void contextLoads() { | ||
| } | ||
|
|
||
| @Test | ||
| void getTopSpenders_ShouldReturnListOfTopSpenders() throws Exception { | ||
| LocalDate startDate = LocalDate.now().minusDays(4); | ||
| LocalDate endDate = LocalDate.now(); | ||
|
|
||
| mockMvc.perform(get("/customer-analytics/top-spenders") | ||
| .param("startDate", startDate.format(ISO_LOCAL_DATE)) | ||
| .param("endDate", endDate.format(ISO_LOCAL_DATE)) | ||
| .param("page", "0") | ||
| .param("size", "10")) | ||
| .andDo(MockMvcResultHandlers.print()) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$", hasSize(2))) | ||
| .andExpect(jsonPath("$[0].customerName").value(customer2.getName())) | ||
| .andExpect(jsonPath("$[0].totalSpending").value(closeTo(order3.getTotalAmount().doubleValue(), 0.01))) | ||
| .andExpect(jsonPath("$[1].customerName").value(customer1.getName())) | ||
| .andExpect(jsonPath("$[1].totalSpending") | ||
| .value(closeTo(order1.getTotalAmount().add(order2.getTotalAmount()).doubleValue(), 0.01))); | ||
| } | ||
|
|
||
| @Test | ||
| void getTopSpenders_ShouldReturnEmptyListWhenNoOrdersInDateRange() throws Exception { | ||
| LocalDate startDate = LocalDate.now().plusDays(1); | ||
| LocalDate endDate = LocalDate.now().plusDays(2); | ||
|
|
||
| mockMvc.perform(get("/customer-analytics/top-spenders") | ||
| .param("startDate", startDate.format(ISO_LOCAL_DATE)) | ||
| .param("endDate", endDate.format(ISO_LOCAL_DATE)) | ||
| .param("page", "0") | ||
| .param("size", "10")) | ||
| .andDo(MockMvcResultHandlers.print()) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$", hasSize(0))); | ||
| } | ||
|
|
||
| @Test | ||
| void getTopSpenders_ShouldHandlePagination() throws Exception { | ||
| LocalDate startDate = LocalDate.now().minusDays(4); | ||
| LocalDate endDate = LocalDate.now(); | ||
|
|
||
| mockMvc.perform(get("/customer-analytics/top-spenders") | ||
| .param("startDate", startDate.format(ISO_LOCAL_DATE)) | ||
| .param("endDate", endDate.format(ISO_LOCAL_DATE)) | ||
| .param("page", "0") | ||
| .param("size", "1")) | ||
| .andDo(MockMvcResultHandlers.print()) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$", hasSize(1))) | ||
| .andExpect(jsonPath("$[0].customerName").value(customer2.getName())) | ||
| .andExpect(jsonPath("$[0].totalSpending").value(closeTo(order3.getTotalAmount().doubleValue(), 0.01))); | ||
| } | ||
|
|
||
| @Test | ||
| void getTopSpenders_ShouldExcludeFailedOrders() throws Exception { | ||
| Order failedOrder = orderRepository.save(Order.builder() | ||
| .id(UUID.randomUUID()) | ||
| .totalAmount(new BigDecimal("3000.00")) | ||
| .finalStatusTimestamp(LocalDateTime.now().minusDays(1)) | ||
| .status(Order.OrderStatus.DELIVERY_FAILED) | ||
| .customer(customer1) | ||
| .region(region1) | ||
| .build()); | ||
|
|
||
| entityManager.flush(); | ||
| entityManager.clear(); | ||
|
|
||
| LocalDate startDate = LocalDate.now().minusDays(4); | ||
| LocalDate endDate = LocalDate.now(); | ||
|
|
||
| mockMvc.perform(get("/customer-analytics/top-spenders") | ||
| .param("startDate", startDate.format(ISO_LOCAL_DATE)) | ||
| .param("endDate", endDate.format(ISO_LOCAL_DATE)) | ||
| .param("page", "0") | ||
| .param("size", "10")) | ||
| .andDo(MockMvcResultHandlers.print()) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$", hasSize(2))) | ||
| .andExpect(jsonPath("$[0].customerName").value(customer2.getName())) | ||
| .andExpect(jsonPath("$[0].totalSpending").value(closeTo(order3.getTotalAmount().doubleValue(), 0.01))) | ||
| .andExpect(jsonPath("$[1].customerName").value(customer1.getName())) | ||
| .andExpect(jsonPath("$[1].totalSpending") | ||
| .value(closeTo(order1.getTotalAmount().add(order2.getTotalAmount()).doubleValue(), 0.01))) | ||
| .andExpect(jsonPath("$[1].totalSpending").value(not(closeTo(order1.getTotalAmount() | ||
| .add(order2.getTotalAmount()).add(failedOrder.getTotalAmount()).doubleValue(), 0.01)))); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The custom query for top spenders lacks an ORDER BY clause, which can lead to nondeterministic result ordering; add
ORDER BY COALESCE(SUM(o.totalAmount), 0) DESCto ensure consistent sorting by total spending.