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.docmentlink;
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.List;
24  
25  import org.apache.log4j.Logger;
26  import org.junit.Test;
27  import org.kuali.rice.kew.api.KewApiServiceLocator;
28  import org.kuali.rice.kew.api.document.DocumentLink;
29  import org.kuali.rice.kew.api.document.WorkflowDocumentService;
30  import org.kuali.rice.kew.test.KEWTestCase;
31  import org.kuali.rice.test.BaselineTestCase;
32  
33  /**
34   * Integration test for {@link DocumentLink}.
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.CLEAR_DB)
39  public class DocumentLinkTest extends KEWTestCase {
40  
41  	private static final Logger LOG = Logger.getLogger(DocumentLinkTest.class);
42  	
43  	private WorkflowDocumentService service;
44  
45  	@Override
46  	protected void setUpAfterDataLoad() throws Exception {
47  		super.setUpAfterDataLoad();
48  		this.service = KewApiServiceLocator.getWorkflowDocumentService();
49  	}
50  
51  	@Test public void testAddLinkBTW2DocsSucess() throws Exception {
52  
53  		// Test add link
54  		DocumentLink testDocLink1 = DocumentLink.Builder.create("5000", "6000").build();
55  		
56  		
57  		testDocLink1 = service.addDocumentLink(testDocLink1);
58  		assertNotNull(testDocLink1.getId());
59  		assertEquals("5000", testDocLink1.getOriginatingDocumentId());
60  		assertEquals("6000", testDocLink1.getDestinationDocumentId());
61  
62  		// ensure that a corresponding link was created with the other document
63  		List<DocumentLink> outgoingLinks = service.getOutgoingDocumentLinks("6000");
64  		assertEquals(1, outgoingLinks.size());
65  		DocumentLink testDocLink2 = outgoingLinks.get(0);
66  		
67  		assertEquals(testDocLink1.getOriginatingDocumentId(), testDocLink2.getDestinationDocumentId());
68  		assertEquals(testDocLink2.getOriginatingDocumentId(), testDocLink1.getDestinationDocumentId());
69  
70  	}
71  
72  	@Test public void testAddDuplicatedLinkBTW2DocsFailure() throws Exception {
73  
74  		DocumentLink testDocLink = DocumentLink.Builder.create("5000", "6000").build();
75  
76  		DocumentLink testDocLinkAdded = service.addDocumentLink(testDocLink);
77  		assertNotNull(testDocLinkAdded);
78  		assertNotNull(testDocLinkAdded.getId());
79  
80  		List<DocumentLink> links1 = service.getOutgoingDocumentLinks("5000");
81  		assertEquals(1, links1.size());
82  
83  		DocumentLink testDocLinkAdded2 = service.addDocumentLink(testDocLink);
84  		assertNotNull(testDocLinkAdded);
85  		assertNotNull(testDocLinkAdded.getId());
86  		assertEquals(testDocLinkAdded2, testDocLinkAdded);
87  
88  		List<DocumentLink> links2 = service.getOutgoingDocumentLinks("5000");
89  		assertEquals(1, links2.size());
90  
91  		assertEquals(links1.size(), links2.size());
92  
93  	}
94  
95  	@Test public void testAddIncomplelteLinkBTW2DocsFailure() throws Exception {
96  
97  		try {
98  			DocumentLink.Builder.create(null, null);
99  			fail();
100 		} catch (IllegalArgumentException e) {}
101 
102 		try{
103 			DocumentLink.Builder.create("6000", null);
104 			fail();
105 		} catch (IllegalArgumentException e){
106 			assertTrue(e.getMessage().contains("was null or blank"));
107 		}
108 
109 	}
110 
111 	@Test public void testGetLinkBTW2DocsSucess() throws Exception {
112 
113 		DocumentLink testDocLink = DocumentLink.Builder.create("5000", "6000").build();
114 
115 		DocumentLink link1 = service.addDocumentLink(testDocLink);
116 
117 		
118 		link1 = service.getDocumentLink(link1.getId());
119 
120 		assertNotNull(link1);
121 		assertEquals(testDocLink.getOriginatingDocumentId(), link1.getOriginatingDocumentId());
122 		assertEquals(testDocLink.getDestinationDocumentId(), link1.getDestinationDocumentId());
123 
124 	}
125 
126 	@Test public void testGetLinkBTW2DocsFailure() throws Exception {
127 
128 
129 		DocumentLink testDocLink = DocumentLink.Builder.create("5000", "6000").build();
130 
131 		service.addDocumentLink(testDocLink);
132 
133 		List<DocumentLink> links = service.getOutgoingDocumentLinks("5001");
134 
135 		assertTrue(links.isEmpty());
136 
137 	}
138 
139 	@Test public void testGetAllLinksFromOrgnDocSucess() throws Exception {
140 
141 		DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
142 		service.addDocumentLink(link1);
143 		
144 		DocumentLink link2 = DocumentLink.Builder.create("5009", "6009").build();
145 		service.addDocumentLink(link2);
146 		
147 		DocumentLink link3 = DocumentLink.Builder.create("5000", "6003").build();
148 		service.addDocumentLink(link3);
149 		
150 		DocumentLink link4 = DocumentLink.Builder.create("5000", "6004").build();
151 		service.addDocumentLink(link4);
152 		
153 		List<DocumentLink> links = service.getOutgoingDocumentLinks("5000");
154 		assertEquals(3, links.size());
155 
156 	}
157 
158 	@Test public void testGetAllLinksFromOrgnDocFailure()throws Exception {
159 
160 		DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
161 		service.addDocumentLink(link1);
162 
163 		DocumentLink link2 = DocumentLink.Builder.create("5009", "6009").build();
164 		service.addDocumentLink(link2);
165 		
166 		DocumentLink link3 = DocumentLink.Builder.create("5000", "6003").build();
167 		service.addDocumentLink(link3);
168 
169 		List<DocumentLink> links = service.getOutgoingDocumentLinks("8000");
170 
171 		assertEquals(0, links.size());
172 
173 	}
174 
175 	@Test public void testRemoveLinkBTW2DocsSucess() throws Exception{
176 
177 		DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
178 		link1 = service.addDocumentLink(link1);
179 
180 		List<DocumentLink> links1 = service.getOutgoingDocumentLinks("5000");
181 
182 		assertEquals(1, links1.size());
183 
184 		List<DocumentLink> links2 = service.getOutgoingDocumentLinks("6000");
185 
186 		assertEquals(1, links2.size());
187 
188 		DocumentLink deletedLink = service.deleteDocumentLink(link1.getId());
189 		assertNotNull(deletedLink);
190 
191 		List<DocumentLink> links3 = service.getOutgoingDocumentLinks("5000");
192 
193 		assertEquals(0, links3.size());
194 	}
195 
196 	@Test public void testRemoveAllLinksFromOrgnDocSucess() throws Exception {
197 
198 		DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
199 		link1 = service.addDocumentLink(link1);
200 
201 		DocumentLink link2 = DocumentLink.Builder.create("5000", "6002").build();
202 		link2 = service.addDocumentLink(link2);
203 
204 		List<DocumentLink> links01 = service.getOutgoingDocumentLinks("5000");
205 		List<DocumentLink> links02 = service.getOutgoingDocumentLinks("6000");
206 		List<DocumentLink> links03 = service.getOutgoingDocumentLinks("6002");
207 
208 		assertEquals(2, links01.size());
209 		assertEquals(1, links02.size());
210 		assertEquals(1, links03.size());
211 
212 
213 		List<DocumentLink> deletedDocuments = service.deleteDocumentLinksByDocumentId("5000");
214 		assertEquals(2, deletedDocuments.size());
215 
216 		links01 = service.getOutgoingDocumentLinks("5000");
217 		links02 = service.getOutgoingDocumentLinks("6000");
218 		links03 = service.getOutgoingDocumentLinks("6002");
219 
220 		assertEquals(0, links01.size());
221 		assertEquals(0, links02.size());
222 		assertEquals(0, links03.size());
223 
224 	}
225 	
226 	@Test public void testDocLinktoItself() throws Exception {
227 		try{			
228 			DocumentLink.Builder.create("5000", "5000");
229 			fail();
230 		} catch(IllegalArgumentException e){
231 			assertTrue(e.getMessage().contains("cannot link a document with itself"));
232 		}
233 	}
234 
235 }