TSK-101: Run TaskServiceImplIntExplicitTest with JAASRunner

This commit is contained in:
Konstantin Kläger 2017-12-11 10:48:28 +01:00 committed by Holger Hagen
parent afcca3b24b
commit b33afe838c
4 changed files with 414 additions and 387 deletions

View File

@ -1,384 +1,324 @@
package pro.taskana.impl.integration; package pro.taskana.impl.integration;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStreamReader; import java.sql.Connection;
import java.security.Principal; import java.sql.SQLException;
import java.security.PrivilegedActionException; import java.sql.Timestamp;
import java.security.PrivilegedExceptionAction; import java.time.LocalDateTime;
import java.sql.Connection; import java.util.List;
import java.sql.SQLException;
import java.sql.Timestamp; import javax.security.auth.login.LoginException;
import java.time.LocalDateTime; import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List; import org.h2.store.fs.FileUtils;
import org.junit.After;
import javax.security.auth.Subject; import org.junit.AfterClass;
import javax.security.auth.login.LoginException; import org.junit.Assert;
import javax.sql.DataSource; import org.junit.Before;
import org.junit.BeforeClass;
import org.apache.ibatis.jdbc.ScriptRunner; import org.junit.Test;
import org.h2.store.fs.FileUtils; import org.junit.runner.RunWith;
import org.junit.After;
import org.junit.AfterClass; import pro.taskana.security.JAASRunner;
import org.junit.Assert; import pro.taskana.security.WithAccessId;
import org.junit.Before; import pro.taskana.Classification;
import org.junit.BeforeClass; import pro.taskana.ClassificationQuery;
import org.junit.Test; import pro.taskana.ClassificationService;
import pro.taskana.ObjectReferenceQuery;
import pro.taskana.Classification; import pro.taskana.TaskanaEngine;
import pro.taskana.ClassificationQuery; import pro.taskana.WorkbasketService;
import pro.taskana.ClassificationService; import pro.taskana.TaskanaEngine.ConnectionManagementMode;
import pro.taskana.ObjectReferenceQuery; import pro.taskana.configuration.TaskanaEngineConfiguration;
import pro.taskana.TaskanaEngine; import pro.taskana.exceptions.ClassificationAlreadyExistException;
import pro.taskana.TaskanaEngine.ConnectionManagementMode; import pro.taskana.exceptions.ClassificationNotFoundException;
import pro.taskana.configuration.TaskanaEngineConfiguration; import pro.taskana.exceptions.NotAuthorizedException;
import pro.taskana.exceptions.ClassificationAlreadyExistException; import pro.taskana.exceptions.TaskNotFoundException;
import pro.taskana.exceptions.ClassificationNotFoundException; import pro.taskana.exceptions.WorkbasketNotFoundException;
import pro.taskana.exceptions.NotAuthorizedException; import pro.taskana.impl.ClassificationQueryImpl;
import pro.taskana.exceptions.TaskNotFoundException; import pro.taskana.impl.ClassificationServiceImpl;
import pro.taskana.exceptions.WorkbasketNotFoundException; import pro.taskana.impl.ObjectReferenceQueryImpl;
import pro.taskana.impl.ClassificationQueryImpl; import pro.taskana.impl.TaskServiceImpl;
import pro.taskana.impl.ClassificationServiceImpl; import pro.taskana.impl.TaskanaEngineImpl;
import pro.taskana.impl.ObjectReferenceQueryImpl; import pro.taskana.impl.WorkbasketServiceImpl;
import pro.taskana.impl.TaskServiceImpl; import pro.taskana.impl.configuration.DBCleaner;
import pro.taskana.impl.TaskanaEngineImpl; import pro.taskana.impl.configuration.TaskanaEngineConfigurationTest;
import pro.taskana.impl.WorkbasketServiceImpl; import pro.taskana.impl.util.IdGenerator;
import pro.taskana.impl.configuration.DBCleaner; import pro.taskana.model.ClassificationImpl;
import pro.taskana.impl.configuration.TaskanaEngineConfigurationTest; import pro.taskana.model.ObjectReference;
import pro.taskana.model.ClassificationImpl; import pro.taskana.model.Task;
import pro.taskana.model.ObjectReference; import pro.taskana.model.TaskState;
import pro.taskana.model.Task; import pro.taskana.model.Workbasket;
import pro.taskana.model.TaskState; import pro.taskana.model.WorkbasketAccessItem;
import pro.taskana.model.Workbasket;
import pro.taskana.security.GroupPrincipal; /**
import pro.taskana.security.UserPrincipal; * Integration Test for TaskServiceImpl transactions with connection management mode EXPLICIT.
* @author EH
/** */
* Integration Test for TaskServiceImpl transactions with connection management mode EXPLICIT. @RunWith(JAASRunner.class)
* @author EH public class TaskServiceImplIntExplicitTest {
*/
public class TaskServiceImplIntExplicitTest { private DataSource dataSource;
private TaskServiceImpl taskServiceImpl;
private DataSource dataSource; private TaskanaEngineConfiguration taskanaEngineConfiguration;
private TaskServiceImpl taskServiceImpl; private TaskanaEngine taskanaEngine;
private TaskanaEngineConfiguration taskanaEngineConfiguration; private TaskanaEngineImpl taskanaEngineImpl;
private TaskanaEngine taskanaEngine; private ClassificationService classificationService;
private TaskanaEngineImpl taskanaEngineImpl; private WorkbasketService workBasketService;
private Subject subject;
private ClassificationService classificationService; @BeforeClass
public static void resetDb() throws SQLException {
@BeforeClass DataSource ds = TaskanaEngineConfigurationTest.getDataSource();
public static void resetDb() throws SQLException { DBCleaner cleaner = new DBCleaner();
DataSource ds = TaskanaEngineConfigurationTest.getDataSource(); cleaner.clearDb(ds, true);
DBCleaner cleaner = new DBCleaner(); }
cleaner.clearDb(ds, true);
} @Before
public void setup() throws FileNotFoundException, SQLException, LoginException {
@Before dataSource = TaskanaEngineConfigurationTest.getDataSource();
public void setup() throws FileNotFoundException, SQLException, LoginException { taskanaEngineConfiguration = new TaskanaEngineConfiguration(dataSource, false);
dataSource = TaskanaEngineConfigurationTest.getDataSource(); taskanaEngine = taskanaEngineConfiguration.buildTaskanaEngine();
taskanaEngineConfiguration = new TaskanaEngineConfiguration(dataSource, false); taskServiceImpl = (TaskServiceImpl) taskanaEngine.getTaskService();
taskanaEngine = taskanaEngineConfiguration.buildTaskanaEngine(); taskanaEngineImpl = (TaskanaEngineImpl) taskanaEngine;
taskServiceImpl = (TaskServiceImpl) taskanaEngine.getTaskService(); classificationService = taskanaEngine.getClassificationService();
taskanaEngineImpl = (TaskanaEngineImpl) taskanaEngine; taskanaEngineImpl.setConnectionManagementMode(ConnectionManagementMode.EXPLICIT);
classificationService = taskanaEngine.getClassificationService(); workBasketService = taskanaEngine.getWorkbasketService();
taskanaEngineImpl.setConnectionManagementMode(ConnectionManagementMode.EXPLICIT); DBCleaner cleaner = new DBCleaner();
DBCleaner cleaner = new DBCleaner(); cleaner.clearDb(dataSource, false);
cleaner.clearDb(dataSource, false); }
subject = new Subject(); @WithAccessId(userName = "Elena")
List<Principal> principalList = new ArrayList<>(); @Test(expected = TaskNotFoundException.class)
principalList.add(new UserPrincipal("Elena")); public void testStartTransactionFail()
principalList.add(new GroupPrincipal("group1")); throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException {
principalList.add(new GroupPrincipal("group2")); Connection connection = dataSource.getConnection();
principalList.add(new GroupPrincipal("group3")); taskanaEngineImpl.setConnection(connection);
subject.getPrincipals().addAll(principalList); // taskServiceImpl = (TaskServiceImpl) taskanaEngine.getTaskService();
try { generateSampleAccessItems();
Connection connection = dataSource.getConnection();
ScriptRunner runner = new ScriptRunner(connection); Workbasket workbasket = new Workbasket();
runner.runScript( workbasket.setName("workbasket");
new InputStreamReader(this.getClass().getResourceAsStream("/sql/workbasket-access-list.sql"))); workbasket.setId("1"); // set id manually for authorization tests
Classification classification = classificationService.newClassification();
} catch (SQLException e1) { taskanaEngine.getWorkbasketService().createWorkbasket(workbasket);
e1.printStackTrace(); taskanaEngine.getClassificationService().createClassification(classification);
}
} Task task = new Task();
task.setName("Unit Test Task");
@Test(expected = TaskNotFoundException.class) task.setWorkbasketId(workbasket.getId());
public void testStartTransactionFail() throws TaskNotFoundException { task.setClassification(classification);
try { task = taskServiceImpl.createTask(task);
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() { connection.commit();
public Object run() throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException { taskServiceImpl.getTaskById(workbasket.getId());
do_testStartTransactionFail();
return null; TaskanaEngineImpl te2 = (TaskanaEngineImpl) taskanaEngineConfiguration.buildTaskanaEngine();
} TaskServiceImpl taskServiceImpl2 = (TaskServiceImpl) te2.getTaskService();
}); taskServiceImpl2.getTaskById(workbasket.getId());
} catch (PrivilegedActionException e) { connection.commit();
Throwable cause = e.getCause(); }
if (cause != null) {
Assert.assertTrue(cause instanceof TaskNotFoundException); @WithAccessId(userName = "Elena")
throw (TaskNotFoundException) cause; @Test
} public void testCreateTask() throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException {
} Connection connection = dataSource.getConnection();
} taskanaEngineImpl.setConnection(connection);
public void do_testStartTransactionFail() generateSampleAccessItems();
throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException {
Connection connection = dataSource.getConnection(); Task task = this.generateDummyTask();
taskanaEngineImpl.setConnection(connection); task = taskServiceImpl.createTask(task);
// taskServiceImpl = (TaskServiceImpl) taskanaEngine.getTaskService(); connection.commit(); // needed so that the change is visible in the other session
Workbasket workbasket = new Workbasket(); TaskanaEngine te2 = taskanaEngineConfiguration.buildTaskanaEngine();
workbasket.setName("workbasket"); TaskServiceImpl taskServiceImpl2 = (TaskServiceImpl) te2.getTaskService();
workbasket.setId("1"); // set id manually for authorization tests Task resultTask = taskServiceImpl2.getTaskById(task.getId());
Classification classification = classificationService.newClassification(); Assert.assertNotNull(resultTask);
taskanaEngine.getWorkbasketService().createWorkbasket(workbasket); connection.commit();
taskanaEngine.getClassificationService().createClassification(classification); }
Task task = new Task(); @Test
task.setName("Unit Test Task"); public void testCreateTaskInTaskanaWithDefaultDb()
task.setWorkbasketId(workbasket.getId()); throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException {
task.setClassification(classification); DataSource ds = TaskanaEngineConfiguration.createDefaultDataSource();
task = taskServiceImpl.createTask(task); TaskanaEngineConfiguration taskanaEngineConfiguration = new TaskanaEngineConfiguration(ds, false, false);
connection.commit(); TaskanaEngine te = taskanaEngineConfiguration.buildTaskanaEngine();
taskServiceImpl.getTaskById(workbasket.getId()); Connection connection = ds.getConnection();
te.setConnection(connection);
TaskanaEngineImpl te2 = (TaskanaEngineImpl) taskanaEngineConfiguration.buildTaskanaEngine(); TaskServiceImpl taskServiceImpl = (TaskServiceImpl) te.getTaskService();
TaskServiceImpl taskServiceImpl2 = (TaskServiceImpl) te2.getTaskService(); WorkbasketServiceImpl workbasketServiceImpl = (WorkbasketServiceImpl) te.getWorkbasketService();
taskServiceImpl2.getTaskById(workbasket.getId()); ClassificationServiceImpl classificationServiceImpl = (ClassificationServiceImpl) te.getClassificationService();
connection.commit();
} Workbasket workbasket = new Workbasket();
workbasket.setName("workbasket");
@Test Classification classification = classificationService.newClassification();
public void testCreateTask() throws Throwable { workbasket.setName("workbasket99");
try { workbasketServiceImpl.createWorkbasket(workbasket);
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() { classificationServiceImpl.createClassification(classification);
@Override
public Object run() throws TaskNotFoundException, WorkbasketNotFoundException, FileNotFoundException, NotAuthorizedException, SQLException, ClassificationNotFoundException, ClassificationAlreadyExistException { Task task = new Task();
do_testCreateTask(); task.setName("Unit Test Task");
return null; task.setWorkbasketId(workbasket.getId());
} task.setClassification(classification);
}); task = taskServiceImpl.createTask(task);
} catch (PrivilegedActionException e) {
throw e.getCause(); Assert.assertNotNull(task);
} Assert.assertNotNull(task.getId());
} connection.commit();
te.setConnection(null);
public void do_testCreateTask() throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException { }
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection); @WithAccessId(userName = "Elena")
@Test
Task task = this.generateDummyTask(); public void testCreateTaskWithPlannedAndName() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException {
task = taskServiceImpl.createTask(task); Connection connection = dataSource.getConnection();
connection.commit(); // needed so that the change is visible in the other session taskanaEngineImpl.setConnection(connection);
TaskanaEngine te2 = taskanaEngineConfiguration.buildTaskanaEngine(); generateSampleAccessItems();
TaskServiceImpl taskServiceImpl2 = (TaskServiceImpl) te2.getTaskService();
Task resultTask = taskServiceImpl2.getTaskById(task.getId()); Classification classification = classificationService.newClassification();
Assert.assertNotNull(resultTask); classification.setCategory("MANUAL");
connection.commit(); classification.setName("classification name");
} classification.setServiceLevel("P1D");
taskanaEngine.getClassificationService().createClassification(classification);
@Test
public void testCreateTaskInTaskanaWithDefaultDb() ObjectReference objectReference = new ObjectReference();
throws FileNotFoundException, SQLException, TaskNotFoundException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException { objectReference.setCompany("Novatec");
DataSource ds = TaskanaEngineConfiguration.createDefaultDataSource(); objectReference.setSystem("System");
TaskanaEngineConfiguration taskanaEngineConfiguration = new TaskanaEngineConfiguration(ds, false, false); objectReference.setSystemInstance("2");
TaskanaEngine te = taskanaEngineConfiguration.buildTaskanaEngine(); objectReference.setValue("4444");
Connection connection = ds.getConnection(); objectReference.setType("type");
te.setConnection(connection);
TaskServiceImpl taskServiceImpl = (TaskServiceImpl) te.getTaskService(); Timestamp tomorrow = Timestamp.valueOf(LocalDateTime.now().plusDays(1));
WorkbasketServiceImpl workbasketServiceImpl = (WorkbasketServiceImpl) te.getWorkbasketService();
ClassificationServiceImpl classificationServiceImpl = (ClassificationServiceImpl) te.getClassificationService(); Task test = this.generateDummyTask();
test.setClassification(classification);
Workbasket workbasket = new Workbasket(); test.setName("Name");
workbasket.setName("workbasket"); test.setPrimaryObjRef(objectReference);
Classification classification = classificationService.newClassification(); test.setPlanned(tomorrow);
workbasket.setName("workbasket99"); test = taskServiceImpl.createTask(test);
workbasketServiceImpl.createWorkbasket(workbasket);
classificationServiceImpl.createClassification(classification); Assert.assertNotEquals(test.getPlanned(), test.getCreated());
Assert.assertNotNull(test.getDue());
Task task = new Task();
task.setName("Unit Test Task"); Task test2 = new Task();
task.setWorkbasketId(workbasket.getId()); test2.setWorkbasketId(test.getWorkbasketId());
task.setClassification(classification); test2.setClassification(classification);
task = taskServiceImpl.createTask(task); test2.setPrimaryObjRef(objectReference);
test2.setDescription("desc");
Assert.assertNotNull(task); taskServiceImpl.createTask(test2);
Assert.assertNotNull(task.getId());
connection.commit(); Assert.assertEquals(test2.getPlanned(), test2.getCreated());
te.setConnection(null); Assert.assertTrue(test2.getName().equals(classification.getName()));
}
Assert.assertEquals(test.getClassification().getId(), test2.getClassification().getId());
@Test Assert.assertTrue(test.getDue().after(test2.getDue()));
public void testCreateTaskWithPlannedAndName() throws Throwable { Assert.assertFalse(test.getName().equals(test2.getName()));
try { }
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
@Override @WithAccessId(userName = "Elena")
public Object run() throws WorkbasketNotFoundException, ClassificationNotFoundException, NotAuthorizedException, SQLException, ClassificationAlreadyExistException { @Test(expected = WorkbasketNotFoundException.class)
do_testCreateTaskWithPlannedAndName(); public void createTaskShouldThrowWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, SQLException, ClassificationAlreadyExistException {
return null; Connection connection = dataSource.getConnection();
} taskanaEngineImpl.setConnection(connection);
});
} catch (PrivilegedActionException e) { generateSampleAccessItems();
throw e.getCause();
} Task test = this.generateDummyTask();
} test.setWorkbasketId("2");
taskServiceImpl.createTask(test);
public void do_testCreateTaskWithPlannedAndName() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException { }
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection); @WithAccessId(userName = "Elena")
@Test(expected = ClassificationNotFoundException.class)
Classification classification = classificationService.newClassification(); public void createManualTaskShouldThrowClassificationNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, SQLException, ClassificationAlreadyExistException {
classification.setCategory("MANUAL"); Connection connection = dataSource.getConnection();
classification.setName("classification name"); taskanaEngineImpl.setConnection(connection);
classification.setServiceLevel("P1D");
taskanaEngine.getClassificationService().createClassification(classification); generateSampleAccessItems();
ObjectReference objectReference = new ObjectReference(); Task test = this.generateDummyTask();
objectReference.setCompany("Novatec"); test.setClassification(new ClassificationImpl());
objectReference.setSystem("System"); taskServiceImpl.createTask(test);
objectReference.setSystemInstance("2"); }
objectReference.setValue("4444");
objectReference.setType("type"); @WithAccessId(userName = "Elena", groupNames = {"DummyGroup"})
@Test
Timestamp tomorrow = Timestamp.valueOf(LocalDateTime.now().plusDays(1)); public void should_ReturnList_when_BuilderIsUsed() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException {
Connection connection = dataSource.getConnection();
Task test = this.generateDummyTask(); taskanaEngineImpl.setConnection(connection);
test.setClassification(classification);
test.setName("Name"); generateSampleAccessItems();
test.setPrimaryObjRef(objectReference);
test.setPlanned(tomorrow); Workbasket workbasket = new Workbasket();
test = taskServiceImpl.createTask(test); workbasket.setName("workbasket");
Classification classification = classificationService.newClassification();
Assert.assertNotEquals(test.getPlanned(), test.getCreated()); workbasket.setId("1"); // set id manually for authorization tests
Assert.assertNotNull(test.getDue()); taskanaEngine.getWorkbasketService().createWorkbasket(workbasket);
taskanaEngine.getClassificationService().createClassification(classification);
Task test2 = new Task();
test2.setWorkbasketId(test.getWorkbasketId()); Task task = new Task();
test2.setClassification(classification); task.setName("Unit Test Task");
test2.setPrimaryObjRef(objectReference); task.setWorkbasketId(workbasket.getId());
test2.setDescription("desc"); task.setClassification(classification);
taskServiceImpl.createTask(test2); task = taskServiceImpl.createTask(task);
Assert.assertEquals(test2.getPlanned(), test2.getCreated()); TaskanaEngineImpl taskanaEngineImpl = (TaskanaEngineImpl) taskanaEngine;
Assert.assertTrue(test2.getName().equals(classification.getName())); ClassificationQuery classificationQuery = new ClassificationQueryImpl(taskanaEngineImpl)
.parentClassification("pId1", "pId2").category("cat1", "cat2").type("oneType").name("1Name", "name2")
Assert.assertEquals(test.getClassification().getId(), test2.getClassification().getId()); .descriptionLike("my desc").priority(1, 2, 1).serviceLevel("me", "and", "you");
Assert.assertTrue(test.getDue().after(test2.getDue()));
Assert.assertFalse(test.getName().equals(test2.getName())); ObjectReferenceQuery objectReferenceQuery = new ObjectReferenceQueryImpl(taskanaEngineImpl)
} .company("first comp", "sonstwo gmbh").system("sys").type("type1", "type2")
.systemInstance("sysInst1", "sysInst2").value("val1", "val2", "val3");
@Test(expected = WorkbasketNotFoundException.class)
public void createTaskShouldThrowWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, SQLException, ClassificationAlreadyExistException { List<Task> results = taskServiceImpl.createTaskQuery().name("bla", "test").descriptionLike("test")
Connection connection = dataSource.getConnection(); .priority(1, 2, 2).state(TaskState.CLAIMED).workbasketId("1", "2")
taskanaEngineImpl.setConnection(connection); .owner("test", "test2", "bla").customFields("test").classification(classificationQuery)
.objectReference(objectReferenceQuery).list();
Task test = this.generateDummyTask();
test.setWorkbasketId("2"); Assert.assertEquals(0, results.size());
taskServiceImpl.createTask(test); connection.commit();
} }
@Test(expected = ClassificationNotFoundException.class) private Task generateDummyTask() throws ClassificationAlreadyExistException {
public void createManualTaskShouldThrowClassificationNotFoundException() throws ClassificationNotFoundException { Workbasket workbasket = new Workbasket();
try { workbasket.setName("wb");
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() { workbasket.setId("1"); // set id manually for authorization tests
public Object run() throws NotAuthorizedException, SQLException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException { taskanaEngine.getWorkbasketService().createWorkbasket(workbasket);
do_createManualTaskShouldThrowClassificationNotFoundException();
return null; Classification classification = classificationService.newClassification();
} taskanaEngine.getClassificationService().createClassification(classification);
});
} catch (PrivilegedActionException e) { Task task = new Task();
Throwable cause = e.getCause(); task.setWorkbasketId(workbasket.getId());
if (cause != null) { task.setClassification(classification);
Assert.assertTrue(cause instanceof ClassificationNotFoundException); return task;
throw (ClassificationNotFoundException) cause; }
}
} private void generateSampleAccessItems() {
} WorkbasketAccessItem accessItem = new WorkbasketAccessItem();
accessItem.setId(IdGenerator.generateWithPrefix("WAI"));
public void do_createManualTaskShouldThrowClassificationNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, SQLException, ClassificationAlreadyExistException { accessItem.setWorkbasketId("1");
Connection connection = dataSource.getConnection(); accessItem.setAccessId("Elena");
taskanaEngineImpl.setConnection(connection); accessItem.setPermAppend(true);
accessItem.setPermOpen(true);
Task test = this.generateDummyTask(); workBasketService.createWorkbasketAuthorization(accessItem);
test.setClassification(new ClassificationImpl());
taskServiceImpl.createTask(test); WorkbasketAccessItem accessItem2 = new WorkbasketAccessItem();
} accessItem2.setId(IdGenerator.generateWithPrefix("WAI"));
accessItem2.setWorkbasketId("2");
@Test accessItem2.setAccessId("DummyGroup");
public void should_ReturnList_when_BuilderIsUsed() throws Throwable { accessItem2.setPermOpen(true);
try { workBasketService.createWorkbasketAuthorization(accessItem2);
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() { }
@Override
public Object run() throws WorkbasketNotFoundException, NotAuthorizedException, SQLException, ClassificationNotFoundException, ClassificationAlreadyExistException { @After
do_should_ReturnList_when_BuilderIsUsed(); public void cleanUp() {
return null; taskanaEngineImpl.setConnection(null);
} }
});
} catch (PrivilegedActionException e) { @AfterClass
throw e.getCause(); public static void cleanUpClass() {
} FileUtils.deleteRecursive("~/data", true);
} }
}
public void do_should_ReturnList_when_BuilderIsUsed() throws SQLException, NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, ClassificationAlreadyExistException {
Connection connection = dataSource.getConnection();
taskanaEngineImpl.setConnection(connection);
Workbasket workbasket = new Workbasket();
workbasket.setName("workbasket");
Classification classification = classificationService.newClassification();
workbasket.setId("1"); // set id manually for authorization tests
taskanaEngine.getWorkbasketService().createWorkbasket(workbasket);
taskanaEngine.getClassificationService().createClassification(classification);
Task task = new Task();
task.setName("Unit Test Task");
task.setWorkbasketId(workbasket.getId());
task.setClassification(classification);
task = taskServiceImpl.createTask(task);
TaskanaEngineImpl taskanaEngineImpl = (TaskanaEngineImpl) taskanaEngine;
ClassificationQuery classificationQuery = new ClassificationQueryImpl(taskanaEngineImpl)
.parentClassification("pId1", "pId2").category("cat1", "cat2").type("oneType").name("1Name", "name2")
.descriptionLike("my desc").priority(1, 2, 1).serviceLevel("me", "and", "you");
ObjectReferenceQuery objectReferenceQuery = new ObjectReferenceQueryImpl(taskanaEngineImpl)
.company("first comp", "sonstwo gmbh").system("sys").type("type1", "type2")
.systemInstance("sysInst1", "sysInst2").value("val1", "val2", "val3");
List<Task> results = taskServiceImpl.createTaskQuery().name("bla", "test").descriptionLike("test")
.priority(1, 2, 2).state(TaskState.CLAIMED).workbasketId("1", "2")
.owner("test", "test2", "bla").customFields("test").classification(classificationQuery)
.objectReference(objectReferenceQuery).list();
Assert.assertEquals(0, results.size());
connection.commit();
}
private Task generateDummyTask() throws ClassificationAlreadyExistException {
Workbasket workbasket = new Workbasket();
workbasket.setName("wb");
workbasket.setId("1"); // set id manually for authorization tests
taskanaEngine.getWorkbasketService().createWorkbasket(workbasket);
Classification classification = classificationService.newClassification();
taskanaEngine.getClassificationService().createClassification(classification);
Task task = new Task();
task.setWorkbasketId(workbasket.getId());
task.setClassification(classification);
return task;
}
@After
public void cleanUp() {
taskanaEngineImpl.setConnection(null);
}
@AfterClass
public static void cleanUpClass() {
FileUtils.deleteRecursive("~/data", true);
}
}

