Example usage for org.springframework.transaction.support TransactionTemplate execute

List of usage examples for org.springframework.transaction.support TransactionTemplate execute

Introduction

In this page you can find the example usage for org.springframework.transaction.support TransactionTemplate execute.

Prototype

@Override
    @Nullable
    public <T> T execute(TransactionCallback<T> action) throws TransactionException 

Source Link

Usage

From source file:ubc.pavlab.aspiredb.server.dao.SubjectDaoTest.java

public void testCreateAndFindByPatientId() throws Exception {

    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override//  w w w  .  j  av  a  2s . c om
        public void doInTransactionWithoutResult(TransactionStatus status) {
            Project p1 = new Project();
            p1.setName(project1Name);

            p1 = testObjectHelper.createPersistentProject(p1);

            assertNull("Subject hasn't been created yet", subjectDao.findByPatientId(p1, patientId));

            //                List<Project> projects = new ArrayList<>();
            //                projects.add( p1 );

            Subject s = new Subject();
            s.setPatientId(patientId);
            s.setProject(p1);
            s = subjectDao.create(s);

            assertNotNull(s);

            assertNotNull("Subject should have been created", subjectDao.findByPatientId(p1, patientId));
        }
    });
}

From source file:ubc.pavlab.aspiredb.server.project.ProjectManagerTest.java

@Test
public void testAddSubjectVariantsToProjectSecurity() {

    super.runAsAdmin();

    final String patientId = RandomStringUtils.randomAlphabetic(5);
    final String projectName = RandomStringUtils.randomAlphabetic(5);

    CharacteristicValueObject cvo = new CharacteristicValueObject();

    cvo.setKey("testChar");
    cvo.setValue("testcharvalue");

    Map<String, CharacteristicValueObject> charMap = new HashMap<String, CharacteristicValueObject>();
    charMap.put(cvo.getKey(), cvo);/* w w w  .  ja  v  a2s . c o m*/

    CNVValueObject cnv = new CNVValueObject();

    cnv.setCharacteristics(charMap);
    cnv.setType("GAIN");

    GenomicRange gr = new GenomicRange("X", 3, 234);

    cnv.setGenomicRange(gr);

    cnv.setPatientId(patientId);

    // Another CNV from the same patient
    CNVValueObject cnv2 = new CNVValueObject();
    cnv2.setCharacteristics(charMap);
    cnv2.setType("LOSS");
    cnv2.setGenomicRange(gr);
    cnv2.setPatientId(patientId);

    ArrayList<VariantValueObject> cnvList = new ArrayList<VariantValueObject>();
    cnvList.add(cnv);
    cnvList.add(cnv2);

    try {

        projectManager.addSubjectVariantsToProject(projectName, true, cnvList);

    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
        fail("projectManager.addSubjectVariantsToProject threw an exception " + e);

    }

    projectManager.alterGroupWritePermissions(projectName, groupName, true);

    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            aclTestUtils.checkHasAcl(project);

            assertFalse(
                    "Project should not be viewable by user '" + aDifferentUsername + "', acl is "
                            + aclTestUtils.getAcl(project),
                    securityService.isViewableByUser(project, aDifferentUsername));

            // now to test
            List<Subject> subjects = project.getSubjects();
            assertEquals(1, subjects.size());

            Subject subject = project.getSubjects().iterator().next();

            aclTestUtils.checkHasAcl(subject);

            assertFalse(securityService.isViewableByUser(subject, aDifferentUsername));

            // equals to cnvList
            assertEquals(2, subject.getVariants().size());

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            // // aclTestUtils.checkHasAcl( v );
            // // assertFalse( securityService.isViewableByUser( v, aDifferentUsername ) );
            //
            // Characteristic c = v.getCharacteristics().iterator().next();
            // aclTestUtils.checkHasAcl( c );
            // assertFalse( securityService.isViewableByUser( c, aDifferentUsername ) );
            // }

        }
    });

    try {

        projectManager.deleteProject(projectName);

    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);

    }

}

From source file:ubc.pavlab.aspiredb.server.project.ProjectManagerTest.java

