Search in sources :

Example 6 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method findStageWithIdentifier_shouldNotCacheWhenTheStageIsNull.

@Test
public void findStageWithIdentifier_shouldNotCacheWhenTheStageIsNull() {
    SqlMapClientTemplate mockTemplate = mock(SqlMapClientTemplate.class);
    stageDao.setSqlMapClientTemplate(mockTemplate);
    when(mockTemplate.queryForObject(eq("findStageWithJobsByIdentifier"), any())).thenReturn(null);
    Stage actual = stageDao.findStageWithIdentifier(new StageIdentifier("pipeline", 1, "stage", "1"));
    assertThat(actual).isEqualTo(new NullStage("stage"));
    stageDao.findStageWithIdentifier(new StageIdentifier("pipeline", 1, "stage", "1"));
    verify(mockTemplate, times(2)).queryForObject(eq("findStageWithJobsByIdentifier"), any());
}
Also used : SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 7 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method shouldNotInvalidateCachedAllStagesForAPipelineInstanceWhenTheStatusOfAStageOfDifferentPipelineInstance.

@Test
public void shouldNotInvalidateCachedAllStagesForAPipelineInstanceWhenTheStatusOfAStageOfDifferentPipelineInstance() {
    SqlMapClientTemplate mockTemplate = mock(SqlMapClientTemplate.class);
    stageDao.setSqlMapClientTemplate(mockTemplate);
    Stage stage1 = StageMother.passedStageInstance("first", "job", "pipeline");
    Stage stage2 = StageMother.passedStageInstance("second", "job", "pipeline");
    List<Stage> stages = asList(stage1, stage2);
    when(mockTemplate.queryForList("getStagesByPipelineNameAndCounter", arguments("pipelineName", "pipeline").and("pipelineCounter", 1).asMap())).thenReturn((List) stages);
    Stages actual = stageDao.findAllStagesFor("pipeline", 1);
    assertThat(actual).isEqualTo(new Stages(stages));
    // Should not invalidate the cached stages as it is from a different Pipeline instance
    stageDao.stageStatusChanged(StageMother.scheduledStage("pipeline", 2, "first", 2, "job"));
    actual = stageDao.findAllStagesFor("pipeline", 1);
    assertThat(actual).isEqualTo(new Stages(stages));
    verify(mockTemplate, times(1)).queryForList("getStagesByPipelineNameAndCounter", arguments("pipelineName", "pipeline").and("pipelineCounter", 1).asMap());
}
Also used : SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 8 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method getTotalStageCountForChart_shouldInvalidateTheCountCacheOnStageSchedule.

@Test
public void getTotalStageCountForChart_shouldInvalidateTheCountCacheOnStageSchedule() throws SQLException {
    SqlMapClientTemplate mockClient = mock(SqlMapClientTemplate.class);
    stageDao.setSqlMapClientTemplate(mockClient);
    Map<String, Object> toGet = arguments("pipelineName", "maar").and("stageName", "khoon").asMap();
    when(mockClient.queryForObject("getTotalStageCountForChart", toGet)).thenReturn(3).thenReturn(4);
    // Should prime the cache
    assertThat(stageDao.getTotalStageCountForChart("maar", "khoon")).isEqualTo(3);
    Stage stage = new Stage("khoon", new JobInstances(), "foo", null, "manual", new TimeProvider());
    Pipeline pipeline = new Pipeline("maar", "${COUNT}", BuildCause.createWithEmptyModifications(), new EnvironmentVariables(), stage);
    pipeline.setId(1);
    // Should Invalidate the cache
    stageDao.save(pipeline, stage);
    // should refetch
    assertThat(stageDao.getTotalStageCountForChart("maar", "khoon")).isEqualTo(4);
    verify(mockClient, times(2)).queryForObject("getTotalStageCountForChart", toGet);
}
Also used : TimeProvider(com.thoughtworks.go.util.TimeProvider) SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 9 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method findStageWithIdentifier_shouldRemoveFromTheCacheOnSave.

@Test
public void findStageWithIdentifier_shouldRemoveFromTheCacheOnSave() {
    SqlMapClientTemplate mockTemplate = mock(SqlMapClientTemplate.class);
    Stage first = StageMother.passedStageInstance("pipeline", "stage", "job", new Date());
    first.setCounter(1);
    Stage second = StageMother.passedStageInstance("pipeline", "stage", "job", new Date());
    second.setCounter(2);
    stageDao.setSqlMapClientTemplate(mockTemplate);
    when(mockTemplate.queryForObject(eq("findStageWithJobsByIdentifier"), any())).thenReturn(first);
    Stage actual = stageDao.findStageWithIdentifier(new StageIdentifier("pipeline", 1, "stage", "1"));
    assertThat(actual).isEqualTo(first);
    Pipeline pipeline = new Pipeline("pipeline", "label", BuildCause.createManualForced(), new EnvironmentVariables(), first);
    pipeline.setCounter(1);
    // save stage 2.. This should invalidate the stage 1 because of the latest run state.
    stageDao.save(pipeline, second);
    stageDao.findStageWithIdentifier(new StageIdentifier("pipeline", 1, "stage", "1"));
    verify(mockTemplate, times(2)).queryForObject(eq("findStageWithJobsByIdentifier"), any());
}
Also used : SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

Example 10 with SqlMapClientTemplate

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

the class StageSqlMapDaoIntegrationTest method getTotalStageCountForChart_shouldInvalidateTheCountCacheOnStageUpdate.

@Test
public void getTotalStageCountForChart_shouldInvalidateTheCountCacheOnStageUpdate() throws SQLException {
    SqlMapClientTemplate mockClient = mock(SqlMapClientTemplate.class);
    stageDao.setSqlMapClientTemplate(mockClient);
    Map<String, Object> toGet = arguments("pipelineName", "maar").and("stageName", "khoon").asMap();
    when(mockClient.queryForObject("getTotalStageCountForChart", toGet)).thenReturn(3).thenReturn(4);
    // Should prime the cache
    assertThat(stageDao.getTotalStageCountForChart("maar", "khoon")).isEqualTo(3);
    Stage stage = new Stage("khoon", new JobInstances(), "foo", null, "manual", new TimeProvider());
    stage.setIdentifier(new StageIdentifier("maar/2/khoon/1"));
    // Should Invalidate the cache
    updateResultInTransaction(stage, StageResult.Cancelled);
    // should refetch
    assertThat(stageDao.getTotalStageCountForChart("maar", "khoon")).isEqualTo(4);
    verify(mockClient, times(2)).queryForObject("getTotalStageCountForChart", toGet);
}
Also used : TimeProvider(com.thoughtworks.go.util.TimeProvider) SqlMapClientTemplate(com.thoughtworks.go.server.transaction.SqlMapClientTemplate) Test(org.junit.jupiter.api.Test)

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