diff --git a/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/PaymentBaseAccessor.java b/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/PaymentBaseAccessor.java index abb294e8..bf4eb852 100644 --- a/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/PaymentBaseAccessor.java +++ b/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/PaymentBaseAccessor.java @@ -194,6 +194,17 @@ public AccessorResponse> list() { throw new AccessorMethodNotImplementedException(); } + /** + * List all payments - Version 20260427 + * + * @return + */ + @GatewayAPI + @API(description = "List all payments version 20260427") + public AccessorResponse> list20260427() { + throw new AccessorMethodNotImplementedException(); + } + /** * Accessor for bill operations * diff --git a/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/RecurringPaymentBaseAccessor.java b/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/RecurringPaymentBaseAccessor.java index a8895df4..67f104ae 100644 --- a/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/RecurringPaymentBaseAccessor.java +++ b/mdx-models/src/main/java/com/mx/path/model/mdx/accessor/payment/RecurringPaymentBaseAccessor.java @@ -67,6 +67,17 @@ public AccessorResponse> frequencies() { throw new AccessorMethodNotImplementedException(); } + /** + * List all recurring payments - Version 20260427 + * + * @return + */ + @GatewayAPI + @API(description = "List all recurring payments verion 20260427") + public AccessorResponse> list20260427() { + throw new AccessorMethodNotImplementedException(); + } + /** * List all recurring payments * diff --git a/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/PaymentsController.java b/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/PaymentsController.java index 0f49df8b..824d9019 100644 --- a/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/PaymentsController.java +++ b/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/PaymentsController.java @@ -90,10 +90,19 @@ public final ResponseEntity createPayment(@RequestBody Payment paymentR return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); } + @SuppressWarnings("MagicNumber") @RequestMapping(value = "/payments", method = RequestMethod.GET) - public final ResponseEntity> getPaymentList() { - AccessorResponse> response = gateway().payments().list(); - return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); + public final ResponseEntity getPaymentList(HttpServletRequest request) { + return versioned(request) + .defaultVersion(MdxList.class, MdxList.class, payments -> { + AccessorResponse> response = gateway().payments().list(); + return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); + }) + .version(20260427, MdxList.class, MdxList.class, payments -> { + AccessorResponse> response = gateway().payments().list20260427(); + return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); + }) + .execute(); } @RequestMapping(value = "/payments/{id}", method = RequestMethod.GET) diff --git a/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/RecurringPaymentsController.java b/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/RecurringPaymentsController.java index 4aa2fab5..f02c9e2c 100644 --- a/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/RecurringPaymentsController.java +++ b/mdx-web/src/main/java/com/mx/path/model/mdx/web/controller/RecurringPaymentsController.java @@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import jakarta.servlet.http.HttpServletRequest; + @RestController @RequestMapping(value = "{clientId}", produces = BaseController.MDX_MEDIA) public class RecurringPaymentsController extends BaseController { @@ -23,10 +25,19 @@ public final ResponseEntity> getRecurringPaymentFrequencies() return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); } + @SuppressWarnings("MagicNumber") @RequestMapping(value = "/users/{userId}/recurring_payments", method = RequestMethod.GET) - public final ResponseEntity> getRecurringPayments() { - AccessorResponse> response = gateway().payments().recurring().list(); - return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); + public final ResponseEntity getRecurringPayments(HttpServletRequest request) { + return versioned(request) + .defaultVersion(MdxList.class, MdxList.class, recurringPayments -> { + AccessorResponse> response = gateway().payments().recurring().list(); + return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); + }) + .version(20260427, MdxList.class, MdxList.class, recurringPayments -> { + AccessorResponse> response = gateway().payments().recurring().list20260427(); + return new ResponseEntity<>(response.getResult().wrapped(), createMultiMapForResponse(response.getHeaders()), HttpStatus.OK); + }) + .execute(); } @RequestMapping(value = "/users/{userId}/recurring_payments", method = RequestMethod.POST, consumes = MDX_MEDIA) diff --git a/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/PaymentsControllerTest.groovy b/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/PaymentsControllerTest.groovy index 21e0fa88..ed2187a8 100644 --- a/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/PaymentsControllerTest.groovy +++ b/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/PaymentsControllerTest.groovy @@ -68,11 +68,11 @@ class PaymentsControllerTest extends Specification { BaseController.setGateway(gateway) def payment = new Payment() - def payments = new MdxList<>().tap { add(payment) } + def payments = new MdxList().tap { add(payment) } when: Mockito.doReturn(new AccessorResponse>().withResult(payments)).when(paymentGateway).list() - def response = subject.getPaymentList() + def response = subject.getPaymentList(buildRequest(null, "application/vnd.mx.mdx.v6+json")) then: HttpStatus.OK == response.getStatusCode() @@ -80,6 +80,23 @@ class PaymentsControllerTest extends Specification { verify(paymentGateway).list() || true } + def "getPayments v20260427 interacts with gateway"() { + given: + BaseController.setGateway(gateway) + + def payment = new Payment() + def payments = new MdxList().tap { add(payment) } + + when: + Mockito.doReturn(new AccessorResponse>().withResult(payments)).when(paymentGateway).list20260427() + def response = subject.getPaymentList(buildRequest(null, "application/vnd.mx.mdx.v6+json;version=20260427")) + + then: + HttpStatus.OK == response.getStatusCode() + response.getBody() == payments + verify(paymentGateway).list20260427() || true + } + def "createPayment interacts with gateway"() { given: BaseController.setGateway(gateway) diff --git a/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/RecurringPaymentsControllerTest.groovy b/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/RecurringPaymentsControllerTest.groovy index bad4a1b7..440a56b8 100644 --- a/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/RecurringPaymentsControllerTest.groovy +++ b/mdx-web/src/test/groovy/com/mx/path/model/mdx/web/controller/RecurringPaymentsControllerTest.groovy @@ -1,14 +1,20 @@ package com.mx.path.model.mdx.web.controller +import static org.mockito.Mockito.mock import static org.mockito.Mockito.spy import static org.mockito.Mockito.verify +import static org.mockito.Mockito.when +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.mx.path.core.context.Session import com.mx.path.gateway.accessor.AccessorResponse import com.mx.path.gateway.api.Gateway import com.mx.path.gateway.api.payment.PaymentGateway import com.mx.path.gateway.api.payment.RecurringPaymentGateway import com.mx.path.model.mdx.model.Frequency import com.mx.path.model.mdx.model.MdxList +import com.mx.path.model.mdx.model.Resources import com.mx.path.model.mdx.model.payment.RecurringPayment import org.mockito.Mockito @@ -16,17 +22,24 @@ import org.springframework.http.HttpStatus import spock.lang.Specification +import jakarta.servlet.http.HttpServletRequest + class RecurringPaymentsControllerTest extends Specification { RecurringPaymentsController subject Gateway gateway PaymentGateway paymentGateway RecurringPaymentGateway recurringPaymentGateway + Gson gson def setup() { subject = new RecurringPaymentsController() recurringPaymentGateway = spy(RecurringPaymentGateway.builder().build()) paymentGateway = PaymentGateway.builder().recurring(recurringPaymentGateway).build() gateway = Gateway.builder().payments(paymentGateway).build() + + GsonBuilder builder = new GsonBuilder() + Resources.registerResources(builder) + gson = builder.create() } def cleanup() { @@ -58,13 +71,29 @@ class RecurringPaymentsControllerTest extends Specification { when: Mockito.doReturn(new AccessorResponse>().withResult(recurringPayments)).when(recurringPaymentGateway).list() - def response = subject.getRecurringPayments() + def response = subject.getRecurringPayments(buildRequest(null, "application/vnd.mx.mdx.v6+json")) then: verify(recurringPaymentGateway).list() || true response.getBody() == recurringPayments } + def "index recurring payments v20260427 invokes gateway"() { + given: + RecurringPayment recurringPayment = new RecurringPayment() + MdxList recurringPayments = new MdxList<>() + recurringPayments.add(recurringPayment) + BaseController.setGateway(gateway) + + when: + Mockito.doReturn(new AccessorResponse>().withResult(recurringPayments)).when(recurringPaymentGateway).list20260427() + def response = subject.getRecurringPayments(buildRequest(null, "application/vnd.mx.mdx.v6+json;version=20260427")) + + then: + verify(recurringPaymentGateway).list20260427() || true + response.getBody() == recurringPayments + } + def "get recurring payment invokes gateway"() { given: RecurringPayment recurringPayment = new RecurringPayment() @@ -126,4 +155,16 @@ class RecurringPaymentsControllerTest extends Specification { verify(recurringPaymentGateway).cancel(recurringPayment.id) || true HttpStatus.NO_CONTENT == response.getStatusCode() } + + def buildRequest(Object body, String contentType) { + HttpServletRequest request = mock(HttpServletRequest.class) + when(request.getReader()).thenReturn(new BufferedReader(new StringReader(gson.toJson(body)))) + if (Session.current() != null) { + when(request.getHeader("mx-session-key")).thenReturn(Session.current().getId()) + } + when(request.getHeaders("Content-Type")).thenReturn(Collections.enumeration([contentType])) + when(request.getHeaders("Accept")).thenReturn(Collections.enumeration([contentType])) + + return request + } }