@Test
public void testAlterGroupWritePermissionsForProject() {

    super.runAsAdmin();

    final String patientId = RandomStringUtils.randomAlphabetic(5);
    final String projectName = RandomStringUtils.randomAlphabetic(7);

    CharacteristicValueObject cvo = new CharacteristicValueObject();

    cvo.setKey("testChar");
    cvo.setValue("testcharvalue");

    Map<String, CharacteristicValueObject> charMap = new HashMap<String, CharacteristicValueObject>();
    charMap.put(cvo.getKey(), cvo);/* ww w  .ja v a2s  .c om*/

    CNVValueObject cnv = new CNVValueObject();

    cnv.setCharacteristics(charMap);
    cnv.setType("GAIN");

    GenomicRange gr = new GenomicRange("X", 3, 234);

    cnv.setGenomicRange(gr);

    cnv.setPatientId(patientId);

    ArrayList<VariantValueObject> cnvList = new ArrayList<VariantValueObject>();
    cnvList.add(cnv);

    try {

        projectManager.addSubjectVariantsToProject(projectName, true, cnvList);

    } catch (Exception e) {

        fail("projectManager.addSubjectVariantsToProject threw an exception");

        e.printStackTrace();
    }

    // authorizedUsername is in groupName
    projectManager.alterGroupWritePermissions(projectName, groupName, true);

    // make sure authorizedUsername has read access to all the stuff in a project after security change
    TransactionTemplate tt = new TransactionTemplate(transactionManager);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            assertTrue(securityService.isViewableByUser(project, authorizedUsername));

            Subject subject = project.getSubjects().iterator().next();

            assertTrue(securityService.isViewableByUser(subject, authorizedUsername));

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            //
            // assertTrue( securityService.isViewableByUser( v, authorizedUsername ) );
            //
            // assertTrue( securityService.isViewableByUser( v.getCharacteristics().iterator().next(),
            // authorizedUsername ) );
            // }

        }
    });

    // make sure aDifferentUserName doesn't have access to stuff in project
    TransactionTemplate tt2 = new TransactionTemplate(transactionManager);
    tt2.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            assertFalse(securityService.isViewableByUser(project, aDifferentUsername));

            Subject subject = project.getSubjects().iterator().next();

            assertFalse(securityService.isViewableByUser(subject, aDifferentUsername));

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            //
            // assertFalse( securityService.isViewableByUser( v, aDifferentUsername ) );
            //
            // assertFalse( securityService.isViewableByUser( v.getCharacteristics().iterator().next(),
            // aDifferentUsername ) );
            // }

        }
    });

    // test out a couple of dao methods

    super.runAsUser(authorizedUsername);

    long projectId = projectDao.findByProjectName(projectName).getId();

    variantDao.findBySubjectPatientId(projectId, patientId);

    super.runAsUser(aDifferentUsername);

    try {
        projectId = projectDao.findByProjectName(projectName).getId();
        fail("should have got Access Denied");
    } catch (AccessDeniedException e) {

    }

    Collection<Variant> vCollection2 = variantDao.findBySubjectPatientId(projectId, patientId);

    assertTrue(vCollection2.isEmpty());

    super.runAsAdmin();
    // test removing permissions

    // authorizedUsername is in groupName
    projectManager.alterGroupWritePermissions(projectName, groupName, false);

    // make sure authorizedUsername does not have read access to all the stuff in a project after security change
    TransactionTemplate tt3 = new TransactionTemplate(transactionManager);
    tt3.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {

            Project project = projectDao.findByProjectName(projectName);

            assertFalse(securityService.isViewableByUser(project, authorizedUsername));

            Subject subject = project.getSubjects().iterator().next();

            assertFalse(securityService.isViewableByUser(subject, authorizedUsername));

            variantDao.findBySubjectPatientId(project.getId(), patientId);

            // for ( Variant v : variantCollection ) {
            //
            // assertFalse( securityService.isViewableByUser( v, authorizedUsername ) );
            //
            // assertFalse( securityService.isViewableByUser( v.getCharacteristics().iterator().next(),
            // authorizedUsername ) );
            // }

        }
    });

    // test out a couple of dao methods to make sure authorizedUsername has no access
    super.runAsUser(authorizedUsername);

    try {
        projectId = projectDao.findByProjectName(projectName).getId();
        fail("should have got Access Denied");
    } catch (AccessDeniedException e) {

    }

    Collection<Variant> vCollection3 = variantDao.findBySubjectPatientId(projectId, patientId);

    assertTrue(vCollection3.isEmpty());

    super.runAsAdmin();

    try {

        projectManager.deleteProject(projectName);

    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);

    }

}