|
2 | 2 |
|
3 | 3 | import com.google.gson.Gson; |
4 | 4 | import com.google.gson.GsonBuilder; |
5 | | -import com.google.gson.JsonArray; |
6 | 5 | import com.google.gson.JsonElement; |
7 | 6 | import com.google.gson.JsonObject; |
8 | 7 | import com.google.gson.JsonPrimitive; |
|
16 | 15 | import org.junit.Test; |
17 | 16 |
|
18 | 17 | import java.util.ArrayList; |
19 | | -import java.util.Map; |
| 18 | +import java.util.Set; |
20 | 19 |
|
21 | 20 | import static com.mapbox.api.directions.v5.utils.MutateJsonUtil.mutateJson; |
22 | 21 | import static org.junit.Assert.assertEquals; |
23 | 22 | import static org.junit.Assert.assertNotNull; |
| 23 | +import static org.junit.Assert.assertNull; |
| 24 | +import static org.junit.Assert.assertTrue; |
24 | 25 |
|
25 | 26 | public class DirectionsResponseTest extends TestUtils { |
26 | 27 |
|
@@ -72,6 +73,49 @@ public void testToFromJsonWithMutatedResponse() throws Exception { |
72 | 73 | assertEquals(mutatedJson, jsonFromObject); |
73 | 74 | } |
74 | 75 |
|
| 76 | + @Test |
| 77 | + public void accessUnrecognizedProperties() throws Exception { |
| 78 | + JsonObject directionsResponseJson = readJsonObject(DIRECTIONS_V5_PRECISION6_FIXTURE_ARTIFICIAL_FIELDS); |
| 79 | + String unrecognizedPropertyName = "testUnrecognizedProperty"; |
| 80 | + String unrecognizedPropertyValue = "test"; |
| 81 | + directionsResponseJson.add(unrecognizedPropertyName, new JsonPrimitive(unrecognizedPropertyValue)); |
| 82 | + DirectionsResponse response = DirectionsResponse.fromJson(directionsResponseJson.toString()); |
| 83 | + |
| 84 | + String value = response.getUnrecognizedProperty(unrecognizedPropertyName).getAsString(); |
| 85 | + JsonElement notExistingProperty = response.getUnrecognizedProperty("notExisting"); |
| 86 | + |
| 87 | + assertEquals(unrecognizedPropertyValue, value); |
| 88 | + assertNull(notExistingProperty); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void noUnrecognizedProperties() throws Exception { |
| 93 | + JsonObject directionsResponseJson = readJsonObject(DIRECTIONS_V5_PRECISION6_FIXTURE_ARTIFICIAL_FIELDS); |
| 94 | + DirectionsResponse response = DirectionsResponse.fromJson(directionsResponseJson.toString()); |
| 95 | + |
| 96 | + JsonElement value = response.getUnrecognizedProperty(""); |
| 97 | + Set<String> propertiesNames = response.getUnrecognizedPropertiesNames(); |
| 98 | + |
| 99 | + assertNull(value); |
| 100 | + assertEquals(0, propertiesNames.size()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void getUnrecognizedPropertiesNames() throws Exception { |
| 105 | + JsonObject directionsResponseJson = readJsonObject(DIRECTIONS_V5_PRECISION6_FIXTURE_ARTIFICIAL_FIELDS); |
| 106 | + directionsResponseJson.add("1", new JsonPrimitive(1)); |
| 107 | + directionsResponseJson.add("2", new JsonPrimitive(2)); |
| 108 | + directionsResponseJson.add("3", new JsonPrimitive(3)); |
| 109 | + DirectionsResponse response = DirectionsResponse.fromJson(directionsResponseJson.toString()); |
| 110 | + |
| 111 | + Set<String> properties = response.getUnrecognizedPropertiesNames(); |
| 112 | + |
| 113 | + assertEquals(3, properties.size()); |
| 114 | + assertTrue(properties.contains("1")); |
| 115 | + assertTrue(properties.contains("2")); |
| 116 | + assertTrue(properties.contains("3")); |
| 117 | + } |
| 118 | + |
75 | 119 | @Test |
76 | 120 | public void fromJson_correctlyBuildsFromJsonWithOptionsAndUuid() throws Exception { |
77 | 121 | String json = loadJsonFixture(DIRECTIONS_V5_PRECISION6_FIXTURE); |
@@ -158,4 +202,9 @@ public void fromToJsonForRouteWithSilentWaypoints() throws IOException { |
158 | 202 |
|
159 | 203 | assertEquals(initial, deserialized); |
160 | 204 | } |
| 205 | + |
| 206 | + private JsonObject readJsonObject(String file) throws IOException { |
| 207 | + Gson gson = new GsonBuilder().create(); |
| 208 | + return gson.fromJson(loadJsonFixture(file), JsonObject.class); |
| 209 | + } |
161 | 210 | } |
0 commit comments