Skip to content

Commit

Permalink
modify logic to support other datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
hani329 committed Nov 15, 2023
1 parent 0685a81 commit 1bec681
Show file tree
Hide file tree
Showing 8 changed files with 590 additions and 388 deletions.
25 changes: 8 additions & 17 deletions app/src/main/java/com/iterable/androidsdk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,24 @@
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.iterable.iterableapi.CommerceItem;
import com.iterable.iterableapi.IterableApi;
import com.iterable.iterableapi.testapp.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

IterableApi.initialize(this, "17886a58ef2148b0a7b4f3ea9d958e7f");
List<CommerceItem> items = new ArrayList<>();
CommerceItem item = new CommerceItem("12", "Mocha", 3.5, 1);
items.add(item);

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(view -> {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
IterableApi.getInstance().trackPurchase(4.67, items, null);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
package com.iterable.iterableapi.util;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class CriteriaCompletionCheckerTest {

private CriteriaCompletionChecker evaluator;

private final String mockDataWithAnd = "{\n" +
" \"count\":2,\n" +
" \"criteriaList\":[\n" +
" {\n" +
" \"criteriaId\":12345,\n" +
" \"searchQuery\":{\n" +
" \"combinator\":\"Or\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"combinator\":\"And\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"dataType\":\"purchase\",\n" +
" \"searchCombo\":{\n" +
" \"combinator\":\"And\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"field\":\"shoppingCartItems.price\",\n" +
" \"fieldType\":\"double\",\n" +
" \"comparatorType\":\"Equals\",\n" +
" \"dataType\":\"purchase\",\n" +
" \"id\":2,\n" +
" \"value\":\"4.67\"\n" +
" },\n" +
" {\n" +
" \"field\":\"shoppingCartItems.quantity\",\n" +
" \"fieldType\":\"long\",\n" +
" \"comparatorType\":\"GreaterThanOrEqualTo\",\n" +
" \"dataType\":\"purchase\",\n" +
" \"id\":3,\n" +
" \"valueLong\":2,\n" +
" \"value\":\"2\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"criteriaId\":5678,\n" +
" \"searchQuery\":{\n" +
" \"combinator\":\"Or\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"combinator\":\"Or\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"dataType\":\"user\",\n" +
" \"searchCombo\":{\n" +
" \"combinator\":\"And\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"field\":\"itblInternal.emailDomain\",\n" +
" \"fieldType\":\"string\",\n" +
" \"comparatorType\":\"Equals\",\n" +
" \"dataType\":\"user\",\n" +
" \"id\":6,\n" +
" \"value\":\"gmail.com\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"dataType\":\"customEvent\",\n" +
" \"searchCombo\":{\n" +
" \"combinator\":\"And\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"field\":\"eventName\",\n" +
" \"fieldType\":\"string\",\n" +
" \"comparatorType\":\"Equals\",\n" +
" \"dataType\":\"customEvent\",\n" +
" \"id\":9,\n" +
" \"value\":\"processing_cancelled\"\n" +
" },\n" +
" {\n" +
" \"field\":\"createdAt\",\n" +
" \"fieldType\":\"date\",\n" +
" \"comparatorType\":\"GreaterThan\",\n" +
" \"dataType\":\"customEvent\",\n" +
" \"id\":10,\n" +
" \"dateRange\":{\n" +
" \n" +
" },\n" +
" \"isRelativeDate\":false,\n" +
" \"value\":\"1688194800000\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";

private final String mockDataWithOr = "{\n" +
" \"count\":2,\n" +
" \"criteriaList\":[\n" +
" {\n" +
" \"criteriaId\":12345,\n" +
" \"searchQuery\":{\n" +
" \"combinator\":\"Or\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"combinator\":\"And\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"dataType\":\"purchase\",\n" +
" \"searchCombo\":{\n" +
" \"combinator\":\"Or\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"field\":\"shoppingCartItems.price\",\n" +
" \"fieldType\":\"double\",\n" +
" \"comparatorType\":\"Equals\",\n" +
" \"dataType\":\"purchase\",\n" +
" \"id\":2,\n" +
" \"value\":\"4.67\"\n" +
" },\n" +
" {\n" +
" \"field\":\"shoppingCartItems.quantity\",\n" +
" \"fieldType\":\"long\",\n" +
" \"comparatorType\":\"GreaterThanOrEqualTo\",\n" +
" \"dataType\":\"purchase\",\n" +
" \"id\":3,\n" +
" \"valueLong\":2,\n" +
" \"value\":\"2\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"criteriaId\":5678,\n" +
" \"searchQuery\":{\n" +
" \"combinator\":\"Or\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"combinator\":\"Or\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"dataType\":\"user\",\n" +
" \"searchCombo\":{\n" +
" \"combinator\":\"And\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"field\":\"itblInternal.emailDomain\",\n" +
" \"fieldType\":\"string\",\n" +
" \"comparatorType\":\"Equals\",\n" +
" \"dataType\":\"user\",\n" +
" \"id\":6,\n" +
" \"value\":\"gmail.com\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" {\n" +
" \"dataType\":\"customEvent\",\n" +
" \"searchCombo\":{\n" +
" \"combinator\":\"And\",\n" +
" \"searchQueries\":[\n" +
" {\n" +
" \"field\":\"eventName\",\n" +
" \"fieldType\":\"string\",\n" +
" \"comparatorType\":\"Equals\",\n" +
" \"dataType\":\"customEvent\",\n" +
" \"id\":9,\n" +
" \"value\":\"processing_cancelled\"\n" +
" },\n" +
" {\n" +
" \"field\":\"createdAt\",\n" +
" \"fieldType\":\"date\",\n" +
" \"comparatorType\":\"GreaterThan\",\n" +
" \"dataType\":\"customEvent\",\n" +
" \"id\":10,\n" +
" \"dateRange\":{\n" +
" \n" +
" },\n" +
" \"isRelativeDate\":false,\n" +
" \"value\":\"1688194800000\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";

@Before
public void setUp() {
evaluator = new CriteriaCompletionChecker();
}

@Test
public void testCompareDataWithANDCombinatorFail() throws Exception {
JSONArray jsonArray = new JSONArray("[{\"items\":\"[{\\\"id\\\":\\\"12\\\",\\\"name\\\":\\\"Mocha\\\",\\\"price\\\":3.5,\\\"quantity\\\":6}]\",\"createdAt\":1700071052507,\"total\":4.67,\"eventType\":\"purchase\"}]");
boolean result = evaluator.getMatchedCriteria(mockDataWithAnd, jsonArray) != null;
assertFalse(result);
}

@Test
public void testCompareDataWithANDCombinator() throws Exception {
JSONArray jsonArray = new JSONArray("[{\"items\":\"[{\\\"id\\\":\\\"12\\\",\\\"name\\\":\\\"Mocha\\\",\\\"price\\\":4.67,\\\"quantity\\\":3}]\",\"createdAt\":1700071052507,\"total\":4.67,\"eventType\":\"purchase\"}]");
boolean result = evaluator.getMatchedCriteria(mockDataWithAnd, jsonArray) != null;
assertTrue(result);
}

@Test
public void testCompareDataWithORCombinator() throws Exception {
JSONArray jsonArray = new JSONArray("[{\"items\":\"[{\\\"id\\\":\\\"12\\\",\\\"name\\\":\\\"Mocha\\\",\\\"price\\\":3.5,\\\"quantity\\\":3}]\",\"createdAt\":1700071052507,\"total\":4.67,\"eventType\":\"purchase\"}]");
boolean result = evaluator.getMatchedCriteria(mockDataWithOr, jsonArray) != null;
assertTrue(result);
}

@Test
public void testCompareDataWithORCombinatorFail() throws Exception {
JSONArray jsonArray = new JSONArray("[{\"items\":\"[{\\\"id\\\":\\\"12\\\",\\\"name\\\":\\\"Mocha\\\",\\\"price\\\":3.5,\\\"quantity\\\":1}]\",\"createdAt\":1700071052507,\"total\":4.67,\"eventType\":\"purchase\"}]");
boolean result = evaluator.getMatchedCriteria(mockDataWithOr, jsonArray) != null;
assertFalse(result);
}
}
Loading

0 comments on commit 1bec681

Please sign in to comment.