diff --git a/src/main/bookingbugAPI/models/Address.java b/src/main/bookingbugAPI/models/Address.java index 2c44b42..f01e559 100644 --- a/src/main/bookingbugAPI/models/Address.java +++ b/src/main/bookingbugAPI/models/Address.java @@ -2,7 +2,6 @@ import helpers.HttpServiceResponse; - public class Address extends BBRoot{ public Address(HttpServiceResponse httpServiceResponse){ @@ -10,4 +9,130 @@ public Address(HttpServiceResponse httpServiceResponse){ } public Address() {} + + /** + * Returns the id. + * + * @return the id associated with the current Address object + */ + public Integer getId() { + return getInteger("id", INTEGER_DEFAULT_VALUE); + } + + /** + * Returns the first address. + * + * @return the first address associated with the current Address object + */ + public String getAddress1() { + return get("address1"); + } + + /** + * Returns the second address. + * + * @return the second address associated with the current Address object + */ + public String getAddress2() { + return get("address2"); + } + + /** + * Returns the third address. + * + * @return the third address associated with the current Address object + */ + public String getAddress3() { + return get("address3"); + } + + /** + * Returns the fourth address. + * + * @return the fourth address associated with the current Address object + */ + public String getAddress4() { + return get("address4"); + } + + /** + * Returns the fifth address. + * + * @return the fifth address associated with the current Address object + */ + public String getAddress5() { + return get("address5"); + } + + /** + * Returns the post code. + * + * @return the post code associated with the current Address object + */ + public String getPostcode() { + return get("postcode"); + } + + /** + * Returns the country from the address. + * + * @return the country associated with the current Address object + */ + public String getCountry() { + return get("country"); + } + + /** + * Returns the latitude expressed in degrees. + * + * @return the latitude associated with the current Address object + */ + public Double getLat() { + return getDouble("lat", DOUBLE_DEFAULT_VALUE); + } + + /** + * Returns the longitude expressed in degrees. + * + * @return the longitude associated with the current Address object + */ + public Double getLong() { + return getDouble("long", DOUBLE_DEFAULT_VALUE); + } + + /** + * Returns the map url. + * + * @return the map url associated with the current Address object + */ + public String getMapUrl() { + return get("map_url"); + } + + /** + * Returns the map marker. + * + * @return the map marker associated with the current Address object + */ + public String getMapMarker() { + return get("map_marker"); + } + + /** + * Returns the phone from address. + * + * @return the phone associated with the current Address object + */ + public String getPhone() { + return get("phone"); + } + + /** + * Returns the home phone from address. + * + * @return the home phone associated with the current Address object + */ + public String getHomephone() { + return get("homephone"); + } } diff --git a/src/main/bookingbugAPI/models/BBRoot.java b/src/main/bookingbugAPI/models/BBRoot.java index 1b5733a..380e867 100644 --- a/src/main/bookingbugAPI/models/BBRoot.java +++ b/src/main/bookingbugAPI/models/BBRoot.java @@ -46,6 +46,7 @@ public class BBRoot { public String id; public Map data; public int INTEGER_DEFAULT_VALUE = 0; + public double DOUBLE_DEFAULT_VALUE = 0.0; public boolean BOOLEAN_DEFAULT_VALUE = false; protected String curl = "N/A"; @@ -133,7 +134,7 @@ public int getInteger(String key, int defaultValue) { return defaultValue; } - public double getDouble(String key, int defaultValue) { + public double getDouble(String key, double defaultValue) { String val = this.get(key); if (val != null) return Double.parseDouble(val); return defaultValue; diff --git a/src/test/bookingbugAPI/models/AddressTest.java b/src/test/bookingbugAPI/models/AddressTest.java new file mode 100644 index 0000000..e332b0a --- /dev/null +++ b/src/test/bookingbugAPI/models/AddressTest.java @@ -0,0 +1,52 @@ +package bookingbugAPI.models; + +import helpers.HttpServiceResponse; +import helpers.Utils; +import org.json.simple.JSONObject; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.text.ParseException; + +import static org.junit.Assert.assertTrue; + +/** + * Created by User on 25.05.2016. + */ +public class AddressTest extends ModelTest{ + private JSONObject jsonObject; + + @Override + @Before + public void setUp() { + jsonObject = getJSON("json/address.json"); + } + + @Override + @Test + public void modelInit() throws ParseException { + Address address = new Address(new HttpServiceResponse(Utils.stringToContentRep(jsonObject.toString()))); + + assertTrue(address.getId().toString().equals(jsonObject.get("id").toString())); + assertTrue(address.getAddress1().equals(jsonObject.get("address1"))); + assertTrue(address.getAddress2().equals(jsonObject.get("address2"))); + assertTrue(address.getAddress3().equals(jsonObject.get("address3"))); + assertTrue(address.getAddress4().equals(jsonObject.get("address4"))); + assertTrue(address.getAddress5().equals(jsonObject.get("address5"))); + assertTrue(address.getPostcode().equals(jsonObject.get("postcode"))); + assertTrue(address.getCountry().equals(jsonObject.get("country"))); + assertTrue(address.getLat().toString().equals(jsonObject.get("lat").toString())); + assertTrue(address.getLong().toString().equals(jsonObject.get("long").toString())); + assertTrue(address.getMapUrl().equals(jsonObject.get("map_url"))); + assertTrue(address.getMapMarker().equals(jsonObject.get("map_marker"))); + assertTrue(address.getPhone().equals(jsonObject.get("phone"))); + assertTrue(address.getHomephone().equals(jsonObject.get("homephone"))); + } + + @Override + @After + public void tearDown() { + jsonObject = null; + } +} diff --git a/src/test/resources/json/address.json b/src/test/resources/json/address.json new file mode 100644 index 0000000..50673bd --- /dev/null +++ b/src/test/resources/json/address.json @@ -0,0 +1,21 @@ +{ + "id": 32352, + "address1": "add test", + "address2": "add 2 test", + "address3": "add 3 test", + "address4": "add 3 test", + "address5": "", + "postcode": "me19 9gj", + "country": "United Kingdom", + "lat": 55.378051, + "long": -3.435973, + "map_url": "", + "map_marker": "United+Kingdom", + "phone": "+1 2032879111", + "homephone": "2032879111", + "_links": { + "self": { + "href": "https://uk.bookingbug.com/api/v1/37901/addresses/32352" + } + } +}