From a56aa48b2fbf5cb82b8b03673065dcb3dcefaa28 Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Wed, 25 Jan 2023 14:40:33 +0100
Subject: [PATCH] - added some tests

---
 .../storage/nfs/TestSLUBStoragePlugin.java    | 106 +++++++++++++++++-
 1 file changed, 100 insertions(+), 6 deletions(-)

diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/TestSLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/TestSLUBStoragePlugin.java
index 538a299..1b1c16c 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/TestSLUBStoragePlugin.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/TestSLUBStoragePlugin.java
@@ -1,13 +1,29 @@
 package org.slub.rosetta.dps.repository.plugin.storage.nfs;
 
+import com.exlibris.core.sdk.storage.containers.StoredEntityMetaData;
+import com.exlibris.digitool.common.dnx.DnxDocument;
+import com.exlibris.digitool.common.dnx.DnxDocumentFactory;
+import com.exlibris.digitool.common.storage.Fixity;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 /**
  * Tests for {@link org.slub.rosetta.dps.repository.plugin.storage.nfs.SLUBStoragePlugin}.
@@ -15,22 +31,98 @@ import static org.junit.Assert.assertEquals;
  * @author andreas.romeyke@slub-dresden.de (Andreas Romeyke)
  */
 @RunWith(JUnit4.class)
+
 public class TestSLUBStoragePlugin {
-  @Test
-  public void getStreamDirectory() {
-    SLUBStoragePlugin mock = new SLUBStoragePlugin();
+  private static SLUBStoragePlugin mock;
+  private static Path testroot;
+  @Before
+  public void setUp() throws IOException {
+    testroot = Files.createTempDirectory("testslubstorage");
+    Files.createFile(Path.of(testroot.toAbsolutePath().toString() + "/foo"));
+    mock = new SLUBStoragePlugin();
     HashMap<String, String> map = new HashMap<String,String> ();
-    map.put("DIR_ROOT", "./");
+    map.put("DIR_ROOT", testroot.toAbsolutePath().toString());
     map.put("BLOCK_SIZE", "8192");
     map.put("FILES_HANDLING_METHOD", "move");
     mock.setParameters( map );
     mock.init(map);
-    // TODO:
+  }
+//  @Test
+//  public  void getDirRoot() {
+    //SLUBStoragePlugin mock = new SLUBStoragePlugin();
+    //assertEquals(mock.getDirRoot());
+//  }
+
+  @Test
+  public void getStreamDirectory() {
     File calced_path = mock.getStreamDirectory("foo", "bar");
-    //System.out.println("getStreamDirectory:"+ calced_path.getName()+ "\n");
     assertEquals("getStreamDirectory test", "bar", calced_path.getName());
   }
+  @Test
+  public void checkFixity() {
+    List<Fixity> fixities = new ArrayList<>();
+    try {
+      var result = mock.checkFixity(fixities, "/tmp/foo");
+      assertTrue("checkFixity", result);
+    }
+    catch (Exception e) {
+      assertEquals("checkFixity", "foo", e);
+    }
+  }
 
+  @Test
+  public void getFullFilePath() {
+    assertEquals( "getFullFilePath", testroot.toAbsolutePath().toString()+"/2022", mock.getFullFilePath("/2022"));
+  }
+  @Test
+  public void retrieveEntity() {
+    try {
+      InputStream is = mock.retrieveEntity("/foo");
+      assertTrue("retrieveEntity", true);
+    } catch (Exception e) {
+      assertEquals("retrieveEntity", "", e);
+    }
+  }
+  @Test
+  public void retrieveEntitybyRange() {
+    try {
+      byte[] bytes = mock.retrieveEntityByRange("/foo", 0, 0);
+      assertTrue("retrieveEntitybyRange", true);
+    } catch (java.io.EOFException e) {
+      assertEquals("retrieveEntitybyRange", null, e.getMessage());
+    } catch (Exception e) {
+      assertEquals("retrieveEntitybyRange", "", e.getMessage());
+    }
+  }
+
+  @Test
+  public void storeEntity() throws IOException {
+    byte[] bytes = "teststring".getBytes(StandardCharsets.UTF_8);
+    InputStream is = new ByteArrayInputStream(bytes);
+    StoredEntityMetaData sem = new StoredEntityMetaData();
+    sem.setCurrentFilePath("/bar");
+    sem.setIePid(null);
+    File file = new File("resources/iemets.xml");
+    InputStream ioStream = new FileInputStream(file);
+    InputStreamReader isr = new InputStreamReader(ioStream);
+    char[] charArray = new char[(int)file.length()];
+    isr.read(charArray);
+    String iemets = new String(charArray);
+    try {
+      DnxDocument dnx = DnxDocumentFactory.getInstance().createDnxDocument(false);
+      dnx.setDocumentXml(iemets);
+      sem.setIeDnx(dnx);
+      sem.setEntityType(StoredEntityMetaData.EntityType.IE);
+      mock.storeEntity(is, sem);
+    }
+    catch (NoClassDefFoundError e) {
+      /* FIXME: Test irgendwie den org/springframework/context/ApplicationContext verpassen... */
+      assertEquals("storeEntity", "org/springframework/context/ApplicationContext", e.getMessage());
+    }
+    catch (Exception e) {
+      assertEquals("storeEntity", "", e.getMessage());
+    }
+  }
   /*
   @Test
   public void getStreamRelativePath() throws Exception {
@@ -59,4 +151,6 @@ public class TestSLUBStoragePlugin {
     mock.getStreamRelativePath( storedEntityMetaData);
   }
   */
+
+
 }
-- 
GitLab