AddCatalogWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(WorkflowInstance wInst, JobContext context) throws WorkflowOperationException { String catalogName = getConfig(wInst, CFG_KEY_CATALOG_NAME); String catalogPath = getConfig(wInst, CFG_KEY_CATALOG_PATH); String catalogTags = getConfig(wInst, CFG_KEY_CATALOG_TAGS, ""); CatalogTypeCollisionBehavior collBehavior = parseCollisionBehavior( getConfig(wInst, CFG_KEY_CATALOG_TYPE_COLLISION_BEHAVIOR)); MediaPackageElementFlavor catalogFlavor = null; try { catalogFlavor = MediaPackageElementFlavor.parseFlavor( getConfig(wInst, CFG_KEY_CATALOG_FLAVOR)); } catch (IllegalArgumentException e) { throw new WorkflowOperationException("Unknown flavor"); } MediaPackage mp = wInst.getMediaPackage(); if (doesCatalogFlavorExist(catalogFlavor, mp.getCatalogs())) { if (collBehavior == CatalogTypeCollisionBehavior.FAIL) { throw new WorkflowOperationException("Catalog Type already exists and 'fail' was specified"); } else if (collBehavior == CatalogTypeCollisionBehavior.SKIP) { return createResult(mp, Action.CONTINUE); } } File catalogFile = new File(catalogPath); String catalogId = UUID.randomUUID().toString(); URI catalogURI = null; try (InputStream catalogInputStream = FileUtils.openInputStream(catalogFile)) { catalogURI = workspace.put(mp.getIdentifier().toString(), catalogId, catalogName, catalogInputStream); } catch (IOException e) { throw new WorkflowOperationException(e); } MediaPackageElement mpe = mp.add(catalogURI, MediaPackageElement.Type.Catalog, catalogFlavor); mpe.setIdentifier(catalogId); mpe.setMimeType(CATALOG_MIME_TYPE); for (String tag : asList(catalogTags)) { mpe.addTag(tag); } return createResult(mp, Action.CONTINUE); }  void setWorkspace(Workspace workspace); @Override WorkflowOperationResult start(WorkflowInstance wInst, JobContext context);  }
@Test public void testBasic() throws WorkflowOperationException { operation.setConfiguration("catalog-flavor", "flavor/test"); operation.setConfiguration("catalog-type-collision-behavior", "keep"); operation.setConfiguration("catalog-tags", "tag1,tag2"); WorkflowOperationResult result = operationHandler.start(instance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); MediaPackage mp = result.getMediaPackage(); Assert.assertEquals(mp.getCatalogs().length, 1); Assert.assertEquals(mp.getCatalogs()[0].getTags().length, 2); Assert.assertEquals(mp.getCatalogs()[0].getTags()[0], "tag1"); Assert.assertEquals(mp.getCatalogs()[0].getTags()[1], "tag2"); Assert.assertEquals(mp.getCatalogs()[0].getFlavor(), MediaPackageElementFlavor.parseFlavor("flavor/test")); }
@Test public void testNoTags() throws WorkflowOperationException { operation.setConfiguration("catalog-flavor", "flavor/test"); operation.setConfiguration("catalog-type-collision-behavior", "keep"); WorkflowOperationResult result = operationHandler.start(instance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); MediaPackage mp = result.getMediaPackage(); Assert.assertEquals(mp.getCatalogs().length, 1); Assert.assertEquals(mp.getCatalogs()[0].getTags().length, 0); Assert.assertEquals(mp.getCatalogs()[0].getFlavor(), MediaPackageElementFlavor.parseFlavor("flavor/test")); }
@Test public void testNoFlavorFail() throws WorkflowOperationException { operation.setConfiguration("catalog-flavor", ""); operation.setConfiguration("catalog-type-collision-behavior", "keep"); expectedException.expect(WorkflowOperationException.class); operationHandler.start(instance, null); }
@Test public void testKeep() throws WorkflowOperationException { operation.setConfiguration("catalog-flavor", "flavor/test"); operation.setConfiguration("catalog-type-collision-behavior", "keep"); operationHandler.start(instance, null); WorkflowOperationResult result = operationHandler.start(instance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); MediaPackage mp = result.getMediaPackage(); Assert.assertEquals(mp.getCatalogs().length, 2); }
@Test public void testSkip() throws WorkflowOperationException { operation.setConfiguration("catalog-flavor", "flavor/test"); operation.setConfiguration("catalog-type-collision-behavior", "skip"); operationHandler.start(instance, null); WorkflowOperationResult result = operationHandler.start(instance, null); Assert.assertEquals(Action.CONTINUE, result.getAction()); MediaPackage mp = result.getMediaPackage(); Assert.assertEquals(mp.getCatalogs().length, 1); }
@Test public void testFail() throws WorkflowOperationException { operation.setConfiguration("catalog-flavor", "flavor/test"); operation.setConfiguration("catalog-type-collision-behavior", "fail"); operationHandler.start(instance, null); expectedException.expect(WorkflowOperationException.class); operationHandler.start(instance, null); }