diff --git a/src/main/java/com/bravo/user/controller/PaymentController.java b/src/main/java/com/bravo/user/controller/PaymentController.java new file mode 100644 index 0000000..2129dfc --- /dev/null +++ b/src/main/java/com/bravo/user/controller/PaymentController.java @@ -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())}) + @GetMapping(value = "/retrieve/{userId}") + @ResponseBody + public List retrieve( + final @PathVariable String userId, + final HttpServletResponse httpResponse + ) + { + log.info("Userid:{}", userId); + userValidator.validateId(userId); + return paymentService.retrieveByUserId(userId); + } + +} diff --git a/src/main/java/com/bravo/user/dao/repository/PaymentRepository.java b/src/main/java/com/bravo/user/dao/repository/PaymentRepository.java new file mode 100644 index 0000000..b50f83d --- /dev/null +++ b/src/main/java/com/bravo/user/dao/repository/PaymentRepository.java @@ -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 { + List findByUserId(final String userId); +} diff --git a/src/main/java/com/bravo/user/service/PaymentService.java b/src/main/java/com/bravo/user/service/PaymentService.java new file mode 100644 index 0000000..eb9d682 --- /dev/null +++ b/src/main/java/com/bravo/user/service/PaymentService.java @@ -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 retrieveByUserId(final String userId) { + final List paymentList = paymentRepository.findByUserId(userId); + log.info("found {} payment(s)", paymentList.size()); + + return resourceMapper.convertPayments(paymentList); + } + +} diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index c4868b9..7f08233 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -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); + \ No newline at end of file diff --git a/src/test/java/com/bravo/user/controller/PaymentControllerTest.java b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java new file mode 100644 index 0000000..ec5b753 --- /dev/null +++ b/src/test/java/com/bravo/user/controller/PaymentControllerTest.java @@ -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 payments; + + @BeforeEach + public void beforeEach() { + log.info("Initializing payment list"); + final List 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 { + + 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(); + payment.setId(userId); + payment.setCardNumberLast4("1111"); + payment.setExpiryMonth(00); + payment.setExpiryYear(11); + return payment; + } +} diff --git a/src/test/java/com/bravo/user/service/PaymentServiceTest.java b/src/test/java/com/bravo/user/service/PaymentServiceTest.java new file mode 100644 index 0000000..02798b9 --- /dev/null +++ b/src/test/java/com/bravo/user/service/PaymentServiceTest.java @@ -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 dtoPayments; + + @BeforeEach + public void beforeEach() { + log.info("Initializing payment list"); + final List userIds = IntStream.range(1, 10).boxed().toList(); + + final List daoPayments = userIds.stream() + .map(id -> createPayment(Integer.toString(id))).collect(Collectors.toList()); + + when(paymentRepository.findByUserId(anyString())).thenReturn(daoPayments); + + this.dtoPayments = userIds.stream().map(id -> createPaymentDto(Integer.toString(id))) + .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 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; + } + +}