Skip to content

Commit

Permalink
Fix logic exception for deposits
Browse files Browse the repository at this point in the history
  • Loading branch information
d-moreira committed Apr 18, 2016
1 parent 6e67bfa commit f2f731a
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 4 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
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

1 comment on commit f2f731a

@alejagapatrick
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.