Search in sources :

Example 11 with TransactionSynchronizationManager

use of com.thoughtworks.go.server.transaction.TransactionSynchronizationManager in project gocd by gocd.

the class StageServiceTest method findStageSummaryByIdentifierShouldRespondWith401WhenUserDoesNotHavePermissionToViewThePipeline.

@Test
public void findStageSummaryByIdentifierShouldRespondWith401WhenUserDoesNotHavePermissionToViewThePipeline() throws Exception {
    SecurityService securityService = mock(SecurityService.class);
    when(securityService.hasViewPermissionForPipeline(ALWAYS_ALLOW_USER, "pipeline_name")).thenReturn(false);
    TransactionSynchronizationManager transactionSynchronizationManager = mock(TransactionSynchronizationManager.class);
    StageService service = new StageService(stageDao, null, null, null, securityService, null, changesetService, goConfigService, transactionTemplate, transactionSynchronizationManager, goCache);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    StageSummaryModel model = service.findStageSummaryByIdentifier(new StageIdentifier("pipeline_name/10/stage_name/1"), ALWAYS_ALLOW_USER, result);
    assertThat(result.httpCode(), is(401));
    assertThat(model, is(nullValue()));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) Test(org.junit.Test)

Example 12 with TransactionSynchronizationManager

use of com.thoughtworks.go.server.transaction.TransactionSynchronizationManager in project gocd by gocd.

the class TransactionSynchronizationManagerTest method shouldUnderstandIfTransactionIsActive.

@Test
public void shouldUnderstandIfTransactionIsActive() {
    final TransactionSynchronizationManager synchronizationManager = new TransactionSynchronizationManager();
    final ArrayList<Boolean> transactionActivity = new ArrayList<>();
    transactionTemplate.execute(new org.springframework.transaction.support.TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            transactionActivity.add(synchronizationManager.isActualTransactionActive());
        }
    });
    assertThat(transactionActivity, is(Arrays.asList(true)));
    assertThat(synchronizationManager.isActualTransactionActive(), is(false));
}
Also used : TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 13 with TransactionSynchronizationManager

use of com.thoughtworks.go.server.transaction.TransactionSynchronizationManager in project gocd by gocd.

the class TransactionSynchronizationManagerTest method shouldUnderstandIfTransactionBodyExecuting.

@Test
public void shouldUnderstandIfTransactionBodyExecuting() {
    final boolean[] inBody = new boolean[] { false };
    final TransactionSynchronizationManager synchronizationManager = new TransactionSynchronizationManager();
    transactionTemplate.execute(new TransactionCallback() {

        public Object doInTransaction(TransactionStatus status) {
            inBody[0] = synchronizationManager.isTransactionBodyExecuting();
            return null;
        }
    });
    assertThat(inBody[0], is(true));
    assertThat(synchronizationManager.isTransactionBodyExecuting(), is(false));
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 14 with TransactionSynchronizationManager

use of com.thoughtworks.go.server.transaction.TransactionSynchronizationManager in project gocd by gocd.

the class MaterialRepositoryTest method setUp.

@Before
public void setUp() {
    databaseStrategy = mock(DatabaseStrategy.class);
    sessionFactory = mock(SessionFactory.class);
    goCache = mock(GoCache.class);
    ourCustomCache = new HashMap<>();
    transactionSynchronizationManager = mock(TransactionSynchronizationManager.class);
    mockHibernateTemplate = mock(HibernateTemplate.class);
    materialConfigConverter = mock(MaterialConfigConverter.class);
    materialExpansionService = mock(MaterialExpansionService.class);
    materialRepository = new MaterialRepository(sessionFactory, goCache, 4242, transactionSynchronizationManager, materialConfigConverter, materialExpansionService, databaseStrategy);
    materialRepository.setHibernateTemplate(mockHibernateTemplate);
    when(goCache.get(anyString())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            return ourCustomCache.get(arguments[0]);
        }
    });
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            return ourCustomCache.put((String) arguments[0], arguments[1]);
        }
    }).when(goCache).put(anyString(), anyObject());
    when(goCache.remove(anyString())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] arguments = invocation.getArguments();
            return ourCustomCache.remove(arguments[0]);
        }
    });
}
Also used : SessionFactory(org.hibernate.SessionFactory) TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) HibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate) MaterialConfigConverter(com.thoughtworks.go.server.service.MaterialConfigConverter) Answer(org.mockito.stubbing.Answer) MaterialExpansionService(com.thoughtworks.go.server.service.MaterialExpansionService) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GoCache(com.thoughtworks.go.server.cache.GoCache) DatabaseStrategy(com.thoughtworks.go.server.database.DatabaseStrategy) Before(org.junit.Before)

Example 15 with TransactionSynchronizationManager

use of com.thoughtworks.go.server.transaction.TransactionSynchronizationManager in project gocd by gocd.

the class TransactionSynchronizationManagerTest method shouldRegisterSynchronization_andNotCallItOnTransactionFailure.

@Test
public void shouldRegisterSynchronization_andNotCallItOnTransactionFailure() {
    final TransactionSynchronizationManager synchronizationManager = new TransactionSynchronizationManager();
    final TransactionSynchronization synchronization = mock(TransactionSynchronization.class);
    try {
        transactionTemplate.execute(new org.springframework.transaction.support.TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                synchronizationManager.registerSynchronization(synchronization);
                throw new RuntimeException();
            }
        });
    } catch (Exception e) {
    // ignore
    }
    verify(synchronization, never()).afterCommit();
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) TransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TransactionSynchronizationManager) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Aggregations

TransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TransactionSynchronizationManager)21 Test (org.junit.Test)15 TestTransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager)13 StageStatusCache (com.thoughtworks.go.domain.activity.StageStatusCache)5 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)5 StageRunFinder (com.thoughtworks.go.server.dao.sparql.StageRunFinder)4 StageStatusTopic (com.thoughtworks.go.server.messaging.StageStatusTopic)4 Before (org.junit.Before)4 TransactionStatus (org.springframework.transaction.TransactionStatus)4 TransactionTemplate (com.thoughtworks.go.server.transaction.TransactionTemplate)3 SessionFactory (org.hibernate.SessionFactory)3 GoConfigDao (com.thoughtworks.go.config.GoConfigDao)2 GitCommand (com.thoughtworks.go.domain.materials.git.GitCommand)2 MaterialRepository (com.thoughtworks.go.server.persistence.MaterialRepository)2 StubGoCache (com.thoughtworks.go.server.service.StubGoCache)2 File (java.io.File)2 Session (org.hibernate.classic.Session)2 SqlMapClientTemplate (org.springframework.orm.ibatis.SqlMapClientTemplate)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)2