-
Notifications
You must be signed in to change notification settings - Fork 41
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
jaxtell - added get payment by user id functionality #28
Open
jaxtell
wants to merge
1
commit into
BravoLT:main
Choose a base branch
from
jaxtell:jaxtell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/bravo/user/controller/PaymentController.java
This file contains 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,44 @@ | ||
package com.bravo.user.controller; | ||
|
||
import java.util.List; | ||
|
||
import com.bravo.user.utility.PageUtil; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
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 javax.servlet.http.HttpServletResponse; | ||
|
||
@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 = "/{userId}/retrieve") | ||
@ResponseBody | ||
public List<PaymentDto> retrieve( | ||
final @PathVariable(name = "userId") String userId, | ||
final @RequestParam(required = false) Integer page, | ||
final @RequestParam(required = false) Integer size, | ||
final HttpServletResponse httpResponse | ||
) { | ||
|
||
userValidator.validateId(userId); | ||
final PageRequest pageRequest = PageUtil.createPageRequest(page, size); | ||
return paymentService.retrieveByUserId(userId, pageRequest, httpResponse); | ||
|
||
} | ||
|
||
} |
This file contains 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
18 changes: 18 additions & 0 deletions
18
src/main/java/com/bravo/user/dao/repository/PaymentRepository.java
This file contains 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,18 @@ | ||
package com.bravo.user.dao.repository; | ||
|
||
import com.bravo.user.dao.model.Payment; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
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<Payment, String>, JpaSpecificationExecutor<Payment> { | ||
|
||
// TODO Users instead uses findAll with a specification and filter. | ||
//If we need to filter on more than just user id then this should too | ||
//But for now this should work and be simpler. | ||
Page<Payment> findByUserId(String userID, Pageable pageRequest); | ||
} |
This file contains 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,37 @@ | ||
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 com.bravo.user.utility.PageUtil; | ||
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 final PaymentRepository paymentRepository; | ||
private final ResourceMapper resourceMapper; | ||
|
||
public PaymentService(PaymentRepository paymentRepository, ResourceMapper resourceMapper) { | ||
this.paymentRepository = paymentRepository; | ||
this.resourceMapper = resourceMapper; | ||
|
||
} | ||
|
||
public List<PaymentDto> retrieveByUserId(String userId, | ||
final PageRequest pageRequest, | ||
final HttpServletResponse httpResponse) { | ||
Page<Payment> paymentPage = paymentRepository.findByUserId(userId, pageRequest); | ||
List<PaymentDto> payments = resourceMapper.convertPayments(paymentPage.getContent()); | ||
PageUtil.updatePageHeaders(httpResponse, paymentPage, pageRequest); | ||
return payments; | ||
} | ||
|
||
|
||
} |
This file contains 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
90 changes: 90 additions & 0 deletions
90
src/test/java/com/bravo/user/controller/PaymentControllerTest.java
This file contains 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,90 @@ | ||
package com.bravo.user.controller; | ||
|
||
import com.bravo.user.App; | ||
import com.bravo.user.model.dto.PaymentDto; | ||
import com.bravo.user.service.PaymentService; | ||
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.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.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.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<PaymentDto> payments; | ||
|
||
@BeforeEach | ||
public void beforeEach(){ | ||
final List<Integer> ids = IntStream | ||
.range(1, 10) | ||
.boxed() | ||
.collect(Collectors.toList()); | ||
|
||
this.payments = ids.stream() | ||
.map(id -> createPaymentDto(Integer.toString(id))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Test | ||
void getRetrieve() throws Exception { | ||
when(paymentService | ||
.retrieveByUserId(anyString(), any(PageRequest.class), any(HttpServletResponse.class))) | ||
.thenReturn(payments); | ||
|
||
final ResultActions result = this.mockMvc | ||
.perform(get("/payment/testid/retrieve")) | ||
.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).retrieveByUserId( | ||
eq("testid"), eq(pageRequest), any(HttpServletResponse.class) | ||
); | ||
} | ||
|
||
@Test | ||
void getRetrieveWithIdMissing() 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); | ||
payment.setCardNumberLast4("0000"); | ||
return payment; | ||
} | ||
} |
This file contains 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
97 changes: 97 additions & 0 deletions
97
src/test/java/com/bravo/user/service/PaymentServiceTest.java
This file contains 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,97 @@ | ||
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 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<PaymentDto> dtoPayments; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
final List<Integer> ids = IntStream | ||
.range(1, 10) | ||
.boxed() | ||
.collect(Collectors.toList()); | ||
|
||
final Page<Payment> mockPage = mock(Page.class); | ||
when(paymentRepository.findByUserId(any(String.class), any(PageRequest.class))) | ||
.thenReturn(mockPage); | ||
|
||
final List<Payment> daoPayments = ids.stream() | ||
.map(id -> createPayment(Integer.toString(id))) | ||
.collect(Collectors.toList()); | ||
when(mockPage.getContent()).thenReturn(daoPayments); | ||
when(mockPage.getTotalPages()).thenReturn(9); | ||
|
||
this.dtoPayments = ids.stream() | ||
.map(id -> createPaymentDto(Integer.toString(id))) | ||
.collect(Collectors.toList()); | ||
when(resourceMapper.convertPayments(daoPayments)).thenReturn(dtoPayments); | ||
} | ||
|
||
@Test | ||
public void retrieveByName() { | ||
final String input = "input"; | ||
final PageRequest pageRequest = PageUtil.createPageRequest(null, null); | ||
final List<PaymentDto> results = paymentService.retrieveByUserId(input, pageRequest, httpResponse); | ||
assertEquals(dtoPayments, results); | ||
assertEquals("9", httpResponse.getHeader("page-count")); | ||
assertEquals("1", httpResponse.getHeader("page-number")); | ||
assertEquals("20", httpResponse.getHeader("page-size")); | ||
|
||
verify(paymentRepository).findByUserId(input, pageRequest); | ||
} | ||
|
||
private PaymentDto createPaymentDto(final String id) { | ||
final PaymentDto payment = new PaymentDto(); | ||
payment.setId(id); | ||
payment.setCardNumberLast4("0000"); | ||
return payment; | ||
} | ||
|
||
private Payment createPayment(final String id) { | ||
final Payment payment = new Payment(); | ||
payment.setId(id); | ||
payment.setCardNumber("5400000000000000"); | ||
return payment; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/resources/ResourceMapperTest/convertPaymentTest.csv
This file contains 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,15 @@ | ||
>{ | ||
"id":"testId", | ||
"userId":"testUserId", | ||
"cardNumber":"4444444444441234", | ||
"expiryMonth":"12", | ||
"expiryYear":"99", | ||
"updated":"2021-07-12 12:00:00" | ||
} | ||
${ | ||
"id":"testId", | ||
"cardNumberLast4":"1234", | ||
"expiryMonth":"12", | ||
"expiryYear":"99", | ||
"updated":"2021-07-12 12:00:00" | ||
} |
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.
Any comment on how the endpoint should look if you were to design it without pattern following?
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.
It depends on how it would be used. If user id is always going to be present it might make sense to be /user/{userId}/payment. If there are future requests that allow searching across users then I'd leave it top level. Instead of using words in the URL I'd favor http verbs to indicate action, e.g. GET on /payment/user/{userId} to get all payments for a user and/or GET on /payment with parameters to get payment(s) based on query parameters, PUT on /payment to add a payment, PATCH on /payment to update a payment, POSTs on /payment/[ACTION] for more complex operations. I'd want to balance keeping the URL short while still being descriptive of what it does.