package org.internna.ossmoney.mvc;
import java.util.Map;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.HashMap;
import java.util.Calendar;
import java.util.ArrayList;
import java.util.Collection;
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.model.Category;
import org.internna.ossmoney.cache.CacheStore;
import org.internna.ossmoney.model.Subcategory;
import org.internna.ossmoney.util.SecurityUtils;
import org.internna.ossmoney.model.support.Interval;
import org.internna.ossmoney.model.AccountTransaction;
import org.internna.ossmoney.model.support.InflowOutflow;
import org.springframework.ui.ModelMap;
import org.springframework.context.MessageSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.StaticMessageSource;
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.*;
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback = true)
@ContextConfiguration(locations = {"classpath:/META-INF/spring/applicationContext.xml", "classpath:/META-INF/spring/applicationContext-security.xml"})
public class ReportingControllerTest {
@Autowired private CacheStore cache;
@Autowired private AuthenticationManager authenticationManager;
private Date date;
private Interval interval;
private AccountTransaction transaction;
private ReportingController controller;
private MessageSource messageSource = new StaticMessageSource();
@Before
public void init() {
controller = new ReportingController();
controller.setCache(cache);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -1);
date = calendar.getTime();
calendar.add(Calendar.DAY_OF_YEAR, 1);
Category category = new Category();
category.setIncome(Boolean.TRUE);
category.setCategory("category");
Subcategory subcategory = new Subcategory();
subcategory.setParentCategory(category);
subcategory.setCategory("subcategory");
transaction = new AccountTransaction();
transaction.setAmount(BigDecimal.TEN);
transaction.setSubcategory(subcategory);
transaction.setOperationDate(date);
Account account = new Account();
account.setLocale(Locale.US);
account.addTransaction(transaction);
calendar.add(Calendar.MONTH, -2);
interval = new Interval(calendar.getTime(), new Date());
SecurityUtils.authenticate(authenticationManager, "jose", "jose");
}
@Test
public void testCalculateMonthlyInflowsOutflows() {
List<AccountTransaction> transactions = new ArrayList<AccountTransaction>();
transactions.add(transaction);
Collection<InflowOutflow> flows = controller.calculateMonthlyInflowsOutflows(interval, transactions);
assertEquals("One flow", new Integer(1), new Integer(flows.size()));
assertEquals("Flow data", BigDecimal.TEN, flows.iterator().next().getInflow(date).get(transaction.getSubcategory().getParentCategory()).get(transaction.getSubcategory()));
}
@Test
public void testGetOrCreateInflowOutflow() {
Map<String, InflowOutflow> inflowsOutflows = new HashMap<String, InflowOutflow>();
Object result = controller.getOrCreateInflowOutflow(interval, transaction.getAccount().getCurrency(), Locale.US, inflowsOutflows);
assertNotNull("InflowOutflow is never null", result);
assertEquals("InflowOutflow already exists", result, controller.getOrCreateInflowOutflow(interval, transaction.getAccount().getCurrency(), Locale.US, inflowsOutflows));
}
@Test
public void testProcess() {
InflowOutflow inflowOutflow = new InflowOutflow(interval, "USD", Locale.US, messageSource);
controller.process(transaction, inflowOutflow);
assertEquals("InflowOutflow modified", BigDecimal.TEN, inflowOutflow.getInflow(date).get(transaction.getSubcategory().getParentCategory()).get(transaction.getSubcategory()));
}
@Test
public void testGetOrCreateCategoryData() {
Map<Category, Map<Subcategory, BigDecimal>> data = new HashMap<Category, Map<Subcategory, BigDecimal>>();
Object result = controller.getOrCreateCategoryData(transaction, data);
assertNotNull("Category data is never null", result);
assertEquals("Category data already exists", result, controller.getOrCreateCategoryData(transaction, data));
}
@Test
public void testGetOrCreateSubcategoryAmount() {
Map<Subcategory, BigDecimal> categoryData = new HashMap<Subcategory, BigDecimal>();
assertEquals("Zero", BigDecimal.ZERO, controller.getOrCreateSubcategoryAmount(transaction, categoryData));
categoryData.put(transaction.getSubcategory(), BigDecimal.TEN);
assertEquals("Ten", BigDecimal.TEN, controller.getOrCreateSubcategoryAmount(transaction, categoryData));
}
@Test
@SuppressWarnings("unchecked")
public void testExpensesByCategoryOverTimeChartData() {
ModelMap modelMap = new ModelMap();
controller.expensesByCategoryOverTimeChartData(8L, "previous_year", modelMap);
assertEquals("Max calculated", new BigDecimal(215), ((Map<String, BigDecimal>) modelMap.get("max")).get("EUR"));
}
}