Skip to content

Commit

Permalink
Add test case for decoding a user profile that is JSON encoded
Browse files Browse the repository at this point in the history
This test case covers what happens when an avatar points to a URL that is not an image, or at least can't be read by ImageIO.read().
Also, the last missing bits of the package `org.openstreetmap.josm.plugins.mapillary.utils.api` have received test coverage, we are now at 100% for this package.
  • Loading branch information
floscher committed Jun 20, 2017
1 parent ab88bf2 commit 925dd7c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.json.JsonValue;
import javax.json.JsonValue.ValueType;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.coor.LatLon;
Expand All @@ -36,8 +35,7 @@ private JsonDecoder() {
public static <T> Collection<T> decodeFeatureCollection(final JsonObject json, Function<JsonObject, T> featureDecoder) {
final Collection<T> result = new HashSet<>();
if (
json != null && "FeatureCollection".equals(json.getString("type", null)) &&
json.containsKey("features") && json.get("features").getValueType() == ValueType.ARRAY
json != null && "FeatureCollection".equals(json.getString("type", null)) && json.containsKey("features")
) {
final JsonValue features = json.get("features");
for (int i = 0; features instanceof JsonArray && i < ((JsonArray) features).size(); i++) {
Expand Down
7 changes: 7 additions & 0 deletions test/data/api/v3/responses/userProfile2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"about": "Having a non-image avatar",
"avatar": "https://example.org",
"created_at": "2016-01-31T01:47:28.000+0500",
"key": "abcdefg1",
"username": "mapillary_userÄ2!"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
package org.openstreetmap.josm.plugins.mapillary.utils.api;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -23,30 +24,54 @@
import org.openstreetmap.josm.plugins.mapillary.utils.TestUtil;

public class JsonUserProfileDecoderTest {
private static final Object FAKE_AVATAR;

static {
Object fakeAvatar;
try {
fakeAvatar = TestUtil.getAccessibleField(JsonUserProfileDecoder.class, "FAKE_AVATAR").get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
fakeAvatar = null;
}
FAKE_AVATAR = fakeAvatar;
}

@Test
public void testUtilityClass() {
TestUtil.testUtilityClass(JsonUserProfileDecoder.class);
}

private static InputStream getJsonInputStream() throws IOException, URISyntaxException {
private static InputStream getJsonInputStream(final String path) throws IOException, URISyntaxException {
String fileContent = String.join("\n", Files.readAllLines(
Paths.get(JsonUserProfileDecoderTest.class.getResource("/api/v3/responses/userProfile.json").toURI()),
Paths.get(JsonUserProfileDecoderTest.class.getResource(path).toURI()),
StandardCharsets.UTF_8
));
fileContent = fileContent.replace(
"https://d4vkkeqw582u.cloudfront.net/3f9f044b34b498ddfb9afbb6/profile.png",
JsonUserProfileDecoder.class.getResource("/images/fake-avatar.png").toString()
);
fileContent = fileContent.replace(
"https://example.org",
JsonUserProfileDecoder.class.getResource("/api/v3/responses/userProfile.json").toString()
);
return new ByteArrayInputStream(fileContent.getBytes());
}

@Test
public void testDecodeUserProfile() throws IOException, URISyntaxException {
UserProfile profile = JsonUserProfileDecoder.decodeUserProfile(Json.createReader(getJsonInputStream()).readObject());
public void testDecodeUserProfile() throws IOException, URISyntaxException, IllegalArgumentException, IllegalAccessException {
UserProfile profile = JsonUserProfileDecoder.decodeUserProfile(Json.createReader(getJsonInputStream("/api/v3/responses/userProfile.json")).readObject());
assertEquals("2BJl04nvnfW1y2GNaj7x5w", profile.getKey());
assertEquals("gyllen", profile.getUsername());
assertNotNull(profile.getAvatar());
assertFalse(FAKE_AVATAR == profile.getAvatar());
}

@Test
public void testDecodeUserProfile2() throws IOException, URISyntaxException, IllegalArgumentException, IllegalAccessException {
UserProfile profile = JsonUserProfileDecoder.decodeUserProfile(Json.createReader(getJsonInputStream("/api/v3/responses/userProfile2.json")).readObject());
assertEquals("abcdefg1", profile.getKey());
assertEquals("mapillary_userÄ2!", profile.getUsername());
assertTrue(FAKE_AVATAR == profile.getAvatar());
}

@Test
Expand All @@ -56,11 +81,9 @@ public void testDecodeInvalidUserProfile() throws IllegalArgumentException, Ille
assertNull(JsonUserProfileDecoder.decodeUserProfile(JsonUtil.string2jsonObject("{\"key\":\"arbitrary_key\"}")));

UserProfile profile = JsonUserProfileDecoder.decodeUserProfile(JsonUtil.string2jsonObject("{\"key\":\"arbitrary_key\", \"username\":\"arbitrary_username\"}"));
Field fakeAvatar = JsonUserProfileDecoder.class.getDeclaredField("FAKE_AVATAR");
fakeAvatar.setAccessible(true);
assertEquals(fakeAvatar.get(null), profile.getAvatar());
assertTrue(FAKE_AVATAR == profile.getAvatar());

profile = JsonUserProfileDecoder.decodeUserProfile(JsonUtil.string2jsonObject("{\"key\":\"arbitrary_key\", \"username\":\"arbitrary_username\", \"avatar\":\"https://127.0.0.1/nonExistingAvatarFile\"}"));
assertEquals(fakeAvatar.get(null), profile.getAvatar());
assertTrue(FAKE_AVATAR == profile.getAvatar());
}
}

0 comments on commit 925dd7c

Please sign in to comment.