Skip to content

Commit b305ec5

Browse files
Mike Kistlerpadamstx
Mike Kistler
authored andcommitted
Fix implementation of FileWithMetadata and add tests
1 parent cee18f5 commit b305ec5

File tree

2 files changed

+137
-4
lines changed

2 files changed

+137
-4
lines changed

src/main/java/com/ibm/cloud/sdk/core/service/model/FileWithMetadata.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.io.InputStream;
2020

2121
/**
22-
* A file and its associated metadata
22+
* A file and its associated metadata.
2323
*/
2424
public class FileWithMetadata {
2525

@@ -64,8 +64,7 @@ public Builder(InputStream data) {
6464
* @throws FileNotFoundException if the file could not be found
6565
*/
6666
public Builder(File file) throws FileNotFoundException {
67-
this.data = new FileInputStream(file);
68-
this.filename = file.getName();
67+
this.data(file);
6968
}
7069

7170
/**
@@ -77,6 +76,17 @@ public FileWithMetadata build() {
7776
return new FileWithMetadata(this);
7877
}
7978

79+
/**
80+
* Set the data.
81+
*
82+
* @param data the data
83+
* @return the FileWithMetadata builder
84+
*/
85+
public Builder data(InputStream data) {
86+
this.data = data;
87+
return this;
88+
}
89+
8090
/**
8191
* Set the filename.
8292
*
@@ -98,6 +108,18 @@ public Builder contentType(String contentType) {
98108
this.contentType = contentType;
99109
return this;
100110
}
111+
112+
/**
113+
* Set the data.
114+
*
115+
* @param file the file to use as the source of file contents and filename
116+
* @throws FileNotFoundException if the file could not be found
117+
*/
118+
public Builder data(File file) throws FileNotFoundException {
119+
this.data = new FileInputStream(file);
120+
this.filename = file.getName();
121+
return this;
122+
}
101123
}
102124

103125
private FileWithMetadata(Builder builder) {
@@ -118,7 +140,7 @@ public Builder newBuilder() {
118140
}
119141

120142
/**
121-
* The data / contents of the file
143+
* The data / contents of the file.
122144
*/
123145
public InputStream data() {
124146
return this.data;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* (C) Copyright IBM Corp. 2019.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package com.ibm.cloud.sdk.core.test.model;
15+
16+
import com.ibm.cloud.sdk.core.service.model.FileWithMetadata;
17+
import org.testng.annotations.Test;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.File;
21+
import java.io.FileNotFoundException;
22+
import java.io.InputStream;
23+
24+
import static org.testng.Assert.assertEquals;
25+
import static org.testng.Assert.assertNotNull;
26+
import static org.testng.Assert.assertNull;
27+
28+
/**
29+
* A few simple tests that exercise the FileWithMetadata model.
30+
*/
31+
public class FileWithMetadataTest {
32+
33+
@Test
34+
public void testBuilderWithOnlyRequired() {
35+
byte[] fileBytes = {(byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef};
36+
InputStream inputStream = new ByteArrayInputStream(fileBytes);
37+
FileWithMetadata.Builder builder = new FileWithMetadata.Builder();
38+
builder.data(inputStream);
39+
FileWithMetadata fileWithMetadata = builder.build();
40+
assertNotNull(fileWithMetadata);
41+
assertNotNull(fileWithMetadata.data());
42+
assertNull(fileWithMetadata.filename());
43+
assertNull(fileWithMetadata.contentType());
44+
}
45+
46+
@Test
47+
public void testBuilderWithFile() throws FileNotFoundException {
48+
String filename = "my-credentials.env";
49+
final File myFile = new File("src/test/resources/" + filename);
50+
FileWithMetadata.Builder builder = new FileWithMetadata.Builder();
51+
builder.data(myFile);
52+
FileWithMetadata fileWithMetadata = builder.build();
53+
assertNotNull(fileWithMetadata);
54+
assertNotNull(fileWithMetadata.data());
55+
assertEquals(filename, fileWithMetadata.filename());
56+
assertNull(fileWithMetadata.contentType());
57+
}
58+
59+
@Test
60+
public void testBuilderWithAllProps() {
61+
byte[] fileBytes = {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe};
62+
String filename = "my-awesome-file.txt";
63+
String contentType = "text/plain";
64+
InputStream inputStream = new ByteArrayInputStream(fileBytes);
65+
FileWithMetadata.Builder builder = new FileWithMetadata.Builder(inputStream);
66+
builder.filename(filename);
67+
builder.contentType(contentType);
68+
FileWithMetadata fileWithMetadata = builder.build();
69+
assertNotNull(fileWithMetadata);
70+
assertNotNull(fileWithMetadata.data());
71+
assertEquals(filename, fileWithMetadata.filename());
72+
assertEquals(contentType, fileWithMetadata.contentType());
73+
}
74+
75+
@Test
76+
public void testBuilderConstructorWithFile() throws FileNotFoundException {
77+
String filename = "my-credentials.env";
78+
final File myFile = new File("src/test/resources/" + filename);
79+
FileWithMetadata fileWithMetadata = new FileWithMetadata.Builder(myFile).build();
80+
assertNotNull(fileWithMetadata);
81+
assertNotNull(fileWithMetadata.data());
82+
assertEquals(filename, fileWithMetadata.filename());
83+
assertNull(fileWithMetadata.contentType());
84+
}
85+
86+
@Test
87+
public void testNewBuilder() {
88+
byte[] fileBytes = {(byte) 0xca, (byte) 0xfe, (byte) 0xba, (byte) 0xbe};
89+
String filename = "my-awesome-file.txt";
90+
String contentType = "text/plain";
91+
FileWithMetadata orig = new FileWithMetadata.Builder()
92+
.data(new ByteArrayInputStream(fileBytes))
93+
.filename("foo.txt")
94+
.contentType("text/plain")
95+
.build();
96+
FileWithMetadata.Builder builder = orig.newBuilder();
97+
builder.filename(filename);
98+
builder.contentType(contentType);
99+
FileWithMetadata fileWithMetadata = builder.build();
100+
assertNotNull(fileWithMetadata);
101+
assertNotNull(fileWithMetadata.data());
102+
assertEquals(filename, fileWithMetadata.filename());
103+
assertEquals(contentType, fileWithMetadata.contentType());
104+
}
105+
106+
@Test(expectedExceptions = {IllegalArgumentException.class})
107+
public void testValidation() {
108+
FileWithMetadata.Builder builder = new FileWithMetadata.Builder();
109+
FileWithMetadata fileWithMetadata = builder.build();
110+
}
111+
}

0 commit comments

Comments
 (0)