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