-
Notifications
You must be signed in to change notification settings - Fork 494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow changes to file metadata via API #6962 #6968
Changes from 6 commits
809643a
508e95c
9954220
e5ec9bc
491caff
a34d7c1
2debed2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import static javax.ws.rs.core.Response.Status.CREATED; | ||
import static javax.ws.rs.core.Response.Status.OK; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.nullValue; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
|
@@ -263,4 +264,233 @@ public void moveFileToDirectoryContainingSameFileName() throws IOException { | |
|
||
} | ||
|
||
/** | ||
* In this test we make sure that other changes in the absence of label and | ||
* directoryLabel go through, such as changing a file description. | ||
*/ | ||
@Test | ||
public void modifyFileDescription() throws IOException { | ||
|
||
Response createUser = UtilIT.createRandomUser(); | ||
createUser.prettyPrint(); | ||
createUser.then().assertThat() | ||
.statusCode(OK.getStatusCode()); | ||
String username = UtilIT.getUsernameFromResponse(createUser); | ||
String apiToken = UtilIT.getApiTokenFromResponse(createUser); | ||
|
||
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken); | ||
createDataverseResponse.prettyPrint(); | ||
createDataverseResponse.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); | ||
|
||
Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apiToken); | ||
createDataset.prettyPrint(); | ||
createDataset.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset); | ||
|
||
Path pathtoReadme1 = Paths.get(Files.createTempDirectory(null) + File.separator + "README.md"); | ||
Files.write(pathtoReadme1, "File 1".getBytes()); | ||
System.out.println("README: " + pathtoReadme1); | ||
|
||
Response uploadReadme1 = UtilIT.uploadFileViaNative(datasetId.toString(), pathtoReadme1.toString(), apiToken); | ||
uploadReadme1.prettyPrint(); | ||
uploadReadme1.then().assertThat() | ||
.statusCode(OK.getStatusCode()) | ||
.body("data.files[0].label", equalTo("README.md")); | ||
|
||
Integer idOfReadme1 = JsonPath.from(uploadReadme1.getBody().asString()).getInt("data.files[0].dataFile.id"); | ||
System.out.println("id: " + idOfReadme1); | ||
|
||
JsonObjectBuilder updateFileMetadata = Json.createObjectBuilder() | ||
.add("description", "This file is awesome."); | ||
Response updateFileMetadataResponse = UtilIT.updateFileMetadata(String.valueOf(idOfReadme1), updateFileMetadata.build().toString(), apiToken); | ||
updateFileMetadataResponse.prettyPrint(); | ||
updateFileMetadataResponse.then().statusCode(OK.getStatusCode()); | ||
|
||
} | ||
|
||
/** | ||
* In this test we make sure that that if you keep the label the same, you | ||
* are still able to change other metadata such as file description. | ||
*/ | ||
@Test | ||
public void modifyFileDescriptionSameLabel() throws IOException { | ||
|
||
Response createUser = UtilIT.createRandomUser(); | ||
createUser.prettyPrint(); | ||
createUser.then().assertThat() | ||
.statusCode(OK.getStatusCode()); | ||
String username = UtilIT.getUsernameFromResponse(createUser); | ||
String apiToken = UtilIT.getApiTokenFromResponse(createUser); | ||
|
||
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken); | ||
createDataverseResponse.prettyPrint(); | ||
createDataverseResponse.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); | ||
|
||
Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apiToken); | ||
createDataset.prettyPrint(); | ||
createDataset.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset); | ||
|
||
Path pathtoReadme1 = Paths.get(Files.createTempDirectory(null) + File.separator + "README.md"); | ||
Files.write(pathtoReadme1, "File 1".getBytes()); | ||
System.out.println("README: " + pathtoReadme1); | ||
|
||
JsonObjectBuilder json1 = Json.createObjectBuilder() | ||
.add("directoryLabel", "code"); | ||
|
||
Response uploadReadme1 = UtilIT.uploadFileViaNative(datasetId.toString(), pathtoReadme1.toString(), json1.build(), apiToken); | ||
uploadReadme1.prettyPrint(); | ||
uploadReadme1.then().assertThat() | ||
.statusCode(OK.getStatusCode()) | ||
.body("data.files[0].label", equalTo("README.md")); | ||
|
||
Integer idOfReadme1 = JsonPath.from(uploadReadme1.getBody().asString()).getInt("data.files[0].dataFile.id"); | ||
System.out.println("id: " + idOfReadme1); | ||
|
||
JsonObjectBuilder updateFileMetadata = Json.createObjectBuilder() | ||
.add("label", "README.md") | ||
.add("directoryLabel", "code") | ||
.add("description", "This file is awesome."); | ||
Response updateFileMetadataResponse = UtilIT.updateFileMetadata(String.valueOf(idOfReadme1), updateFileMetadata.build().toString(), apiToken); | ||
updateFileMetadataResponse.prettyPrint(); | ||
updateFileMetadataResponse.then().statusCode(OK.getStatusCode()); | ||
|
||
} | ||
|
||
/** | ||
* What if the directory for the file exists and you pass the filename | ||
* (label) while changing the description? This should be allowed. | ||
*/ | ||
@Test | ||
public void existingDirectoryPassLabelChangeDescription() throws IOException { | ||
|
||
Response createUser = UtilIT.createRandomUser(); | ||
createUser.prettyPrint(); | ||
createUser.then().assertThat() | ||
.statusCode(OK.getStatusCode()); | ||
String username = UtilIT.getUsernameFromResponse(createUser); | ||
String apiToken = UtilIT.getApiTokenFromResponse(createUser); | ||
|
||
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken); | ||
createDataverseResponse.prettyPrint(); | ||
createDataverseResponse.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); | ||
|
||
Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apiToken); | ||
createDataset.prettyPrint(); | ||
createDataset.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset); | ||
|
||
Path pathToFile = Paths.get(Files.createTempDirectory(null) + File.separator + "label"); | ||
Files.write(pathToFile, "File 1".getBytes()); | ||
System.out.println("file: " + pathToFile); | ||
|
||
JsonObjectBuilder json1 = Json.createObjectBuilder() | ||
.add("directory", "code"); | ||
|
||
Response uploadFile = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile.toString(), json1.build(), apiToken); | ||
uploadFile.prettyPrint(); | ||
uploadFile.then().assertThat() | ||
.statusCode(OK.getStatusCode()) | ||
.body("data.files[0].label", equalTo("label")); | ||
|
||
Integer idOfFile = JsonPath.from(uploadFile.getBody().asString()).getInt("data.files[0].dataFile.id"); | ||
System.out.println("id: " + idOfFile); | ||
|
||
JsonObjectBuilder updateFileMetadata = Json.createObjectBuilder() | ||
.add("label", "label") | ||
.add("description", "This file is awesome."); | ||
Response updateFileMetadataResponse = UtilIT.updateFileMetadata(String.valueOf(idOfFile), updateFileMetadata.build().toString(), apiToken); | ||
updateFileMetadataResponse.prettyPrint(); | ||
updateFileMetadataResponse.then().statusCode(OK.getStatusCode()); | ||
|
||
} | ||
|
||
/** | ||
* This test is for the following scenario. | ||
* | ||
* What if the database has null for the directoryLabel? What if you pass in | ||
* directory as “” (because you don’t realize you can just not pass it). | ||
* when it check and compares, the old directory is null. so will that mean | ||
* labelChange = true and it will fail even though you didn’t really change | ||
* the directory? | ||
* | ||
* While "labelChange" does end up being true, | ||
* IngestUtil.conflictsWithExistingFilenames returns false, so the change is | ||
* allowed to go through. The description is allowed to be changed and the | ||
* directoryLabel remains null even though the user passed in an empty | ||
* string, which is what we want. | ||
Comment on lines
+432
to
+436
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue here is that if it gets to the conflictswithexisting stage and that returns false, I think we have a problem: if conflictswithexisting filenames returns false, it’s because it’s treating ‘’ and null differently, since the name is the same). So if you did the same thing, but actually changed the name of the file to a different file name that does exist? Wouldn’t conflictswithexisting still return false then and allow you to rename to an already existing file. The use case is you have two files: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add, one solution could be to not store null for directoryLabel ever. We could either store "" or even "/". This would also help in adding a unique constraint, since unique constraints are problematic with nulls. |
||
*/ | ||
@Test | ||
public void existingDirectoryNullPassEmptyStringChangeDescription() throws IOException { | ||
|
||
Response createUser = UtilIT.createRandomUser(); | ||
createUser.prettyPrint(); | ||
createUser.then().assertThat() | ||
.statusCode(OK.getStatusCode()); | ||
String username = UtilIT.getUsernameFromResponse(createUser); | ||
String apiToken = UtilIT.getApiTokenFromResponse(createUser); | ||
|
||
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken); | ||
createDataverseResponse.prettyPrint(); | ||
createDataverseResponse.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); | ||
|
||
Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apiToken); | ||
createDataset.prettyPrint(); | ||
createDataset.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset); | ||
|
||
Path pathToFile = Paths.get(Files.createTempDirectory(null) + File.separator + "file1.txt"); | ||
Files.write(pathToFile, "File 1".getBytes()); | ||
System.out.println("file: " + pathToFile); | ||
|
||
JsonObjectBuilder json1 = Json.createObjectBuilder() | ||
.add("description", "This is my file."); | ||
|
||
Response uploadFile = UtilIT.uploadFileViaNative(datasetId.toString(), pathToFile.toString(), json1.build(), apiToken); | ||
uploadFile.prettyPrint(); | ||
uploadFile.then().assertThat() | ||
.statusCode(OK.getStatusCode()) | ||
.body("data.files[0].label", equalTo("file1.txt")); | ||
|
||
Integer idOfFile = JsonPath.from(uploadFile.getBody().asString()).getInt("data.files[0].dataFile.id"); | ||
System.out.println("id: " + idOfFile); | ||
|
||
JsonObjectBuilder updateFileMetadata = Json.createObjectBuilder() | ||
// It doesn't make sense to pass "" as a directoryLabel. | ||
.add("directoryLabel", "") | ||
.add("description", "This file is awesome."); | ||
Response updateFileMetadataResponse = UtilIT.updateFileMetadata(String.valueOf(idOfFile), updateFileMetadata.build().toString(), apiToken); | ||
updateFileMetadataResponse.prettyPrint(); | ||
updateFileMetadataResponse.then().statusCode(OK.getStatusCode()); | ||
|
||
Response datasetJson = UtilIT.nativeGet(datasetId, apiToken); | ||
datasetJson.prettyPrint(); | ||
datasetJson.then().assertThat() | ||
.statusCode(OK.getStatusCode()) | ||
.body("data.latestVersion.files[0].label", equalTo("file1.txt")) | ||
.body("data.latestVersion.files[0].description", equalTo("This file is awesome.")) | ||
// Even though we tried to set directoryValue to "" above, it's correctly null in the database. | ||
.body("data.latestVersion.files[0].directoryLabel", nullValue()); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is just for the error message, but why not just use "incomingPathPlusName" here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because if we do (I just tried), we get "null/" as part of the output. Tests fail with messages like this:
That is to say, that code (below) is there to make sure there's no "null/" in the output, like above.