-
Notifications
You must be signed in to change notification settings - Fork 39
Robert test branch #39
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.bravo.user.controller; | ||
|
|
||
| import com.bravo.user.annotation.SwaggerController; | ||
| import com.bravo.user.model.dto.PaymentDto; | ||
| import com.bravo.user.service.PaymentService; | ||
| import com.bravo.user.validator.UserValidator; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.ResponseBody; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RequestMapping(value = "/payment") | ||
| @SwaggerController | ||
| public class PaymentController { | ||
|
|
||
| private final PaymentService paymentService; | ||
| private final UserValidator userValidator; | ||
|
|
||
| public PaymentController(PaymentService paymentService, UserValidator userValidator) { | ||
| this.paymentService = paymentService; | ||
| this.userValidator = userValidator; | ||
| } | ||
|
|
||
| @GetMapping(value = "/retrieve/{userId}") | ||
| @ResponseBody | ||
| public List<PaymentDto> retrieve(final @PathVariable String userId) { | ||
| userValidator.validateId(userId); | ||
| return paymentService.retrieveByUserId(userId); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.bravo.user.dao.repository; | ||
|
|
||
| import com.bravo.user.dao.model.Payment; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public interface PaymentRepository extends JpaRepository<Payment, String> { | ||
|
|
||
| List<Payment> findPaymentByUserId(final String userId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 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.exception.DataNotFoundException; | ||
| import com.bravo.user.model.dto.PaymentDto; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| 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<PaymentDto> retrieveByUserId(final String userId) { | ||
| final List<Payment> paymentList = paymentRepository.findPaymentByUserId(userId); | ||
| if (paymentList.isEmpty()) { | ||
| throw new DataNotFoundException(String.format("No payment methods were found for user %s", userId)); | ||
| } | ||
| LOGGER.info("found {} payment method(s)", paymentList.size()); | ||
|
|
||
| return resourceMapper.convertPayments(paymentList); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.bravo.user.controller; | ||
|
|
||
| import com.bravo.user.App; | ||
| import com.bravo.user.model.dto.PaymentDto; | ||
| import com.bravo.user.service.PaymentService; | ||
| 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.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 java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
| 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 | ||
| class PaymentControllerTest { | ||
|
|
||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @MockBean | ||
| private PaymentService paymentService; | ||
|
|
||
| private List<PaymentDto> payments; | ||
|
|
||
| @BeforeEach | ||
| public void beforeEach() { | ||
| this.payments = IntStream.range(1, 10).boxed() | ||
| .map(id -> createPaymentDto(Integer.toString(id))) | ||
| .collect(Collectors.toList()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call out. I have refactored the code and fixed this to removed the redundant variable. |
||
| } | ||
|
|
||
| @Test | ||
| void givenValidUserId_retrievePayment_shouldReturnPayment() throws Exception { | ||
| final String userId = "123a-456b"; | ||
|
|
||
| when(paymentService.retrieveByUserId(anyString())).thenReturn(payments); | ||
|
|
||
| final ResultActions result = this.mockMvc.perform(get("/payment/retrieve/".concat(userId))) | ||
| .andExpect(status().isOk()); | ||
|
|
||
| for (int i = 0; i < payments.size(); i++) { | ||
| result.andExpect(jsonPath(String.format("$[%d].id", i)).value(payments.get(i).getId())); | ||
| } | ||
|
|
||
| verify(paymentService).retrieveByUserId(userId); | ||
| } | ||
|
|
||
| @Test | ||
| void givenWhitespaceUserId_retrievePayment_shouldBadRequestException() throws Exception { | ||
| this.mockMvc.perform(get("/payment/retrieve/ /")).andExpect(status().isBadRequest()); | ||
| } | ||
|
|
||
| @Test | ||
| void givenMissingUserId_retrievePayment_shouldThrowNotFoundException() throws Exception { | ||
| this.mockMvc.perform(get("/payment/retrieve")).andExpect(status().isNotFound()); | ||
| } | ||
|
|
||
| private PaymentDto createPaymentDto(final String id) { | ||
| final PaymentDto payment = new PaymentDto(); | ||
| payment.setId(id); | ||
| return payment; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| 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.model.dto.PaymentDto; | ||
| 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.test.context.ContextConfiguration; | ||
| import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
|
||
| 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.anyString; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ContextConfiguration(classes = { App.class }) | ||
| @ExtendWith(SpringExtension.class) | ||
| @SpringBootTest | ||
| class PaymentServiceTest { | ||
|
|
||
| @Autowired | ||
| private PaymentService paymentService; | ||
|
|
||
| @MockBean | ||
| private ResourceMapper resourceMapper; | ||
|
|
||
| @MockBean | ||
| private PaymentRepository paymentRepository; | ||
|
|
||
| private List<PaymentDto> paymentDtos; | ||
|
|
||
| @BeforeEach | ||
| public void beforeEach() { | ||
| final List<Integer> ids = IntStream.range(1, 10).boxed().collect(Collectors.toList()); | ||
|
|
||
| final List<Payment> daoPayments = ids.stream() | ||
| .map(id -> createPayment(Integer.toString(id))).collect(Collectors.toList()); | ||
|
|
||
| when(paymentRepository.findPaymentByUserId(anyString())).thenReturn(daoPayments); | ||
|
|
||
| this.paymentDtos = ids.stream().map(id -> createPaymentDto(Integer.toString(id))) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| when(resourceMapper.convertPayments(daoPayments)).thenReturn(paymentDtos); | ||
| } | ||
|
|
||
| @Test | ||
| void givenValidUserId_findPaymentByUserId_retrievesPaymentMethods() { | ||
| final String userId = "123a-456b"; | ||
| final List<PaymentDto> results = paymentService.retrieveByUserId(userId); | ||
| assertEquals(paymentDtos, results); | ||
|
|
||
| verify(paymentRepository).findPaymentByUserId(userId); | ||
| } | ||
|
|
||
| private Payment createPayment(final String id) { | ||
| final Payment payment = new Payment(); | ||
| payment.setId(id); | ||
| return payment; | ||
| } | ||
|
|
||
| private PaymentDto createPaymentDto(final String id) { | ||
| final PaymentDto paymentDto = new PaymentDto(); | ||
| paymentDto.setId(id); | ||
| return paymentDto; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| >{ | ||
| "id":"testId", | ||
| "userId":"testUserId", | ||
| "cardNumber":"1234567890123456", | ||
| "expiryMonth":"01", | ||
| "expiryYear":"2023", | ||
| "updated":"2021-07-12 12:00:00" | ||
| } | ||
| ${ | ||
| "id":"testId", | ||
| "cardNumberLast4":"3456", | ||
| "expiryMonth":"01", | ||
| "expiryYear":"2023", | ||
| "updated":"2021-07-12 12:00:00" | ||
| } |
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.
Good job catching this bug! 👍🏻