diff --git a/README.md b/README.md index af2f67c..14850cd 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,9 @@ For WhatsApp quick start guide, view [these examples](whatsapp.md). #### Messages API For Messages API quick start guide, view [these examples](messages-api.md). +#### Moments +For Moments quick start guide, view [these examples](moments.md). + ## Ask for help Feel free to open issues on the repository for any encountered problem or feature request. For pull requests, go to the `CONTRIBUTING` [file][contributing] related to it. This code is auto generated, and we are unable to merge any pull requests form here. diff --git a/moments.md b/moments.md index ecb2c78..ce3679e 100644 --- a/moments.md +++ b/moments.md @@ -55,7 +55,9 @@ You can now create an instance of `FormsApi` which allows you to manage your for To get all forms, you can use the following code: ````java - // TODO add code example here + FormsResponse response = formsApi + .getForms() + .execute(); ```` ### Get form by ID @@ -63,7 +65,11 @@ To get all forms, you can use the following code: To get a specific form by ID, you can use the following code: ````java - // TODO add code example here + String formId = "cec5dfd2-4238-48e0-933b-9acbdb2e6f5f"; + + FormsResponseContent response = formsApi + .getForm(formId) + .execute(); ```` ### Increment form view count @@ -82,5 +88,16 @@ To increase the view counter of a specific form, you can use the following code: To submit data to a specific form, you can use the following code: ````java - // TODO add code example here + var firstValue = 26; + var secondValue = true; + + String formId = "cec5dfd2-4238-48e0-933b-9acbdb2e6f5f"; + Map formDataRequest = Map.of( + "firstValue", firstValue, + "secondValue", secondValue + ); + + FormsStatusResponse response = formsApi + .submitFormData(formId, formDataRequest) + .execute(); ```` diff --git a/src/test/java/com/infobip/api/FormsApiTest.java b/src/test/java/com/infobip/api/FormsApiTest.java index 4b61d44..e0ee9df 100644 --- a/src/test/java/com/infobip/api/FormsApiTest.java +++ b/src/test/java/com/infobip/api/FormsApiTest.java @@ -1,29 +1,186 @@ package com.infobip.api; -import static com.infobip.api.util.ResponseStatuses.*; -import static org.assertj.core.api.BDDAssertions.then; - -import com.infobip.model.FormsStatusResponse; +import com.infobip.model.*; import org.junit.jupiter.api.Test; +import java.time.OffsetDateTime; +import java.util.List; import java.util.Map; import java.util.function.Consumer; +import static org.assertj.core.api.BDDAssertions.then; + public class FormsApiTest extends ApiTest { private static final String FORMS = "/forms/1/forms"; private static final String FORM = "/forms/1/forms/{id}"; private static final String FORMS_VIEWS = "/forms/1/forms/{id}/views"; - private static final String FORMS_DATA = "/forms/1/forms/{id}/data"; + private static final String FORMS_SUBMIT = "/forms/1/forms/{id}/data"; @Test - void shouldGetForms() { + void shouldGetAllForms() { + String givenFormId1 = "f23f0f7c-9898-4feb-8f21-5afe2c29db7e"; + String givenFormName1 = "Test form"; + int givenOffset = 0; + int givenLimit = 25; + long givenTotal = 1; + boolean givenResubmitEnabled = true; + FormsType givenFormType = FormsType.OPT_IN; + FormsStatus givenFormStatus = FormsStatus.ACTIVE; + String givenCreatedAt = "2023-02-09T12:00:00+01:00"; + OffsetDateTime givenCreatedAtDateTime = OffsetDateTime.parse(givenCreatedAt); + String givenUpdatedAt = "2023-02-09T12:00:00+01:00"; + OffsetDateTime givenUpdatedAtDateTime = OffsetDateTime.parse(givenUpdatedAt); + FormsComponentType givenComponent = FormsComponentType.TEXT; + String givenFieldId = "last_name"; + String givenPersonField = ""; + String givenLabel = ""; + boolean givenIsHidden = true; + boolean givenIsRequired = true; + String givenPlaceholder = ""; + + String givenResponse = String.format( + "{\n" + + " \"forms\": [\n" + + " {\n" + + " \"id\": \"%s\",\n" + + " \"name\": \"%s\",\n" + + " \"elements\": [\n" + + " {\n" + + " \"component\": \"%s\",\n" + + " \"fieldId\": \"%s\",\n" + + " \"personField\": \"%s\",\n" + + " \"label\": \"%s\",\n" + + " \"isRequired\": %b,\n" + + " \"isHidden\": %b,\n" + + " \"placeholder\": \"%s\"\n" + + " }\n" + + " ],\n" + + " \"createdAt\": \"%s\",\n" + + " \"updatedAt\": \"%s\",\n" + + " \"resubmitEnabled\": %b,\n" + + " \"formType\": \"%s\",\n" + + " \"formStatus\": \"%s\"\n" + + " }\n" + + " ],\n" + + " \"offset\": %d,\n" + + " \"limit\": %d,\n" + + " \"total\": %d\n" + + "}", + givenFormId1, givenFormName1, givenComponent, givenFieldId, givenPersonField, givenLabel, givenIsRequired, givenIsHidden, givenPlaceholder, + givenCreatedAt, givenUpdatedAt, givenResubmitEnabled, givenFormType, givenFormStatus, givenOffset, givenLimit, givenTotal); + + setUpGetRequest(FORMS, Map.of(), givenResponse, 200); + + FormsApi api = new FormsApi(getApiClient()); + + FormsResponse expectedResponse = new FormsResponse() + .forms(List.of( + new FormsResponseContent() + .id(givenFormId1) + .name(givenFormName1) + .elements(List.of( + new FormsElement() + .component(givenComponent) + .fieldId(givenFieldId) + .personField(givenPersonField) + .label(givenLabel) + .isRequired(givenIsRequired) + .isHidden(givenIsHidden) + .placeholder(givenPlaceholder) + )) + .createdAt(givenCreatedAtDateTime) + .updatedAt(givenUpdatedAtDateTime) + .resubmitEnabled(givenResubmitEnabled) + .formType(givenFormType) + .formStatus(givenFormStatus) + )) + .offset(givenOffset) + .limit(givenLimit) + .total(givenTotal); + + Consumer assertions = (response) -> { + then(response).isEqualTo(expectedResponse); + }; + var call = api.getForms(); + testSuccessfulCall(call::execute, assertions); + testSuccessfulAsyncCall(call::executeAsync, assertions); } @Test void shouldGetFormById() { + String givenFormId = "f23f0f7c-9898-4feb-8f21-5afe2c29db7e"; + String givenFormName = "Test form"; + boolean givenResubmitEnabled = true; + FormsType givenFormType = FormsType.OPT_IN; + FormsStatus givenFormStatus = FormsStatus.ACTIVE; + String givenCreatedAt = "2023-02-09T12:00:00+01:00"; + OffsetDateTime givenCreatedAtDateTime = OffsetDateTime.parse(givenCreatedAt); + String givenUpdatedAt = "2023-02-09T12:00:00+01:00"; + OffsetDateTime givenUpdatedAtDateTime = OffsetDateTime.parse(givenUpdatedAt); + FormsComponentType givenComponent = FormsComponentType.TEXT; + String givenFieldId = "last_name"; + String givenPersonField = ""; + String givenLabel = ""; + boolean givenIsHidden = true; + boolean givenIsRequired = true; + String givenPlaceholder = ""; + String givenResponse = String.format( + " {\n" + + " \"id\": \"%s\",\n" + + " \"name\": \"%s\",\n" + + " \"elements\": [\n" + + " {\n" + + " \"component\": \"%s\",\n" + + " \"fieldId\": \"%s\",\n" + + " \"personField\": \"%s\",\n" + + " \"label\": \"%s\",\n" + + " \"isRequired\": %b,\n" + + " \"isHidden\": %b,\n" + + " \"placeholder\": \"%s\"\n" + + " }\n" + + " ],\n" + + " \"createdAt\": \"%s\",\n" + + " \"updatedAt\": \"%s\",\n" + + " \"resubmitEnabled\": %b,\n" + + " \"formType\": \"%s\",\n" + + " \"formStatus\": \"%s\"\n" + + " }", + givenFormId, givenFormName, givenComponent, givenFieldId, givenPersonField, givenLabel, givenIsRequired, givenIsHidden, givenPlaceholder, + givenCreatedAt, givenUpdatedAt, givenResubmitEnabled, givenFormType, givenFormStatus); + + setUpGetRequest(FORM.replace("{id}", givenFormId), Map.of(), givenResponse, 200); + + FormsApi api = new FormsApi(getApiClient()); + + FormsResponseContent expectedResponse = new FormsResponseContent() + .id(givenFormId) + .name(givenFormName) + .elements(List.of( + new FormsElement() + .component(givenComponent) + .fieldId(givenFieldId) + .personField(givenPersonField) + .label(givenLabel) + .isRequired(givenIsRequired) + .isHidden(givenIsHidden) + .placeholder(givenPlaceholder) + )) + .createdAt(givenCreatedAtDateTime) + .updatedAt(givenUpdatedAtDateTime) + .resubmitEnabled(givenResubmitEnabled) + .formType(givenFormType) + .formStatus(givenFormStatus); + + Consumer assertions = (response) -> { + then(response).isEqualTo(expectedResponse); + }; + + var call = api.getForm(givenFormId); + testSuccessfulCall(call::execute, assertions); + testSuccessfulAsyncCall(call::executeAsync, assertions); } @Test @@ -52,6 +209,40 @@ void shouldIncrementFormViewCount() { @Test void shouldSubmitFormData() { + String formId = "12345"; + String givenStatus = "OK"; + String givenResponse = String.format( + "{\n" + + " \"status\": \"%s\"\n" + + "}", givenStatus); + + var givenNumber = 26; + var givenBoolean = true; + + var requestBody = String.format( + "{\n" + + " \"number\": %d,\n" + + " \"boolean\": %b\n" + + "}", givenNumber, givenBoolean); + + setUpPostRequest(FORMS_SUBMIT.replace("{id}", formId), requestBody, givenResponse, 200); + + FormsApi formsApi = new FormsApi(getApiClient()); + + Map formDataRequest = Map.of( + "number", givenNumber, + "boolean", givenBoolean + ); + + Consumer assertions = (response) -> { + then(response).isNotNull(); + then(response.getStatus()).isEqualTo(givenStatus); + }; + + var call = formsApi.submitFormData(formId, formDataRequest); + testSuccessfulCall(call::execute, assertions); + testSuccessfulAsyncCall(call::executeAsync, assertions); } + }