XmlDownloadTask implements Supplier<XmlDownloadResult> { @Override public XmlDownloadResult get() { try { final DSSDocument dssDocument = dssFileLoader.getDocument(url); final Document dom = DomUtils.buildDOM(dssDocument); final byte[] canonicalizedContent = DSSXMLUtils.canonicalizeSubtree(CanonicalizationMethod.EXCLUSIVE, dom); return new XmlDownloadResult(dssDocument, new Digest(DigestAlgorithm.SHA256, DSSUtils.digest(DigestAlgorithm.SHA256, canonicalizedContent))); } catch (DSSException e) { throw e; } catch (Exception e) { throw new DSSException(String.format("Unable to retrieve the content for url '%s'. Reason : '%s'", url, e.getMessage()), e); } } XmlDownloadTask(DSSFileLoader dssFileLoader, String url); @Override XmlDownloadResult get();  }
@Test public void nullResult() { Map<String, byte[]> dataMap = new HashMap<>(); dataMap.put("null", null); dataMap.put("empty-array", new byte[] {}); dataMap.put("0", new byte[] { 0 }); MemoryDataLoader memoryDataLoader = new MemoryDataLoader(dataMap); DSSFileLoader dataLoader = new FileCacheDataLoader(memoryDataLoader); for (String url : dataMap.keySet()) { XmlDownloadTask task = new XmlDownloadTask(dataLoader, url); assertThrows(DSSException.class, () -> task.get()); } }
@Test public void nonNullResults() { Map<String, byte[]> dataMap = new HashMap<>(); byte[] sampleByteArray = DSSUtils.toByteArray(new FileDocument(new File("src/test/resources/sample.xml"))); dataMap.put("sample", sampleByteArray); dataMap.put("sample-spaces", DSSUtils.toByteArray(new FileDocument(new File("src/test/resources/sample-spaces.xml")))); dataMap.put("sample-comment", DSSUtils.toByteArray(new FileDocument(new File("src/test/resources/sample-comment.xml")))); byte[] sampleWithBomByteArray = DSSUtils.toByteArray(new FileDocument(new File("src/test/resources/sample-bom.xml"))); dataMap.put("sample-bom", sampleWithBomByteArray); assertNotEquals(DSSUtils.getMD5Digest(sampleByteArray), DSSUtils.getMD5Digest(sampleWithBomByteArray)); MemoryDataLoader memoryDataLoader = new MemoryDataLoader(dataMap); FileCacheDataLoader fileCacheDataLoader = new FileCacheDataLoader(memoryDataLoader); fileCacheDataLoader.setCacheExpirationTime(0); XmlDownloadResult first = null; for (String url : dataMap.keySet()) { XmlDownloadTask task = new XmlDownloadTask(fileCacheDataLoader, url); XmlDownloadResult downloadResult = task.get(); assertNotNull(downloadResult); assertNotNull(downloadResult.getDSSDocument()); assertNotNull(downloadResult.getDigest()); assertNotNull(downloadResult.getDigest().getAlgorithm()); assertNotNull(downloadResult.getDigest().getValue()); if (first == null) { first = downloadResult; } else { assertEquals(first.getDigest(), downloadResult.getDigest()); } } dataMap.put("sample-diff", DSSUtils.toByteArray(new FileDocument(new File("src/test/resources/sample-diff.xml")))); memoryDataLoader = new MemoryDataLoader(dataMap); fileCacheDataLoader = new FileCacheDataLoader(memoryDataLoader); XmlDownloadTask task = new XmlDownloadTask(fileCacheDataLoader, "sample-diff"); XmlDownloadResult downloadResultDiff = task.get(); assertNotNull(downloadResultDiff); assertNotNull(downloadResultDiff.getDigest()); assertNotEquals(first.getDigest(), downloadResultDiff.getDigest()); }