View Javadoc
1   /**
2    * Copyright 2005-2014 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.krad.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.kew.api.WorkflowDocument;
20  import org.kuali.rice.krad.UserSession;
21  import org.kuali.rice.krad.document.Document;
22  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
23  import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
24  import org.kuali.rice.krad.test.KRADTestCase;
25  import org.kuali.rice.krad.test.document.AccountRequestDocument;
26  import org.kuali.rice.krad.test.document.RuleEventImpl;
27  import org.kuali.rice.krad.test.document.bo.Account;
28  import org.kuali.rice.krad.util.ErrorMessage;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.MessageMap;
31  
32  import java.util.List;
33  
34  import static org.junit.Assert.*;
35  
36  /**
37   * This class tests the DocumentService (currently only getNewDocument is tested).
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class DocumentServiceTest extends KRADTestCase {
43  
44      public DocumentServiceTest() {
45      }
46  
47      @Override
48      public void setUp() throws Exception {
49          super.setUp();
50          GlobalVariables.setMessageMap(new MessageMap());
51          GlobalVariables.setUserSession(new UserSession("quickstart"));
52      }
53  
54      @Override
55      public void tearDown() throws Exception {
56          GlobalVariables.setMessageMap(new MessageMap());
57          GlobalVariables.setUserSession(null);
58          super.tearDown();
59      }
60  
61      /**
62       * This method tests getNewDocument
63       *
64       * @throws Exception
65       */
66      @Test public void testGetNewDocument() throws Exception {
67          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
68          WorkflowDocument wd =  travelDocument.getDocumentHeader().getWorkflowDocument();
69  
70          assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(),
71                  GlobalVariables.getUserSession().getPerson().getPrincipalId());
72      }
73  
74      /**
75       * This method tests getNewDocument but the initiator is not the current user
76       *
77       * @throws Exception
78       */
79      @Test public void testGetNewDocumentDifferentInitiatorThanCurrentUser() throws Exception {
80          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "testuser1");
81          WorkflowDocument wd =  travelDocument.getDocumentHeader().getWorkflowDocument();
82  
83          assertEquals("Initiator should be testuser1", wd.getInitiatorPrincipalId(), "testuser1");
84      }
85  
86      /**
87       * This method tests getNewDocument but the initiator is invalid
88       *
89       * @throws Exception
90       */
91      @Test public void testGetNewDocumentInvalidInitiator() throws Exception {
92          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "notValidUserAtAll");
93          WorkflowDocument wd =  travelDocument.getDocumentHeader().getWorkflowDocument();
94  
95          assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(), GlobalVariables.getUserSession().getPerson().getPrincipalId());
96      }
97  
98      /**
99       * tests saveDocument, in particular, the save document rule event and the custom rule method
100      * invocation of the business rule associated with the document.
101      *
102      * @throws Exception
103      */
104     @Test
105     public void testSaveDocument_DocumentEvent() throws Exception {
106         MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb
107                 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" );
108 
109         Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject();
110 
111         SaveDocumentEvent documentEvent = new SaveDocumentEvent( maintenanceDocument );
112         documentEvent.setName( "DocumentControllerBaseSaveDocumentRuleTest#testSave_SaveDocumentEvent()" );
113         documentEvent.setRuleMethodName( "processEvent" );
114 
115         Document savedDocument = KRADServiceLocatorWeb.getDocumentService()
116                 .saveDocument(maintenanceDocument, documentEvent);
117 
118         assertNull( "New maintenance document should not have a version number yet.",
119                 maintenanceDocument.getDocumentHeader().getVersionNumber() );
120         assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() );
121 
122         List<ErrorMessage> msgs = GlobalVariables.getMessageMap().getInfoMessagesForProperty( documentEvent.getName() );
123 
124         assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() );
125         assertEquals( "The message set by the business rule must match the test message.",
126                 documentEvent.getRuleMethodName() + "()", msgs.get(0).toString() );
127     }
128 
129     /**
130      * tests saveDocument, in particular, the save document rule event and the default rule method
131      * invocation of the business rule associated with the document.
132      *
133      * @throws Exception
134      */
135     @Test
136     public void testSaveDocument_Default() throws Exception {
137         MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb
138                 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" );
139 
140         Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject();
141 
142         RuleEventImpl documentEvent = new RuleEventImpl( maintenanceDocument );
143         documentEvent.setName("DocumentControllerBaseSaveDocumentRuleTest#testSave_Default()");
144 
145         Document savedDocument = KRADServiceLocatorWeb.getDocumentService()
146                 .saveDocument(maintenanceDocument, documentEvent);
147 
148         assertNull( "New maintenance document should not have a version number yet.",
149                 maintenanceDocument.getDocumentHeader().getVersionNumber() );
150         assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() );
151 
152         List<ErrorMessage> msgs = GlobalVariables.getMessageMap()
153                 .getInfoMessagesForProperty( documentEvent.getClass().getName() );
154 
155         assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() );
156         assertEquals( "The message set by the business rule must match the test message.",
157                 "org.kuali.rice.krad.test.document.AccountRules" + "()", msgs.get( 0 ).toString() );
158     }
159 }