2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.server.ejb;
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 import gr.ebs.gss.client.exceptions.DuplicateNameException;
23 import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
24 import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
25 import gr.ebs.gss.server.domain.dto.FileHeaderDTO;
26 import gr.ebs.gss.server.domain.dto.FolderDTO;
27 import gr.ebs.gss.server.domain.dto.GroupDTO;
28 import gr.ebs.gss.server.domain.dto.UserDTO;
30 import java.util.Hashtable;
31 import java.util.Iterator;
32 import java.util.List;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import javax.persistence.NoResultException;
38 import javax.rmi.PortableRemoteObject;
40 import junit.framework.Assert;
41 import junit.framework.TestCase;
46 public class ExternalAPITest extends TestCase {
49 * Utility method for creating and returning a NamingContext for looking
53 * @throws NamingException
55 private Context getInitialContext() throws NamingException {
56 final Hashtable<String, String> env = new Hashtable<String, String>();
57 env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
58 env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
59 env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
60 return new InitialContext(env);
64 * Utility method for looking up the remote service to be tested
66 * @return ExternalAPIRemote
67 * @throws NamingException
69 private ExternalAPIRemote getService() throws NamingException {
70 final Context ctx = getInitialContext();
71 final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
72 final ExternalAPIRemote service = (ExternalAPIRemote) PortableRemoteObject.narrow(ref, ExternalAPIRemote.class);
78 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getRootFolder(java.lang.Long)}.
79 * Tests with normal userId, fails if null is returned
81 public final void testGetRootFolderNormal() {
83 final ExternalAPIRemote service = getService();
84 final FolderDTO f = service.getRootFolder(Long.valueOf(1));
85 Assert.assertNotNull(f);
86 } catch (final Exception e) {
94 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getRootFolder(java.lang.Long)}.
95 * Tests with null userId, fails if {@link IllegalArgumentException} is not
98 public final void testGetRootFolderWithNullUserId() {
100 final ExternalAPIRemote service = getService();
101 service.getRootFolder(null);
103 } catch (final Exception e) {
104 if (!(e instanceof ObjectNotFoundException)) {
113 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getRootFolder(java.lang.Long)}.
114 * Tests with userId which has no folder, fails if {@link NoResultException}
117 public final void testGetRootFolderWithUserWithoutFolder() {
119 final ExternalAPIRemote service = getService();
120 service.getRootFolder(Long.valueOf(2));
122 } catch (final ObjectNotFoundException e) {
123 } catch (final Exception e) {
131 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getRootFolder(java.lang.Long)}.
132 * Tests with non-existent userId, fails if {@link NoResultException} is not
135 public final void testGetRootFolderWithNonExistentUserId() {
137 final ExternalAPIRemote service = getService();
138 service.getRootFolder(Long.valueOf(-1));
140 } catch (final ObjectNotFoundException e) {
141 } catch (final Exception e) {
149 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getFiles(java.lang.Long,java.lang.Long,boolean)}.
150 * Tests with normal folderId, fails if null or empty list is returned
152 public final void testGetFilesNormal() {
154 final ExternalAPIRemote service = getService();
155 final List<FileHeaderDTO> files = service.getFiles(new Long(1L), Long.valueOf(1), true);
156 Assert.assertNotNull(files);
157 Assert.assertFalse(files.isEmpty());
158 } catch (final Exception e) {
166 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getFiles(java.lang.Long,java.lang.Long,boolean)}.
167 * Tests with folderId of empty folder, fails if null or not empty list is
170 public final void testGetFilesWithEmptyFolder() {
172 final ExternalAPIRemote service = getService();
173 final List<FileHeaderDTO> files = service.getFiles(new Long(1L), Long.valueOf(2), true);
174 Assert.assertNotNull(files);
175 Assert.assertTrue(files.isEmpty());
176 } catch (final Exception e) {
184 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getFiles(java.lang.Long,java.lang.Long,boolean)}.
185 * Tests with null folderId, fails if {@link IllegalArgumentException} is
188 public final void testGetFilesWithNullFolderId() {
190 final ExternalAPIRemote service = getService();
191 service.getFiles(new Long(1L), null, true);
193 } catch (final Exception e) {
194 if (!(e instanceof ObjectNotFoundException)) {
203 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getFiles(java.lang.Long,java.lang.Long,boolean)}.
204 * Tests with folderId of non-existent folder, fails if null or not empty
207 public final void testGetFilesWithNonExistentFolder() {
209 final ExternalAPIRemote service = getService();
210 final List<FileHeaderDTO> files = service.getFiles(new Long(1L), Long.valueOf(-1), true);
211 Assert.assertNotNull(files);
212 Assert.assertTrue(files.isEmpty());
213 } catch (final Exception e) {
220 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getGroups(Long)} for a
221 * normal user with groups
223 public final void testGetGroupsNormal() {
225 final ExternalAPIRemote service = getService();
226 final List<GroupDTO> groups = service.getGroups(Long.valueOf(1));
227 Assert.assertNotNull(groups);
228 Assert.assertFalse(groups.isEmpty());
229 } catch (final Exception e) {
236 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getGroups(Long)} for a
237 * normal user with no groups
239 public final void testGetGroupsForUserWithNoGroups() {
241 final ExternalAPIRemote service = getService();
242 final List<GroupDTO> groups = service.getGroups(Long.valueOf(2));
243 Assert.assertNotNull(groups);
244 Assert.assertTrue(groups.isEmpty());
245 } catch (final Exception e) {
252 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getGroups(Long)} for a
255 public final void testGetGroupsForNullUserId() {
257 final ExternalAPIRemote service = getService();
258 service.getGroups(null);
260 } catch (final Exception e) {
261 if (!(e instanceof ObjectNotFoundException)) {
269 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getGroups(Long)} for a
272 public final void testGetGroupsForNonExistentUser() {
274 final ExternalAPIRemote service = getService();
275 final List<GroupDTO> groups = service.getGroups(Long.valueOf(-1));
276 Assert.assertNotNull(groups);
277 Assert.assertTrue(groups.isEmpty());
278 } catch (final Exception e) {
285 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getUsers(java.lang.Long,java.lang.Long)} for a
286 * normal group with users
288 public final void testGetUsersNormal() {
290 final ExternalAPIRemote service = getService();
291 final List<UserDTO> users = service.getUsers(Long.valueOf(1), Long.valueOf(1));
292 Assert.assertNotNull(users);
293 Assert.assertFalse(users.isEmpty());
294 } catch (final Exception e) {
301 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getUsers(java.lang.Long,java.lang.Long)}
302 * for an empty group.
304 public final void testGetUsersForEmptyGroup() {
306 final ExternalAPIRemote service = getService();
307 final List<UserDTO> users = service.getUsers(Long.valueOf(1L), Long.valueOf(2));
308 Assert.assertNotNull(users);
309 Assert.assertTrue(users.isEmpty());
310 } catch (final Exception e) {
317 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getUsers(java.lang.Long,java.lang.Long)}
318 * for a non-existent group.
320 public final void testGetUsersNonExistentGroup() {
322 final ExternalAPIRemote service = getService();
323 final List<UserDTO> users = service.getUsers(Long.valueOf(1L), Long.valueOf(-1));
324 Assert.assertNotNull(users);
325 Assert.assertTrue(users.isEmpty());
326 } catch (final Exception e) {
333 * Tests {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getUsers(java.lang.Long,java.lang.Long)}
334 * for a null groupId.
336 public final void testGetUsersWithNullGroupId() {
338 final ExternalAPIRemote service = getService();
339 service.getUsers(Long.valueOf(1L), null);
341 } catch (final Exception e) {
342 if (!(e instanceof ObjectNotFoundException)) {
351 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#createFolder(Long, Long, String)}
352 * with normal parameters
354 public final void testCreateFolderNormal() {
355 String name = "junitTestFolder";
357 final ExternalAPIRemote service = getService();
358 final List rootSubfolders = service.getRootFolder(Long.valueOf(1)).getSubfolders();
361 final Iterator i = rootSubfolders.iterator();
363 while (i.hasNext()) {
364 final FolderDTO f = (FolderDTO) i.next();
365 if (f.getName().equals(name)) {
372 service.createFolder(Long.valueOf(1), Long.valueOf(1), name);
373 final Iterator i = service.getRootFolder(Long.valueOf(1)).getSubfolders().iterator();
374 while (i.hasNext()) {
375 final FolderDTO f = (FolderDTO) i.next();
376 if (f.getName().equals(name))
380 } catch (final Exception e) {
388 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#createFolder(Long, Long, String)}
389 * with an existing folder name
391 public final void testCreateFolderWithExistingName() {
393 final ExternalAPIRemote service = getService();
394 final List rootSubfolders = service.getRootFolder(Long.valueOf(1)).getSubfolders();
395 service.createFolder(Long.valueOf(1), Long.valueOf(1), ((FolderDTO) rootSubfolders.get(0)).getName());
397 } catch (final Exception e) {
398 if (!(e instanceof DuplicateNameException)) {
407 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#createFolder(Long, Long, String)}
408 * with an empty folder name
410 public final void testCreateFolderWithEmptyName() {
412 final ExternalAPIRemote service = getService();
413 service.createFolder(Long.valueOf(1), Long.valueOf(1), "");
415 } catch (final Exception e) {
416 if (!(e instanceof ObjectNotFoundException)) {
425 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#createFolder(Long, Long, String)}
428 public final void testCreateFolderWithNullOwner() {
430 final ExternalAPIRemote service = getService();
431 service.createFolder(null, Long.valueOf(1), "test");
433 } catch (final Exception e) {
434 if (!(e instanceof ObjectNotFoundException)) {
443 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#createFolder(Long, Long, String)}
444 * with a non-existent owner
446 public final void testCreateFolderWithNonExistentOwner() {
448 final ExternalAPIRemote service = getService();
449 service.createFolder(Long.valueOf(-1), Long.valueOf(1), "test");
451 } catch (final Exception e) {
452 if (!(e instanceof ObjectNotFoundException)) {
461 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#createFolder(Long, Long, String)}
464 public final void testCreateFolderWithNullParent() {
466 final ExternalAPIRemote service = getService();
467 service.createFolder(Long.valueOf(1), null, "test");
469 } catch (final Exception e) {
470 if (!(e instanceof ObjectNotFoundException)) {
479 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#createFolder(Long, Long, String)}
480 * with a non-existent parent
482 public final void testCreateFolderWithNonExistentParent() {
484 final ExternalAPIRemote service = getService();
485 service.createFolder(Long.valueOf(1), Long.valueOf(-1), "testFolder");
487 } catch (final Exception e) {
488 if (!(e instanceof ObjectNotFoundException)) {
497 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#deleteFolder(Long, Long)}
498 * with normal parameters
500 public final void testDeleteFolderNormal() {
502 final String name = "junitTestFolder";
503 final ExternalAPIRemote service = getService();
504 final Iterator i = service.getRootFolder(Long.valueOf(1)).getSubfolders().iterator();
505 while (i.hasNext()) {
506 final FolderDTO f = (FolderDTO) i.next();
507 if (f.getName().equals(name))
508 service.deleteFolder(Long.valueOf(1), f.getId());
510 } catch (final Exception e) {
518 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#deleteFolder(Long, Long)}
521 public final void testDeleteFolderWithNullUserId() {
523 final String name = "deletedFolder";
524 final ExternalAPIRemote service = getService();
525 Long folderId = null;
527 service.createFolder(Long.valueOf(1), Long.valueOf(1), name);
528 final Iterator i = service.getRootFolder(Long.valueOf(1)).getSubfolders().iterator();
529 while (i.hasNext()) {
530 final FolderDTO f = (FolderDTO) i.next();
531 if (f.getName().equals(name)) {
532 folderId = f.getId();
533 service.deleteFolder(null, f.getId());
537 } catch (final Exception e) {
538 service.deleteFolder(Long.valueOf(1), folderId);
539 if (!(e instanceof ObjectNotFoundException)) {
544 } catch (final Exception e) {
552 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#deleteFolder(Long, Long)}
553 * with non-existent user
555 public final void testDeleteFolderWithNonExistentUser() {
557 final String name = "deletedFolder";
558 final ExternalAPIRemote service = getService();
559 Long folderId = null;
561 service.createFolder(Long.valueOf(1), Long.valueOf(1), name);
562 final Iterator i = service.getRootFolder(Long.valueOf(1)).getSubfolders().iterator();
563 while (i.hasNext()) {
564 final FolderDTO f = (FolderDTO) i.next();
565 if (f.getName().equals(name)) {
566 folderId = f.getId();
567 service.deleteFolder(Long.valueOf(-1), f.getId());
571 } catch (final Exception e) {
572 service.deleteFolder(Long.valueOf(1), folderId);
573 if (!(e instanceof ObjectNotFoundException)) {
578 } catch (final Exception e) {
586 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#deleteFolder(Long, Long)}
589 public final void testDeleteFolderWithNullFolderId() {
591 final ExternalAPIRemote service = getService();
592 service.deleteFolder(Long.valueOf(1), null);
594 } catch (final Exception e) {
595 if (!(e instanceof ObjectNotFoundException)) {
604 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#deleteFolder(Long, Long)}
607 public final void testDeleteFolderWithNonExistentFolder() {
609 final ExternalAPIRemote service = getService();
610 service.deleteFolder(Long.valueOf(1), Long.valueOf(-1));
612 } catch (final Exception e) {
613 if (!(e instanceof ObjectNotFoundException)) {
622 * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#deleteFolder(Long, Long)}
623 * with no delete permission
625 public final void testDeleteFolderNoPermission() {
627 final String name = "deletedFolder";
628 final ExternalAPIRemote service = getService();
629 Long folderId = null;
631 service.createFolder(Long.valueOf(1), Long.valueOf(1), name);
632 final Iterator i = service.getRootFolder(Long.valueOf(1)).getSubfolders().iterator();
633 while (i.hasNext()) {
634 final FolderDTO f = (FolderDTO) i.next();
635 if (f.getName().equals(name)) {
636 folderId = f.getId();
637 service.deleteFolder(Long.valueOf(2), f.getId());
641 } catch (final Exception e) {
642 service.deleteFolder(Long.valueOf(1), folderId);
643 if (!(e instanceof InsufficientPermissionsException)) {
648 } catch (final Exception e) {
655 * Tests {@link ExternalAPIBean#getUserDTO(Long)} with normal parameters
657 public final void testGetUserNormal() {
659 final ExternalAPIRemote service = getService();
660 final UserDTO user = service.getUserDTO(Long.valueOf(1));
662 assertNotNull(user.getId());
663 assertEquals(user.getId().longValue(), 1L);
664 } catch (final Exception e) {
671 * Tests {@link ExternalAPIBean#getUserDTO(Long)} with null userId
673 public final void testGetUserNullId() {
675 final ExternalAPIRemote service = getService();
676 service.getUserDTO(null);
678 } catch (final NamingException e) {
681 } catch (final Exception e) {
682 if (!(e instanceof ObjectNotFoundException)) {
690 * Tests {@link ExternalAPIBean#getUserDTO(Long)} with non-existent userId
692 public final void testGetUserNonExistentId() {
694 final ExternalAPIRemote service = getService();
695 service.getUserDTO(Long.valueOf(-1));
697 } catch (final NamingException e) {
700 } catch (final Exception e) {
701 if (!(e instanceof ObjectNotFoundException)) {
709 * Tests {@link ExternalAPIBean#createTag(Long, Long, String)} with normal
712 public final void testCreateTagNormal() {
714 final ExternalAPIRemote service = getService();
715 service.createTag(Long.valueOf(1), Long.valueOf(165), "testTag");
716 } catch (final Exception e) {
723 * Tests {@link ExternalAPIBean#createTag(Long, Long, String)} with null
726 public final void testCreateTagNullUser() {
728 final ExternalAPIRemote service = getService();
729 service.createTag(null, Long.valueOf(162), "testTag");
731 } catch (final Exception e) {
732 if (!(e instanceof ObjectNotFoundException)) {
740 * Tests {@link ExternalAPIBean#createTag(Long, Long, String)} with null
743 public final void testCreateTagNullFile() {
745 final ExternalAPIRemote service = getService();
746 service.createTag(Long.valueOf(1), null, "testTag");
748 } catch (final Exception e) {
749 if (!(e instanceof ObjectNotFoundException)) {
757 * Tests {@link ExternalAPIBean#createTag(Long, Long, String)} with null tag
759 public final void testCreateTagNullTag() {
761 final ExternalAPIRemote service = getService();
762 service.createTag(Long.valueOf(1), Long.valueOf(162), null);
764 } catch (final Exception e) {
765 if (!(e instanceof ObjectNotFoundException)) {
773 * Tests {@link ExternalAPIBean#createTag(Long, Long, String)} with empty
776 public final void testCreateTagEmptyTag() {
778 final ExternalAPIRemote service = getService();
779 service.createTag(Long.valueOf(1), Long.valueOf(162), "");
781 } catch (final Exception e) {
782 if (!(e instanceof ObjectNotFoundException)) {
790 * Tests {@link ExternalAPIBean#createTag(Long, Long, String)} with non
793 public final void testCreateTagNonExistingUser() {
795 final ExternalAPIRemote service = getService();
796 service.createTag(Long.valueOf(-1), Long.valueOf(162), "testTag");
798 } catch (final Exception e) {
799 if (!(e instanceof ObjectNotFoundException)) {
807 * Tests {@link ExternalAPIBean#createTag(Long, Long, String)} with non
810 public final void testCreateTagNonExistingFile() {
812 final ExternalAPIRemote service = getService();
813 service.createTag(Long.valueOf(1), Long.valueOf(-1), "testTag");
815 } catch (final Exception e) {
816 if (!(e instanceof ObjectNotFoundException)) {
824 * Tests {@link ExternalAPIBean#copyFile}
827 public final void testCopyFile(){
829 final ExternalAPIRemote service = getService();
830 service.copyFile(Long.valueOf(1), Long.valueOf(14065), Long.valueOf(14067), "papa.txt");
831 } catch (final Exception e) {
832 if (!(e instanceof ObjectNotFoundException)) {
840 * Tests {@link ExternalAPIBean#indexFile}
842 public final void testIndexFile() {
844 final ExternalAPIRemote service = getService();
845 service.indexFile(Long.valueOf(599), false);
846 } catch (final Exception e) {
853 * Tests {@link ExternalAPIBean#indexFile} with delete
855 public final void testDeleteIndexedFile() {
857 final ExternalAPIRemote service = getService();
858 service.indexFile(Long.valueOf(14076), true);
859 } catch (final Exception e) {
866 * Tests {@link ExternalAPIBean#searchFiles}
868 public final void testSearchFiles() {
870 final ExternalAPIRemote service = getService();
871 service.searchFiles(Long.valueOf(1), "κείμενο");
872 } catch (final Exception e) {