@Test
public void testCalculateChecksum() throws Exception {

  String extendedFlavorType = "extended";
  DublinCoreCatalog dc = SchedulerServiceImplTest.generateExtendedEvent(Opt.<String> none(), extendedFlavorType);
  FileUtils.writeStringToFile(workspaceFile, dc.toXmlString(), "UTF-8");
  List<MediaPackageElementFlavor> catalogAdapterFlavors = new ArrayList<>();
  catalogAdapterFlavors.add(new MediaPackageElementFlavor(extendedFlavorType, "episode"));
  AccessControlList acl = new AccessControlList(new AccessControlEntry("ROLE_ADMIN", "write", true));
  Date start = new Date(DateTimeSupport.fromUTC("2008-03-16T14:00:00Z"));
  Date end = new Date(DateTimeSupport.fromUTC("2008-03-16T15:00:00Z"));
  String captureDeviceID = "demo";
  String seriesId = "series1";
  Set<String> userIds = new HashSet<>();
  userIds.add("user2");
  userIds.add("user1");
  MediaPackage mp = SchedulerServiceImplTest.generateEvent(Opt.<String> none());
  mp.setSeries(seriesId);
  DublinCoreCatalog event = SchedulerServiceImplTest.generateEvent(captureDeviceID, start, end);
  event.set(PROPERTY_CREATED, EncodingSchemeUtils.encodeDate(start, Precision.Minute));
  String catalogId = UUID.randomUUID().toString();
  MediaPackageElement catalog = mp.add(new URI("location" + catalogId), Type.Catalog, event.getFlavor());
  catalog.setIdentifier(catalogId);
  String extendedCatalogId = UUID.randomUUID().toString();
  MediaPackageElement extendedCatalog = mp.add(new URI("location" + extendedCatalogId), Type.Catalog, dc.getFlavor());
  extendedCatalog.setIdentifier(extendedCatalogId);
  Map<String, String> caProperties = SchedulerServiceImplTest.generateCaptureAgentMetadata("demo");
  Map<String, String> wfProperties = new HashMap<String, String>();
  wfProperties.put("test", "true");
  wfProperties.put("clear", "all");
  String expectedChecksum = "2d5398c961f0784a13eaad323ebc43f3";
  String checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID,
          userIds, mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertEquals(expectedChecksum, checksum);
  // change start date
  start = new Date();
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change end date
  start = new Date(DateTimeSupport.fromUTC("2008-03-16T14:00:00Z"));
  end = new Date();
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change device
  end = new Date(DateTimeSupport.fromUTC("2008-03-16T15:00:00Z"));
  captureDeviceID = "demo1";
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change users
  captureDeviceID = "demo";
  userIds.add("test");
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change episode dublincore
  userIds.remove("test");
  catalog.setChecksum(null);
  event.set(PROPERTY_CREATED, EncodingSchemeUtils.encodeDate(end, Precision.Minute));
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change extended dublincore
  catalog.setChecksum(null);
  event.set(PROPERTY_CREATED, EncodingSchemeUtils.encodeDate(start, Precision.Minute));
  extendedCatalog.setChecksum(null);
  dc.set(PROPERTY_CREATED, EncodingSchemeUtils.encodeDate(start, Precision.Minute));
  FileUtils.writeStringToFile(workspaceFile, dc.toXmlString(), "UTF-8");
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change wf properties
  extendedCatalog.setChecksum(null);
  dc.remove(PROPERTY_CREATED);
  FileUtils.writeStringToFile(workspaceFile, dc.toXmlString(), "UTF-8");
  wfProperties.put("change", "change");
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change ca properties
  wfProperties.remove("change");
  caProperties.put("change", "change");
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties, acl);
  Assert.assertNotEquals(expectedChecksum, checksum);
  // change access control list
  checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds,
          mp, Opt.some(event), wfProperties, caProperties,
          new AccessControlList(new AccessControlEntry("ROLE_ADMIN", "write", false)));
  Assert.assertNotEquals(expectedChecksum, checksum);
}