1 package org.exoplatform.social.rest.impl.space;
2
3
4
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.ws.rs.core.EntityTag;
12 import javax.ws.rs.core.MultivaluedMap;
13
14 import org.exoplatform.portal.config.UserACL;
15 import org.exoplatform.services.rest.impl.ContainerResponse;
16 import org.exoplatform.services.rest.impl.MultivaluedMapImpl;
17 import org.exoplatform.services.security.MembershipEntry;
18 import org.exoplatform.social.common.RealtimeListAccess;
19 import org.exoplatform.social.core.activity.model.ExoSocialActivity;
20 import org.exoplatform.social.core.activity.model.ExoSocialActivityImpl;
21 import org.exoplatform.social.core.identity.model.Identity;
22 import org.exoplatform.social.core.identity.provider.SpaceIdentityProvider;
23 import org.exoplatform.social.core.manager.ActivityManager;
24 import org.exoplatform.social.core.manager.IdentityManager;
25 import org.exoplatform.social.core.space.impl.DefaultSpaceApplicationHandler;
26 import org.exoplatform.social.core.space.model.Space;
27 import org.exoplatform.social.core.space.spi.SpaceService;
28 import org.exoplatform.social.rest.entity.CollectionEntity;
29 import org.exoplatform.social.rest.entity.SpaceEntity;
30 import org.exoplatform.social.service.test.AbstractResourceTest;
31
32 public class SpaceRestResourcesTest extends AbstractResourceTest {
33 private IdentityManager identityManager;
34 private UserACL userACL;
35 private ActivityManager activityManager;
36 private SpaceService spaceService;
37
38 private SpaceRestResourcesV1 spaceRestResources;
39
40 private Identity rootIdentity;
41 private Identity johnIdentity;
42 private Identity maryIdentity;
43 private Identity demoIdentity;
44
45 public void setUp() throws Exception {
46 super.setUp();
47
48 System.setProperty("gatein.email.domain.url", "localhost:8080");
49
50 identityManager = getContainer().getComponentInstanceOfType(IdentityManager.class);
51 activityManager = getContainer().getComponentInstanceOfType(ActivityManager.class);
52 spaceService = getContainer().getComponentInstanceOfType(SpaceService.class);
53
54 rootIdentity = identityManager.getOrCreateIdentity("organization", "root", true);
55 johnIdentity = identityManager.getOrCreateIdentity("organization", "john", true);
56 maryIdentity = identityManager.getOrCreateIdentity("organization", "mary", true);
57 demoIdentity = identityManager.getOrCreateIdentity("organization", "demo", true);
58
59 spaceRestResources = new SpaceRestResourcesV1(identityManager);
60 registry(spaceRestResources);
61 }
62
63 public void tearDown() throws Exception {
64
65
66
67
68
69
70
71 super.tearDown();
72 removeResource(spaceRestResources.getClass());
73 }
74
75 public void testSpaceVisibilityUpdateWithDifferentCases () throws Exception {
76 startSessionAs("root");
77
78
79
80
81
82 Space space = getSpaceInstance(1, "root");
83
84 Map<String,String> listOfResponses = new HashMap<String,String>() {{
85 put("{\"visibility\":PRIVATE}", Space.PRIVATE);
86 put("{\"visibility\":private}", Space.PRIVATE);
87 put("{\"visibility\":PriVatE}", Space.PRIVATE);
88 put("{\"visibility\":HIDDEN}", Space.HIDDEN);
89 put("{\"visibility\":hidden}", Space.HIDDEN);
90 put("{\"visibility\":HiDdEn}", Space.HIDDEN);
91 }};
92
93 ContainerResponse response = null;
94
95 for (Map.Entry<String, String> entry : listOfResponses.entrySet()) {
96 String input = entry.getKey();
97 String expectedOutput = entry.getValue();
98 response = getResponse("PUT", getURLResource("spaces/" + space.getId()), input);
99 assertNotNull(response);
100 assertEquals(200, response.getStatus());
101 SpaceEntity spaceEntity = getBaseEntity(response.getEntity(), SpaceEntity.class);
102 assertEquals(expectedOutput, spaceEntity.getVisibility());
103 }
104 }
105
106 public void testGetSpaces() throws Exception {
107 getSpaceInstance(1, "root");
108 getSpaceInstance(2, "john");
109 getSpaceInstance(3, "demo");
110
111 startSessionAs("demo");
112 ContainerResponse response = service("GET", getURLResource("spaces?limit=5&offset=0"), "", null, null);
113 assertEquals(200, response.getStatus());
114 CollectionEntity collections = (CollectionEntity) response.getEntity();
115
116 assertEquals(1, collections.getEntities().size());
117
118 HashSet<MembershipEntry> ms = new HashSet<MembershipEntry>();
119 ms.add(new MembershipEntry("/platform/administrators"));
120 startSessionAs("john", ms);
121 response = service("GET", getURLResource("spaces?limit=5&offset=0"), "", null, null);
122 assertNotNull(response);
123 assertEquals(200, response.getStatus());
124 collections = (CollectionEntity) response.getEntity();
125 assertEquals(1, collections.getEntities().size());
126
127
128 startSessionAs("root", ms);
129 response = service("GET", getURLResource("spaces?limit=5&offset=0"), "", null, null);
130 assertNotNull(response);
131 assertEquals(200, response.getStatus());
132 collections = (CollectionEntity) response.getEntity();
133 assertEquals(3, collections.getEntities().size());
134
135 response = service("GET", getURLResource("spaces?limit=5&offset=1"), "", null, null);
136 assertNotNull(response);
137 assertEquals(200, response.getStatus());
138 collections = (CollectionEntity) response.getEntity();
139 assertEquals(2, collections.getEntities().size());
140 }
141
142 public void testShouldUseCacheWhenSpacesDidNotChanged() throws Exception {
143 getSpaceInstance(1, "root");
144 getSpaceInstance(2, "john");
145 getSpaceInstance(3, "demo");
146
147 startSessionAs("root");
148 ContainerResponse response = service("GET", getURLResource("spaces?limit=5&offset=0"), "", null, null);
149 assertNotNull(response);
150 assertEquals(200, response.getStatus());
151 CollectionEntity collections = (CollectionEntity) response.getEntity();
152 assertEquals(3, collections.getEntities().size());
153 EntityTag eTag = (EntityTag) response.getHttpHeaders().getFirst("ETAG");
154 assertNotNull(eTag);
155
156 MultivaluedMap<String,String> headers = new MultivaluedMapImpl();
157 headers.putSingle("If-None-Match", "\"" + eTag.getValue() + "\"");
158 response = service("GET", getURLResource("spaces?limit=5&offset=0"), "", headers, null);
159 assertNotNull(response);
160 assertEquals(304, response.getStatus());
161 }
162
163 public void testCreateSpace() throws Exception {
164 startSessionAs("root");
165 String input = "{\"displayName\":social}";
166
167 ContainerResponse response = getResponse("POST", getURLResource("spaces/"), input);
168 assertNotNull(response);
169 assertEquals(200, response.getStatus());
170
171 SpaceEntity spaceEntity = getBaseEntity(response.getEntity(), SpaceEntity.class);
172 Space space = spaceService.getSpaceById(spaceEntity.getId());
173 assertNotNull(space);
174 assertEquals("social", space.getDisplayName());
175 }
176
177 public void testGetSpace() throws Exception {
178 startSessionAs("root");
179 String input = "{\"displayName\":\"test space\"}";
180
181 ContainerResponse response = getResponse("POST", getURLResource("spaces/"), input);
182 assertNotNull(response);
183 assertEquals(200, response.getStatus());
184
185 SpaceEntity spaceEntity = getBaseEntity(response.getEntity(), SpaceEntity.class);
186 Space space = spaceService.getSpaceById(spaceEntity.getId());
187 assertNotNull(space);
188
189
190 response = service("GET", getURLResource("spaces/" + space.getId()), "", null, null);
191 assertNotNull(response);
192 assertEquals(200, response.getStatus());
193
194 spaceEntity = getBaseEntity(response.getEntity(), SpaceEntity.class);
195 assertNotNull(spaceEntity);
196 assertEquals("test space", spaceEntity.getDisplayName());
197 }
198
199 public void testGetUpdateDeleteSpaceById() throws Exception {
200
201 Space space = getSpaceInstance(1, "root");
202 startSessionAs("root");
203 ContainerResponse response = service("GET", getURLResource("spaces/" + space.getId()), "", null, null);
204 assertNotNull(response);
205 assertEquals(200, response.getStatus());
206
207 SpaceEntity spaceEntity = getBaseEntity(response.getEntity(), SpaceEntity.class);
208 assertEquals("space1", spaceEntity.getDisplayName());
209 assertEquals(Space.PRIVATE, spaceEntity.getVisibility());
210
211
212 String spaceId = spaceEntity.getId();
213 String input = "{\"displayName\":displayName_updated, \"description\":description_updated}";
214 response = getResponse("PUT", getURLResource("spaces/" + spaceId), input);
215 assertNotNull(response);
216 assertEquals(200, response.getStatus());
217 space = spaceService.getSpaceById(spaceId);
218 assertEquals("displayName_updated", space.getDisplayName());
219 assertEquals("description_updated", space.getDescription());
220
221
222 response = service("DELETE", getURLResource("spaces/" + space.getId()), "", null, null);
223 assertNotNull(response);
224 assertEquals(200, response.getStatus());
225 space = spaceService.getSpaceById(spaceId);
226 assertNull(space);
227 }
228
229 public void testGetUsersSpaceById() throws Exception {
230
231 Space space = getSpaceInstance(1, "root");
232 space.setMembers(new String[] {"root", "john", "mary", "demo"});
233 space.setManagers(new String[] {"root", "john"});
234 spaceService.updateSpace(space);
235
236 startSessionAs("root");
237 ContainerResponse response = service("GET", getURLResource("spaces/" + space.getId() + "/users"), "", null, null);
238 assertNotNull(response);
239 assertEquals(200, response.getStatus());
240 CollectionEntity collections = (CollectionEntity) response.getEntity();
241 assertEquals(4, collections.getEntities().size());
242
243 response = service("GET", getURLResource("spaces/" + space.getId() + "/users?role=manager"), "", null, null);
244 assertNotNull(response);
245 assertEquals(200, response.getStatus());
246 collections = (CollectionEntity) response.getEntity();
247 assertEquals(2, collections.getEntities().size());
248 }
249
250 public void testGetActivitiesSpaceById() throws Exception {
251
252 Space space = getSpaceInstance(1, "root");
253 Identity spaceIdentity = identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, space.getPrettyName(), false);
254 for (int i = 0; i < 5 ; i++) {
255 ExoSocialActivity activity = new ExoSocialActivityImpl();
256 activity.setTitle("title " + i);
257 activity.setUserId(rootIdentity.getId());
258 activityManager.saveActivityNoReturn(spaceIdentity, activity);
259 }
260
261 startSessionAs("root");
262 ContainerResponse response = service("GET", getURLResource("spaces/" + space.getId() + "/activities"), "", null, null);
263 assertNotNull(response);
264 assertEquals(200, response.getStatus());
265 CollectionEntity activitiesCollections = (CollectionEntity) response.getEntity();
266 assertEquals(6, activitiesCollections.getEntities().size());
267
268
269 String input = "{\"title\":title6}";
270 response = getResponse("POST", getURLResource("spaces/" + space.getId() + "/activities"), input);
271 assertNotNull(response);
272 assertEquals(200, response.getStatus());
273
274 RealtimeListAccess<ExoSocialActivity> listAccess = activityManager.getActivitiesOfSpaceWithListAccess(spaceIdentity);
275 assertEquals(7, listAccess.getSize());
276 ExoSocialActivity activity = listAccess.load(0, 10)[0];
277 assertEquals("title6", activity.getTitle());
278 }
279
280 private Space getSpaceInstance(int number, String creator) throws Exception {
281 Space space = new Space();
282 space.setDisplayName("space" + number);
283 space.setPrettyName(space.getDisplayName());
284 space.setRegistration(Space.OPEN);
285 space.setDescription("add new space " + number);
286 space.setType(DefaultSpaceApplicationHandler.NAME);
287 space.setVisibility(Space.PRIVATE);
288 space.setRegistration(Space.VALIDATION);
289 space.setPriority(Space.INTERMEDIATE_PRIORITY);
290 this.spaceService.createSpace(space, creator);
291 return space;
292 }
293
294 private List<ExoSocialActivity> getCreatedSpaceActivities(Space space) {
295 Identity spaceIdentity = identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, space.getPrettyName(), false);
296 RealtimeListAccess<ExoSocialActivity> listAccess = activityManager.getActivitiesOfSpaceWithListAccess(spaceIdentity);
297 return listAccess.loadAsList(0, 10);
298 }
299 }