Skip to content

Commit 5045e81

Browse files
author
Ilya Nevolin
committed
group appointments + cleanup
1 parent 47e17b6 commit 5045e81

File tree

4 files changed

+75
-37
lines changed

4 files changed

+75
-37
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ dependencies {
1212

1313
}
1414

15+
run {
16+
enableAssertions = true
17+
}
18+
1519
mainClassName = 'test.Test'

src/main/java/spurwing/util/Client.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ static public String complete_booking(String provider_id,
5959
String last_name,
6060
String email,
6161
String phone_number,
62-
String contact_type) throws IOException, JSONException {
62+
String contact_type,
63+
String appointment_id) throws IOException, JSONException {
6364
String url = API_URL + "bookings/complete_booking.json";
6465
JSONObject params = new JSONObject();
6566
Map<String, String> headers = new HashMap<String, String>();
@@ -73,6 +74,22 @@ static public String complete_booking(String provider_id,
7374
if (email != null) params.put("email", email);
7475
if (phone_number != null) params.put("phone_number", phone_number);
7576
if (contact_type != null) params.put("contact_type", contact_type);
77+
if (appointment_id != null) params.put("appointment_id", appointment_id);
78+
return HttpUtil.postJson(url, params.toString(), headers);
79+
}
80+
81+
static public String create_group_appointment(String authorization,
82+
String provider_id,
83+
String appointment_type_id,
84+
String datetime) throws IOException, JSONException {
85+
String url = API_URL + "appointments";
86+
JSONObject params = new JSONObject();
87+
Map<String, String> headers = new HashMap<String, String>();
88+
headers.put("authorization", "Bearer " + authorization);
89+
90+
if (provider_id != null) params.put("provider_id", provider_id);
91+
if (appointment_type_id != null) params.put("appointment_type_id", appointment_type_id);
92+
if (datetime != null) params.put("datetime", datetime);
7693
return HttpUtil.postJson(url, params.toString(), headers);
7794
}
7895

src/main/java/spurwing/util/HttpUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static public String fetch(String method, String url, String body,
296296
InputStream is = conn.getInputStream();
297297
String response = streamToString(is);
298298

299-
System.out.println(method + " " + conn.getResponseCode() + ": " + url);
299+
// System.out.println(method + " " + conn.getResponseCode() + ": " + url);
300300

301301
// handle redirects
302302
if (conn.getResponseCode() == 301) {

src/main/java/test/Test.java

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ public class Test {
1515
public static void main (String[] args) throws Exception {
1616
try {
1717
test_1();
18-
18+
test_2();
1919
} catch (Exception e) {
2020
e.printStackTrace();
2121
}
2222
}
2323

2424
public static void test_1() throws Exception {
25-
25+
System.out.println("test_1 START");
2626
// Calling Our PID and Key
2727
String public_PID;
2828
String private_KEY;
@@ -40,24 +40,16 @@ public static void test_1() throws Exception {
4040
throw new Exception("Missing PID and/or KEY values");
4141
}
4242

43-
// Listing all Appointment types categories
4443
String s = Client.get_appointment_types(public_PID);
45-
// System.out.println(s);
4644
JSONArray jsonarray = new JSONArray(s);
4745
JSONObject jsonobject = jsonarray.getJSONObject(0);
4846
String appointment_types_id = jsonobject.getString("id");
49-
assert jsonarray.length() == 3; // default 3
47+
assert jsonarray.length() >= 1; // default 3
5048

5149
Date date = new Date();
5250
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
5351
String strDate = dateFormat.format(date);
54-
// List of available days for appointment from a given date
55-
String s1 = Client.get_days_available(public_PID,
56-
appointment_types_id,
57-
strDate,
58-
null,
59-
null);
60-
// System.out.println(s1);
52+
String s1 = Client.get_days_available(public_PID, appointment_types_id, strDate, null, null);
6153
JSONObject obj_2 = new JSONObject(s1);
6254
JSONArray json_arr = obj_2.getJSONArray("days_available");
6355
assert json_arr.length() > 1;
@@ -66,50 +58,75 @@ public static void test_1() throws Exception {
6658
Calendar c = Calendar.getInstance();
6759
c.add(Calendar.DATE, 10); // number of days to add
6860
String end_date = (String)(dateFormat.format(c.getTime()));
69-
70-
// List of all slot available for appointment between start_date and end_date
71-
String s2 = Client.get_slots_available(public_PID,
72-
appointment_types_id,
73-
start_date,
74-
end_date,
75-
null);
76-
// System.out.println(s2);
61+
String s2 = Client.get_slots_available(public_PID, appointment_types_id, start_date, end_date, null);
7762

7863
JSONObject obj = new JSONObject(s2);
7964
JSONArray arr = obj.getJSONArray("slots_available");
8065
assert arr.length() > 10;
8166

8267
String date_available = arr.getJSONObject(0).getString("date");
83-
// Complete Booking of available slot
84-
String s3 = Client.complete_booking(public_PID,
85-
appointment_types_id,
86-
date_available, // must be available timeslot from API
87-
null,
88-
"Shubhanshu", "Arya", "shubhanshuarya@example.com",
89-
null, "Secure Videochat");
90-
// System.out.println(s3);
68+
String s3 = Client.complete_booking(public_PID, appointment_types_id, date_available, null, "Shubhanshu", "Arya", "shubhanshuarya@example.com", null, "Secure Videochat", null);
9169
JSONObject obj_3 = new JSONObject(s3);
9270
assert obj_3.has("appointment");
9371

94-
// List of all appointments available
9572
String s4 = Client.list_appointments(private_KEY, null, null, public_PID);
96-
// System.out.println(s4);
97-
9873
JSONObject jsonObj = new JSONObject(s4);
9974
assert jsonObj.has("data");
10075
JSONArray arr_value = jsonObj.getJSONObject("data").getJSONArray("appointments");
101-
assert arr_value.length() > 1;
76+
assert arr_value.length() >= 1;
10277
String appointment_id = arr_value.getJSONObject(0).getString("id");
103-
// System.out.println(appointment_id);
10478

105-
// deleting the appointment
10679
String s5 = Client.delete_appointment(private_KEY, appointment_id);
107-
// System.out.println(s5);
10880
JSONObject json_Obj = new JSONObject(s5);
10981
assert jsonObj.has("data");
11082
JSONObject json_Obj_2 = json_Obj.getJSONObject("data");
11183
assert json_Obj_2.has("appointment");
11284
String json_Obj_3 = json_Obj.getJSONObject("data").getJSONObject("appointment").getString("id");
11385
assert json_Obj_3.equals(appointment_id);
86+
87+
System.out.println("test_1 END");
88+
}
89+
public static void test_2() throws Exception {
90+
System.out.println("test_2 START");
91+
// Calling Our PID and Key
92+
String public_PID;
93+
String private_KEY;
94+
95+
if (System.getenv("SPURWING_PID") != null && System.getenv("SPURWING_KEY") != null) {
96+
public_PID = System.getenv("SPURWING_PID");
97+
private_KEY = System.getenv("SPURWING_KEY");
98+
}else{
99+
Credentials private_details = new Credentials();
100+
public_PID = private_details.getPID();
101+
private_KEY = private_details.getKEY();
102+
}
103+
104+
if ((public_PID == null || public_PID.trim().isEmpty()) || (private_KEY == null || private_KEY.trim().isEmpty())){
105+
throw new Exception("Missing PID and/or KEY values");
106+
}
107+
108+
String s = Client.get_appointment_types(public_PID);
109+
JSONArray jsonarray = new JSONArray(s);
110+
JSONObject jsonobject = jsonarray.getJSONObject(3); // let 4th apt be of group type
111+
String appointment_type_id = jsonobject.getString("id");
112+
assert jsonarray.length() >= 4;
113+
114+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
115+
Calendar c = Calendar.getInstance();
116+
c.add(Calendar.DATE, 1); // number of days to add
117+
String end_date = (String)(dateFormat.format(c.getTime()));
118+
s = Client.create_group_appointment(private_KEY, public_PID, appointment_type_id, end_date);
119+
jsonobject = new JSONObject(s);
120+
jsonobject = jsonobject.getJSONObject("data").getJSONObject("appointment");
121+
String apid = jsonobject.getString("id");
122+
123+
s = Client.complete_booking(public_PID, appointment_type_id, null, null, "John", "G", "john@nevolin.be", null, "java unit test", apid);
124+
assert ((new JSONObject(s)).has("appointment"));
125+
s = Client.complete_booking(public_PID, appointment_type_id, null, null, "Bill", "H", "bill@nevolin.be", null, "java unit test", apid);
126+
assert ((new JSONObject(s)).has("appointment"));
127+
128+
s = Client.delete_appointment(private_KEY, apid);
129+
assert ((new JSONObject(s)).getJSONObject("data").getJSONObject("appointment").has("id"));
130+
System.out.println("test_2 END");
114131
}
115132
}

0 commit comments

Comments
 (0)