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.core.rule;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.Set;
025: import java.util.TreeSet;
026:
027: import org.apache.log4j.Logger;
028: import org.kuali.core.bo.Parameter;
029: import org.kuali.core.bo.ParameterDetailType;
030: import org.kuali.core.datadictionary.BusinessObjectEntry;
031: import org.kuali.core.datadictionary.DocumentEntry;
032: import org.kuali.core.datadictionary.HeaderNavigation;
033: import org.kuali.core.datadictionary.HelpDefinition;
034: import org.kuali.core.service.BusinessObjectService;
035: import org.kuali.core.service.DataDictionaryService;
036: import org.kuali.core.util.ObjectUtils;
037: import org.kuali.kfs.context.KualiTestBase;
038: import org.kuali.kfs.context.SpringContext;
039: import org.kuali.kfs.rules.ParameterRule;
040: import org.kuali.kfs.service.ParameterService;
041: import org.kuali.test.ConfigureContext;
042: import org.kuali.test.suite.AnnotationTestSuite;
043: import org.kuali.test.suite.PreCommitSuite;
044:
045: @ConfigureContext
046: @AnnotationTestSuite(PreCommitSuite.class)
047: public class ParameterConfigurationTest extends KualiTestBase {
048: private static final Logger LOG = Logger
049: .getLogger(ParameterConfigurationTest.class);
050:
051: /**
052: *
053: * This method...
054: * @throws Exception
055: */
056: public void testValidateParameterComponents() throws Exception {
057: Collection<Parameter> params = SpringContext.getBean(
058: BusinessObjectService.class).findAll(Parameter.class);
059: ParameterRule paramRule = new ParameterRule();
060: StringBuffer badComponents = new StringBuffer();
061: int failCount = 0;
062: for (Parameter param : params) {
063: if (!paramRule.checkComponent(param)) {
064: badComponents.append("\n").append(
065: param.getParameterNamespaceCode()).append("\t")
066: .append(param.getParameterDetailTypeCode())
067: .append("\t").append(param.getParameterName())
068: .append("\t");
069: failCount++;
070: }
071: }
072: badComponents.insert(0, "The following " + failCount
073: + " parameters have invalid components:");
074: if (failCount > 0) {
075: Set<String> components = new TreeSet<String>();
076: for (ParameterDetailType pdt : SpringContext.getBean(
077: ParameterService.class).getNonDatabaseDetailTypes()) {
078: components.add(pdt.getParameterNamespaceCode() + "/"
079: + pdt.getParameterDetailTypeCode());
080: }
081: for (ParameterDetailType pdt : (Collection<ParameterDetailType>) SpringContext
082: .getBean(BusinessObjectService.class).findAll(
083: ParameterDetailType.class)) {
084: components.add(pdt.getParameterNamespaceCode() + "/"
085: + pdt.getParameterDetailTypeCode());
086: }
087: System.out.println("Valid Components: ");
088: for (String component : components) {
089: System.out.println(component);
090: }
091: }
092: assertTrue(badComponents.toString(), failCount == 0);
093: }
094:
095: /**
096: *
097: * This method retrieves all the help parameters identified in the data dictionary classes and confirms that each parameter
098: * exists in the parameters table. Currently, help parameters are only defined for the following object types:
099: * - Business Objects (defined by <objectHelp> in DD files)
100: * - Maintenance or Transactional Documents (defined by <help> in DD files)
101: * - Document Header Tab Navigation (defined by <pageHelp> in DD files)
102: *
103: *
104: * @throws Exception
105: */
106: public void testAllDataDictionaryReferencedParametersExistInParameterTable()
107: throws Exception {
108: /**
109: * Retrieve all business objects
110: * For each object, check to see if it has a help definition defined. <objectHelp>
111: * - If yes, attempt to pull parameter from DB using values defined in definition
112: * - If no param found, add to error list and move on
113: *
114: * Retrieve all documents
115: * For each document, check to see if it has a help definition defined. <help>
116: * - If yes, attempt to pull the parameter from DB using values in definition
117: * - If no param found, add to error list and move on.
118: * - For each document, check to see if it has a header page help definition defined. <pageHelp>
119: * - - If yes, attempt to pull the parameter from DB using values in definition
120: * - - If no param found, add to error list and move on.
121: *
122: */
123:
124: // Force the datadictionary to load
125: SpringContext.getBean(DataDictionaryService.class)
126: .forceCompleteDataDictionaryLoad();
127:
128: boolean exists = true;
129: List<String> boParamsMissingFromDB = new ArrayList<String>();
130: List<String> documentParamsMissingFromDB = new ArrayList<String>();
131: List<String> documentHeaderParamsMissingFromDB = new ArrayList<String>();
132:
133: int documentHeaderCount = 0;
134:
135: HashSet<BusinessObjectEntry> boEntries = new HashSet<BusinessObjectEntry>(
136: SpringContext.getBean(DataDictionaryService.class)
137: .getDataDictionary().getBusinessObjectEntries()
138: .values());
139: for (BusinessObjectEntry boEntry : boEntries) {
140: HelpDefinition boHelp = boEntry.getHelpDefinition();
141: if (ObjectUtils.isNotNull(boHelp)) {
142: exists = SpringContext.getBean(ParameterService.class)
143: .parameterExists(
144: boEntry.getBusinessObjectClass(),
145: boHelp.getParameterName());
146: if (!exists) {
147: boParamsMissingFromDB
148: .add("Business Object Parameter: "
149: + boHelp.getParameterNamespace()
150: + " - "
151: + boHelp.getParameterName()
152: + " help parameter in "
153: + boEntry.getBusinessObjectClass()
154: .getName()
155: + " is not in the database.");
156: exists = true;
157: }
158: }
159: }
160:
161: // Using HashSet since duplicate objects would otherwise be returned
162: HashSet<DocumentEntry> documentEntries = new HashSet(
163: SpringContext.getBean(DataDictionaryService.class)
164: .getDataDictionary().getDocumentEntries()
165: .values());
166: List<String> ddEntriesWithMissingTypes = new ArrayList<String>();
167: for (DocumentEntry documentEntry : documentEntries) {
168: HelpDefinition docHelp = documentEntry.getHelpDefinition();
169: if (ObjectUtils.isNotNull(docHelp)) {
170: /*
171: Class paramClass = null;
172: if(documentEntry instanceof MaintenanceDocumentEntry) {
173: paramClass = ((MaintenanceDocumentEntry)documentEntry).getBusinessObjectClass();
174: }
175: if(paramClass == null) {
176: paramClass = documentEntry.getDocumentClass();
177: }
178: exists = SpringContext.getBean(ParameterService.class).parameterExists(paramClass, docHelp.getParameterName());
179: */
180:
181: HashMap<String, String> crit = new HashMap<String, String>(
182: 3);
183: crit.put("parameterNamespaceCode", docHelp
184: .getParameterNamespace());
185: crit.put("parameterDetailTypeCode", docHelp
186: .getParameterDetailType());
187: crit.put("parameterName", docHelp.getParameterName());
188: exists = (Parameter) SpringContext.getBean(
189: BusinessObjectService.class).findByPrimaryKey(
190: Parameter.class, crit) != null;
191:
192: if (!exists) {
193: documentParamsMissingFromDB
194: .add("Document Parameter: "
195: + docHelp.getParameterNamespace()
196: + " - "
197: + docHelp.getParameterName()
198: + " help parameter in "
199: + documentEntry
200: .getDocumentTypeName()
201: + " is not in the database.");
202: exists = true;
203: }
204: }
205: List<HeaderNavigation> headerNavigations = Arrays
206: .asList(documentEntry.getHeaderTabNavigation());
207: for (HeaderNavigation headerNav : headerNavigations) {
208: HelpDefinition headerNavHelp = headerNav
209: .getHelpDefinition();
210: if (ObjectUtils.isNotNull(headerNavHelp)) {
211: documentHeaderCount++;
212:
213: HashMap<String, String> crit = new HashMap<String, String>(
214: 3);
215: crit.put("parameterNamespaceCode", headerNavHelp
216: .getParameterNamespace());
217: crit.put("parameterDetailTypeCode", headerNavHelp
218: .getParameterDetailType());
219: crit.put("parameterName", headerNavHelp
220: .getParameterName());
221: exists = (Parameter) SpringContext.getBean(
222: BusinessObjectService.class)
223: .findByPrimaryKey(Parameter.class, crit) != null;
224:
225: if (!exists) {
226: documentHeaderParamsMissingFromDB
227: .add("Document Header Parameter: "
228: + headerNavHelp
229: .getParameterNamespace()
230: + " - "
231: + headerNavHelp
232: .getParameterName()
233: + " help parameter in "
234: + documentEntry
235: .getDocumentTypeName()
236: + " is not in the database.");
237: exists = true;
238: }
239: }
240: }
241: }
242:
243: /**
244: * The below commented out code is useful when debugging the test locally, as it allows the errors to be posted
245: * to general error output stream.
246: */
247:
248: // Collections.sort(boParamsMissingFromDB);
249: // Collections.sort(documentParamsMissingFromDB);
250: // Collections.sort(documentHeaderParamsMissingFromDB);
251: //
252: // LOG.info("======== BO Missing Params ========");
253: // for(String missingParam : boParamsMissingFromDB) {
254: // LOG.info(missingParam);
255: // }
256: //
257: // LOG.info("======== Document Missing Params ========");
258: // for(String missingParam : documentParamsMissingFromDB) {
259: // LOG.info(missingParam);
260: // }
261: //
262: // LOG.info("======== Document Header Missing Params ========");
263: // for(String missingParam : documentHeaderParamsMissingFromDB) {
264: // LOG.info(missingParam);
265: // }
266: //
267: // LOG.info("Total number of business objects reviewed: "+boEntries.size());
268: // LOG.info("Number of missing BO parameters: "+boParamsMissingFromDB.size());
269: // LOG.info("Total number of documents reviewed: "+documentEntries.size());
270: // LOG.info("Number of missing document parameters: "+documentParamsMissingFromDB.size());
271: // LOG.info("Total number of documents headers reviewed: "+documentHeaderCount);
272: // LOG.info("Number of missing document header parameters: "+documentHeaderParamsMissingFromDB.size());
273: List<String> allMissingParams = new ArrayList<String>();
274:
275: allMissingParams.addAll(boParamsMissingFromDB);
276: allMissingParams.addAll(documentParamsMissingFromDB);
277: allMissingParams.addAll(documentHeaderParamsMissingFromDB);
278:
279: assertEquals(
280: "dataDictionaryReferencedParametersExistInParameterTable: "
281: + allMissingParams, 0, allMissingParams.size());
282:
283: // assertEquals("dataDictionaryReferencedParametersExistInParameterTable: " + boParamsMissingFromDB, 0, boParamsMissingFromDB.size());
284: // assertEquals("dataDictionaryReferencedParametersExistInParameterTable: " + documentParamsMissingFromDB, 0, documentParamsMissingFromDB.size());
285: // assertEquals("dataDictionaryReferencedParametersExistInParameterTable: " + documentHeaderParamsMissingFromDB, 0, documentHeaderParamsMissingFromDB.size());
286:
287: }
288:
289: }
|