View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Tests that client interaction with document content behaves approriately.
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
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          // test no content prior to server creation
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          // test content after server creation
65          document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
66          // this will create the document on the server
67          document.saveDocumentData();
68          assertNotNull(document.getDocumentId());
69          // the document id on the document content should be there now
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          // verify the content on the actual document stored in the database
77          DocumentRouteHeaderValue routeHeader = KEWServiceLocator.getRouteHeaderService().getRouteHeader(document.getDocumentId());
78          assertTrue("Invalid initial content.", routeHeader.getDocContent().equals(emptyContent1) || routeHeader.getDocContent().equals(emptyContent2));
79          
80          // test simple case, no attributes
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          // now reload the document
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          // now, add an attribute and then clear it, document content should remain the same
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         // now really add an attribute and save the content
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         // let's reload the document and try appending a couple of attributes for good measure, this will test appending to existing content on non-materialized document content
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         // now let's try clearing the attribute content
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         // now save it and make sure it comes back from the server the same way
132         document.saveDocumentData();
133         fullContent = document.getDocumentContent().getFullContent();
134         assertEquals("Invalid content conversion.", StringUtils.deleteWhitespace(expectedContent), StringUtils.deleteWhitespace(fullContent));
135         
136         // Test backward compatibility with old-school document content, this document content could look something
137         // like <myRadContent>abcd</myRadContent>, when converted to the new form, it should come out like
138         // <documentContent><applicationContent><myRadContent>abcd</myRadContent></applicationContent></documentContent>
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         // reload the client document and check that the XML has been auto-magically converted
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      * Tests that document content is reloaded from the database after every call (such as Approve, etc.)
160      * so as to verify that the document content stored on the WorkflowDocument will not go stale in between
161      * calls.
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     	// load the document and modify the content
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     	// the original document should not notice these changes yet
179     	XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
180     	// but if we saveRoutingData, we should see the new value
181     	document.saveDocumentData();
182     	XMLAssert.assertXMLEqual(appContent2, document.getApplicationContent());
183     	
184     	// also verify that just setting the content, but not saving it, doesn't get persisted
185     	document2.setApplicationContent("<bad>content</bad>");
186     	document2 = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document2.getDocumentId());
187     	XMLAssert.assertXMLEqual(appContent2, document.getApplicationContent());
188     }
189     
190     /**
191      * Tests modification of the DocumentContentVO object directly.
192      */
193     @Test public void testManualDocumentContentModification() throws Exception {
194     	WorkflowDocument document = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("ewestfal"), "TestDocumentType");
195     	document.saveDocumentData();
196     	
197     	// fetch it from WorkflowInfo
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     	// test that the content on the document is the same as the content we just set
209     	XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
210     	
211     	// fetch the document fresh and make sure the content is correct
212     	document = WorkflowDocumentFactory.loadDocument(getPrincipalIdForName("ewestfal"), document.getDocumentId());
213     	XMLAssert.assertXMLEqual(appContent, document.getApplicationContent());
214     	
215     }
216 }