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.krad.service;
17  
18  import org.junit.Test;
19  import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
20  import org.kuali.rice.krad.test.KRADTestCase;
21  
22  import static org.junit.Assert.assertTrue;
23  
24  /**
25   * KualiRuleServiceTest tests {@link KualiRuleService}
26   * <p>
27   * Testing applyRules( event ) has proven to be too strongly dependent on an actual document type, and business rule, and XML
28   * document which binds them together, to be really useful. Instead, we'll test each of the applyRules( rule, event ) methods which
29   * are called by applyRules( event ), since they do the actual work involved in applying a rule.</p>
30   */
31  public class KualiRuleServiceTest extends KRADTestCase {
32      private static boolean entriesAdded = false;
33      private static KualiRuleService kualiRuleService;
34  
35      @Override
36      public void setUp() throws Exception {
37          super.setUp();
38  
39          if (!entriesAdded) {
40              entriesAdded = true;
41              kualiRuleService = KRADServiceLocatorWeb.getKualiRuleService();
42          }
43      }
44  
45      @Test public void testApplyRules_approveDocument_nullEvent() {
46          boolean failedAsExpected = false;
47  
48          ApproveDocumentEvent event = null;
49          try {
50              kualiRuleService.applyRules(event);
51          }
52          catch (IllegalArgumentException e) {
53              failedAsExpected = true;
54          }
55  
56          assertTrue(failedAsExpected);
57      }
58  
59      @Test public void testApplyRules_approveDocument_nullDocument() {
60          boolean failedAsExpected = false;
61  
62          try {
63              ApproveDocumentEvent event = new ApproveDocumentEvent(null);
64  
65              kualiRuleService.applyRules(event);
66          }
67          catch (RuntimeException e) {
68              failedAsExpected = true;
69          }
70  
71          assertTrue(failedAsExpected);
72      }
73  
74  //    private static final String NONE = "none";
75  //    private static final String ROUTE_DOCUMENT = "routeDoc";
76  //    private static final String APPROVE_DOCUMENT = "approveDoc";
77  //    private static final String SAVE_DOCUMENT = "saveDoc";
78  //    private static final String ADD_LINE = "addLine";
79  //    private static final String DELETE_LINE = "deleteLine";
80  //    private static final String UPDATE_LINE = "updateLine";
81  //    private static final String REVIEW_LINE = "reviewLine";
82  //
83  //    @Test public void testApplyRules_approveDocument_validRule_validDocument() {
84  //        RuleMockDocument d = new RuleMockDocument();
85  //        ApproveDocumentEvent event = new ApproveDocumentEvent(d);
86  //
87  //
88  //        assertFalse(d.isProcessed());
89  //        assertEquals(NONE, d.getEventType());
90  //
91  //        boolean success = kualiRuleService.applyRules(event);
92  //        assertTrue(success);
93  //
94  //        assertTrue(d.isProcessed());
95  //        assertEquals(ROUTE_DOCUMENT, d.getPrevEventType());
96  //        assertEquals(APPROVE_DOCUMENT, d.getEventType());
97  //    }
98  //
99  //    @Test public void testApplyRules_approveDocument_validRule_validTransactionalDocument_noAccountingLines() {
100 //        RuleMockDocument t = new RuleMockDocument();
101 //        ApproveDocumentEvent event = new ApproveDocumentEvent(t);
102 //
103 //
104 //        assertFalse(t.isProcessed());
105 //        assertEquals(NONE, t.getEventType());
106 //
107 //        boolean success = kualiRuleService.applyRules(event);
108 //        assertTrue(success);
109 //
110 //        assertTrue(t.isProcessed());
111 //        assertEquals(ROUTE_DOCUMENT, t.getPrevEventType());
112 //        assertEquals(APPROVE_DOCUMENT, t.getEventType());
113 //    }
114 //
115 //    @Test public void testApplyRules_routeDocument_validRule_validDocument() {
116 //        RuleMockDocument d = new RuleMockDocument();
117 //        RouteDocumentEvent event = new RouteDocumentEvent(d);
118 //
119 //
120 //        assertFalse(d.isProcessed());
121 //        assertEquals(NONE, d.getEventType());
122 //
123 //        boolean success = kualiRuleService.applyRules(event);
124 //        assertTrue(success);
125 //
126 //        assertTrue(d.isProcessed());
127 //        assertEquals(SAVE_DOCUMENT, d.getPrevEventType());
128 //        assertEquals(ROUTE_DOCUMENT, d.getEventType());
129 //    }
130 //
131 //    @Test public void testApplyRules_routeDocument_validRule_validTransactionalDocument_noAccountingLines() {
132 //        RuleMockDocument t = new RuleMockDocument();
133 //        RouteDocumentEvent event = new RouteDocumentEvent(t);
134 //
135 //
136 //        assertFalse(t.isProcessed());
137 //        assertEquals(NONE, t.getEventType());
138 //
139 //        boolean success = kualiRuleService.applyRules(event);
140 //        assertTrue(success);
141 //
142 //        assertTrue(t.isProcessed());
143 //        assertEquals(SAVE_DOCUMENT, t.getPrevEventType());
144 //        assertEquals(ROUTE_DOCUMENT, t.getEventType());
145 //    }
146 //
147 //    @Test public void testApplyRules_routeDocument_validRule_validTransactionalDocument_someAccountingLines() throws Exception {
148 //        RuleMockDocument t = new RuleMockDocument();
149 //        t.addSourceAccountingLine(buildSourceLine("1"));
150 //
151 //        RouteDocumentEvent event = new RouteDocumentEvent(t);
152 //
153 //
154 //        assertFalse(t.isProcessed());
155 //        assertEquals(NONE, t.getEventType());
156 //
157 //        boolean success = kualiRuleService.applyRules(event);
158 //        assertTrue(success);
159 //
160 //        assertTrue(t.isProcessed());
161 //        assertEquals(SAVE_DOCUMENT, t.getPrevEventType());
162 //        assertEquals(ROUTE_DOCUMENT, t.getEventType());
163 //    }
164 //
165 //    @Test public void testApplyRules_saveDocument_validRule_validDocument() {
166 //        RuleMockDocument d = new RuleMockDocument();
167 //        SaveDocumentEvent event = new SaveDocumentEvent(d);
168 //
169 //
170 //        assertFalse(d.isProcessed());
171 //        assertEquals(NONE, d.getEventType());
172 //
173 //        boolean success = kualiRuleService.applyRules(event);
174 //        assertTrue(success);
175 //
176 //        assertTrue(d.isProcessed());
177 //        assertEquals(NONE, d.getPrevEventType());
178 //        assertEquals(SAVE_DOCUMENT, d.getEventType());
179 //    }
180 //
181 //    @Test public void testApplyRules_saveDocument_validRule_validTransactionalDocument_noAccountingLines() {
182 //        RuleMockDocument t = new RuleMockDocument();
183 //        SaveDocumentEvent event = new SaveDocumentEvent(t);
184 //
185 //
186 //        assertFalse(t.isProcessed());
187 //        assertEquals(NONE, t.getEventType());
188 //
189 //        boolean success = kualiRuleService.applyRules(event);
190 //        assertTrue(success);
191 //
192 //        assertTrue(t.isProcessed());
193 //        assertEquals(NONE, t.getPrevEventType());
194 //        assertEquals(SAVE_DOCUMENT, t.getEventType());
195 //    }
196 //
197 //    @Test public void testApplyRules_saveDocument_validRule_validTransactionalDocument_someAccountingLines() throws Exception {
198 //        RuleMockDocument t = new RuleMockDocument();
199 //        t.addSourceAccountingLine(buildSourceLine("1"));
200 //
201 //        SaveDocumentEvent event = new SaveDocumentEvent(t);
202 //
203 //
204 //        assertFalse(t.isProcessed());
205 //        assertEquals(NONE, t.getEventType());
206 //
207 //        boolean success = kualiRuleService.applyRules(event);
208 //        assertTrue(success);
209 //
210 //        assertTrue(t.isProcessed());
211 //        assertEquals(ADD_LINE, t.getPrevEventType());
212 //        assertEquals(SAVE_DOCUMENT, t.getEventType());
213 //    }
214 //
215 //    @Test public void testApplyRules_addAccountingLine_validRule_nullDocument() {
216 //        boolean failedAsExpected = false;
217 //
218 //        AddAccountingLineEvent event = new AddAccountingLineEvent("test", null, null);
219 //        try {
220 //            kualiRuleService.applyRules(event);
221 //        }
222 //        catch (IllegalArgumentException e) {
223 //            failedAsExpected = true;
224 //        }
225 //
226 //        assertTrue(failedAsExpected);
227 //    }
228 //
229 //    @Test public void testApplyRules_addAccountingLine_validRule_validTransactionalDocument_nullAccountingLine() {
230 //        boolean failedAsExpected = false;
231 //
232 //        AddAccountingLineEvent event = new AddAccountingLineEvent("test", new RuleMockDocument(), null);
233 //        try {
234 //            kualiRuleService.applyRules(event);
235 //        }
236 //        catch (IllegalArgumentException e) {
237 //            failedAsExpected = true;
238 //        }
239 //
240 //        assertTrue(failedAsExpected);
241 //    }
242 //
243 //    @Test public void testApplyRules_addAccountingLine_validRule_validTransactionalDocument_validAccountingLine() throws Exception {
244 //        RuleMockDocument t = new RuleMockDocument();
245 //        SourceAccountingLine s = buildSourceLine("1");
246 //        t.addSourceAccountingLine(s);
247 //
248 //        AddAccountingLineEvent event = new AddAccountingLineEvent("test", t, s);
249 //
250 //        assertFalse(t.isProcessed());
251 //
252 //        boolean success = kualiRuleService.applyRules(event);
253 //        assertTrue(success);
254 //
255 //        assertTrue(t.isProcessed());
256 //        assertEquals(NONE, t.getPrevEventType());
257 //        assertEquals(ADD_LINE, t.getEventType());
258 //    }
259 //
260 //    @Test public void testApplyRules_deleteAccountingLine_validRule_nullDocument() {
261 //        boolean failedAsExpected = false;
262 //
263 //        DeleteAccountingLineEvent event = new DeleteAccountingLineEvent("test", null, null, false);
264 //        try {
265 //            kualiRuleService.applyRules(event);
266 //        }
267 //        catch (IllegalArgumentException e) {
268 //            failedAsExpected = true;
269 //        }
270 //
271 //        assertTrue(failedAsExpected);
272 //    }
273 //
274 //    @Test public void testApplyRules_deleteAccountingLine_validRule_validTransactionalDocument_nullAccountingLine() {
275 //        boolean failedAsExpected = false;
276 //
277 //        DeleteAccountingLineEvent event = new DeleteAccountingLineEvent("test", new RuleMockDocument(), null, false);
278 //        try {
279 //            kualiRuleService.applyRules(event);
280 //        }
281 //        catch (IllegalArgumentException e) {
282 //            failedAsExpected = true;
283 //        }
284 //
285 //        assertTrue(failedAsExpected);
286 //    }
287 //
288 //    @Test public void testApplyRules_deleteAccountingLine_validRule_validTransactionalDocument_validAccountingLine() throws Exception {
289 //        RuleMockDocument t = new RuleMockDocument();
290 //        SourceAccountingLine s = buildSourceLine("1");
291 //        t.addSourceAccountingLine(s);
292 //
293 //        DeleteAccountingLineEvent event = new DeleteAccountingLineEvent("test", t, s, false);
294 //
295 //        assertFalse(t.isProcessed());
296 //
297 //        boolean success = kualiRuleService.applyRules(event);
298 //        assertTrue(success);
299 //
300 //        assertTrue(t.isProcessed());
301 //        assertEquals(NONE, t.getPrevEventType());
302 //        assertEquals(DELETE_LINE, t.getEventType());
303 //    }
304 //
305 //
306 //    @Test public void testApplyRules_updateAccountingLine_validRule_validTransactionalDocument_nullAccountingLine() {
307 //        boolean failedAsExpected = false;
308 //
309 //        UpdateAccountingLineEvent event = new UpdateAccountingLineEvent("test", new RuleMockDocument(), null, null);
310 //        try {
311 //            kualiRuleService.applyRules(event);
312 //        }
313 //        catch (IllegalArgumentException e) {
314 //            failedAsExpected = true;
315 //        }
316 //
317 //        assertTrue(failedAsExpected);
318 //    }
319 //
320 //    @Test public void testApplyRules_updateAccountingLine_validRule_validTransactionalDocument_validAccountingLine_nullUpdatedAccountingLine() throws Exception {
321 //        boolean failedAsExpected = false;
322 //
323 //        RuleMockDocument t = new RuleMockDocument();
324 //        SourceAccountingLine s = buildSourceLine("1");
325 //        t.addSourceAccountingLine(s);
326 //
327 //        UpdateAccountingLineEvent event = new UpdateAccountingLineEvent("test", t, s, null);
328 //
329 //        try {
330 //            kualiRuleService.applyRules(event);
331 //        }
332 //        catch (IllegalArgumentException e) {
333 //            failedAsExpected = true;
334 //        }
335 //
336 //        assertTrue(failedAsExpected);
337 //    }
338 //
339 //    @Test public void testApplyRules_updateAccountingLine_validRule_validTransactionalDocument_validAccountingLines() throws Exception {
340 //        RuleMockDocument t = new RuleMockDocument();
341 //        SourceAccountingLine s = buildSourceLine("1");
342 //        s.setOrganizationReferenceId("A");
343 //        t.addSourceAccountingLine(s);
344 //
345 //        SourceAccountingLine updatedS = (SourceAccountingLine) ObjectUtils.deepCopy(t.getSourceAccountingLine(0));
346 //        updatedS.setOrganizationReferenceId("B");
347 //
348 //        UpdateAccountingLineEvent event = new UpdateAccountingLineEvent("test", t, s, updatedS);
349 //
350 //        assertFalse(t.isProcessed());
351 //
352 //        boolean success = kualiRuleService.applyRules(event);
353 //        assertTrue(success);
354 //
355 //        assertTrue(t.isProcessed());
356 //        assertEquals(NONE, t.getPrevEventType());
357 //        assertEquals(UPDATE_LINE, t.getEventType());
358 //    }
359 //
360 //    @Test public void testApplyRules_reviewAccountingLine_validRule_validTransactionalDocument_nullAccountingLine() {
361 //        boolean failedAsExpected = false;
362 //
363 //        ReviewAccountingLineEvent event = new ReviewAccountingLineEvent("test", new RuleMockDocument(), null);
364 //        try {
365 //            kualiRuleService.applyRules(event);
366 //        }
367 //        catch (IllegalArgumentException e) {
368 //            failedAsExpected = true;
369 //        }
370 //
371 //        assertTrue(failedAsExpected);
372 //    }
373 //
374 //    @Test public void testApplyRules_reviewAccountingLine_validRule_validTransactionalDocument_validAccountingLine() throws Exception {
375 //        RuleMockDocument t = new RuleMockDocument();
376 //        SourceAccountingLine s = buildSourceLine("1");
377 //        t.addSourceAccountingLine(s);
378 //
379 //        ReviewAccountingLineEvent event = new ReviewAccountingLineEvent("test", t, s);
380 //
381 //        assertFalse(t.isProcessed());
382 //
383 //        boolean success = kualiRuleService.applyRules(event);
384 //        assertTrue(success);
385 //
386 //        assertTrue(t.isProcessed());
387 //        assertEquals(NONE, t.getPrevEventType());
388 //        assertEquals(REVIEW_LINE, t.getEventType());
389 //    }
390 //
391 //    // TODO: commenting thse out for now, need to analyze and see if it still makes sense with new notes"copied from document " +
392 //    // sourceDocumentHeaderId);
393 //    @Test public void testApplyRules_addDocumentNote_validRule_nullDocument_nullDocumentNote() {
394 //        // boolean failedAsExpected = false;
395 //        //
396 //        // AddDocumentNoteEvent event = new AddDocumentNoteEvent(null, null);
397 //        // try {
398 //        // kualiRuleService.applyRules(event);
399 //        // }
400 //        // catch (IllegalArgumentException e) {
401 //        // failedAsExpected = true;
402 //        // }
403 //        //
404 //        // assertTrue(failedAsExpected);
405 //    }
406 //
407 //    @Test public void testApplyRules_addDocumentNote_validRule_validDocument_nullDocumentNote() {
408 //        // boolean failedAsExpected = false;
409 //        //
410 //        // MockTransactionalDocument d = new MockTransactionalDocument();
411 //        // DocumentNote n = null;
412 //        //
413 //        // AddDocumentNoteEvent event = new AddDocumentNoteEvent(d, n);
414 //        //
415 //        // try {
416 //        // kualiRuleService.applyRules(event);
417 //        // }
418 //        // catch (IllegalArgumentException e) {
419 //        // failedAsExpected = true;
420 //        // }
421 //        //
422 //        // assertTrue(failedAsExpected);
423 //    }
424 //
425 //    @Test public void testApplyRules_addDocumentNote_validRule_validDocument_validDocumentNote() {
426 //        // MockTransactionalDocument d = new MockTransactionalDocument();
427 //        // DocumentNote n = new DocumentNote();
428 //        // n.setFinancialDocumentNoteText("Test");
429 //        // d.getDocumentHeader().getNotes().add(n);
430 //        //
431 //        // AddDocumentNoteEvent event = new AddDocumentNoteEvent(d, n);
432 //        //
433 //        // assertFalse(d.isProcessed());
434 //        // assertTrue(kualiRuleService.applyRules(event));
435 //        // assertTrue(d.isProcessed());
436 //        // assertEquals(NONE, d.getPrevEventType());
437 //        // assertEquals(ADD_DOC_NOTE, d.getEventType());
438 //    }
439 //
440 //    private SourceAccountingLine buildSourceLine(String documentHeaderId) throws InstantiationException, IllegalAccessException {
441 //        SourceAccountingLine sourceLine = LINE.createSourceAccountingLine();
442 //        return sourceLine;
443 //
444 //    }
445 }