Skip to content

Commit 22da346

Browse files
committed
This is a combination of 5 commits.
add overloaded method to add input stream fix tests Update src/main/java/com/researchspace/dataverse/entities/ObjectOrStringMessageDeserializer.java docs: javadocs Update src/main/java/com/researchspace/dataverse/http/DataverseOperationsImplV1.java remove obsolete comment
1 parent 865b981 commit 22da346

12 files changed

Lines changed: 135 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
Significant changes since 0.1.0
22

3+
1.1.0 In progress
4+
5+
- feature: Support upload of files to a dataset using native API. #16
6+
- feature: After creating a Dataset, the persistent ID is stored in the Identifier object. #22
7+
38
1.0.0 2022-11-21
49

510
Increasing major version due to major updates to dependencies. However, there are no
@@ -8,7 +13,7 @@ breaking API changes in this library.
813
- dependencies: Major dependency updates to Spring 5.3, Lombok 18.24.
914
- build: fix integration tests
1015
- build: enable integration test running through Github actions
11-
- build: test build and test on Java 8, 11, and 17
16+
- build: test build and test on Java 8, 11, and 17.
1217

1318
0.2.0 2022-11-20
1419

src/integration-test/java/com/researchspace/dataverse/http/DatasetOperationsTest.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
import org.junit.Ignore;
1212
import org.junit.Test;
1313

14+
import java.io.ByteArrayInputStream;
1415
import java.io.File;
1516
import java.io.IOException;
17+
import java.net.MalformedURLException;
1618
import java.net.URISyntaxException;
1719
import java.util.Arrays;
1820
import java.util.List;
@@ -63,17 +65,51 @@ public void testPostSampleDataset() throws IOException, InterruptedException, UR
6365
}
6466

