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