Commit 30ebb831 authored by Maxym Borodenko's avatar Maxym Borodenko

Merge branch 'genesys-532-divtree-json-file' into 'master'

Repository: updateBytes should also update metadata

See merge request !43
parents f6548d98 f6eec8d6
Pipeline #14990 passed with stage
in 1 minute and 38 seconds
......@@ -163,7 +163,7 @@ public interface RepositoryService {
<T extends RepositoryFile> T updateMetadata(T fileData) throws NoSuchRepositoryFileException;
/**
* Update file bytes.
* Update file bytes and file metadata
*
* @param <T> the generic type
* @param fileData the file data
......
......@@ -31,6 +31,7 @@ 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;
import com.fasterxml.jackson.databind.SerializationFeature;
// TODO: Auto-generated Javadoc
/**
......@@ -60,7 +61,7 @@ public class MetadataInStorageAspect {
{
LOG.warn("Instantiated " + getClass().getName());
objectWriter = new ObjectMapper().writerWithDefaultPrettyPrinter();
objectWriter = new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS).writerWithDefaultPrettyPrinter();
}
/**
......
......@@ -569,16 +569,19 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
return (T) updateImageBytes((RepositoryImage) repositoryFile, contentType, bytes);
}
// Update metadata
storedFile.apply(repositoryFile);
contentType = updateContentTypeIfNecessary(contentType, bytes);
storedFile.setContentType(contentType);
// Calculate SHA-1 and MD5 sums
LOG.debug("updateByes length={}", bytes.length);
LOG.debug("updateBytes length={}", bytes.length);
storedFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
storedFile.setMd5Sum(DigestUtils.md5Hex(bytes));
storedFile.setSize(bytes.length);
LOG.debug("updateByes length={} repoFile.size={}", bytes.length, repositoryFile.getSize());
LOG.debug("updateBytes length={} repoFile.size={}", bytes.length, repositoryFile.getSize());
storedFile.setContentType(contentType);
bytesStorageService.upsert(storedFile.storagePath(), bytes);
return lazyLoad(repositoryFilePersistence.save(storedFile));
......@@ -598,13 +601,18 @@ public class RepositoryServiceImpl implements RepositoryService, InitializingBea
if (storedFile == null) {
throw new NoSuchRepositoryFileException();
}
// Update metadata
storedFile.apply(repositoryImage);
contentType = updateContentTypeIfNecessary(contentType, bytes);
storedFile.setContentType(contentType);
// Calculate SHA-1 and MD5 sums
storedFile.setSha1Sum(DigestUtils.sha1Hex(bytes));
storedFile.setMd5Sum(DigestUtils.md5Hex(bytes));
storedFile.setSize(bytes.length);
storedFile.setContentType(contentType);
fillImageProperties(storedFile, bytes);
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2020 Global Crop Diversity Trust
*
* 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
* 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,
......@@ -16,9 +16,9 @@
package org.genesys.filerepository.service;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import java.io.IOException;
import java.nio.file.Path;
......@@ -31,10 +31,8 @@ import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;
// TODO: Auto-generated Javadoc
/**
* The Class FileRepositoryAddTest.
*/
......@@ -56,10 +54,6 @@ public class FileRepositoryAddTest extends RepositoryServiceTest {
/** The Constant DEFAULT_FILENAME. */
private static final String DEFAULT_FILENAME = "test" + DEFAULT_EXT;
/** The file repo service. */
@Autowired
private RepositoryService repositoryService;
/**
* Test basic submission to file repository.
*
......@@ -79,6 +73,32 @@ public class FileRepositoryAddTest extends RepositoryServiceTest {
FileRepositoryTestUtil.checkFile(repoFile, PATH, originalFilename, extension, contentType);
}
/**
* Adds the file and removes it
*
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@Test
public void addFileAndRemove() throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException, NoSuchRepositoryFileException {
final String contentType = "text/plain;charset=UTF-8";
final String extension = ".txt";
final String originalFilename = "requirements" + extension;
final RepositoryFile repoFile = repositoryService.addFile(PATH, originalFilename, contentType, SOME_BYTES, null);
assertThat(repoFile.getSize(), is(SOME_BYTES.length));
assertThat(fileRepository.count(), is(1l));
FileRepositoryTestUtil.checkFile(repoFile, PATH, originalFilename, extension, contentType);
repositoryService.removeFile(repoFile);
assertThat(fileRepository.count(), is(0l));
}
/**
* Test metadata submission to file repository.
......@@ -375,9 +395,43 @@ public class FileRepositoryAddTest extends RepositoryServiceTest {
final String sha1hex = DigestUtils.sha1Hex(randomBytes);
final String md5hex = DigestUtils.md5Hex(randomBytes);
assertThat("sha1 does must match", repositoryFile.getSha1Sum(), equalTo(sha1hex));
assertThat("md5 does must match", repositoryFile.getMd5Sum(), equalTo(md5hex));
assertThat("sha1 must match", repositoryFile.getSha1Sum(), equalTo(sha1hex));
assertThat("md5 must match", repositoryFile.getMd5Sum(), equalTo(md5hex));
repositoryService.removeFile(repositoryFile);
}
/**
* Test metadata update on update bytes.
*
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws NoSuchRepositoryFileException the no such repository file exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@Test
public void testMetadataUpdateOnUpdateBytes() throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, NoSuchRepositoryFileException, IOException {
final String contentType = "text/plain;charset=UTF-8";
RepositoryFile repositoryFile = repositoryService.addFile(PATH, DEFAULT_FILENAME, contentType, SOME_BYTES, null);
FileRepositoryTestUtil.checkFile(repositoryFile, PATH, DEFAULT_FILENAME, DEFAULT_EXT, contentType);
final byte[] randomBytes = new byte[100];
final Random rnd = new Random(System.currentTimeMillis());
rnd.nextBytes(randomBytes);
repositoryFile.setTitle("Updated title");
repositoryFile.setSubject("The subject");
RepositoryFile updatedFile = repositoryService.updateBytes(repositoryFile, contentType, randomBytes);
FileRepositoryTestUtil.checkFile(repositoryFile, PATH, DEFAULT_FILENAME, DEFAULT_EXT, contentType);
assertThat(updatedFile, not(repositoryFile));
assertThat("title must be updated", updatedFile.getTitle(), is(repositoryFile.getTitle()));
assertThat("subject must be updated", updatedFile.getSubject(), is(repositoryFile.getSubject()));
assertThat("md5 must be updated", updatedFile.getMd5Sum(), not(repositoryFile.getMd5Sum()));
assertThat("sha1 must be updated", updatedFile.getSha1Sum(), not(repositoryFile.getSha1Sum()));
repositoryService.removeFile(updatedFile);
}
}
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