6567
@Test
66-
public void uploadFileToDataSetWithNativeApi() throws IOException, URISyntaxException {
67-
DatasetFacade facade = createFacade();
68-
Identifier datasetId = dataverseOps.createDataset(facade, dataverseAlias);
68+
public void uploadFileToDataSetWithNativeApiBytes() throws IOException, URISyntaxException {
69+
//arrange
70+
Identifier datasetId = createADataset();
6971
assertNotNull(datasetId.getId());
70-
FileUploadMetadata meta = FileUploadMetadata.builder().description("My description.").categories(Arrays.asList(new String[]{"Data"}))
71-
.directoryLabel("test/x").build();
72-
DatasetFileList datasetFileList = datasetOps.uploadNativeFile(meta, datasetId, new byte[]{1, 2, 3, 4, 5}, "myFileName.dat");
72+
FileUploadMetadata meta = getUploadMetadata();
73+
74+
//act
75+
DatasetFileList datasetFileList = datasetOps.uploadNativeFile(new byte[]{1, 2, 3, 4, 5}, meta, datasetId, "myFileName.dat");
76+
77+
//assert
7378
assertNotNull(datasetFileList);
7479
assertEquals(1, datasetFileList.getFiles().size());
7580
assertTrue(datasetFileList.getFiles().get(0).getCategories().contains("Data"));
7681
assertTrue(datasetFileList.getFiles().get(0).getDescription().equals(("My description.")));
82+
assertEquals(5 ,datasetFileList.getFiles().get(0).getDataFile().getFilesize());
83+
}
84+
85+
@Test
86+
public void uploadFileToDataSetWithNativeApiInputStream() throws IOException, URISyntaxException {
87+
// arrange
88+
Identifier datasetId = createADataset();
89+
assertNotNull(datasetId.getId());
90+
FileUploadMetadata meta = getUploadMetadata();
91+
92+
//act
93+
DatasetFileList datasetFileList = datasetOps.uploadNativeFile(new ByteArrayInputStream(new byte[]{1, 2, 3, 4, 5,6}), 6, meta, datasetId, "myFileName.dat");
94+
95+
//assert
96+
assertNotNull(datasetFileList);
97+
assertEquals(1, datasetFileList.getFiles().size());
98+
DatasetFile uploadedFile = datasetFileList.getFiles().get(0);
99+
assertTrue(uploadedFile.getCategories().contains("Data"));
100+
assertTrue(uploadedFile.getDescription().equals(("My description.")));
101+
assertEquals(6 ,uploadedFile.getDataFile().getFilesize());
102+
}
103+
104+
private Identifier createADataset() throws MalformedURLException, URISyntaxException {
105+
DatasetFacade facade = createFacade();
106+
Identifier datasetId = dataverseOps.createDataset(facade, dataverseAlias);
107+
return datasetId;
108+
}
109+
110+
private FileUploadMetadata getUploadMetadata() {
111+
return FileUploadMetadata.builder().description("My description.").categories(Arrays.asList(new String[]{"Data"}))
112+
.directoryLabel("test/x").build();
77113
}
78114

79115
@Test

src/main/java/com/researchspace/dataverse/api/v1/DatasetOperations.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public interface DatasetOperations {
4242
*/
4343
DatasetVersion updateDataset(DatasetFacade facade, Identifier id);
4444

45-
4645
/**
4746
* Retrieves a {@link Dataset} based on its Id.
4847
* @param dsIdentifier
@@ -57,7 +56,27 @@ public interface DatasetOperations {
5756
*/
5857
List<DatasetVersion> getDatasetVersions(Identifier dsIdentifier);
5958

60-
DatasetFileList uploadNativeFile(FileUploadMetadata metadata, Identifier dsIdentifier, byte[] data, String fileName);
59+
/**
60+
* Upload a file to a dataset using Dataverse's native API (not Sword)
61+
* @param metadata Metadata to attach to the file upload
62+
* @param dsIdentifier The persistent identifier of the dataset
63+
* @param data bytes of data to upload
64+
* @param fileName The name of the file to be created on Dataverse
65+
* @return DatasetFileList information about the uploaded file.
66+
*/
67+
DatasetFileList uploadNativeFile( byte[] data, FileUploadMetadata metadata, Identifier dsIdentifier, String fileName);
68+
69+
/**
70+
* Upload a file to a dataset using Dataverse's native API (not Sword).
71+
* @param metadata Metadata to attach to the file upload
72+
* @param contentLength The length of the stream
73+
* @param dsIdentifier The persistent identifier of the dataset
74+
* @param data bytes of data to upload
75+
* @param fileName The name of the file to be created on Dataverse
76+
* @return DatasetFileList information about the uploaded file.
77+
*/
78+
DatasetFileList uploadNativeFile(InputStream data, long contentLength, FileUploadMetadata metadata,
79+
Identifier dsIdentifier, String fileName);
6180

6281
/**
6382
* Uploads a file to a dataset

src/main/java/com/researchspace/dataverse/entities/Checksum.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.researchspace.dataverse.entities;
22

3+
import com.researchspace.dataverse.http.FileUploadMetadata;
34
import lombok.Data;
45

6+
/**
7+
* Checksum is part of the response from
8+
* {@link com.researchspace.dataverse.api.v1.DatasetOperations#uploadNativeFile(byte[], FileUploadMetadata, Identifier, String)}
9+
*/
510
@Data
611
public class Checksum {
712
private String type;

src/main/java/com/researchspace/dataverse/entities/DatasetFile.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.researchspace.dataverse.entities;
22

3+
import com.researchspace.dataverse.http.FileUploadMetadata;
34
import lombok.Data;
45

56
import java.util.List;
67

8+
/**
9+
* DatasetFile is part of the response from
10+
* {@link com.researchspace.dataverse.api.v1.DatasetOperations#uploadNativeFile(byte[], FileUploadMetadata, Identifier, String)}
11+
*/
712
@Data
813
public class DatasetFile {
914
private String description;

src/main/java/com/researchspace/dataverse/entities/DatasetFileDetails.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.researchspace.dataverse.entities;
22

3+
import com.researchspace.dataverse.http.FileUploadMetadata;
34
import lombok.Data;
45

56
import java.util.Date;
67

8+
/**
9+
* DatasetFileDetails is a subsection of the response from
10+
* {@link com.researchspace.dataverse.api.v1.DatasetOperations#uploadNativeFile(byte[], FileUploadMetadata, Identifier, String)}
11+
*/
712
@Data
813
public class DatasetFileDetails {
914
private int id;

src/main/java/com/researchspace/dataverse/entities/DatasetFileList.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import lombok.Data;
44

55
import java.util.List;
6+
7+
/**
8+
* DatasetFileList is the response from uploading a file using the native API
9+
*/
610
@Data
711
public class DatasetFileList {
812
List<DatasetFile> files;

src/main/java/com/researchspace/dataverse/entities/ObjectOrStringMessageDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public String deserialize(JsonParser jp, DeserializationContext ctxt)
2121
} else if (node.isObject()){
2222
return node.get("message").toString();
2323
} else{
24-
throw new IllegalArgumentException("expect a string or an object with a string property");
24+
throw new IllegalArgumentException("expect a string or an object with a string property 'message'");
2525
}
2626

2727
}

src/main/java/com/researchspace/dataverse/http/DataverseOperationsImplV1.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import lombok.extern.slf4j.Slf4j;
1717
import org.apache.commons.lang3.StringUtils;
1818
import org.springframework.core.ParameterizedTypeReference;
19+
import org.springframework.core.io.AbstractResource;
20+
import org.springframework.core.io.ByteArrayResource;
21+
import org.springframework.core.io.InputStreamResource;
1922
import org.springframework.http.HttpEntity;
2023
import org.springframework.http.HttpHeaders;
2124
import org.springframework.http.HttpMethod;
@@ -192,17 +195,43 @@ public List<DatasetVersion> getDatasetVersions (Identifier dsIdentifier) {
192195
}
193196

194197
@Override
195-
public DatasetFileList uploadNativeFile( FileUploadMetadata metadata, Identifier dsIdentifier, byte[] data, String fileName){
198+
public DatasetFileList uploadNativeFile( byte[] data, FileUploadMetadata metadata, Identifier dsIdentifier, String fileName){
199+
ByteArrayResource resource = new ByteArrayResource(data){
200+
@Override
201+
public String getFilename(){
202+
return fileName;
203+
}
204+
};
205+
return getDatasetFileList(metadata, dsIdentifier, resource);
206+
}
207+
@Override
208+
public DatasetFileList uploadNativeFile(InputStream data, long contentLength, FileUploadMetadata metadata, Identifier dsIdentifier, String fileName) {
209+
InputStreamResource resource = new InputStreamResource(data) {
210+
@Override
211+
public String getFilename(){
212+
return fileName;
213+
}
214+
215+
@Override
216+
public long contentLength() throws IOException {
217+
return contentLength;
218+
}
219+
};
220+
return getDatasetFileList(metadata, dsIdentifier, resource);
221+
222+
}
223+
224+
private DatasetFileList getDatasetFileList(FileUploadMetadata metadata, Identifier dsIdentifier, AbstractResource resource) {
196225
String url = createV1Url("datasets", ":persistentId", "add") + "?persistentId=" + dsIdentifier.getPersistentId();
197226
ParameterizedTypeReference<DataverseResponse<DatasetFileList>> type =
198227
new ParameterizedTypeReference<DataverseResponse<DatasetFileList>>() {};
199-
HttpEntity<MultiValueMap<String, Object>> entity = new NativeFileUploader().uploadFile(metadata, apiKey, data, fileName);
228+
HttpEntity<MultiValueMap<String, Object>> entity = new NativeFileUploader().createFileUploadEntity(metadata, apiKey, resource);
200229
ResponseEntity<DataverseResponse<DatasetFileList>> resp = template.exchange(url, HttpMethod.POST, entity, type);
201230
log.debug("{}", resp.getBody());
202231
handleError(resp);
203232
return resp.getBody().getData();
204233
}
205-
234+
206235
/* (non-Javadoc)
207236
* @see com.researchspace.dataverse.http.DataverseAPI#uploadFile(java.lang.String, java.io.File)
208237
*/

src/main/java/com/researchspace/dataverse/http/FileUploadMetadata.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
*/
1111
@Data
1212
@Builder
13-
1413
public class FileUploadMetadata {
1514
private String description;
1615
private String directoryLabel;

0 commit comments

Comments
 (0)