SchedulerUtil { public static String calculateChecksum( Workspace workspace, List<MediaPackageElementFlavor> eventCatalogUIAdapterFlavors, Date startDateTime, Date endDateTime, String captureAgentId, Set<String> userIds, MediaPackage mediaPackage, Opt<DublinCoreCatalog> episodeDublincore, Map<String, String> wfProperties, Map<String, String> finalCaProperties, AccessControlList acl) { List<String> userIdsList = new ArrayList<>(userIds); Collections.sort(userIdsList); final MessageDigest messageDigest = mkMd5MessageDigest(); messageDigest.update(mkChecksumInput(startDateTime)); messageDigest.update(mkChecksumInput(endDateTime)); messageDigest.update(mkChecksumInput(captureAgentId)); for (String user : userIdsList) { messageDigest.update(mkChecksumInput(user)); } if (episodeDublincore.isSome()) { Catalog episodeCatalog = $(mediaPackage.getCatalogs()) .filter(MediaPackageSupport.Filters.isEpisodeDublinCore.toFn()).head2(); Checksum checksum = episodeCatalog.getChecksum(); if (checksum == null) { checksum = DublinCoreUtil.calculateChecksum(episodeDublincore.get()); episodeCatalog.setChecksum(checksum); } messageDigest.update(mkChecksumInput(checksum.toString())); } for (Catalog c : $(mediaPackage.getCatalogs()).sort(sortCatalogById)) { if (eventCatalogUIAdapterFlavors.contains(c.getFlavor())) { Checksum checksum = c.getChecksum(); if (checksum == null) { DublinCoreCatalog dublinCore = DublinCoreUtil.loadDublinCore(workspace, c); checksum = DublinCoreUtil.calculateChecksum(dublinCore); c.setChecksum(checksum); } messageDigest.update(mkChecksumInput(checksum.toString())); } } messageDigest.update(mkChecksumInput(AccessControlUtil.calculateChecksum(acl).toString())); for (Entry<String, String> entry : new TreeMap<>(wfProperties).entrySet()) { messageDigest.update(mkChecksumInput(entry.getKey())); messageDigest.update(mkChecksumInput(entry.getValue())); } for (Entry<String, String> entry : new TreeMap<>(finalCaProperties).entrySet()) { messageDigest.update(mkChecksumInput(entry.getKey())); messageDigest.update(mkChecksumInput(entry.getValue())); } return Checksum.convertToHex(messageDigest.digest()); } private  SchedulerUtil(); static String calculateChecksum(
      Workspace workspace,
      List<MediaPackageElementFlavor> eventCatalogUIAdapterFlavors,
      Date startDateTime,
      Date endDateTime,
      String captureAgentId,
      Set<String> userIds,
      MediaPackage mediaPackage,
      Opt<DublinCoreCatalog> episodeDublincore,
      Map<String, String> wfProperties,
      Map<String, String> finalCaProperties,
      AccessControlList acl); static String toHumanReadableString(Workspace workspace, List<MediaPackageElementFlavor> catalogFlavors,
          SchedulerEvent event); static final Comparator<Catalog> sortCatalogById; static final Fn<MediaPackageElementFlavor, Boolean> isNotEpisodeDublinCore; static final Fn<EventCatalogUIAdapter, MediaPackageElementFlavor> uiAdapterToFlavor; static final Fn2<EventCatalogUIAdapter, String, Boolean> eventOrganizationFilter; static final Fn<Snapshot, MediaPackage> episodeToMp; static final Fn<ARecord, Opt<MediaPackage>> recordToMp; }
@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); start = new Date(); checksum = SchedulerUtil.calculateChecksum(workspace, catalogAdapterFlavors, start, end, captureDeviceID, userIds, mp, Opt.some(event), wfProperties, caProperties, acl); Assert.assertNotEquals(expectedChecksum, checksum); 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); 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); 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); 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); 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); 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); 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); 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); }