|
| 1 | +package com.contentstack.sdk; |
| 2 | + |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONException; |
| 5 | +import org.json.JSONObject; |
| 6 | +import org.junit.runners.MethodSorters; |
| 7 | +import org.junit.*; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.lang.reflect.Field; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.concurrent.CountDownLatch; |
| 15 | + |
| 16 | +import static junit.framework.TestCase.*; |
| 17 | + |
| 18 | +import android.content.Context; |
| 19 | +import android.util.Log; |
| 20 | + |
| 21 | +import androidx.test.core.app.ApplicationProvider; |
| 22 | + |
| 23 | +import okhttp3.Request; |
| 24 | +import okhttp3.ResponseBody; |
| 25 | +import retrofit2.Call; |
| 26 | +import retrofit2.Response; |
| 27 | + |
| 28 | + |
| 29 | +public class TaxonomyTestCase { |
| 30 | + |
| 31 | + private static Stack stack; |
| 32 | + private final static String TAG = TaxonomyTestCase.class.getSimpleName(); |
| 33 | + |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void oneTimeSetUp() throws Exception { |
| 37 | + Context appContext = ApplicationProvider.getApplicationContext(); |
| 38 | + Config config = new Config(); |
| 39 | + String DEFAULT_HOST = BuildConfig.host; |
| 40 | + config.setHost(DEFAULT_HOST); |
| 41 | + stack = Contentstack.stack(appContext, BuildConfig.APIKey, BuildConfig.deliveryToken, BuildConfig.environment, config); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testInstance() { |
| 46 | + assertNotNull(stack); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void operationIn() { |
| 51 | + Taxonomy taxonomy = stack.taxonomy(); |
| 52 | + List<String> listOfItems = new ArrayList<>(); |
| 53 | + listOfItems.add("maroon"); |
| 54 | + listOfItems.add("red"); |
| 55 | + Request req = taxonomy.in("taxonomies.color", listOfItems).makeRequest().request(); |
| 56 | + assertEquals("GET", req.method()); |
| 57 | + assertEquals("cdn.contentstack.io", req.url().host()); |
| 58 | + assertEquals("/v3/taxonomies/entries", req.url().encodedPath()); |
| 59 | + assertEquals("query={\"taxonomies.color\":{\"$in\":[\"maroon\",\"red\"]}}", req.url().query()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void operationOr() throws JSONException, IOException { |
| 64 | +// query={ $or: [ |
| 65 | +// { "taxonomies.taxonomy_uid_1" : "term_uid1" }, |
| 66 | +// { "taxonomies.taxonomy_uid_2" : "term_uid2" } |
| 67 | +// ]} |
| 68 | + Taxonomy taxonomy = stack.taxonomy(); |
| 69 | + List<JSONObject> listOfItems = new ArrayList<>(); |
| 70 | + JSONObject item1 = new JSONObject(); |
| 71 | + item1.put("taxonomies.color", "orange"); |
| 72 | + JSONObject item2 = new JSONObject(); |
| 73 | + item2.put("taxonomies.country", "zambia"); |
| 74 | + listOfItems.add(item1); |
| 75 | + listOfItems.add(item2); |
| 76 | + taxonomy.or(listOfItems); |
| 77 | + Request req = taxonomy.makeRequest().request(); |
| 78 | + assertEquals("query={\"$or\":[{\"taxonomies.color\":\"orange\"},{\"taxonomies.country\":\"zambia\"}]}", req.url().query()); |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void operatorAnd() throws JSONException { |
| 84 | + Taxonomy taxonomy = stack.taxonomy(); |
| 85 | + List<JSONObject> listOfItems = new ArrayList<>(); |
| 86 | + JSONObject items1 = new JSONObject(); |
| 87 | + items1.put("taxonomies.color", "green"); |
| 88 | + JSONObject items2 = new JSONObject(); |
| 89 | + items2.put("taxonomies.country", "india"); |
| 90 | + listOfItems.add(items1); |
| 91 | + listOfItems.add(items2); |
| 92 | + taxonomy.and(listOfItems); |
| 93 | + Request req = taxonomy.makeRequest().request(); |
| 94 | + assertEquals("query={\"$and\":[{\"taxonomies.color\":\"green\"},{\"taxonomies.country\":\"india\"}]}", req.url().query()); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + @Test |
| 99 | + public void operationExists() throws IOException { |
| 100 | + Taxonomy taxonomy = stack.taxonomy().exists("taxonomies.color", true); |
| 101 | + Request req = taxonomy.makeRequest().request(); |
| 102 | + assertEquals("query={\"taxonomies.color\":{\"$exists\":true}}", req.url().query()); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + @Test |
| 107 | + public void operationEqualAndBelow() throws IOException { |
| 108 | + Taxonomy taxonomy = stack.taxonomy().equalAndBelow("taxonomies.color", "red"); |
| 109 | + Request req = taxonomy.makeRequest().request(); |
| 110 | + assertEquals("query={\"taxonomies.color\":{\"$eq_below\":\"red\"}}", req.url().query()); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + @Test |
| 115 | + public void operationEqualAbove() { |
| 116 | + Taxonomy taxonomy = stack.taxonomy().equalAbove("taxonomies.appliances", "led"); |
| 117 | + Request req = taxonomy.makeRequest().request(); |
| 118 | + assertEquals("query={\"taxonomies.appliances\":{\"$eq_above\":\"led\"}}", req.url().query()); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void above() { |
| 124 | + Taxonomy taxonomy = stack.taxonomy().above("taxonomies.appliances", "led"); |
| 125 | + Request req = taxonomy.makeRequest().request(); |
| 126 | + assertEquals("query={\"taxonomies.appliances\":{\"$above\":\"led\"}}", req.url().query()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void below() { |
| 131 | + Taxonomy taxonomy = stack.taxonomy().below("taxonomies.appliances", "TV"); |
| 132 | + Request req = taxonomy.makeRequest().request(); |
| 133 | + assertEquals("query={\"taxonomies.appliances\":{\"$below\":\"TV\"}}", req.url().query()); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void aboveAPI() { |
| 138 | + Taxonomy taxonomy = stack.taxonomy().below("taxonomies.color", "red"); |
| 139 | + Request req = taxonomy.makeRequest().request(); |
| 140 | + taxonomy.find(new TaxonomyCallback() { |
| 141 | + @Override |
| 142 | + public void onResponse(JSONObject response, Error error) { |
| 143 | + Log.d("Result",response.toString()); |
| 144 | + } |
| 145 | + }); |
| 146 | + assertEquals("query={\"taxonomies.color\":{\"$below\":\"red\"}}", req.url().query()); |
| 147 | + } |
| 148 | + |
| 149 | + |
| 150 | +} |
| 151 | + |
0 commit comments