Skip to content

Commit

Permalink
Address model: getters, unittesting, javadoc. Fix getDouble in BBRoot…
Browse files Browse the repository at this point in the history
….java
  • Loading branch information
sebastianmacarescu committed May 27, 2016
1 parent 5718d91 commit 90fc0c9
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 2 deletions.
127 changes: 126 additions & 1 deletion src/main/bookingbugAPI/models/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,137 @@

import helpers.HttpServiceResponse;


public class Address extends BBRoot{

public Address(HttpServiceResponse httpServiceResponse){
super(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");
}
}
3 changes: 2 additions & 1 deletion src/main/bookingbugAPI/models/BBRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class BBRoot {
public String id;
public Map<String, String> 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";
Expand Down Expand Up @@ -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;
Expand Down
52 changes: 52 additions & 0 deletions src/test/bookingbugAPI/models/AddressTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
21 changes: 21 additions & 0 deletions src/test/resources/json/address.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit 90fc0c9

Please sign in to comment.