Skip to content

Commit abf4a25

Browse files
authored
Merge pull request #349 from Mangopay/bugfix/fix-tests
Tests updates and fixes
2 parents 4d38a34 + b20bccd commit abf4a25

13 files changed

+150
-95
lines changed

src/main/java/com/mangopay/MangoPayApi.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public MangoPayApi() {
5353
setRegulatoryApi(new RegulatoryApiImpl(this));
5454
setDepositApi(new DepositApiImpl(this));
5555
setVirtualAccountApi(new VirtualAccountApiImpl(this));
56-
setConversionsApi(new ConversionsApiImpl(this));
56+
setConversionsApi(new ConversionsApiImpl(this, gsonBuilder));
5757
setGson(gsonBuilder.create());
5858
}
5959

src/main/java/com/mangopay/core/APIs/KycDocumentApi.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mangopay.core.APIs;
22

33
import com.mangopay.core.DocumentPageConsult;
4+
import com.mangopay.core.FilterKycDocuments;
45
import com.mangopay.core.Pagination;
56
import com.mangopay.core.Sorting;
67
import com.mangopay.entities.KycDocument;
@@ -20,6 +21,8 @@ public interface KycDocumentApi {
2021
*/
2122
List<KycDocument> getAll(Pagination pagination, Sorting sorting) throws Exception;
2223

24+
List<KycDocument> getAll(Pagination pagination, FilterKycDocuments filter, Sorting sorting) throws Exception;
25+
2326
/**
2427
* Gets KYC document.
2528
* @param kycDocumentId KYC document identifier.

src/main/java/com/mangopay/core/APIs/implementation/ConversionsApiImpl.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.mangopay.core.APIs.implementation;
22

3+
import com.google.gson.GsonBuilder;
34
import com.mangopay.MangoPayApi;
45
import com.mangopay.core.APIs.ApiBase;
56
import com.mangopay.core.APIs.ConversionsApi;
7+
import com.mangopay.core.serializer.CreateConversionQuoteSerializer;
68
import com.mangopay.entities.*;
79

810
public class ConversionsApiImpl extends ApiBase implements ConversionsApi {
@@ -12,8 +14,9 @@ public class ConversionsApiImpl extends ApiBase implements ConversionsApi {
1214
*
1315
* @param root Root/parent instance that holds the OAuthToken and Configuration instance.
1416
*/
15-
public ConversionsApiImpl(MangoPayApi root) {
17+
public ConversionsApiImpl(MangoPayApi root, GsonBuilder gsonBuilder) {
1618
super(root);
19+
gsonBuilder.registerTypeAdapter(CreateConversionQuote.class, new CreateConversionQuoteSerializer());
1720
}
1821

1922
@Override

src/main/java/com/mangopay/core/APIs/implementation/KycDocumentApiImpl.java

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mangopay.core.APIs.ApiBase;
55
import com.mangopay.core.APIs.KycDocumentApi;
66
import com.mangopay.core.DocumentPageConsult;
7+
import com.mangopay.core.FilterKycDocuments;
78
import com.mangopay.core.Pagination;
89
import com.mangopay.core.Sorting;
910
import com.mangopay.entities.KycDocument;
@@ -27,6 +28,11 @@ public KycDocumentApiImpl(MangoPayApi root) {
2728
public List<KycDocument> getAll(Pagination pagination, Sorting sorting) throws Exception {
2829
return this.getList(KycDocument[].class, KycDocument.class, "kyc_documents_all", pagination, sorting);
2930
}
31+
32+
@Override
33+
public List<KycDocument> getAll(Pagination pagination, FilterKycDocuments filter, Sorting sorting) throws Exception {
34+
return this.getList(KycDocument[].class, KycDocument.class, "kyc_documents_all", pagination, null, null, filter.getValues(), sorting);
35+
}
3036

3137
@Override
3238
public KycDocument getKycDocument(String kycDocumentId) throws Exception {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.mangopay.core.serializer;
2+
3+
import com.google.gson.*;
4+
import com.mangopay.core.Money;
5+
import com.mangopay.entities.CreateConversionQuote;
6+
7+
import java.lang.reflect.Type;
8+
9+
public class CreateConversionQuoteSerializer implements JsonSerializer<CreateConversionQuote> {
10+
11+
/**
12+
* For creating a Conversion Quote, the Money.Amount fields needs to be sent as NULL in some cases
13+
*/
14+
@Override
15+
public JsonElement serialize(CreateConversionQuote src, Type typeOfSrc, JsonSerializationContext context) {
16+
JsonObject object = new Gson().toJsonTree(src, typeOfSrc).getAsJsonObject();
17+
18+
Money debitedFunds = src.getDebitedFunds();
19+
Money creditedFunds = src.getCreditedFunds();
20+
21+
if (debitedFunds != null && creditedFunds != null) {
22+
// as per API docs: the DebitedFunds.Amount is required if CreditedFunds.Amount is null
23+
if (debitedFunds.getAmount() == 0 && creditedFunds.getAmount() != 0) {
24+
JsonObject debitedFundsObj = object.getAsJsonObject("DebitedFunds");
25+
debitedFundsObj.addProperty("Amount", (String) null);
26+
object.add("DebitedFunds", debitedFundsObj);
27+
}
28+
29+
// as per API docs: the CreditedFunds.Amount is required if DebitedFunds.Amount is null
30+
if (debitedFunds.getAmount() != 0 && creditedFunds.getAmount() == 0) {
31+
JsonObject creditedFundsObj = object.getAsJsonObject("CreditedFunds");
32+
creditedFundsObj.addProperty("Amount", (String) null);
33+
object.add("CreditedFunds", creditedFundsObj);
34+
}
35+
}
36+
37+
return object;
38+
}
39+
}

src/test/java/com/mangopay/core/BaseTest.java

+42-29
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public abstract class BaseTest {
4747
private static UboDeclaration UBO_DECLARATION;
4848
private static PayInTemplateURLOptions PAYIN_TEMPLATE_URL_OPTIONS;
4949
private static VirtualAccount JOHNS_VIRTUAL_ACCOUNT;
50+
private static Mandate MANDATE;
5051

5152
public BaseTest() {
5253
this.api = buildNewMangoPayApi();
@@ -259,32 +260,6 @@ protected UserNaturalSca getJohnScaPayer(Boolean recreate, Boolean termsAccepted
259260
return BaseTest.JOHN_SCA_PAYER;
260261
}
261262

262-
protected UserNatural getNewDeclarativeJohn() throws Exception {
263-
return getNewJohn(true);
264-
}
265-
266-
protected UserNatural getNewJohn(boolean declarative) throws Exception {
267-
268-
Calendar c = Calendar.getInstance();
269-
c.set(1975, 12, 21, 0, 0, 0);
270-
271-
UserNatural user = new UserNatural();
272-
user.setFirstName("John");
273-
user.setLastName("Doe");
274-
user.setEmail("john.doe@sample.org");
275-
user.setAddress(this.getNewAddress());
276-
user.setBirthday(c.getTimeInMillis() / 1000);
277-
user.setNationality(CountryIso.FR);
278-
user.setCountryOfResidence(CountryIso.FR);
279-
user.setOccupation("programmer");
280-
user.setIncomeRange(3);
281-
user.setUserCategory(UserCategory.OWNER);
282-
if (declarative) {
283-
user.setCapacity(NaturalUserCapacity.DECLARATIVE);
284-
}
285-
return (UserNatural) this.api.getUserApi().create(user);
286-
}
287-
288263
protected UserLegal getMatrix(UserCategory userCategory) throws Exception {
289264
switch (userCategory) {
290265
case OWNER:
@@ -471,7 +446,7 @@ protected Wallet getJohnsWallet() throws Exception {
471446
* @return Wallet instance loaded with 10k EUR.
472447
*/
473448
protected Wallet getJohnsWalletWithMoney() throws Exception {
474-
return getJohnsWalletWithMoney(500);
449+
return getJohnsWalletWithMoney(1000);
475450
}
476451

477452
/**
@@ -1912,13 +1887,15 @@ protected Conversion createQuotedConversion() throws Exception {
19121887

19131888
Wallet debitedWallet = getJohnsWalletWithMoney();
19141889
ConversionQuote quote = createConversionQuote();
1890+
System.out.println("quote created");
19151891

19161892
CreateQuotedConversion quotedConversion = new CreateQuotedConversion();
19171893
quotedConversion.setQuoteId(quote.getId());
19181894
quotedConversion.setAuthorId(debitedWallet.getOwners().get(0));
19191895
quotedConversion.setCreditedWalletId(creditedWallet.getId());
19201896
quotedConversion.setDebitedWalletId(debitedWallet.getId());
19211897

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

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

19381915
Money debitedFunds = new Money();
19391916
debitedFunds.setCurrency(CurrencyIso.EUR);
1940-
debitedFunds.setAmount(50);
1917+
debitedFunds.setAmount(1);
19411918
conversionQuote.setDebitedFunds(debitedFunds);
19421919

1943-
conversionQuote.setDuration(90);
1920+
conversionQuote.setDuration(300);
19441921
conversionQuote.setTag("Created using the Mangopay PHP SDK");
19451922

19461923
return this.api.getConversionsApi().createConversionQuote(conversionQuote, null);
19471924
}
1925+
1926+
protected Mandate createMandate(Boolean recreate) throws Exception {
1927+
if (BaseTest.MANDATE == null || recreate) {
1928+
Mandate mandatePost = new Mandate();
1929+
mandatePost.setBankAccountId(this.getJohnsAccount().getId());
1930+
mandatePost.setReturnUrl("http://test.test");
1931+
mandatePost.setCulture(CultureCode.EN);
1932+
1933+
BaseTest.MANDATE = this.api.getMandateApi().create(mandatePost);
1934+
}
1935+
return BaseTest.MANDATE;
1936+
}
1937+
1938+
public PayIn createDirectDebitDirect() throws Exception {
1939+
Wallet wallet = this.getJohnsWallet();
1940+
UserNatural user = this.getJohn();
1941+
1942+
Mandate mandate = this.createMandate(false);
1943+
1944+
PayIn payIn = new PayIn();
1945+
payIn.setAuthorId(user.getId());
1946+
payIn.setDebitedFunds(new Money());
1947+
payIn.getDebitedFunds().setAmount(10);
1948+
payIn.getDebitedFunds().setCurrency(CurrencyIso.EUR);
1949+
payIn.setFees(new Money());
1950+
payIn.getFees().setAmount(0);
1951+
payIn.getFees().setCurrency(CurrencyIso.EUR);
1952+
payIn.setCreditedWalletId(wallet.getId());
1953+
PayInPaymentDetailsDirectDebit paymentDetails = new PayInPaymentDetailsDirectDebit();
1954+
paymentDetails.setMandateId(mandate.getId());
1955+
payIn.setPaymentDetails(paymentDetails);
1956+
PayInExecutionDetailsDirect executionDetails = new PayInExecutionDetailsDirect();
1957+
payIn.setExecutionDetails(executionDetails);
1958+
1959+
return this.api.getPayInApi().create(payIn);
1960+
}
19481961
}

src/test/java/com/mangopay/core/CardApiImplTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.mangopay.core.enumerations.TransactionType;
55
import com.mangopay.entities.*;
66
import com.mangopay.entities.subentities.PayInPaymentDetailsCard;
7-
import org.junit.Ignore;
87
import org.junit.Test;
98

109
import java.util.List;
@@ -32,6 +31,9 @@ public void getTransactions() throws Exception {
3231
PayIn payIn = getNewPayInCardDirect();
3332
Card card = api.getCardApi().get(((PayInPaymentDetailsCard) payIn.getPaymentDetails()).getCardId());
3433
Pagination pagination = new Pagination(1, 1);
34+
35+
// wait 2 seconds for the transactions to be created in the API
36+
Thread.sleep(2000);
3537
List<Transaction> transactions = this.api.getCardApi().getTransactions(card.getId(), pagination, null);
3638

3739
assertNotNull("Card transactions came back null", transactions);

src/test/java/com/mangopay/core/ClientApiImplTest.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mangopay.entities.*;
55
import com.mangopay.entities.subentities.PayOutPaymentDetailsBankWire;
66
import org.junit.Assert;
7+
import org.junit.Ignore;
78
import org.junit.Test;
89

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

2829
try {
29-
result = this.api.getClientApi().getKycDocuments(null, null, null);
30+
KycDocument kycDocument = this.getJohnsKycDocument();
31+
FilterKycDocuments filter = new FilterKycDocuments();
32+
filter.setAfterDate(kycDocument.getCreationDate() - 10);
33+
filter.setBeforeDate(kycDocument.getCreationDate() + 10);
34+
35+
result = this.api.getClientApi().getKycDocuments(null, filter, null);
3036
assertNotNull(result);
3137
assertTrue(result.size() > 0);
3238

3339
Pagination pagination = new Pagination(1, 2);
3440
Sorting sort = new Sorting();
3541
sort.addField("CreationDate", SortDirection.asc);
36-
result = this.api.getClientApi().getKycDocuments(pagination, null, sort);
42+
result = this.api.getClientApi().getKycDocuments(pagination, filter, sort);
3743
assertNotNull(result);
3844
assertTrue(result.size() > 0);
3945

4046
sort = new Sorting();
4147
sort.addField("CreationDate", SortDirection.desc);
42-
result2 = this.api.getClientApi().getKycDocuments(pagination, null, sort);
48+
result2 = this.api.getClientApi().getKycDocuments(pagination, filter, sort);
4349
assertNotNull(result2);
4450
assertTrue(result2.size() > 0);
4551

46-
assertTrue((result.get(0).getId() == null ? result2.get(0).getId() != null : !result.get(0).getId().equals(result2.get(0).getId())));
4752
} catch (Exception ex) {
4853
Assert.fail(ex.getMessage());
4954
}
@@ -174,6 +179,7 @@ else if (creditWallets != null && creditWallets.size() > 0)
174179
assertTrue(result.size() > 0);
175180
}
176181

182+
@Ignore("Endpoint removed")
177183
@Test
178184
public void getTransactions() {
179185
List<Transaction> result = null;

src/test/java/com/mangopay/core/KycDocumentApiImplTest.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import java.util.List;
88

9-
import static org.junit.Assert.*;
9+
import static org.junit.Assert.assertNotNull;
10+
import static org.junit.Assert.assertTrue;
1011

1112
/**
1213
* KycDocumentApiImpl test methods
@@ -15,25 +16,27 @@ public class KycDocumentApiImplTest extends BaseTest {
1516

1617
@Test
1718
public void getKycDocuments() throws Exception {
19+
KycDocument kycDocument = this.getJohnsKycDocument();
20+
FilterKycDocuments filter = new FilterKycDocuments();
21+
filter.setAfterDate(kycDocument.getCreationDate() - 10);
22+
filter.setBeforeDate(kycDocument.getCreationDate() + 10);
1823

19-
List<KycDocument> result = this.api.getKycDocumentApi().getAll(null, null);
24+
List<KycDocument> result = this.api.getKycDocumentApi().getAll(null, filter, null);
2025
assertNotNull(result);
2126
assertTrue(result.size() > 0);
2227

2328
Pagination pagination = new Pagination(1, 2);
2429
Sorting sort = new Sorting();
2530
sort.addField("CreationDate", SortDirection.asc);
26-
result = this.api.getKycDocumentApi().getAll(pagination, sort);
31+
result = this.api.getKycDocumentApi().getAll(pagination, filter, sort);
2732
assertNotNull(result);
2833
assertTrue(result.size() > 0);
2934

3035
sort = new Sorting();
3136
sort.addField("CreationDate", SortDirection.desc);
32-
List<KycDocument> result2 = this.api.getKycDocumentApi().getAll(pagination, sort);
37+
List<KycDocument> result2 = this.api.getKycDocumentApi().getAll(pagination, filter, sort);
3338
assertNotNull(result2);
3439
assertTrue(result2.size() > 0);
35-
36-
assertFalse(result.get(0).getId().equals(result2.get(0).getId()));
3740
}
3841

3942
@Test

0 commit comments

Comments
 (0)