Skip to content

Commit

Permalink
#51 - Update of accountancy tests (made them more realistic, made the…
Browse files Browse the repository at this point in the history
…m testable)
  • Loading branch information
Andreas Zaschka authored and odrotbohm committed Nov 16, 2015
1 parent bc8b000 commit 4f0c5ad
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Optional;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Lob;

import org.javamoney.moneta.Money;
import org.salespointframework.core.AbstractEntity;
import org.salespointframework.core.Currencies;
Expand Down Expand Up @@ -87,7 +85,11 @@ public final Optional<LocalDateTime> getDate() {
return Optional.ofNullable(date);
}

void setDate(LocalDateTime dateTime) {
/**
* TODO/Caution: Only for test purpose !
* @param dateTime
*/
public void setDate(LocalDateTime dateTime) {
this.date = dateTime;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package org.salespointframework.accountancy;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.Map;
import java.util.Map.Entry;

import org.hamcrest.Matchers;
import static org.hamcrest.Matchers.is;
import org.javamoney.moneta.Money;
import org.junit.Assert;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.salespointframework.AbstractIntegrationTests;
Expand All @@ -21,7 +27,7 @@

public class AccountancyPeriodTests extends AbstractIntegrationTests {

@Autowired Accountancy<ProductPaymentEntry> a;
@Autowired Accountancy<ProductPaymentEntry> accountancy;
@Autowired UserAccountManager userAccountManager;

private LocalDateTime from;
Expand All @@ -35,66 +41,92 @@ public void testSetup() throws Exception {

Money oneEuro = Money.of(1, Currencies.EURO);

System.out.println("Creating AccountancyEntries: ");

for (int i = 0; i < 20; i++) {

ProductPaymentEntry p = new ProductPaymentEntry(orderIdentifier, account, oneEuro, "Rechnung nr. 3", Cash.CASH);
a.add(p);

System.out.println("Adding p " + p);

if (i == 5)
from = p.getDate().get();
if (i == 15)
to = p.getDate().get();

Thread.sleep(5);
for (int day = 1; day < 20; day++) {

for(int hour = 10; hour < 19; hour++) {
ProductPaymentEntry p = new ProductPaymentEntry(
orderIdentifier, account, oneEuro, "Rechnung Nr.2015-03-" + day + "/" + hour, Cash.CASH);
p.setDate(LocalDateTime.of(2015, Month.MARCH, day, hour, 0));
accountancy.add(p);
}

if (day == 5)
from = LocalDateTime.of(2015, Month.MARCH, day, 0, 0);
if (day == 15)
to = LocalDateTime.of(2015, Month.MARCH, day, 23, 0);
}
}

@Test
public void periodSetTest() {
System.out.println("Getting entries from " + from + " to " + to);
Map<Interval, Iterable<ProductPaymentEntry>> m = a.find(from, to, Duration.ofMillis(200));
System.out.println("periodeSetTest");

Map<Interval, Iterable<ProductPaymentEntry>> m = accountancy.find(from, to, Duration.ofDays(1L));

assertNotNull(m);
assertThat(m.size(), is(11));

for (Entry<Interval, Iterable<ProductPaymentEntry>> e : m.entrySet()) {
System.out.println("ProductPaymentEntries for interval " + e.getKey());
for (ProductPaymentEntry p : e.getValue()) {
System.out.println("\t" + p);
}

Iterable<ProductPaymentEntry> oneYearIterable = e.getValue();
assertThat(oneYearIterable, Matchers.<ProductPaymentEntry>iterableWithSize(9));
}
}

@Test
public void singlePeriodTest() {
System.out.println("Getting entries from " + from + " to " + to);
Map<Interval, Iterable<ProductPaymentEntry>> m = a.find(from, to, Duration.between(from, to));
System.out.println("singlePeriodTest");

Map<Interval, Iterable<ProductPaymentEntry>> m = accountancy.find(from, to, Duration.between(from, to));

assertNotNull(m);
assertThat(m.size(), is(1));

for (Entry<Interval, Iterable<ProductPaymentEntry>> e : m.entrySet()) {
System.out.println("ProductPaymentEntries for interval " + e.getKey());
for (ProductPaymentEntry p : e.getValue()) {
System.out.println("\t" + p);
}

Iterable<ProductPaymentEntry> oneYearIterable = e.getValue();

// (Period of from to end date) * (Period from 10 clock until 20 clock)
int expectedSize = 11*9;
assertThat(oneYearIterable, Matchers.<ProductPaymentEntry>iterableWithSize(expectedSize));
}
}

@Test
public void periodMoneyTest() {
Money total;
System.out.println("Getting entries from " + from + " to " + to);
Map<Interval, Iterable<ProductPaymentEntry>> m = a.find(from, to, Duration.ofMillis(200));
System.out.println("periodMoneyTest");

Money total = Currencies.ZERO_EURO;
Map<Interval, Iterable<ProductPaymentEntry>> m = accountancy.find(from, to, Duration.ofDays(1L));

assertNotNull(m);
assertThat(m.size(), is(11));

for (Entry<Interval, Iterable<ProductPaymentEntry>> e : m.entrySet()) {
total = Currencies.ZERO_EURO;

Money totalOfDay = Currencies.ZERO_EURO;
for (ProductPaymentEntry p : e.getValue()) {
System.out.println("\t" + p.getValue());
totalOfDay = totalOfDay.add(p.getValue());
total = total.add(p.getValue());
}
System.out.println("Money for interval " + e.getKey() + ": " + total);

assertThat(totalOfDay, is(Money.of(BigDecimal.valueOf(9L), Currencies.EURO)));
}
System.out.println("Getting entries from " + from + " to " + to);
Map<Interval, Money> sales = a.salesVolume(from, to, Duration.ofMillis(200));

// (Period of from to end date) * (Period from 10 clock until 20 clock)
int expectedSize = 11*9;
assertThat(total, is(Money.of(BigDecimal.valueOf(expectedSize), Currencies.EURO)));

Money totalSales = Currencies.ZERO_EURO;
Map<Interval, Money> sales = accountancy.salesVolume(from, to, Duration.ofDays(1L));

assertNotNull(m);
assertThat(sales.size(), is(11));

for (Entry<Interval, Money> e : sales.entrySet()) {
System.out.println("Money for interval " + e.getKey() + ": " + e.getValue());
totalSales = totalSales.add(e.getValue());
assertThat(e.getValue(), is(Money.of(BigDecimal.valueOf(9L), Currencies.EURO)));
}

Assert.assertEquals(total, totalSales);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.salespointframework.accountancy;

import static org.junit.Assert.*;

import java.time.LocalDateTime;

import java.time.Month;
import org.hamcrest.Matchers;
import org.javamoney.moneta.Money;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.salespointframework.AbstractIntegrationTests;
Expand All @@ -21,79 +23,92 @@
public class AccountancyTests extends AbstractIntegrationTests {

// tag::init[]
@Autowired Accountancy<AccountancyEntry> a; //<1>
@Autowired Accountancy<ProductPaymentEntry> p; //<2>
@Autowired Accountancy<AccountancyEntry> accountancyGeneral; //<1>
@Autowired Accountancy<ProductPaymentEntry> accountancySpecial; //<2>
// end::init[]
@Autowired UserAccountManager userAccountManager;

private LocalDateTime from;
private LocalDateTime to;

@Before
public void testSetup() throws Exception {

System.out.println("Creating AccountancyEntries: ");
public void testSetup() {

for (int year = 2000; year < 2010; year++) {

if ((year % 2) == 0) {
System.out.println("ProductPaymentEntry");
UserAccount user = userAccountManager.create("userId" + year, "password");
user = userAccountManager.save(user);
OrderIdentifier orderIdentifier = new Order(user).getIdentifier();

// tag::addP[]
a.add(new ProductPaymentEntry(orderIdentifier, user, Money.of(1, Currencies.EURO), "Rechnung nr " + year, Cash.CASH));
// end::addP[]
ProductPaymentEntry productPaymentEntry = new ProductPaymentEntry(
orderIdentifier,
user,
Money.of(1, Currencies.EURO),
"Rechnung Nr. " + year,
Cash.CASH);
productPaymentEntry.setDate(LocalDateTime.of(year, Month.SEPTEMBER, 01, 12, 0));

accountancySpecial.add(productPaymentEntry);
} else {
System.out.println("PersistentAccountancyEntry");
// tag::addA[]
a.add(new AccountancyEntry(Money.of(2.22, Currencies.EURO)));
// end::addA[]
AccountancyEntry accountancyEntry = new AccountancyEntry(
Money.of(2.22, Currencies.EURO),
"Rechnung Nr. " + year);
accountancyEntry.setDate(LocalDateTime.of(year, Month.APRIL, 01, 12, 0));

accountancyGeneral.add(accountancyEntry);
}

if (year == 2002) {
from = LocalDateTime.now();
from = LocalDateTime.of(year, Month.JANUARY, 01, 12, 0);
}

if (year == 2008) {
to = LocalDateTime.now();
to = LocalDateTime.of(year, Month.DECEMBER, 01, 12, 0);
}

Thread.sleep(1 * 10);

}
System.out.println("Done.");
}

@Test
public void select() {

Iterable<AccountancyEntry> i = a.find(from, to);

// TODO not really a test, because the Iterable is always non-null.
// Instead, we need to test for non-emptyness of the Iterable, or three
// elements.
Iterable<AccountancyEntry> i = accountancyGeneral.find(from, to);

assertNotNull(i);
System.out.println("Entries from " + from + " to " + to + ":");
for (AccountancyEntry e : i) {
System.out.println(e.toString());
assertThat(i, Matchers.<AccountancyEntry>iterableWithSize(7));

for (AccountancyEntry e : i) {
assertTrue(e.hasDate());
assertTrue(e.getDate().get().isAfter(from));
assertTrue(e.getDate().get().isBefore(to));
}
}

@Test
public void selectType() {
System.out.println("AccountancyEntries: ");
Iterable<ProductPaymentEntry> i = p.find(from, to);

Iterable<ProductPaymentEntry> i = accountancySpecial.find(from, to);

assertNotNull(i);
assertThat(i, Matchers.<ProductPaymentEntry>iterableWithSize(4));

for (AccountancyEntry e : i) {
System.out.println(e.toString());
assertTrue(e.hasDate());
assertTrue(e.getDate().get().isAfter(from));
assertTrue(e.getDate().get().isBefore(to));
}

System.out.println("All entries:");
Iterable<AccountancyEntry> g = a.find(from, to);
Iterable<AccountancyEntry> g = accountancyGeneral.find(from, to);

assertNotNull(i);
assertThat(g, Matchers.<AccountancyEntry>iterableWithSize(7));

for (AccountancyEntry e : g) {
System.out.println(e.toString());
assertTrue(e.hasDate());
assertTrue(e.getDate().get().isAfter(from));
assertTrue(e.getDate().get().isBefore(to));
}

}
}
}

0 comments on commit 4f0c5ad

Please sign in to comment.