From d98f0b9eee24ee173304bbc2022682e205f5a423 Mon Sep 17 00:00:00 2001 From: Macarescu Sebastian Date: Fri, 27 May 2016 12:37:09 +0300 Subject: [PATCH] Booking model: getters, unittesting, javadoc. Added note model --- src/main/bookingbugAPI/models/Booking.java | 266 ++++++++++++------ src/main/bookingbugAPI/models/Notes.java | 31 ++ .../bookingbugAPI/models/BookingTest.java | 101 +++++++ src/test/resources/json/booking.json | 241 ++++++++++++++++ 4 files changed, 548 insertions(+), 91 deletions(-) create mode 100644 src/main/bookingbugAPI/models/Notes.java create mode 100644 src/test/bookingbugAPI/models/BookingTest.java create mode 100644 src/test/resources/json/booking.json diff --git a/src/main/bookingbugAPI/models/Booking.java b/src/main/bookingbugAPI/models/Booking.java index 63c470f..e2021a6 100644 --- a/src/main/bookingbugAPI/models/Booking.java +++ b/src/main/bookingbugAPI/models/Booking.java @@ -11,51 +11,18 @@ import java.io.IOException; import java.net.URL; import java.text.ParseException; -import java.text.SimpleDateFormat; + import java.util.Calendar; import java.util.Date; -import java.util.logging.Logger; +import org.joda.time.DateTime; + +import java.util.List; +import java.util.Map; -public class Booking extends BBRoot { - /* - private String updated_at; - private String company_id; - private String session_id; - private String settings; - private String questions; - private String on_waitlist; - private String duration; - private String client_email; - private String id; - private String slot_id; - private String is_cancelled; - private String client_name; - private String client_mobile; - private String attended; - private String full_describe; - private String channel; - private String notes; - private String service_id; - private String client_id; - private String service_name; - private String status; - private String multi_status; - private String created_at; - private String price; - private String datetime; - private String resource_name; - private String slot_settings; - private String quantity; - private String booking_updated; - private String client_phone; - private String resource_id; - private String purchase_id; - private String member_id; - private String paid; - private String purchase_ref; - */ + +public class Booking extends BBRoot { public Booking(HttpServiceResponse httpServiceResponse, String auth_token) { super(httpServiceResponse, auth_token); @@ -65,6 +32,7 @@ public Booking(HttpServiceResponse response) { super(response); } + public Booking(){} public BBRoot getSchema() throws IOException { String link = getRep().getLinkByRel("edit").getHref(); @@ -90,45 +58,16 @@ public Booking bookingUpdate_Admin(BookingUpdateParams bParams) throws IOExcepti */ public Booking bookingCancel_Admin(BookingCancelParams bcParams) throws IOException { URL url = new URL(AdminURLS.Bookings.bookingCancel().set("companyId", getCompany_id()).set("id", this.id).expand()); - return new Booking(HttpService.api_DELETE(url, HttpService.jsonContentType, bcParams.getParams(), auth_token), auth_token); - } - - - public String getFirstName() { - return get("first_name"); - } - - public String getLastName() { - return get("last_name"); - } - - public String getEmail() { - return get("email"); + return new Booking(HttpService.api_DELETE(url, HttpService.jsonContentType, bcParams.getParams(), auth_token), auth_token); } - public String getAddress1() { - return get("address1"); - } - - public String getAddress2() { - return get("address2"); - } - - public String getAddress3() { - return get("address3"); - } - - public String getAddress4() { - return get("address4"); - } - - public String getPostCode() { - return get("postcode"); - } - - - public String getDuration() { - return get("duration"); + /** + * Returns the duration. + * + * @return The duration associated with the current Booking object. + */ + public Integer getDuration() { + return getInteger("duration", INTEGER_DEFAULT_VALUE); } public String getUpdated_at() { @@ -139,12 +78,13 @@ public String getCompany_id() { return get("company_id"); } - public String getSession_id() { - return get("session_id"); - } - public String getSettings() { - return get("settings"); + /** + * Returns the settings. Settings consists of an obfuscated id. + * @return The settings associated with the current Booking object. + */ + public Map getSettings() { + return getObject("settings", Map.class); } public String getQuestions() { @@ -179,10 +119,20 @@ public String getClient_mobile() { return get("client_mobile"); } - public String getAttended() { - return get("attended"); + /** + * Returns true if it's attended, false otherwise. + * + * @return The attended attribute associated with the current Booking object. + */ + public Boolean getAttended() { + return getBoolean("attended", BOOLEAN_DEFAULT_VALUE); } + /** + * Returns the full_describe of the booking. + * + * @return The full_describe associated with the current Booking object. + */ public String getFull_describe() { return get("full_describe"); } @@ -191,8 +141,13 @@ public String getChannel() { return get("channel"); } - public String getNotes() { - return get("notes"); + /** + * Returns the notes map. + * + * @return The notes associated with the current Booking object. + */ + public Notes getNotes() { + return getObject("notes", Notes.class); } public String getService_id() { @@ -211,8 +166,13 @@ public String getStatus() { return get("status"); } - public String getMulti_status() { - return get("multi_status"); + /** + * Returns the multi-status map. + * + * @return The multi-status map associated with the current Booking object. + */ + public Map getMulti_status() { + return getObject("multi_status", Map.class); } public String getCreated_at() { @@ -244,7 +204,7 @@ public Date getEndDateTimeObj(){ try { Calendar cal = Calendar.getInstance(); cal.setTime(startDateTime); - cal.add(Calendar.MINUTE, Integer.parseInt(getDuration())); + cal.add(Calendar.MINUTE, getDuration()); endDateTime = cal.getTime(); }catch (Exception e){ log.warning("Cannot get booking end datetime: " + e.toString()); @@ -256,8 +216,13 @@ public String getResource_name() { return get("resource_name"); } - public String getSlot_settings() { - return get("slot_settings"); + /** + * Returns the slot settings. + * + * @return The slot settings associated with the current Booking object. + */ + public Map getSlot_settings() { + return getObject("slot_settings", Map.class); } public String getQuantity() { @@ -291,4 +256,123 @@ public String getPaid() { public String getPurchase_ref() { return get("purchase_ref"); } + + /** + * Returns the client link. + * + * @return The client link associated with the current Booking object. + */ + public String getClientLink() { + return getLink("client"); + } + + /** + * Returns the check in link. + * + * @return The check in link associated with the current Booking object. + */ + public String getCheckInLink() { + return getLink("check_in"); + } + + /** + * Returns the questions link. + * + * @return The questions link associated with the current Booking object. + */ + public String getQuestionsLink() { + return getLink("questions"); + } + + /** + * Returns the edit link. + * + * @return The edit link associated with the current Booking object. + */ + public String getEditLink() { + return getLink("edit"); + } + + /** + * Returns the event groups link. + * + * @return The event groups link associated with the current Booking object. + */ + public String getEventGroupsLink() { + return getLink("event_groups"); + } + + /** + * Returns the event chain link. + * + * @return The event chain link associated with the current Booking object. + */ + public String getEventChainLink() { + return getLink("event_chain"); + } + + /** + * Returns the cancel link. + * + * @return The cancel link associated with the current Booking object. + */ + public String getCancelLink() { + return getLink("cancel"); + } + + /** + * Returns the address link. + * + * @return The address link associated with the current Booking object. + */ + public String getAddressLink() { + return getLink("address"); + } + + /** + * Returns the event chain id. + * + * @return The event chain id associated with the current Booking object. + */ + public Integer getEventChainId() { + return getInteger("event_chain_id", INTEGER_DEFAULT_VALUE); + } + + /** + * Returns the client resource. + * + * @return The client resource associated with the current Booking object. + */ + public Client getClient() { + return new Client(new HttpServiceResponse(getResource("client"))); + } + + /** + * Returns the answer array resource. + * + * @return The answers resource associated with the current Booking object. + */ + public BBCollection getAnswers() { + return new BBCollection<>(new HttpServiceResponse(getResource("answers")), auth_token, Answer.class); + } + + + /** + * Returns the survey answers summary resource. + * + * @return The survey answers summary resource associated with the current Booking object. + */ + public List getSurveyAnswersSummary() { + return getArray("survey_answers_summary"); + } + + /** + * Returns the minimum date and time of the booking cancel, with {@link DateTime DateTime()} as date format. + * + * @return The minimum date and time of the booking cancel associated with the current Booking object. + */ + public DateTime getMinCancellationTime() { + return getDate("min_cancellation_time"); + } + } diff --git a/src/main/bookingbugAPI/models/Notes.java b/src/main/bookingbugAPI/models/Notes.java new file mode 100644 index 0000000..f9d07ea --- /dev/null +++ b/src/main/bookingbugAPI/models/Notes.java @@ -0,0 +1,31 @@ +package bookingbugAPI.models; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * Created by User on 20.05.2016. + */ +public class Notes { + @JsonProperty("private") + private String[] privateNotes; + @JsonProperty("public") + private String[] publicNotes; + + /** + * Returns public notes. + * + * @return The private notes associated with the Note Object + */ + public String[] getPublicNotes() { + return publicNotes; + } + + /** + * Returns the private notes. + * + * @return The private notes associated with the Note Object + */ + public String[] getPrivateNotes() { + return privateNotes; + } +} diff --git a/src/test/bookingbugAPI/models/BookingTest.java b/src/test/bookingbugAPI/models/BookingTest.java new file mode 100644 index 0000000..07449ea --- /dev/null +++ b/src/test/bookingbugAPI/models/BookingTest.java @@ -0,0 +1,101 @@ +package bookingbugAPI.models; + +import com.fasterxml.jackson.core.JsonProcessingException; +import helpers.HttpServiceResponse; +import helpers.Utils; +import helpers.hal_addon.CustomJsonDeserializer; +import org.joda.time.DateTime; +import org.json.simple.JSONObject; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * Created by User on 17.05.2016. + */ +public class BookingTest extends ModelTest { + + private JSONObject jsonObject; + + @Override + @Before + public void setUp() { + jsonObject = getJSON("json/booking.json"); + } + + @Override + @Test + public void modelInit() throws java.text.ParseException { + Booking booking = new Booking(new HttpServiceResponse(Utils.stringToContentRep(jsonObject.toString()))); + JSONObject jsonLinks = (JSONObject) jsonObject.get("_links"); + JSONObject jsonEmbedded = (JSONObject) jsonObject.get("_embedded"); + CustomJsonDeserializer jsonDeserializer = new CustomJsonDeserializer(); + + assertTrue(booking.getId().toString().equals(jsonObject.get("id").toString())); + assertTrue(booking.getFull_describe().equals(jsonObject.get("full_describe").toString())); + assertTrue(booking.getResource_name().equals(jsonObject.get("resource_name").toString())); + assertTrue(booking.getService_name().equals(jsonObject.get("service_name").toString())); + assertTrue(booking.getResource_id().toString().equals(jsonObject.get("resource_id").toString())); + assertTrue(booking.getMember_id().toString().equals(jsonObject.get("member_id").toString())); + assertTrue(booking.getClient_name().equals(jsonObject.get("client_name"))); + assertTrue(booking.getClient_email().equals(jsonObject.get("client_email"))); + assertTrue(booking.getClient_phone().equals(jsonObject.get("client_phone"))); + assertTrue(booking.getClient_mobile().equals(jsonObject.get("client_mobile"))); + assertTrue(booking.getService_id().equals(jsonObject.get("service_id").toString())); + + //TODO: fix datetime with Joda and java.util.Date. Also find way to compare dates (not strings) + //assertTrue(booking.getDatetime().equals(new DateTime(jsonObject.get("datetime").toString()))); + + + assertTrue(booking.getDuration().toString().equals(jsonObject.get("duration").toString())); + assertTrue(booking.getOn_waitlist().toString().equals(jsonObject.get("on_waitlist").toString())); + assertTrue(booking.getCompany_id().toString().equals(jsonObject.get("company_id").toString())); + assertTrue(booking.getEventChainId().toString().equals(jsonObject.get("event_chain_id").toString())); + assertTrue(booking.getAttended().toString().equals(jsonObject.get("attended").toString())); + + //TODO: fix datetime with Joda and java.util.Date. Also find way to compare dates (not strings) + //assertTrue(booking.getBooking_updated().equals(new DateTime(jsonObject.get("booking_updated")))); + //assertTrue(booking.getUpdated_at().equals(new DateTime(jsonObject.get("updated_at")))); + //assertTrue(booking.getCreated_at().equals(new DateTime(jsonObject.get("created_at")))); + + assertTrue(booking.getClient_id().toString().equals(jsonObject.get("client_id").toString())); + assertTrue(booking.getPrice().toString().equals(jsonObject.get("price").toString())); + assertTrue(booking.getPaid().toString().equals(jsonObject.get("paid").toString())); + assertTrue(booking.getQuantity().toString().equals(jsonObject.get("quantity").toString())); + assertTrue(booking.getIs_cancelled().toString().equals(jsonObject.get("is_cancelled").toString())); + assertTrue(booking.getMulti_status().equals(jsonObject.get("multi_status"))); + assertTrue(booking.getPurchase_id().toString().equals(jsonObject.get("purchase_id").toString())); + assertTrue(booking.getPurchase_ref().equals(jsonObject.get("purchase_ref"))); + assertTrue(booking.getChannel().equals(jsonObject.get("channel"))); + assertTrue(booking.getStatus().toString().equals(jsonObject.get("status").toString())); + assertTrue(booking.getSlot_id().toString().equals(jsonObject.get("slot_id").toString())); + assertTrue(booking.getClientLink().equals(((JSONObject) jsonLinks.get("client")).get("href"))); + assertTrue(booking.getCheckInLink().equals(((JSONObject) jsonLinks.get("check_in")).get("href"))); + assertTrue(booking.getQuestionsLink().equals(((JSONObject) jsonLinks.get("questions")).get("href"))); + assertTrue(booking.getEventGroupsLink().equals(((JSONObject) jsonLinks.get("event_groups")).get("href"))); + assertTrue(booking.getEventChainLink().equals(((JSONObject) jsonLinks.get("event_chain")).get("href"))); + assertTrue(booking.getEditLink().equals(((JSONObject) jsonLinks.get("edit")).get("href"))); + assertTrue(booking.getCancelLink().equals(((JSONObject) jsonLinks.get("cancel")).get("href"))); + assertTrue(booking.getAddressLink().equals(((JSONObject) jsonLinks.get("address")).get("href"))); + assertTrue(booking.getSettings().equals(jsonObject.get("settings"))); + assertTrue(booking.getSlot_settings().equals(jsonObject.get("slot_settings"))); + assertTrue(booking.getSurveyAnswersSummary().equals(jsonObject.get("survey_answers_summary"))); + assertTrue(booking.getMinCancellationTime().equals(new DateTime(jsonObject.get("min_cancellation_time")))); + try { + assertTrue((jsonDeserializer.getMapper().writeValueAsString(booking.getNotes())).equals(jsonObject.get("notes").toString())); +// assertTrue((jsonDeserializer.getMapper().writeValueAsString(booking.getClient())).equals(jsonEmbedded.get("client").toString())); +// assertTrue((jsonDeserializer.getMapper().writeValueAsString(booking.getAnswers())).equals(jsonEmbedded.get("answers").toString())); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + } + + @Override + @After + public void tearDown() { + jsonObject = null; + } + +} diff --git a/src/test/resources/json/booking.json b/src/test/resources/json/booking.json new file mode 100644 index 0000000..20a5446 --- /dev/null +++ b/src/test/resources/json/booking.json @@ -0,0 +1,241 @@ +{ + "id": 2336940, + "full_describe": "Nutrition Consulting with Barry - Consultation Room 1", + "resource_name": "Consultation Room 1", + "person_name": "Barry", + "service_name": "Nutrition Consulting", + "resource_id": 23352, + "member_id": 900649, + "client_name": "Conor McCafferson", + "client_email": "conor.cafferkey@gmail.com", + "client_phone": "", + "client_mobile": "+44 (0)7472 391991", + "service_id": 52718, + "datetime": "2013-12-03T15:00:00+02:00", + "duration": 45, + "on_waitlist": false, + "company_id": 37901, + "attended": true, + "booking_updated": "2013-12-04T01:22:25Z", + "updated_at": "2013-12-03T14:34:07Z", + "created_at": "2013-12-03T14:34:07Z", + "client_id": 900649, + "person_id": 16246, + "price": 0, + "paid": 0, + "quantity": 1, + "is_cancelled": false, + "multi_status": {"here":"here"}, + "purchase_id": 1941779, + "purchase_ref": "6DJzrNwXnHXbMK1pMTk0MTc3OQ%3D%3D", + "event_chain_id": 3, + "notes": { + "public": [], + "private": [] + }, + "channel": "Client", + "status": 2, + "_embedded": { + "client": { + "first_name": "Conor", + "last_name": "McCafferson", + "email": "conor.cafferkey@gmail.com", + "address1": "", + "address2": "", + "address3": "", + "address4": "", + "postcode": "", + "country": "United Kingdom", + "phone": "", + "mobile": "7472391991", + "id": 900649, + "member_type": 1, + "reference": "", + "files": [], + "answers": [{ + "question_id": 17353, + "name": "Private Notes", + "answer": "123" + }], + "deleted": false, + "phone_prefix": "44", + "mobile_prefix": "44", + "q": { + "17353": { + "answer": "123" + } + }, + "_links": { + "self": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/client/900649" + }, + "bookings": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/bookings{/id}?client_id=900649{&start_date,end_date,page,per_page,include_cancelled,modified_since,slot_id,event_id,resource_id,service_id,person_id}", + "templated": true + }, + "pre_paid_bookings": { + "href": "https://uk.bookingbug.com/api/v1/37901/members/900649/pre_paid_bookings{?include_invalid,event_id}", + "templated": true + }, + "questions": { + "href": "https://uk.bookingbug.com/api/v1/37901/client_details" + }, + "edit": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/client/900649/edit" + } + } + }, + "answers": [{ + "id": 3696983, + "value": "whatever", + "price": 0.0, + "question_id": 15574, + "admin_only": false, + "important": false, + "_embedded": { + "question": { + "id": 15574, + "name": "What does your pet usually eat?", + "required": false, + "important": false, + "admin_only": false, + "applies_to": 0, + "ask_member": true, + "detail_type": "text_field", + "settings": {}, + "price": 0, + "price_per_booking": false, + "outcome": false, + "_links": { + "self": { + "href": "https://uk.bookingbug.com/api/v1/37901/questions/15574" + } + } + } + }, + "question_text": "What does your pet usually eat?", + "outcome": false, + "company_id": 37901, + "_links": { + "self": { + "href": "https://uk.bookingbug.com/api/v1/37901/answers/3696983" + }, + "question": { + "title": "What does your pet usually eat?", + "href": "https://uk.bookingbug.com/api/v1/37901/questions/15574" + } + } + }, { + "id": 3696984, + "value": "Dog", + "price": 0.0, + "question_id": 15571, + "admin_only": false, + "important": false, + "_embedded": { + "question": { + "id": 15571, + "name": "What type of pet?", + "required": true, + "important": false, + "admin_only": false, + "applies_to": 0, + "ask_member": true, + "detail_type": "select-price", + "options": [{ + "name": "Dog", + "price": -1000, + "is_default": false, + "id": 27946 + }, { + "name": "Cat", + "price": 0, + "is_default": false, + "id": 27947 + }, { + "name": "Rabbit", + "price": 0, + "is_default": false, + "id": 27948 + }], + "settings": {}, + "price": 0, + "price_per_booking": false, + "outcome": false, + "_links": { + "self": { + "href": "https://uk.bookingbug.com/api/v1/37901/questions/15571" + } + } + } + }, + "question_text": "What type of pet?", + "outcome": false, + "company_id": 37901, + "_links": { + "self": { + "href": "https://uk.bookingbug.com/api/v1/37901/answers/3696984" + }, + "question": { + "title": "What type of pet?", + "href": "https://uk.bookingbug.com/api/v1/37901/questions/15571" + } + } + }] + }, + "slot_id": 5452882, + "settings": {}, + "slot_settings": {}, + "answers_summary": [{ + "question_id": 15574, + "name": "What does your pet usually eat?", + "answer": "whatever" + }, { + "question_id": 15571, + "name": "What type of pet?", + "answer": "Dog" + }], + "survey_answers_summary": ["test1","test2"], + "questions": { + "15574": { + "answer": "whatever" + }, + "15571": { + "answer": "Dog" + } + }, + "min_cancellation_time": "2015-12-08T00:00:00+00:00", + "_links": { + "self": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/bookings/2336940" + }, + "client": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/client/900649" + }, + "check_in": { + "href": "https://uk.bookingbug.com/api/v1/bookings/2336940/check_in" + }, + "questions": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/questions?detail_group_id=19492" + }, + "event_groups": { + "title": "Our Events Group", + "href": "https://assist-dev.bookingbug.com/api/v1/37018/event_groups/48320" + }, + "event_chain": { + "title": "My Recurring Event 1", + "href": "https://assist-dev.bookingbug.com/api/v1/37018/event_chains/3{?member_level_id}", + "templated": true + }, + "edit": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/bookings/2336940/edit" + }, + "cancel": { + "href": "https://uk.bookingbug.com/api/v1/admin/37901/bookings/2336940/cancel{?notify,cancel_reason}", + "templated": true + }, + "address": { + "href": "https://assist-dev.bookingbug.com/api/v1/37018/addresses/30185" + } + } +}