View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.gl.batch;
20  
21  import java.util.Calendar;
22  import java.util.GregorianCalendar;
23  
24  import org.kuali.kfs.gl.batch.service.EncumbranceClosingOriginEntryGenerationService;
25  import org.kuali.kfs.gl.batch.service.impl.OriginEntryOffsetPair;
26  import org.kuali.kfs.gl.businessobject.Encumbrance;
27  import org.kuali.kfs.gl.businessobject.OriginEntryTestBase;
28  import org.kuali.kfs.sys.ConfigureContext;
29  import org.kuali.kfs.sys.KFSConstants;
30  import org.kuali.kfs.sys.context.SpringContext;
31  import org.kuali.kfs.sys.context.TestUtils;
32  import org.kuali.rice.core.api.util.type.KualiDecimal;
33  
34  /**
35   * Tests that the forward encumbrance process is generating cost share encumbrance forwarding origin entries correctly
36   */
37  @ConfigureContext
38  public class ForwardEncumbranceTest extends OriginEntryTestBase {
39      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ForwardEncumbranceTest.class);
40  
41      /**
42       * An enum with a set of encumbrances to test; here, we've only got one encumbrance, an encumbrance
43       * that should force forward encumbrances to generate a cost share forwarding entry/offset pair
44       */
45      enum ENCUMBRANCE_FIXTURE {
46          COST_SHARE_ENCUMBRANCE("BL", "4531413", "CS001", "7100", "EX", "EE");
47  
48          // to find account: select ca_prior_yr_acct_t.fin_coa_cd, ca_prior_yr_acct_t.ACCOUNT_NBR, CA_A21_SUB_ACCT_T.sub_acct_nbr
49          // from (ca_prior_yr_acct_t join ca_sub_fund_grp_t on ca_prior_yr_acct_t.SUB_FUND_GRP_CD =
50          // ca_sub_fund_grp_t.SUB_FUND_GRP_CD) join CA_A21_SUB_ACCT_T on CA_PRIOR_YR_ACCT_T.fin_coa_cd = CA_A21_SUB_ACCT_T.fin_coa_cd
51          // and CA_PRIOR_YR_ACCT_T.account_nbr = CA_A21_SUB_ACCT_T.account_nbr where ca_sub_fund_grp_t.FUND_GRP_CD = 'CG' and
52          // CA_A21_SUB_ACCT_T.sub_acct_typ_cd = 'CS'
53          // this was a rough one, it needed a cost share sub account and a CG sub fund group fund group type
54  
55          private String chart;
56          private String accountNumber;
57          private String subAccountNumber;
58          private String objectCode;
59          private String balanceType;
60          private String objectTypeCode;
61  
62          /**
63           * Constructs a ForwardEncumbranceTest.ENCUMBRANCE_FIXTURE
64           * @param chart the chart of the encumbrance
65           * @param accountNumber the account of the encumbrance
66           * @param subAccountNumber the sub account of the encumbrance
67           * @param objectCode the object code of the encumbrance
68           * @param balanceType the balance type code of the encumbrance
69           * @param objectTypeCode the object type code of the encumbrance
70           */
71          private ENCUMBRANCE_FIXTURE(String chart, String accountNumber, String subAccountNumber, String objectCode, String balanceType, String objectTypeCode) {
72              this.chart = chart;
73              this.accountNumber = accountNumber;
74              this.subAccountNumber = subAccountNumber;
75              this.objectCode = objectCode;
76              this.balanceType = balanceType;
77              this.objectTypeCode = objectTypeCode;
78          }
79  
80          /**
81           * Converts one of the members of this enum to an actual Encumbrance
82           * 
83           * @return a real encumbrance!
84           */
85          public Encumbrance convertToEncumbrance() {
86              Encumbrance e = new Encumbrance();
87              Integer fy = TestUtils.getFiscalYearForTesting().intValue() - 1;
88              e.setUniversityFiscalYear(fy);
89              e.setChartOfAccountsCode(chart);
90              e.setAccountNumber(accountNumber);
91              e.setSubAccountNumber(subAccountNumber);
92              e.setObjectCode(objectCode);
93              e.setSubObjectCode(KFSConstants.getDashFinancialSubObjectCode());
94              e.setBalanceTypeCode(balanceType);
95              e.setDocumentTypeCode("EXEN"); // we don't need this field
96              e.setOriginCode("EP");
97              e.setDocumentNumber("000001"); // we don't need this field
98              GregorianCalendar lastYear = new GregorianCalendar();
99              lastYear.set(Calendar.YEAR, (TestUtils.getFiscalYearForTesting().intValue() - 1));
100             e.setTransactionEncumbranceDate(new java.sql.Date(lastYear.getTimeInMillis()));
101             e.setTransactionEncumbranceDescription("MONKEYS-R-US IS THE NEWEST AND GREATEST STORE IN THE ENTIRE TRI-STATE AREA");
102             e.setAccountLineEncumbranceAmount(new KualiDecimal(1000));
103             e.setAccountLineEncumbranceClosedAmount(KualiDecimal.ZERO);
104             return e;
105         }
106 
107         /**
108          * Returns the object type code of this enum
109          * 
110          * @return this enum's object type code
111          */
112         public String getObjectType() {
113             return this.objectTypeCode;
114         }
115     }
116 
117     /**
118      * Tests that the expected encumbrance fixtures would be selected by the forward encumbrance process
119      */
120     public void testEncumbranceSelection() {
121         final EncumbranceClosingOriginEntryGenerationService encumbranceClosingOriginEntryGenerationService = SpringContext.getBean(EncumbranceClosingOriginEntryGenerationService.class);
122 
123         assertTrue(encumbranceClosingOriginEntryGenerationService.shouldForwardEncumbrance(ENCUMBRANCE_FIXTURE.COST_SHARE_ENCUMBRANCE.convertToEncumbrance()));
124     }
125 
126     /**
127      * Tests that the expted fixtures would be selected for cost share entry/offset generation by the forward encumbrance process
128      * 
129      * @throws Exception thrown if something goes wrong
130      */
131     public void testCostShareSelection() throws Exception {
132         final EncumbranceClosingOriginEntryGenerationService encumbranceClosingOriginEntryGenerationService = SpringContext.getBean(EncumbranceClosingOriginEntryGenerationService.class);
133 
134         Encumbrance encumbrance = ENCUMBRANCE_FIXTURE.COST_SHARE_ENCUMBRANCE.convertToEncumbrance();
135         final Integer postingYear = new Integer(TestUtils.getFiscalYearForTesting().intValue() - 1);
136         OriginEntryOffsetPair entryPair = encumbranceClosingOriginEntryGenerationService.createBeginningBalanceEntryOffsetPair(encumbrance, postingYear, new java.sql.Date(new GregorianCalendar().getTimeInMillis()));
137 
138         assertTrue(encumbranceClosingOriginEntryGenerationService.shouldForwardCostShareForEncumbrance(entryPair.getEntry(), entryPair.getOffset(), encumbrance, ENCUMBRANCE_FIXTURE.COST_SHARE_ENCUMBRANCE.getObjectType()));
139 
140         OriginEntryOffsetPair costShareEntryPair = encumbranceClosingOriginEntryGenerationService.createCostShareBeginningBalanceEntryOffsetPair(encumbrance, new java.sql.Date(new GregorianCalendar().getTimeInMillis()));
141         assertFalse( "Should not have had a fatal error: " + costShareEntryPair, costShareEntryPair.isFatalErrorFlag() );
142         LOG.info(costShareEntryPair.getEntry().getLine());
143         LOG.info(costShareEntryPair.getOffset().getLine());
144 
145         assertTrue(costShareEntryPair.getOffset().getLine().indexOf("GENERATED") >= 0);
146     }
147 }