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.impl.note;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.joda.time.DateTime;
27  import org.junit.Test;
28  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
29  import org.kuali.rice.kew.api.KewApiServiceLocator;
30  import org.kuali.rice.kew.api.WorkflowDocument;
31  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
32  import org.kuali.rice.kew.api.note.Note;
33  import org.kuali.rice.kew.api.note.NoteService;
34  import org.kuali.rice.kew.test.KEWTestCase;
35  
36  public class NoteServiceTest extends KEWTestCase {
37  	
38  	@Test
39  	public void testIllegalNoteOperations() throws Exception {
40  		NoteService noteService = KewApiServiceLocator.getNoteService();
41  		
42  		try {
43  			noteService.createNote(null);
44  			fail("RiceIllegalArgumentException should have been thrown");
45  		} catch (RiceIllegalArgumentException e) {}
46  		
47  		Note.Builder testNote = Note.Builder.create("1234", "ewestfal");
48  		// shouldn't be able to create with an id already on it
49  		testNote.setId("4321");
50  		try {
51  			noteService.createNote(testNote.build());
52  			fail("RiceIllegalArgumentException should have been thrown");
53  		} catch (RiceIllegalArgumentException e) {}
54  		
55  		try {
56  			noteService.updateNote(null);
57  			fail("RiceIllegalArgumentException should have been thrown");
58  		} catch (RiceIllegalArgumentException e) {}
59  		
60  		// shouldn't be able to update a note that doesn't have an id
61  		testNote = Note.Builder.create("1234", "ewestfal");
62  		try {
63  			noteService.updateNote(testNote.build());
64  			fail("RiceIllegalArgumentException should have been thrown");
65  		} catch (RiceIllegalArgumentException e) {}
66  		
67  		// shouldn't be able to update a note if it doesn't have a version number
68  		testNote.setId("4321");
69  		try {
70  			noteService.updateNote(testNote.build());
71  			fail("RiceIllegalArgumentException should have been thrown");
72  		} catch (RiceIllegalArgumentException e) {}
73  		
74  		// add version number, but now shouldn't be able to update a note if it doesn't have a valid create date
75  		testNote.setVersionNumber(new Long(1));
76  		try {
77  			noteService.updateNote(testNote.build());
78  			fail("RiceIllegalArgumentException should have been thrown");
79  		} catch (RiceIllegalArgumentException e) {}
80  		
81  		// add create date, but now shouldn't be able to update a note with an id of a note that doesn't exist
82  		testNote.setCreateDate(new DateTime());
83  		try {
84  			noteService.updateNote(testNote.build());
85  			fail("RiceIllegalArgumentException should have been thrown");
86  		} catch (RiceIllegalArgumentException e) {}
87  
88  		// shouldn't be able to delete a note with a note id that doesn't exist
89  		try {
90  			noteService.deleteNote("1234");
91  			fail("RiceIllegalArgumentException should have been thrown");
92  		} catch (RiceIllegalArgumentException e) {}
93  				
94  	}
95  	
96  	@Test public void testNotesClient() throws Exception {
97  		NoteService noteService = KewApiServiceLocator.getNoteService();
98  		
99  		WorkflowDocument doc = WorkflowDocumentFactory.createDocument(getPrincipalIdForName("rkirkend"), "TestDocumentType");
100 
101 		assertTrue(noteService.getNotes(doc.getDocumentId()).isEmpty());
102 		
103 		// Test add notes
104 		Note.Builder testNote = Note.Builder.create(doc.getDocumentId(), "andlee");
105 		testNote.setText("first added note");
106 		
107 		Note createdNote = noteService.createNote(testNote.build());
108 		assertNotNull(createdNote);
109 		assertNotNull(createdNote.getId());
110 		assertNotNull(createdNote.getVersionNumber());
111 		assertEquals(doc.getDocumentId(), createdNote.getDocumentId());
112 		assertEquals("andlee", createdNote.getAuthorPrincipalId());
113 		assertEquals("first added note", createdNote.getText());
114 		
115 		testNote = Note.Builder.create(doc.getDocumentId(), "rou");
116 		testNote.setText("second added note");
117 		Note createdNote2 = noteService.createNote(testNote.build());
118 		
119 		List<Note> notesList = noteService.getNotes(doc.getDocumentId());
120 		
121         assertEquals ("Two notes are added.", 2, notesList.size());
122                 
123         assertEquals("Note List size changed",2,notesList.size());
124         for (Iterator<Note> iter = notesList.iterator(); iter.hasNext();) {
125 			Note note = iter.next();
126 			assertNotNull("Note saved", note.getId());
127 			if (note.getId().equals(createdNote.getId())) {
128 				assertEquals("text altered during save", "first added note", note.getText());
129 				assertEquals("note user associated with saved note", "andlee", note.getAuthorPrincipalId());
130 			}
131 			if (note.getId().equals(createdNote2.getId())) {
132 				assertEquals("text altered during save", "second added note", note.getText());
133 				assertEquals("note user associated with saved note", "rou", note.getAuthorPrincipalId());
134 			}
135 			
136 		}
137                 
138         notesList = noteService.getNotes(doc.getDocumentId());
139         Note note1 = null;
140         Note note2 = null;
141         for (Note note : notesList) {
142         	if (note.getId().equals(createdNote.getId())) {
143         		note1 = note;
144         	} else if(note.getId().equals(createdNote2.getId())) {
145         		note2 = note;
146         	} else {
147         		fail("encountered unexpected note!: " + note.getId());
148         	}
149         }
150         noteService.deleteNote(note1.getId());
151 
152         Note.Builder note2Builder = Note.Builder.create(note2);
153         note2Builder.setText("Update second note text");
154         noteService.updateNote(note2Builder.build());
155         
156         notesList = noteService.getNotes(doc.getDocumentId());
157         assertEquals("Note List size changed",1,notesList.size());
158         Note remainingNote = notesList.get(0);
159         assertNotNull("Note saved", remainingNote.getId());
160         assertEquals("text altered during save", "Update second note text", remainingNote.getText());
161         assertEquals("note user associated with saved note", "rou", remainingNote.getAuthorPrincipalId());
162 	}
163 
164 }