Skip to content
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

0013 - [backend] Add payment method retrieve API #33

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/com/bravo/user/controller/PaymentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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.*;

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
Expand Up @@ -48,7 +48,8 @@ public <T extends Collection<Payment>> List<PaymentDto> convertPayments(final T
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));
// Modified, substring gave 5 digits instead of 4 digits
dto.setCardNumberLast4(cardNumber.substring(cardNumber.length() - 4));
return dto;
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/bravo/user/dao/repository/PaymentRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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;

import java.util.List;

@Repository
public interface PaymentRepository extends JpaRepository<Payment, String>, JpaSpecificationExecutor<Payment> {

List<Payment> findByUserId(final String userId);
}
32 changes: 32 additions & 0 deletions src/main/java/com/bravo/user/service/PaymentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.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.findByUserId(userId);
LOGGER.info("found {} payment(s)", paymentList.size());

return resourceMapper.convertPayments(paymentList);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -648,3 +648,9 @@ 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
('4361116e-bae1-4539-9144-2b17ac76216c', '008a4215-0b1d-445e-b655-a964039cbb5a', '6011836725346986', 09, 2026),
('85b8cf40-2953-4004-8289-82a72110e75c', '008a4215-0b1d-445e-b655-a964039cbb5a', '373996186314348', 11, 2022),
('622ef959-da96-4448-bfa1-0cb82d21af3f', '00963d9b-f884-485e-9455-fcf30c6ac379', '371297579926182', 08, 2023),
('edc49523-1fe8-471e-9fe9-9f7861fb026e', '00bed3ac-5f3c-4a2d-a67b-80376ea9f941', '3559744047486115', 03, 2024);
85 changes: 85 additions & 0 deletions src/test/java/com/bravo/user/controller/PaymentControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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)
.mapToObj(this::createPaymentDto)
.collect(Collectors.toList());
}

@Test
void getRetrieveByUserId() 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 getRetrieveByUserId_Space() throws Exception {
this.mockMvc.perform(get("/payment/retrieve/ /")).andExpect(status().isBadRequest());
}

@Test
void getRetrieveByUserId_Missing() throws Exception {
this.mockMvc.perform(get("/payment/retrieve")).andExpect(status().isNotFound());
}

private PaymentDto createPaymentDto(final int id) {
final PaymentDto payment = new PaymentDto();
payment.setId(Integer.toString(id));
return payment;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bravo.user.dao.model.mapper;

import com.bravo.user.dao.model.Payment;
import com.bravo.user.model.dto.PaymentDto;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -48,4 +50,15 @@ void convertAddressTest(
@ConvertWith(MapperArgConverter.class) AddressDto addressDto) {
Assertions.assertEquals(addressDto, resourceMapper.convertAddress(address));
}

@ParameterizedTest
@CsvFileSource(
resources = ("/ResourceMapperTest/convertPaymentTest.csv"),
delimiter = '$',
lineSeparator = ">")
void convertPaymentTest(
@ConvertWith(MapperArgConverter.class) Payment payment,
@ConvertWith(MapperArgConverter.class) PaymentDto paymentDto) {
Assertions.assertEquals(paymentDto, resourceMapper.convertPayment(payment));
}
}
78 changes: 78 additions & 0 deletions src/test/java/com/bravo/user/service/PaymentServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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> dtoPayments;

@BeforeEach
public void beforeEach() {
final List<Payment> daoPayments = IntStream.range(1, 10)
.mapToObj(this::createPayment)
.collect(Collectors.toList());

when(paymentRepository.findByUserId(anyString())).thenReturn(daoPayments);

this.dtoPayments = daoPayments.stream()
.map(this::createPaymentDto)
.collect(Collectors.toList());

when(resourceMapper.convertPayments(daoPayments)).thenReturn(dtoPayments);
}

@Test
void retrieveByUserId() {
final String userId = "123a-456b";
final List<PaymentDto> results = paymentService.retrieveByUserId(userId);
assertEquals(dtoPayments, results);

verify(paymentRepository).findByUserId(userId);
}

private Payment createPayment(final int id) {
final Payment payment = new Payment();
payment.setId(Integer.toString(id));
return payment;
}

private PaymentDto createPaymentDto(final Payment payment) {
final PaymentDto paymentDta = new PaymentDto();
paymentDta.setId(payment.getId());
return paymentDta;
}

}
30 changes: 30 additions & 0 deletions src/test/resources/ResourceMapperTest/convertPaymentTest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
>{
"id":"testId",
"userId":"testUserId",
"cardNumber":"3570643196366822",
"expiryMonth":"02",
"expiryYear":"2024",
"updated":"2022-04-13 12:00:00"
}
${
"id":"testId",
"cardNumberLast4":"6822",
"expiryMonth":"02",
"expiryYear":"2024",
"updated":"2022-04-13 12:00:00"
}
>{
"id":"testId",
"userId":"testUserId",
"cardNumber":"30353997949251",
"expiryMonth":"09",
"expiryYear":"2023",
"updated":"2022-08-17 12:00:00"
}
${
"id":"testId",
"cardNumberLast4":"9251",
"expiryMonth":"09",
"expiryYear":"2023",
"updated":"2022-08-17 12:00:00"
}