Skip to content

Commit

Permalink
added new integration tests for cash transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
agrsachin81 committed Sep 27, 2019
1 parent dc91555 commit 891359d
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package home.test.restapi.example.moneytransfer;

import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.DEBIT_FAILED;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.INIT_BALANCE;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.STATUS;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.STATUS_ERROR;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.TRANSFER_TYPE_DEBIT;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.createAccountRetJson;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.createAccountReturnAccountId;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.getAccountStatus;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.transferAndRetJson;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.verifyAccountDetails;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.verifyTransaction;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.verifyTransactionFailed;
import static home.test.restapi.example.moneytransfer.MoneyTransferAPITestHelper.transferCashAndRetJson;
import static home.test.restapi.testtool.TestRekuestHelper.request;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.After;
import org.junit.AfterClass;
Expand All @@ -27,26 +38,13 @@
*/
public class ITMoneyTransferAPITest {

private static final String DEBIT_FAILED = "DEBIT_FAILED";
private static final String AMOUNT_TRANSACTED = "amount";
private static final String CP_ACCOUNT_ID = "cpAccountId";
private static final String TRANSACTION_REKUEST_ID = "transactionRekuestId";
private static final String NAME_PREFIX = "john";
private static final String NAME = "name";
private static final String BALANCE = "balance";
private static final String ACCOUNT_ID = "accountId";
private static final String STATUS = "status";
private static final String MOBILE_NUMBER = "mobileNumber";
private static final String TRAN_REK_ID_PREFIX = "TRANS_";

private static final int MOBILE_NUMBER_START = 1000000;

private static final double INIT_BALANCE = 300;


@BeforeClass
public static void setUpBeforeClass() throws Exception {
MoneyTransferAPI.main(null);

//just to make sure the server has been started completely
Thread.sleep(5000);
}

Expand Down Expand Up @@ -81,7 +79,7 @@ public void testAccountGetNonExisting() {
TestResponse response = request("GET", "/account/PA_9876", "");
assertEquals(200, response.status);
Map<String, Object> json = response.json();
assertEquals("Status is not correct", "ERROR", json.get(STATUS));
assertEquals("Status is not correct", STATUS_ERROR, json.get(STATUS));
}

@Ignore
Expand All @@ -90,10 +88,16 @@ public void testAccountDelete() {

}

@Ignore
@Test
public void testGetAccounts() {

TestResponse response = request("GET", "/account", "");
assertEquals(200, response.status);
Map<String, Object> json = response.json();
System.out.println("GETACCOUNTS");
json.forEach((key,value) -> {
System.out.println("---> KEY "+ key +" VALUE "+ value);
});
System.out.println("GETACCOUNTS END");
}

@Test
Expand All @@ -114,102 +118,16 @@ public void testTransactionFailedInsufficientFunds() {
verifyTransactionFailed(accountId, json, accountId2, INIT_BALANCE+0.1, DEBIT_FAILED);
}

@Ignore
@Test
public void testTransactionSuccessfulDebitCash() {
String accountId = createAccountReturnAccountId();

Map<String, Object> json = transferCashAndRetJson(accountId, 30, TRANSFER_TYPE_DEBIT );
verifyTransaction(accountId, json, null, 30);
}

@Ignore
@Test
public void testTransactionSuccessfulCreditCash() {

}

private Map<String,Object> transferAndRetJson(String origAccountId, double amount, String cpAccountId) {
int counter = transactionCreateCounter.incrementAndGet();

TestResponse res = request("POST", "/account/"+origAccountId+"/transact", "{amount:" + amount + ",cpAccountId:"+cpAccountId
+ ",transactionRekuestId:" +TRAN_REK_ID_PREFIX+ counter + "}");
Map<String, Object> json = res.json();
assertEquals(200, res.status);
return json;
}

private void verifyTransactionFailed(String origAccountId, Map<String, Object> json, String cpAccountId, double amount, String transactionStatus) {
int currentCounterValue = transactionCreateCounter.get();
assertEquals("Status is not correct", "ERROR", json.get(STATUS));
assertEquals("Transaction status is not correct", transactionStatus, json.get("transactionStatus"));
String accId = (String)json.get(ACCOUNT_ID);
assertNotNull(accId);

assertEquals("origAccId", origAccountId, accId);
assertEquals("TransactionRek Id match ", TRAN_REK_ID_PREFIX+currentCounterValue , json.get(TRANSACTION_REKUEST_ID));
assertEquals("cpAccId", cpAccountId, (String)json.get(CP_ACCOUNT_ID));

double origAccountBalance= getAccountBalance(origAccountId);
double cpAccountBalance = getAccountBalance(cpAccountId);
assertEquals("origAccountBalance ", INIT_BALANCE , origAccountBalance,0.001);
assertEquals("origAccountBalance ", INIT_BALANCE , cpAccountBalance,0.001);
}

private void verifyTransaction(String origAccountId, Map<String, Object> json, String cpAccountId, double amount) {
int currentCounterValue = transactionCreateCounter.get();
assertEquals("Status is not correct", "SUCCESS", json.get(STATUS));
assertEquals("Transaction status is not correct", "DONE", json.get("transactionStatus"));
String accId = (String)json.get(ACCOUNT_ID);
assertNotNull(accId);

assertEquals("origAccId does not match", origAccountId, accId);
assertEquals("TransactionRek Id do not match ", TRAN_REK_ID_PREFIX+currentCounterValue , json.get(TRANSACTION_REKUEST_ID));
assertEquals("cpAccId does not match ", cpAccountId, (String)json.get(CP_ACCOUNT_ID));

double origAccountBalance= getAccountBalance(origAccountId);
double cpAccountBalance = getAccountBalance(cpAccountId);
assertEquals("origAccountBalance ", INIT_BALANCE -amount, origAccountBalance,0.001);
assertEquals("origAccountBalance ", INIT_BALANCE +amount, cpAccountBalance,0.001);
}

private Map<String, Object> createAccountRetJson() {
int counter = accCreateCounter.incrementAndGet();
TestResponse res = request("POST", "/account", "{name:john" + counter + ",mobileNumber:"
+ String.valueOf(MOBILE_NUMBER_START + counter) + ",balance:" + INIT_BALANCE + "}");
Map<String, Object> json = res.json();
assertEquals(200, res.status);
return json;
}

private String createAccountReturnAccountId() {
Map<String, Object> json = createAccountRetJson();
return (String) json.get(ACCOUNT_ID);
}

private Map<String, Object> getAccountStatus(String accId){
TestResponse response = request("GET", "/account/" + accId, "");
System.out.println(response.body);
Map<String, Object> json = response.json();
assertEquals(200, response.status);
return json;
}

private double getAccountBalance(String accountId) {

Map<String, Object> json = getAccountStatus(accountId);

return (double)json.get(BALANCE);
}

AtomicInteger accCreateCounter = new AtomicInteger(0);
AtomicInteger transactionCreateCounter = new AtomicInteger(0);

private Map<String, Object> verifyAccountDetails(Map<String, Object> json) {
int currentCounterValue = accCreateCounter.get();
assertEquals(NAME_PREFIX + currentCounterValue, json.get(NAME));

assertEquals(String.valueOf(MOBILE_NUMBER_START + currentCounterValue), json.get(MOBILE_NUMBER));
assertEquals("Status is not correct", "SUCCESS", json.get(STATUS));
assertNotNull(json.get(ACCOUNT_ID));
assertEquals("balance does not match", INIT_BALANCE, (double) json.get(BALANCE), 0.0001);
return json;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*******************************************************************************
* Copyright (c) quickfixengine.org All rights reserved.
*
* This file is part of the QuickFIX FIX Engine
*
* This file may be distributed under the terms of the quickfixengine.org
* license as defined by quickfixengine.org and appearing in the file
* LICENSE included in the packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* See http://www.quickfixengine.org/LICENSE for licensing information.
*
* Contact [email protected] if any conditions of this licensing
* are not clear to you.
******************************************************************************/

package home.test.restapi.example.moneytransfer;

import static home.test.restapi.testtool.TestRekuestHelper.request;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import home.test.restapi.testtool.TestResponse;

public class MoneyTransferAPITestHelper {
public static final String TRANSFER_TYPE_DEBIT = "debit";
public static final String TRANSFER_TYPE_CREDIT = "credit";
private static AtomicInteger accCreateCounter = new AtomicInteger(0);
private static AtomicInteger transactionCreateCounter = new AtomicInteger(0);

public static final String STATUS_SUCCESS = "SUCCESS";
public static final String STATUS_ERROR = "ERROR";
public static final String DEBIT_FAILED = "DEBIT_FAILED";
public static final String AMOUNT_TRANSACTED = "amount";
public static final String CP_ACCOUNT_ID = "cpAccountId";
public static final String TRANSACTION_REKUEST_ID = "transactionRekuestId";
public static final String NAME_PREFIX = "john";
public static final String NAME = "name";
public static final String BALANCE = "balance";
public static final String ACCOUNT_ID = "accountId";
public static final String STATUS = "status";
public static final String MOBILE_NUMBER = "mobileNumber";
public static final String TRAN_REK_ID_PREFIX = "TRANS_";

public static final int MOBILE_NUMBER_START = 1000000;

public static final double INIT_BALANCE = 300;

public static void verifyTransactionFailed(String origAccountId, Map<String, Object> json, String cpAccountId,
double amount, String transactionStatus) {
int currentCounterValue = transactionCreateCounter.get();
assertEquals("Status is not correct", STATUS_ERROR, json.get(STATUS));
assertEquals("Transaction status is not correct", transactionStatus, json.get("transactionStatus"));
String accId = (String) json.get(ACCOUNT_ID);
assertNotNull(accId);

assertEquals("Originating account Id do not match", origAccountId, accId);
assertEquals("TransactionRek Id does not match ", TRAN_REK_ID_PREFIX + currentCounterValue,
json.get(TRANSACTION_REKUEST_ID));
assertEquals("counterparty AccId do not match ", cpAccountId, (String) json.get(CP_ACCOUNT_ID));

double origAccountBalance = getAccountBalance(origAccountId);
double cpAccountBalance = getAccountBalance(cpAccountId);
assertEquals("origAccountBalance ", INIT_BALANCE, origAccountBalance, 0.001);
assertEquals("origAccountBalance ", INIT_BALANCE, cpAccountBalance, 0.001);
}

public static Map<String, Object> verifyAccountDetails(Map<String, Object> json) {
int currentCounterValue = accCreateCounter.get();
assertEquals(NAME_PREFIX + currentCounterValue, json.get(NAME));

assertEquals(String.valueOf(MOBILE_NUMBER_START + currentCounterValue), json.get(MOBILE_NUMBER));
assertEquals("Status is not correct", STATUS_SUCCESS, json.get(STATUS));
assertNotNull(json.get(ACCOUNT_ID));
assertEquals("balance does not match", INIT_BALANCE, (double) json.get(BALANCE), 0.0001);
return json;
}

public static Map<String, Object> createAccountRetJson() {
int counter = accCreateCounter.incrementAndGet();
TestResponse res = request("POST", "/account", "{name:john" + counter + ",mobileNumber:"
+ String.valueOf(MOBILE_NUMBER_START + counter) + ",balance:" + INIT_BALANCE + "}");
Map<String, Object> json = res.json();
assertEquals(200, res.status);
return json;
}

public static String createAccountReturnAccountId() {
Map<String, Object> json = createAccountRetJson();
return (String) json.get(ACCOUNT_ID);
}

public static Map<String, Object> getAccountStatus(String accId) {
TestResponse response = request("GET", "/account/" + accId, "");
System.out.println(response.body);
Map<String, Object> json = response.json();
assertEquals(200, response.status);
return json;
}

public static double getAccountBalance(String accountId) {

Map<String, Object> json = getAccountStatus(accountId);

return (double) json.get(BALANCE);
}

public static Map<String, Object> transferAndRetJson(String origAccountId, double amount, String cpAccountId) {
int counter = transactionCreateCounter.incrementAndGet();

TestResponse res = request("POST", "/account/" + origAccountId + "/transact", "{amount:" + amount
+ ",cpAccountId:" + cpAccountId + ",transactionRekuestId:" + TRAN_REK_ID_PREFIX + counter + "}");
Map<String, Object> json = res.json();
assertEquals(200, res.status);
return json;
}

public static Map<String, Object> transferCashAndRetJson(String origAccountId, double amount, String transferType) {
int counter = transactionCreateCounter.incrementAndGet();

TestResponse res = request("POST", "/account/" + origAccountId + "/transact", "{amount:" + amount
+ ",transactionRekuestId:" + TRAN_REK_ID_PREFIX + counter + ",transactionType:" + transferType + "}");

Map<String, Object> json = res.json();
assertEquals(200, res.status);
return json;
}

public static void verifyTransaction(String origAccountId, Map<String, Object> json, String cpAccountId,
double amount) {
int currentCounterValue = transactionCreateCounter.get();
assertEquals("Status is not correct", STATUS_SUCCESS, json.get(STATUS));
assertEquals("Transaction status is not correct", "DONE", json.get("transactionStatus"));
String accId = (String) json.get(ACCOUNT_ID);
assertNotNull(accId);

assertEquals("origAccId does not match", origAccountId, accId);
assertEquals("TransactionRek Id do not match ", TRAN_REK_ID_PREFIX + currentCounterValue,
json.get(TRANSACTION_REKUEST_ID));

double origAccountBalance = getAccountBalance(origAccountId);

assertEquals("origAccountBalance ", INIT_BALANCE - amount, origAccountBalance, 0.001);

if (cpAccountId != null) {
double cpAccountBalance = getAccountBalance(cpAccountId);
assertEquals("cpAccId does not match ", cpAccountId, (String) json.get(CP_ACCOUNT_ID));
assertEquals("origAccountBalance ", INIT_BALANCE + amount, cpAccountBalance, 0.001);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static TestResponse request(String method, String path, String json) {
connection.setRequestProperty("charset", "utf-8");
byte[] postData = json.getBytes(StandardCharsets.UTF_8);
connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
//connection.connect();
// connection.connect();
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(postData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Optional;

import org.junit.BeforeClass;
import org.junit.Test;

Expand Down Expand Up @@ -48,4 +50,18 @@ public void transactConverionTest() {
String expected = "abcd".intern();
assertEquals("transactionId not parsed", expected, transaction.getTransactionRekuestId());
}

@Test
public void transactConverionTestDebitCash() {
String rek = "{\"amount\":100.0,\"transactionType\":\"debit\",\"transactionRekuestId\":\"abcd\"}";

TransactionRekuestImpl transaction = gson.fromJson(rek, TransactionRekuestImpl.class);

assertNotNull("JSON TO OBJ CONVERSION FAILED |" + rek + "|", transaction);
assertEquals("amount is not parsed", 100.0, transaction.getAmount(), 0.0000000001);
assertEquals("CounterParty is not parsed Correctly", Optional.empty(), transaction.getCpAccountId());
assertEquals("default type is not correct", TransactionType.DEBIT_CASH, transaction.getTransactionType());
String expected = "abcd".intern();
assertEquals("transactionId not parsed", expected, transaction.getTransactionRekuestId());
}
}

0 comments on commit 891359d

Please sign in to comment.