Skip to content

Commit

Permalink
fix: #799 linkfiles must have names
Browse files Browse the repository at this point in the history
  • Loading branch information
marikaris committed Oct 10, 2024
1 parent 5180587 commit ddf8972
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.molgenis.armadillo.exceptions;

import static java.lang.String.format;
import static org.springframework.http.HttpStatus.BAD_REQUEST;

import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(BAD_REQUEST)
public class InvalidObjectNameException extends RuntimeException {

public InvalidObjectNameException(String objectName) {
super(
format(
"Object name '%s' is invalid. Object format should be in the following format: folder/file.",
objectName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public ArmadilloLinkFile(
String variables,
String linkObject,
String project) {
if (!isValidLinkObject(linkObject)) {
throw new IllegalArgumentException(format("Invalid link object: %s", linkObject));
}
this.linkObject = linkObject;
this.sourceProject = sourceProject;
this.sourceObject = sourceObject;
Expand Down Expand Up @@ -70,6 +73,22 @@ public ArmadilloLinkFile(InputStream armadilloLinkStream, String linkProject, St
}
}

static Boolean isValidLinkObject(String linkObject) {
if (linkObject.endsWith("/")) {
return false;
}
int count = 0;
for (int i = 0; i < linkObject.length(); i++) {
if (linkObject.charAt(i) == '/') {
count++;
if (count > 1) {
return false;
}
}
}
return count == 1;
}

public String getSourceProject() {
return this.sourceProject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void createLinkedObject(
String linkName,
String linkProject,
String variables)
throws IOException {
throws IOException, StorageException {
throwIfUnknown(sourceProject, sourceObject + PARQUET);
throwIfUnknown(linkProject);
throwIfDuplicate(linkProject, linkName + LINK_FILE);
Expand All @@ -102,11 +102,15 @@ public void createLinkedObject(
throw new UnknownVariableException(
sourceProject, sourceObject, unavailableVariables.toString());
}
ArmadilloLinkFile armadilloLinkFile =
createLinkFileFromSource(sourceProject, sourceObject, variables, linkName, linkProject);
InputStream is = armadilloLinkFile.toStream();
storageService.save(
is, SHARED_PREFIX + linkProject, armadilloLinkFile.getFileName(), APPLICATION_JSON);
try {
ArmadilloLinkFile armadilloLinkFile =
createLinkFileFromSource(sourceProject, sourceObject, variables, linkName, linkProject);
InputStream is = armadilloLinkFile.toStream();
storageService.save(
is, SHARED_PREFIX + linkProject, armadilloLinkFile.getFileName(), APPLICATION_JSON);
} catch (IllegalArgumentException e) {
throw new InvalidObjectNameException(linkName);
}
}

public ArmadilloLinkFile createArmadilloLinkFileFromStream(
Expand Down Expand Up @@ -223,17 +227,18 @@ private static Workspace toWorkspace(ObjectMetadata item) {
.build();
}

private void trySaveWorkspace (ArmadilloWorkspace workspace, Principal principal, String id) {
private void trySaveWorkspace(ArmadilloWorkspace workspace, Principal principal, String id) {
try {
storageService.save(
workspace.createInputStream(),
getUserBucketName(principal),
getWorkspaceObjectName(id),
APPLICATION_OCTET_STREAM);
workspace.createInputStream(),
getUserBucketName(principal),
getWorkspaceObjectName(id),
APPLICATION_OCTET_STREAM);
} catch (StorageException e) {
throw new StorageException(e);
}
}

public void saveWorkspace(InputStream is, Principal principal, String id) {
// Load root dir
File drive = new File("/");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.molgenis.armadillo.storage;

import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import static org.molgenis.armadillo.storage.ArmadilloLinkFile.isValidLinkObject;

import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
Expand Down Expand Up @@ -102,6 +103,30 @@ public void testLoadLinkFileFromStreamInvalidProject() {
}
}

@Test
public void testIsValidLinkObjectTooManySlashes() {
String tooManySlashes = "/this/is/too/many";
assertFalse(isValidLinkObject(tooManySlashes));
}

@Test
public void testIsValidLinkObjectEndsWithSlash() {
String endsWithSlash = "endswith/";
assertFalse(isValidLinkObject(endsWithSlash));
}

@Test
public void testIsValidLinkObjectNotEnoughSlashes() {
String noSlash = "no slash";
assertFalse(isValidLinkObject(noSlash));
}

@Test
public void testIsValidLinkObjectSucceeds() {
String object = "folder/file";
assertTrue(isValidLinkObject(object));
}

@Test
public void testLoadLinkFileFromStreamInvalidVariables() {
String testData =
Expand Down

0 comments on commit ddf8972

Please sign in to comment.