From 1f0a7fb5aa8c85bf251c1494cd5e9ce5cd8ae090 Mon Sep 17 00:00:00 2001 From: Maciej Lew Date: Tue, 9 Apr 2024 14:34:49 +0200 Subject: [PATCH] Extracts list response abstract factory --- .../pl/smsapi/api/action/user/UserList.java | 4 +- .../api/response/CheckNumberResponse.java | 50 +++----- .../api/response/CountableResponse.java | 1 - .../pl/smsapi/api/response/ListResponse.java | 29 +++++ .../api/response/SendStatusResponse.java | 9 +- .../smsapi/api/response/StatusResponse.java | 29 +++-- .../pl/smsapi/api/response/UsersResponse.java | 35 +++--- .../contacts/ContactsContactListResponse.java | 54 ++++----- .../contacts/ContactsGroupListResponse.java | 40 +++---- .../ContactsPermissionListResponse.java | 34 +++--- .../pl/smsapi/test/doubles/ClientStub.java | 18 +++ .../test/doubles/ProxyResponseStub.java | 21 ++++ .../response/CheckNumberResponseTest.java | 61 ++++++++++ .../unit/response/SendStatusResponseTest.java | 59 ++++++++++ .../unit/response/StatusResponseTest.java | 53 +++++++++ .../test/unit/response/UsersResponseTest.java | 85 ++++++++++++++ .../ContactsContactListResponseTest.java | 109 ++++++++++++++++++ .../ContactsGroupListResponseTest.java | 94 +++++++++++++++ 18 files changed, 631 insertions(+), 154 deletions(-) create mode 100644 src/main/java/pl/smsapi/api/response/ListResponse.java create mode 100644 src/test/java/pl/smsapi/test/doubles/ClientStub.java create mode 100644 src/test/java/pl/smsapi/test/doubles/ProxyResponseStub.java create mode 100644 src/test/java/pl/smsapi/test/unit/response/CheckNumberResponseTest.java create mode 100644 src/test/java/pl/smsapi/test/unit/response/SendStatusResponseTest.java create mode 100644 src/test/java/pl/smsapi/test/unit/response/StatusResponseTest.java create mode 100644 src/test/java/pl/smsapi/test/unit/response/UsersResponseTest.java create mode 100644 src/test/java/pl/smsapi/test/unit/response/contacts/ContactsContactListResponseTest.java create mode 100644 src/test/java/pl/smsapi/test/unit/response/contacts/ContactsGroupListResponseTest.java diff --git a/src/main/java/pl/smsapi/api/action/user/UserList.java b/src/main/java/pl/smsapi/api/action/user/UserList.java index 63f3ac1..e668ec5 100644 --- a/src/main/java/pl/smsapi/api/action/user/UserList.java +++ b/src/main/java/pl/smsapi/api/action/user/UserList.java @@ -1,5 +1,6 @@ package pl.smsapi.api.action.user; +import org.json.JSONArray; import pl.smsapi.api.action.AbstractAction; import pl.smsapi.api.response.UsersResponse; @@ -11,7 +12,8 @@ public UserList() { } protected UsersResponse createResponse(String data) { - return new UsersResponse(data); + JSONArray jsonArray = new JSONArray(data); + return new UsersResponse(jsonArray.length(), jsonArray); } @Override diff --git a/src/main/java/pl/smsapi/api/response/CheckNumberResponse.java b/src/main/java/pl/smsapi/api/response/CheckNumberResponse.java index d724037..bfe6c9b 100644 --- a/src/main/java/pl/smsapi/api/response/CheckNumberResponse.java +++ b/src/main/java/pl/smsapi/api/response/CheckNumberResponse.java @@ -3,41 +3,25 @@ import org.json.JSONArray; import org.json.JSONObject; -import java.util.ArrayList; +public class CheckNumberResponse extends ListResponse { -public class CheckNumberResponse extends CountableResponse { - - private ArrayList list = new ArrayList(); - - public CheckNumberResponse(int count, JSONArray array) { - - super(count); - - if (array != null) { - - int arrayLength = array.length(); - for (int i = 0; i < arrayLength; i++) { - - JSONObject tmp = array.getJSONObject(i); - list.add( - new NumberResponse( - tmp.optString("id"), - tmp.optString("number"), - tmp.optInt("mcc"), - tmp.optInt("mnc"), - tmp.optString("info"), - tmp.optString("status"), - tmp.optInt("date"), - tmp.optInt("ported"), - tmp.optInt("ported_from"), - tmp.optString("price") - ) - ); - } - } + public CheckNumberResponse(int count, JSONArray jsonArray) { + super(count, jsonArray); } - public ArrayList getList() { - return list; + @Override + protected NumberResponse buildItem(JSONObject jsonObject) { + return new NumberResponse( + jsonObject.optString("id"), + jsonObject.optString("number"), + jsonObject.optInt("mcc"), + jsonObject.optInt("mnc"), + jsonObject.optString("info"), + jsonObject.optString("status"), + jsonObject.optInt("date"), + jsonObject.optInt("ported"), + jsonObject.optInt("ported_from"), + jsonObject.optString("price") + ); } } diff --git a/src/main/java/pl/smsapi/api/response/CountableResponse.java b/src/main/java/pl/smsapi/api/response/CountableResponse.java index 5286ef7..bef9bc4 100644 --- a/src/main/java/pl/smsapi/api/response/CountableResponse.java +++ b/src/main/java/pl/smsapi/api/response/CountableResponse.java @@ -5,7 +5,6 @@ public class CountableResponse implements Response { protected int count; public CountableResponse(int count) { - this.count = count; } diff --git a/src/main/java/pl/smsapi/api/response/ListResponse.java b/src/main/java/pl/smsapi/api/response/ListResponse.java new file mode 100644 index 0000000..8557ee0 --- /dev/null +++ b/src/main/java/pl/smsapi/api/response/ListResponse.java @@ -0,0 +1,29 @@ +package pl.smsapi.api.response; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; + +abstract public class ListResponse extends CountableResponse { + + private final ArrayList list = new ArrayList<>(); + + public ListResponse(int count, JSONArray jsonArray) { + super(count); + + if (jsonArray != null) { + final int n = jsonArray.length(); + for (int i = 0; i < n; i++) { + JSONObject row = jsonArray.getJSONObject(i); + list.add(buildItem(row)); + } + } + } + + abstract protected T buildItem(JSONObject jsonObject); + + public ArrayList getList() { + return list; + } +} diff --git a/src/main/java/pl/smsapi/api/response/SendStatusResponse.java b/src/main/java/pl/smsapi/api/response/SendStatusResponse.java index 743650b..ecfa03e 100644 --- a/src/main/java/pl/smsapi/api/response/SendStatusResponse.java +++ b/src/main/java/pl/smsapi/api/response/SendStatusResponse.java @@ -1,16 +1,13 @@ package pl.smsapi.api.response; import org.json.JSONArray; -import org.json.JSONObject; - -import java.util.ArrayList; public class SendStatusResponse extends StatusResponse { - private int parts; + private final int parts; - public SendStatusResponse(int count, int parts, JSONArray list) { - super(count, list); + public SendStatusResponse(int count, int parts, JSONArray jsonArray) { + super(count, jsonArray); this.parts = parts; } diff --git a/src/main/java/pl/smsapi/api/response/StatusResponse.java b/src/main/java/pl/smsapi/api/response/StatusResponse.java index 1a8d23a..5772fbd 100644 --- a/src/main/java/pl/smsapi/api/response/StatusResponse.java +++ b/src/main/java/pl/smsapi/api/response/StatusResponse.java @@ -3,24 +3,21 @@ import org.json.JSONArray; import org.json.JSONObject; -import java.util.ArrayList; +public class StatusResponse extends ListResponse { -public class StatusResponse extends CountableResponse { - - private ArrayList list = new ArrayList(); - - public StatusResponse(int count, JSONArray list) { - - super(count); - - final int n = list.length(); - for (int i = 0; i < n; i++) { - JSONObject row = list.getJSONObject(i); - this.list.add(new MessageResponse(row.optString("id"), row.optString("points"), row.optString("number"), row.optString("status"), row.optString("error"), row.optString("idx"))); - } + public StatusResponse(int count, JSONArray jsonArray) { + super(count, jsonArray); } - public ArrayList getList() { - return list; + @Override + protected MessageResponse buildItem(JSONObject jsonObject) { + return new MessageResponse( + jsonObject.optString("id"), + jsonObject.optString("points"), + jsonObject.optString("number"), + jsonObject.optString("status"), + jsonObject.optString("error"), + jsonObject.optString("idx") + ); } } diff --git a/src/main/java/pl/smsapi/api/response/UsersResponse.java b/src/main/java/pl/smsapi/api/response/UsersResponse.java index 1f5c823..671fe25 100644 --- a/src/main/java/pl/smsapi/api/response/UsersResponse.java +++ b/src/main/java/pl/smsapi/api/response/UsersResponse.java @@ -3,29 +3,22 @@ import org.json.JSONArray; import org.json.JSONObject; -import java.util.ArrayList; - -public class UsersResponse implements Response { - - private ArrayList list = new ArrayList(); - - public UsersResponse(String data) { - - if (data != null && !data.isEmpty()) { - - JSONArray aData = new JSONArray(data); - final int n = aData.length(); - if (n > 0) { - for (int i = 0; i < n; i++) { - JSONObject tmp = aData.getJSONObject(i); - list.add(new UserResponse(tmp.optString("username"), tmp.optDouble("limit"), tmp.optDouble("month_limit"), tmp.optInt("senders"), tmp.optInt("phonebook"), tmp.optInt("active"), tmp.optString("info"))); - } - } - } +public class UsersResponse extends ListResponse { + public UsersResponse(int count, JSONArray jsonArray) { + super(count, jsonArray); } - public ArrayList getList() { - return list; + @Override + protected UserResponse buildItem(JSONObject jsonObject) { + return new UserResponse( + jsonObject.optString("username"), + jsonObject.optDouble("limit"), + jsonObject.optDouble("month_limit"), + jsonObject.optInt("senders"), + jsonObject.optInt("phonebook"), + jsonObject.optInt("active"), + jsonObject.optString("info") + ); } } diff --git a/src/main/java/pl/smsapi/api/response/contacts/ContactsContactListResponse.java b/src/main/java/pl/smsapi/api/response/contacts/ContactsContactListResponse.java index 890fd44..8c73fa9 100644 --- a/src/main/java/pl/smsapi/api/response/contacts/ContactsContactListResponse.java +++ b/src/main/java/pl/smsapi/api/response/contacts/ContactsContactListResponse.java @@ -2,39 +2,31 @@ import org.json.JSONArray; import org.json.JSONObject; -import pl.smsapi.api.response.CountableResponse; +import pl.smsapi.api.response.ListResponse; -import java.util.ArrayList; +public class ContactsContactListResponse extends ListResponse { -public class ContactsContactListResponse extends CountableResponse { - private ArrayList list = new ArrayList(); - - public ContactsContactListResponse(int count, JSONArray list) { - super(count); - - final int n = list.length(); - for (int i = 0; i < n; i++) { - JSONObject row = list.getJSONObject(i); - this.list.add(new ContactsContactResponse( - row.getString("id"), - row.optString("first_name"), - row.optString("last_name"), - row.optString("birthday_date"), - row.optString("phone_number"), - row.optString("email"), - row.optString("gender"), - row.optString("city"), - row.optString("country"), - row.optString("date_created"), - row.optString("date_updated"), - row.optString("description"), - row.optJSONArray("groups"), - row.optString("source") - )); - } + public ContactsContactListResponse(int count, JSONArray jsonArray) { + super(count, jsonArray); } - public ArrayList getList() { - return list; + @Override + protected ContactsContactResponse buildItem(JSONObject jsonObject) { + return new ContactsContactResponse( + jsonObject.getString("id"), + jsonObject.optString("first_name"), + jsonObject.optString("last_name"), + jsonObject.optString("birthday_date"), + jsonObject.optString("phone_number"), + jsonObject.optString("email"), + jsonObject.optString("gender"), + jsonObject.optString("city"), + jsonObject.optString("country"), + jsonObject.optString("date_created"), + jsonObject.optString("date_updated"), + jsonObject.optString("description"), + jsonObject.optJSONArray("groups"), + jsonObject.optString("source") + ); } -} +} \ No newline at end of file diff --git a/src/main/java/pl/smsapi/api/response/contacts/ContactsGroupListResponse.java b/src/main/java/pl/smsapi/api/response/contacts/ContactsGroupListResponse.java index 18b3bf2..3130349 100644 --- a/src/main/java/pl/smsapi/api/response/contacts/ContactsGroupListResponse.java +++ b/src/main/java/pl/smsapi/api/response/contacts/ContactsGroupListResponse.java @@ -2,33 +2,25 @@ import org.json.JSONArray; import org.json.JSONObject; -import pl.smsapi.api.response.CountableResponse; +import pl.smsapi.api.response.ListResponse; -import java.util.ArrayList; +public class ContactsGroupListResponse extends ListResponse { -public class ContactsGroupListResponse extends CountableResponse { - private ArrayList list = new ArrayList(); - - public ContactsGroupListResponse(int count, JSONArray list) { - super(count); - - final int n = list.length(); - for (int i = 0; i < n; i++) { - JSONObject row = list.getJSONObject(i); - this.list.add(new ContactsGroupResponse( - row.optString("id"), - row.optString("name"), - row.optString("description"), - row.optString("date_created"), - row.optString("date_updated"), - row.optString("created_by"), - row.optString("idx"), - row.optJSONArray("permissions") - )); - } + public ContactsGroupListResponse(int count, JSONArray jsonArray) { + super(count, jsonArray); } - public ArrayList getList() { - return list; + @Override + protected ContactsGroupResponse buildItem(JSONObject jsonObject) { + return new ContactsGroupResponse( + jsonObject.optString("id"), + jsonObject.optString("name"), + jsonObject.optString("description"), + jsonObject.optString("date_created"), + jsonObject.optString("date_updated"), + jsonObject.optString("created_by"), + jsonObject.optString("idx"), + jsonObject.optJSONArray("permissions") + ); } } diff --git a/src/main/java/pl/smsapi/api/response/contacts/ContactsPermissionListResponse.java b/src/main/java/pl/smsapi/api/response/contacts/ContactsPermissionListResponse.java index cf0735c..314e125 100644 --- a/src/main/java/pl/smsapi/api/response/contacts/ContactsPermissionListResponse.java +++ b/src/main/java/pl/smsapi/api/response/contacts/ContactsPermissionListResponse.java @@ -2,30 +2,22 @@ import org.json.JSONArray; import org.json.JSONObject; -import pl.smsapi.api.response.CountableResponse; +import pl.smsapi.api.response.ListResponse; -import java.util.ArrayList; +public class ContactsPermissionListResponse extends ListResponse { -public class ContactsPermissionListResponse extends CountableResponse { - private ArrayList list = new ArrayList(); - - public ContactsPermissionListResponse(int count, JSONArray list) { - super(count); - - final int n = list.length(); - for (int i = 0; i < n; i++) { - JSONObject row = list.getJSONObject(i); - this.list.add(new ContactsPermissionResponse( - row.optString("group_id"), - row.optString("username"), - row.optBoolean("write"), - row.optBoolean("read"), - row.optBoolean("send") - )); - } + public ContactsPermissionListResponse(int count, JSONArray jsonArray) { + super(count, jsonArray); } - public ArrayList getList() { - return list; + @Override + protected ContactsPermissionResponse buildItem(JSONObject jsonObject) { + return new ContactsPermissionResponse( + jsonObject.optString("group_id"), + jsonObject.optString("username"), + jsonObject.optBoolean("write"), + jsonObject.optBoolean("read"), + jsonObject.optBoolean("send") + ); } } diff --git a/src/test/java/pl/smsapi/test/doubles/ClientStub.java b/src/test/java/pl/smsapi/test/doubles/ClientStub.java new file mode 100644 index 0000000..0e78785 --- /dev/null +++ b/src/test/java/pl/smsapi/test/doubles/ClientStub.java @@ -0,0 +1,18 @@ +package pl.smsapi.test.doubles; + +import pl.smsapi.Client; +import pl.smsapi.api.authenticationStrategy.AuthenticationStrategy; + +public class ClientStub implements Client { + @Override + public AuthenticationStrategy getAuthenticationStrategy() { + return new AuthenticationStrategyStub(); + } + + private static class AuthenticationStrategyStub implements AuthenticationStrategy { + @Override + public String getAuthenticationHeader() { + return null; + } + } +} diff --git a/src/test/java/pl/smsapi/test/doubles/ProxyResponseStub.java b/src/test/java/pl/smsapi/test/doubles/ProxyResponseStub.java new file mode 100644 index 0000000..d5adca6 --- /dev/null +++ b/src/test/java/pl/smsapi/test/doubles/ProxyResponseStub.java @@ -0,0 +1,21 @@ +package pl.smsapi.test.doubles; + +import pl.smsapi.api.authenticationStrategy.AuthenticationStrategy; +import pl.smsapi.proxy.Proxy; + +import java.io.InputStream; +import java.util.Map; + +public class ProxyResponseStub implements Proxy { + + private final String rawResponse; + + public ProxyResponseStub(String rawResponse) { + this.rawResponse = rawResponse; + } + + @Override + public String execute(String endpoint, Map data, Map files, String httpMethod, AuthenticationStrategy authenticationStrategy) throws Exception { + return rawResponse; + } +} diff --git a/src/test/java/pl/smsapi/test/unit/response/CheckNumberResponseTest.java b/src/test/java/pl/smsapi/test/unit/response/CheckNumberResponseTest.java new file mode 100644 index 0000000..fa1d64f --- /dev/null +++ b/src/test/java/pl/smsapi/test/unit/response/CheckNumberResponseTest.java @@ -0,0 +1,61 @@ +package pl.smsapi.test.unit.response; + +import org.junit.Test; +import pl.smsapi.api.action.hlr.HLRCheckNumber; +import pl.smsapi.api.response.CheckNumberResponse; +import pl.smsapi.api.response.NumberResponse; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.doubles.ClientStub; +import pl.smsapi.test.doubles.ProxyResponseStub; + +import java.util.Optional; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; + +public class CheckNumberResponseTest { + + @Test + public void deserialize_non_empty_response() throws SmsapiException { + HLRCheckNumber action = new HLRCheckNumber(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "{" + + " \"count\": 1," + + " \"list\": [" + + " {" + + " \"date\": 1712565008," + + " \"id\": \"1\"," + + " \"info\": \"Resource description\"," + + " \"mcc\": 260," + + " \"mnc\": 3," + + " \"number\": \"500600700\"," + + " \"ported\": 0," + + " \"ported_from\": 3," + + " \"price\": 0.04," + + " \"status\": \"OK\"" + + " }" + + " ]" + + "}" + )); + + CheckNumberResponse response = action.execute(); + + assertFalse(response.getList().isEmpty()); + assertEquals(1, response.getCount()); + + Optional hlr1 = response.getList().stream().filter( + hlrResponse -> hlrResponse.getId().equals("1") + ).findFirst(); + assertTrue(hlr1.isPresent()); + assertEquals(1712565008, hlr1.get().getDate()); + assertEquals("Resource description", hlr1.get().getInfo()); + assertEquals(260, hlr1.get().getMcc()); + assertEquals(3, hlr1.get().getMnc()); + assertEquals("500600700", hlr1.get().getNumber()); + assertEquals(0, hlr1.get().getPorted()); + assertEquals(3, hlr1.get().getPortedFrom()); + assertEquals(0.04, hlr1.get().getPoints(), 0.01); + assertEquals("OK", hlr1.get().getStatus()); + } +} diff --git a/src/test/java/pl/smsapi/test/unit/response/SendStatusResponseTest.java b/src/test/java/pl/smsapi/test/unit/response/SendStatusResponseTest.java new file mode 100644 index 0000000..a0b0aec --- /dev/null +++ b/src/test/java/pl/smsapi/test/unit/response/SendStatusResponseTest.java @@ -0,0 +1,59 @@ +package pl.smsapi.test.unit.response; + +import org.junit.Test; +import pl.smsapi.api.action.sms.SMSSend; +import pl.smsapi.api.response.MessageResponse; +import pl.smsapi.api.response.SendStatusResponse; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.doubles.ClientStub; +import pl.smsapi.test.doubles.ProxyResponseStub; + +import java.util.Optional; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; + +public class SendStatusResponseTest { + + @Test + public void deserialize_response() throws SmsapiException { + SMSSend action = new SMSSend(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "{" + + " \"count\": 1," + + " \"length\": 7," + + " \"message\": \"Message\"," + + " \"parts\": 1," + + " \"list\": [" + + " {" + + " \"id\": \"7074294081650020450\"," + + " \"points\": 0.165," + + " \"number\": \"48327201200\"," + + " \"date_sent\": 1712570310," + + " \"submitted_number\": \"327201200\"," + + " \"status\": \"QUEUE\"," + + " \"error\": null," + + " \"idx\": \"example-user-provided-id-123\"," + + " \"parts\": 10" + + " }" + + " ]" + + "}" + )); + + SendStatusResponse response = action.execute(); + + assertFalse(response.getList().isEmpty()); + assertEquals(1, response.getCount()); + + Optional message1 = response.getList().stream().filter( + contactResponse -> contactResponse.getId().equals("7074294081650020450") + ).findFirst(); + assertTrue(message1.isPresent()); + assertEquals(0.165, message1.get().getPoints(), 0.001); + assertEquals("48327201200", message1.get().getNumber()); + assertEquals("QUEUE", message1.get().getStatus()); + assertEquals("", message1.get().getError()); + assertEquals("example-user-provided-id-123", message1.get().getIdx()); + } +} diff --git a/src/test/java/pl/smsapi/test/unit/response/StatusResponseTest.java b/src/test/java/pl/smsapi/test/unit/response/StatusResponseTest.java new file mode 100644 index 0000000..435dd4b --- /dev/null +++ b/src/test/java/pl/smsapi/test/unit/response/StatusResponseTest.java @@ -0,0 +1,53 @@ +package pl.smsapi.test.unit.response; + +import org.junit.Test; +import pl.smsapi.api.action.sms.SMSGet; +import pl.smsapi.api.response.MessageResponse; +import pl.smsapi.api.response.StatusResponse; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.doubles.ClientStub; +import pl.smsapi.test.doubles.ProxyResponseStub; + +import java.util.Optional; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + +public class StatusResponseTest { + + @Test + public void deserialize_response() throws SmsapiException { + SMSGet action = new SMSGet(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "{" + + " \"count\": 1," + + " \"list\": [" + + " {" + + " \"error\": null," + + " \"id\": \"7074294081650020450\"," + + " \"idx\": \"example-user-provided-id-123\"," + + " \"number\": \"48327201200\"," + + " \"points\": 0.165," + + " \"status\": \"QUEUE\"" + + " }" + + " ]" + + "}" + )); + + StatusResponse response = action.execute(); + + assertFalse(response.getList().isEmpty()); + assertEquals(1, response.getCount()); + + Optional message1 = response.getList().stream().filter( + messageResponse -> messageResponse.getId().equals("7074294081650020450") + ).findFirst(); + assertTrue(message1.isPresent()); + assertEquals(0.165, message1.get().getPoints(), 0.001); + assertEquals("48327201200", message1.get().getNumber()); + assertEquals("QUEUE", message1.get().getStatus()); + assertEquals("", message1.get().getError()); + assertEquals("example-user-provided-id-123", message1.get().getIdx()); + } +} diff --git a/src/test/java/pl/smsapi/test/unit/response/UsersResponseTest.java b/src/test/java/pl/smsapi/test/unit/response/UsersResponseTest.java new file mode 100644 index 0000000..8a4d6c4 --- /dev/null +++ b/src/test/java/pl/smsapi/test/unit/response/UsersResponseTest.java @@ -0,0 +1,85 @@ +package pl.smsapi.test.unit.response; + +import org.junit.Test; +import pl.smsapi.api.action.user.UserList; +import pl.smsapi.api.response.UserResponse; +import pl.smsapi.api.response.UsersResponse; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.doubles.ClientStub; +import pl.smsapi.test.doubles.ProxyResponseStub; + +import java.util.ArrayList; +import java.util.Optional; + +import static org.junit.Assert.*; + +public class UsersResponseTest { + + @Test + public void deserialize_empty_response() throws SmsapiException { + UserList action = new UserList(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "[]" + )); + + UsersResponse response = action.execute(); + ArrayList list = response.getList(); + + assertTrue(list.isEmpty()); + } + + @Test + public void deserialize_non_empty_response() throws SmsapiException { + UserList action = new UserList(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "[" + + "{" + + " \"active\": \"1\"," + + " \"info\": null," + + " \"limit\": \"100.0000\"," + + " \"month_limit\": \"0.0000\"," + + " \"phonebook\": \"0\"," + + " \"senders\": \"0\"," + + " \"username\": \"user1\"" + + "}," + + "{" + + " \"active\": \"0\"," + + " \"info\": \"any\"," + + " \"limit\": \"200.0000\"," + + " \"month_limit\": \"20.0000\"," + + " \"phonebook\": \"1\"," + + " \"senders\": \"1\"," + + " \"username\": \"user2\"" + + "}" + + "]" + )); + + UsersResponse response = action.execute(); + + assertEquals(2, response.getList().size()); + + Optional user1 = response.getList().stream().filter( + userResponse -> userResponse.getUsername().equals("user1") + ).findFirst(); + assertTrue(user1.isPresent()); + assertEquals(100L, user1.get().getLimit(), 0.01); + assertEquals(0L, user1.get().getMonthLimit(), 0.01); + assertEquals(0, user1.get().getSenders()); + assertEquals(0, user1.get().getPhonebook()); + assertEquals(1, user1.get().getActive()); + assertEquals("", user1.get().getInfo()); + + Optional user2 = response.getList().stream().filter( + userResponse -> userResponse.getUsername().equals("user2") + ).findFirst(); + assertTrue(user2.isPresent()); + assertEquals(200L, user2.get().getLimit(), 0.01); + assertEquals(20L, user2.get().getMonthLimit(), 0.01); + assertEquals(1, user2.get().getSenders()); + assertEquals(1, user2.get().getPhonebook()); + assertEquals(0, user2.get().getActive()); + assertEquals("any", user2.get().getInfo()); + } +} diff --git a/src/test/java/pl/smsapi/test/unit/response/contacts/ContactsContactListResponseTest.java b/src/test/java/pl/smsapi/test/unit/response/contacts/ContactsContactListResponseTest.java new file mode 100644 index 0000000..e773ce8 --- /dev/null +++ b/src/test/java/pl/smsapi/test/unit/response/contacts/ContactsContactListResponseTest.java @@ -0,0 +1,109 @@ +package pl.smsapi.test.unit.response.contacts; + +import org.junit.Test; +import pl.smsapi.api.action.contacts.ContactsContactList; +import pl.smsapi.api.response.contacts.ContactsContactListResponse; +import pl.smsapi.api.response.contacts.ContactsContactResponse; +import pl.smsapi.api.response.contacts.ContactsGroupResponse; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.doubles.ClientStub; +import pl.smsapi.test.doubles.ProxyResponseStub; + +import java.util.Optional; + +import static org.junit.Assert.*; + +public class ContactsContactListResponseTest { + + @Test + public void deserialize_empty_response() throws SmsapiException { + ContactsContactList action = new ContactsContactList(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "{" + + " \"collection\": []," + + " \"size\": 0" + + "}" + )); + + ContactsContactListResponse response = action.execute(); + + assertTrue(response.getList().isEmpty()); + assertEquals(0, response.getCount()); + } + + @Test + public void deserialize_non_empty_response() throws SmsapiException { + ContactsContactList action = new ContactsContactList(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "{" + + " \"size\": 1," + + " \"collection\": [" + + " {" + + " \"id\": \"0f0f0f0f0f0f0f0f0f0f0f0f\"," + + " \"first_name\": \"John\"," + + " \"last_name\": \"Doe\"," + + " \"phone_number\": \"48327201200\"," + + " \"email\": \"john.doe@example.com\"," + + " \"gender\": \"undefined\"," + + " \"birthday_date\": \"2017-07-21\"," + + " \"description\": \"Resource description\"," + + " \"city\": \"Example City\"," + + " \"country\": \"Example Country\"," + + " \"source\": \"Example Source\"," + + " \"date_created\": \"2017-07-21T17:32:28Z\"," + + " \"date_updated\": \"2017-07-21T17:32:28Z\"," + + " \"groups\": [" + + " {" + + " \"id\": \"0f0f0f0f0f0f0f0f0f0f0f0f\"," + + " \"name\": \"Example Group\"," + + " \"description\": \"Resource description\"," + + " \"contacts_count\": 0," + + " \"date_created\": \"2017-07-21T17:32:28Z\"," + + " \"date_updated\": \"2017-07-21T17:32:28Z\"," + + " \"created_by\": \"example_username\"," + + " \"idx\": \"example-user-provided-id-123\"," + + " \"contact_expire_after\": 0," + + " }" + + " ]" + + " }" + + " ]" + + "}" + )); + + ContactsContactListResponse response = action.execute(); + + assertFalse(response.getList().isEmpty()); + assertEquals(1, response.getCount()); + + Optional contact1 = response.getList().stream().filter( + contactResponse -> contactResponse.getId().equals("0f0f0f0f0f0f0f0f0f0f0f0f") + ).findFirst(); + assertTrue(contact1.isPresent()); + assertEquals("John", contact1.get().getFirstName()); + assertEquals("Doe", contact1.get().getLastName()); + assertEquals("48327201200", contact1.get().getPhoneNumber()); + assertEquals("john.doe@example.com", contact1.get().getEmail()); + assertEquals("undefined", contact1.get().getGender()); + assertEquals("2017-07-21", contact1.get().getBirthdayDate()); + assertEquals("Resource description", contact1.get().getDescription()); + assertEquals("Example City", contact1.get().getCity()); + assertEquals("Example Country", contact1.get().getCountry()); + assertEquals("Example Source", contact1.get().getSource()); + assertEquals("2017-07-21T17:32:28Z", contact1.get().getDateCreated()); + assertEquals("2017-07-21T17:32:28Z", contact1.get().getDateUpdated()); + assertEquals(1, contact1.get().getGroups().getCount()); + + Optional group1 = contact1.get().getGroups().getList().stream().filter( + contactResponse -> contactResponse.getId().equals("0f0f0f0f0f0f0f0f0f0f0f0f") + ).findFirst(); + assertTrue(group1.isPresent()); + assertEquals("Example Group", group1.get().getName()); + assertEquals("Resource description", group1.get().getDescription()); + assertEquals("2017-07-21T17:32:28Z", group1.get().getDateCreated()); + assertEquals("2017-07-21T17:32:28Z", group1.get().getDateUpdated()); + assertEquals("example_username", group1.get().getCreatedBy()); + assertEquals("example-user-provided-id-123", group1.get().getIdx()); + } +} diff --git a/src/test/java/pl/smsapi/test/unit/response/contacts/ContactsGroupListResponseTest.java b/src/test/java/pl/smsapi/test/unit/response/contacts/ContactsGroupListResponseTest.java new file mode 100644 index 0000000..84f3763 --- /dev/null +++ b/src/test/java/pl/smsapi/test/unit/response/contacts/ContactsGroupListResponseTest.java @@ -0,0 +1,94 @@ +package pl.smsapi.test.unit.response.contacts; + +import org.junit.Test; +import pl.smsapi.api.action.contacts.ContactsGroupList; +import pl.smsapi.api.response.contacts.ContactsGroupListResponse; +import pl.smsapi.api.response.contacts.ContactsGroupResponse; +import pl.smsapi.api.response.contacts.ContactsPermissionResponse; +import pl.smsapi.exception.SmsapiException; +import pl.smsapi.test.doubles.ClientStub; +import pl.smsapi.test.doubles.ProxyResponseStub; + +import java.util.Optional; + +import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + +public class ContactsGroupListResponseTest { + + @Test + public void deserialize_empty_response() throws SmsapiException { + ContactsGroupList action = new ContactsGroupList(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "{" + + " \"collection\": []," + + " \"size\": 0" + + "}")); + + ContactsGroupListResponse response = action.execute(); + + assertTrue(response.getList().isEmpty()); + assertEquals(0, response.getCount()); + } + + @Test + public void deserialize_non_empty_response() throws SmsapiException { + ContactsGroupList action = new ContactsGroupList(); + action.client(new ClientStub()); + action.proxy(new ProxyResponseStub( + "{" + + " \"size\": 1," + + " \"collection\": [" + + " {" + + " \"id\": \"0f0f0f0f0f0f0f0f0f0f0f0f\"," + + " \"name\": \"Example Group\"," + + " \"description\": \"Resource description\"," + + " \"contacts_count\": 0," + + " \"date_created\": \"2017-07-21T17:32:28Z\"," + + " \"date_updated\": \"2017-07-21T17:32:28Z\"," + + " \"created_by\": \"example_username\"," + + " \"idx\": \"example-user-provided-id-123\"," + + " \"contact_expire_after\": 0," + + " \"permissions\": [" + + " {" + + " \"group_id\": \"0f0f0f0f0f0f0f0f0f0f0f0f\"," + + " \"username\": \"example_username\"," + + " \"write\": false," + + " \"read\": false," + + " \"send\": false" + + " }" + + " ]" + + " }" + + " ]" + + "}" + )); + + ContactsGroupListResponse response = action.execute(); + + assertFalse(response.getList().isEmpty()); + assertEquals(1, response.getCount()); + + Optional group1 = response.getList().stream().filter( + contactResponse -> contactResponse.getId().equals("0f0f0f0f0f0f0f0f0f0f0f0f") + ).findFirst(); + assertTrue(group1.isPresent()); + assertEquals("Example Group", group1.get().getName()); + assertEquals("Resource description", group1.get().getDescription()); + assertEquals("2017-07-21T17:32:28Z", group1.get().getDateCreated()); + assertEquals("2017-07-21T17:32:28Z", group1.get().getDateUpdated()); + assertEquals("example_username", group1.get().getCreatedBy()); + assertEquals("example-user-provided-id-123", group1.get().getIdx()); + + assertFalse(group1.get().getPermissions().getList().isEmpty()); + assertEquals(1, group1.get().getPermissions().getCount()); + + Optional permission1 = group1.get().getPermissions().getList().stream().filter( + permissionResponse -> permissionResponse.getUsername().equals("example_username") + ).findFirst(); + assertTrue(permission1.isPresent()); + assertFalse(permission1.get().isWrite()); + assertFalse(permission1.get().isRead()); + assertFalse(permission1.get().isSend()); + } +}