Skip to content

Commit aa1e8ae

Browse files
committed
Merge pull request #70 from codyebberson/fix-metadata-bugs
Fixes #67, Fixes #68, Fixes #69 - Metadata feedback and fixes
2 parents f265be4 + 347c352 commit aa1e8ae

File tree

4 files changed

+131
-11
lines changed

4 files changed

+131
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
build/
33
.DS_Store
44
src/test/config/config.properties
5+
/target/

src/main/java/com/box/sdk/Metadata.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.eclipsesource.json.JsonArray;
44
import com.eclipsesource.json.JsonObject;
5+
import com.eclipsesource.json.JsonValue;
56

67
/**
78
* The Metadata class represents one type instance of Box metadata.
@@ -41,23 +42,23 @@ public Metadata(Metadata other) {
4142
* @return the metadata ID.
4243
*/
4344
public String getID() {
44-
return this.values.get("$id").asString();
45+
return this.get("/$id");
4546
}
4647

4748
/**
4849
* Returns the metadata type.
4950
* @return the metadata type.
5051
*/
5152
public String getTypeName() {
52-
return this.values.get("$type").asString();
53+
return this.get("/$type");
5354
}
5455

5556
/**
5657
* Returns the parent object ID (typically the file ID).
5758
* @return the parent object ID.
5859
*/
5960
public String getParentID() {
60-
return this.values.get("$parent").asString();
61+
return this.get("/$parent");
6162
}
6263

