package org.internna.ossmoney.services.impl;
import java.util.Date;
import java.math.BigDecimal;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.internna.ossmoney.model.Account;
import org.internna.ossmoney.util.DateUtils;
import org.internna.ossmoney.model.Investment;
import org.internna.ossmoney.model.Subcategory;
import org.internna.ossmoney.util.SecurityUtils;
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.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.test.context.transaction.TransactionConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback = true)
@ContextConfiguration(locations = {"classpath:/META-INF/spring/applicationContext.xml", "classpath:/META-INF/spring/applicationContext-security.xml"})
public class InvestmentServiceTest {
@Autowired private org.internna.ossmoney.services.InvestmentService investmentService;
@Autowired private AuthenticationManager authenticationManager;
@Before
public void setup() {
SecurityUtils.authenticate(authenticationManager, "jose", "jose");
}
@Test
public void testAddInvestment() {
Investment investment = new Investment();
Account account = Account.findAccount(1L);
investment.setSymbol("INV");
investment.setName("investment");
investment.setProductType("type");
investment.setOwner(account.getOwner());
investment.persist();
InvestmentTransaction transaction = new InvestmentTransaction();
transaction.setQuantity(5D);
transaction.setInvestment(new Investment());
transaction.getInvestment().setId(investment.getId());
transaction.setPrice(new InvestmentPrice());
transaction.getPrice().setPrice(2D);
transaction.setAccountTransaction(new AccountTransaction());
transaction.getAccountTransaction().setAccount(new Account());
transaction.getAccountTransaction().getAccount().setId(1L);
transaction.getAccountTransaction().setAmount(BigDecimal.TEN);
transaction.getAccountTransaction().setOperationDate(DateUtils.getMidnight(new Date()));
transaction.getAccountTransaction().setSubcategory(new Subcategory());
transaction.getAccountTransaction().getSubcategory().setCategory("category.investment.buy");
double balance = account.calculateBalance().doubleValue();
investmentService.addInvestment(account.getOwner(), account, transaction, 11D);
assertNotNull("Transaction persisted", transaction.getId());
assertNotNull("Investment price persisted", transaction.getPrice().getId());
assertNotNull("Account transaction persisted", transaction.getAccountTransaction().getId());
assertEquals("One price available for the investment", new Double(2D), investment.getCurrentPrice());
assertEquals("Balance was updated accordingly", new Double(balance - 21D), new Double(account.calculateBalance().doubleValue()));
}
}