Commit d3a2a787 authored by Matija Obreza's avatar Matija Obreza

Metadata for .json files is stored in .json.json

parent 7cde148e
Pipeline #13083 passed with stage
in 1 minute and 6 seconds
......@@ -245,7 +245,11 @@ public class RepositoryFile extends AuditedVersionedModelWithoutId implements Ac
return null;
}
return new StringBuffer().append(uuid.toString()).append(".json").toString();
StringBuffer sb = new StringBuffer().append(uuid.toString());
if (StringUtils.equals(".json", extension)) {
sb.append(extension);
}
return sb.append(".json").toString();
}
/**
......
......@@ -26,6 +26,7 @@ import java.nio.file.Paths;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryImage;
import org.junit.After;
import org.junit.Test;
......@@ -109,4 +110,37 @@ public class MetadataTest extends RepositoryServiceTest {
metadata = bytesStorageService.get(repoImage1.storageFolder().resolve(repoImage1.getMetadataFilename()));
assertThat(metadata, is(nullValue()));
}
/**
* Test handling of metadata .json and assure it doesn't overwrite actual file contents.
*
* @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 createMetadataForJson() throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException, NoSuchRepositoryFileException {
final String json = "{}";
RepositoryFile repoJson = repositoryService.addFile(initialPath, "file.json", "application/json", json.getBytes(), null);
assertThat(repoJson.getUuid(), not(nullValue()));
assertThat(repoJson.getMetadataFilename(), endsWith(".json.json"));
byte[] contents = bytesStorageService.get(repoJson.storagePath());
assertThat(contents, not(nullValue()));
assertThat("JSON contents is messed up", new String(contents), equalTo(json));
byte[] metadata = bytesStorageService.get(repoJson.storageFolder().resolve(repoJson.getMetadataFilename()));
assertThat("Metadata .json not found", metadata, not(nullValue()));
metadata = bytesStorageService.get(repoJson.storageFolder().resolve(repoJson.getMetadataFilename()));
RepositoryFile meta = objectMapper.readValue(metadata, RepositoryFile.class);
assertThat("Metadata does not contain valid JSON metadata", meta.getOriginalFilename(), is(repoJson.getOriginalFilename()));
repositoryService.removeFile(repoJson);
metadata = bytesStorageService.get(repoJson.storageFolder().resolve(repoJson.getMetadataFilename()));
assertThat(metadata, is(nullValue()));
}
}
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