1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.workflow;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.kuali.rice.core.api.impex.xml.XmlConstants;
23 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24 import org.kuali.rice.core.api.reflect.ObjectDefinition;
25 import org.kuali.rice.kew.api.KewApiConstants;
26 import org.kuali.rice.kew.api.extension.ExtensionDefinition;
27 import org.kuali.rice.kew.rule.bo.RuleAttribute;
28 import org.kuali.rice.kew.rule.xmlrouting.XPathHelper;
29 import org.kuali.rice.krad.datadictionary.AttributeDefinition;
30 import org.kuali.rice.krad.datadictionary.BusinessObjectEntry;
31 import org.kuali.rice.krad.datadictionary.DocumentEntry;
32 import org.kuali.rice.krad.service.DataDictionaryService;
33 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
34 import org.kuali.rice.krad.workflow.attribute.KualiXmlAttribute;
35 import org.kuali.rice.krad.workflow.attribute.KualiXmlAttributeHelper;
36 import org.kuali.rice.krad.workflow.attribute.KualiXmlRuleAttributeImpl;
37 import org.kuali.rice.krad.workflow.attribute.KualiXmlSearchableAttributeImpl;
38 import org.kuali.test.KRADTestCase;
39 import org.w3c.dom.NamedNodeMap;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.NodeList;
42 import org.xml.sax.InputSource;
43
44 import javax.xml.transform.Result;
45 import javax.xml.transform.Source;
46 import javax.xml.transform.TransformerException;
47 import javax.xml.transform.TransformerFactory;
48 import javax.xml.transform.dom.DOMSource;
49 import javax.xml.transform.stream.StreamResult;
50 import javax.xml.xpath.XPath;
51 import javax.xml.xpath.XPathConstants;
52 import java.io.StringReader;
53 import java.io.StringWriter;
54 import java.util.HashSet;
55 import java.util.Set;
56
57 import static org.junit.Assert.*;
58
59
60
61
62
63
64
65
66 @Ignore
67 public class KualiXMLAttributeImplTest extends KRADTestCase {
68 private static Log LOG = LogFactory.getLog(KualiXMLAttributeImplTest.class);
69
70 private static final String RULE_ATTRIBUTE_CONFIG_NODE_NAME = XmlConstants.ROUTING_CONFIG;
71 private static final String SEARCH_ATTRIBUTE_CONFIG_NODE_NAME = XmlConstants.SEARCHING_CONFIG;
72
73 XPath myXPath = XPathHelper.newXPath();
74 String ruleAttributeXml = "";
75 String searchAttributeXml = "";
76
77
78 @Override
79 public void setUp() throws Exception {
80 super.setUp();
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 }
166
167
168
169
170 @Test public void testConfirmLabels() {
171 boolean failed = false;
172
173 failed |= confirmLabels(KualiXmlAttributeHelper.notFound, ruleAttributeXml, RULE_ATTRIBUTE_CONFIG_NODE_NAME);
174
175
176 failed |= confirmLabels(KualiXmlAttributeHelper.notFound, searchAttributeXml, SEARCH_ATTRIBUTE_CONFIG_NODE_NAME);
177
178 assertFalse("At least one label was incorrect", failed);
179 }
180
181
182
183
184
185
186
187
188
189
190
191 private Node configureRuleAttribute(Node xmlNode, KualiXmlAttribute myAttribute) throws TransformerException {
192 ExtensionDefinition.Builder extensionDefinition = ExtensionDefinition.Builder.create("fakeName", "fakeType", "fakeResourceDescriptor");
193
194 StringWriter xmlBuffer = new StringWriter();
195 Source source = new DOMSource(xmlNode);
196 Result result = new StreamResult(xmlBuffer);
197 TransformerFactory.newInstance().newTransformer().transform(source, result);
198
199 extensionDefinition.getConfiguration().put(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, new String(xmlBuffer.getBuffer()));
200
201 if (LOG.isDebugEnabled()) {
202 LOG.debug("This is the XML that was added to the attribute");
203 LOG.debug(new String(xmlBuffer.getBuffer()));
204 StringWriter xmlBuffer2 = new StringWriter();
205 Source source2 = new DOMSource(xmlNode);
206 Result result2 = new StreamResult(xmlBuffer2);
207 TransformerFactory.newInstance().newTransformer().transform(source2, result2);
208 LOG.debug("This is the XML that was returned from the ruleAttribute");
209 LOG.debug(new String(xmlBuffer2.getBuffer()));
210 }
211 return myAttribute.getConfigXML(extensionDefinition.build());
212 }
213
214
215
216
217
218
219
220
221
222
223
224 private boolean confirmLabels(String testString, String attributeXml, String configNodeName) {
225 boolean testFailed = false;
226 String theTitle = "";
227 String theName = "";
228 String attributeName = "";
229 try {
230 NodeList tempList = (NodeList) myXPath.evaluate("//ruleAttribute", new InputSource(new StringReader(attributeXml)), XPathConstants.NODESET);
231 for (int i = 0; i < tempList.getLength(); i++) {
232 Node originalNode = tempList.item(i);
233 Set ruleAttributeFieldDefNames = new HashSet();
234 Set ruleAttributeFieldDefTitles = new HashSet();
235 attributeName = (String) myXPath.evaluate(WorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX + "name", originalNode, XPathConstants.STRING);
236 Node classNameNode = (Node) myXPath.evaluate(WorkflowUtils.XSTREAM_MATCH_RELATIVE_PREFIX + "className", originalNode, XPathConstants.NODE);
237 if ((classNameNode != null) && (classNameNode.getFirstChild() != null)) {
238 if (LOG.isInfoEnabled()) {
239 LOG.info("Checking attribute with name '" + attributeName + "'");
240 }
241 KualiXmlAttribute myAttribute = (KualiXmlAttribute) GlobalResourceLoader.getObject(new ObjectDefinition(classNameNode.getFirstChild().getNodeValue()));
242 Node xmlNode = configureRuleAttribute(originalNode, myAttribute);
243 NamedNodeMap fieldDefAttributes = null;
244 String potentialFailMessage = "";
245
246 try {
247 NodeList xmlNodeList = (NodeList) myXPath.evaluate("//fieldDef", xmlNode, XPathConstants.NODESET);
248
249 for (int j = 0; j < xmlNodeList.getLength(); j++) {
250 Node fieldDefXmlNode = xmlNodeList.item(j);
251 fieldDefAttributes = fieldDefXmlNode.getAttributes();
252
253 theTitle = fieldDefAttributes.getNamedItem("title").getNodeValue();
254 theName = fieldDefAttributes.getNamedItem("name").getNodeValue();
255 if (LOG.isDebugEnabled()) {
256 LOG.debug(attributeName);
257 LOG.debug("name=" + theName + " title=" + theTitle);
258 }
259 if (ruleAttributeFieldDefNames.contains(theName)) {
260
261 potentialFailMessage = "Each fieldDef name on a single attribute must be unique and the fieldDef name '" + theName + "' already exists on the attribute '" + attributeName + "'";
262 fail(potentialFailMessage);
263 }
264 else {
265 ruleAttributeFieldDefNames.add(theName);
266 }
267 if (testString.equals(KualiXmlAttributeHelper.notFound)) {
268 potentialFailMessage = "Each fieldDef title should be a valid value and currently the title for attribute '" + attributeName + "' is '" + theTitle + "'";
269 assertFalse(potentialFailMessage, theTitle.equals(testString));
270 if (ruleAttributeFieldDefTitles.contains(theTitle)) {
271
272
273
274
275
276
277 potentialFailMessage = "Each fieldDef title on a single attribute must be unique and the fieldDef title '" + theTitle + "' already exists on the attribute '" + attributeName + "'";
278 fail(potentialFailMessage);
279 }
280 else {
281 ruleAttributeFieldDefTitles.add(theTitle);
282 }
283 }
284 else {
285 potentialFailMessage = "For attribute '" + attributeName + "' the title should have been '" + testString + "' but was actually '" + theTitle + "'";
286 assertEquals(potentialFailMessage, testString, theTitle);
287 }
288 }
289 }
290 catch (AssertionError afe) {
291 LOG.warn("Assertion Failed for attribute '" + attributeName + "' with error " + potentialFailMessage, afe);
292 testFailed = true;
293 }
294 finally {
295 attributeName = "";
296 }
297 }
298 else {
299 throw new RuntimeException("Could not find class for attribute named '" + attributeName + "'");
300 }
301 }
302 }
303 catch (Exception e) {
304 LOG.error("General Exception thrown for attribute '" + attributeName + "'", e);
305 testFailed = true;
306 }
307 return testFailed;
308 }
309
310
311
312
313
314
315
316
317 @Test public void testLabelSource() {
318 DataDictionaryService myDDService = KRADServiceLocatorWeb.getDataDictionaryService();
319 XPath xpath = XPathHelper.newXPath();
320 String nonsenseString = "BananaRama";
321 for (Object tempEntity : myDDService.getDataDictionary().getBusinessObjectEntries().values()) {
322
323 for ( AttributeDefinition attribute : ((BusinessObjectEntry) tempEntity).getAttributes() ) {
324 attribute.setLabel(nonsenseString);
325 attribute.setShortLabel(nonsenseString);
326 }
327
328 }
329 for (Object tempEntity : myDDService.getDataDictionary().getDocumentEntries().values()) {
330
331 for ( AttributeDefinition attribute : ((DocumentEntry) tempEntity).getAttributes() ) {
332 attribute.setLabel(nonsenseString);
333 attribute.setShortLabel(nonsenseString);
334 }
335
336 }
337
338
339 boolean failed = false;
340 assertFalse("At least one label was incorrect", failed);
341
342 failed |= confirmLabels(nonsenseString, ruleAttributeXml, RULE_ATTRIBUTE_CONFIG_NODE_NAME);
343
344
345 failed |= confirmLabels(nonsenseString, searchAttributeXml, SEARCH_ATTRIBUTE_CONFIG_NODE_NAME);
346
347 assertFalse("At least one label was incorrect", failed);
348 }
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367 }