ZipSourceFileHelper { protected static SourceFiles sourceFilesFromZip(ZipFile zipFile, DescriptorLanguage.FileType workflowFileType) { DockstoreYaml10 dockstoreYml = readAndPrevalidateDockstoreYml(zipFile); final String primaryDescriptor = dockstoreYml.primaryDescriptor; List<String> testParameterFiles = dockstoreYml.testParameterFiles; if (primaryDescriptor != null) { checkWorkflowType(workflowFileType, primaryDescriptor); zipFile.stream() .filter(zipEntry -> primaryDescriptor.equals(zipEntry.getName())) .findFirst() .orElseThrow(() -> new CustomWebApplicationException("Primary descriptor missing: " + primaryDescriptor, HttpStatus.SC_BAD_REQUEST)); final List<SourceFile> sourceFiles = zipFile .stream() .filter(zipEntry -> !zipEntry.isDirectory()) .map(zipEntry -> { SourceFile sourceFile = new SourceFile(); if (testParameterFiles != null && testParameterFiles.contains(zipEntry.getName())) { sourceFile.setType(paramFileType(workflowFileType)); } else if (".dockstore.yml".equals(zipEntry.getName())) { sourceFile.setType(DescriptorLanguage.FileType.DOCKSTORE_YML); } else { sourceFile.setType(workflowFileType); } sourceFile.setPath(zipEntry.getName()); sourceFile.setAbsolutePath(addLeadingSlashIfNecessary(zipEntry.getName())); sourceFile.setContent(getContent(zipFile, zipEntry)); return sourceFile; }).collect(Collectors.toList()); return new SourceFiles( sourceFiles.stream().filter(sf -> sf.getPath().equals(primaryDescriptor)).findFirst().get(), sourceFiles); } else { throw new CustomWebApplicationException("Invalid or no primary descriptor specified in .dockstore.yml", HttpStatus.SC_BAD_REQUEST); } } private  ZipSourceFileHelper(); static SourceFiles sourceFilesFromInputStream(InputStream payload, DescriptorLanguage.FileType fileType); static String addLeadingSlashIfNecessary(final String name);  }
@Test public void sourceFilesFromZip() throws IOException { try (ZipFile smartSeqZipFile = new ZipFile(new File(SMART_SEQ_ZIP_PATH))) { final ZipSourceFileHelper.SourceFiles sourceFiles = ZipSourceFileHelper.sourceFilesFromZip(smartSeqZipFile, DescriptorLanguage.FileType.DOCKSTORE_WDL); Assert.assertEquals("SmartSeq2SingleSample.wdl", sourceFiles.getPrimaryDescriptor().getPath()); Assert.assertEquals("/SmartSeq2SingleSample.wdl", sourceFiles.getPrimaryDescriptor().getAbsolutePath()); Assert.assertEquals(9, sourceFiles.getAllDescriptors().size()); Assert.assertEquals("Expecting one .dockstore.yml", 1, sourceFiles.getAllDescriptors().stream().filter(sf -> sf.getType() == DescriptorLanguage.FileType.DOCKSTORE_YML).count()); } }
@Test public void sourceFilesFromZipWithFolder() throws IOException { try (ZipFile whalesayZipFile = new ZipFile(new File(WHALESAY_ZIP_PATH))) { final ZipSourceFileHelper.SourceFiles sourceFiles = ZipSourceFileHelper.sourceFilesFromZip(whalesayZipFile, DescriptorLanguage.FileType.DOCKSTORE_WDL); Assert.assertEquals("subdir/Dockstore.wdl", sourceFiles.getPrimaryDescriptor().getPath()); Assert.assertEquals("/subdir/Dockstore.wdl", sourceFiles.getPrimaryDescriptor().getAbsolutePath()); Assert.assertEquals(2, sourceFiles.getAllDescriptors().size()); } }
@Test public void validateType() throws IOException { ZipFile smartSeqZipFile = new ZipFile(new File(SMART_SEQ_ZIP_PATH)); try { ZipSourceFileHelper.sourceFilesFromZip(smartSeqZipFile, DescriptorLanguage.FileType.DOCKSTORE_CWL); Assert.fail("Expected failure because zip has WDL but workflow is CWL"); } catch (CustomWebApplicationException ex) { } }