Commit 9bb00fd9 authored by Matija Obreza's avatar Matija Obreza

Cleanup and tests after upgrade to using Path

parent b37e4523
Pipeline #6562 passed with stage
in 2 minutes and 58 seconds
......@@ -51,6 +51,7 @@ import org.hibernate.annotations.Type;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
......@@ -83,6 +84,7 @@ public class RepositoryFile extends AuditedVersionedModelWithoutId implements Ac
/** Path in the repository. */
@Column(length = 250)
@JsonIgnore
private String path;
/**
......@@ -187,7 +189,7 @@ public class RepositoryFile extends AuditedVersionedModelWithoutId implements Ac
if (uuid == null) {
uuid = UUID.randomUUID();
}
trimStringsToNull();
}
......@@ -210,16 +212,6 @@ public class RepositoryFile extends AuditedVersionedModelWithoutId implements Ac
return uuid == null ? null : "urn:uuid:" + uuid.toString();
}
/**
* Get the relative URL to the resource. As such, it cannot serve as an
* {@link #getIdentifier()} property.
*
* @return relative URL to the file resource
*/
public String getUrl() {
return getStorageFullPath().toString();
}
/**
* Gets the filename as used by {@link BytesStorageService}.
*
......@@ -258,21 +250,38 @@ public class RepositoryFile extends AuditedVersionedModelWithoutId implements Ac
*
* @return the storage path
*/
public Path getStoragePath() {
public String getStorageFolder() {
if (uuid == null) {
return null;
}
return Paths.get("/", uuid.toString().substring(0, 3));
return "/" + uuid.toString().substring(0, 3);
}
/**
* Get the full path to the file as used by {@link BytesStorageService}. This is
* the concatenation of {@link #getStoragePath()} and {@link #getFilename()}.
* the concatenation of {@link #getStorageFolder()} and {@link #getFilename()}.
*
* @return the storage full path
*/
public Path getStorageFullPath() {
return getStoragePath().resolve(getFilename());
public String getStoragePath() {
if (uuid == null) {
return null;
}
return getStorageFolder() + "/" + getFilename();
}
public Path storagePath() {
if (uuid == null) {
return null;
}
return Paths.get(getStorageFolder(), getFilename());
}
public Path storageFolder() {
if (uuid == null) {
return null;
}
return Paths.get(getStorageFolder());
}
/*
......@@ -715,7 +724,7 @@ public class RepositoryFile extends AuditedVersionedModelWithoutId implements Ac
}
this.active = source.active;
this.folder = source.folder;
// this.folder = source.folder;
this.accessRights = source.accessRights;
this.bibliographicCitation = source.bibliographicCitation;
this.contentType = source.contentType;
......
......@@ -130,28 +130,15 @@ public class RepositoryImage extends RepositoryFile implements ImageMetadata {
orientation = width > height ? Orientation.LANDSCAPE : Orientation.PORTRAIT;
}
/**
* Apply.
*
* @param source the source
* @return the repository image
*/
public RepositoryImage apply(final RepositoryImage source) {
super.apply(source);
this.height = source.height;
this.orientation = source.orientation;
this.width = source.width;
return this;
}
/**
* Get the path where thumbnails of this image are stored.
*
* @return Thumbnails path
*/
public String getThumbnailPath() {
return getStoragePath() + "/" + getUuid();
if (getUuid() == null) {
return null;
}
return getStorageFolder() + "/" + getUuid();
}
}
......@@ -18,10 +18,7 @@ package org.genesys.filerepository.service.aspect;
import java.io.IOException;
import java.nio.charset.Charset;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.nio.file.Paths;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
......@@ -32,6 +29,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
// TODO: Auto-generated Javadoc
/**
* Make sure that repository metadata is persisted as .json next to the data
......@@ -169,7 +170,7 @@ public class MetadataInStorageAspect {
final String metadataJson = objectWriter.writeValueAsString(repositoryFile);
try {
bytesStorageService.upsert(repositoryFile.getStoragePath().resolve(repositoryFile.getMetadataFilename()), metadataJson.getBytes(UTF8));
bytesStorageService.upsert(repositoryFile.storageFolder().resolve(repositoryFile.getMetadataFilename()), metadataJson.getBytes(UTF8));
} catch (final IOException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to upload metadata bytes", e);
......@@ -196,7 +197,7 @@ public class MetadataInStorageAspect {
LOG.trace("File was deleted path={} originalFilename={}. Removing metadata.json", repositoryFile.getFolder(), repositoryFile.getOriginalFilename());
try {
bytesStorageService.remove(repositoryFile.getStoragePath().resolve(repositoryFile.getMetadataFilename()));
bytesStorageService.remove(repositoryFile.storageFolder().resolve(repositoryFile.getMetadataFilename()));
} catch (final IOException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to delete metadata bytes", e);
......
......@@ -256,7 +256,7 @@ public class ImageGalleryServiceImpl implements ImageGalleryService {
LOG.debug("Generating new thumbnail width={} height={} for image={}", width, height, repositoryImage.getUuid());
}
final byte[] bytesPng = thumbnailGenerator.createThumbnail(width, height, bytesStorageService.get(repositoryImage.getStorageFullPath()));
final byte[] bytesPng = thumbnailGenerator.createThumbnail(width, height, bytesStorageService.get(repositoryImage.storagePath()));
if (LOG.isDebugEnabled()) {
LOG.debug("Persisting new thumbnail width={} height={} for image={}", width, height, repositoryImage.getUuid());
......
......@@ -175,7 +175,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
repositoryFile = repositoryFilePersistence.save(repositoryFile);
try {
bytesStorageService.upsert(repositoryFile.getStorageFullPath(), bytes);
bytesStorageService.upsert(repositoryFile.storagePath(), bytes);
} catch (final IOException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to upload bytes", e);
......@@ -228,7 +228,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
repositoryImage = repositoryImagePersistence.save(repositoryImage);
try {
bytesStorageService.upsert(repositoryImage.getStorageFullPath(), bytes);
bytesStorageService.upsert(repositoryImage.storagePath(), bytes);
} catch (final IOException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to upload bytes", e);
......@@ -351,7 +351,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#repositoryFile, 'read')")
public byte[] getFileBytes(final RepositoryFile repositoryFile) throws IOException {
return bytesStorageService.get(repositoryFile.getStorageFullPath());
return bytesStorageService.get(repositoryFile.storagePath());
}
/*
......@@ -446,7 +446,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
LOG.debug("Applying metadata " + imageData);
repositoryImage.apply(imageData);
LOG.debug("Image title: " + repositoryImage.getTitle());
return repositoryImagePersistence.save(repositoryImage);
return lazyLoad(repositoryImagePersistence.save(repositoryImage));
}
/*
......@@ -478,9 +478,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
LOG.debug("updateByes length={} repoFile.size={}", bytes.length, repositoryFile.getSize());
storedFile.setContentType(contentType);
bytesStorageService.upsert(storedFile.getStorageFullPath(), bytes);
bytesStorageService.upsert(storedFile.storagePath(), bytes);
return repositoryFilePersistence.save(storedFile);
return lazyLoad(repositoryFilePersistence.save(storedFile));
}
/*
......@@ -507,9 +507,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
fillImageProperties(storedFile, bytes);
bytesStorageService.upsert(storedFile.getStorageFullPath(), bytes);
bytesStorageService.upsert(storedFile.storagePath(), bytes);
return repositoryImagePersistence.save(storedFile);
return lazyLoad(repositoryImagePersistence.save(storedFile));
}
/**
......@@ -549,7 +549,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
return removeImage((RepositoryImage) repositoryFile);
}
bytesStorageService.remove(repositoryFile.getStorageFullPath());
bytesStorageService.remove(repositoryFile.storagePath());
repositoryFilePersistence.delete(repositoryFile);
return repositoryFile;
}
......@@ -568,7 +568,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
throw new NoSuchRepositoryFileException();
}
bytesStorageService.remove(repositoryImage.getStorageFullPath());
bytesStorageService.remove(repositoryImage.storagePath());
repositoryImagePersistence.delete(repositoryImage);
return repositoryImage;
}
......@@ -602,7 +602,7 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
file = repositoryFilePersistence.save(repositoryFile);
}
return file;
return lazyLoad(file);
}
/*
......@@ -632,9 +632,9 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
repositoryFile.setOriginalFilename(path.getFileName().toString());
if (repositoryFile instanceof RepositoryImage) {
return repositoryImagePersistence.save((RepositoryImage) repositoryFile);
return lazyLoad(repositoryImagePersistence.save((RepositoryImage) repositoryFile));
} else {
return repositoryFilePersistence.save(repositoryFile);
return lazyLoad(repositoryFilePersistence.save(repositoryFile));
}
}
......
......@@ -54,7 +54,7 @@ public class FileRepositoryTestUtil {
assertThat("RepositoryFile#extension doesn't match", repoFile.getExtension(), equalTo(extension));
assertThat("RepositoryFile#contentType doesn't match", repoFile.getContentType(), equalTo(contentType));
assertThat("RepositoryFile#filename doesn't match", repoFile.getFilename(), equalTo(repoFile.getUuid().toString() + (extension != null ? extension : "")));
assertThat("RepositoryFile#URL doesn't match", repoFile.getUrl(), equalTo("/" + repoFile.getUuid().toString().substring(0, 3) + "/" + repoFile.getUuid().toString()
assertThat("RepositoryFile#URL doesn't match", repoFile.getStoragePath(), equalTo("/" + repoFile.getUuid().toString().substring(0, 3) + "/" + repoFile.getUuid().toString()
+ (extension != null ? extension : "")));
return repoFile;
......@@ -123,6 +123,7 @@ public class FileRepositoryTestUtil {
final int width, final int height, final Orientation orientation) {
checkFile(imageData, path, originalFilename, extension, contentType);
assertThat("RepositoryImage#thumbnailPath doesn't match", imageData.getThumbnailPath(), is(imageData.getStorageFolder() + "/" + imageData.getUuid()));
assertThat("RepositoryImage#width doesn't match", imageData.getWidth(), is(width));
assertThat("RepositoryImage#height doesn't match", imageData.getHeight(), is(height));
assertThat("RepositoryImage#orientation doesn't match", imageData.getOrientation(), is(orientation));
......
......@@ -86,7 +86,7 @@ public class MetadataTest extends RepositoryServiceTest {
RepositoryImage repoImage1 = repositoryService.addImage(initialPath, image1.getOriginalFilename(), image1.getContentType(), image1.getImageBytes(), null);
assertThat(repoImage1.getUuid(), not(nullValue()));
byte[] metadata = bytesStorageService.get(repoImage1.getStoragePath().resolve(repoImage1.getMetadataFilename()));
byte[] metadata = bytesStorageService.get(repoImage1.storageFolder().resolve(repoImage1.getMetadataFilename()));
assertThat("Metadata .json not found", metadata, not(nullValue()));
RepositoryImage meta = objectMapper.readValue(metadata, RepositoryImage.class);
......@@ -99,12 +99,12 @@ public class MetadataTest extends RepositoryServiceTest {
repoImage1.setOriginalFilename("test.png");
repoImage1 = repositoryService.updateMetadata(repoImage1);
metadata = bytesStorageService.get(repoImage1.getStoragePath().resolve(repoImage1.getMetadataFilename()));
metadata = bytesStorageService.get(repoImage1.storageFolder().resolve(repoImage1.getMetadataFilename()));
meta = objectMapper.readValue(metadata, RepositoryImage.class);
assertThat(meta.getOriginalFilename(), is(repoImage1.getOriginalFilename()));
repositoryService.removeFile(repoImage1);
metadata = bytesStorageService.get(repoImage1.getStoragePath().resolve(repoImage1.getMetadataFilename()));
metadata = bytesStorageService.get(repoImage1.storageFolder().resolve(repoImage1.getMetadataFilename()));
assertThat(metadata, is(nullValue()));
}
}
......@@ -133,6 +133,30 @@ public class RepositoryImageAddTest {
fileRepoService.removeFile(repoImage);
}
/**
* Test image size not modified.
*
* @throws IOException Signals that an I/O exception has occurred.
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@Test
public void testImageMetadataUpdate() throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException, NoSuchRepositoryFileException {
final byte[] image10x10 = FileRepositoryTestUtil.readImage("images/10x10.png");
final RepositoryImage repoImage = fileRepoService.addImage(initialPath, initialOriginalFilename, initialContentType, image10x10, null);
FileRepositoryTestUtil.checkImage(repoImage, initialPath, initialOriginalFilename, initialExtension, initialContentType, 7, 7, Orientation.PORTRAIT);
repoImage.setTitle("This is a test");
RepositoryImage repoImage2 = fileRepoService.updateImageMetadata(repoImage);
FileRepositoryTestUtil.checkImage(repoImage2, initialPath, initialOriginalFilename, initialExtension, initialContentType, 7, 7, Orientation.PORTRAIT);
assertThat(repoImage2.getTitle(), is("This is a test"));
fileRepoService.removeFile(repoImage2);
}
/**
* Test valid image data10x10.
*
......
......@@ -25,7 +25,7 @@ log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %t %5p %c{1}:%L - %m
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
log4j.category.org.genesys.filerepository=debug
log4j.category.org.genesys.filerepository=info
# Internal Hibernate logging is at ERROR
log4j.category.org.hibernate.engine.jdbc=fatal
......@@ -38,6 +38,8 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationConfig {
public static final int FTP_PORT = 8323;
/**
* Ftp server.
*
......@@ -46,7 +48,7 @@ public class ApplicationConfig {
@Bean
public RepositoryFtpServer ftpServer() {
final RepositoryFtpServer ftpServer = new RepositoryFtpServer();
ftpServer.setFtpPort(8021);
ftpServer.setFtpPort(FTP_PORT);
ftpServer.setUserManager(userManager());
ftpServer.setKeystorePath(getClass().getResource("/ftpserver.jks").getPath());
ftpServer.setKeystorePsw("genesys");
......
......@@ -58,6 +58,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(classes = { DatabaseConfig.class, ServiceBeanConfig.class, ApplicationConfig.class })
public class FtpServerTest {
private static final int FTP_PORT = ApplicationConfig.FTP_PORT;
private static final String TEST_FILE_CONTENTS = "This is a test";
private static final String TEST_FILE_CONTENTS_2 = "Some other text";
......@@ -129,7 +130,7 @@ public class FtpServerTest {
public void serverListening() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
} finally {
......@@ -147,7 +148,7 @@ public class FtpServerTest {
public void serverListeningFTPS() throws SocketException, IOException {
final FTPSClient ftp = (FTPSClient) createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
} finally {
......@@ -165,7 +166,7 @@ public class FtpServerTest {
public void userLoginFTPS() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.login(username, password);
......@@ -188,7 +189,7 @@ public class FtpServerTest {
public void mkdirTest() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.login(username, password);
......@@ -238,7 +239,7 @@ public class FtpServerTest {
public void rmdirTest() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.enterLocalPassiveMode();
......@@ -286,7 +287,7 @@ public class FtpServerTest {
public void listFiles() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.enterLocalPassiveMode();
......@@ -317,7 +318,7 @@ public class FtpServerTest {
public void simpleUploadTest() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.enterLocalPassiveMode();
......@@ -376,7 +377,7 @@ public class FtpServerTest {
public void folderListing() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.enterLocalPassiveMode();
......@@ -416,7 +417,7 @@ public class FtpServerTest {
public void simpleRewriteTest() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.enterLocalPassiveMode();
......@@ -474,7 +475,7 @@ public class FtpServerTest {
public void simpleAppendTest() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.enterLocalPassiveMode();
......@@ -533,7 +534,7 @@ public class FtpServerTest {
public void folderNavigationTest() throws SocketException, IOException {
final FTPClient ftp = createFtpClient();
try {
ftp.connect("localhost", 8021);
ftp.connect("localhost", FTP_PORT);
assertThat("FTP server refused connection", FTPReply.isPositiveCompletion(ftp.getReplyCode()), is(true));
ftp.login(username, password);
......
......@@ -25,6 +25,6 @@ log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %t %5p %c{1}:%L - %m
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=error, stdout
log4j.category.org.genesys.filerepository=trace
log4j.category.org.genesys.filerepository=info
log4j.category.org.apache.ftpserver=debug
log4j.category.org.apache.commons.net=info
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment