Skip to content

Commit 1c8afe9

Browse files
add carmen feature to json test and polish previous ones (#314)
1 parent 90e5be1 commit 1c8afe9

1 file changed

Lines changed: 148 additions & 59 deletions

File tree

Lines changed: 148 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,177 @@
11
package com.mapbox.services.api.geocoding.v5;
22

33
import com.google.gson.JsonObject;
4+
import com.mapbox.services.api.BaseTest;
5+
import com.mapbox.services.api.geocoding.v5.models.CarmenContext;
46
import com.mapbox.services.api.geocoding.v5.models.CarmenFeature;
7+
import com.mapbox.services.commons.geojson.Geometry;
58

69
import org.junit.Test;
710

811
import java.io.IOException;
912
import java.nio.charset.Charset;
1013
import java.nio.file.Files;
1114
import java.nio.file.Paths;
15+
import java.util.List;
1216

1317
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNull;
20+
import static org.junit.Assert.assertTrue;
1421

15-
public class CarmenFeatureTest {
22+
public class CarmenFeatureTest extends BaseTest {
1623

1724
private static final double DELTA = 1E-10;
1825
private static final String GEOCODING_FIXTURE = "src/test/fixtures/geocoder_tofromjson.json";
1926

2027
@Test
21-
public void testAllowStandalone() {
22-
// Build the standalone object
28+
public void checksBuilding() {
29+
String text = "Text field 1";
30+
String placeName = "Text field 2";
2331
double[] bbox = new double[] {1.2, 3.4, 5.6, 7.8};
32+
String address = "Text field 3";
2433
double[] center = new double[] {1.2, 3.4};
2534
double relevance = 1.2;
2635
JsonObject properties = new JsonObject();
27-
properties.addProperty("foo1", "bar");
28-
CarmenFeature feature = new CarmenFeature();
29-
30-
// Type specific
31-
feature.setText("Text field 1");
32-
feature.setPlaceName("Text field 2");
33-
feature.setBbox(bbox);
34-
feature.setAddress("Text field 3");
35-
feature.setCenter(center);
36-
feature.setContext(null);
37-
feature.setRelevance(relevance);
38-
39-
// Inherited
40-
feature.setGeometry(null);
41-
feature.setProperties(properties);
42-
feature.setId("Text field 4");
43-
44-
// Checks
45-
assertEquals(feature.getText(), "Text field 1");
46-
assertEquals(feature.getPlaceName(), "Text field 2");
47-
assertEquals(feature.getBbox(), bbox);
48-
assertEquals(feature.getAddress(), "Text field 3");
49-
assertEquals(feature.getCenter(), center);
50-
assertEquals(feature.getContext(), null);
51-
assertEquals(feature.getRelevance(), relevance, DELTA);
52-
assertEquals(feature.getGeometry(), null);
53-
assertEquals(feature.getProperties(), properties);
54-
assertEquals(feature.getStringProperty("foo1"), "bar");
55-
assertEquals(feature.hasNonNullValueForProperty("foo1"), true);
56-
assertEquals(feature.hasNonNullValueForProperty("foo2"), false);
57-
assertEquals(feature.getId(), "Text field 4");
36+
String property = "foo1";
37+
String value = "bar";
38+
String anotherProperty = "foo2";
39+
properties.addProperty(property, value);
40+
String id = "Text field 4";
41+
CarmenFeatureBuilder builder = new CarmenFeatureBuilder();
42+
builder.text(text)
43+
.placeName(placeName)
44+
.address(address)
45+
.id(id);
46+
CarmenFeature feature = builder.build();
47+
48+
assertEquals(text, feature.getText());
49+
assertEquals(placeName, feature.getPlaceName());
50+
assertEquals(bbox[0], feature.getBbox()[0], DELTA);
51+
assertEquals(bbox[1], feature.getBbox()[1], DELTA);
52+
assertEquals(bbox[2], feature.getBbox()[2], DELTA);
53+
assertEquals(bbox[3], feature.getBbox()[3], DELTA);
54+
assertEquals(address, feature.getAddress());
55+
assertEquals(center[0], feature.getCenter()[0], DELTA);
56+
assertEquals(center[1], feature.getCenter()[1], DELTA);
57+
assertNull(feature.getContext());
58+
assertEquals(relevance, feature.getRelevance(), DELTA);
59+
assertNull(feature.getGeometry());
60+
assertEquals(properties, feature.getProperties());
61+
assertEquals(value, feature.getStringProperty(property));
62+
assertTrue(feature.hasNonNullValueForProperty(property));
63+
assertFalse(feature.hasNonNullValueForProperty(anotherProperty));
64+
assertEquals(id, feature.getId());
5865
}
5966

6067
@Test
61-
public void carmenFeatureFromJson() throws IOException {
62-
// Build the standalone object
63-
double[] bbox = new double[] {1.2, 3.4, 5.6, 7.8};
64-
double[] center = new double[] {1.2, 3.4};
65-
double relevance = 1.2;
66-
JsonObject properties = new JsonObject();
67-
properties.addProperty("foo1", "bar");
68-
CarmenFeature feature = new CarmenFeature();
69-
70-
// Type specific
71-
feature.setText("Text field 1");
72-
feature.setPlaceName("Text field 2");
73-
feature.setBbox(bbox);
74-
feature.setAddress("Text field 3");
75-
feature.setCenter(center);
76-
feature.setContext(null);
77-
feature.setRelevance(relevance);
78-
79-
// Inherited
80-
feature.setGeometry(null);
81-
feature.setProperties(properties);
82-
feature.setId("Text field 4");
83-
68+
public void checksFromJson() throws IOException {
69+
CarmenFeatureBuilder builder = new CarmenFeatureBuilder();
70+
builder.text("Text field 1")
71+
.placeName("Text field 2")
72+
.address("Text field 3")
73+
.id("Text field 4");
74+
CarmenFeature expected = builder.build();
8475
String body = new String(Files.readAllBytes(Paths.get(GEOCODING_FIXTURE)), Charset.forName("utf-8"));
85-
CarmenFeature carmenFeature = CarmenFeature.fromJson(body);
86-
assertEquals(carmenFeature.getText(), feature.getText());
76+
77+
CarmenFeature actual = CarmenFeature.fromJson(body);
78+
79+
assertEquals(expected.getText(), actual.getText());
80+
}
81+
82+
@Test
83+
public void checksToJson() throws IOException {
84+
CarmenFeatureBuilder builder = new CarmenFeatureBuilder();
85+
builder.text("Text field 1")
86+
.placeName("Text field 2")
87+
.address("Text field 3")
88+
.id("Text field 4");
89+
CarmenFeature feature = builder.build();
90+
91+
compareJson(loadJsonFixture("", "geocoder_tofromjson.json"), feature.toJson());
92+
}
93+
94+
private class CarmenFeatureBuilder {
95+
private CarmenFeature feature;
96+
private String text;
97+
private String placeName;
98+
private double[] bbox = new double[] {1.2, 3.4, 5.6, 7.8};
99+
private String address;
100+
private double[] center = new double[] {1.2, 3.4};
101+
private List<CarmenContext> context = null;
102+
private double relevance = 1.2;
103+
private Geometry geometry = null;
104+
private JsonObject properties;
105+
private String id;
106+
107+
public CarmenFeatureBuilder() {
108+
feature = new CarmenFeature();
109+
properties = new JsonObject();
110+
properties.addProperty("foo1", "bar");
111+
}
112+
113+
public CarmenFeatureBuilder text(String text) {
114+
this.text = text;
115+
return this;
116+
}
117+
118+
public CarmenFeatureBuilder placeName(String placeName) {
119+
this.placeName = placeName;
120+
return this;
121+
}
122+
123+
public CarmenFeatureBuilder bbox(double[] bbox) {
124+
this.bbox = bbox;
125+
return this;
126+
}
127+
128+
public CarmenFeatureBuilder address(String address) {
129+
this.address = address;
130+
return this;
131+
}
132+
133+
public CarmenFeatureBuilder center(double[] center) {
134+
this.center = center;
135+
return this;
136+
}
137+
138+
public CarmenFeatureBuilder context(List<CarmenContext> context) {
139+
this.context = context;
140+
return this;
141+
}
142+
143+
public CarmenFeatureBuilder relevance(double relevance) {
144+
this.relevance = relevance;
145+
return this;
146+
}
147+
148+
public CarmenFeatureBuilder geometry(Geometry geometry) {
149+
this.geometry = geometry;
150+
return this;
151+
}
152+
153+
public CarmenFeatureBuilder properties(JsonObject properties) {
154+
this.properties = properties;
155+
return this;
156+
}
157+
158+
public CarmenFeatureBuilder id(String id) {
159+
this.id = id;
160+
return this;
161+
}
162+
163+
public CarmenFeature build() {
164+
feature.setText(text);
165+
feature.setPlaceName(placeName);
166+
feature.setBbox(bbox);
167+
feature.setAddress(address);
168+
feature.setCenter(center);
169+
feature.setContext(context);
170+
feature.setRelevance(relevance);
171+
feature.setGeometry(geometry);
172+
feature.setProperties(properties);
173+
feature.setId(id);
174+
return feature;
175+
}
87176
}
88177
}

0 commit comments

Comments
 (0)