WDLHandler implements LanguageHandlerInterface { @Override public Version parseWorkflowContent(String filepath, String content, Set<SourceFile> sourceFiles, Version version) { Optional<String> optValidationMessageObject = reportValidationForLocalRecursiveImports(content, sourceFiles, filepath); if (optValidationMessageObject.isPresent()) { Map<String, String> validationMessageObject = new HashMap<>(); validationMessageObject.put(filepath, optValidationMessageObject.get()); version.addOrUpdateValidation(new Validation(DescriptorLanguage.FileType.DOCKSTORE_WDL, false, validationMessageObject)); return version; } WdlBridge wdlBridge = new WdlBridge(); final Map<String, String> secondaryFiles = sourceFiles.stream() .collect(Collectors.toMap(SourceFile::getAbsolutePath, SourceFile::getContent)); wdlBridge.setSecondaryFiles((HashMap<String, String>)secondaryFiles); File tempMainDescriptor = null; try { tempMainDescriptor = File.createTempFile("main", "descriptor", Files.createTempDir()); Files.asCharSink(tempMainDescriptor, StandardCharsets.UTF_8).write(content); try { List<Map<String, String>> metadata = wdlBridge.getMetadata(tempMainDescriptor.getAbsolutePath(), filepath); Set<String> authors = new HashSet<>(); Set<String> emails = new HashSet<>(); final String[] mainDescription = { null }; metadata.forEach(metaBlock -> { String author = metaBlock.get("author"); String[] callAuthors = author != null ? author.split(",") : null; if (callAuthors != null) { for (String callAuthor : callAuthors) { authors.add(callAuthor.trim()); } } String email = metaBlock.get("email"); String[] callEmails = email != null ? email.split(",") : null; if (callEmails != null) { for (String callEmail : callEmails) { emails.add(callEmail.trim()); } } String description = metaBlock.get("description"); if (description != null && !description.isBlank()) { mainDescription[0] = description; } }); if (!authors.isEmpty()) { version.setAuthor(String.join(", ", authors)); } if (!emails.isEmpty()) { version.setEmail(String.join(", ", emails)); } if (!Strings.isNullOrEmpty(mainDescription[0])) { version.setDescriptionAndDescriptionSource(mainDescription[0], DescriptionSource.DESCRIPTOR); } } catch (WdlParser.SyntaxError ex) { LOG.error("Unable to parse WDL file " + filepath, ex); Map<String, String> validationMessageObject = new HashMap<>(); validationMessageObject.put(filepath, "WDL file is malformed or missing, cannot extract metadata. " + ex.getMessage()); version.addOrUpdateValidation(new Validation(DescriptorLanguage.FileType.DOCKSTORE_WDL, false, validationMessageObject)); version.setAuthor(null); version.setDescriptionAndDescriptionSource(null, null); version.setEmail(null); return version; } } catch (IOException e) { throw new CustomWebApplicationException(e.getMessage(), HttpStatus.SC_INTERNAL_SERVER_ERROR); } finally { FileUtils.deleteQuietly(tempMainDescriptor); } return version; }  static void checkForRecursiveLocalImports(String content, Set<SourceFile> sourceFiles, Set<String> absolutePaths, String parent); @Override Version parseWorkflowContent(String filepath, String content, Set<SourceFile> sourceFiles, Version version); Optional<String> reportValidationForLocalRecursiveImports(String primaryDescriptorContent, Set<SourceFile> sourcefiles,
            String primaryDescriptorFilePath); VersionTypeValidation validateEntrySet(Set<SourceFile> sourcefiles, String primaryDescriptorFilePath, String type); void checkForRecursiveHTTPImports(String content, Set<String> currentFileImports); @Override VersionTypeValidation validateWorkflowSet(Set<SourceFile> sourcefiles, String primaryDescriptorFilePath); @Override VersionTypeValidation validateToolSet(Set<SourceFile> sourcefiles, String primaryDescriptorFilePath); @Override VersionTypeValidation validateTestParameterSet(Set<SourceFile> sourceFiles); @Override Map<String, SourceFile> processImports(String repositoryId, String content, Version version,
            SourceCodeRepoInterface sourceCodeRepoInterface, String filepath); @Override String getContent(String mainDescName, String mainDescriptor, Set<SourceFile> secondarySourceFiles,
            LanguageHandlerInterface.Type type, ToolDAO dao); static final Logger LOG; static final String ERROR_PARSING_WORKFLOW_YOU_MAY_HAVE_A_RECURSIVE_IMPORT; static final String ERROR_PARSING_WORKFLOW_RECURSIVE_LOCAL_IMPORT; }
@Test public void getWorkflowContent() throws IOException { final WDLHandler wdlHandler = new WDLHandler(); final Version workflow = new WorkflowVersion(); workflow.setAuthor("Jane Doe"); workflow.setDescriptionAndDescriptionSource("A good description", DescriptionSource.DESCRIPTOR); workflow.setEmail("janedoe@example.org"); final String validFilePath = ResourceHelpers.resourceFilePath("valid_description_example.wdl"); final String goodWdl = FileUtils.readFileToString(new File(validFilePath), StandardCharsets.UTF_8); Version version = wdlHandler.parseWorkflowContent(validFilePath, goodWdl, Collections.emptySet(), workflow); Assert.assertEquals(version.getAuthor(), "Mr. Foo"); Assert.assertEquals(version.getEmail(), "foo@foo.com"); Assert.assertEquals(version.getDescription(), "This is a cool workflow trying another line \n## This is a header\n* First Bullet\n* Second bullet"); final String invalidFilePath = ResourceHelpers.resourceFilePath("invalid_description_example.wdl"); final String invalidDescriptionWdl = FileUtils.readFileToString(new File(invalidFilePath), StandardCharsets.UTF_8); Version version1 = wdlHandler.parseWorkflowContent(invalidFilePath, invalidDescriptionWdl, Collections.emptySet(), version); Assert.assertNull(version1.getAuthor()); Assert.assertNull(version1.getEmail()); }
@Test public void getWorkflowContentOfTool() throws IOException { final WDLHandler wdlHandler = new WDLHandler(); final Tool tool = new Tool(); tool.setAuthor("Jane Doe"); tool.setDescription("A good description"); tool.setEmail("janedoe@example.org"); Assert.assertEquals("Jane Doe", tool.getAuthor()); Assert.assertEquals("A good description", tool.getDescription()); Assert.assertEquals("janedoe@example.org", tool.getEmail()); final String invalidFilePath = ResourceHelpers.resourceFilePath("invalid_description_example.wdl"); final String invalidDescriptionWdl = FileUtils.readFileToString(new File(invalidFilePath), StandardCharsets.UTF_8); wdlHandler.parseWorkflowContent(invalidFilePath, invalidDescriptionWdl, Collections.emptySet(), new WorkflowVersion()); Assert.assertEquals("Jane Doe", tool.getAuthor()); Assert.assertEquals("A good description", tool.getDescription()); Assert.assertEquals("janedoe@example.org", tool.getEmail()); }