View File

@ -0,0 +1,72 @@
package pro.taskana.security;
import java.security.Principal;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import javax.security.auth.Subject;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
/**
* Runner for integration tests that enables JAAS subject.
*/
public class JAASRunner extends BlockJUnit4ClassRunner {
public JAASRunner(Class<?> c) throws InitializationError {
super(c);
}
@Override
protected Statement methodInvoker(FrameworkMethod method, Object test) {
Subject subject = new Subject();
List<Principal> principalList = new ArrayList<>();
if (test != null) {
WithAccessId withAccessId = method.getMethod().getAnnotation(WithAccessId.class);
if (withAccessId != null) {
if (withAccessId.userName() != null) {
principalList.add(new UserPrincipal(withAccessId.userName()));
}
for (String groupName : withAccessId.groupNames()) {
if (groupName != null) {
principalList.add(new GroupPrincipal(groupName));
}
}
}
subject.getPrincipals().addAll(principalList);
}
final Statement base = super.methodInvoker(method, test);
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
Subject.doAs(subject, new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
try {
base.evaluate();
} catch (Throwable e) {
throw (Exception) e;
}
return null;
}
});
} catch (PrivilegedActionException e) {
throw (Exception) e.getCause();
}
}
};
}
}

View File

@ -0,0 +1,18 @@
package pro.taskana.security;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Specify user id for JUnit JAASRunner.
* @author bbr
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface WithAccessId {
String userName();
String[] groupNames() default {};
}

View File

@ -1,3 +0,0 @@
INSERT INTO WORKBASKET_ACCESS_LIST VALUES ('1', '1', 'Elena', true, true, true, true, true, false, false, false, false, false, false, false, false);
INSERT INTO WORKBASKET_ACCESS_LIST VALUES ('2', '2', 'Elena', true, true, true, true, true, true, true, true, true, false, false, false, false);
INSERT INTO WORKBASKET_ACCESS_LIST VALUES ('3', '3', 'Simone', true, true, true, true, true, true, true, true, true, true, true, true, true);