Skip to content

Commit

Permalink
Test case per issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
martin authored and martin committed Mar 10, 2020
1 parent 0846d59 commit 308778c
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public Boolean delete(String id) {
@Override
public StorageObject getObject(String key) {
try {
key = key.replaceAll("//", "/");
Path objectPath = absolutizePath(Paths.get(key));
if (Files.exists(objectPath)) {
Map<String, Object> metadata = getMetadata(Paths.get(key));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package it.cnr.si.spring.storage;

import static it.cnr.si.spring.storage.FilesystemTestUtils.PIPPO;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import it.cnr.si.spring.storage.config.StoragePropertyNames;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:storage-filesystem-test-context.xml")
@TestPropertySource("classpath:META-INF/spring/filesystem.properties")
public class CreateAndDeleteTest {

private final Logger log = LoggerFactory.getLogger(CreateAndDeleteTest.class);

@Autowired
private StoreService storeService;
@Autowired
private FilesystemTestUtils filesystemTestUtils;

@Value("${cnr.storage.filesystem.directory}")
private String baseDir;

@Test
public void testCreateAndReadSimpleDocumentAndMetadata() {
InputStream is = new ByteArrayInputStream(PIPPO.getBytes());
String contentType = "text/plain";
String path = "";
Map<String, Object> metadata = new HashMap<>();
StorageObject so = storeService.storeSimpleDocument(is, contentType, path, metadata);

StorageObject tre = storeService.getStorageObjectBykey(so.getKey());
StorageObject quattro = storeService.getStorageObjectByPath(so.getPath());

log.info("{}", so);
log.info("{}", tre);
log.info("{}", quattro);

assertEquals(so.toString(), tre.toString());
assertEquals(so.toString(), quattro.toString());
assertEquals(so.getPath(), tre.getPath());
assertEquals(so.getKey(), tre.getKey());

InputStream content = storeService.getResource(so.getKey());
so = storeService.getStorageObjectBykey(so.getKey());
BigInteger length= so.<BigInteger>getPropertyValue(StoragePropertyNames.CONTENT_STREAM_LENGTH.value());
String ctype = so.<String>getPropertyValue(StoragePropertyNames.CONTENT_STREAM_MIME_TYPE.value());
assertNotEquals(length,0);
assertNotNull(ctype);

String contentString = new Scanner(content).next();

log.info("{}", contentString);

assertEquals(PIPPO, contentString);

}


@Test
public void testDeleteFile() {

StorageObject so = filesystemTestUtils.createFile();

so = storeService.getStorageObjectBykey(so.getKey());
assertTrue( storeService.delete(so.getKey()) );
assertNull( storeService.getStorageObjectBykey(so.getKey()) );

assertFalse(Files.exists( Paths.get(baseDir, so.getPath()) ));
assertFalse(Files.exists( Paths.get(baseDir, so.getPath() + ".properties") ));

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.cnr.si.spring.storage;

import it.cnr.si.spring.storage.bulk.StorageFile;

import it.cnr.si.spring.storage.config.StoragePropertyNames;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -22,6 +23,7 @@
import static it.cnr.si.spring.storage.config.StoragePropertyNames.DESCRIPTION;
import static it.cnr.si.spring.storage.config.StoragePropertyNames.TITLE;
import static org.junit.Assert.*;
import static it.cnr.si.spring.storage.FilesystemTestUtils.*;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:storage-filesystem-test-context.xml")
Expand All @@ -34,9 +36,8 @@ public class FilesystemStorageTest {
private StoreService storeService;
@Autowired
private StorageDriver storageDriver;

private static final String PIPPO = "pippo",
PLUTO = "pluto";
@Autowired
private FilesystemTestUtils filesystemTestUtils;

@Test
public void testCorrectStorType() {
Expand Down Expand Up @@ -117,7 +118,7 @@ public void testCreateAndReadSimpleDocumentAndMetadata() {
@Test
public void testRestoreSimpleDocument() {

StorageObject so = createFile();
StorageObject so = filesystemTestUtils.createFile();

log.info("{}", so);
StorageObject quattro = storeService.getStorageObjectByPath(so.getPath());
Expand All @@ -138,7 +139,7 @@ public void testRestoreSimpleDocument() {
@Test
public void testUpdateProperties() {

StorageObject so = createFile();
StorageObject so = filesystemTestUtils.createFile();

so = storeService.getStorageObjectBykey(so.getKey());

Expand All @@ -157,7 +158,7 @@ public void testUpdateProperties() {
@Test
public void testUpdateStream() {

StorageObject so = createFile();
StorageObject so = filesystemTestUtils.createFile();

so = storeService.getStorageObjectBykey(so.getKey());
InputStream content = storeService.getResource(so.getKey());
Expand Down Expand Up @@ -185,7 +186,7 @@ public void testUpdateStream() {
@Test
public void testDeleteFile() {

StorageObject so = createFile();
StorageObject so = filesystemTestUtils.createFile();

so = storeService.getStorageObjectBykey(so.getKey());
assertTrue( storeService.delete(so.getKey()) );
Expand Down Expand Up @@ -232,20 +233,6 @@ public void testDeleteNonEmptyDirectory() {

}

private StorageObject createFile() {
InputStream is = new ByteArrayInputStream(PIPPO.getBytes());

StorageFile file = new StorageFile(is,
"text/plain",
"Titolo");

return storeService.restoreSimpleDocument(
file,
new ByteArrayInputStream(file.getBytes()),
"text/plain",
"Titolo",
"/",
true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package it.cnr.si.spring.storage;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import it.cnr.si.spring.storage.bulk.StorageFile;

@Component
public class FilesystemTestUtils {

/*friendly*/ static final String PIPPO = "pippo",
PLUTO = "pluto";

@Autowired
private StoreService storeService;

/*friendly*/ StorageObject createFile() {
InputStream is = new ByteArrayInputStream(PIPPO.getBytes());

StorageFile file = new StorageFile(is,
"text/plain",
"Titolo");

return storeService.restoreSimpleDocument(
file,
new ByteArrayInputStream(file.getBytes()),
"text/plain",
"Titolo",
"/",
true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static it.cnr.si.spring.storage.FilesystemTestUtils.*;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:storage-filesystem-test-context.xml")
Expand All @@ -26,14 +27,9 @@ public class GetChildrenTest {

@Autowired
private StoreService storeService;
@Autowired
private StorageDriver storageDriver;

private StorageObject savedFile;

private static final String PIPPO = "pippo",
PLUTO = "pluto";


@Before
public void setUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ public class SecondaryObjectTypeIdsTest {
public static final String ASPECT_1 = "ASPECT 1";
public static final String ASPECT_2 = "ASPECT 2";
private static final String PIPPO = "pippo";
private final Logger log = LoggerFactory.getLogger(FilesystemStorageTest.class);
private final Logger log = LoggerFactory.getLogger(SecondaryObjectTypeIdsTest.class);

@Autowired
private StoreService storeService;
@Autowired
private StorageDriver storageDriver;
private StorageObject savedFile;

@Before
Expand Down

0 comments on commit 308778c

Please sign in to comment.