6364
/**
@@ -108,11 +109,15 @@ public Metadata test(String path, String value) {
108109

109110
/**
110111
* Returns a value.
111-
* @param key the metadata property name.
112+
* @param path the path that designates the key. Must be prefixed with a "/".
112113
* @return the metadata property value.
113114
*/
114-
public String get(String key) {
115-
return this.values.get(key).asString();
115+
public String get(String path) {
116+
final JsonValue value = this.values.get(this.pathToProperty(path));
117+
if (value == null) {
118+
return null;
119+
}
120+
return value.asString();
116121
}
117122

118123
/**
@@ -142,6 +147,9 @@ public String toString() {
142147
* @return the JSON property name.
143148
*/
144149
private String pathToProperty(String path) {
150+
if (path == null || !path.startsWith("/")) {
151+
throw new IllegalArgumentException("Path must be prefixed with a \"/\".");
152+
}
145153
return path.substring(1);
146154
}
147155

src/test/java/com/box/sdk/BoxFileTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,11 @@ public void createMetadataSucceeds() {
368368

369369
InputStream uploadStream = new ByteArrayInputStream(fileBytes);
370370
BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();
371-
uploadedFile.createMetadata(new Metadata().add("foo", "bar"));
371+
uploadedFile.createMetadata(new Metadata().add("/foo", "bar"));
372372

373373
Metadata check1 = uploadedFile.getMetadata();
374374
Assert.assertNotNull(check1);
375-
Assert.assertEquals("bar", check1.get("foo"));
375+
Assert.assertEquals("bar", check1.get("/foo"));
376376

377377
uploadedFile.delete();
378378
}
@@ -387,17 +387,17 @@ public void updateMetadataSucceeds() {
387387

388388
InputStream uploadStream = new ByteArrayInputStream(fileBytes);
389389
BoxFile uploadedFile = rootFolder.uploadFile(uploadStream, fileName).getResource();
390-
uploadedFile.createMetadata(new Metadata().add("foo", "bar"));
390+
uploadedFile.createMetadata(new Metadata().add("/foo", "bar"));
391391

392392
Metadata check1 = uploadedFile.getMetadata();
393393
Assert.assertNotNull(check1);
394-
Assert.assertEquals("bar", check1.get("foo"));
394+
Assert.assertEquals("bar", check1.get("/foo"));
395395

396396
uploadedFile.updateMetadata(check1.replace("/foo", "baz"));
397397

398398
Metadata check2 = uploadedFile.getMetadata();
399399
Assert.assertNotNull(check2);
400-
Assert.assertEquals("baz", check2.get("foo"));
400+
Assert.assertEquals("baz", check2.get("/foo"));
401401

402402
uploadedFile.delete();
403403
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.box.sdk;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.experimental.categories.Category;
6+
7+
import com.eclipsesource.json.JsonArray;
8+
import com.eclipsesource.json.JsonObject;
9+
10+
public class MetadataTest {
11+
12+
@Test
13+
@Category(UnitTest.class)
14+
public void testConstructor() {
15+
Metadata m = new Metadata();
16+
Assert.assertEquals("{}", m.toString());
17+
}
18+
19+
@Test
20+
@Category(UnitTest.class)
21+
public void testCopyConstructor() {
22+
Metadata m1 = new Metadata().add("/foo", "bar");
23+
Metadata m2 = new Metadata(m1);
24+
Assert.assertEquals("{\"foo\":\"bar\"}", m2.toString());
25+
}
26+
27+
@Test
28+
@Category(UnitTest.class)
29+
public void testAdd() {
30+
Metadata m = new Metadata().add("/foo", "bar");
31+
JsonArray operations = JsonArray.readFrom(m.getPatch());
32+
Assert.assertEquals(1, operations.size());
33+
JsonObject op = operations.get(0).asObject();
34+
Assert.assertEquals("add", op.get("op").asString());
35+
Assert.assertEquals("/foo", op.get("path").asString());
36+
Assert.assertEquals("bar", op.get("value").asString());
37+
}
38+
39+
@Test
40+
@Category(UnitTest.class)
41+
public void testReplace() {
42+
Metadata m = new Metadata().replace("/foo", "bar");
43+
JsonArray operations = JsonArray.readFrom(m.getPatch());
44+
Assert.assertEquals(1, operations.size());
45+
JsonObject op = operations.get(0).asObject();
46+
Assert.assertEquals("replace", op.get("op").asString());
47+
Assert.assertEquals("/foo", op.get("path").asString());
48+
Assert.assertEquals("bar", op.get("value").asString());
49+
}
50+
51+
@Test
52+
@Category(UnitTest.class)
53+
public void testTest() {
54+
Metadata m = new Metadata().test("/foo", "bar");
55+
JsonArray operations = JsonArray.readFrom(m.getPatch());
56+
Assert.assertEquals(1, operations.size());
57+
JsonObject op = operations.get(0).asObject();
58+
Assert.assertEquals("test", op.get("op").asString());
59+
Assert.assertEquals("/foo", op.get("path").asString());
60+
Assert.assertEquals("bar", op.get("value").asString());
61+
}
62+
63+
@Test
64+
@Category(UnitTest.class)
65+
public void testRemove() {
66+
Metadata m = new Metadata().remove("/foo");
67+
JsonArray operations = JsonArray.readFrom(m.getPatch());
68+
Assert.assertEquals(1, operations.size());
69+
JsonObject op = operations.get(0).asObject();
70+
Assert.assertEquals("remove", op.get("op").asString());
71+
Assert.assertEquals("/foo", op.get("path").asString());
72+
}
73+
74+
@Test
75+
@Category(UnitTest.class)
76+
public void testInvalidGet() {
77+
Metadata m = new Metadata();
78+
Assert.assertEquals(null, m.get("/foo"));
79+
}
80+
81+
@Test(expected = IllegalArgumentException.class)
82+
@Category(UnitTest.class)
83+
public void testNullPath() {
84+
new Metadata().add(null, "value");
85+
}
86+
87+
@Test(expected = IllegalArgumentException.class)
88+
@Category(UnitTest.class)
89+
public void testInvalidPath() {
90+
new Metadata().add("key", "value");
91+
}
92+
93+
@Test
94+
@Category(UnitTest.class)
95+
public void testMetaProperties() {
96+
String json = "{\"$id\":\"123\",\"$type\":\"my type\",\"$parent\":\"456\"}";
97+
Metadata m = new Metadata(JsonObject.readFrom(json));
98+
Assert.assertEquals("123", m.getID());
99+
Assert.assertEquals("my type", m.getTypeName());
100+
Assert.assertEquals("456", m.getParentID());
101+
}
102+
103+
@Test
104+
@Category(UnitTest.class)
105+
public void testMissingMetaProperties() {
106+
Metadata m = new Metadata();
107+
Assert.assertEquals(null, m.getID());
108+
Assert.assertEquals(null, m.getTypeName());
109+
Assert.assertEquals(null, m.getParentID());
110+
}
111+
}

0 commit comments

Comments
 (0)