1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.clientapp;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertTrue;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.custommonkey.xmlunit.XMLAssert;
24 import org.junit.Test;
25 import org.kuali.rice.kew.api.KewApiServiceLocator;
26 import org.kuali.rice.kew.api.WorkflowDocument;
27 import org.kuali.rice.kew.api.WorkflowDocumentFactory;
28 import org.kuali.rice.kew.api.document.DocumentContent;
29 import org.kuali.rice.kew.api.document.DocumentContentUpdate;
30 import org.kuali.rice.kew.api.document.attribute.WorkflowAttributeDefinition;
31 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
32 import org.kuali.rice.kew.rule.TestRuleAttribute;
33 import org.kuali.rice.kew.service.KEWServiceLocator;
34 import org.kuali.rice.kew.test.KEWTestCase;
35 import org.kuali.rice.kew.api.KewApiConstants;
36
37
38
39
40
41
42 public class DocumentContentTest extends KEWTestCase {
43
44 private static final String DOCUMENT_CONTENT = KewApiConstants.DOCUMENT_CONTENT_ELEMENT;
45 private static final String ATTRIBUTE_CONTENT = KewApiConstants.ATTRIBUTE_CONTENT_ELEMENT;
46 private static final String SEARCHABLE_CONTENT = KewApiConstants.SEARCHABLE_CONTENT_ELEMENT;
47 private static final String APPLICATION_CONTENT = KewApiConstants.APPLICATION_CONTENT_ELEMENT;
48
49 @Test public void testDocumentContent() throws Exception {
50 String startContent = "<"+DOCUMENT_CONTENT+">";
51 String endContent = "</"+DOCUMENT_CONTENT+">";
52 String emptyContent1 = startContent+endContent;
53 String emptyContent2 = "<"+DOCUMENT_CONTENT+"/>";
54
55 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
56
57
58 assertEquals("Content should be empty.", "", document.getApplicationContent());
59 assertEquals("Content should be empty.", "", document.getAttributeContent());
60 assertEquals("Content should be empty.", "", document.getDocumentContent().getSearchableContent());
61 String fullContent = document.getDocumentContent().getFullContent();
62 assertTrue("Invalid content conversion.", fullContent.equals(emptyContent1) || fullContent.equals(emptyContent2));
63
64
65 document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
66
67 document.saveDocumentData();
68 assertNotNull(document.getDocumentId());
69
70 assertEquals("Incorrect document id.", document.getDocumentId(), document.getDocumentContent().getDocumentId());
71 assertEquals("Content should be empty.", "", document.getApplicationContent());
72 assertEquals("Content should be empty.", "", document.getAttributeContent());
73 assertEquals("Content should be empty.", "", document.getDocumentContent().getSearchableContent());
74 fullContent = document.getDocumentContent().getFullContent();
75 assertTrue("Invalid content conversion.", fullContent.equals(emptyContent1) || fullContent.equals(emptyContent2));
76
77 DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId());
78 assertTrue("Invalid initial content.", routeHeader.getDocContent().equals(emptyContent1) || routeHeader.getDocContent().equals(emptyContent2));
79
80
81 document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
82 String attributeContent = "<attribute1><id value=\"3\"/></attribute1>";
83 String searchableContent = "<searchable1><data>hello</data></searchable1>";
84 DocumentContentUpdate.Builder contentVO = DocumentContentUpdate.Builder.create(document.getDocumentContent());
85 contentVO.setAttributeContent(constructContent(ATTRIBUTE_CONTENT, attributeContent));
86 contentVO.setSearchableContent(constructContent(SEARCHABLE_CONTENT, searchableContent));
87 document.updateDocumentContent(contentVO.build());
88 document.saveDocumentData();
89
90 document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
91 String expectedContent = startContent+constructContent(ATTRIBUTE_CONTENT, attributeContent)+constructContent(SEARCHABLE_CONTENT, searchableContent)+endContent;
92 fullContent = document.getDocumentContent().getFullContent();
93 assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
94
95
96 String testAttributeContent = new TestRuleAttribute().getDocContent();
97 WorkflowAttributeDefinition attributeDefinition = WorkflowAttributeDefinition.Builder.create("TestRuleAttribute").build();
98 document.addAttributeDefinition(attributeDefinition);
99 document.clearAttributeDefinitions();
100 document.saveDocumentData();
101 fullContent = document.getDocumentContent().getFullContent();
102 assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
103
104
105 document.addAttributeDefinition(attributeDefinition);
106 document.saveDocumentData();
107 fullContent = document.getDocumentContent().getFullContent();
108 expectedContent = startContent+
109 constructContent(ATTRIBUTE_CONTENT, attributeContent+testAttributeContent)+
110 constructContent(SEARCHABLE_CONTENT, searchableContent)+
111 endContent;
112 assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
113
114
115 document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
116 document.addAttributeDefinition(attributeDefinition);
117 document.addAttributeDefinition(attributeDefinition);
118 document.saveDocumentData();
119 fullContent = document.getDocumentContent().getFullContent();
120 expectedContent = startContent+
121 constructContent(ATTRIBUTE_CONTENT, attributeContent+testAttributeContent+testAttributeContent+testAttributeContent)+
122 constructContent(SEARCHABLE_CONTENT, searchableContent)+
123 endContent;
124 assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
125
126
127 document.clearAttributeContent();
128 expectedContent = startContent+constructContent(SEARCHABLE_CONTENT, searchableContent)+endContent;
129 fullContent = document.getDocumentContent().getFullContent();
130 assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
131
132 document.saveDocumentData();
133 fullContent = document.getDocumentContent().getFullContent();
134 assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
135
136
137
138
139 String myRadContent = "<myRadContent>abcd</myRadContent>";
140 document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
141 DocumentRouteHeaderValue documentValue = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId());
142 documentValue.setDocContent(myRadContent);
143 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(documentValue);
144
145 document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
146 String expected = startContent+constructContent(APPLICATION_CONTENT, myRadContent)+endContent;
147 fullContent = document.getDocumentContent().getFullContent();
148 assertEquals("Backward compatibility failure.", StringUtils.deleteWhitespace(expected), StringUtils.deleteWhitespace(fullContent));
149 }
150
151 private String constructContent(String type, String content) {
152 if (content == null) {
153 return "";
154 }
155 return "<"+type+">"+content+"</"+type+">";
156 }
157
158
159
160
161
162
163 @Test public void testDocumentContentConsistency() throws Exception {
164 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
165 String appContent = "<app>content</app>";
166 document.setApplicationContent(appContent);
167 document.saveDocumentData();
168 XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
169
170
171 WorkflowDocument document2 = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
172 XMLAssert.assertXMLEqual(appContent, document2.getApplicationContent());
173 String appContent2 = "<app>content2</app>";
174 document2.setApplicationContent(appContent2);
175 XMLAssert.assertXMLEqual(appContent2, document2.getApplicationContent());
176 document2.saveDocumentData();
177
178
179 XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
180
181 document.saveDocumentData();
182 XMLAssert.assertXMLEqual(appContent2, document.getApplicationContent());
183
184
185 document2.setApplicationContent("<bad>content</bad>");
186 document2 = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document2.getDocumentId());
187 XMLAssert.assertXMLEqual(appContent2, document.getApplicationContent());
188 }
189
190
191
192
193 @Test public void testManualDocumentContentModification() throws Exception {
194 WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
195 document.saveDocumentData();
196
197
198 DocumentContent content = KewApiServiceLocator.getWorkflowDocumentService().getDocumentContent(document.getDocumentId());
199 assertTrue("Should contain default content, was " + content.getFullContent(), KewApiConstants.DEFAULT_DOCUMENT_CONTENT.equals(content.getFullContent()) ||
200 KewApiConstants.DEFAULT_DOCUMENT_CONTENT2.equals(content.getFullContent()));
201
202 String appContent = "<abcdefg>hijklm n o p</abcdefg>";
203 DocumentContentUpdate.Builder contentUpdate = DocumentContentUpdate.Builder.create(content);
204 contentUpdate.setApplicationContent(appContent);
205 document.updateDocumentContent(contentUpdate.build());
206 document.saveDocumentData();
207
208
209 XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
210
211
212 document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
213 XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
214
215 }
216 }