Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added versions of createReverse able to preserve tags of entries #159

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions modules/minigl/src/main/java/org/jpos/gl/GLTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,36 @@ public GLEntry createCredit (FinalAccount acct, BigDecimal amount) {
{
return createGLEntry (acct, amount, detail, true, layer);
}

/**
*
* Create a reverse transaction based on this one
*
* @return a reversal transaction
*/
public GLTransaction createReverse() {
return createReverse(false);
}

/**
*
* Create a reverse transaction based on this one
*
* @param keepEntryTags if true entries tags are copied to the reversal entries
* @return a reversal transaction
*/
public GLTransaction createReverse(boolean keepEntryTags) {
GLTransaction glt = new GLTransaction ("(" + getDetail() + ")");
glt.setJournal (getJournal());
for (GLEntry e : getEntries()) {
glt.createGLEntry(
GLEntry reversalEntry = glt.createGLEntry(
e.getAccount(),
negate(e.getAmount()),
e.getDetail(),
e.isCredit(),
e.getLayer()
);
if (keepEntryTags) reversalEntry.setTags(e.getTags());
}
return glt;
}
Expand All @@ -315,20 +328,35 @@ public GLTransaction createReverse() {
* @return a reversal transaction
*/
public GLTransaction createReverse(Tags withTags, Tags withoutTags) {
return createReverse(withTags, withoutTags, false);
}

/**
*
* Create a reverse transaction based on this one
*
* @param withTags entries with any tag in <code>withTags</code> are selected (can be null)
* @param withoutTags entries with any tag in <code>withoutTags</code> are discarded (can be null)
*
* @param keepEntryTags if true, reverse entries have the same tags as the original ones
* @return a reversal transaction
*/
public GLTransaction createReverse(Tags withTags, Tags withoutTags, boolean keepEntryTags) {
GLTransaction glt = new GLTransaction ("(" + getDetail() + ")");
glt.setJournal (getJournal());
for (GLEntry e : getEntries()) {
Tags tags = e.getTags() != null ? e.getTags() : new Tags();
if ((withTags == null || tags.containsAll(withTags)) &&
(withoutTags == null || !tags.containsAny(withoutTags)))
{
glt.createGLEntry(
GLEntry reversalEntry = glt.createGLEntry(
e.getAccount(),
negate(e.getAmount()),
e.getDetail(),
e.isCredit(),
e.getLayer()
);
if (keepEntryTags) reversalEntry.setTags(e.getTags());
}
}
return glt;
Expand Down Expand Up @@ -394,7 +422,7 @@ public int hashCode() {
.toHashCode();
}
public boolean hasLayers (short[] layers) {
for (GLEntry e : (List<GLEntry>) getEntries()) {
for (GLEntry e : getEntries()) {
if (e.hasLayers (layers))
return true;
}
Expand All @@ -418,7 +446,7 @@ public BigDecimal getCredits (short[] layers) {
}
return credits;
}
@SuppressWarnings("unchecked")

public BigDecimal getImpact(Account acct, short[] layers) {
BigDecimal impact = GLSession.ZERO;
for (GLEntry e : getEntries()) {
Expand Down
89 changes: 89 additions & 0 deletions modules/minigl/src/test/java/org/jpos/gl/GLTransactionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jpos.gl;

import org.jpos.util.Tags;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Date;

class GLTransactionTest extends TestBase{

FinalAccount bobEquity, aliceEquity;
GLTransaction transactionForReverse;
static final String CREDIT_TAG = "credit";
static final String DEBIT_TAG = "debit";
static final String TEST_TAG = "test";
static final short LAYER = 2;
static final short[] LAYERS = {LAYER};

static final BigDecimal AMOUNT = new BigDecimal(1000).setScale(2);


@BeforeEach
public void setUp() throws Exception {
bobEquity = (FinalAccount) gls.getAccount ("TestChart", "31");
aliceEquity = (FinalAccount) gls.getAccount ("TestChart", "32");
transactionForReverse = new GLTransaction ("Single-post in layer two");
transactionForReverse.setPostDate (new Date());
GLEntry debit = transactionForReverse.createDebit (
bobEquity,
AMOUNT,
null, (short) 2
);
debit.setTags(new Tags(DEBIT_TAG, TEST_TAG));
GLEntry credit = transactionForReverse.createCredit (
aliceEquity,
AMOUNT,
null, (short) 2
);

credit.setTags(new Tags(CREDIT_TAG, TEST_TAG));
}

@Test
void testCreateReverse() {
GLTransaction reversal = transactionForReverse. createReverse();
Assertions.assertEquals(reversal.getImpact(bobEquity, LAYERS), transactionForReverse.getImpact(bobEquity, LAYERS).negate(), "reverse impact is not the opposite");
Assertions.assertEquals(reversal.getImpact(aliceEquity, LAYERS), transactionForReverse.getImpact(aliceEquity, LAYERS).negate(), "reverse impact is not the opposite");
Assertions.assertEquals(reversal.getEntries().size(), 2);
for (GLEntry entry : reversal.getEntries()) {
Assertions.assertNull(entry.getTags());
}
}

@Test
void testCreateReverseKeepTags() {
GLTransaction reversal = transactionForReverse. createReverse(true);
Assertions.assertEquals(reversal.getImpact(bobEquity, LAYERS), transactionForReverse.getImpact(bobEquity, LAYERS).negate(), "reverse impact is not the opposite");
Assertions.assertEquals(reversal.getImpact(aliceEquity, LAYERS), transactionForReverse.getImpact(aliceEquity, LAYERS).negate(), "reverse impact is not the opposite");
Assertions.assertEquals(reversal.getEntries().size(), 2);
for (GLEntry entry : reversal.getEntries()) {
Assertions.assertTrue(entry.getTags().contains(TEST_TAG));
}
}

@Test
void testCreateReverseWithTags() {
GLTransaction reversal = transactionForReverse. createReverse(new Tags(TEST_TAG), new Tags(DEBIT_TAG));
Assertions.assertEquals(reversal.getImpact(aliceEquity, LAYERS), transactionForReverse.getImpact(aliceEquity, LAYERS).negate(), "reverse impact is not the opposite");
Assertions.assertEquals(0, reversal.getImpact(bobEquity, LAYERS).compareTo(BigDecimal.ZERO), "reverse impact is not the opposite");
Assertions.assertEquals(reversal.getEntries().size(), 1);
for (GLEntry entry : reversal.getEntries()) {
Assertions.assertNull(entry.getTags());
}
}

@Test
void testCreateReverseWithTagsKeepTags() {
GLTransaction reversal = transactionForReverse. createReverse(new Tags(TEST_TAG), new Tags(DEBIT_TAG),true);
Assertions.assertEquals(reversal.getImpact(aliceEquity, LAYERS), transactionForReverse.getImpact(aliceEquity, LAYERS).negate(), "reverse impact is not the opposite");
Assertions.assertEquals(0, reversal.getImpact(bobEquity, LAYERS).compareTo(BigDecimal.ZERO), "reverse impact is not the opposite");
Assertions.assertEquals(reversal.getEntries().size(), 1);
for (GLEntry entry : reversal.getEntries()) {
Assertions.assertEquals(entry.getTags(), new Tags(TEST_TAG, CREDIT_TAG));
}
}
}
2 changes: 1 addition & 1 deletion modules/minigl/src/test/java/org/jpos/gl/TestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public void setUpBase () throws Exception {
String userName = System.getProperty("user.name");
System.setProperty("user.name", "travis");
new Import().parse("../test-classes/testdata.xml");
System.setProperty("user.name", userName);
new Export().export(System.out);
System.setProperty("user.name", userName);
} catch (Exception e) {
e.printStackTrace();
throw e;
Expand Down