Package org.internna.ossmoney.model.support

Source Code of org.internna.ossmoney.model.support.InvestmentStatusTest

package org.internna.ossmoney.model.support;

import java.util.Date;
import java.math.BigDecimal;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.internna.ossmoney.model.Account;
import org.internna.ossmoney.util.DateUtils;
import org.internna.ossmoney.model.Subcategory;
import org.internna.ossmoney.model.Investment;
import org.internna.ossmoney.model.InvestmentPrice;
import org.internna.ossmoney.model.AccountTransaction;
import org.internna.ossmoney.model.InvestmentTransaction;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;

import static org.junit.Assert.*;

@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback = true)
@ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext.xml")
public class InvestmentStatusTest {

  @Test
  @SuppressWarnings("deprecation")
  public void testGetAsJSONString() {
    Account account = Account.findAccount(1L);
    AccountTransaction transaction = new AccountTransaction();
    transaction.setAccount(account);
    Subcategory subcategory = Subcategory.findBySubcategory("category.investment.buy", account.getOwner());
    transaction.setSubcategory(subcategory);
    transaction.setAmount(BigDecimal.TEN.negate());
    transaction.setOperationDate(new Date(100, 10, 20));
    Investment investment = new Investment();
    investment.setName("Investment");
    investment.setProductType("stocks");
    investment.setOwner(account.getOwner());
    investment.persist();
    InvestmentPrice price = new InvestmentPrice();
    price.setPrice(1D);
    price.setInvestment(investment);
    price.setUpdateTime(DateUtils.getMidnight(new Date()));
    price.persist();
    InvestmentTransaction investmentTransaction = new InvestmentTransaction();
    investmentTransaction.setId(14L);
    investmentTransaction.setPrice(price);
    investmentTransaction.setQuantity(10D);
    investmentTransaction.setInvestment(investment);
    investmentTransaction.setAccountTransaction(transaction);
    transaction.setInvestment(investmentTransaction);
    InvestmentStatus investmentStatus = new InvestmentStatus(account, investment);
    investmentStatus.add(transaction);
    assertEquals("JSON", "{operationDate:'', id: 5, label: 'Investment', buy: '10,00 €', sell: '', interest: '', gainLoss: '', pctg: '0%', quantity: '10', price: '1,00 €', operations: [{id: 999914, label: '', buy: '10,00 €', sell: '', interest: '', quantity: '10', price: '1,00 €', gainLoss: '', pctg: '', quant: 10.0, operationDate: '2000-11-20'}], quant: 10.0}", investmentStatus.getAsJSONString());
    AccountTransaction sellTransaction = new AccountTransaction();
    sellTransaction.setAccount(account);
    Subcategory sellSubcategory = Subcategory.findBySubcategory("category.investment.sell", account.getOwner());
    sellTransaction.setSubcategory(sellSubcategory);
    sellTransaction.setAmount(new BigDecimal(1.5));
    InvestmentPrice updatedPrice = new InvestmentPrice();
    updatedPrice.setPrice(1.5D);
    updatedPrice.setUpdateTime(DateUtils.nextDate(DateUtils.getMidnight(new Date())));
    updatedPrice.setInvestment(investment);
    updatedPrice.persist();
    InvestmentTransaction investmentSellTransaction = new InvestmentTransaction();
    investmentSellTransaction.setId(15L);
    investmentSellTransaction.setQuantity(1D);
    investmentSellTransaction.setPrice(updatedPrice);
    investmentSellTransaction.setInvestment(investment);
    investmentSellTransaction.setAccountTransaction(sellTransaction);
    sellTransaction.setInvestment(investmentSellTransaction);
    sellTransaction.setOperationDate(new Date(100, 10, 21));
    investmentStatus.add(sellTransaction);
    assertEquals("JSON (buy & sell)", "{operationDate:'', id: 5, label: 'Investment', buy: '10,00 €', sell: '1,50 €', interest: '', gainLoss: '5,00 €', pctg: '50%', quantity: '9', price: '1,50 €', operations: [{id: 999914, label: '', buy: '10,00 €', sell: '', interest: '', quantity: '10', price: '1,00 €', gainLoss: '', pctg: '', quant: 10.0, operationDate: '2000-11-20'},{id: 999915, label: '', buy: '', sell: '1,50 €', interest: '', quantity: '1', price: '1,50 €', gainLoss: '', pctg: '', quant: 1.0, operationDate: '2000-11-21'}], quant: 9.0}", investmentStatus.getAsJSONString());
    Investment amountedInvestment = new Investment();
    amountedInvestment.setName("Investment (amount)");
    amountedInvestment.setOwner(account.getOwner());
    amountedInvestment.setProductType("deposit");
    amountedInvestment.persist();
    InvestmentStatus amountedInvestmentStatus = new InvestmentStatus(account, amountedInvestment);
    InvestmentTransaction amountedInvestmentTransaction = new InvestmentTransaction();
    amountedInvestmentTransaction.setId(26L);
    amountedInvestmentTransaction.setQuantity(0D);
    amountedInvestmentTransaction.setInvestment(amountedInvestment);
    AccountTransaction amountedTransaction = new AccountTransaction();
    amountedTransaction.setAccount(account);
    amountedTransaction.setSubcategory(subcategory);
    amountedTransaction.setAmount(BigDecimal.TEN.negate());
    amountedTransaction.setOperationDate(new Date(100, 10, 22));
    amountedInvestmentTransaction.setAccountTransaction(amountedTransaction);
    amountedTransaction.setInvestment(amountedInvestmentTransaction);
    amountedInvestmentStatus.add(amountedTransaction);
    assertEquals("No quantity, no gain/loss", "{operationDate:'', id: 6, label: 'Investment (amount)', buy: '10,00 €', sell: '', interest: '', gainLoss: '', pctg: '0%', quantity: '', price: '', operations: [{id: 999926, label: '', buy: '10,00 €', sell: '', interest: '', quantity: '', price: '', gainLoss: '', pctg: '', quant: 0.0, operationDate: '2000-11-22'}], quant: 0.0}", amountedInvestmentStatus.getAsJSONString());
    InvestmentTransaction amountedInvestmentInterestTransaction = new InvestmentTransaction();
    amountedInvestmentInterestTransaction.setId(29L);
    amountedInvestmentInterestTransaction.setQuantity(0D);
    amountedInvestmentInterestTransaction.setInvestment(amountedInvestment);
    AccountTransaction amountedInterestTransaction = new AccountTransaction();
    amountedInterestTransaction.setAccount(account);
    amountedInterestTransaction.setOperationDate(new Date(100, 10, 23));
    Subcategory interest = Subcategory.findBySubcategory("category.investment.interest", account.getOwner());
    amountedInterestTransaction.setSubcategory(interest);
    amountedInterestTransaction.setAmount(BigDecimal.TEN);
    amountedInvestmentInterestTransaction.setAccountTransaction(amountedInterestTransaction);
    amountedInterestTransaction.setInvestment(amountedInvestmentInterestTransaction);
    amountedInvestmentStatus.add(amountedInterestTransaction);
    assertEquals("Interest gain/loss", "{operationDate:'', id: 6, label: 'Investment (amount)', buy: '10,00 €', sell: '', interest: '10,00 €', gainLoss: '10,00 €', pctg: '100%', quantity: '', price: '', operations: [{id: 999926, label: '', buy: '10,00 €', sell: '', interest: '', quantity: '', price: '', gainLoss: '', pctg: '', quant: 0.0, operationDate: '2000-11-22'},{id: 999929, label: '', buy: '', sell: '', interest: '10,00 €', quantity: '', price: '', gainLoss: '', pctg: '', quant: 0.0, operationDate: '2000-11-23'}], quant: 0.0}", amountedInvestmentStatus.getAsJSONString());
  }

}
TOP

Related Classes of org.internna.ossmoney.model.support.InvestmentStatusTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.