001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.outerj.daisy.repository.test;
017:
018: import org.outerj.daisy.repository.testsupport.AbstractDaisyTestCase;
019: import org.outerj.daisy.repository.*;
020: import org.outerj.daisy.repository.schema.DocumentType;
021: import org.outerj.daisy.repository.user.Role;
022: import org.outerj.daisy.repository.user.User;
023: import org.outerj.daisy.emailnotifier.EmailSubscriptionManager;
024: import org.outerj.daisy.emailnotifier.Subscription;
025: import org.outerj.daisy.emailnotifier.CollectionSubscriptionKey;
026:
027: import java.util.HashSet;
028: import java.util.Arrays;
029:
030: public abstract class AbstractEmailSubscriptionTest extends
031: AbstractDaisyTestCase {
032: protected boolean resetDataStores() {
033: return true;
034: }
035:
036: protected abstract RepositoryManager getRepositoryManager()
037: throws Exception;
038:
039: public void testSubscriptions() throws Exception {
040: RepositoryManager repositoryManager = getRepositoryManager();
041: Repository repository = repositoryManager
042: .getRepository(new Credentials("testuser", "testuser"));
043: repository.switchRole(Role.ADMINISTRATOR);
044:
045: // create some content
046: CollectionManager collectionManager = repository
047: .getCollectionManager();
048: DocumentCollection collection1 = collectionManager
049: .createCollection("collection1");
050: collection1.save();
051: DocumentCollection collection2 = collectionManager
052: .createCollection("collection2");
053: collection2.save();
054:
055: DocumentType documentType = repository.getRepositorySchema()
056: .createDocumentType("testdoctype");
057: documentType.save();
058:
059: Document document1 = repository.createDocument("doc1",
060: "testdoctype");
061: document1.addToCollection(collection1);
062: document1.addToCollection(collection2);
063: document1.save();
064:
065: Document document2 = repository.createDocument("doc2",
066: "testdoctype");
067: document2.addToCollection(collection1);
068: document2.addToCollection(collection2);
069: document2.save();
070:
071: EmailSubscriptionManager emailSubscriptionManager = (EmailSubscriptionManager) repository
072: .getExtension("EmailSubscriptionManager");
073:
074: Subscription subscription = emailSubscriptionManager
075: .getSubscription();
076: subscription.setReceiveDocumentEvents(true);
077: subscription.save();
078:
079: assertEquals(1, emailSubscriptionManager.getSubscriptions()
080: .getArray().length);
081:
082: // Create a non-admin role and user
083: Role userRole = repository.getUserManager().getRole("User",
084: false);
085:
086: User jules = repository.getUserManager().createUser("jules");
087: jules.addToRole(userRole);
088: jules.setDefaultRole(userRole);
089: jules.setPassword("jules");
090: jules.save();
091:
092: Repository julesRepository = repositoryManager
093: .getRepository(new Credentials("jules", "jules"));
094: EmailSubscriptionManager julesSM = (EmailSubscriptionManager) julesRepository
095: .getExtension("EmailSubscriptionManager");
096: Subscription julesSubscription = julesSM.getSubscription();
097: julesSubscription.setReceiveDocumentEvents(true);
098: julesSubscription.setReceiveSchemaEvents(true);
099: julesSubscription.setReceiveCommentEvents(true);
100: julesSubscription.save();
101:
102: julesSubscription = julesSM.getSubscription();
103: assertEquals(julesSubscription.getUserId(), jules.getId());
104: assertEquals(julesSubscription.getReceiveDocumentEvents(), true);
105: assertEquals(julesSubscription.getReceiveSchemaEvents(), true);
106: assertEquals(julesSubscription.getReceiveUserEvents(), false);
107: assertEquals(julesSubscription.getReceiveCollectionEvents(),
108: false);
109: assertEquals(julesSubscription.getReceiveAclEvents(), false);
110: assertEquals(julesSubscription.getReceiveCommentEvents(), true);
111:
112: try {
113: julesSM.getSubscription(repository.getUserId());
114: fail("User jules shouldn't be able to retrieve subscription of other user.");
115: } catch (Exception e) {
116: }
117:
118: try {
119: julesSM.deleteSubscription(repository.getUserId());
120: fail("User jules shouldn't be able to delete subscription of other user.");
121: } catch (Exception e) {
122: }
123:
124: try {
125: julesSM.getSubscriptions();
126: fail("User jules shouldn't be able to get list of all subscriptions.");
127: } catch (Exception e) {
128: }
129:
130: try {
131: julesSM.deleteAllSubscriptionsForDocument("1");
132: fail("User jules shouldn't be able to delete all subscriptions of a document.");
133: } catch (Exception e) {
134: }
135:
136: try {
137: julesSM.deleteAllSubscriptionsForCollection(1);
138: fail("User jules shouldn't be able to delete all subscriptions of a collection.");
139: } catch (Exception e) {
140: }
141:
142: assertEquals(0, emailSubscriptionManager
143: .getAllCollectionEventSubscribers().getArray().length);
144: assertEquals(1, emailSubscriptionManager
145: .getAllSchemaEventSubscribers().getArray().length);
146: assertEquals(0, emailSubscriptionManager
147: .getAllUserEventSubscribers().getArray().length);
148:
149: julesSubscription
150: .setSubscribedVariantKeys(new VariantKey[] { new VariantKey(
151: document1.getId(), -1, -1) });
152: julesSubscription.save();
153:
154: julesSM.addDocumentSubscription(new VariantKey(document2
155: .getId(), -1, -1));
156:
157: julesSubscription = julesSM.getSubscription();
158: assertEquals(2,
159: julesSubscription.getSubscribedVariantKeys().length);
160:
161: assertEquals(1, emailSubscriptionManager
162: .getAllDocumentEventSubscribers(document1.getId(), -1,
163: -1, new long[] {}).getArray().length);
164: assertEquals(1, emailSubscriptionManager
165: .getAllCommentEventSubscribers(document1.getId(), -1,
166: -1, new long[] {}).getArray().length);
167:
168: emailSubscriptionManager
169: .addDocumentSubscription(new VariantKey(document1
170: .getId(), -1, -1));
171: assertEquals(2, emailSubscriptionManager
172: .getAllDocumentEventSubscribers(document1.getId(), -1,
173: -1, new long[] {}).getArray().length);
174:
175: julesSubscription.setSubscribedVariantKeys(new VariantKey[] {});
176: julesSubscription
177: .setSubscribedCollectionKeys(new CollectionSubscriptionKey[] {
178: new CollectionSubscriptionKey(collection1
179: .getId(), -1, -1),
180: new CollectionSubscriptionKey(collection2
181: .getId(), -1, -1) });
182: julesSubscription.save();
183:
184: assertEquals(2,
185: emailSubscriptionManager
186: .getAllDocumentEventSubscribers(
187: document1.getId(),
188: -1,
189: -1,
190: new long[] { collection1.getId(),
191: collection2.getId() })
192: .getArray().length);
193:
194: julesSM.addDocumentSubscription(new VariantKey(document1
195: .getId(), -1, -1));
196: julesSM.deleteDocumentSubscription(new VariantKey(document1
197: .getId(), -1, -1));
198:
199: julesSubscription = julesSM.getSubscription();
200: assertEquals(0,
201: julesSubscription.getSubscribedVariantKeys().length);
202:
203: emailSubscriptionManager.addDocumentSubscription(jules.getId(),
204: new VariantKey(document1.getId(), -1, -1));
205: emailSubscriptionManager.addDocumentSubscription(jules.getId(),
206: new VariantKey(document2.getId(), -1, -1));
207: julesSubscription = julesSM.getSubscription();
208: assertEquals(2,
209: julesSubscription.getSubscribedVariantKeys().length);
210:
211: emailSubscriptionManager
212: .deleteAllSubscriptionsForDocument(document1.getId());
213: julesSubscription = julesSM.getSubscription();
214: assertEquals(1,
215: julesSubscription.getSubscribedVariantKeys().length);
216:
217: assertTrue(julesSM.isSubsribed(new VariantKey(
218: document2.getId(), -1, -1)));
219:
220: julesSM.deleteSubscription();
221:
222: // user with admin privileges should be able to manipulate other users' subscriptions
223: julesSubscription = emailSubscriptionManager
224: .getSubscription(jules.getId());
225: julesSubscription.setReceiveSchemaEvents(false);
226: julesSubscription.save();
227:
228: // try subscription deletion
229: julesSM.deleteSubscription();
230:
231: // try implicit subscription creation when calling addDocumentSubscription
232: User jef = repository.getUserManager().createUser("jef");
233: jef.addToRole(userRole);
234: jef.setDefaultRole(userRole);
235: jef.setPassword("jef");
236: jef.save();
237:
238: Repository jefRepository = repositoryManager
239: .getRepository(new Credentials("jef", "jef"));
240: EmailSubscriptionManager jefSM = (EmailSubscriptionManager) jefRepository
241: .getExtension("EmailSubscriptionManager");
242: jefSM.addDocumentSubscription(new VariantKey(document1.getId(),
243: -1, -1));
244:
245: Subscription jefSubscription = jefSM.getSubscription();
246: assertEquals(1,
247: jefSubscription.getSubscribedVariantKeys().length);
248:
249: jefSM.deleteSubscription();
250: emailSubscriptionManager.deleteSubscription();
251:
252: //
253: // Test variant-specific subscription things
254: //
255:
256: // Note: for the subscription manager it doesn't matter whether branches & languages
257: // actually exist (nor documents for that matter), so we just use fake numbers here
258:
259: // first reset the subscription
260: julesSubscription = julesSM.getSubscription();
261: julesSubscription.setSubscribedVariantKeys(new VariantKey[] {});
262: julesSubscription
263: .setSubscribedCollectionKeys(new CollectionSubscriptionKey[] {});
264: julesSubscription.save();
265:
266: // add subscription for different languages & branches
267: julesSM.addDocumentSubscription(new VariantKey(document1
268: .getId(), 1, 1));
269: julesSM.addDocumentSubscription(new VariantKey(document1
270: .getId(), 2, 2));
271: julesSM.addDocumentSubscription(new VariantKey(document2
272: .getId(), 1, -1));
273:
274: julesSubscription = julesSM.getSubscription();
275: HashSet keys = new HashSet(Arrays.asList(julesSubscription
276: .getSubscribedVariantKeys()));
277: assertEquals(3, keys.size());
278: assertTrue(keys
279: .contains(new VariantKey(document1.getId(), 1, 1)));
280: assertTrue(keys
281: .contains(new VariantKey(document1.getId(), 2, 2)));
282: assertTrue(keys.contains(new VariantKey(document2.getId(), 1,
283: -1)));
284:
285: assertTrue(julesSM.isSubsribed(new VariantKey(
286: document2.getId(), 1, -1)));
287: assertTrue(julesSM.isSubsribed(new VariantKey(
288: document1.getId(), 1, 1)));
289: assertFalse(julesSM.isSubsribed(new VariantKey(document1
290: .getId(), 1, 2)));
291:
292: julesSM.deleteDocumentSubscription(new VariantKey(document1
293: .getId(), 1, 1));
294: julesSM.deleteDocumentSubscription(new VariantKey(document1
295: .getId(), 2, 2));
296: julesSM.deleteDocumentSubscription(new VariantKey(document2
297: .getId(), 1, -1));
298:
299: assertEquals(0, julesSM.getSubscription()
300: .getSubscribedVariantKeys().length);
301:
302: julesSubscription = julesSM.getSubscription();
303: julesSubscription.setReceiveDocumentEvents(true);
304: julesSubscription
305: .setSubscribedCollectionKeys(new CollectionSubscriptionKey[] { new CollectionSubscriptionKey(
306: collection1.getId(), 1, -1) });
307: julesSubscription.save();
308:
309: assertEquals(1, emailSubscriptionManager
310: .getAllDocumentEventSubscribers(document1.getId(), 1,
311: 123, new long[] { collection1.getId() })
312: .getArray().length);
313: assertEquals(0, emailSubscriptionManager
314: .getAllDocumentEventSubscribers(document1.getId(), 2,
315: 1, new long[] { collection1.getId() })
316: .getArray().length);
317: }
318: }
|