@SuppressWarnings("unchecked")
@Test
public void testSavingsAccountCharges() {
final ResponseSpecification erroResponseSpec = new ResponseSpecBuilder().build();
this.savingsAccountHelper = new SavingsAccountHelper(this.requestSpec, this.responseSpec);
final SavingsAccountHelper validationErrorHelper = new SavingsAccountHelper(this.requestSpec, erroResponseSpec);
final Integer clientID = ClientHelper.createClient(this.requestSpec, this.responseSpec);
Assert.assertNotNull(clientID);
final String minBalanceForInterestCalculation = null;
final String minRequiredBalance = null;
final String enforceMinRequiredBalance = "false";
final boolean allowOverdraft = false;
final Integer savingsProductID = createSavingsProduct(this.requestSpec, this.responseSpec, MINIMUM_OPENING_BALANCE,
minBalanceForInterestCalculation, minRequiredBalance, enforceMinRequiredBalance, allowOverdraft);
Assert.assertNotNull(savingsProductID);
Assert.assertNotNull(savingsProductID);
final Integer savingsId = this.savingsAccountHelper.applyForSavingsApplication(clientID, savingsProductID, ACCOUNT_TYPE_INDIVIDUAL);
HashMap savingsStatusHashMap = SavingsStatusChecker.getStatusOfSavings(this.requestSpec, this.responseSpec, savingsId);
SavingsStatusChecker.verifySavingsIsPending(savingsStatusHashMap);
final Integer withdrawalChargeId = ChargesHelper.createCharges(this.requestSpec, this.responseSpec,
ChargesHelper.getSavingsWithdrawalFeeJSON());
Assert.assertNotNull(withdrawalChargeId);
this.savingsAccountHelper.addChargesForSavings(savingsId, withdrawalChargeId);
ArrayList<HashMap> chargesPendingState = this.savingsAccountHelper.getSavingsCharges(savingsId);
Assert.assertEquals(1, chargesPendingState.size());
Integer savingsChargeId = (Integer) chargesPendingState.get(0).get("id");
HashMap chargeChanges = this.savingsAccountHelper.updateCharges(savingsChargeId, savingsId);
Assert.assertTrue(chargeChanges.containsKey("amount"));
Integer deletedChargeId = this.savingsAccountHelper.deleteCharge(savingsChargeId, savingsId);
assertEquals(savingsChargeId, deletedChargeId);
chargesPendingState = this.savingsAccountHelper.getSavingsCharges(savingsId);
Assert.assertTrue(chargesPendingState == null || chargesPendingState.size() == 0);
savingsStatusHashMap = this.savingsAccountHelper.approveSavings(savingsId);
SavingsStatusChecker.verifySavingsIsApproved(savingsStatusHashMap);
savingsStatusHashMap = this.savingsAccountHelper.activateSavings(savingsId);
SavingsStatusChecker.verifySavingsIsActive(savingsStatusHashMap);
final Integer chargeId = ChargesHelper.createCharges(this.requestSpec, this.responseSpec, ChargesHelper.getSavingsAnnualFeeJSON());
Assert.assertNotNull(chargeId);
ArrayList<HashMap> charges = this.savingsAccountHelper.getSavingsCharges(savingsId);
Assert.assertTrue(charges == null || charges.size() == 0);
this.savingsAccountHelper.addChargesForSavings(savingsId, chargeId);
charges = this.savingsAccountHelper.getSavingsCharges(savingsId);
Assert.assertEquals(1, charges.size());
HashMap savingsChargeForPay = charges.get(0);
Integer annualSavingsChargeId = (Integer) savingsChargeForPay.get("id");
ArrayList<HashMap> savingsAccountErrorData = (ArrayList<HashMap>) validationErrorHelper.inactivateCharge(annualSavingsChargeId,
savingsId, CommonConstants.RESPONSE_ERROR);
assertEquals("validation.msg.savingsaccountcharge.inactivation.of.charge.not.allowed.when.charge.is.due", savingsAccountErrorData
.get(0).get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE));
SimpleDateFormat sdf = new SimpleDateFormat(CommonConstants.dateFormat, Locale.US);
Calendar cal = Calendar.getInstance();
List dates = (List) savingsChargeForPay.get("dueDate");
cal.set(Calendar.YEAR, (Integer) dates.get(0));
cal.set(Calendar.MONTH, (Integer) dates.get(1) - 1);
cal.set(Calendar.DAY_OF_MONTH, (Integer) dates.get(2));
this.savingsAccountHelper.payCharge((Integer) savingsChargeForPay.get("id"), savingsId,
((Float) savingsChargeForPay.get("amount")).toString(), sdf.format(cal.getTime()));
HashMap paidCharge = this.savingsAccountHelper.getSavingsCharge(savingsId, (Integer) savingsChargeForPay.get("id"));
assertEquals(savingsChargeForPay.get("amount"), paidCharge.get("amountPaid"));
Integer inactivatedChargeId = (Integer) this.savingsAccountHelper.inactivateCharge(annualSavingsChargeId, savingsId,
CommonConstants.RESPONSE_RESOURCE_ID);
assertEquals("Inactivated Savings Charges Id", annualSavingsChargeId, inactivatedChargeId);
final Integer monthlyFeechargeId = ChargesHelper.createCharges(this.requestSpec, this.responseSpec,
ChargesHelper.getSavingsMonthlyFeeJSON());
Assert.assertNotNull(monthlyFeechargeId);
this.savingsAccountHelper.addChargesForSavings(savingsId, monthlyFeechargeId);
charges = this.savingsAccountHelper.getSavingsCharges(savingsId);
Assert.assertEquals(2, charges.size());
HashMap savingsChargeForWaive = charges.get(1);
final Integer monthlySavingsCharge = (Integer) savingsChargeForWaive.get("id");
savingsAccountErrorData = (ArrayList<HashMap>) validationErrorHelper.inactivateCharge(monthlySavingsCharge, savingsId,
CommonConstants.RESPONSE_ERROR);
assertEquals("validation.msg.savingsaccountcharge.inactivation.of.charge.not.allowed.when.charge.is.due", savingsAccountErrorData
.get(0).get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE));
this.savingsAccountHelper.waiveCharge((Integer) savingsChargeForWaive.get("id"), savingsId);
HashMap waiveCharge = this.savingsAccountHelper.getSavingsCharge(savingsId, (Integer) savingsChargeForWaive.get("id"));
assertEquals(savingsChargeForWaive.get("amount"), waiveCharge.get("amountWaived"));
this.savingsAccountHelper.waiveCharge((Integer) savingsChargeForWaive.get("id"), savingsId);
waiveCharge = this.savingsAccountHelper.getSavingsCharge(savingsId, (Integer) savingsChargeForWaive.get("id"));
BigDecimal totalWaiveAmount = BigDecimal.valueOf(Double.valueOf((Float) savingsChargeForWaive.get("amount")));
totalWaiveAmount = totalWaiveAmount.add(totalWaiveAmount);
assertEquals(totalWaiveAmount.floatValue(), waiveCharge.get("amountWaived"));
final Integer weeklyFeeId = ChargesHelper.createCharges(this.requestSpec, this.responseSpec,
ChargesHelper.getSavingsWeeklyFeeJSON());
Assert.assertNotNull(weeklyFeeId);
this.savingsAccountHelper.addChargesForSavings(savingsId, weeklyFeeId);
charges = this.savingsAccountHelper.getSavingsCharges(savingsId);
Assert.assertEquals(3, charges.size());
savingsChargeForPay = charges.get(2);
final Integer weeklySavingsFeeId = (Integer) savingsChargeForPay.get("id");
savingsAccountErrorData = (ArrayList<HashMap>) validationErrorHelper.inactivateCharge(weeklySavingsFeeId, savingsId,
CommonConstants.RESPONSE_ERROR);
assertEquals("validation.msg.savingsaccountcharge.inactivation.of.charge.not.allowed.when.charge.is.due", savingsAccountErrorData
.get(0).get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE));
cal = Calendar.getInstance();