Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Support of Arrays in followupEvent #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions libai/src/main/java/ai/api/model/AIEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@

package ai.api.model;

import com.google.gson.annotations.SerializedName;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;

public class AIEvent implements Serializable {
private static final long serialVersionUID = 1L;

@SerializedName("name")
private String name;

@SerializedName("data")
private Map<String, String> data;
private Map<String, JsonElement> data;

public AIEvent() {

Expand All @@ -53,33 +56,50 @@ public void setName(String name) {
/**
* String data map
*/
public Map<String, String> getData() {
public Map<String, JsonElement> getData() {
return data;
}

public void setData(Map<String, String> data) {
public void setData(Map<String, JsonElement> data) {
this.data = data;
}

public void addDataField(String key, String value) {
if (data == null)
setData(new HashMap<String, String>());
data.put(key, value);
setData(new HashMap<String, JsonElement>());
data.put(key, new Gson().toJsonTree(value));
}

public void addDataField(Map<String, String> dataParams) {
if (data == null)
setData(new HashMap<String, String>());
data.putAll(dataParams);
setData(new HashMap<String, JsonElement>());

if (dataParams != null) {
for (Map.Entry<String, String> entry : dataParams.entrySet()) {
this.data.put(entry.getKey(), new Gson().toJsonTree(entry.getValue()));
}
}
}

public void addDataCollection(String key, Collection<String> collection) {
if (data == null)
setData(new HashMap<String, JsonElement>());

data.put(key, new Gson().toJsonTree(collection));
}

public String getDataField(final String name) {
return getDataField(name, "");
}

public String getDataField(final String name, final String defaultValue) {
if (data.containsKey(name)) {
return data.get(name);
if (data.containsKey(name) && data.get(name) != null) {
if(data.get(name).isJsonArray()) {
return data.get(name).toString();
} else {
return data.get(name).getAsString();
}

}
return defaultValue;
}
Expand Down
37 changes: 30 additions & 7 deletions libai/src/test/java/ai/api/model/AIEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,20 @@

package ai.api.model;

import ai.api.GsonFactory;
import com.google.gson.Gson;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

import ai.api.GsonFactory;

/**
* Created by alexey on 07/12/2016.
Expand All @@ -43,14 +47,22 @@ public class AIEventTest {
" \"poker\": \"face\""+
" }\n" +
"}";

private static final String ONE_EVENT_COLLECTION = "{\n" +
" \"name\": \"joke\",\n" +
" \"data\": {" +
" \"poker\": [\"face\",\"hand\"] "+
" }\n" +
"}";

private static final String EVENTS_ARRAY = "{\n" +
" \"name\": \"test\",\n" +
" \"data\": {" +
" \"heart\": \"ace\","+
" \"diamond\": \"King\","+
" \"club\": \"queen\","+
" \"spade\": \"Jack\""+
" \"spade\": \"Jack\","+
" \"poker\": [\"face\",\"hand\"] "+
" }\n" +
"}";

Expand All @@ -70,6 +82,16 @@ public void OneEvent() {
assertEquals("joke", aiEvent.getName());
assertEquals("face", aiEvent.getDataField("poker"));
}

@Test
public void OneEventCollection() {
final AIEvent aiEvent = gson.fromJson(ONE_EVENT_COLLECTION, AIEvent.class);
List<String> pokerBits = new ArrayList<>();
pokerBits.add("face");
pokerBits.add("hand");
assertEquals("joke", aiEvent.getName());
assertEquals("[\"face\",\"hand\"]", aiEvent.getDataField("poker"));
}

@Test
public void ListEvents() {
Expand All @@ -79,12 +101,13 @@ public void ListEvents() {
assertEquals("King", aiEvent.getDataField("diamond"));
assertEquals("queen", aiEvent.getDataField("club"));
assertEquals("Jack", aiEvent.getDataField("spade"));
assertEquals("[\"face\",\"hand\"]", aiEvent.getDataField("poker"));
}

@Test
public void CreateEmptyEvent() {
final AIEvent aiEvent = new AIEvent("test");
aiEvent.setData(new HashMap<String, String>());
aiEvent.setData(new HashMap<String, JsonElement>());
assertEquals("{\"name\":\"test\",\"data\":{}}", gson.toJson(aiEvent));
}

Expand Down
9 changes: 5 additions & 4 deletions libai/src/test/java/ai/api/model/FulfillmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class FulfillmentTest {
"{\"speech\":\"text\"," + "\"displayText\":\"DisplayText\", \"source\":\"webhook\", "
+ "\"contextOut\": [{\"name\":\"weather\", \"lifespan\":2, \"parameters\":{\"city\":\"Rome\"}}],"
+ "\"data\":{\"param\":\"value\"},"
+ "\"followupEvent\":{\"data\":{\"event-param\":\"event-value\"},\"name\":\"test-event\"}}";
+ "\"followupEvent\":{\"data\":{\"event-param\":\"event-value\",\"event-param-array\":[\"event-value1\",\"event-value2\"]},\"name\":\"test-event\"}}";

private static final String TEST_FULFILLMENT_WITH_MESSAGES = "{\"speech\":\"test speech\","+
"\"messages\":[{\"imageUrl\":\"url image\",\"type\":3},{\"title\":\"Quick title\","+
Expand Down Expand Up @@ -93,9 +93,10 @@ public void testDeserializationWebhookResponse() {

assertNotNull(fulfillment.getFollowupEvent());
assertEquals("test-event", fulfillment.getFollowupEvent().getName());
assertEquals(1, fulfillment.getFollowupEvent().getData().size());
assertEquals("event-value", fulfillment.getFollowupEvent().getData().get("event-param"));
}
assertEquals(2, fulfillment.getFollowupEvent().getData().size());
assertEquals("event-value", fulfillment.getFollowupEvent().getData().get("event-param").getAsString());
assertEquals("[\"event-value1\",\"event-value2\"]", fulfillment.getFollowupEvent().getData().get("event-param-array").getAsJsonArray().toString());
}

@Test
public void testSerializationWithMessages() {
Expand Down