DataObjectMerger extends AbstractElementMerger { @Override public void mergeNodes(List<Node> fromParents, Element toParent, Document toDocument) throws PDUStitchingException { final String fileName = getFileName(fromParents, toParent); final File file = getFile(fileName, toParent); final long size = file.length(); final Element byteStreamElement = toDocument.createElement("byteStream"); toParent.appendChild(byteStreamElement); byteStreamElement.setAttribute("mimeType", "application/x-netcdf"); byteStreamElement.setAttribute("size", String.valueOf(size)); final Element fileLocationElement = toDocument.createElement("fileLocation"); byteStreamElement.appendChild(fileLocationElement); fileLocationElement.setAttribute("locatorType", "URL"); fileLocationElement.setAttribute("href", fileName); final Element checksumElement = toDocument.createElement("checksum"); byteStreamElement.appendChild(checksumElement); checksumElement.setAttribute("checksumName", "MD5"); final String checksum; checksum = getChecksum(file); if (checksum.equals("")) { throw new PDUStitchingException("Could not create checksum for file " + fileName); } addTextToNode(checksumElement, checksum, toDocument); } DataObjectMerger(String fileLocation); @Override void mergeNodes(List<Node> fromParents, Element toParent, Document toDocument);  }
@Test public void testMergeNodes() throws Exception { List<Node> fromParents = new ArrayList<>(); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <dataObject ID=\"ADFData\">\n" + " <byteStream mimeType=\"application/x-netcdf\" size=\"36985\">\n" + " <fileLocation locatorType=\"URL\" href=\"viscal.nc\"/>\n" + " <checksum checksumName=\"MD5\">9c0ed7a6a15d3bfebbeb1609a8d41fa0</checksum>\n" + " </byteStream>\n" + " </dataObject>").getFirstChild()); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <dataObject ID=\"ADFData\">\n" + " <byteStream mimeType=\"application/x-netcdf\" size=\"36985\">\n" + " <fileLocation locatorType=\"URL\" href=\"viscal.nc\"/>\n" + " <checksum checksumName=\"MD5\">bd2e513c1fc816c899fe5b3130846bdd</checksum>\n" + " </byteStream>\n" + " </dataObject>").getFirstChild()); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <dataObject ID=\"ADFData\">\n" + " <byteStream mimeType=\"application/x-netcdf\" size=\"36985\">\n" + " <fileLocation locatorType=\"URL\" href=\"viscal.nc\"/>\n" + " <checksum checksumName=\"MD5\">b12d0db5bbfdc22ea728219511894868</checksum>\n" + " </byteStream>\n" + " </dataObject>").getFirstChild()); Document manifest = ManifestTestUtils.createDocument(); final Element manifestElement = manifest.createElement("dataObject"); manifestElement.setAttribute("ID", "test"); manifest.appendChild(manifestElement); final DataObjectMerger dataObjectMerger = new DataObjectMerger(DataObjectMergerTest.class.getResource("").getFile()); dataObjectMerger.mergeNodes(fromParents, manifestElement, manifest); final NodeList manifestChilds = manifestElement.getChildNodes(); assertEquals(1, manifestChilds.getLength()); final Node byteStreamElement = manifestChilds.item(0); assertEquals("byteStream", byteStreamElement.getNodeName()); final NamedNodeMap byteStreamElementAttributes = byteStreamElement.getAttributes(); assertEquals(2, byteStreamElementAttributes.getLength()); final Node mimeTypeAttribute = byteStreamElementAttributes.getNamedItem("mimeType"); assertNotNull(mimeTypeAttribute); assertEquals("application/x-netcdf", mimeTypeAttribute.getNodeValue()); final Node sizeAttribute = byteStreamElementAttributes.getNamedItem("size"); assertNotNull(sizeAttribute); assertEquals("30", sizeAttribute.getNodeValue()); final NodeList byteStreamChildren = byteStreamElement.getChildNodes(); assertEquals(2, byteStreamChildren.getLength()); final Node fileLocationElement = byteStreamChildren.item(0); assertEquals("fileLocation", fileLocationElement.getNodeName()); final NamedNodeMap fileLocationElementAttributes = fileLocationElement.getAttributes(); assertEquals(2, fileLocationElementAttributes.getLength()); final Node locatorTypeAttribute = fileLocationElementAttributes.getNamedItem("locatorType"); assertNotNull(locatorTypeAttribute); assertEquals("URL", locatorTypeAttribute.getNodeValue()); final Node hrefAttribute = fileLocationElementAttributes.getNamedItem("href"); assertNotNull(hrefAttribute); assertEquals("viscal.nc", hrefAttribute.getNodeValue()); final Node checksumElement = byteStreamChildren.item(1); assertEquals("checksum", checksumElement.getNodeName()); final NamedNodeMap checksumElementAttributes = checksumElement.getAttributes(); assertEquals(1, checksumElementAttributes.getLength()); final Node checksumNameAttribute = checksumElementAttributes.getNamedItem("checksumName"); assertNotNull(checksumNameAttribute); assertEquals("MD5", checksumNameAttribute.getNodeValue()); assertEquals("97b5dcca12501c1f051186b9d8755373", checksumElement.getTextContent()); }