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.document.DocumentBase;
23  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
24  import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase;
25  import org.kuali.rice.krad.rules.rule.event.SaveDocumentEvent;
26  import org.kuali.rice.krad.test.KRADTestCase;
27  import org.kuali.rice.krad.test.document.AccountRequestDocument;
28  import org.kuali.rice.krad.test.document.RuleEventImpl;
29  import org.kuali.rice.krad.test.document.bo.Account;
30  import org.kuali.rice.krad.util.ErrorMessage;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.MessageMap;
33  
34  import java.util.Arrays;
35  import java.util.List;
36  
37  import static org.junit.Assert.*;
38  
39  /**
40   * This class tests the DocumentService (currently only getNewDocument is tested).
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  public class DocumentServiceTest extends KRADTestCase {
46  
47      public DocumentServiceTest() {
48      }
49  
50      @Override
51      public void setUp() throws Exception {
52          super.setUp();
53          GlobalVariables.setMessageMap(new MessageMap());
54          GlobalVariables.setUserSession(new UserSession("quickstart"));
55      }
56  
57      @Override
58      public void tearDown() throws Exception {
59          GlobalVariables.setMessageMap(new MessageMap());
60          GlobalVariables.setUserSession(null);
61          super.tearDown();
62      }
63  
64      /**
65       * This method tests getNewDocument
66       *
67       * @throws Exception
68       */
69      @Test public void testGetNewDocument() throws Exception {
70          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest");
71          WorkflowDocument wd =  travelDocument.getDocumentHeader().getWorkflowDocument();
72  
73          assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(),
74                  GlobalVariables.getUserSession().getPerson().getPrincipalId());
75      }
76  
77      /**
78       * This method tests getNewDocument but the initiator is not the current user
79       *
80       * @throws Exception
81       */
82      @Test public void testGetNewDocumentDifferentInitiatorThanCurrentUser() throws Exception {
83          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "testuser1");
84          WorkflowDocument wd =  travelDocument.getDocumentHeader().getWorkflowDocument();
85  
86          assertEquals("Initiator should be testuser1", wd.getInitiatorPrincipalId(), "testuser1");
87      }
88  
89      /**
90       * This method tests getNewDocument but the initiator is invalid
91       *
92       * @throws Exception
93       */
94      @Test public void testGetNewDocumentInvalidInitiator() throws Exception {
95          AccountRequestDocument travelDocument = (AccountRequestDocument) KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountRequest", "notValidUserAtAll");
96          WorkflowDocument wd =  travelDocument.getDocumentHeader().getWorkflowDocument();
97  
98          assertEquals("Initiator should be the current user", wd.getInitiatorPrincipalId(), GlobalVariables.getUserSession().getPerson().getPrincipalId());
99      }
100 
101     /**
102      * tests saveDocument, in particular, the save document rule event and the custom rule method
103      * invocation of the business rule associated with the document.
104      *
105      * @throws Exception
106      */
107     @Test
108     public void testSaveDocument_DocumentEvent() throws Exception {
109         MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb
110                 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" );
111 
112         Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject();
113 
114         SaveDocumentEvent documentEvent = new SaveDocumentEvent( maintenanceDocument );
115         documentEvent.setName( "DocumentControllerBaseSaveDocumentRuleTest#testSave_SaveDocumentEvent()" );
116         documentEvent.setRuleMethodName( "processEvent" );
117 
118         Document savedDocument = KRADServiceLocatorWeb.getDocumentService()
119                 .saveDocument(maintenanceDocument, documentEvent);
120 
121         assertNull( "New maintenance document should not have a version number yet.",
122                 maintenanceDocument.getDocumentHeader().getVersionNumber() );
123         assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() );
124 
125         List<ErrorMessage> msgs = GlobalVariables.getMessageMap().getInfoMessagesForProperty( documentEvent.getName() );
126 
127         assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() );
128         assertEquals( "The message set by the business rule must match the test message.",
129                 documentEvent.getRuleMethodName() + "()", msgs.get(0).toString() );
130     }
131 
132     /**
133      * tests saveDocument, in particular, the save document rule event and the default rule method
134      * invocation of the business rule associated with the document.
135      *
136      * @throws Exception
137      */
138     @Test
139     public void testSaveDocument_Default() throws Exception {
140         MaintenanceDocument maintenanceDocument = ( MaintenanceDocument ) KRADServiceLocatorWeb
141                 .getDocumentService().getNewDocument( "AccountMaintenanceDocument" );
142 
143         Account account = ( Account ) maintenanceDocument.getNewMaintainableObject().getDataObject();
144 
145         RuleEventImpl documentEvent = new RuleEventImpl( maintenanceDocument );
146         documentEvent.setName("DocumentControllerBaseSaveDocumentRuleTest#testSave_Default()");
147 
148         Document savedDocument = KRADServiceLocatorWeb.getDocumentService()
149                 .saveDocument(maintenanceDocument, documentEvent);
150 
151         assertNull( "New maintenance document should not have a version number yet.",
152                 maintenanceDocument.getDocumentHeader().getVersionNumber() );
153         assertNotNull( "Saved maintenance document must have a version number.", savedDocument.getDocumentHeader().getVersionNumber() );
154 
155         List<ErrorMessage> msgs = GlobalVariables.getMessageMap()
156                 .getInfoMessagesForProperty( documentEvent.getClass().getName() );
157 
158         assertEquals( "There must be one entry added by the business rule method.", 1, msgs.size() );
159         assertEquals( "The message set by the business rule must match the test message.",
160                 "org.kuali.rice.krad.test.document.AccountRules" + "()", msgs.get( 0 ).toString() );
161     }
162 
163     /**
164      * Tests getting documents by a list of document numbers.
165      *
166      * @throws Exception for any test issues
167      */
168     @Test
169     public void testGetDocumentsByListOfDocumentHeaderIds_Default() throws Exception {
170         Document document1 = KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountMaintenanceDocument");
171         Document document2 = KRADServiceLocatorWeb.getDocumentService().getNewDocument("AccountMaintenanceDocument");
172 
173         RuleEventImpl documentEvent1 = new RuleEventImpl(document1);
174         documentEvent1.setName("saving document 1");
175         RuleEventImpl documentEvent2 = new RuleEventImpl(document2);
176         documentEvent2.setName("saving document 2");
177 
178         Document savedDocument1 = KRADServiceLocatorWeb.getDocumentService().saveDocument(document1, documentEvent1);
179         Document savedDocument2 = KRADServiceLocatorWeb.getDocumentService().saveDocument(document2, documentEvent2);
180 
181         String documentNumber1 = savedDocument1.getDocumentNumber();
182         String documentNumber2 = savedDocument2.getDocumentNumber();
183 
184         List<Document> documents = KRADServiceLocatorWeb.getDocumentService().getDocumentsByListOfDocumentHeaderIds(
185                 MaintenanceDocumentBase.class, Arrays.asList(documentNumber1, documentNumber2));
186 
187         assertEquals("wrong number of documents found", 2, documents.size());
188 
189         for (Document document : documents) {
190             assertNotNull("document was null", document);
191         }
192     }
193 
194     /**
195      * Tests getting documents by a list of document numbers which don't exist.
196      *
197      * @throws Exception for any test issues
198      */
199     @Test
200     public void testGetDocumentsByListOfDocumentHeaderIds_MissingDocuments() throws Exception {
201         String fakeDocumentNumber1 = "1234";
202         String fakeDocumentNumber2 = "5678";
203 
204         List<Document> documents = KRADServiceLocatorWeb.getDocumentService().getDocumentsByListOfDocumentHeaderIds(
205                 MaintenanceDocumentBase.class, Arrays.asList(fakeDocumentNumber1, fakeDocumentNumber2));
206 
207         assertTrue("documents found", documents.isEmpty());
208     }
209 
210 }