void persistConfiguration() {
   List<PastSnapshot> pastSnapshots = configuration.getProjectPastSnapshots();
   for (PastSnapshot pastSnapshot : pastSnapshots) {
     projectSnapshot = session.reattach(Snapshot.class, projectSnapshot.getId());
     projectSnapshot.setPeriodMode(pastSnapshot.getIndex(), pastSnapshot.getMode());
     projectSnapshot.setPeriodModeParameter(
         pastSnapshot.getIndex(), pastSnapshot.getModeParameter());
     projectSnapshot.setPeriodDate(pastSnapshot.getIndex(), pastSnapshot.getTargetDate());
     session.save(projectSnapshot);
   }
   session.commit();
 }
 /**
  * Only used to get the real date of the snapshot on the current period. The date is used to
  * calculate new_violations measures
  */
 @CheckForNull
 private Snapshot findSnapshot(Snapshot projectSnapshot) {
   String hql =
       "from "
           + Snapshot.class.getSimpleName()
           + " where resourceId=:resourceId and (rootId=:rootSnapshotId or id=:rootSnapshotId)";
   List<Snapshot> snapshots =
       session
           .createQuery(hql)
           .setParameter("resourceId", projectSnapshot.getResourceId())
           .setParameter("rootSnapshotId", projectSnapshot.getId())
           .setMaxResults(1)
           .getResultList();
   return snapshots.isEmpty() ? null : snapshots.get(0);
 }
  private void initModulePastSnapshots() {
    periods = newLinkedList();
    modulePastSnapshots = newLinkedList();
    for (PastSnapshot projectPastSnapshot : periodsDefinition.getRootProjectPastSnapshots()) {
      Snapshot snapshot = findSnapshot(projectPastSnapshot.getProjectSnapshot());

      PastSnapshot pastSnapshot = projectPastSnapshot.clonePastSnapshot();
      modulePastSnapshots.add(pastSnapshot);
      // When no snapshot is found, date of the period is null
      periods.add(
          new Period(
              pastSnapshot.getIndex(),
              snapshot != null ? longToDate(snapshot.getCreatedAtMs()) : null));
      LOG.info(pastSnapshot.toString());
    }
  }
  @Test
  public void shouldFindByPreviousAnalysis() throws ParseException {
    final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    final Date date = format.parse("2010-05-18");
    Snapshot snapshot = new Snapshot();
    snapshot.setCreatedAt(date);
    when(finderByPreviousAnalysis.findByPreviousAnalysis(null))
        .thenReturn(new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, snapshot));

    PastSnapshot variationSnapshot =
        finder.find(null, 2, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS);

    verify(finderByPreviousAnalysis).findByPreviousAnalysis(null);
    assertThat(variationSnapshot.getIndex(), is(2));
    assertThat(variationSnapshot.getMode(), is(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS));
    assertThat(variationSnapshot.getProjectSnapshot(), not(nullValue()));
  }
 private static Snapshot snapshot(int id) {
   Snapshot snapshot = mock(Snapshot.class);
   when(snapshot.getId()).thenReturn(id);
   return snapshot;
 }