001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.workflow.attribute;
017:
018: import java.io.IOException;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import javax.xml.xpath.XPath;
025: import javax.xml.xpath.XPathConstants;
026: import javax.xml.xpath.XPathExpressionException;
027:
028: import org.apache.commons.lang.StringUtils;
029: import org.kuali.kfs.context.KualiTestBase;
030: import org.kuali.module.chart.bo.SubAccount;
031: import org.kuali.test.ConfigureContext;
032: import org.kuali.test.fixtures.SubAccountFixture;
033: import org.kuali.workflow.KualiWorkflowUtils;
034: import org.w3c.dom.Node;
035: import org.w3c.dom.NodeList;
036:
037: import edu.iu.uis.eden.engine.RouteContext;
038: import edu.iu.uis.eden.exception.InvalidXmlException;
039: import edu.iu.uis.eden.routeheader.DocumentContent;
040:
041: @ConfigureContext
042: public class KualiSubAccountAttributeTest extends KualiTestBase {
043:
044: private static Map VALID_SUB_ACCOUNT_PARAM_MAP = new HashMap();
045: private static Map VALID_SUB_ACCOUNT_REPORTS_TO_PARAM_MAP = new HashMap();
046: private static Map INVALID_SUB_ACCOUNT_PARAM_MAP = new HashMap();
047: static {
048: SubAccount valid = SubAccountFixture.SUB_ACCOUNT_WITHOUT_REPORTS_TO_ORGANIZATION
049: .createSubAccount();
050: VALID_SUB_ACCOUNT_PARAM_MAP.put(
051: KualiSubAccountAttribute.FIN_COA_CD_KEY, valid
052: .getChartOfAccountsCode());
053: VALID_SUB_ACCOUNT_PARAM_MAP.put(
054: KualiSubAccountAttribute.ACCOUNT_NBR_KEY, valid
055: .getAccountNumber());
056: VALID_SUB_ACCOUNT_PARAM_MAP.put(
057: KualiSubAccountAttribute.SUB_ACCOUNT_NBR_KEY, valid
058: .getSubAccountNumber());
059:
060: SubAccount validReportsTo = SubAccountFixture.SUB_ACCOUNT_WITH_REPORTS_TO_ORGANIZATION
061: .createSubAccount();
062: VALID_SUB_ACCOUNT_REPORTS_TO_PARAM_MAP.put(
063: KualiSubAccountAttribute.FIN_COA_CD_KEY, validReportsTo
064: .getFinancialReportChartCode());
065: VALID_SUB_ACCOUNT_REPORTS_TO_PARAM_MAP.put(
066: KualiSubAccountAttribute.ORG_CD_KEY, validReportsTo
067: .getFinReportOrganizationCode());
068: VALID_SUB_ACCOUNT_REPORTS_TO_PARAM_MAP.put(
069: KualiSubAccountAttribute.SUB_ACCOUNT_NBR_KEY,
070: validReportsTo.getSubAccountNumber());
071:
072: SubAccount invalid = SubAccountFixture.INVALID_SUB_ACCOUNT
073: .createSubAccount();
074: INVALID_SUB_ACCOUNT_PARAM_MAP.put(
075: KualiSubAccountAttribute.FIN_COA_CD_KEY, invalid
076: .getChartOfAccountsCode());
077: INVALID_SUB_ACCOUNT_PARAM_MAP.put(
078: KualiSubAccountAttribute.ACCOUNT_NBR_KEY, invalid
079: .getAccountNumber());
080: INVALID_SUB_ACCOUNT_PARAM_MAP.put(
081: KualiSubAccountAttribute.SUB_ACCOUNT_NBR_KEY, invalid
082: .getSubAccountNumber());
083: }
084:
085: public void testValidateRuleData() {
086: KualiSubAccountAttribute attribute = new KualiSubAccountAttribute();
087: List errors = attribute
088: .validateRuleData(VALID_SUB_ACCOUNT_PARAM_MAP);
089: assertTrue("No errors should be returned but found "
090: + errors.size(), errors.isEmpty());
091:
092: attribute = new KualiSubAccountAttribute();
093: errors = attribute
094: .validateRuleData(VALID_SUB_ACCOUNT_REPORTS_TO_PARAM_MAP);
095: assertTrue("No errors should be returned but found "
096: + errors.size(), errors.isEmpty());
097:
098: attribute = new KualiSubAccountAttribute();
099: errors = attribute
100: .validateRuleData(INVALID_SUB_ACCOUNT_PARAM_MAP);
101: assertFalse(
102: "At least one error should have been found but we found none",
103: errors.isEmpty());
104: assertEquals("Exactly one error should have been found", 1,
105: errors.size());
106: }
107:
108: public void testValidateRoutingData() {
109: KualiSubAccountAttribute attribute = new KualiSubAccountAttribute();
110: List errors = attribute
111: .validateRoutingData(VALID_SUB_ACCOUNT_PARAM_MAP);
112: assertTrue("No errors should be returned but found "
113: + errors.size(), errors.isEmpty());
114:
115: attribute = new KualiSubAccountAttribute();
116: errors = attribute
117: .validateRoutingData(VALID_SUB_ACCOUNT_REPORTS_TO_PARAM_MAP);
118: assertTrue("No errors should be returned but found "
119: + errors.size(), errors.isEmpty());
120:
121: attribute = new KualiSubAccountAttribute();
122: errors = attribute
123: .validateRoutingData(INVALID_SUB_ACCOUNT_PARAM_MAP);
124: assertFalse(
125: "At least one error should have been found but we found none",
126: errors.isEmpty());
127: assertEquals("Exactly one error should have been found", 1,
128: errors.size());
129: }
130:
131: public void testGetSubAccountValuesFromDocContentData()
132: throws IOException, InvalidXmlException,
133: XPathExpressionException {
134: String documentTypeName = "TransferOfFundsDocument";
135: DocumentContent docContent = KualiAttributeTestUtil
136: .getDocumentContentFromXmlFile(
137: KualiAttributeTestUtil.TOF_SUB_ACCOUNT_TEST_DOC,
138: documentTypeName);
139: XPath xpath = KualiWorkflowUtils.getXPath(docContent
140: .getDocument());
141: KualiSubAccountAttribute subAccountAttribute = new KualiSubAccountAttribute();
142: Set subAccountValues = subAccountAttribute
143: .populateFromDocContent(documentTypeName, docContent,
144: new RouteContext());
145:
146: assertFalse(
147: "At least one valid sub account should be returned",
148: subAccountValues.isEmpty());
149: }
150:
151: public void testGetNonReportDocumentContentValues()
152: throws IOException, InvalidXmlException,
153: XPathExpressionException {
154: String docTypeName = "TransferOfFundsDocument";
155: DocumentContent docContent = KualiAttributeTestUtil
156: .getDocumentContentFromXmlFile(
157: KualiAttributeTestUtil.TOF_SUB_ACCOUNT_TEST_DOC,
158: docTypeName);
159: String xpathExp = null;
160:
161: if (!KualiWorkflowUtils.isSourceLineOnly(docTypeName)) {
162: // checking target lines
163: xpathExp = new StringBuffer(
164: KualiWorkflowUtils.XSTREAM_SAFE_PREFIX)
165: .append(
166: KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX)
167: .append(
168: KualiWorkflowUtils
169: .getTargetAccountingLineClassName(docTypeName))
170: .append("/subAccount").append(
171: KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX)
172: .toString();
173: checkValidValues(docContent, xpathExp, "BL", "1031400",
174: null, "DPD");
175: }
176:
177: if (!KualiWorkflowUtils.isTargetLineOnly(docTypeName)) {
178: // checking source lines
179: xpathExp = new StringBuffer(
180: KualiWorkflowUtils.XSTREAM_SAFE_PREFIX)
181: .append(
182: KualiWorkflowUtils.XSTREAM_MATCH_ANYWHERE_PREFIX)
183: .append(
184: KualiWorkflowUtils
185: .getSourceAccountingLineClassName(docTypeName))
186: .append("/subAccount").append(
187: KualiWorkflowUtils.XSTREAM_SAFE_SUFFIX)
188: .toString();
189: checkValidValues(docContent, xpathExp, "BL", "1031400",
190: null, "BLDG");
191: }
192: }
193:
194: private void checkValidValues(DocumentContent docContent,
195: String xpathExp, String chart, String account, String org,
196: String subAccount) throws XPathExpressionException {
197: XPath xpath = KualiWorkflowUtils.getXPath(docContent
198: .getDocument());
199: NodeList nodes = (NodeList) xpath.evaluate(xpathExp, docContent
200: .getDocument(), XPathConstants.NODESET);
201: for (int i = 0; i < nodes.getLength(); i++) {
202: Node subAccountNode = nodes.item(i);
203: String referenceString = xpath.evaluate("@reference",
204: subAccountNode);
205: if (!StringUtils.isEmpty(referenceString)) {
206: subAccountNode = (Node) xpath.evaluate(referenceString,
207: subAccountNode, XPathConstants.NODE);
208: }
209: // we check for blanks below because of the potential for account or org being null as a valid value
210: String accountNbr = null;
211: if (StringUtils.isNotBlank(account)) {
212: accountNbr = xpath
213: .evaluate(
214: KualiWorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX
215: + "accountNumber",
216: subAccountNode);
217: assertEquals("Account number found was invalid",
218: account, accountNbr);
219: }
220: if (StringUtils.isNotBlank(org)) {
221: String orgCd = xpath
222: .evaluate(
223: KualiWorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX
224: + "finReportOrganizationCode",
225: subAccountNode);
226: assertEquals("Org code found was invalid", org, orgCd);
227: }
228: if (StringUtils.isNotBlank(chart)) {
229: String fieldName = "chartOfAccountsCode";
230: if (StringUtils.isBlank(accountNbr)) {
231: fieldName = "financialReportChartCode";
232: }
233: String finCoaCd = xpath
234: .evaluate(
235: KualiWorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX
236: + fieldName, subAccountNode);
237: assertEquals("Chart code found was invalid", chart,
238: finCoaCd);
239: }
240: if (StringUtils.isNotBlank(subAccount)) {
241: String subAccountNbr = xpath
242: .evaluate(
243: KualiWorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX
244: + "subAccountNumber",
245: subAccountNode);
246: assertEquals("Sub account number found was invalid",
247: subAccount, subAccountNbr);
248: }
249: }
250: }
251: }
|