-
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
avi8pE7N : Ticket from trello by [email protected] #50
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,53 @@ | ||
package com.bravo.user.controller; | ||
|
||
import java.util.List; | ||
|
||
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 org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.bravo.user.model.dto.PaymentDto; | ||
import com.bravo.user.service.PaymentService; | ||
import com.bravo.user.validator.UserValidator; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestController | ||
@Tag(name="Payment", description="Payment Actions") | ||
@RequestMapping(value = "/payment") | ||
public class PaymentController { | ||
private final PaymentService paymentService; | ||
private final UserValidator userValidator; | ||
|
||
public PaymentController(PaymentService paymentService, UserValidator userValidator) { | ||
this.paymentService = paymentService; | ||
this.userValidator = userValidator; | ||
} | ||
|
||
@Operation(summary = "Retrieve User payment Information") | ||
@ApiResponse(responseCode = "200", content = { | ||
@Content(schema = @Schema(implementation = List.class), mediaType = "application/json") | ||
}) | ||
@ApiResponse(responseCode = "400", content = {@Content(schema = @Schema())}) | ||
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. The empty |
||
@GetMapping(value = "/retrieve/{userId}") | ||
@ResponseBody | ||
public List<PaymentDto> retrieve( | ||
final @PathVariable String userId, | ||
final HttpServletResponse httpResponse | ||
) | ||
{ | ||
log.info("Userid:{}", userId); | ||
userValidator.validateId(userId); | ||
return paymentService.retrieveByUserId(userId); | ||
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.
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.bravo.user.dao.repository; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.bravo.user.dao.model.Payment; | ||
|
||
public interface PaymentRepository extends JpaRepository<Payment, String> { | ||
List<Payment> findByUserId(final String userId); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.bravo.user.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.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 lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@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(final String userId) { | ||
final List<Payment> paymentList = paymentRepository.findByUserId(userId); | ||
log.info("found {} payment(s)", paymentList.size()); | ||
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. Maybe the userId belongs in this log statement as well? |
||
|
||
return resourceMapper.convertPayments(paymentList); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.bravo.user.controller; | ||
|
||
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; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
|
||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.ResultActions; | ||
|
||
import com.bravo.user.model.dto.PaymentDto; | ||
import com.bravo.user.service.PaymentService; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@SpringBootTest() | ||
@AutoConfigureMockMvc | ||
public class PaymentControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private PaymentService paymentService; | ||
|
||
private List<PaymentDto> payments; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
log.info("Initializing payment list"); | ||
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. I think logs in tests are usually unnecessary as there are methods built into the testing tools to provide more clear output. Additionally the |
||
final List<Integer> userIds = IntStream.range(1, 10).boxed().toList(); | ||
|
||
this.payments = userIds.stream().map(id -> createPaymentDto(Integer.toString(id))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Test | ||
void getRetrieveByUserId() throws Exception { | ||
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. Weird formatting. What IDE are you using? |
||
|
||
log.info("ENTRY::: PaymentControllerTest.getRetrieveByUserId"); | ||
final String userId = "00963d9b-f884-485e-9455-fcf30c6ac379"; | ||
|
||
when(paymentService.retrieveByUserId(anyString())).thenReturn(this.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())); | ||
} | ||
log.info("Assignment: By Verify the retrieveByUserId "); | ||
verify(paymentService).retrieveByUserId(userId); | ||
} | ||
|
||
private PaymentDto createPaymentDto(String userId) { | ||
final PaymentDto payment = new PaymentDto(); | ||
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.
|
||
payment.setId(userId); | ||
payment.setCardNumberLast4("1111"); | ||
payment.setExpiryMonth(00); | ||
payment.setExpiryYear(11); | ||
return payment; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.bravo.user.service; | ||
|
||
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; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
|
||
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 lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@SpringBootTest | ||
public class PaymentServiceTest { | ||
|
||
@Autowired | ||
private PaymentService paymentService; | ||
|
||
@MockBean | ||
private ResourceMapper resourceMapper; | ||
|
||
@MockBean | ||
private PaymentRepository paymentRepository; | ||
|
||
private List<PaymentDto> dtoPayments; | ||
|
||
@BeforeEach | ||
public void beforeEach() { | ||
log.info("Initializing payment list"); | ||
final List<Integer> userIds = IntStream.range(1, 10).boxed().toList(); | ||
|
||
final List<Payment> daoPayments = userIds.stream() | ||
.map(id -> createPayment(Integer.toString(id))).collect(Collectors.toList()); | ||
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. If you update the
|
||
|
||
when(paymentRepository.findByUserId(anyString())).thenReturn(daoPayments); | ||
|
||
this.dtoPayments = userIds.stream().map(id -> createPaymentDto(Integer.toString(id))) | ||
.collect(Collectors.toList()); | ||
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. Does |
||
|
||
when(resourceMapper.convertPayments(daoPayments)).thenReturn(dtoPayments); | ||
} | ||
|
||
@Test | ||
void getRetrieveByUserId() throws Exception { | ||
|
||
log.info("ENTRY::: PaymentServiceTest.getRetrieveByUserId"); | ||
final String userId = "00963d9b-f884-485e-9455-fcf30c6ac379"; | ||
|
||
final List<PaymentDto> results = paymentService.retrieveByUserId(userId); | ||
log.info("By do the Assert Equals"); | ||
assertEquals(dtoPayments, results); | ||
log.info("By Verify"); | ||
verify(paymentRepository).findByUserId(userId); | ||
} | ||
|
||
private Payment createPayment(final String id) { | ||
final Payment payment = new Payment(); | ||
payment.setId(id); | ||
payment.setCardNumber("1111"); | ||
payment.setExpiryMonth(00); | ||
payment.setExpiryYear(11); | ||
return payment; | ||
} | ||
|
||
private PaymentDto createPaymentDto(String userId) { | ||
final PaymentDto payment = new PaymentDto(); | ||
payment.setId(userId); | ||
payment.setCardNumberLast4("1111"); | ||
payment.setExpiryMonth(00); | ||
payment.setExpiryYear(11); | ||
return payment; | ||
} | ||
|
||
} |
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.
Have you heard of
@RequiredArgsConstructor
from Lombok?