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

avi8pE7N : Ticket from trello by [email protected] #50

Open
wants to merge 2 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
53 changes: 53 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,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) {
Copy link
Member

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?

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())})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty @Schema() is weird. Additionally the @ArraySchema seems better in this situation because you can list the PaymentDto type as well.
https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#arrayschema

@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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

httpResponse is unused, and this log is vague and unnecessary.

}

}
11 changes: 11 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,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);
}
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 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());
Copy link
Member

Choose a reason for hiding this comment

The 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);
}

}
14 changes: 14 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,17 @@ 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');

TRUNCATE TABLE payment RESTART IDENTITY;
insert into payment (id, user_id, card_number, expiry_month, expiry_year) values
('2c0807a3-8698-4f70-bceb-47df9e7bf663','008a4215-0b1d-445e-b655-a964039cbb5a','4111111111111111',03,29),
('83df293c-51e2-4c0b-8d3b-659594b526f0','00963d9b-f884-485e-9455-fcf30c6ac379','4111111111111112',04,28),
('3653e97a-a0b9-41cb-ad7a-ce8cd4d6ef1a','00bed3ac-5f3c-4a2d-a67b-80376ea9f941','4111111111111113',05,27),
('51acef19-df8c-4065-ba82-740c7d803012','0111d3ca-514b-4ae8-8f57-e85cca43fb1e','4111111111111114',06,26),
('4b0c55ab-373f-49d1-832a-f3ae39acc583','01316816-0cb7-41c4-8424-8367294aea27','4111111111111115',07,25),
('b76316bb-6b2f-4436-942f-b0deb7f514ed','008a4215-0b1d-445e-b655-a964039cbb5a','4111111111111116',08,26),
('cf48c4d0-fc67-42c5-9815-d2a5ea589c47','00963d9b-f884-485e-9455-fcf30c6ac379','4111111111111117',09,27),
('018d4c9a-00a2-4574-8711-057719bf72e0','00bed3ac-5f3c-4a2d-a67b-80376ea9f941','4111111111111118',10,25),
('ef4b5545-ae2b-4898-80dd-3b505744d540','0111d3ca-514b-4ae8-8f57-e85cca43fb1e','4111111111111119',11,28),
('7df8fdc8-91b6-4100-914c-ff4cd89ddab0','01316816-0cb7-41c4-8424-8367294aea27','4111111111111122',12,29);

76 changes: 76 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,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");
Copy link
Member

@wheeleruniverse wheeleruniverse Aug 25, 2023

Choose a reason for hiding this comment

The 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 public modifier is unnecessary.

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 {
Copy link
Member

Choose a reason for hiding this comment

The 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Builder from Lombok might make this code nicer.

payment.setId(userId);
payment.setCardNumberLast4("1111");
payment.setExpiryMonth(00);
payment.setExpiryYear(11);
return payment;
}
}
87 changes: 87 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,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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you update the createPayment method to accept a String then you can use method reference instead of this Lambda.

.map(this::createPayment)


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

this.dtoPayments = userIds.stream().map(id -> createPaymentDto(Integer.toString(id)))
.collect(Collectors.toList());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does .toList() work instead of .collect(Collectors.toList())?


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;
}

}