Skip to content

Commit

Permalink
Updated FormsApiTest, updated moments.md, added moments to Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ib-tjuhasz committed Dec 12, 2024
1 parent 70a5637 commit adaa85e
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 20 additions & 3 deletions moments.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ 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

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
Expand All @@ -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<String, Object> formDataRequest = Map.of(
"firstValue", firstValue,
"secondValue", secondValue
);

FormsStatusResponse response = formsApi
.submitFormData(formId, formDataRequest)
.execute();
````
203 changes: 197 additions & 6 deletions src/test/java/com/infobip/api/FormsApiTest.java
Original file line number Diff line number Diff line change
@@ -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<FormsResponse> 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<FormsResponseContent> assertions = (response) -> {
then(response).isEqualTo(expectedResponse);
};

var call = api.getForm(givenFormId);
testSuccessfulCall(call::execute, assertions);
testSuccessfulAsyncCall(call::executeAsync, assertions);
}

@Test
Expand Down Expand Up @@ -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<String, Object> formDataRequest = Map.of(
"number", givenNumber,
"boolean", givenBoolean
);

Consumer<FormsStatusResponse> assertions = (response) -> {
then(response).isNotNull();
then(response.getStatus()).isEqualTo(givenStatus);
};

var call = formsApi.submitFormData(formId, formDataRequest);
testSuccessfulCall(call::execute, assertions);
testSuccessfulAsyncCall(call::executeAsync, assertions);
}

}

0 comments on commit adaa85e

Please sign in to comment.