From 2919d58136ebdaa4ff47e704ea63abc8cd906c75 Mon Sep 17 00:00:00 2001 From: Diogo Moreira Date: Mon, 17 Apr 2017 15:07:32 +0100 Subject: [PATCH] Add support for the documents endpoint --- .../test/integration/model/UserTest.java | 52 ++++++++++++++++ .../model/user/DocumentRequestTest.java | 35 +++++++++++ .../integration/model/user/DocumentTest.java | 35 +++++++++++ .../integration/service/UserServiceTest.java | 59 ++++++++++++++++--- .../test/util/Fixtures.java | 19 ++++++ .../uphold/uphold_android_sdk/model/User.java | 35 ++++++++++- .../model/user/Document.java | 46 +++++++++++++++ .../model/user/DocumentRequest.java | 46 +++++++++++++++ .../service/UserService.java | 21 +++++++ .../unit/model/user/DocumentRequestTest.java | 29 +++++++++ .../test/unit/model/user/DocumentTest.java | 29 +++++++++ 11 files changed, 398 insertions(+), 8 deletions(-) create mode 100644 src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentRequestTest.java create mode 100644 src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentTest.java create mode 100644 src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/Document.java create mode 100644 src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/DocumentRequest.java create mode 100644 src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentRequestTest.java create mode 100644 src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentTest.java diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/UserTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/UserTest.java index 8183ec8..09d5ec6 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/UserTest.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/UserTest.java @@ -15,6 +15,7 @@ import com.uphold.uphold_android_sdk.model.card.CardRequest; import com.uphold.uphold_android_sdk.model.card.Settings; import com.uphold.uphold_android_sdk.model.user.Contact; +import com.uphold.uphold_android_sdk.model.user.Document; import com.uphold.uphold_android_sdk.model.user.Phone; import com.uphold.uphold_android_sdk.paginator.Paginator; import com.uphold.uphold_android_sdk.test.BuildConfig; @@ -129,6 +130,31 @@ public Promise call(UpholdRestAdapter adapter) { Assert.assertEquals(contact.getName(), "Foo Bar"); } + @Test + public void createDocumentShouldReturnTheDocument() throws Exception { + String responseString = "{\"type\": \"foo\", \"value\": \"bar\"}"; + MockRestAdapter adapter = new MockRestAdapter<>("foobar", responseString, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter adapter) { + User user = Fixtures.loadUser(); + + user.setUpholdRestAdapter(adapter); + + return user.createDocument(Fixtures.loadDocumentRequest()); + } + }); + + Document document = adapter.getResult(); + Request request = adapter.getRequest(); + + Assert.assertEquals(request.getMethod(), "POST"); + Assert.assertEquals(request.getUrl(), String.format("%s/v0/me/documents", BuildConfig.API_SERVER_URL)); + Assert.assertEquals(document.getType(), "foo"); + Assert.assertEquals(document.getValue(), "bar"); + } + @Test public void getAccountByIdShouldReturnTheAccountWithId() throws Exception { String responseString = "{ \"currency\": \"FOO\", \"id\": \"FOOBAR\", \"label\": \"BAR\", \"status\": \"FOO BAR\", \"type\": \"Bar\" }"; @@ -410,6 +436,32 @@ public void getCurrenciesShouldReturnTheListOfCurrencies() { Assert.assertEquals(user.getCurrencies().get(2), "EUR"); } + @Test + public void getDocumentsShouldReturnTheListOfDocuments() throws Exception { + String responseString = "[{\"type\": \"foo\", \"value\": \"bar\"}]"; + MockRestAdapter> adapter = new MockRestAdapter<>("foobar", responseString, null); + + adapter.request(new RepromiseFunction>() { + @Override + public Promise> call(UpholdRestAdapter adapter) { + User user = Fixtures.loadUser(); + + user.setUpholdRestAdapter(adapter); + + return user.getDocuments(); + } + }); + + List documents = adapter.getResult(); + Request request = adapter.getRequest(); + + Assert.assertEquals(request.getMethod(), "GET"); + Assert.assertEquals(request.getUrl(), String.format("%s/v0/me/documents", BuildConfig.API_SERVER_URL)); + Assert.assertEquals(documents.size(), 1); + Assert.assertEquals(documents.get(0).getType(), "foo"); + Assert.assertEquals(documents.get(0).getValue(), "bar"); + } + @Test public void getEmailShouldReturnTheEmail() { User user = Fixtures.loadUser(new HashMap() {{ diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentRequestTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentRequestTest.java new file mode 100644 index 0000000..5580e5e --- /dev/null +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentRequestTest.java @@ -0,0 +1,35 @@ +package com.uphold.uphold_android_sdk.test.integration.model.user; + +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.SmallTest; + +import com.uphold.uphold_android_sdk.model.user.DocumentRequest; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * DocumentRequest integration tests. + */ + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class DocumentRequestTest { + + @Test + public void getTypeShouldReturnTheType() { + DocumentRequest documentRequest = new DocumentRequest("foo", "bar"); + + Assert.assertEquals(documentRequest.getType(), "foo"); + } + + @Test + public void getValueShouldReturnTheValue() { + DocumentRequest documentRequest = new DocumentRequest("foo", "bar"); + + Assert.assertEquals(documentRequest.getValue(), "bar"); + } + +} diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentTest.java new file mode 100644 index 0000000..ee2f620 --- /dev/null +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/user/DocumentTest.java @@ -0,0 +1,35 @@ +package com.uphold.uphold_android_sdk.test.integration.model.user; + +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.SmallTest; + +import com.uphold.uphold_android_sdk.model.user.Document; + +import junit.framework.Assert; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Document integration tests. + */ + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class DocumentTest { + + @Test + public void getTypeShouldReturnTheType() { + Document document = new Document("foo", "bar"); + + Assert.assertEquals(document.getType(), "foo"); + } + + @Test + public void getValueShouldReturnTheValue() { + Document document = new Document("foo", "bar"); + + Assert.assertEquals(document.getValue(), "bar"); + } + +} diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserServiceTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserServiceTest.java index c3bd2ad..ed81703 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserServiceTest.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserServiceTest.java @@ -1,27 +1,28 @@ package com.uphold.uphold_android_sdk.test.integration.service; +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.SmallTest; + import com.darylteo.rx.promises.java.Promise; import com.darylteo.rx.promises.java.functions.RepromiseFunction; - -import junit.framework.Assert; - import com.uphold.uphold_android_sdk.client.restadapter.UpholdRestAdapter; import com.uphold.uphold_android_sdk.client.retrofitpromise.RetrofitPromise; import com.uphold.uphold_android_sdk.model.Balance; import com.uphold.uphold_android_sdk.model.Transaction; import com.uphold.uphold_android_sdk.model.User; import com.uphold.uphold_android_sdk.model.user.Contact; +import com.uphold.uphold_android_sdk.model.user.Document; import com.uphold.uphold_android_sdk.model.user.Phone; import com.uphold.uphold_android_sdk.service.UserService; -import com.uphold.uphold_android_sdk.test.util.Fixtures; import com.uphold.uphold_android_sdk.test.BuildConfig; +import com.uphold.uphold_android_sdk.test.util.Fixtures; import com.uphold.uphold_android_sdk.test.util.MockRestAdapter; + +import junit.framework.Assert; + import org.junit.Test; import org.junit.runner.RunWith; -import android.support.test.runner.AndroidJUnit4; -import android.test.suitebuilder.annotation.SmallTest; - import java.util.HashMap; import java.util.List; @@ -58,6 +59,50 @@ public Promise call(UpholdRestAdapter upholdRestAdapter) { Assert.assertEquals(request.getUrl(), String.format("%s/v0/me/contacts", BuildConfig.API_SERVER_URL)); } + @Test + public void createDocumentShouldReturnTheRequest() throws Exception { + final MockRestAdapter adapter = new MockRestAdapter<>(null, null, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter upholdRestAdapter) { + UserService userService = adapter.getRestAdapter().create(UserService.class); + RetrofitPromise promise = new RetrofitPromise<>(); + + userService.createDocument(Fixtures.loadDocumentRequest(), promise); + + return promise; + } + }); + + Request request = adapter.getRequest(); + + Assert.assertEquals(request.getMethod(), "POST"); + Assert.assertEquals(request.getUrl(), String.format("%s/v0/me/documents", BuildConfig.API_SERVER_URL)); + } + + @Test + public void getDocumentsShouldReturnTheRequest() throws Exception { + final MockRestAdapter> adapter = new MockRestAdapter<>(null, null, null); + + adapter.request(new RepromiseFunction>() { + @Override + public Promise> call(UpholdRestAdapter upholdRestAdapter) { + UserService userService = adapter.getRestAdapter().create(UserService.class); + RetrofitPromise> promise = new RetrofitPromise<>(); + + userService.getDocuments(promise); + + return promise; + } + }); + + Request request = adapter.getRequest(); + + Assert.assertEquals(request.getMethod(), "GET"); + Assert.assertEquals(request.getUrl(), String.format("%s/v0/me/documents", BuildConfig.API_SERVER_URL)); + } + @Test public void getUserShouldReturnTheRequest() throws Exception { final MockRestAdapter adapter = new MockRestAdapter<>(null, null, null); diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/util/Fixtures.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/util/Fixtures.java index 114476f..dbe2c21 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/util/Fixtures.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/util/Fixtures.java @@ -18,6 +18,7 @@ import com.uphold.uphold_android_sdk.model.transaction.TransactionDepositRequest; import com.uphold.uphold_android_sdk.model.transaction.TransactionTransferRequest; import com.uphold.uphold_android_sdk.model.user.ContactRequest; +import com.uphold.uphold_android_sdk.model.user.DocumentRequest; import com.uphold.uphold_android_sdk.model.user.Settings; import com.uphold.uphold_android_sdk.model.user.settings.InternationalizationUserSettings; import com.uphold.uphold_android_sdk.model.user.settings.Otp; @@ -127,6 +128,24 @@ public static ContactRequest loadContactRequest(HashMap fields) return new ContactRequest(addresses, fakerFields.get("company"), emails, fakerFields.get("firstName"), fakerFields.get("lastName")); } + public static DocumentRequest loadDocumentRequest() { + return loadDocumentRequest(null); + } + + public static DocumentRequest loadDocumentRequest(HashMap fields) { + final Faker faker = new Faker(); + final HashMap fakerFields = new HashMap() {{ + put("type", faker.lorem().fixedString(5)); + put("value", faker.lorem().fixedString(5)); + }}; + + if (fields != null) { + fakerFields.putAll(fields); + } + + return new DocumentRequest(fakerFields.get("type"), fakerFields.get("value")); + } + public static Transaction loadTransaction() { return loadTransaction(null); } diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/User.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/User.java index db0d30b..1e86d5c 100644 --- a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/User.java +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/User.java @@ -2,7 +2,6 @@ import com.darylteo.rx.promises.java.Promise; import com.darylteo.rx.promises.java.functions.PromiseFunction; - import com.uphold.uphold_android_sdk.client.retrofitpromise.RetrofitPaginatorPromise; import com.uphold.uphold_android_sdk.client.retrofitpromise.RetrofitPromise; import com.uphold.uphold_android_sdk.model.balance.Currency; @@ -10,6 +9,8 @@ import com.uphold.uphold_android_sdk.model.card.CardRequest; import com.uphold.uphold_android_sdk.model.user.Contact; import com.uphold.uphold_android_sdk.model.user.ContactRequest; +import com.uphold.uphold_android_sdk.model.user.Document; +import com.uphold.uphold_android_sdk.model.user.DocumentRequest; import com.uphold.uphold_android_sdk.model.user.Phone; import com.uphold.uphold_android_sdk.model.user.Settings; import com.uphold.uphold_android_sdk.model.user.Status; @@ -114,6 +115,23 @@ public Contact call(Contact contact) { }); } + /** + * Creates a document for the user. + * + * @param documentRequest The {@link DocumentRequest} with the information to create the document. + * + * @return a {@link Promise< Document >} with the document created. + */ + + public Promise createDocument(DocumentRequest documentRequest) { + RetrofitPromise promise = new RetrofitPromise<>(); + UserService userService = this.getUpholdRestAdapter().create(UserService.class); + + userService.createDocument(documentRequest, promise); + + return promise; + } + /** * Gets the user's accounts. * @@ -299,6 +317,21 @@ public List getCurrencies() { return currencies; } + /** + * Gets the user documents. + * + * @return a promise {@link Promise>} with the list of user documents. + */ + + public Promise> getDocuments() { + RetrofitPromise> promise = new RetrofitPromise<>(); + UserService userService = this.getUpholdRestAdapter().create(UserService.class); + + userService.getDocuments(promise); + + return promise; + } + /** * Gets the user email. * diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/Document.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/Document.java new file mode 100644 index 0000000..01f6442 --- /dev/null +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/Document.java @@ -0,0 +1,46 @@ +package com.uphold.uphold_android_sdk.model.user; + +import java.io.Serializable; + +/** + * Document model. + */ + +public class Document implements Serializable { + + private final String type; + private final String value; + + /** + * Constructor. + * + * @param type The document type. + * @param value The document value. + */ + + public Document(String type, String value) { + this.type = type; + this.value = value; + } + + /** + * Gets the document type. + * + * @return the document type. + */ + + public String getType() { + return type; + } + + /** + * Gets the document value. + * + * @return the document value. + */ + + public String getValue() { + return value; + } + +} diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/DocumentRequest.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/DocumentRequest.java new file mode 100644 index 0000000..8623623 --- /dev/null +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/user/DocumentRequest.java @@ -0,0 +1,46 @@ +package com.uphold.uphold_android_sdk.model.user; + +import java.io.Serializable; + +/** + * Document request model. + */ + +public class DocumentRequest implements Serializable { + + private final String type; + private final String value; + + /** + * Constructor. + * + * @param type The document type. + * @param value The document value. + */ + + public DocumentRequest(String type, String value) { + this.type = type; + this.value = value; + } + + /** + * Gets the document type. + * + * @return the document type. + */ + + public String getType() { + return type; + } + + /** + * Gets the document value. + * + * @return the document value. + */ + + public String getValue() { + return value; + } + +} diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/service/UserService.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/service/UserService.java index 78e7f0a..734c499 100644 --- a/src/app/src/main/java/com/uphold/uphold_android_sdk/service/UserService.java +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/service/UserService.java @@ -5,6 +5,8 @@ import com.uphold.uphold_android_sdk.model.User; import com.uphold.uphold_android_sdk.model.user.Contact; import com.uphold.uphold_android_sdk.model.user.ContactRequest; +import com.uphold.uphold_android_sdk.model.user.Document; +import com.uphold.uphold_android_sdk.model.user.DocumentRequest; import com.uphold.uphold_android_sdk.model.user.Phone; import java.util.HashMap; @@ -33,6 +35,25 @@ public interface UserService { @POST("/v0/me/contacts") void createContact(@Body ContactRequest contact, Callback callback); + /** + * Creates a request to create a document. + * + * @param document The document information. + * @param callback A callback to receive the request information. + */ + + @POST("/v0/me/documents") + void createDocument(@Body DocumentRequest document, Callback callback); + + /** + * Performs a request to get the user documents. + * + * @param callback A callback to receive the request information. + */ + + @GET("/v0/me/documents") + void getDocuments(Callback> callback); + /** * Performs a request to get the user information. * diff --git a/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentRequestTest.java b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentRequestTest.java new file mode 100644 index 0000000..2283d17 --- /dev/null +++ b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentRequestTest.java @@ -0,0 +1,29 @@ +package com.uphold.uphold_android_sdk.test.unit.model.user; + +import com.uphold.uphold_android_sdk.model.user.DocumentRequest; + +import junit.framework.Assert; + +import org.apache.commons.lang3.SerializationUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * DocumentRequest unit tests. + */ + +@RunWith(JUnit4.class) +public class DocumentRequestTest { + + @Test + public void shouldBeSerializable() { + DocumentRequest documentRequest = new DocumentRequest("foo", "bar"); + byte[] serializedDocumentRequest = SerializationUtils.serialize(documentRequest); + DocumentRequest deserializedDocumentRequest = SerializationUtils.deserialize(serializedDocumentRequest); + + Assert.assertEquals(documentRequest.getType(), deserializedDocumentRequest.getType()); + Assert.assertEquals(documentRequest.getValue(), deserializedDocumentRequest.getValue()); + } + +} \ No newline at end of file diff --git a/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentTest.java b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentTest.java new file mode 100644 index 0000000..0f0e1a9 --- /dev/null +++ b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/user/DocumentTest.java @@ -0,0 +1,29 @@ +package com.uphold.uphold_android_sdk.test.unit.model.user; + +import com.uphold.uphold_android_sdk.model.user.Document; + +import junit.framework.Assert; + +import org.apache.commons.lang3.SerializationUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Document unit tests. + */ + +@RunWith(JUnit4.class) +public class DocumentTest { + + @Test + public void shouldBeSerializable() { + Document document = new Document("foo", "bar"); + byte[] serializedDocument = SerializationUtils.serialize(document); + Document deserializedDocument = SerializationUtils.deserialize(serializedDocument); + + Assert.assertEquals(document.getType(), deserializedDocument.getType()); + Assert.assertEquals(document.getValue(), deserializedDocument.getValue()); + } + +}