001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.FxLanguage;
038: import com.flexive.shared.content.FxContent;
039: import com.flexive.shared.content.FxPK;
040: import com.flexive.shared.exceptions.*;
041: import com.flexive.shared.interfaces.ContentEngine;
042: import com.flexive.shared.interfaces.MandatorEngine;
043: import com.flexive.shared.interfaces.TypeEngine;
044: import com.flexive.shared.search.query.SqlQueryBuilder;
045: import com.flexive.shared.security.Account;
046: import com.flexive.shared.security.Mandator;
047: import com.flexive.shared.structure.FxType;
048: import com.flexive.shared.structure.FxTypeEdit;
049: import com.flexive.shared.tree.FxTreeMode;
050: import com.flexive.shared.tree.FxTreeNode;
051: import com.flexive.shared.tree.FxTreeNodeEdit;
052: import static com.flexive.tests.embedded.FxTestUtils.*;
053: import org.apache.commons.lang.RandomStringUtils;
054: import org.apache.commons.logging.Log;
055: import org.apache.commons.logging.LogFactory;
056: import org.testng.Assert;
057: import org.testng.annotations.AfterClass;
058: import org.testng.annotations.BeforeClass;
059: import org.testng.annotations.Test;
060:
061: import java.util.Date;
062:
063: /**
064: * Mandator tests.
065: *
066: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
067: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
068: */
069: @Test(groups={"ejb","mandator"})
070: public class MandatorTest {
071:
072: private static transient Log LOG = LogFactory
073: .getLog(MandatorTest.class);
074:
075: private MandatorEngine me = null;
076: private TypeEngine te = null;
077: private ContentEngine ce = null;
078: private long testMandator = -1;
079: private long testType = -1;
080:
081: @BeforeClass
082: public void beforeClass() throws Exception {
083: me = EJBLookup.getMandatorEngine();
084: te = EJBLookup.getTypeEngine();
085: ce = EJBLookup.getContentEngine();
086: login(TestUsers.SUPERVISOR);
087: try {
088: testMandator = me.create("MANDATOR_"
089: + RandomStringUtils.randomAlphanumeric(10), true);
090: testType = te.save(FxTypeEdit.createNew("TEST_"
091: + RandomStringUtils.randomAlphanumeric(10)));
092: } catch (FxApplicationException e) {
093: LOG.error(e);
094: }
095: }
096:
097: @AfterClass
098: public void afterClass() throws Exception {
099: if (testMandator != -1) {
100: if (testType != -1)
101: ce.removeForType(testType);
102: me.remove(testMandator);
103: }
104: logout();
105: }
106:
107: /**
108: * Tests if the current mandator (= the test user's mandator) is active.
109: * If it wasn't active, the test user may not have been permitted to log in
110: * at all.
111: *
112: * @throws Exception if an error occured
113: */
114: @Test
115: public void currentMandatorActive() throws Exception {
116: Mandator mandator = CacheAdmin.getEnvironment().getMandator(
117: getUserTicket().getMandatorId());
118: assert mandator.isActive() : "Current mandator is inactive.";
119: }
120:
121: /**
122: * Tests the creation/removal of new mandators.
123: *
124: * @throws Exception if an error occured
125: */
126: @Test
127: public void createRemoveMandator() throws Exception {
128: int mandatorId = me.create("TestMandator", false);
129: Mandator test = null;
130: try {
131: test = CacheAdmin.getEnvironment().getMandator(mandatorId);
132: } catch (Exception e) {
133: assert false : "Failed to get created mandator from cache: "
134: + e.getMessage();
135: }
136: assert test != null : "Loaded mandator is null!";
137: assert test.getName().equals("TestMandator") : "Name mismatch!";
138: assert !test.hasMetadata() : "Mandator should have no meta data attached!";
139: assert !test.isActive() : "Mandator should be inactive!";
140: me.activate(test.getId());
141: try {
142: test = CacheAdmin.getEnvironment().getMandator(mandatorId);
143: } catch (Exception e) {
144: assert false : "Failed to get activated mandator from cache: "
145: + e.getMessage();
146: }
147: assert test.isActive() : "Mandator should be active!";
148: me.deactivate(test.getId());
149: try {
150: test = CacheAdmin.getEnvironment().getMandator(mandatorId);
151: } catch (Exception e) {
152: assert false : "Failed to get deactivated mandator from cache: "
153: + e.getMessage();
154: }
155: assert !test.isActive() : "Mandator should be deactivated!";
156:
157: me.remove(test.getId());
158: try {
159: CacheAdmin.getEnvironment().getMandator(mandatorId);
160: assert false : "Removed mandator could be retrieved from cache!";
161: } catch (Exception e) {
162: //ignore
163: }
164: try {
165: me.remove(Mandator.MANDATOR_FLEXIVE); //try to remove the public mandator -> got to fail
166: assert false : "Removing the public mandator should have failed!";
167: } catch (FxEntryInUseException e) {
168: //ok
169: } catch (Exception e) {
170: assert false : "Unexpected exception: " + e.getMessage();
171: }
172: }
173:
174: /**
175: * Test active/inactive mandators with contents
176: *
177: * @throws FxApplicationException on errors
178: */
179: public void activeContent() throws FxApplicationException {
180: me.activate(testMandator); //make sure the mandator is active
181: FxType type = CacheAdmin.getEnvironment().getType(testType);
182: Assert.assertTrue(CacheAdmin.getEnvironment().getMandator(
183: testMandator).isActive(),
184: "Expected mandator to be active!");
185: FxContent co_act1 = ce.initialize(testType, testMandator, -1,
186: type.getWorkflow().getSteps().get(0).getId(),
187: FxLanguage.DEFAULT_ID);
188: me.deactivate(testMandator);
189: Assert.assertFalse(CacheAdmin.getEnvironment().getMandator(
190: testMandator).isActive(),
191: "Expected mandator to be inactive!");
192: try {
193: ce.initialize(testType, testMandator, -1, type
194: .getWorkflow().getSteps().get(0).getId(),
195: FxLanguage.DEFAULT_ID);
196: Assert
197: .fail("Initialize on a deactivated mandator should fail!");
198: } catch (FxNotFoundException e) {
199: //expected
200: } catch (Exception ex) {
201: Assert.fail("FxNotFoundException expected! Got: "
202: + ex.getClass().getCanonicalName());
203: }
204: try {
205: ce.save(co_act1);
206: Assert.fail("Save on a deactivated mandator should fail!");
207: } catch (FxCreateException e) {
208: //expected
209: } catch (Exception ex) {
210: Assert.fail("FxCreateException expected! Got: "
211: + ex.getClass().getCanonicalName());
212: }
213: me.activate(testMandator);
214: FxPK pk = ce.save(co_act1);
215: FxContent co_loaded = ce.load(pk);
216: FxPK pk_ver = ce.createNewVersion(co_loaded);
217: ce.removeVersion(pk_ver);
218: ce.remove(pk);
219: pk = ce.save(co_act1);
220: me.deactivate(testMandator);
221: try {
222: ce.load(pk);
223: Assert.fail("Load on a deactivated mandator should fail!");
224: } catch (FxNotFoundException e) {
225: //expected
226: } catch (Exception ex) {
227: Assert.fail("FxNotFoundException expected! Got: "
228: + ex.getClass().getCanonicalName());
229: }
230: me.activate(testMandator);
231: co_loaded = ce.load(pk);
232: me.deactivate(testMandator);
233: try {
234: ce.createNewVersion(co_loaded);
235: Assert
236: .fail("CreateNewVersion on a deactivated mandator should fail!");
237: } catch (FxNotFoundException e) {
238: //expected
239: } catch (Exception ex) {
240: Assert.fail("FxNotFoundException expected! Got: "
241: + ex.getClass().getCanonicalName());
242: }
243: me.activate(testMandator);
244: pk_ver = ce.createNewVersion(co_loaded);
245: me.deactivate(testMandator);
246: try {
247: ce.removeVersion(pk_ver);
248: Assert
249: .fail("RemoveVersion on a deactivated mandator should fail!");
250: } catch (FxNotFoundException e) {
251: //expected
252: } catch (Exception ex) {
253: Assert.fail("FxNotFoundException expected! Got: "
254: + ex.getClass().getCanonicalName());
255: }
256: try {
257: ce.remove(pk);
258: Assert
259: .fail("Remove on a deactivated mandator should fail!");
260: } catch (FxRemoveException e) {
261: //expected
262: } catch (Exception ex) {
263: Assert.fail("FxRemoveException expected! Got: "
264: + ex.getClass().getCanonicalName());
265: }
266: me.activate(testMandator);
267: }
268:
269: /**
270: * Test active/inactive mandators with the tree
271: *
272: * @throws FxApplicationException on errors
273: */
274: public void activeTree() throws Exception {
275: EJBLookup.getTreeEngine().clear(FxTreeMode.Edit);
276: me.activate(testMandator); //make sure we're active
277: FxType type = CacheAdmin.getEnvironment().getType(testType);
278: Assert.assertTrue(CacheAdmin.getEnvironment().getMandator(
279: testMandator).isActive(),
280: "Expected mandator to be active!");
281: FxContent co_act1 = ce.initialize(testType, testMandator, -1,
282: type.getWorkflow().getSteps().get(0).getId(),
283: FxLanguage.DEFAULT_ID);
284: FxPK pk = ce.save(co_act1);
285: EJBLookup.getTreeEngine().getNode(FxTreeMode.Edit,
286: FxTreeNode.ROOT_NODE).getTotalChildCount();
287: long folder = EJBLookup.getTreeEngine().save(
288: FxTreeNodeEdit.createNew("MandatorTestFolder"));
289: long node_root = EJBLookup.getTreeEngine().save(
290: FxTreeNodeEdit.createNew("MandatorTestRoot")
291: .setReference(pk));
292: EJBLookup.getTreeEngine().save(
293: FxTreeNodeEdit.createNew("MandatorTestChild")
294: .setReference(pk).setParentNodeId(folder));
295: FxTreeNode node = EJBLookup.getTreeEngine().getTree(
296: FxTreeMode.Edit, FxTreeNode.ROOT_NODE, 100);
297: Assert.assertEquals(node.getTotalChildCount(), 3);
298: Assert.assertEquals(node.getDirectChildCount(), 2);
299: Assert.assertEquals(node.getChildren().size(), 2);
300: Assert.assertEquals(node.getChildren().get(0).getChildren()
301: .size(), 1);
302: me.deactivate(testMandator);
303: node = EJBLookup.getTreeEngine().getTree(FxTreeMode.Edit,
304: FxTreeNode.ROOT_NODE, 100);
305: Assert.assertEquals(node.getChildren().size(), 1);
306: Assert.assertEquals(node.getChildren().get(0).getChildren()
307: .size(), 0);
308: try {
309: EJBLookup.getTreeEngine().getNode(FxTreeMode.Edit,
310: node_root);
311: Assert
312: .fail("getNode on a deactivated mandator should fail!");
313: } catch (FxNotFoundException e) {
314: //expected
315: } catch (Exception ex) {
316: Assert.fail("FxNotFoundException expected! Got: "
317: + ex.getClass().getCanonicalName());
318: }
319: me.activate(testMandator);
320: node = EJBLookup.getTreeEngine().getTree(FxTreeMode.Edit,
321: FxTreeNode.ROOT_NODE, 100);
322: Assert.assertEquals(node.getChildren().size(), 2);
323: Assert.assertEquals(node.getChildren().get(0).getChildren()
324: .size(), 1);
325: EJBLookup.getTreeEngine().getNode(FxTreeMode.Edit, node_root);
326: EJBLookup.getTreeEngine().clear(FxTreeMode.Edit);
327: }
328:
329: /**
330: * Test active/inactive mandators concerning login
331: *
332: * @throws Exception on errors
333: */
334: public void activeLogin() throws Exception {
335: String name = "USR_" + RandomStringUtils.randomAlphanumeric(10);
336: String pwd = RandomStringUtils.randomAlphanumeric(10);
337: long accountId = EJBLookup.getAccountEngine().create(name,
338: name, pwd, "test@flexive.org", FxLanguage.ENGLISH,
339: testMandator, true, true, new Date(),
340: Account.VALID_FOREVER, 0, "", false, true);
341: try {
342: me.activate(testMandator);
343: logout();
344: //check if login/logout works while active
345: login(name, pwd);
346: logout();
347: login(TestUsers.SUPERVISOR);
348: me.deactivate(testMandator);
349: try {
350: EJBLookup.getAccountEngine().login(name, pwd, false);
351: Assert
352: .fail("Expected an account of a deactivated mandator to not be able to log in.");
353: } catch (FxLoginFailedException e) {
354: //expected
355: }
356: logout();
357: login(TestUsers.SUPERVISOR);
358: me.activate(testMandator);
359: //check if login/logout works again
360: login(name, pwd);
361: logout();
362: } finally {
363: login(TestUsers.SUPERVISOR);
364: EJBLookup.getAccountEngine().remove(accountId);
365: }
366: }
367:
368: /**
369: * Test active/inactive mandators concerning queries
370: *
371: * @throws Exception on errors
372: */
373: public void activeQuery() throws Exception {
374: me.activate(testMandator); //make sure the mandator is active
375: FxType type = CacheAdmin.getEnvironment().getType(testType);
376: FxContent co_act = ce.initialize(testType, testMandator, -1,
377: type.getWorkflow().getSteps().get(0).getId(),
378: FxLanguage.DEFAULT_ID);
379: FxPK pk = ce.save(co_act);
380: long org = new SqlQueryBuilder().type(testType).getResult()
381: .getRowCount();
382: Assert.assertTrue(org > 0, "Expected at least one result!");
383: me.deactivate(testMandator);
384: Assert.assertEquals(new SqlQueryBuilder().type(testType)
385: .getResult().getRowCount(), 0, "Expected 0 results!");
386: me.activate(testMandator);
387: Assert.assertEquals(new SqlQueryBuilder().type(testType)
388: .getResult().getRowCount(), org, "Expected " + org
389: + " results!");
390: ce.remove(pk);
391: }
392: }
|