Statistics
| Branch: | Tag: | Revision:

root / test / gr / ebs / gss / server / ejb / ScenarioTest.java @ 672:163de098320c

History | View | Annotate | Download (6 kB)

1
/*
2
 * Copyright 2008, 2009, 2010 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
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.
10
 *
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.
15
 *
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/>.
18
 */
19
package gr.ebs.gss.server.ejb;
20

    
21
import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22
import gr.ebs.gss.client.exceptions.InsufficientPermissionsException;
23
import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
24
import gr.ebs.gss.server.domain.User;
25
import gr.ebs.gss.server.domain.dto.FolderDTO;
26
import gr.ebs.gss.server.domain.dto.PermissionDTO;
27

    
28
import java.util.Hashtable;
29
import java.util.List;
30
import java.util.Set;
31

    
32
import javax.naming.Context;
33
import javax.naming.InitialContext;
34
import javax.naming.NamingException;
35
import javax.rmi.PortableRemoteObject;
36

    
37
import junit.framework.Assert;
38
import junit.framework.TestCase;
39

    
40
/**
41
 * @author kman
42
 */
43
public class ScenarioTest extends TestCase {
44

    
45
        User user1 = null;
46

    
47
        User user2 = null;
48

    
49
        /**
50
         *
51
         */
52
        public ScenarioTest() {
53

    
54
                try {
55
                        user1 = getService().createUser("test1", "test user 1", "test1@ebs.gr", "http://my.idp.com/foo", "bar");
56
                        user2 = getService().createUser("test2", "test user 2", "test2@ebs.gr", "http://my.idp.com/foo", "baz");
57
                } catch (Exception e) {
58
                        try {
59
                                user1 = getService().findUser("test1");
60
                                user2 = getService().findUser("test2");
61
                                e.printStackTrace();
62
                        } catch (Exception ex) {
63
                                ex.printStackTrace();
64
                                fail("Error initializing users");
65
                        }
66
                }
67

    
68
        }
69

    
70
        /**
71
         * Utility method for creating and returning a NamingContext for looking
72
         * EJBs up in the JNDI
73
         *
74
         * @return Context
75
         * @throws NamingException
76
         */
77
        private Context getInitialContext() throws NamingException {
78
                final Hashtable<String, String> env = new Hashtable<String, String>();
79
                env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
80
                env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
81
                env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
82
                return new InitialContext(env);
83
        }
84

    
85
        /**
86
         * Utility method for looking up the remote service to be tested
87
         *
88
         * @return ExternalAPI
89
         * @throws NamingException
90
         */
91
        private ExternalAPI getService() throws NamingException {
92
                final Context ctx = getInitialContext();
93
                final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
94
                final ExternalAPI service = (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
95
                return service;
96
        }
97

    
98
        /**
99
         * Test method for
100
         * {@link gr.ebs.gss.server.ejb.ExternalAPIBean#getRootFolder(java.lang.Long)}
101
         * .
102
         */
103
        public void testGetRootFolder() {
104
                try {
105
                        FolderDTO root = getService().getRootFolder(user1.getId());
106
                        Assert.assertNotNull(root);
107
                        List<FolderDTO> subfolders = getService().getSubfolders(user1.getId(), root.getId());
108
                        for(FolderDTO f : subfolders)
109
                                getService().deleteFolder(user1.getId(), f.getId());
110

    
111
                } catch (Exception e) {
112
                        e.printStackTrace();
113
                        Assert.fail();
114
                }
115
        }
116

    
117

    
118
        //test create folder , folder permissions, copySharedFolders
119
        public void testFolderOperations(){
120
                try{
121
                        FolderDTO root = getService().getRootFolder(user1.getId());
122
                        Assert.assertNotNull(root);
123
                        getService().createFolder(user1.getId(), root.getId(), "subfolder1");
124
                        List<FolderDTO> subfolders = getService().getSubfolders(user1.getId(), root.getId());
125
                        Assert.assertTrue(subfolders.size() == 1);
126
                        FolderDTO subFolder1 = subfolders.get(0);
127
                        getService().createFolder(user1.getId(), subFolder1.getId(), "subfolder2");
128
                        PermissionDTO permission = new PermissionDTO();
129
                        permission.setUser(user2.getDTO());
130
                        permission.setRead(true);
131
                        permission.setWrite(true);
132
                        Set<PermissionDTO> perms = getService().getFolderPermissions(user1.getId(), subFolder1.getId());
133
                        perms.add(permission);
134
                        getService().updateFolder(user1.getId(), subFolder1.getId(), null, subFolder1.isReadForAll(), perms);
135
                        List<FolderDTO> sharedFolders = getService().getSharedRootFolders(user1.getId());
136
                        assertTrue(sharedFolders.size() == 1 && sharedFolders.get(0).getId().equals(subFolder1.getId()));
137
                        List<FolderDTO> sharedForUser2 = getService().getSharedRootFolders(user1.getId(), user2.getId());
138
                        assertTrue(sharedFolders.get(0).getId().equals(sharedForUser2.get(0).getId()));
139

    
140
                        FolderDTO root2 = getService().getRootFolder(user2.getId());
141
                        getService().copyFolderStructure(user2.getId(), subFolder1.getId(), root2.getId(), subFolder1.getName());
142
                        List<FolderDTO> subfolders2 = getService().getSubfolders(user2.getId(), root2.getId());
143
                        Assert.assertTrue(subfolders2.size() == 1);
144
                        FolderDTO subFolder2 = subfolders2.get(0);
145
                        assertTrue(subFolder2.getSubfolders().get(0).getName().equals("subfolder2"));
146
                        perms.remove(permission);
147
                        getService().updateFolder(user1.getId(), subFolder1.getId(), null, subFolder1.isReadForAll(), perms);
148
                        sharedForUser2 = getService().getSharedRootFolders(user1.getId(), user2.getId());
149
                        assertTrue(sharedForUser2.size() == 0);
150
                        canUserGetFolderWithoutPermissions(user2.getId(), subFolder1.getId());
151
                        getService().deleteFolder(user1.getId(), subFolder1.getId());
152
                        getService().deleteFolder(user2.getId(), subFolder2.getId());
153

    
154
                }
155
                catch(Exception ex){
156
                        ex.printStackTrace();
157
                        Assert.fail();
158
                }
159
        }
160

    
161
        private void canUserGetFolderWithoutPermissions(Long userId,Long folderId){
162
                try {
163
                        getService().getFolder(userId, folderId);
164
                        Assert.fail();
165
                } catch (ObjectNotFoundException e) {
166
                        Assert.fail();
167
                        e.printStackTrace();
168
                } catch (InsufficientPermissionsException e) {
169
                        //e.printStackTrace();
170
                } catch (NamingException e) {
171
                        Assert.fail();
172
                }
173
        }
174

    
175

    
176

    
177
}