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 testEmptyDocumentContent() throws Exception {
50          DocumentContent content = DocumentContent.Builder.create("1234").build();
51          assertEquals("<"+DOCUMENT_CONTENT + "></"+DOCUMENT_CONTENT+">", content.getFullContent());
52      }
53  
54      @Test public void testDocumentContent() throws Exception {
55          String startContent = "<"+DOCUMENT_CONTENT+">";
56          String endContent = "</"+DOCUMENT_CONTENT+">";
57          String emptyContent1 = startContent+endContent;
58          String emptyContent2 = "<"+DOCUMENT_CONTENT+"/>";
59          
60          WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
61          
62          
63          assertEquals("Content should be empty.", "", document.getApplicationContent());
64          assertEquals("Content should be empty.", "", document.getAttributeContent());
65          assertEquals("Content should be empty.", "", document.getDocumentContent().getSearchableContent());
66          String fullContent = document.getDocumentContent().getFullContent();
67          assertTrue("Invalid content conversion.", fullContent.equals(emptyContent1) || fullContent.equals(emptyContent2));
68          
69          
70          document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
71          
72          document.saveDocumentData();
73          assertNotNull(document.getDocumentId());
74          
75          assertEquals("Incorrect document id.", document.getDocumentId(), document.getDocumentContent().getDocumentId());
76          assertEquals("Content should be empty.", "", document.getApplicationContent());
77          assertEquals("Content should be empty.", "", document.getAttributeContent());
78          assertEquals("Content should be empty.", "", document.getDocumentContent().getSearchableContent());
79          fullContent = document.getDocumentContent().getFullContent();
80          assertTrue("Invalid content conversion.", fullContent.equals(emptyContent1) || fullContent.equals(emptyContent2));
81          
82          DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId());
83          assertTrue("Invalid initial content.", routeHeader.getDocContent().equals(emptyContent1) || routeHeader.getDocContent().equals(emptyContent2));
84          
85          
86          document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
87          String attributeContent = "<attribute1><id value=\"3\"/></attribute1>";
88          String searchableContent = "<searchable1><data>hello</data></searchable1>";
89          DocumentContentUpdate.Builder contentVO = DocumentContentUpdate.Builder.create(document.getDocumentContent());
90          contentVO.setAttributeContent(constructContent(ATTRIBUTE_CONTENT, attributeContent));
91          contentVO.setSearchableContent(constructContent(SEARCHABLE_CONTENT, searchableContent));
92          document.updateDocumentContent(contentVO.build());
93          document.saveDocumentData();
94          
95          document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
96          String expectedContent = startContent+constructContent(ATTRIBUTE_CONTENT, attributeContent)+constructContent(SEARCHABLE_CONTENT, searchableContent)+endContent;
97          fullContent = document.getDocumentContent().getFullContent();
98          assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
99          
100         
101         String testAttributeContent = new TestRuleAttribute().getDocContent();
102         WorkflowAttributeDefinition attributeDefinition = WorkflowAttributeDefinition.Builder.create("TestRuleAttribute").build();
103         document.addAttributeDefinition(attributeDefinition);
104         document.clearAttributeDefinitions();
105         document.saveDocumentData();
106         fullContent = document.getDocumentContent().getFullContent();
107         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
108         
109         
110         document.addAttributeDefinition(attributeDefinition);
111         document.saveDocumentData();
112         fullContent = document.getDocumentContent().getFullContent();
113         expectedContent = startContent+
114             constructContent(ATTRIBUTE_CONTENT, attributeContent+testAttributeContent)+
115             constructContent(SEARCHABLE_CONTENT, searchableContent)+
116             endContent;
117         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
118 
119         
120         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
121         document.addAttributeDefinition(attributeDefinition);
122         document.addAttributeDefinition(attributeDefinition);
123         document.saveDocumentData();
124         fullContent = document.getDocumentContent().getFullContent();
125         expectedContent = startContent+
126             constructContent(ATTRIBUTE_CONTENT, attributeContent+testAttributeContent+testAttributeContent+testAttributeContent)+
127             constructContent(SEARCHABLE_CONTENT, searchableContent)+
128             endContent;
129         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
130         
131         
132         document.clearAttributeContent();
133         expectedContent = startContent+constructContent(SEARCHABLE_CONTENT, searchableContent)+endContent;
134         fullContent = document.getDocumentContent().getFullContent();
135         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
136         
137         document.saveDocumentData();
138         fullContent = document.getDocumentContent().getFullContent();
139         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
140         
141         
142         
143         
144         String myRadContent = "<myRadContent>abcd</myRadContent>";
145         document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
146         DocumentRouteHeaderValue documentValue = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId());
147         documentValue.setDocContent(myRadContent);
148         KEWServiceLocator.getRouteHeaderService().saveRouteHeader(documentValue);
149         
150         document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
151         String expected = startContent+constructContent(APPLICATION_CONTENT, myRadContent)+endContent;
152         fullContent = document.getDocumentContent().getFullContent();
153         assertEquals("Backward compatibility failure.", StringUtils.deleteWhitespace(expected), StringUtils.deleteWhitespace(fullContent));
154     }
155     
156     private String constructContent(String type, String content) {
157         if (content == null) {
158             return "";
159         }
160         return "<"+type+">"+content+"</"+type+">";
161     }
162     
163     
164 
165 
166 
167 
168     @Test public void testDocumentContentConsistency() throws Exception {
169     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
170     	String appContent = "<app>content</app>";
171     	document.setApplicationContent(appContent);
172     	document.saveDocumentData();
173         XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
174     	
175     	
176     	WorkflowDocument document2 = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
177     	XMLAssert.assertXMLEqual(appContent, document2.getApplicationContent());
178     	String appContent2 = "<app>content2</app>";
179     	document2.setApplicationContent(appContent2);
180     	XMLAssert.assertXMLEqual(appContent2, document2.getApplicationContent());
181     	document2.saveDocumentData();
182     	
183     	
184     	XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
185     	
186     	document.saveDocumentData();
187     	XMLAssert.assertXMLEqual(appContent2, document.getApplicationContent());
188     	
189     	
190     	document2.setApplicationContent("<bad>content</bad>");
191     	document2 = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document2.getDocumentId());
192     	XMLAssert.assertXMLEqual(appContent2, document.getApplicationContent());
193     }
194     
195     
196 
197 
198     @Test public void testManualDocumentContentModification() throws Exception {
199     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
200     	document.saveDocumentData();
201     	
202     	
203     	DocumentContent content = KewApiServiceLocator.getWorkflowDocumentService().getDocumentContent(document.getDocumentId());
204     	assertTrue("Should contain default content, was " + content.getFullContent(), KewApiConstants.DEFAULT_DOCUMENT_CONTENT.equals(content.getFullContent()) ||
205     			KewApiConstants.DEFAULT_DOCUMENT_CONTENT2.equals(content.getFullContent()));
206     	
207     	String appContent = "<abcdefg>hijklm n o p</abcdefg>";
208     	DocumentContentUpdate.Builder contentUpdate = DocumentContentUpdate.Builder.create(content);
209     	contentUpdate.setApplicationContent(appContent);
210     	document.updateDocumentContent(contentUpdate.build());
211         document.saveDocumentData();
212     	
213     	
214     	XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
215     	
216     	
217     	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
218     	XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
219     	
220     }
221 }