package org.internna.ossmoney.model.support;
import org.junit.Test;
import org.junit.Before;
import java.text.NumberFormat;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.Locale;
import java.util.TreeMap;
import java.util.Calendar;
import java.math.BigDecimal;
import org.internna.ossmoney.model.Category;
import org.internna.ossmoney.model.Subcategory;
import org.internna.ossmoney.util.DateUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.support.StaticMessageSource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class InflowOutflowTest {
private InflowOutflow inflowOutflow;
private Category category, otherCategory;
private Subcategory subcategory, otherSubcategory, thirdSubcategory;
private MessageSource messageSource = new StaticMessageSource();
@Before
public void init() {
category = new Category();
category.setId(100L);
category.setIncome(Boolean.TRUE);
category.setCategory("category");
subcategory = new Subcategory();
subcategory.setId(1000L);
subcategory.setCategory("subcategory");
subcategory.setParentCategory(category);
otherSubcategory = new Subcategory();
otherSubcategory.setId(1001L);
otherSubcategory.setParentCategory(category);
otherSubcategory.setCategory("otherSubcategory");
otherCategory = new Category();
otherCategory.setId(101L);
otherCategory.setIncome(Boolean.TRUE);
otherCategory.setCategory("otherCategory");
thirdSubcategory = new Subcategory();
thirdSubcategory.setId(1002L);
thirdSubcategory.setCategory("thirdSubcategory");
thirdSubcategory.setParentCategory(otherCategory);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -2);
Interval interval = new Interval(calendar.getTime(), new Date());
inflowOutflow = new InflowOutflow(interval, "USD", Locale.US, messageSource);
}
@Test
public void testGet() {
Date now = new Date();
Object result = inflowOutflow.get(now, true);
assertNotNull("Created", result);
assertEquals("Returned", result, inflowOutflow.get(now, true));
}
@Test
public void testGetMonth() {
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH) - 5;
calendar.setTime(inflowOutflow.getMonth(5));
assertEquals("January", new Integer(month < 0 ? month + 12 : month), new Integer(calendar.get(Calendar.MONTH)));
}
@Test
public void testCalculateCategoryTotal() {
Map<Subcategory, BigDecimal> data = new TreeMap<Subcategory, BigDecimal>();
data.put(subcategory, BigDecimal.ONE);
data.put(otherSubcategory, BigDecimal.TEN);
assertEquals("Total per category", new BigDecimal(11), inflowOutflow.calculateCategoryTotal(data));
}
@Test
public void testCalculateMonthTotal() {
Map<Subcategory, BigDecimal> subdata = new TreeMap<Subcategory, BigDecimal>();
subdata.put(subcategory, BigDecimal.ONE);
subdata.put(otherSubcategory, BigDecimal.TEN);
Map<Category, Map<Subcategory, BigDecimal>> data = new TreeMap<Category, Map<Subcategory, BigDecimal>>();
data.put(category, subdata);
Map<Date, Map<Category, Map<Subcategory, BigDecimal>>> monthData = new TreeMap<Date, Map<Category, Map<Subcategory, BigDecimal>>>();
Date now = DateUtils.getMonthStartDate(new Date());
monthData.put(now, data);
assertEquals("Total per category", new BigDecimal(11), inflowOutflow.calculateMonthTotal(0, monthData));
}
@Test
public void testAsJSON() {
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
Map<Subcategory, BigDecimal> subdata = new TreeMap<Subcategory, BigDecimal>();
subdata.put(subcategory, BigDecimal.ONE);
subdata.put(otherSubcategory, BigDecimal.TEN);
Map<Category, Map<Subcategory, BigDecimal>> data = new TreeMap<Category, Map<Subcategory, BigDecimal>>();
data.put(category, subdata);
Map<Date, Map<Category, Map<Subcategory, BigDecimal>>> monthData = new TreeMap<Date, Map<Category, Map<Subcategory, BigDecimal>>>();
Date now = DateUtils.getMonthStartDate(new Date());
monthData.put(now, data);
Date past = inflowOutflow.getMonth(1);
Map<Subcategory, BigDecimal> moreSubdata = new TreeMap<Subcategory, BigDecimal>();
moreSubdata.put(subcategory, BigDecimal.ONE);
Map<Subcategory, BigDecimal> extraSubdata = new TreeMap<Subcategory, BigDecimal>();
extraSubdata.put(thirdSubcategory, BigDecimal.TEN);
Map<Category, Map<Subcategory, BigDecimal>> moreData = new TreeMap<Category, Map<Subcategory, BigDecimal>>();
moreData.put(category, moreSubdata);
moreData.put(otherCategory, extraSubdata);
monthData.put(past, moreData);
inflowOutflow.getInflow().putAll(monthData);
NameValuePair<String, List<BigDecimal>> values = inflowOutflow.asJSON(formatter, 1, "i18n", monthData);
assertEquals("JSON formatted", "{\"id\": 1, \"label\": \"Flow\", \"totalMonth0\": \"$11.00\", \"totalMonth1\": \"$11.00\", \"total\": \"$22.00\", \"categories\": [{\"id\": 999100, \"label\": \"category\", \"totalMonth0\": \"$11.00\", \"totalMonth1\": \"$1.00\", \"total\": \"$12.00\", \"subcategories\": [{\"id\": 1001, \"label\": \"otherSubcategory\", \"totalMonth0\": \"$10.00\", \"totalMonth1\": \"\", \"total\": \"$10.00\"},{\"id\": 1000, \"label\": \"subcategory\", \"totalMonth0\": \"$1.00\", \"totalMonth1\": \"$1.00\", \"total\": \"$2.00\"}]},{\"id\": 999101, \"label\": \"otherCategory\", \"totalMonth0\": \"\", \"totalMonth1\": \"$10.00\", \"total\": \"$10.00\", \"subcategories\": [{\"id\": 1002, \"label\": \"thirdSubcategory\", \"totalMonth0\": \"\", \"totalMonth1\": \"$10.00\", \"total\": \"$10.00\"}]}]}", values.getKey());
assertEquals("Two month totals", new Integer(2), new Integer(values.getValue().size()));
}
@Test
public void testGetOrCreateCategory() {
Map<Category, Map<Subcategory, Map<Date, BigDecimal>>> reverted = new TreeMap<Category, Map<Subcategory, Map<Date, BigDecimal>>>();
Object result = inflowOutflow.getOrCreateCategory(reverted, category);
assertNotNull("Always return submaps", result);
assertEquals("Same submap returned", result, inflowOutflow.getOrCreateCategory(reverted, category));
}
@Test
public void testGetOrCreateSubcategory() {
Map<Subcategory, Map<Date, BigDecimal>> values = new TreeMap<Subcategory, Map<Date, BigDecimal>>();
Object result = inflowOutflow.getOrCreateSubcategory(values, subcategory);
assertNotNull("Always return date totals", result);
assertEquals("Same date totals returned", result, inflowOutflow.getOrCreateSubcategory(values, subcategory));
}
@Test
public void testRevert() {
Map<Subcategory, BigDecimal> subdata = new TreeMap<Subcategory, BigDecimal>();
subdata.put(subcategory, BigDecimal.ONE);
subdata.put(otherSubcategory, BigDecimal.TEN);
Map<Category, Map<Subcategory, BigDecimal>> data = new TreeMap<Category, Map<Subcategory, BigDecimal>>();
data.put(category, subdata);
Map<Date, Map<Category, Map<Subcategory, BigDecimal>>> monthData = new TreeMap<Date, Map<Category, Map<Subcategory, BigDecimal>>>();
Date now = DateUtils.getMonthStartDate(new Date());
monthData.put(now, data);
Date past = inflowOutflow.getMonth(1);
Map<Subcategory, BigDecimal> moreSubdata = new TreeMap<Subcategory, BigDecimal>();
moreSubdata.put(subcategory, BigDecimal.ONE);
Map<Subcategory, BigDecimal> extraSubdata = new TreeMap<Subcategory, BigDecimal>();
extraSubdata.put(thirdSubcategory, BigDecimal.TEN);
Map<Category, Map<Subcategory, BigDecimal>> moreData = new TreeMap<Category, Map<Subcategory, BigDecimal>>();
moreData.put(category, moreSubdata);
moreData.put(otherCategory, extraSubdata);
monthData.put(past, moreData);
Map<Category, Map<Subcategory, Map<Date, BigDecimal>>> reverted = inflowOutflow.revert(monthData);
assertEquals("Two categories", new Integer(2), new Integer(reverted.size()));
assertEquals("Two subcategories", new Integer(2), new Integer(reverted.get(category).size()));
assertEquals("Two dates", new Integer(2), new Integer(reverted.get(category).get(subcategory).size()));
assertEquals("Not all subcategories have both dates", new Integer(1), new Integer(reverted.get(otherCategory).get(thirdSubcategory).size()));
}
@Test
public void testCalculateCategoryDateTotal() {
Date past = inflowOutflow.getMonth(1);
Date now = DateUtils.getMonthStartDate(new Date());
Map<Subcategory, Map<Date, BigDecimal>> data = new TreeMap<Subcategory, Map<Date, BigDecimal>>();
Map<Date, BigDecimal> subdata = new TreeMap<Date, BigDecimal>();
subdata.put(now, BigDecimal.TEN);
subdata.put(past, BigDecimal.ONE);
data.put(subcategory, subdata);
Map<Date, BigDecimal> otherSubdata = new TreeMap<Date, BigDecimal>();
otherSubdata.put(now, BigDecimal.TEN);
data.put(otherSubcategory, otherSubdata);
assertEquals("Category total", new BigDecimal(20), inflowOutflow.calculateCategoryDateTotal(0, data));
assertEquals("Category total past month", new BigDecimal(1), inflowOutflow.calculateCategoryDateTotal(1, data));
}
@Test
public void testCalculateSubcategoryDateTotal() {
Date past = inflowOutflow.getMonth(1);
Date now = DateUtils.getMonthStartDate(new Date());
Map<Date, BigDecimal> data = new TreeMap<Date, BigDecimal>();
data.put(now, BigDecimal.TEN);
data.put(past, BigDecimal.ONE);
assertEquals("Subcategory total", BigDecimal.TEN, inflowOutflow.calculateSubcategoryDateTotal(0, data));
assertEquals("Subcategory past total", BigDecimal.ONE, inflowOutflow.calculateSubcategoryDateTotal(1, data));
}
}