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.kew.docmentlink;
017
018 import static org.junit.Assert.assertEquals;
019 import static org.junit.Assert.assertNotNull;
020 import static org.junit.Assert.assertTrue;
021 import static org.junit.Assert.fail;
022
023 import java.util.List;
024
025 import org.apache.log4j.Logger;
026 import org.junit.Test;
027 import org.kuali.rice.kew.api.KewApiServiceLocator;
028 import org.kuali.rice.kew.api.document.DocumentLink;
029 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
030 import org.kuali.rice.kew.test.KEWTestCase;
031 import org.kuali.rice.test.BaselineTestCase;
032
033 /**
034 * This is a description of what this class does - g1zhang don't forget to fill this in.
035 *
036 * @author Kuali Rice Team (kuali-rice@googlegroups.com)
037 *
038 */
039 @BaselineTestCase.BaselineMode(BaselineTestCase.Mode.NONE)
040 public class DocumentLinkTest extends KEWTestCase {
041
042 private static final Logger LOG = Logger.getLogger(DocumentLinkTest.class);
043
044 private WorkflowDocumentService service;
045
046 @Override
047 protected void setUpAfterDataLoad() throws Exception {
048 super.setUpAfterDataLoad();
049 this.service = KewApiServiceLocator.getWorkflowDocumentService();
050 }
051
052 @Test public void testAddLinkBTW2DocsSucess() throws Exception {
053
054 // Test add link
055 DocumentLink testDocLink1 = DocumentLink.Builder.create("5000", "6000").build();
056
057
058 testDocLink1 = service.addDocumentLink(testDocLink1);
059 assertNotNull(testDocLink1.getId());
060 assertEquals("5000", testDocLink1.getOriginatingDocumentId());
061 assertEquals("6000", testDocLink1.getDestinationDocumentId());
062
063 // ensure that a corresponding link was created with the other document
064 List<DocumentLink> outgoingLinks = service.getOutgoingDocumentLinks("6000");
065 assertEquals(1, outgoingLinks.size());
066 DocumentLink testDocLink2 = outgoingLinks.get(0);
067
068 assertEquals(testDocLink1.getOriginatingDocumentId(), testDocLink2.getDestinationDocumentId());
069 assertEquals(testDocLink2.getOriginatingDocumentId(), testDocLink1.getDestinationDocumentId());
070
071 }
072
073 @Test public void testAddDuplicatedLinkBTW2DocsFailure() throws Exception {
074
075 DocumentLink testDocLink = DocumentLink.Builder.create("5000", "6000").build();
076
077 DocumentLink testDocLinkAdded = service.addDocumentLink(testDocLink);
078 assertNotNull(testDocLinkAdded);
079 assertNotNull(testDocLinkAdded.getId());
080
081 List<DocumentLink> links1 = service.getOutgoingDocumentLinks("5000");
082 assertEquals(1, links1.size());
083
084 DocumentLink testDocLinkAdded2 = service.addDocumentLink(testDocLink);
085 assertNotNull(testDocLinkAdded);
086 assertNotNull(testDocLinkAdded.getId());
087 assertEquals(testDocLinkAdded2, testDocLinkAdded);
088
089 List<DocumentLink> links2 = service.getOutgoingDocumentLinks("5000");
090 assertEquals(1, links2.size());
091
092 assertEquals(links1.size(), links2.size());
093
094 }
095
096 @Test public void testAddIncomplelteLinkBTW2DocsFailure() throws Exception {
097
098 try {
099 DocumentLink.Builder.create(null, null);
100 fail();
101 } catch (IllegalArgumentException e) {}
102
103 try{
104 DocumentLink.Builder.create("6000", null);
105 fail();
106 } catch (IllegalArgumentException e){
107 assertTrue(e.getMessage().contains("was null or blank"));
108 }
109
110 }
111
112 @Test public void testGetLinkBTW2DocsSucess() throws Exception {
113
114 DocumentLink testDocLink = DocumentLink.Builder.create("5000", "6000").build();
115
116 DocumentLink link1 = service.addDocumentLink(testDocLink);
117
118
119 link1 = service.getDocumentLink(link1.getId());
120
121 assertNotNull(link1);
122 assertEquals(testDocLink.getOriginatingDocumentId(), link1.getOriginatingDocumentId());
123 assertEquals(testDocLink.getDestinationDocumentId(), link1.getDestinationDocumentId());
124
125 }
126
127 @Test public void testGetLinkBTW2DocsFailure() throws Exception {
128
129
130 DocumentLink testDocLink = DocumentLink.Builder.create("5000", "6000").build();
131
132 service.addDocumentLink(testDocLink);
133
134 List<DocumentLink> links = service.getOutgoingDocumentLinks("5001");
135
136 assertTrue(links.isEmpty());
137
138 }
139
140 @Test public void testGetAllLinksFromOrgnDocSucess() throws Exception {
141
142 DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
143 service.addDocumentLink(link1);
144
145 DocumentLink link2 = DocumentLink.Builder.create("5009", "6009").build();
146 service.addDocumentLink(link2);
147
148 DocumentLink link3 = DocumentLink.Builder.create("5000", "6003").build();
149 service.addDocumentLink(link3);
150
151 DocumentLink link4 = DocumentLink.Builder.create("5000", "6004").build();
152 service.addDocumentLink(link4);
153
154 List<DocumentLink> links = service.getOutgoingDocumentLinks("5000");
155 assertEquals(3, links.size());
156
157 }
158
159 @Test public void testGetAllLinksFromOrgnDocFailure()throws Exception {
160
161 DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
162 service.addDocumentLink(link1);
163
164 DocumentLink link2 = DocumentLink.Builder.create("5009", "6009").build();
165 service.addDocumentLink(link2);
166
167 DocumentLink link3 = DocumentLink.Builder.create("5000", "6003").build();
168 service.addDocumentLink(link3);
169
170 List<DocumentLink> links = service.getOutgoingDocumentLinks("8000");
171
172 assertEquals(0, links.size());
173
174 }
175
176 @Test public void testRemoveLinkBTW2DocsSucess() throws Exception{
177
178 DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
179 link1 = service.addDocumentLink(link1);
180
181 List<DocumentLink> links1 = service.getOutgoingDocumentLinks("5000");
182
183 assertEquals(1, links1.size());
184
185 List<DocumentLink> links2 = service.getOutgoingDocumentLinks("6000");
186
187 assertEquals(1, links2.size());
188
189 DocumentLink deletedLink = service.deleteDocumentLink(link1.getId());
190 assertNotNull(deletedLink);
191
192 List<DocumentLink> links3 = service.getOutgoingDocumentLinks("5000");
193
194 assertEquals(0, links3.size());
195 }
196
197 @Test public void testRemoveAllLinksFromOrgnDocSucess() throws Exception {
198
199 DocumentLink link1 = DocumentLink.Builder.create("5000", "6000").build();
200 link1 = service.addDocumentLink(link1);
201
202 DocumentLink link2 = DocumentLink.Builder.create("5000", "6002").build();
203 link2 = service.addDocumentLink(link2);
204
205 List<DocumentLink> links01 = service.getOutgoingDocumentLinks("5000");
206 List<DocumentLink> links02 = service.getOutgoingDocumentLinks("6000");
207 List<DocumentLink> links03 = service.getOutgoingDocumentLinks("6002");
208
209 assertEquals(2, links01.size());
210 assertEquals(1, links02.size());
211 assertEquals(1, links03.size());
212
213
214 List<DocumentLink> deletedDocuments = service.deleteDocumentLinksByDocumentId("5000");
215 assertEquals(2, deletedDocuments.size());
216
217 links01 = service.getOutgoingDocumentLinks("5000");
218 links02 = service.getOutgoingDocumentLinks("6000");
219 links03 = service.getOutgoingDocumentLinks("6002");
220
221 assertEquals(0, links01.size());
222 assertEquals(0, links02.size());
223 assertEquals(0, links03.size());
224
225 }
226
227 @Test public void testDocLinktoItself() throws Exception {
228 try{
229 DocumentLink.Builder.create("5000", "5000");
230 fail();
231 } catch(IllegalArgumentException e){
232 assertTrue(e.getMessage().contains("cannot link a document with itself"));
233 }
234 }
235
236 }