Skip to content

Commit

Permalink
Merge pull request #81 from uphold/bugfix/logic-exception-for-deposits
Browse files Browse the repository at this point in the history
Fix logic exception for transaction deposits
  • Loading branch information
ruipenso committed Apr 18, 2016
2 parents 46c9983 + 1572be8 commit a3f0c9f
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void cancelShouldReturnALogicExceptionIfCardIdIsMissing() throws Exceptio
@Override
public Promise<Transaction> call(UpholdRestAdapter adapter) {
Transaction transaction = Fixtures.loadTransaction(new HashMap<String, String>() {{
put("destinationCardId", null);
put("originAccountId", null);
put("originCardId", null);
}});

Expand All @@ -55,6 +57,54 @@ public Promise<Transaction> call(UpholdRestAdapter adapter) {
Assert.assertEquals(exception.getMessage(), "Origin CardId is missing from this transaction");
}

@Test
public void cancelShouldReturnALogicExceptionIfDestinationCardIdIsMissingForDeposits() throws Exception {
MockRestAdapter<Transaction> adapter = new MockRestAdapter<>("foobar", null, null);

adapter.request(new RepromiseFunction<UpholdRestAdapter, Transaction>() {
@Override
public Promise<Transaction> call(UpholdRestAdapter adapter) {
Transaction transaction = Fixtures.loadTransaction(new HashMap<String, String>() {{
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<Transaction> adapter = new MockRestAdapter<>("foobar", null, null);

adapter.request(new RepromiseFunction<UpholdRestAdapter, Transaction>() {
@Override
public Promise<Transaction> call(UpholdRestAdapter adapter) {
Transaction transaction = Fixtures.loadTransaction(new HashMap<String, String>() {{
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<Transaction> adapter = new MockRestAdapter<>("foobar", null, null);
Expand Down Expand Up @@ -137,6 +187,8 @@ public void commitShouldReturnTheLogicExceptionCardIdIsMissing() throws Exceptio
@Override
public Promise<Transaction> call(UpholdRestAdapter adapter) {
Transaction transaction = Fixtures.loadTransaction(new HashMap<String, String>() {{
put("destinationCardId", null);
put("originAccountId", null);
put("originCardId", null);
}});

Expand All @@ -152,6 +204,54 @@ public Promise<Transaction> call(UpholdRestAdapter adapter) {
Assert.assertEquals(exception.getMessage(), "Origin CardId is missing from this transaction");
}

@Test
public void commitShouldReturnTheLogicExceptionIfDestinationCardIdIsMissingForDeposits() throws Exception {
MockRestAdapter<Transaction> adapter = new MockRestAdapter<>("foobar", null, null);

adapter.request(new RepromiseFunction<UpholdRestAdapter, Transaction>() {
@Override
public Promise<Transaction> call(UpholdRestAdapter adapter) {
Transaction transaction = Fixtures.loadTransaction(new HashMap<String, String>() {{
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<Transaction> adapter = new MockRestAdapter<>("foobar", null, null);

adapter.request(new RepromiseFunction<UpholdRestAdapter, Transaction>() {
@Override
public Promise<Transaction> call(UpholdRestAdapter adapter) {
Transaction transaction = Fixtures.loadTransaction(new HashMap<String, String>() {{
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<Transaction> adapter = new MockRestAdapter<>("foobar", null, null);
Expand Down Expand Up @@ -261,6 +361,49 @@ public Promise<Transaction> call(UpholdRestAdapter adapter) {
Assert.assertEquals(bodyOutput.toString(), "{\"message\":\"foobar\"}");
}

@Test
public void commitWithMessageAndSecurityCodeShouldReturnTheTransaction() throws Exception {
ByteArrayOutputStream bodyOutput = new ByteArrayOutputStream();
String responseString = "{ \"id\": \"foobar\" }";

MockRestAdapter<Transaction> adapter = new MockRestAdapter<>("foobar", responseString, null);

adapter.request(new RepromiseFunction<UpholdRestAdapter, Transaction>() {
@Override
public Promise<Transaction> call(UpholdRestAdapter adapter) {
Transaction transaction = Fixtures.loadTransaction(new HashMap<String, String>() {{
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Promise<Transaction> call(UpholdRestAdapter upholdRestAdapter) {
UserCardService userCardService = adapter.getRestAdapter().create(UserCardService.class);
RetrofitPromise<Transaction> promise = new RetrofitPromise<>();

userCardService.confirmTransaction("foo", "bar", new TransactionCommitRequest("message"), "otp", promise);
userCardService.confirmTransaction("foo", "bar", new TransactionCommitRequest("message", "securityCode"), "otp", promise);

return promise;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -82,7 +96,21 @@ public Promise<Transaction> cancel() {
RetrofitPromise<Transaction> 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;
Expand All @@ -100,7 +128,7 @@ public Promise<Transaction> cancel() {
return promise;
}

userCardService.cancelTransaction(this.getOrigin().getCardId(), this.getId(), EmptyOutput.INSTANCE, promise);
userCardService.cancelTransaction(getCardIdPath(), this.getId(), EmptyOutput.INSTANCE, promise);

return promise;
}
Expand Down Expand Up @@ -152,7 +180,21 @@ public Promise<Transaction> commit(String otp, TransactionCommitRequest transact
RetrofitPromise<Transaction> 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;
Expand All @@ -168,7 +210,7 @@ public Promise<Transaction> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -30,4 +43,14 @@ public String getMessage() {
return message;
}

/**
* Gets the transaction security code.
*
* @return the transaction security code
*/

public String getSecurityCode() {
return securityCode;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,12 +17,22 @@
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);

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());
}

}

0 comments on commit a3f0c9f

Please sign in to comment.