001: package org.obe.test.standalone;
002:
003: import java.security.Principal;
004: import java.util.ArrayList;
005: import java.util.Arrays;
006: import java.util.List;
007: import junit.framework.TestCase;
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.obe.client.api.repository.RepositoryException;
011: import org.obe.engine.WorkflowEngine;
012: import org.obe.runtime.strategy.AllAssignmentStrategy;
013: import org.obe.spi.model.ProcessInstance;
014: import org.obe.spi.service.SecurityRealm;
015: import org.obe.xpdl.model.participant.ParticipantType;
016: import org.wfmc.wapi.WMWorkflowException;
017:
018: /**
019: * Tests the BasicSecurityRealm.
020: *
021: * @author Adrian Price
022: */
023: public class BasicRealmTest extends TestCase {
024: private static final Log _logger = LogFactory
025: .getLog(BasicRealmTest.class);
026: private static final SecurityRealm _realm = WorkflowEngine
027: .getEngine().getServiceManager().getSecurityRealm();
028: private static final AllAssignmentStrategy _strategy = new AllAssignmentStrategy();
029: private static final String[][] _testData = {
030: // id, pType, [,expected]...
031: { "testuser1", ParticipantType.HUMAN.toString(),
032: "testuser1" },
033: { "testgroup1", ParticipantType.ROLE.toString(),
034: "testuser1", "testuser2" },
035: { "testgroup2", ParticipantType.ROLE.toString(),
036: "testuser2", "testuser3" },
037: { "testgroup3", ParticipantType.ROLE.toString(),
038: "testuser2", "testuser3", "testuser4" },
039: { "testorg1",
040: ParticipantType.ORGANIZATIONAL_UNIT.toString(), }, // (exception)
041: { "testres1", ParticipantType.RESOURCE.toString(), }, // (exception)
042: { "testsys1", ParticipantType.SYSTEM.toString(), }, // (exception)
043: // {"testuser1", ParticipantType.ROLE.toString(), }, // (exception)
044: // {"testgroup1", ParticipantType.HUMAN.toString(), }, // (exception)
045: { "nouser", ParticipantType.HUMAN.toString(), }, // (exception)
046: { "nogroup", ParticipantType.ROLE.toString(), } // (exception)
047: };
048:
049: public void testFindPrincipal() throws RepositoryException {
050: for (int i = 0; i < _testData.length; i++) {
051: String[] td = _testData[i];
052: String prefix = "_testData[" + i + "]";
053: String name = td[0];
054: ParticipantType pType = ParticipantType.valueOf(td[1]);
055: try {
056: Principal principal = _realm.findPrincipal(name);
057:
058: // If we were expecting an exception, we didn't get one.
059: if (td.length == 2 && pType != ParticipantType.HUMAN
060: && pType != ParticipantType.ROLE) {
061:
062: fail(prefix + " failed to throw an exception");
063: }
064:
065: assertEquals(prefix, name, principal.getName());
066: } catch (RepositoryException e) {
067: // If we were expecting an exception, we got one.
068: if (td.length == 2)
069: continue;
070: throw e;
071: }
072: _logger.info("findPrincipal(\"" + name + "\" passed");
073: }
074: }
075:
076: public void testResolveParticipants() throws RepositoryException,
077: WMWorkflowException {
078:
079: List expected = new ArrayList();
080: ProcessInstance procInst = null;
081: for (int i = 0; i < _testData.length; i++) {
082: // Prepare the test input.
083: String[] td = _testData[i];
084: String id = td[0];
085:
086: // Prepare the expected output.
087: expected.clear();
088: for (int j = 2; j < td.length; j++)
089: expected.add(td[j]);
090:
091: // Invoke the repository method.
092: String prefix = "_testData[" + i + "]";
093: String[] result;
094: try {
095: Principal[] principals = _realm.resolveParticipants(id,
096: procInst);
097: assertEquals(prefix
098: + " resolved _principal count is wrong;", 1,
099: principals.length);
100: assertEquals(prefix
101: + " resolved _principal ID is wrong;", id,
102: principals[0].getName());
103: principals = _strategy.apply(principals, true, null);
104: result = new String[principals.length];
105: for (int j = 0; j < principals.length; j++)
106: result[j] = principals[j].getName();
107:
108: // If we were expecting an exception, we didn't get one.
109: if (td.length == 2)
110: fail(prefix + " failed to throw an exception");
111: } catch (RepositoryException e) {
112: // If we were expecting an exception, we got one.
113: if (td.length == 2)
114: continue;
115: throw e;
116: }
117: List actual = Arrays.asList(result);
118:
119: // Verify the results.
120: assertEquals(prefix + " count is wrong:", expected.size(),
121: actual.size());
122: assertTrue(prefix + " resolved members incorrect", expected
123: .containsAll(actual));
124: _logger.info("resolveParticipants(" + id + ", " + procInst
125: + ") passed");
126: }
127: }
128: }
|