From 5a94198fdae0bc92bd8c464a0180262a90ce1839 Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 6 Aug 2022 19:38:12 -0500 Subject: [PATCH 1/8] Addition of the Payment Get functionality and Junit tests --- .../user/controller/PaymentController.java | 70 +++++++++++++++++++ .../user/dao/model/mapper/ResourceMapper.java | 3 +- .../dao/repository/PaymentRepository.java | 9 +++ .../specification/PaymentSpecification.java | 31 ++++++++ .../com/bravo/user/model/dto/PaymentDto.java | 2 + .../user/model/filter/PaymentFilter.java | 17 +++++ .../bravo/user/service/PaymentService.java | 70 +++++++++++++++++++ src/main/resources/data.sql | 9 +++ .../controller/PaymentControllerTest.java | 2 + .../user/service/PaymentServiceTest.java | 2 + 10 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bravo/user/controller/PaymentController.java create mode 100644 src/main/java/com/bravo/user/dao/repository/PaymentRepository.java create mode 100644 src/main/java/com/bravo/user/dao/specification/PaymentSpecification.java create mode 100644 src/main/java/com/bravo/user/model/filter/PaymentFilter.java create mode 100644 src/main/java/com/bravo/user/service/PaymentService.java create mode 100644 src/test/java/com/bravo/user/controller/PaymentControllerTest.java create mode 100644 src/test/java/com/bravo/user/service/PaymentServiceTest.java diff --git a/src/main/java/com/bravo/user/controller/PaymentController.java b/src/main/java/com/bravo/user/controller/PaymentController.java new file mode 100644 index 0000000..cb3af1c --- /dev/null +++ b/src/main/java/com/bravo/user/controller/PaymentController.java @@ -0,0 +1,70 @@ +package com.bravo.user.controller; + +import com.bravo.user.annotation.SwaggerController; +import com.bravo.user.exception.BadRequestException; +import com.bravo.user.model.dto.PaymentDto; +import com.bravo.user.model.filter.PaymentFilter; +import com.bravo.user.service.PaymentService; +import com.bravo.user.utility.PageUtil; +import com.bravo.user.validator.UserValidator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.domain.PageRequest; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +@RequestMapping(value = "/payment") +@SwaggerController +public class PaymentController { + + private final UserValidator userValidator; + private final PaymentService paymentService; + private static final Logger LOGGER = + LoggerFactory.getLogger(PaymentController.class); + + public PaymentController( + UserValidator userValidator, + PaymentService paymentService + ){ + this.userValidator = userValidator; + this.paymentService = paymentService; + } + + /* + Included a Get for direct retrieve by UserId, and + a Post with a filter for possible future expansion + of the code, making it more robust and easy to + add to the code down the line. E. Erwin + */ + @GetMapping(value = "/retrieve") + @ResponseBody + public List retrieve( + final @RequestParam(required = false) String userId, + final @RequestParam(required = false) Integer page, + final @RequestParam(required = false) Integer size, + final HttpServletResponse httpResponse + ){ + if(userId != null) { + userValidator.validateId(userId); + final PageRequest pageRequest = PageUtil.createPageRequest(page, size); + return paymentService.retrievePaymentByUserId(userId, pageRequest, httpResponse); + } else { + throw new BadRequestException("'userId' is required!"); + } + } + @PostMapping(value = "/retrieve") + @ResponseBody + public List retrieve( + final @RequestBody PaymentFilter filter, + final @RequestParam(required = false) Integer page, + final @RequestParam(required = false) Integer size, + final HttpServletResponse httpResponse + ){ + LOGGER.info("Request to retrieve payment information being conducted..."); + final PageRequest pageRequest = PageUtil.createPageRequest(page, size); + return paymentService.retrieve(filter, pageRequest, httpResponse); + } + +} diff --git a/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java b/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java index 4a78188..c288c41 100644 --- a/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java +++ b/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java @@ -45,10 +45,11 @@ public > List convertPayments(final T return payments.stream().map(this::convertPayment).collect(Collectors.toList()); } + /*Changed this because it was giving the last 5 numbers, not the last 4*/ public PaymentDto convertPayment(final Payment payment){ final String cardNumber = payment.getCardNumber(); final PaymentDto dto = mapperFacade.map(payment, PaymentDto.class); - dto.setCardNumberLast4(cardNumber.substring(cardNumber.length() - 5)); + dto.setCardNumberLast4(cardNumber.substring(cardNumber.length() - 4)); return dto; } diff --git a/src/main/java/com/bravo/user/dao/repository/PaymentRepository.java b/src/main/java/com/bravo/user/dao/repository/PaymentRepository.java new file mode 100644 index 0000000..89e123f --- /dev/null +++ b/src/main/java/com/bravo/user/dao/repository/PaymentRepository.java @@ -0,0 +1,9 @@ +package com.bravo.user.dao.repository; + +import com.bravo.user.dao.model.Payment; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Repository; + +@Repository +public interface PaymentRepository extends JpaRepository, JpaSpecificationExecutor{} diff --git a/src/main/java/com/bravo/user/dao/specification/PaymentSpecification.java b/src/main/java/com/bravo/user/dao/specification/PaymentSpecification.java new file mode 100644 index 0000000..4df6f87 --- /dev/null +++ b/src/main/java/com/bravo/user/dao/specification/PaymentSpecification.java @@ -0,0 +1,31 @@ +package com.bravo.user.dao.specification; + +import com.bravo.user.dao.model.Payment; +import com.bravo.user.model.filter.PaymentFilter; +import lombok.EqualsAndHashCode; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import java.util.Set; + +@EqualsAndHashCode +public class PaymentSpecification extends AbstractSpecification{ + + private final PaymentFilter filter; + + public PaymentSpecification(final PaymentFilter filter) { + this.filter = filter; + } + + @Override + void doFilter( + Root root, + CriteriaQuery criteriaQuery, + CriteriaBuilder criteriaBuilder + ){ + applyStringFilterToFields(Set.of( + root.get("userId") + ), filter.getUserId()); + } +} diff --git a/src/main/java/com/bravo/user/model/dto/PaymentDto.java b/src/main/java/com/bravo/user/model/dto/PaymentDto.java index db32487..123220a 100644 --- a/src/main/java/com/bravo/user/model/dto/PaymentDto.java +++ b/src/main/java/com/bravo/user/model/dto/PaymentDto.java @@ -7,6 +7,8 @@ public class PaymentDto { private String id; + /*added this to make sure the payload is returning the correct user*/ + private String userId; private String cardNumberLast4; private Integer expiryMonth; private Integer expiryYear; diff --git a/src/main/java/com/bravo/user/model/filter/PaymentFilter.java b/src/main/java/com/bravo/user/model/filter/PaymentFilter.java new file mode 100644 index 0000000..5f6ab7e --- /dev/null +++ b/src/main/java/com/bravo/user/model/filter/PaymentFilter.java @@ -0,0 +1,17 @@ +package com.bravo.user.model.filter; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Set; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class PaymentFilter { + + private String userId; +} diff --git a/src/main/java/com/bravo/user/service/PaymentService.java b/src/main/java/com/bravo/user/service/PaymentService.java new file mode 100644 index 0000000..fc0f787 --- /dev/null +++ b/src/main/java/com/bravo/user/service/PaymentService.java @@ -0,0 +1,70 @@ +package com.bravo.user.service; + +import com.bravo.user.dao.model.Payment; +import com.bravo.user.dao.model.mapper.ResourceMapper; +import com.bravo.user.dao.repository.PaymentRepository; +import com.bravo.user.dao.specification.PaymentSpecification; +import com.bravo.user.model.dto.PaymentDto; +import com.bravo.user.model.filter.PaymentFilter; +import com.bravo.user.utility.PageUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +@Service +public class PaymentService { + + private static final Logger LOGGER = LoggerFactory.getLogger(PaymentService.class); + + private final PaymentRepository paymentRepository; + private final ResourceMapper resourceMapper; + + public PaymentService( + PaymentRepository paymentRepository, + ResourceMapper resourceMapper + ){ + this.paymentRepository = paymentRepository; + this.resourceMapper = resourceMapper; + } + + public List retrievePaymentByUserId( + final String userId, + final PageRequest pageRequest, + final HttpServletResponse httpResponse + ){ + LOGGER.info("Finding Payments for user..."); + final PaymentSpecification paymentSpecification = new PaymentSpecification(PaymentFilter.builder() + .userId(userId) + .build()); + + final Page paymentPage = paymentRepository.findAll(paymentSpecification, pageRequest); + final List payments = resourceMapper.convertPayments(paymentPage.getContent()); + LOGGER.info("Found {} payment(s)", payments.size()); + + PageUtil.updatePageHeaders(httpResponse, paymentPage, pageRequest); + return payments; + } + + public List retrieve( + final PaymentFilter paymentFilter, + final PageRequest pageRequest, + final HttpServletResponse httpResponse + ){ + LOGGER.info("Attempting to find User Payment Information..."); + final PaymentSpecification paymentSpecification = + new PaymentSpecification(paymentFilter); + final Page paymentPage = paymentRepository.findAll(paymentSpecification,pageRequest); + final List payments = resourceMapper.convertPayments(paymentPage.getContent()); + LOGGER.info("Found {} payment(s)", payments.size()); + + PageUtil.updatePageHeaders(httpResponse, paymentPage, pageRequest); + return payments; + } + + +} diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index da45720..9646575 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -648,3 +648,12 @@ insert into address (id, user_id, line1, line2, city, state, zip) values ('42f33d30-f3f8-4743-a94e-4db11fdb747d', '008a4215-0b1d-445e-b655-a964039cbb5a', '412 Maple St', null, 'Dowagiac', 'Michigan', '49047'), ('579872ec-46f8-46b5-b809-d0724d965f0e', '00963d9b-f884-485e-9455-fcf30c6ac379', '237 Mountain Ter', 'Apt 10', 'Odenville', 'Alabama', '35120'), ('95a983d0-ba0e-4f30-afb6-667d4724b253', '00963d9b-f884-485e-9455-fcf30c6ac379', '107 Annettes Ct', null, 'Aydlett', 'North Carolina', '27916'); + + +insert into payment(id,user_id,card_number,expiry_month,expiry_year) values +('9k9v8x6v-9hir-8c6a-00s2-dp8922xhik','008a4215-0b1d-445e-b655-a964039cbb5a', '1234567890123456', '10', '2024'), +('7qzfn2fz-kugj-maq7-0rly-r9q8hi0mf7','00963d9b-f884-485e-9455-fcf30c6ac379', '2345678901234567', '07', '2025'), +('ad7yhtjn-qqlm-x1p4-ijdw-6dluiyysd2','00bed3ac-5f3c-4a2d-a67b-80376ea9f941', '3456789012345678', '03', '2025'), +('y414leo3-3imm-nxp4-coev-r7rjytdmur','008a4215-0b1d-445e-b655-a964039cbb5a', '4567890123456789', '11', '2026'), +('tzkif37a-vkx8-341h-hzka-pm4hj5svgi','0111d3ca-514b-4ae8-8f57-e85cca43fb1e', '5678901234567890', '07', '2025'), +('934g1gle-e26f-tnta-elos-k7misr3v36','01316816-0cb7-41c4-8424-8367294aea27', '6789012345678901', '03', '2025'); \ No newline at end of file diff --git a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java new file mode 100644 index 0000000..81ec412 --- /dev/null +++ b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java @@ -0,0 +1,2 @@ +package com.bravo.user.controller;public class PaymentControllerTest { +} diff --git a/src/test/java/com/bravo/user/service/PaymentServiceTest.java b/src/test/java/com/bravo/user/service/PaymentServiceTest.java new file mode 100644 index 0000000..6e8b87a --- /dev/null +++ b/src/test/java/com/bravo/user/service/PaymentServiceTest.java @@ -0,0 +1,2 @@ +package com.bravo.user.service;public class PaymentServiceTest { +} From 43db612eb2fa8b4ecdae22b6412bdc0455cfaf31 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 7 Aug 2022 21:15:14 -0500 Subject: [PATCH 2/8] Junit Tests --- .../controller/PaymentControllerTest.java | 138 ++++++++++++++- .../user/service/PaymentServiceTest.java | 159 +++++++++++++++++- 2 files changed, 295 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java index 81ec412..a3481c7 100644 --- a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java +++ b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java @@ -1,2 +1,138 @@ -package com.bravo.user.controller;public class PaymentControllerTest { +package com.bravo.user.controller; + +import com.bravo.user.App; +import com.bravo.user.model.dto.PaymentDto; +import com.bravo.user.model.filter.PaymentFilter; +import com.bravo.user.service.PaymentService; +import com.bravo.user.utility.PageUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +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.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultActions; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + + +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@ContextConfiguration(classes = {App.class}) +@ExtendWith(SpringExtension.class) +@SpringBootTest() +@AutoConfigureMockMvc +public class PaymentControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private PaymentService paymentService; + + private List payments; + private PaymentFilter paymentFilter; + private static final ObjectMapper mapper = new ObjectMapper(); + + @BeforeEach + public void beforeEach(){ + final List ids = IntStream + .range(1,6) + .boxed() + .collect(Collectors.toList()); + + this.payments = ids.stream() + .map(id -> createPaymentDto(Integer.toString(id))) + .collect(Collectors.toList()); + + String paymentId = "1"; + this.paymentFilter = createPaymentFilter(paymentId); + } + + + + @Test + void getRetrieveWithUserId() throws Exception + { + when(paymentService + .retrievePaymentByUserId(anyString(), any(PageRequest.class), any(HttpServletResponse.class))) + .thenReturn(payments); + + final ResultActions result = this.mockMvc + .perform(get( "/payment/retrieve?userId=1")) + .andExpect(status().isOk()); + + for(int i = 0; i < payments.size(); i++) { + result.andExpect((jsonPath(String.format("$[%d].id", i)).value(payments.get(i).getId()))); + } + + final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + verify(paymentService).retrievePaymentByUserId( + eq("1"), eq(pageRequest), any(HttpServletResponse.class) + ); + + } + + @Test + void postRetrieveWithFilter() throws Exception + { + when(paymentService.retrieve(any(PaymentFilter.class), any(PageRequest.class), any(HttpServletResponse.class))) + .thenReturn(payments); + + String jsonResult = mapper.writeValueAsString(paymentFilter); + + final ResultActions result = this.mockMvc + .perform(post("/payment/retrieve") + .contentType(MediaType.APPLICATION_JSON) + .content(jsonResult) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()); + + for(int i = 0; i < payments.size(); i++) { + result.andExpect((jsonPath(String.format("$[%d].id", i)).value(payments.get(i).getId()))); + } + + final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + verify(paymentService).retrieve(eq(paymentFilter), eq(pageRequest), any(HttpServletResponse.class)); + } + + @Test + public void retrieveWithUserIdMissing() throws Exception + { + this.mockMvc.perform(get("/payment/retrieve")).andExpect(status().isBadRequest()); + } + + @Test + public void retrieveWithEmptyUserId() throws Exception{ + this.mockMvc.perform(get("/payment/retrieve?userId=")).andExpect(status().isBadRequest()); + } + + private PaymentDto createPaymentDto(String paymentId) + { + final PaymentDto payment = new PaymentDto(); + payment.setId(paymentId); + return payment; + } + + private PaymentFilter createPaymentFilter(String paymentId) { + final PaymentFilter paymentFilter = new PaymentFilter(); + paymentFilter.setUserId(paymentId); + return paymentFilter; + } } diff --git a/src/test/java/com/bravo/user/service/PaymentServiceTest.java b/src/test/java/com/bravo/user/service/PaymentServiceTest.java index 6e8b87a..1bdd45c 100644 --- a/src/test/java/com/bravo/user/service/PaymentServiceTest.java +++ b/src/test/java/com/bravo/user/service/PaymentServiceTest.java @@ -1,2 +1,159 @@ -package com.bravo.user.service;public class PaymentServiceTest { +package com.bravo.user.service; + +import com.bravo.user.App; +import com.bravo.user.dao.model.Payment; +import com.bravo.user.dao.model.mapper.ResourceMapper; +import com.bravo.user.dao.repository.PaymentRepository; +import com.bravo.user.dao.specification.PaymentSpecification; +import com.bravo.user.model.dto.PaymentDto; +import com.bravo.user.model.filter.PaymentFilter; +import com.bravo.user.utility.PageUtil; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@ContextConfiguration(classes = {App.class}) +@ExtendWith(SpringExtension.class) +@SpringBootTest +public class PaymentServiceTest { + + @Autowired + private HttpServletResponse httpResponse; + @Autowired + private PaymentService paymentService; + @MockBean + private ResourceMapper resourceMapper; + @MockBean + private PaymentRepository paymentRepository; + private List paymentDtos; + private PaymentFilter paymentFilter; + + + @BeforeEach + public void beforeEach() { + final List ids = IntStream + .range(1,6) + .boxed() + .collect(Collectors.toList()); + + this.paymentFilter = createPaymentFilter("1"); + + final List payments = ids.stream() + .map(id -> createPayment(id.toString())) + .collect(Collectors.toList()); + this.paymentDtos = ids.stream() + .map(id -> createPaymentDto(id.toString())) + .collect(Collectors.toList()); + when(resourceMapper.convertPayments(payments)).thenReturn(paymentDtos); + + final Page mockPage = mock(Page.class); + when(paymentRepository.findAll(any(PaymentSpecification.class), any(PageRequest.class))) + .thenReturn(mockPage); + + when(mockPage.getContent()).thenReturn(payments); + when(mockPage.getTotalPages()).thenReturn(6); + + + } + + + + @Test + public void testRetrievePaymentByUserId() + { + final String userId = "1"; + final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + final List results = paymentService.retrievePaymentByUserId(userId, pageRequest, httpResponse); + assertEquals(paymentDtos, results); + assertEquals("6", httpResponse.getHeader("page-count")); + assertEquals("1", httpResponse.getHeader("page-number")); + assertEquals("20", httpResponse.getHeader("page-size")); + + final PaymentFilter filter = new PaymentFilter(userId); + final PaymentSpecification specification = new PaymentSpecification(filter); + verify(paymentRepository).findAll(specification, pageRequest); + } + + @Test + public void testRetrievePaymentByUserIdWithPagination() + { + final String userId = "1"; + final PageRequest pageRequest = PageUtil.createPageRequest(1, 6); + final List results = paymentService.retrievePaymentByUserId(userId, pageRequest, httpResponse); + assertEquals(paymentDtos, results); + assertEquals("6", httpResponse.getHeader("page-count")); + assertEquals("1", httpResponse.getHeader("page-number")); + assertEquals("6", httpResponse.getHeader("page-size")); + + final PaymentFilter filter = new PaymentFilter(userId); + final PaymentSpecification specification = new PaymentSpecification(filter); + verify(paymentRepository).findAll(specification, pageRequest); + } + + @Test + public void testRetrieveWithFilter() + { + final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + final List results = paymentService.retrieve(paymentFilter, pageRequest, httpResponse); + assertEquals(paymentDtos, results); + assertEquals("6", httpResponse.getHeader("page-count")); + assertEquals("1", httpResponse.getHeader("page-number")); + assertEquals("20", httpResponse.getHeader("page-size")); + + final PaymentSpecification specification = new PaymentSpecification(paymentFilter); + verify(paymentRepository).findAll(specification, pageRequest); + + } + + @Test + public void testRetrieveWithFilterWithPagination() + { + final PageRequest pageRequest = PageUtil.createPageRequest(1, 6); + final List results = paymentService.retrieve(paymentFilter, pageRequest, httpResponse); + assertEquals(paymentDtos, results); + assertEquals("6", httpResponse.getHeader("page-count")); + assertEquals("1", httpResponse.getHeader("page-number")); + assertEquals("6", httpResponse.getHeader("page-size")); + + final PaymentSpecification specification = new PaymentSpecification(paymentFilter); + verify(paymentRepository).findAll(specification, pageRequest); + + } + + private PaymentFilter createPaymentFilter(String userId) { + + final PaymentFilter paymentFilter = new PaymentFilter(); + paymentFilter.setUserId(userId); + return paymentFilter; + } + + private Payment createPayment(String paymentId) + { + final Payment payment = new Payment(); + payment.setId(paymentId); + return payment; + } + + private PaymentDto createPaymentDto(String paymentDtoId) + { + final PaymentDto paymentDto = new PaymentDto(); + paymentDto.setId(paymentDtoId); + return paymentDto; + } } From 12698621cf4f741da80b4df915c67f8f8b26de46 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 7 Aug 2022 23:02:52 -0500 Subject: [PATCH 3/8] Fixes to the application --- pom.xml | 12 +++ .../user/controller/PaymentController.java | 23 ++---- .../com/bravo/user/model/dto/PaymentDto.java | 10 +++ .../bravo/user/service/PaymentService.java | 24 ++---- .../java/com/bravo/user/utility/PageUtil.java | 3 + .../controller/PaymentControllerTest.java | 48 +++--------- .../user/controller/UserControllerTest.java | 3 +- .../user/service/PaymentServiceTest.java | 78 +++++++------------ .../bravo/user/service/UserServiceTest.java | 2 +- 9 files changed, 79 insertions(+), 124 deletions(-) diff --git a/pom.xml b/pom.xml index 5b31f78..4b03bdc 100644 --- a/pom.xml +++ b/pom.xml @@ -130,6 +130,18 @@ org.springframework.boot spring-boot-maven-plugin + + org.pitest + pitest-maven + 1.6.4 + + + org.pitest + pitest-junit5-plugin + 0.12 + + + diff --git a/src/main/java/com/bravo/user/controller/PaymentController.java b/src/main/java/com/bravo/user/controller/PaymentController.java index cb3af1c..65257a9 100644 --- a/src/main/java/com/bravo/user/controller/PaymentController.java +++ b/src/main/java/com/bravo/user/controller/PaymentController.java @@ -21,8 +21,6 @@ public class PaymentController { private final UserValidator userValidator; private final PaymentService paymentService; - private static final Logger LOGGER = - LoggerFactory.getLogger(PaymentController.class); public PaymentController( UserValidator userValidator, @@ -32,28 +30,19 @@ public PaymentController( this.paymentService = paymentService; } - /* - Included a Get for direct retrieve by UserId, and - a Post with a filter for possible future expansion - of the code, making it more robust and easy to - add to the code down the line. E. Erwin - */ @GetMapping(value = "/retrieve") @ResponseBody public List retrieve( - final @RequestParam(required = false) String userId, + final @RequestParam String userId, final @RequestParam(required = false) Integer page, final @RequestParam(required = false) Integer size, final HttpServletResponse httpResponse ){ - if(userId != null) { - userValidator.validateId(userId); - final PageRequest pageRequest = PageUtil.createPageRequest(page, size); - return paymentService.retrievePaymentByUserId(userId, pageRequest, httpResponse); - } else { - throw new BadRequestException("'userId' is required!"); - } + userValidator.validateId(userId); + final PageRequest pageRequest = PageUtil.createPageRequest(page, size); + return paymentService.retrievePaymentByUserId(userId, pageRequest, httpResponse); } + @PostMapping(value = "/retrieve") @ResponseBody public List retrieve( @@ -62,9 +51,7 @@ public List retrieve( final @RequestParam(required = false) Integer size, final HttpServletResponse httpResponse ){ - LOGGER.info("Request to retrieve payment information being conducted..."); final PageRequest pageRequest = PageUtil.createPageRequest(page, size); return paymentService.retrieve(filter, pageRequest, httpResponse); } - } diff --git a/src/main/java/com/bravo/user/model/dto/PaymentDto.java b/src/main/java/com/bravo/user/model/dto/PaymentDto.java index 123220a..ac02402 100644 --- a/src/main/java/com/bravo/user/model/dto/PaymentDto.java +++ b/src/main/java/com/bravo/user/model/dto/PaymentDto.java @@ -1,9 +1,13 @@ package com.bravo.user.model.dto; import java.time.LocalDateTime; + +import com.bravo.user.dao.model.Payment; import lombok.Data; +import lombok.NoArgsConstructor; @Data +@NoArgsConstructor public class PaymentDto { private String id; @@ -13,4 +17,10 @@ public class PaymentDto { private Integer expiryMonth; private Integer expiryYear; private LocalDateTime updated; + + public PaymentDto(final String id) { + this(); + this.id = id; + } + } diff --git a/src/main/java/com/bravo/user/service/PaymentService.java b/src/main/java/com/bravo/user/service/PaymentService.java index fc0f787..20ff6d8 100644 --- a/src/main/java/com/bravo/user/service/PaymentService.java +++ b/src/main/java/com/bravo/user/service/PaymentService.java @@ -20,7 +20,6 @@ public class PaymentService { private static final Logger LOGGER = LoggerFactory.getLogger(PaymentService.class); - private final PaymentRepository paymentRepository; private final ResourceMapper resourceMapper; @@ -37,34 +36,21 @@ public List retrievePaymentByUserId( final PageRequest pageRequest, final HttpServletResponse httpResponse ){ - LOGGER.info("Finding Payments for user..."); - final PaymentSpecification paymentSpecification = new PaymentSpecification(PaymentFilter.builder() - .userId(userId) - .build()); - - final Page paymentPage = paymentRepository.findAll(paymentSpecification, pageRequest); - final List payments = resourceMapper.convertPayments(paymentPage.getContent()); - LOGGER.info("Found {} payment(s)", payments.size()); - - PageUtil.updatePageHeaders(httpResponse, paymentPage, pageRequest); - return payments; + return retrieve(new PaymentFilter(userId), pageRequest, httpResponse); } public List retrieve( - final PaymentFilter paymentFilter, + final PaymentFilter filter, final PageRequest pageRequest, final HttpServletResponse httpResponse ){ - LOGGER.info("Attempting to find User Payment Information..."); - final PaymentSpecification paymentSpecification = - new PaymentSpecification(paymentFilter); - final Page paymentPage = paymentRepository.findAll(paymentSpecification,pageRequest); + LOGGER.info("Request to retrieve payment information being conducted... paymentFilter: {}", filter); + final PaymentSpecification specification = new PaymentSpecification(filter); + final Page paymentPage = paymentRepository.findAll(specification,pageRequest); final List payments = resourceMapper.convertPayments(paymentPage.getContent()); LOGGER.info("Found {} payment(s)", payments.size()); PageUtil.updatePageHeaders(httpResponse, paymentPage, pageRequest); return payments; } - - } diff --git a/src/main/java/com/bravo/user/utility/PageUtil.java b/src/main/java/com/bravo/user/utility/PageUtil.java index 15d7f85..5ac725d 100644 --- a/src/main/java/com/bravo/user/utility/PageUtil.java +++ b/src/main/java/com/bravo/user/utility/PageUtil.java @@ -10,6 +10,9 @@ public class PageUtil { private static final int DEFAULT_SIZE = 20; + public static PageRequest createPageRequest() { + return createPageRequest(null, null); + } public static PageRequest createPageRequest(final Integer page, final Integer size){ return createPageRequest(page, size, DEFAULT_SIZE); } diff --git a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java index a3481c7..8f92b98 100644 --- a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java +++ b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java @@ -40,36 +40,28 @@ @AutoConfigureMockMvc public class PaymentControllerTest { + private static final ObjectMapper MAPPER = new ObjectMapper(); + @Autowired private MockMvc mockMvc; - @MockBean private PaymentService paymentService; private List payments; private PaymentFilter paymentFilter; - private static final ObjectMapper mapper = new ObjectMapper(); @BeforeEach public void beforeEach(){ - final List ids = IntStream + this.payments = IntStream .range(1,6) - .boxed() - .collect(Collectors.toList()); - - this.payments = ids.stream() - .map(id -> createPaymentDto(Integer.toString(id))) + .mapToObj(id -> new PaymentDto(Integer.toString(id))) .collect(Collectors.toList()); - String paymentId = "1"; - this.paymentFilter = createPaymentFilter(paymentId); + this.paymentFilter = new PaymentFilter("1"); } - - @Test - void getRetrieveWithUserId() throws Exception - { + public void getRetrieveWithUserId() throws Exception { when(paymentService .retrievePaymentByUserId(anyString(), any(PageRequest.class), any(HttpServletResponse.class))) .thenReturn(payments); @@ -82,20 +74,18 @@ void getRetrieveWithUserId() throws Exception result.andExpect((jsonPath(String.format("$[%d].id", i)).value(payments.get(i).getId()))); } - final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + final PageRequest pageRequest = PageUtil.createPageRequest(); verify(paymentService).retrievePaymentByUserId( eq("1"), eq(pageRequest), any(HttpServletResponse.class) ); - } @Test - void postRetrieveWithFilter() throws Exception - { + public void postRetrieveWithFilter() throws Exception { when(paymentService.retrieve(any(PaymentFilter.class), any(PageRequest.class), any(HttpServletResponse.class))) .thenReturn(payments); - String jsonResult = mapper.writeValueAsString(paymentFilter); + final String jsonResult = MAPPER.writeValueAsString(paymentFilter); final ResultActions result = this.mockMvc .perform(post("/payment/retrieve") @@ -108,31 +98,17 @@ void postRetrieveWithFilter() throws Exception result.andExpect((jsonPath(String.format("$[%d].id", i)).value(payments.get(i).getId()))); } - final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + final PageRequest pageRequest = PageUtil.createPageRequest(); verify(paymentService).retrieve(eq(paymentFilter), eq(pageRequest), any(HttpServletResponse.class)); } @Test - public void retrieveWithUserIdMissing() throws Exception - { + public void retrieveWithUserIdMissing() throws Exception { this.mockMvc.perform(get("/payment/retrieve")).andExpect(status().isBadRequest()); } @Test - public void retrieveWithEmptyUserId() throws Exception{ + public void retrieveWithEmptyUserId() throws Exception { this.mockMvc.perform(get("/payment/retrieve?userId=")).andExpect(status().isBadRequest()); } - - private PaymentDto createPaymentDto(String paymentId) - { - final PaymentDto payment = new PaymentDto(); - payment.setId(paymentId); - return payment; - } - - private PaymentFilter createPaymentFilter(String paymentId) { - final PaymentFilter paymentFilter = new PaymentFilter(); - paymentFilter.setUserId(paymentId); - return paymentFilter; - } } diff --git a/src/test/java/com/bravo/user/controller/UserControllerTest.java b/src/test/java/com/bravo/user/controller/UserControllerTest.java index 6f98aac..5b72c9f 100644 --- a/src/test/java/com/bravo/user/controller/UserControllerTest.java +++ b/src/test/java/com/bravo/user/controller/UserControllerTest.java @@ -41,7 +41,6 @@ class UserControllerTest { @Autowired private MockMvc mockMvc; - @MockBean private UserService userService; @@ -73,7 +72,7 @@ void getRetrieveWithName() throws Exception { result.andExpect(jsonPath(String.format("$[%d].id", i)).value(users.get(i).getId())); } - final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + final PageRequest pageRequest = PageUtil.createPageRequest(); verify(userService).retrieveByName( eq("lucy"), eq(pageRequest), any(HttpServletResponse.class) ); diff --git a/src/test/java/com/bravo/user/service/PaymentServiceTest.java b/src/test/java/com/bravo/user/service/PaymentServiceTest.java index 1bdd45c..d3ad627 100644 --- a/src/test/java/com/bravo/user/service/PaymentServiceTest.java +++ b/src/test/java/com/bravo/user/service/PaymentServiceTest.java @@ -33,6 +33,9 @@ @SpringBootTest public class PaymentServiceTest { + private static final String PAGE_COUNT = "page-count"; + private static final String PAGE_NUMBER = "page-number"; + private static final String PAGE_SIZE = "page-size"; @Autowired private HttpServletResponse httpResponse; @Autowired @@ -41,10 +44,10 @@ public class PaymentServiceTest { private ResourceMapper resourceMapper; @MockBean private PaymentRepository paymentRepository; + private List paymentDtos; private PaymentFilter paymentFilter; - @BeforeEach public void beforeEach() { final List ids = IntStream @@ -52,38 +55,34 @@ public void beforeEach() { .boxed() .collect(Collectors.toList()); - this.paymentFilter = createPaymentFilter("1"); + this.paymentFilter = new PaymentFilter("1"); + + this.paymentDtos = ids.stream() + .map(id -> new PaymentDto(Integer.toString(id))) + .collect(Collectors.toList()); + when(resourceMapper.convertPayments(anyList())).thenReturn(paymentDtos); final List payments = ids.stream() .map(id -> createPayment(id.toString())) .collect(Collectors.toList()); - this.paymentDtos = ids.stream() - .map(id -> createPaymentDto(id.toString())) - .collect(Collectors.toList()); - when(resourceMapper.convertPayments(payments)).thenReturn(paymentDtos); final Page mockPage = mock(Page.class); - when(paymentRepository.findAll(any(PaymentSpecification.class), any(PageRequest.class))) - .thenReturn(mockPage); - when(mockPage.getContent()).thenReturn(payments); when(mockPage.getTotalPages()).thenReturn(6); - + when(paymentRepository.findAll(any(PaymentSpecification.class), any(PageRequest.class))) + .thenReturn(mockPage); } - - @Test - public void testRetrievePaymentByUserId() - { + public void retrievePaymentByUserId() { final String userId = "1"; - final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + final PageRequest pageRequest = PageUtil.createPageRequest(); final List results = paymentService.retrievePaymentByUserId(userId, pageRequest, httpResponse); assertEquals(paymentDtos, results); - assertEquals("6", httpResponse.getHeader("page-count")); - assertEquals("1", httpResponse.getHeader("page-number")); - assertEquals("20", httpResponse.getHeader("page-size")); + assertEquals("6", httpResponse.getHeader(PAGE_COUNT)); + assertEquals("1", httpResponse.getHeader(PAGE_NUMBER)); + assertEquals("20", httpResponse.getHeader(PAGE_SIZE)); final PaymentFilter filter = new PaymentFilter(userId); final PaymentSpecification specification = new PaymentSpecification(filter); @@ -91,15 +90,14 @@ public void testRetrievePaymentByUserId() } @Test - public void testRetrievePaymentByUserIdWithPagination() - { + public void retrievePaymentByUserIdWithPagination() { final String userId = "1"; final PageRequest pageRequest = PageUtil.createPageRequest(1, 6); final List results = paymentService.retrievePaymentByUserId(userId, pageRequest, httpResponse); assertEquals(paymentDtos, results); - assertEquals("6", httpResponse.getHeader("page-count")); - assertEquals("1", httpResponse.getHeader("page-number")); - assertEquals("6", httpResponse.getHeader("page-size")); + assertEquals("6", httpResponse.getHeader(PAGE_COUNT)); + assertEquals("1", httpResponse.getHeader(PAGE_NUMBER)); + assertEquals("6", httpResponse.getHeader(PAGE_SIZE)); final PaymentFilter filter = new PaymentFilter(userId); final PaymentSpecification specification = new PaymentSpecification(filter); @@ -107,14 +105,13 @@ public void testRetrievePaymentByUserIdWithPagination() } @Test - public void testRetrieveWithFilter() - { - final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + public void retrieveWithFilter() { + final PageRequest pageRequest = PageUtil.createPageRequest(); final List results = paymentService.retrieve(paymentFilter, pageRequest, httpResponse); assertEquals(paymentDtos, results); - assertEquals("6", httpResponse.getHeader("page-count")); - assertEquals("1", httpResponse.getHeader("page-number")); - assertEquals("20", httpResponse.getHeader("page-size")); + assertEquals("6", httpResponse.getHeader(PAGE_COUNT)); + assertEquals("1", httpResponse.getHeader(PAGE_NUMBER)); + assertEquals("20", httpResponse.getHeader(PAGE_SIZE)); final PaymentSpecification specification = new PaymentSpecification(paymentFilter); verify(paymentRepository).findAll(specification, pageRequest); @@ -122,38 +119,23 @@ public void testRetrieveWithFilter() } @Test - public void testRetrieveWithFilterWithPagination() - { + public void retrieveWithFilterWithPagination() { final PageRequest pageRequest = PageUtil.createPageRequest(1, 6); final List results = paymentService.retrieve(paymentFilter, pageRequest, httpResponse); assertEquals(paymentDtos, results); - assertEquals("6", httpResponse.getHeader("page-count")); - assertEquals("1", httpResponse.getHeader("page-number")); - assertEquals("6", httpResponse.getHeader("page-size")); + assertEquals("6", httpResponse.getHeader(PAGE_COUNT)); + assertEquals("1", httpResponse.getHeader(PAGE_NUMBER)); + assertEquals("6", httpResponse.getHeader(PAGE_SIZE)); final PaymentSpecification specification = new PaymentSpecification(paymentFilter); verify(paymentRepository).findAll(specification, pageRequest); } - private PaymentFilter createPaymentFilter(String userId) { - - final PaymentFilter paymentFilter = new PaymentFilter(); - paymentFilter.setUserId(userId); - return paymentFilter; - } - private Payment createPayment(String paymentId) { final Payment payment = new Payment(); payment.setId(paymentId); return payment; } - - private PaymentDto createPaymentDto(String paymentDtoId) - { - final PaymentDto paymentDto = new PaymentDto(); - paymentDto.setId(paymentDtoId); - return paymentDto; - } } diff --git a/src/test/java/com/bravo/user/service/UserServiceTest.java b/src/test/java/com/bravo/user/service/UserServiceTest.java index 78043ec..47be7df 100644 --- a/src/test/java/com/bravo/user/service/UserServiceTest.java +++ b/src/test/java/com/bravo/user/service/UserServiceTest.java @@ -77,7 +77,7 @@ public void beforeEach(){ @Test public void retrieveByName() { final String input = "input"; - final PageRequest pageRequest = PageUtil.createPageRequest(null, null); + final PageRequest pageRequest = PageUtil.createPageRequest(); final List results = userService.retrieveByName(input, pageRequest, httpResponse); assertEquals(dtoUsers, results); assertEquals("9", httpResponse.getHeader("page-count")); From 212c5f15dca0f3fa13a0d1318d3b52b3f5ce3a34 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 7 Aug 2022 23:51:00 -0500 Subject: [PATCH 4/8] Removed comments, will add them to PR request, and fixed data.sql new entries to show I added the same userId twice to return multiple values to show filter is working correctly. --- .../java/com/bravo/user/dao/model/mapper/ResourceMapper.java | 1 - src/main/java/com/bravo/user/model/dto/PaymentDto.java | 1 - src/main/resources/data.sql | 4 ++-- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java b/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java index c288c41..81efe58 100644 --- a/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java +++ b/src/main/java/com/bravo/user/dao/model/mapper/ResourceMapper.java @@ -45,7 +45,6 @@ public > List convertPayments(final T return payments.stream().map(this::convertPayment).collect(Collectors.toList()); } - /*Changed this because it was giving the last 5 numbers, not the last 4*/ public PaymentDto convertPayment(final Payment payment){ final String cardNumber = payment.getCardNumber(); final PaymentDto dto = mapperFacade.map(payment, PaymentDto.class); diff --git a/src/main/java/com/bravo/user/model/dto/PaymentDto.java b/src/main/java/com/bravo/user/model/dto/PaymentDto.java index ac02402..7e30a65 100644 --- a/src/main/java/com/bravo/user/model/dto/PaymentDto.java +++ b/src/main/java/com/bravo/user/model/dto/PaymentDto.java @@ -11,7 +11,6 @@ public class PaymentDto { private String id; - /*added this to make sure the payload is returning the correct user*/ private String userId; private String cardNumberLast4; private Integer expiryMonth; diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 9646575..f17a26a 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -650,10 +650,10 @@ insert into address (id, user_id, line1, line2, city, state, zip) values ('95a983d0-ba0e-4f30-afb6-667d4724b253', '00963d9b-f884-485e-9455-fcf30c6ac379', '107 Annettes Ct', null, 'Aydlett', 'North Carolina', '27916'); -insert into payment(id,user_id,card_number,expiry_month,expiry_year) values +insert into payment(id, user_id, card_number, expiry_month, expiry_year) values ('9k9v8x6v-9hir-8c6a-00s2-dp8922xhik','008a4215-0b1d-445e-b655-a964039cbb5a', '1234567890123456', '10', '2024'), +('y414leo3-3imm-nxp4-coev-r7rjytdmur','008a4215-0b1d-445e-b655-a964039cbb5a', '4567890123456789', '11', '2026'), ('7qzfn2fz-kugj-maq7-0rly-r9q8hi0mf7','00963d9b-f884-485e-9455-fcf30c6ac379', '2345678901234567', '07', '2025'), ('ad7yhtjn-qqlm-x1p4-ijdw-6dluiyysd2','00bed3ac-5f3c-4a2d-a67b-80376ea9f941', '3456789012345678', '03', '2025'), -('y414leo3-3imm-nxp4-coev-r7rjytdmur','008a4215-0b1d-445e-b655-a964039cbb5a', '4567890123456789', '11', '2026'), ('tzkif37a-vkx8-341h-hzka-pm4hj5svgi','0111d3ca-514b-4ae8-8f57-e85cca43fb1e', '5678901234567890', '07', '2025'), ('934g1gle-e26f-tnta-elos-k7misr3v36','01316816-0cb7-41c4-8424-8367294aea27', '6789012345678901', '03', '2025'); \ No newline at end of file From e1e8463c03eb679dc7df855496434643b52567f0 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 10 Aug 2022 15:49:43 -0500 Subject: [PATCH 5/8] Minor improvements/changes as pointed out to me by the code reviewer --- .../com/bravo/user/dao/model/Payment.java | 7 +++++++ .../com/bravo/user/model/dto/PaymentDto.java | 11 ++++------- .../user/model/filter/PaymentFilter.java | 2 -- .../bravo/user/service/PaymentService.java | 2 +- .../controller/PaymentControllerTest.java | 2 +- .../user/service/PaymentServiceTest.java | 19 +++---------------- 6 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/bravo/user/dao/model/Payment.java b/src/main/java/com/bravo/user/dao/model/Payment.java index 12eb7a5..ef5d377 100644 --- a/src/main/java/com/bravo/user/dao/model/Payment.java +++ b/src/main/java/com/bravo/user/dao/model/Payment.java @@ -6,6 +6,8 @@ import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; + +import lombok.Builder; import lombok.Data; @Entity @@ -37,4 +39,9 @@ public Payment(){ this.id = UUID.randomUUID().toString(); this.updated = LocalDateTime.now(); } + + public Payment(final String id) { + super(); + this.id = id; + } } diff --git a/src/main/java/com/bravo/user/model/dto/PaymentDto.java b/src/main/java/com/bravo/user/model/dto/PaymentDto.java index 7e30a65..e693a48 100644 --- a/src/main/java/com/bravo/user/model/dto/PaymentDto.java +++ b/src/main/java/com/bravo/user/model/dto/PaymentDto.java @@ -2,12 +2,15 @@ import java.time.LocalDateTime; -import com.bravo.user.dao.model.Payment; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data +@Builder @NoArgsConstructor +@AllArgsConstructor public class PaymentDto { private String id; @@ -16,10 +19,4 @@ public class PaymentDto { private Integer expiryMonth; private Integer expiryYear; private LocalDateTime updated; - - public PaymentDto(final String id) { - this(); - this.id = id; - } - } diff --git a/src/main/java/com/bravo/user/model/filter/PaymentFilter.java b/src/main/java/com/bravo/user/model/filter/PaymentFilter.java index 5f6ab7e..542d0a8 100644 --- a/src/main/java/com/bravo/user/model/filter/PaymentFilter.java +++ b/src/main/java/com/bravo/user/model/filter/PaymentFilter.java @@ -5,8 +5,6 @@ import lombok.Data; import lombok.NoArgsConstructor; -import java.util.Set; - @Data @Builder @AllArgsConstructor diff --git a/src/main/java/com/bravo/user/service/PaymentService.java b/src/main/java/com/bravo/user/service/PaymentService.java index 20ff6d8..79d9312 100644 --- a/src/main/java/com/bravo/user/service/PaymentService.java +++ b/src/main/java/com/bravo/user/service/PaymentService.java @@ -44,7 +44,7 @@ public List retrieve( final PageRequest pageRequest, final HttpServletResponse httpResponse ){ - LOGGER.info("Request to retrieve payment information being conducted... paymentFilter: {}", filter); + LOGGER.trace("Request to retrieve payment information being conducted... paymentFilter: {}", filter); final PaymentSpecification specification = new PaymentSpecification(filter); final Page paymentPage = paymentRepository.findAll(specification,pageRequest); final List payments = resourceMapper.convertPayments(paymentPage.getContent()); diff --git a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java index 8f92b98..68a82bc 100644 --- a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java +++ b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java @@ -54,7 +54,7 @@ public class PaymentControllerTest { public void beforeEach(){ this.payments = IntStream .range(1,6) - .mapToObj(id -> new PaymentDto(Integer.toString(id))) + .mapToObj(id -> PaymentDto.builder().id(Integer.toString(id)).build()) .collect(Collectors.toList()); this.paymentFilter = new PaymentFilter("1"); diff --git a/src/test/java/com/bravo/user/service/PaymentServiceTest.java b/src/test/java/com/bravo/user/service/PaymentServiceTest.java index d3ad627..1fd7767 100644 --- a/src/test/java/com/bravo/user/service/PaymentServiceTest.java +++ b/src/test/java/com/bravo/user/service/PaymentServiceTest.java @@ -22,7 +22,6 @@ import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.stream.Collectors; -import java.util.stream.IntStream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -50,20 +49,17 @@ public class PaymentServiceTest { @BeforeEach public void beforeEach() { - final List ids = IntStream - .range(1,6) - .boxed() - .collect(Collectors.toList()); + final List ids = List.of(1, 2, 3, 4, 5, 6); this.paymentFilter = new PaymentFilter("1"); this.paymentDtos = ids.stream() - .map(id -> new PaymentDto(Integer.toString(id))) + .map(id -> PaymentDto.builder().id(Integer.toString(id)).build()) .collect(Collectors.toList()); when(resourceMapper.convertPayments(anyList())).thenReturn(paymentDtos); final List payments = ids.stream() - .map(id -> createPayment(id.toString())) + .map(id -> new Payment(id.toString())) .collect(Collectors.toList()); final Page mockPage = mock(Page.class); @@ -115,7 +111,6 @@ public void retrieveWithFilter() { final PaymentSpecification specification = new PaymentSpecification(paymentFilter); verify(paymentRepository).findAll(specification, pageRequest); - } @Test @@ -129,13 +124,5 @@ public void retrieveWithFilterWithPagination() { final PaymentSpecification specification = new PaymentSpecification(paymentFilter); verify(paymentRepository).findAll(specification, pageRequest); - - } - - private Payment createPayment(String paymentId) - { - final Payment payment = new Payment(); - payment.setId(paymentId); - return payment; } } From e55716f8f05c5c7addd32b8b6a2f68bf85473ad6 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 10 Aug 2022 15:58:37 -0500 Subject: [PATCH 6/8] changing the info loggers to debug --- src/main/java/com/bravo/user/service/PaymentService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bravo/user/service/PaymentService.java b/src/main/java/com/bravo/user/service/PaymentService.java index 79d9312..118db39 100644 --- a/src/main/java/com/bravo/user/service/PaymentService.java +++ b/src/main/java/com/bravo/user/service/PaymentService.java @@ -44,11 +44,11 @@ public List retrieve( final PageRequest pageRequest, final HttpServletResponse httpResponse ){ - LOGGER.trace("Request to retrieve payment information being conducted... paymentFilter: {}", filter); + LOGGER.debug("Request to retrieve payment information being conducted... paymentFilter: {}", filter); final PaymentSpecification specification = new PaymentSpecification(filter); final Page paymentPage = paymentRepository.findAll(specification,pageRequest); final List payments = resourceMapper.convertPayments(paymentPage.getContent()); - LOGGER.info("Found {} payment(s)", payments.size()); + LOGGER.debug("Found {} payment(s)", payments.size()); PageUtil.updatePageHeaders(httpResponse, paymentPage, pageRequest); return payments; From 535cd72854dd75d77128346f71e01feb47b48486 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 10 Aug 2022 16:24:18 -0500 Subject: [PATCH 7/8] made the PaymentFilter more immutable with @Value and @Jacksonized to still be usable. --- src/main/java/com/bravo/user/dao/model/Payment.java | 1 - .../java/com/bravo/user/model/filter/PaymentFilter.java | 9 ++++----- src/main/java/com/bravo/user/service/PaymentService.java | 2 +- .../com/bravo/user/controller/PaymentControllerTest.java | 2 +- .../java/com/bravo/user/service/PaymentServiceTest.java | 6 +++--- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/bravo/user/dao/model/Payment.java b/src/main/java/com/bravo/user/dao/model/Payment.java index ef5d377..ee9d144 100644 --- a/src/main/java/com/bravo/user/dao/model/Payment.java +++ b/src/main/java/com/bravo/user/dao/model/Payment.java @@ -7,7 +7,6 @@ import javax.persistence.Id; import javax.persistence.Table; -import lombok.Builder; import lombok.Data; @Entity diff --git a/src/main/java/com/bravo/user/model/filter/PaymentFilter.java b/src/main/java/com/bravo/user/model/filter/PaymentFilter.java index 542d0a8..e47c70a 100644 --- a/src/main/java/com/bravo/user/model/filter/PaymentFilter.java +++ b/src/main/java/com/bravo/user/model/filter/PaymentFilter.java @@ -1,14 +1,13 @@ package com.bravo.user.model.filter; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.Value; +import lombok.extern.jackson.Jacksonized; -@Data +@Value @Builder -@AllArgsConstructor -@NoArgsConstructor +@Jacksonized public class PaymentFilter { private String userId; diff --git a/src/main/java/com/bravo/user/service/PaymentService.java b/src/main/java/com/bravo/user/service/PaymentService.java index 118db39..d109675 100644 --- a/src/main/java/com/bravo/user/service/PaymentService.java +++ b/src/main/java/com/bravo/user/service/PaymentService.java @@ -36,7 +36,7 @@ public List retrievePaymentByUserId( final PageRequest pageRequest, final HttpServletResponse httpResponse ){ - return retrieve(new PaymentFilter(userId), pageRequest, httpResponse); + return retrieve(PaymentFilter.builder().userId(userId).build(), pageRequest, httpResponse); } public List retrieve( diff --git a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java index 68a82bc..042682f 100644 --- a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java +++ b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java @@ -57,7 +57,7 @@ public void beforeEach(){ .mapToObj(id -> PaymentDto.builder().id(Integer.toString(id)).build()) .collect(Collectors.toList()); - this.paymentFilter = new PaymentFilter("1"); + this.paymentFilter = PaymentFilter.builder().userId("1").build(); } @Test diff --git a/src/test/java/com/bravo/user/service/PaymentServiceTest.java b/src/test/java/com/bravo/user/service/PaymentServiceTest.java index 1fd7767..d1e50bc 100644 --- a/src/test/java/com/bravo/user/service/PaymentServiceTest.java +++ b/src/test/java/com/bravo/user/service/PaymentServiceTest.java @@ -51,7 +51,7 @@ public class PaymentServiceTest { public void beforeEach() { final List ids = List.of(1, 2, 3, 4, 5, 6); - this.paymentFilter = new PaymentFilter("1"); + this.paymentFilter = PaymentFilter.builder().userId("1").build(); this.paymentDtos = ids.stream() .map(id -> PaymentDto.builder().id(Integer.toString(id)).build()) @@ -80,7 +80,7 @@ public void retrievePaymentByUserId() { assertEquals("1", httpResponse.getHeader(PAGE_NUMBER)); assertEquals("20", httpResponse.getHeader(PAGE_SIZE)); - final PaymentFilter filter = new PaymentFilter(userId); + final PaymentFilter filter = PaymentFilter.builder().userId("1").build(); final PaymentSpecification specification = new PaymentSpecification(filter); verify(paymentRepository).findAll(specification, pageRequest); } @@ -95,7 +95,7 @@ public void retrievePaymentByUserIdWithPagination() { assertEquals("1", httpResponse.getHeader(PAGE_NUMBER)); assertEquals("6", httpResponse.getHeader(PAGE_SIZE)); - final PaymentFilter filter = new PaymentFilter(userId); + final PaymentFilter filter = PaymentFilter.builder().userId(userId).build(); final PaymentSpecification specification = new PaymentSpecification(filter); verify(paymentRepository).findAll(specification, pageRequest); } From 1e12677f7f6b9da665f69f336e50c72bb1d138d7 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 10 Aug 2022 16:50:49 -0500 Subject: [PATCH 8/8] Able to remove the constructor I used before, had to add annotations, but tests correctly and runs live correctly as well. --- src/main/java/com/bravo/user/dao/model/Payment.java | 9 ++++----- .../java/com/bravo/user/service/PaymentServiceTest.java | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/bravo/user/dao/model/Payment.java b/src/main/java/com/bravo/user/dao/model/Payment.java index ee9d144..15a31d4 100644 --- a/src/main/java/com/bravo/user/dao/model/Payment.java +++ b/src/main/java/com/bravo/user/dao/model/Payment.java @@ -7,10 +7,14 @@ import javax.persistence.Id; import javax.persistence.Table; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Data; @Entity @Data +@Builder +@AllArgsConstructor @Table(name = "payment") public class Payment { @@ -38,9 +42,4 @@ public Payment(){ this.id = UUID.randomUUID().toString(); this.updated = LocalDateTime.now(); } - - public Payment(final String id) { - super(); - this.id = id; - } } diff --git a/src/test/java/com/bravo/user/service/PaymentServiceTest.java b/src/test/java/com/bravo/user/service/PaymentServiceTest.java index d1e50bc..5d06749 100644 --- a/src/test/java/com/bravo/user/service/PaymentServiceTest.java +++ b/src/test/java/com/bravo/user/service/PaymentServiceTest.java @@ -59,7 +59,7 @@ public void beforeEach() { when(resourceMapper.convertPayments(anyList())).thenReturn(paymentDtos); final List payments = ids.stream() - .map(id -> new Payment(id.toString())) + .map(id -> Payment.builder().id(Integer.toString(id)).build()) .collect(Collectors.toList()); final Page mockPage = mock(Page.class);