Skip to content

Commit

Permalink
Merge pull request #349 from Mangopay/bugfix/fix-tests
Browse files Browse the repository at this point in the history
Tests updates and fixes
  • Loading branch information
iulian03 authored Feb 4, 2025
2 parents 4d38a34 + b20bccd commit abf4a25
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/mangopay/MangoPayApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public MangoPayApi() {
setRegulatoryApi(new RegulatoryApiImpl(this));
setDepositApi(new DepositApiImpl(this));
setVirtualAccountApi(new VirtualAccountApiImpl(this));
setConversionsApi(new ConversionsApiImpl(this));
setConversionsApi(new ConversionsApiImpl(this, gsonBuilder));
setGson(gsonBuilder.create());
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/mangopay/core/APIs/KycDocumentApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mangopay.core.APIs;

import com.mangopay.core.DocumentPageConsult;
import com.mangopay.core.FilterKycDocuments;
import com.mangopay.core.Pagination;
import com.mangopay.core.Sorting;
import com.mangopay.entities.KycDocument;
Expand All @@ -20,6 +21,8 @@ public interface KycDocumentApi {
*/
List<KycDocument> getAll(Pagination pagination, Sorting sorting) throws Exception;

List<KycDocument> getAll(Pagination pagination, FilterKycDocuments filter, Sorting sorting) throws Exception;

/**
* Gets KYC document.
* @param kycDocumentId KYC document identifier.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mangopay.core.APIs.implementation;

import com.google.gson.GsonBuilder;
import com.mangopay.MangoPayApi;
import com.mangopay.core.APIs.ApiBase;
import com.mangopay.core.APIs.ConversionsApi;
import com.mangopay.core.serializer.CreateConversionQuoteSerializer;
import com.mangopay.entities.*;

public class ConversionsApiImpl extends ApiBase implements ConversionsApi {
Expand All @@ -12,8 +14,9 @@ public class ConversionsApiImpl extends ApiBase implements ConversionsApi {
*
* @param root Root/parent instance that holds the OAuthToken and Configuration instance.
*/
public ConversionsApiImpl(MangoPayApi root) {
public ConversionsApiImpl(MangoPayApi root, GsonBuilder gsonBuilder) {
super(root);
gsonBuilder.registerTypeAdapter(CreateConversionQuote.class, new CreateConversionQuoteSerializer());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mangopay.core.APIs.ApiBase;
import com.mangopay.core.APIs.KycDocumentApi;
import com.mangopay.core.DocumentPageConsult;
import com.mangopay.core.FilterKycDocuments;
import com.mangopay.core.Pagination;
import com.mangopay.core.Sorting;
import com.mangopay.entities.KycDocument;
Expand All @@ -27,6 +28,11 @@ public KycDocumentApiImpl(MangoPayApi root) {
public List<KycDocument> getAll(Pagination pagination, Sorting sorting) throws Exception {
return this.getList(KycDocument[].class, KycDocument.class, "kyc_documents_all", pagination, sorting);
}

@Override
public List<KycDocument> getAll(Pagination pagination, FilterKycDocuments filter, Sorting sorting) throws Exception {
return this.getList(KycDocument[].class, KycDocument.class, "kyc_documents_all", pagination, null, null, filter.getValues(), sorting);
}

@Override
public KycDocument getKycDocument(String kycDocumentId) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mangopay.core.serializer;

import com.google.gson.*;
import com.mangopay.core.Money;
import com.mangopay.entities.CreateConversionQuote;

import java.lang.reflect.Type;

public class CreateConversionQuoteSerializer implements JsonSerializer<CreateConversionQuote> {

/**
* For creating a Conversion Quote, the Money.Amount fields needs to be sent as NULL in some cases
*/
@Override
public JsonElement serialize(CreateConversionQuote src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject object = new Gson().toJsonTree(src, typeOfSrc).getAsJsonObject();

Money debitedFunds = src.getDebitedFunds();
Money creditedFunds = src.getCreditedFunds();

if (debitedFunds != null && creditedFunds != null) {
// as per API docs: the DebitedFunds.Amount is required if CreditedFunds.Amount is null
if (debitedFunds.getAmount() == 0 && creditedFunds.getAmount() != 0) {
JsonObject debitedFundsObj = object.getAsJsonObject("DebitedFunds");
debitedFundsObj.addProperty("Amount", (String) null);
object.add("DebitedFunds", debitedFundsObj);
}

// as per API docs: the CreditedFunds.Amount is required if DebitedFunds.Amount is null
if (debitedFunds.getAmount() != 0 && creditedFunds.getAmount() == 0) {
JsonObject creditedFundsObj = object.getAsJsonObject("CreditedFunds");
creditedFundsObj.addProperty("Amount", (String) null);
object.add("CreditedFunds", creditedFundsObj);
}
}

return object;
}
}
71 changes: 42 additions & 29 deletions src/test/java/com/mangopay/core/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public abstract class BaseTest {
private static UboDeclaration UBO_DECLARATION;
private static PayInTemplateURLOptions PAYIN_TEMPLATE_URL_OPTIONS;
private static VirtualAccount JOHNS_VIRTUAL_ACCOUNT;
private static Mandate MANDATE;

public BaseTest() {
this.api = buildNewMangoPayApi();
Expand Down Expand Up @@ -259,32 +260,6 @@ protected UserNaturalSca getJohnScaPayer(Boolean recreate, Boolean termsAccepted
return BaseTest.JOHN_SCA_PAYER;
}

protected UserNatural getNewDeclarativeJohn() throws Exception {
return getNewJohn(true);
}

protected UserNatural getNewJohn(boolean declarative) throws Exception {

Calendar c = Calendar.getInstance();
c.set(1975, 12, 21, 0, 0, 0);

UserNatural user = new UserNatural();
user.setFirstName("John");
user.setLastName("Doe");
user.setEmail("[email protected]");
user.setAddress(this.getNewAddress());
user.setBirthday(c.getTimeInMillis() / 1000);
user.setNationality(CountryIso.FR);
user.setCountryOfResidence(CountryIso.FR);
user.setOccupation("programmer");
user.setIncomeRange(3);
user.setUserCategory(UserCategory.OWNER);
if (declarative) {
user.setCapacity(NaturalUserCapacity.DECLARATIVE);
}
return (UserNatural) this.api.getUserApi().create(user);
}

protected UserLegal getMatrix(UserCategory userCategory) throws Exception {
switch (userCategory) {
case OWNER:
Expand Down Expand Up @@ -471,7 +446,7 @@ protected Wallet getJohnsWallet() throws Exception {
* @return Wallet instance loaded with 10k EUR.
*/
protected Wallet getJohnsWalletWithMoney() throws Exception {
return getJohnsWalletWithMoney(500);
return getJohnsWalletWithMoney(1000);
}

/**
Expand Down Expand Up @@ -1912,13 +1887,15 @@ protected Conversion createQuotedConversion() throws Exception {

Wallet debitedWallet = getJohnsWalletWithMoney();
ConversionQuote quote = createConversionQuote();
System.out.println("quote created");

CreateQuotedConversion quotedConversion = new CreateQuotedConversion();
quotedConversion.setQuoteId(quote.getId());
quotedConversion.setAuthorId(debitedWallet.getOwners().get(0));
quotedConversion.setCreditedWalletId(creditedWallet.getId());
quotedConversion.setDebitedWalletId(debitedWallet.getId());

System.out.println("creating conversion quote");
return this.api.getConversionsApi().createQuotedConversion(quotedConversion, null);
}

Expand All @@ -1937,12 +1914,48 @@ protected ConversionQuote createConversionQuote() throws Exception {

Money debitedFunds = new Money();
debitedFunds.setCurrency(CurrencyIso.EUR);
debitedFunds.setAmount(50);
debitedFunds.setAmount(1);
conversionQuote.setDebitedFunds(debitedFunds);

conversionQuote.setDuration(90);
conversionQuote.setDuration(300);
conversionQuote.setTag("Created using the Mangopay PHP SDK");

return this.api.getConversionsApi().createConversionQuote(conversionQuote, null);
}

protected Mandate createMandate(Boolean recreate) throws Exception {
if (BaseTest.MANDATE == null || recreate) {
Mandate mandatePost = new Mandate();
mandatePost.setBankAccountId(this.getJohnsAccount().getId());
mandatePost.setReturnUrl("http://test.test");
mandatePost.setCulture(CultureCode.EN);

BaseTest.MANDATE = this.api.getMandateApi().create(mandatePost);
}
return BaseTest.MANDATE;
}

public PayIn createDirectDebitDirect() throws Exception {
Wallet wallet = this.getJohnsWallet();
UserNatural user = this.getJohn();

Mandate mandate = this.createMandate(false);

PayIn payIn = new PayIn();
payIn.setAuthorId(user.getId());
payIn.setDebitedFunds(new Money());
payIn.getDebitedFunds().setAmount(10);
payIn.getDebitedFunds().setCurrency(CurrencyIso.EUR);
payIn.setFees(new Money());
payIn.getFees().setAmount(0);
payIn.getFees().setCurrency(CurrencyIso.EUR);
payIn.setCreditedWalletId(wallet.getId());
PayInPaymentDetailsDirectDebit paymentDetails = new PayInPaymentDetailsDirectDebit();
paymentDetails.setMandateId(mandate.getId());
payIn.setPaymentDetails(paymentDetails);
PayInExecutionDetailsDirect executionDetails = new PayInExecutionDetailsDirect();
payIn.setExecutionDetails(executionDetails);

return this.api.getPayInApi().create(payIn);
}
}
4 changes: 3 additions & 1 deletion src/test/java/com/mangopay/core/CardApiImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mangopay.core.enumerations.TransactionType;
import com.mangopay.entities.*;
import com.mangopay.entities.subentities.PayInPaymentDetailsCard;
import org.junit.Ignore;
import org.junit.Test;

import java.util.List;
Expand Down Expand Up @@ -32,6 +31,9 @@ public void getTransactions() throws Exception {
PayIn payIn = getNewPayInCardDirect();
Card card = api.getCardApi().get(((PayInPaymentDetailsCard) payIn.getPaymentDetails()).getCardId());
Pagination pagination = new Pagination(1, 1);

// wait 2 seconds for the transactions to be created in the API
Thread.sleep(2000);
List<Transaction> transactions = this.api.getCardApi().getTransactions(card.getId(), pagination, null);

assertNotNull("Card transactions came back null", transactions);
Expand Down
14 changes: 10 additions & 4 deletions src/test/java/com/mangopay/core/ClientApiImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mangopay.entities.*;
import com.mangopay.entities.subentities.PayOutPaymentDetailsBankWire;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.io.File;
Expand All @@ -26,24 +27,28 @@ public void getKycDocuments() throws Exception {
List<KycDocument> result2 = null;

try {
result = this.api.getClientApi().getKycDocuments(null, null, null);
KycDocument kycDocument = this.getJohnsKycDocument();
FilterKycDocuments filter = new FilterKycDocuments();
filter.setAfterDate(kycDocument.getCreationDate() - 10);
filter.setBeforeDate(kycDocument.getCreationDate() + 10);

result = this.api.getClientApi().getKycDocuments(null, filter, null);
assertNotNull(result);
assertTrue(result.size() > 0);

Pagination pagination = new Pagination(1, 2);
Sorting sort = new Sorting();
sort.addField("CreationDate", SortDirection.asc);
result = this.api.getClientApi().getKycDocuments(pagination, null, sort);
result = this.api.getClientApi().getKycDocuments(pagination, filter, sort);
assertNotNull(result);
assertTrue(result.size() > 0);

sort = new Sorting();
sort.addField("CreationDate", SortDirection.desc);
result2 = this.api.getClientApi().getKycDocuments(pagination, null, sort);
result2 = this.api.getClientApi().getKycDocuments(pagination, filter, sort);
assertNotNull(result2);
assertTrue(result2.size() > 0);

assertTrue((result.get(0).getId() == null ? result2.get(0).getId() != null : !result.get(0).getId().equals(result2.get(0).getId())));
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
Expand Down Expand Up @@ -174,6 +179,7 @@ else if (creditWallets != null && creditWallets.size() > 0)
assertTrue(result.size() > 0);
}

@Ignore("Endpoint removed")
@Test
public void getTransactions() {
List<Transaction> result = null;
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/com/mangopay/core/KycDocumentApiImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import java.util.List;

import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* KycDocumentApiImpl test methods
Expand All @@ -15,25 +16,27 @@ public class KycDocumentApiImplTest extends BaseTest {

@Test
public void getKycDocuments() throws Exception {
KycDocument kycDocument = this.getJohnsKycDocument();
FilterKycDocuments filter = new FilterKycDocuments();
filter.setAfterDate(kycDocument.getCreationDate() - 10);
filter.setBeforeDate(kycDocument.getCreationDate() + 10);

List<KycDocument> result = this.api.getKycDocumentApi().getAll(null, null);
List<KycDocument> result = this.api.getKycDocumentApi().getAll(null, filter, null);
assertNotNull(result);
assertTrue(result.size() > 0);

Pagination pagination = new Pagination(1, 2);
Sorting sort = new Sorting();
sort.addField("CreationDate", SortDirection.asc);
result = this.api.getKycDocumentApi().getAll(pagination, sort);
result = this.api.getKycDocumentApi().getAll(pagination, filter, sort);
assertNotNull(result);
assertTrue(result.size() > 0);

sort = new Sorting();
sort.addField("CreationDate", SortDirection.desc);
List<KycDocument> result2 = this.api.getKycDocumentApi().getAll(pagination, sort);
List<KycDocument> result2 = this.api.getKycDocumentApi().getAll(pagination, filter, sort);
assertNotNull(result2);
assertTrue(result2.size() > 0);

assertFalse(result.get(0).getId().equals(result2.get(0).getId()));
}

@Test
Expand Down
Loading

0 comments on commit abf4a25

Please sign in to comment.