From f2f731a96c288efd71658fc1db7956ae3a5a99c3 Mon Sep 17 00:00:00 2001 From: Diogo Moreira Date: Fri, 15 Apr 2016 17:46:56 +0100 Subject: [PATCH 1/2] Fix logic exception for deposits --- .../integration/model/TransactionTest.java | 100 ++++++++++++++++++ .../uphold_android_sdk/model/Transaction.java | 50 ++++++++- 2 files changed, 146 insertions(+), 4 deletions(-) diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java index 3a4457b..d321461 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java @@ -40,6 +40,8 @@ public void cancelShouldReturnALogicExceptionIfCardIdIsMissing() throws Exceptio @Override public Promise call(UpholdRestAdapter adapter) { Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("destinationCardId", null); + put("originAccountId", null); put("originCardId", null); }}); @@ -55,6 +57,54 @@ public Promise call(UpholdRestAdapter adapter) { Assert.assertEquals(exception.getMessage(), "Origin CardId is missing from this transaction"); } + @Test + public void cancelShouldReturnALogicExceptionIfDestinationCardIdIsMissingForDeposits() throws Exception { + MockRestAdapter adapter = new MockRestAdapter<>("foobar", null, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter adapter) { + Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("destinationCardId", null); + put("transactionType", "deposit"); + }}); + + transaction.setUpholdRestAdapter(adapter); + + return transaction.cancel(); + } + }); + + Exception exception = adapter.getException(); + + Assert.assertEquals(exception.getClass().getName(), LogicException.class.getName()); + Assert.assertEquals(exception.getMessage(), "Destination CardId is missing from this deposit transaction"); + } + + @Test + public void cancelShouldReturnALogicExceptionIfOriginAccountIdIsMissingForDeposits() throws Exception { + MockRestAdapter adapter = new MockRestAdapter<>("foobar", null, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter adapter) { + Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("originAccountId", null); + put("transactionType", "deposit"); + }}); + + transaction.setUpholdRestAdapter(adapter); + + return transaction.cancel(); + } + }); + + Exception exception = adapter.getException(); + + Assert.assertEquals(exception.getClass().getName(), LogicException.class.getName()); + Assert.assertEquals(exception.getMessage(), "Origin AccountId is missing from this deposit transaction"); + } + @Test public void cancelShouldReturnTheLogicExceptionUncommitedTransaction() throws Exception { MockRestAdapter adapter = new MockRestAdapter<>("foobar", null, null); @@ -137,6 +187,8 @@ public void commitShouldReturnTheLogicExceptionCardIdIsMissing() throws Exceptio @Override public Promise call(UpholdRestAdapter adapter) { Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("destinationCardId", null); + put("originAccountId", null); put("originCardId", null); }}); @@ -152,6 +204,54 @@ public Promise call(UpholdRestAdapter adapter) { Assert.assertEquals(exception.getMessage(), "Origin CardId is missing from this transaction"); } + @Test + public void commitShouldReturnTheLogicExceptionIfDestinationCardIdIsMissingForDeposits() throws Exception { + MockRestAdapter adapter = new MockRestAdapter<>("foobar", null, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter adapter) { + Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("destinationCardId", null); + put("transactionType", "deposit"); + }}); + + transaction.setUpholdRestAdapter(adapter); + + return transaction.commit(new TransactionCommitRequest("foobar")); + } + }); + + Exception exception = adapter.getException(); + + Assert.assertEquals(exception.getClass().getName(), LogicException.class.getName()); + Assert.assertEquals(exception.getMessage(), "Destination CardId is missing from this deposit transaction"); + } + + @Test + public void commitShouldReturnTheLogicExceptionIfOriginAccountIdIsMissingForDeposits() throws Exception { + MockRestAdapter adapter = new MockRestAdapter<>("foobar", null, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter adapter) { + Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("originAccountId", null); + put("transactionType", "deposit"); + }}); + + transaction.setUpholdRestAdapter(adapter); + + return transaction.commit(new TransactionCommitRequest("foobar")); + } + }); + + Exception exception = adapter.getException(); + + Assert.assertEquals(exception.getClass().getName(), LogicException.class.getName()); + Assert.assertEquals(exception.getMessage(), "Origin AccountId is missing from this deposit transaction"); + } + @Test public void commitShouldReturnTheLogicExceptionTransactionCouldNotBeCommited() throws Exception { MockRestAdapter adapter = new MockRestAdapter<>("foobar", null, null); diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java index 7f4ce2f..bd59cec 100644 --- a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java @@ -72,6 +72,20 @@ public Transaction(String id, String createdAt, Denomination denomination, Desti this.type = type; } + /** + * Gets the card id path to commit the transaction. + * + * @return the card id path to commit the transaction. + */ + + private String getCardIdPath() { + if ("deposit".equalsIgnoreCase(this.getType())) { + return this.getDestination().getCardId(); + } + + return this.getOrigin().getCardId(); + } + /** * Cancel a transaction. * @@ -82,7 +96,21 @@ public Promise cancel() { RetrofitPromise promise = new RetrofitPromise<>(); UserCardService userCardService = this.getUpholdRestAdapter().create(UserCardService.class); - if (TextUtils.isEmpty(this.getOrigin().getCardId())) { + if ("deposit".equalsIgnoreCase(this.getType())) { + if (TextUtils.isEmpty(this.getOrigin().getAccountId())) { + promise.reject(new LogicException("Origin AccountId is missing from this deposit transaction")); + + return promise; + } + + if (TextUtils.isEmpty(this.getDestination().getCardId())) { + promise.reject(new LogicException("Destination CardId is missing from this deposit transaction")); + + return promise; + } + } + + if (TextUtils.isEmpty(this.getOrigin().getCardId()) && TextUtils.isEmpty(this.getDestination().getCardId())) { promise.reject(new LogicException("Origin CardId is missing from this transaction")); return promise; @@ -100,7 +128,7 @@ public Promise cancel() { return promise; } - userCardService.cancelTransaction(this.getOrigin().getCardId(), this.getId(), EmptyOutput.INSTANCE, promise); + userCardService.cancelTransaction(getCardIdPath(), this.getId(), EmptyOutput.INSTANCE, promise); return promise; } @@ -152,7 +180,21 @@ public Promise commit(String otp, TransactionCommitRequest transact RetrofitPromise promise = new RetrofitPromise<>(); UserCardService userCardService = this.getUpholdRestAdapter().create(UserCardService.class); - if (TextUtils.isEmpty(this.getOrigin().getCardId())) { + if ("deposit".equalsIgnoreCase(this.getType())) { + if (TextUtils.isEmpty(this.getOrigin().getAccountId())) { + promise.reject(new LogicException("Origin AccountId is missing from this deposit transaction")); + + return promise; + } + + if (TextUtils.isEmpty(this.getDestination().getCardId())) { + promise.reject(new LogicException("Destination CardId is missing from this deposit transaction")); + + return promise; + } + } + + if (TextUtils.isEmpty(this.getOrigin().getCardId()) && TextUtils.isEmpty(this.getDestination().getCardId())) { promise.reject(new LogicException("Origin CardId is missing from this transaction")); return promise; @@ -168,7 +210,7 @@ public Promise commit(String otp, TransactionCommitRequest transact transactionCommitRequest = new TransactionCommitRequest(null); } - userCardService.confirmTransaction(this.getOrigin().getCardId(), this.getId(), transactionCommitRequest, otp, promise); + userCardService.confirmTransaction(getCardIdPath(), this.getId(), transactionCommitRequest, otp, promise); return promise; } From 1572be83fa23f6ec33dcc5d4fc360450c607679e Mon Sep 17 00:00:00 2001 From: Diogo Moreira Date: Fri, 15 Apr 2016 18:01:48 +0100 Subject: [PATCH 2/2] Update transaction commit request model with security code --- .../integration/model/TransactionTest.java | 43 +++++++++++++++++++ .../service/UserCardServiceTest.java | 2 +- .../transaction/TransactionCommitRequest.java | 25 ++++++++++- .../TransactionCommitRequestTest.java | 15 ++++++- 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java index d321461..a727af6 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java @@ -361,6 +361,49 @@ public Promise call(UpholdRestAdapter adapter) { Assert.assertEquals(bodyOutput.toString(), "{\"message\":\"foobar\"}"); } + @Test + public void commitWithMessageAndSecurityCodeShouldReturnTheTransaction() throws Exception { + ByteArrayOutputStream bodyOutput = new ByteArrayOutputStream(); + String responseString = "{ \"id\": \"foobar\" }"; + + MockRestAdapter adapter = new MockRestAdapter<>("foobar", responseString, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter adapter) { + Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("transactionStatus", "pending"); + put("originCardId", "foo"); + put("transactionId", "bar"); + }}); + + transaction.setUpholdRestAdapter(adapter); + + return transaction.commit(new TransactionCommitRequest("foobar", "foo")); + } + }); + + Header otpHeader = null; + Request request = adapter.getRequest(); + Transaction transaction = adapter.getResult(); + + for (Header header : request.getHeaders()) { + if (header.getName().compareToIgnoreCase("X-Bitreserve-OTP") == 0) { + otpHeader = header; + + break; + } + } + + request.getBody().writeTo(bodyOutput); + + Assert.assertEquals(request.getUrl(), String.format("%s/v0/me/cards/foo/transactions/bar/commit", BuildConfig.API_SERVER_URL)); + Assert.assertEquals(request.getMethod(), "POST"); + Assert.assertNull(otpHeader); + Assert.assertEquals(transaction.getId(), "foobar"); + Assert.assertEquals(bodyOutput.toString(), "{\"message\":\"foobar\",\"securityCode\":\"foo\"}"); + } + @Test public void commitWithOTPShouldReturnTheTransaction() throws Exception { ByteArrayOutputStream bodyOutput = new ByteArrayOutputStream(); diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java index 3ed4110..1639bd5 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java @@ -72,7 +72,7 @@ public Promise call(UpholdRestAdapter upholdRestAdapter) { UserCardService userCardService = adapter.getRestAdapter().create(UserCardService.class); RetrofitPromise promise = new RetrofitPromise<>(); - userCardService.confirmTransaction("foo", "bar", new TransactionCommitRequest("message"), "otp", promise); + userCardService.confirmTransaction("foo", "bar", new TransactionCommitRequest("message", "securityCode"), "otp", promise); return promise; } diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java index 298b85f..37f56b2 100644 --- a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java @@ -9,17 +9,30 @@ public class TransactionCommitRequest implements Serializable { String message; + String securityCode; /** * Constructor. * - * @param message The transanction message. + * @param message The transaction message. */ public TransactionCommitRequest(String message) { this.message = message; } + /** + * Constructor. + * + * @param message The transaction message. + * @param securityCode The transaction security code. + */ + + public TransactionCommitRequest(String message, String securityCode) { + this.message = message; + this.securityCode = securityCode; + } + /** * Gets the transaction request message. * @@ -30,4 +43,14 @@ public String getMessage() { return message; } + /** + * Gets the transaction security code. + * + * @return the transaction security code + */ + + public String getSecurityCode() { + return securityCode; + } + } diff --git a/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java index ec71c81..59c4676 100644 --- a/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java +++ b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java @@ -1,9 +1,10 @@ package com.uphold.uphold_android_sdk.test.unit.model.transaction; +import com.uphold.uphold_android_sdk.model.transaction.TransactionCommitRequest; + import junit.framework.Assert; import org.apache.commons.lang3.SerializationUtils; -import com.uphold.uphold_android_sdk.model.transaction.TransactionCommitRequest; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -16,7 +17,7 @@ public class TransactionCommitRequestTest { @Test - public void shouldBeSerializable() { + public void shouldBeSerializableTransactionCommitRequest() { TransactionCommitRequest transactionCommitRequest = new TransactionCommitRequest("foobar"); byte[] serializedTransactionCommitRequestTest = SerializationUtils.serialize(transactionCommitRequest); TransactionCommitRequest deserializedTransactionCommitRequestTest = SerializationUtils.deserialize(serializedTransactionCommitRequestTest); @@ -24,4 +25,14 @@ public void shouldBeSerializable() { Assert.assertEquals(transactionCommitRequest.getMessage(), deserializedTransactionCommitRequestTest.getMessage()); } + @Test + public void shouldBeSerializableTransactionCommitRequestWithSecurityCode() { + TransactionCommitRequest transactionCommitRequest = new TransactionCommitRequest("foobar", "foo"); + byte[] serializedTransactionCommitRequestTest = SerializationUtils.serialize(transactionCommitRequest); + TransactionCommitRequest deserializedTransactionCommitRequestTest = SerializationUtils.deserialize(serializedTransactionCommitRequestTest); + + Assert.assertEquals(transactionCommitRequest.getMessage(), deserializedTransactionCommitRequestTest.getMessage()); + Assert.assertEquals(transactionCommitRequest.getSecurityCode(), deserializedTransactionCommitRequestTest.getSecurityCode()); + } + }