001    /**
002     * Copyright 2005-2011 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.service;
017    
018    import org.junit.Test;
019    import org.kuali.rice.krad.rule.event.ApproveDocumentEvent;
020    import org.kuali.test.KRADTestCase;
021    
022    import static org.junit.Assert.assertTrue;
023    
024    /**
025     * This class tests the KualiRuleService.
026     * <p>
027     * Testing applyRules( event ) has proven to be too strongly dependent on an actual document type, and business rule, and XML
028     * document which binds them together, to be really useful. Instead, we'll test each of the applyRules( rule, event ) methods which
029     * are called by applyRules( event ), since they do the actual work involved in applying a rule.
030     */
031    public class KualiRuleServiceTest extends KRADTestCase {
032        private static boolean entriesAdded = false;
033        private static KualiRuleService kualiRuleService;
034    
035        @Override
036        public void setUp() throws Exception {
037            super.setUp();
038    
039            if (!entriesAdded) {
040                entriesAdded = true;
041                kualiRuleService = KRADServiceLocatorWeb.getKualiRuleService();
042            }
043        }
044    
045        @Test public void testApplyRules_approveDocument_nullEvent() {
046            boolean failedAsExpected = false;
047    
048            ApproveDocumentEvent event = null;
049            try {
050                kualiRuleService.applyRules(event);
051            }
052            catch (IllegalArgumentException e) {
053                failedAsExpected = true;
054            }
055    
056            assertTrue(failedAsExpected);
057        }
058    
059        @Test public void testApplyRules_approveDocument_nullDocument() {
060            boolean failedAsExpected = false;
061    
062            try {
063                ApproveDocumentEvent event = new ApproveDocumentEvent(null);
064    
065                kualiRuleService.applyRules(event);
066            }
067            catch (RuntimeException e) {
068                failedAsExpected = true;
069            }
070    
071            assertTrue(failedAsExpected);
072        }
073    
074    //    private static final String NONE = "none";
075    //    private static final String ROUTE_DOCUMENT = "routeDoc";
076    //    private static final String APPROVE_DOCUMENT = "approveDoc";
077    //    private static final String SAVE_DOCUMENT = "saveDoc";
078    //    private static final String ADD_LINE = "addLine";
079    //    private static final String DELETE_LINE = "deleteLine";
080    //    private static final String UPDATE_LINE = "updateLine";
081    //    private static final String REVIEW_LINE = "reviewLine";
082    //
083    //    @Test public void testApplyRules_approveDocument_validRule_validDocument() {
084    //        RuleMockDocument d = new RuleMockDocument();
085    //        ApproveDocumentEvent event = new ApproveDocumentEvent(d);
086    //
087    //
088    //        assertFalse(d.isProcessed());
089    //        assertEquals(NONE, d.getEventType());
090    //
091    //        boolean success = kualiRuleService.applyRules(event);
092    //        assertTrue(success);
093    //
094    //        assertTrue(d.isProcessed());
095    //        assertEquals(ROUTE_DOCUMENT, d.getPrevEventType());
096    //        assertEquals(APPROVE_DOCUMENT, d.getEventType());
097    //    }
098    //
099    //    @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    }