From f810a6daccfd83e984558db86caaa87d442fdf64 Mon Sep 17 00:00:00 2001 From: Jing Tao Date: Wed, 26 Jun 2024 18:23:24 -0700 Subject: [PATCH] Modified the ObjectManagerTest class to use the new methods. --- .../cn/indexer/object/ObjectManager.java | 1 - .../cn/index/SolrFieldXPathFgdcTest.java | 4 +- .../cn/indexer/object/ObjectManagerTest.java | 154 ++++++++---------- .../fgdc/nasa_d_FEDGPS1293Sysmeta.xml | 4 +- 4 files changed, 71 insertions(+), 92 deletions(-) diff --git a/src/main/java/org/dataone/cn/indexer/object/ObjectManager.java b/src/main/java/org/dataone/cn/indexer/object/ObjectManager.java index 631d0f59..8da964de 100644 --- a/src/main/java/org/dataone/cn/indexer/object/ObjectManager.java +++ b/src/main/java/org/dataone/cn/indexer/object/ObjectManager.java @@ -69,7 +69,6 @@ public class ObjectManager { private static Storage storage = null; private static final String TOKEN_VARIABLE_NAME = "DATAONE_AUTH_TOKEN"; private static final String TOKEN_FILE_PATH_PROP_NAME = "dataone.nodeToken.file"; - private static final String SYSTEMMETA_FILE_NAME = "systemmetadata.xml"; private static MultipartD1Node d1Node = null; private static Session session = null; diff --git a/src/test/java/org/dataone/cn/index/SolrFieldXPathFgdcTest.java b/src/test/java/org/dataone/cn/index/SolrFieldXPathFgdcTest.java index 4e317dd1..3ab0d6fc 100644 --- a/src/test/java/org/dataone/cn/index/SolrFieldXPathFgdcTest.java +++ b/src/test/java/org/dataone/cn/index/SolrFieldXPathFgdcTest.java @@ -296,8 +296,8 @@ public static void setUp() throws Exception { fgdcNasaExpected.put("mediaTypeProperty", ""); fgdcNasaExpected.put("formatId", "FGDC-STD-001.1-1999"); fgdcNasaExpected.put("formatType", "METADATA"); - fgdcNasaExpected.put("size", "14880"); - fgdcNasaExpected.put("checksum", "c72ff66bbe7fa99e5fb399bab8cb6f85"); + fgdcNasaExpected.put("size", "14828"); + fgdcNasaExpected.put("checksum", "1755a557c13be7af44d676bb09274b0e"); fgdcNasaExpected.put("checksumAlgorithm", "MD5"); fgdcNasaExpected.put("submitter", "CN=Dave Vieglais T799,O=Google,C=US,DC=cilogon,DC=org"); fgdcNasaExpected.put("rightsHolder", diff --git a/src/test/java/org/dataone/cn/indexer/object/ObjectManagerTest.java b/src/test/java/org/dataone/cn/indexer/object/ObjectManagerTest.java index b50c6e5c..32cc9b12 100644 --- a/src/test/java/org/dataone/cn/indexer/object/ObjectManagerTest.java +++ b/src/test/java/org/dataone/cn/indexer/object/ObjectManagerTest.java @@ -1,33 +1,25 @@ -/** - * This work was created by participants in the DataONE project, and is - * jointly copyrighted by participating institutions in DataONE. For - * more information on DataONE, see our web site at http://dataone.org. - * - * Copyright 2022 - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ package org.dataone.cn.indexer.object; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; + +import java.io.InputStream; +import java.io.OutputStream; +import java.security.MessageDigest; -import java.nio.file.Paths; +import javax.xml.bind.DatatypeConverter; -import org.dataone.cn.index.DataONESolrJettyTestBase; -import org.dataone.service.exceptions.NotFound; + +import org.dataone.indexer.storage.StorageFactory; +import org.dataone.service.types.v1.Identifier; import org.dataone.service.types.v2.SystemMetadata; +import org.dataone.service.util.TypeMarshaller; +import org.junit.Before; import org.junit.Test; /** @@ -36,73 +28,61 @@ * */ public class ObjectManagerTest { - - /** - * Test the getFilePath method - * @throws Exception - */ - @Test - public void testgetFilePath() throws Exception { - ObjectManager manager = ObjectManager.getInstance(); - String path = null; - String format = "eml://ecoinformatics.org/eml-2.0.1"; - String resultPath = manager.getFilePath(path, format); - assertTrue(resultPath == null); - format = "image/bmp"; - resultPath = manager.getFilePath(path, format); - assertTrue(resultPath == null); - - path = ""; - format = "eml://ecoinformatics.org/eml-2.0.1"; - resultPath = manager.getFilePath(path, format); - assertTrue(resultPath == null); - format = "image/bmp"; - resultPath = manager.getFilePath(path, format); - assertTrue(resultPath == null); - - path = "/var/metacat/documents/foo.1.1"; - format = "eml://ecoinformatics.org/eml-2.0.1"; - resultPath = manager.getFilePath(path, format); - assertTrue(resultPath.equals("//var/metacat/documents/foo.1.1")); - - path = "/var/metacat/documents/foo.2.1"; - format = "image/bmp";; - resultPath = manager.getFilePath(path, format); - assertTrue(resultPath.equals("//var/metacat/documents/foo.2.1")); + + private String identifier; + + @Before + public void setUp() throws Exception { + identifier = "ObjectManagerTest-" + System.currentTimeMillis(); + File objectFile = new File("src/test/resources/org/dataone/cn/index/resources/d1_testdocs/" + + "fgdc/nasa_d_FEDGPS1293.xml"); + try (InputStream object = new FileInputStream(objectFile)) { + StorageFactory.getStorage().storeObject(object, identifier); + } + File sysmetaFile = new File("src/test/resources/org/dataone/cn/index/resources/" + + "d1_testdocs/fgdc/nasa_d_FEDGPS1293Sysmeta.xml"); + try (InputStream sysmetaStream = new FileInputStream(sysmetaFile)) { + SystemMetadata sysmeta = TypeMarshaller + .unmarshalTypeFromStream(SystemMetadata.class, sysmetaStream); + Identifier pid = new Identifier(); + pid.setValue(identifier); + sysmeta.setIdentifier(pid); + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { + TypeMarshaller.marshalTypeToOutputStream(sysmeta, output); + try (ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray())) { + StorageFactory.getStorage().storeMetadata(input, identifier); + } + } + } } - + /** - * Test the getSystemMetadata method + * Test the getObject and getSystemMetadata method * @throws Exception */ - @Test - public void testGetSystemMetadata() throws Exception { - //Test to get system metadata from a file - String currentDir = Paths.get(".").toAbsolutePath().normalize().toString(); - System.out.println("current dir " + currentDir); - String path = currentDir + "/src/test/resources/org/dataone/cn/index/resources/d1_testdocs/json-ld/hakai-deep-schema/hakai-deep-schema.jsonld"; - String id = "hakai-deep-schema.jsonld"; - SystemMetadata sysmeta = ObjectManager.getInstance().getSystemMetadata(id, path); - assertTrue(sysmeta.getIdentifier().getValue().equals(id)); - - //Test to get system metadata from the Mock dataone cn server. - id = "ala-wai-canal-ns02-matlab-processing.eml.1.xml"; - path = null; - MockMNode mockMNode = new MockMNode("http://mnode.foo"); - mockMNode.setContext(DataONESolrJettyTestBase.getContext()); - ObjectManager.setD1Node(mockMNode); - sysmeta = ObjectManager.getInstance().getSystemMetadata(id, path); - assertTrue(sysmeta.getIdentifier().getValue().equals(id)); - - //Test the system metadata not found - id = "foo.1.1"; - path = "foo1"; - try { - sysmeta = ObjectManager.getInstance().getSystemMetadata(id, path); - fail("We should reach here"); - } catch (NotFound e) { - assert(true); + @Test + public void testGetObjectAndSystemMetadata() throws Exception { + try (InputStream input = ObjectManager.getInstance().getObject(identifier)) { + assertNotNull(input); + try (OutputStream os = new ByteArrayOutputStream()) { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + // Calculate hex digests + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = input.read(buffer)) != -1) { + os.write(buffer, 0, bytesRead); + md5.update(buffer, 0, bytesRead); + } + String md5Digest = DatatypeConverter.printHexBinary(md5.digest()).toLowerCase(); + assertEquals("1755a557c13be7af44d676bb09274b0e", md5Digest); + } } + org.dataone.service.types.v1.SystemMetadata sysmeta = ObjectManager.getInstance() + .getSystemMetadata(identifier); + assertEquals(identifier, sysmeta.getIdentifier().getValue()); + assertEquals("1755a557c13be7af44d676bb09274b0e", sysmeta.getChecksum().getValue()); + assertEquals(14828, sysmeta.getSize().intValue()); } + } diff --git a/src/test/resources/org/dataone/cn/index/resources/d1_testdocs/fgdc/nasa_d_FEDGPS1293Sysmeta.xml b/src/test/resources/org/dataone/cn/index/resources/d1_testdocs/fgdc/nasa_d_FEDGPS1293Sysmeta.xml index 42998531..9b0dfbd6 100644 --- a/src/test/resources/org/dataone/cn/index/resources/d1_testdocs/fgdc/nasa_d_FEDGPS1293Sysmeta.xml +++ b/src/test/resources/org/dataone/cn/index/resources/d1_testdocs/fgdc/nasa_d_FEDGPS1293Sysmeta.xml @@ -9,8 +9,8 @@ 22 www.nbii.gov_metadata_mdata_NASA_nasa_d_FEDGPS1293 FGDC-STD-001.1-1999 - 14880 - c72ff66bbe7fa99e5fb399bab8cb6f85 + 14828 + 1755a557c13be7af44d676bb09274b0e CN=Dave Vieglais T799,O=Google,C=US,DC=cilogon,DC=org CN=Dave Vieglais T799,O=Google,C=US,DC=cilogon,DC=org