@Test
  public void activeJobs_shouldRemoveCacheActiveJobOnUpdateJobStatus() {
    final ActiveJob first = new ActiveJob(1L, "pipeline", 1, "label", "stage", "first");
    final ActiveJob second = new ActiveJob(2L, "another", 2, "label", "stage", "job1");
    List<ActiveJob> expectedJobs = Arrays.asList(first, second);

    when(mockTemplate.queryForList("getActiveJobIds")).thenReturn(Arrays.asList(1L, 2L));
    when(mockTemplate.queryForObject("getActiveJobById", arguments("id", 1L).asMap()))
        .thenReturn(first);
    when(mockTemplate.queryForObject("getActiveJobById", arguments("id", 2L).asMap()))
        .thenReturn(second);

    jobInstanceDao.setSqlMapClientTemplate(mockTemplate);
    jobInstanceDao.activeJobs(); // cache it first

    jobInstanceDao.updateStateAndResult(instance(1L)); // should remove from cache

    List<ActiveJob> activeJobs = jobInstanceDao.activeJobs();

    assertThat(expectedJobs, is(activeJobs));

    verify(mockTemplate, times(2)).queryForList("getActiveJobIds");
    verify(mockTemplate, times(2)).queryForObject("getActiveJobById", arguments("id", 1L).asMap());
    verify(mockTemplate, times(1)).queryForObject("getActiveJobById", arguments("id", 2L).asMap());
  }
  @Test
  public void updateStatus_shouldRemoveCachedJobPlan() {
    when(mockTemplate.queryForList(eq("scheduledPlanIds"))).thenReturn(Arrays.asList(1L));

    final DefaultJobPlan firstJob = jobPlan(1);
    List<JobPlan> expectedPlans =
        new ArrayList<JobPlan>() {
          {
            add(firstJob);
          }
        };
    when(mockTemplate.queryForObject("scheduledPlan", arguments("id", 1L).asMap()))
        .thenReturn(firstJob);

    jobInstanceDao.setSqlMapClientTemplate(mockTemplate);
    jobInstanceDao.orderedScheduledBuilds(); // populate the cache

    JobInstance instance = instance(1);
    jobInstanceDao.updateStateAndResult(instance);

    List<JobPlan> plans = jobInstanceDao.orderedScheduledBuilds();

    assertThat(plans, is(expectedPlans));

    verify(mockTemplate, times(2))
        .queryForObject(
            "scheduledPlan", arguments("id", 1L).asMap()); // because the cache is cleared
    verify(mockTemplate, times(2)).queryForList(eq("scheduledPlanIds"));
  }
  @Test
  public void orderedScheduledBuilds_shouldCacheJobPlan() {
    when(mockTemplate.queryForList(eq("scheduledPlanIds"))).thenReturn(Arrays.asList(1L, 2L));

    final DefaultJobPlan firstJob = jobPlan(1);
    final DefaultJobPlan secondJob = jobPlan(2);
    List<JobPlan> expectedPlans =
        new ArrayList<JobPlan>() {
          {
            add(firstJob);
            add(secondJob);
          }
        };
    when(mockTemplate.queryForObject("scheduledPlan", arguments("id", 1L).asMap()))
        .thenReturn(firstJob);
    when(mockTemplate.queryForObject("scheduledPlan", arguments("id", 2L).asMap()))
        .thenReturn(secondJob);

    jobInstanceDao.setSqlMapClientTemplate(mockTemplate);
    jobInstanceDao.orderedScheduledBuilds();

    List<JobPlan> plans = jobInstanceDao.orderedScheduledBuilds();

    assertThat(plans, is(expectedPlans));

    verify(mockTemplate, times(2)).queryForObject(eq("scheduledPlan"), any());
    verify(mockTemplate, times(2)).queryForList(eq("scheduledPlanIds"));
  }
  @Test
  public void buildByIdWithTransitions_shouldCacheWhenQueriedFor() {
    jobInstanceDao.setSqlMapClientTemplate(mockTemplate);

    JobInstance job = JobInstanceMother.assigned("job");
    job.setId(1L);
    when(mockTemplate.queryForObject("buildByIdWithTransitions", 1L)).thenReturn(job);

    JobInstance actual = jobInstanceDao.buildByIdWithTransitions(1L);
    assertThat(actual, is(job));
    assertThat(actual == job, is(false));

    jobInstanceDao.buildByIdWithTransitions(1L);
    verify(mockTemplate, times(1)).queryForObject("buildByIdWithTransitions", 1L);
  }
  @Test
  public void buildByIdWithTransitions_shouldClearFromCacheOnUpdateStatusOfJob() {
    jobInstanceDao.setSqlMapClientTemplate(mockTemplate);

    JobInstance job = JobInstanceMother.assigned("job");
    job.setId(1L);
    when(mockTemplate.queryForObject("buildByIdWithTransitions", 1L)).thenReturn(job);

    JobInstance actual = jobInstanceDao.buildByIdWithTransitions(1L);
    assertThat(actual, is(job));
    assertThat(actual == job, is(false));

    jobInstanceDao.updateStateAndResult(job); // Must clear cahced job instance

    jobInstanceDao.buildByIdWithTransitions(1L);
    verify(mockTemplate, times(2)).queryForObject("buildByIdWithTransitions", 1L);
  }
  @Test
  public void activeJobs_shouldCacheCurrentlyActiveJobIds() {
    final ActiveJob first = new ActiveJob(1L, "pipeline", 1, "label", "stage", "job1");
    final ActiveJob second = new ActiveJob(2L, "another", 2, "label", "stage", "job1");
    List<ActiveJob> expectedJobs = Arrays.asList(first, second);

    when(mockTemplate.queryForList("getActiveJobIds")).thenReturn(Arrays.asList(1L, 2L));
    when(mockTemplate.queryForObject("getActiveJobById", arguments("id", 1L).asMap()))
        .thenReturn(first);
    when(mockTemplate.queryForObject("getActiveJobById", arguments("id", 2L).asMap()))
        .thenReturn(second);

    jobInstanceDao.setSqlMapClientTemplate(mockTemplate);
    jobInstanceDao.activeJobs(); // populate the cache
    List<ActiveJob> activeJobs = jobInstanceDao.activeJobs();

    assertThat(expectedJobs, is(activeJobs));
    verify(mockTemplate, times(1)).queryForList("getActiveJobIds");
    verify(mockTemplate, times(1)).queryForObject("getActiveJobById", arguments("id", 1L).asMap());
    verify(mockTemplate, times(1)).queryForObject("getActiveJobById", arguments("id", 2L).asMap());
  }