Search in sources :

Example 11 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method shouldRemoveCachedMostRecentIdOnStageStatusChange.

@Test
public void shouldRemoveCachedMostRecentIdOnStageStatusChange() throws Exception {
    SqlMapClientTemplate mockTemplate = mock(SqlMapClientTemplate.class);
    stageDao.setSqlMapClientTemplate(mockTemplate);
    when(mockTemplate.queryForObject(eq("getMostRecentId"), any())).thenReturn(20L);
    String pipelineName = CaseInsensitiveString.str(mingleConfig.name());
    String stageName = CaseInsensitiveString.str(mingleConfig.get(0).name());
    String key = stageDao.cacheKeyForMostRecentId(pipelineName, stageName);
    // should query and cache value
    stageDao.mostRecentId(pipelineName, stageName);
    Long id = stageDao.mostRecentId(pipelineName, stageName);
    assertThat(id).isEqualTo(20L);
    // should clear the cache
    Stage stage = StageMother.custom(pipelineName, stageName, new JobInstances());
    stageDao.stageStatusChanged(stage);
    assertThat(goCache.get(key)).isNull();
    // should requery and cache
    stageDao.mostRecentId(pipelineName, stageName);
    id = stageDao.mostRecentId(pipelineName, stageName);
    assertThat(id).isEqualTo(20L);
    assertThat(goCache.get(key)).isEqualTo(20L);
    verify(mockTemplate, times(2)).queryForObject(eq("getMostRecentId"), any());
}
Also used : SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 12 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method isStageActive_shouldCacheTheResultAfterFirstExecution.

@Test
public void isStageActive_shouldCacheTheResultAfterFirstExecution() {
    SqlMapClientTemplate mockTemplate = mock(SqlMapClientTemplate.class);
    stageDao.setSqlMapClientTemplate(mockTemplate);
    when(mockTemplate.queryForObject(eq("isStageActive"), any())).thenReturn(1);
    boolean stageActive = stageDao.isStageActive(CaseInsensitiveString.str(mingleConfig.name()), CaseInsensitiveString.str(mingleConfig.getFirstStageConfig().name()));
    assertThat(stageActive).isTrue();
    stageActive = stageDao.isStageActive(CaseInsensitiveString.str(mingleConfig.name()), CaseInsensitiveString.str(mingleConfig.getFirstStageConfig().name()));
    assertThat(stageActive).isTrue();
    verify(mockTemplate, times(1)).queryForObject(eq("isStageActive"), any());
}
Also used : SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 13 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method isStageActive_shouldMakeIsActiveFalseWhenTheStageCompletes.

@Test
public void isStageActive_shouldMakeIsActiveFalseWhenTheStageCompletes() {
    SqlMapClientTemplate mockTemplate = mock(SqlMapClientTemplate.class);
    stageDao.setSqlMapClientTemplate(mockTemplate);
    when(mockTemplate.queryForObject(eq("isStageActive"), any())).thenReturn(1).thenReturn(0);
    boolean stageActive = stageDao.isStageActive(CaseInsensitiveString.str(mingleConfig.name()), CaseInsensitiveString.str(mingleConfig.getFirstStageConfig().name()));
    assertThat(stageActive).isTrue();
    Stage stage = StageMother.completedFailedStageInstance(CaseInsensitiveString.str(mingleConfig.name()), CaseInsensitiveString.str(mingleConfig.getFirstStageConfig().name()), "job");
    // The cached 'true' should now be removed
    stageDao.stageStatusChanged(stage);
    assertThat(stageDao.isStageActive(CaseInsensitiveString.str(mingleConfig.name()), CaseInsensitiveString.str(mingleConfig.getFirstStageConfig().name()))).isFalse();
    verify(mockTemplate, times(2)).queryForObject(eq("isStageActive"), any());
}
Also used : SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 14 with SqlMapClientTemplate

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

the class PipelineSqlMapDaoTest method loadHistoryByIds_shouldLoadHistoryByIdWhenOnlyASingleIdIsNeedeSoThatItUsesTheExistingCacheForEnvironmentsPage.

@Test
void loadHistoryByIds_shouldLoadHistoryByIdWhenOnlyASingleIdIsNeedeSoThatItUsesTheExistingCacheForEnvironmentsPage() throws Exception {
    SqlMapClientTemplate mockTemplate = mock(SqlMapClientTemplate.class);
    when(mockTemplate.queryForList(eq("getPipelineRange"), any())).thenReturn(Arrays.asList(2L));
    pipelineSqlMapDao.setSqlMapClientTemplate(mockTemplate);
    pipelineSqlMapDao.loadHistory("pipelineName", 1, 0);
    verify(mockTemplate, never()).queryForList(eq("getPipelineHistoryByName"), any());
    verify(mockTemplate, times(1)).queryForList(eq("getPipelineRange"), any());
}
Also used : SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 15 with SqlMapClientTemplate

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

the class StageSqlMapDaoTest method setUp.

@BeforeEach
void setUp() {
    goCache = new StubGoCache(new TestTransactionSynchronizationManager());
    sqlMapClientTemplate = mock(SqlMapClientTemplate.class);
    stageSqlMapDao = new StageSqlMapDao(mock(JobInstanceSqlMapDao.class), new Cache(true, false, false), mock(TransactionTemplate.class), mock(SqlSessionFactory.class), goCache, mock(TransactionSynchronizationManager.class), mock(SystemEnvironment.class), null);
    stageSqlMapDao.setSqlMapClientTemplate(sqlMapClientTemplate);
    cloner = mock(Cloner.class);
    ReflectionUtil.setField(stageSqlMapDao, "cloner", cloner);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) {
            return invocationOnMock.getArguments()[0];
        }
    }).when(cloner).deepClone(any());
}
Also used : Answer(org.mockito.stubbing.Answer) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) GoCache(com.thoughtworks.go.server.cache.GoCache) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) Cache(com.opensymphony.oscache.base.Cache) Cloner(com.rits.cloning.Cloner) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SqlMapClientTemplate (com.thoughtworks.go.server.transaction.SqlMapClientTemplate)26 Test (org.junit.jupiter.api.Test)25 StageHistoryEntry (com.thoughtworks.go.presentation.pipelinehistory.StageHistoryEntry)2 StageHistoryPage (com.thoughtworks.go.presentation.pipelinehistory.StageHistoryPage)2 TimeProvider (com.thoughtworks.go.util.TimeProvider)2 Cache (com.opensymphony.oscache.base.Cache)1 Cloner (com.rits.cloning.Cloner)1 FeedEntry (com.thoughtworks.go.domain.feed.FeedEntry)1 StageFeedEntry (com.thoughtworks.go.domain.feed.stage.StageFeedEntry)1 GoCache (com.thoughtworks.go.server.cache.GoCache)1 StubGoCache (com.thoughtworks.go.server.service.StubGoCache)1 TestTransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1