Skip to content

Commit ca63af0

Browse files
committed
enh: Taxonomy implementation and testcases
1 parent d89986a commit ca63af0

File tree

5 files changed

+621
-1
lines changed

5 files changed

+621
-1
lines changed

contentstack/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ android {
6767
exclude("META-INF/notice.txt")
6868
exclude("META-INF/ASL2.0")
6969
exclude("META-INF/*.kotlin_module")
70+
exclude("META-INF/LICENSE.md")
71+
exclude("META-INF/LICENSE-notice.md")
7072
}
7173

7274
testOptions {
@@ -99,7 +101,7 @@ android {
99101
defaultConfig {
100102
// Required when setting minSdkVersion to 20 or lower
101103
multiDexEnabled true
102-
minSdkVersion 23
104+
minSdk 24
103105
versionCode 1
104106
versionName "1.0"
105107
useLibrary 'org.apache.http.legacy'
@@ -154,6 +156,12 @@ dependencies {
154156

155157
// implementation 'com.squareup.okio:okio:3.9.0'
156158
implementation 'com.github.rjeschke:txtmark:0.12'
159+
// // Retrofit
160+
implementation("com.squareup.retrofit2:retrofit:2.9.0")
161+
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
162+
// // OkHttp
163+
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
164+
// implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'
157165
}
158166
tasks.register('clearJar', Delete) { delete 'build/libs/contentstack.jar' }
159167
tasks.register('unzip', Copy) {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+

contentstack/src/main/java/com/contentstack/sdk/Config.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import android.text.TextUtils;
44

5+
import java.net.Proxy;
56
import java.util.Objects;
7+
import java.util.concurrent.TimeUnit;
8+
9+
import okhttp3.ConnectionPool;
610

711

812
/**
@@ -17,6 +21,10 @@ public class Config {
1721
protected String environment = null;
1822
protected String branch = null;
1923
protected String[] earlyAccess = null;
24+
protected Proxy proxy = null;
25+
protected ConnectionPool connectionPool = new ConnectionPool();
26+
protected String endpoint;
27+
2028

2129

2230
/**
@@ -179,5 +187,55 @@ public String getEnvironment() {
179187
return environment;
180188
}
181189

190+
/**
191+
* Proxy can be set like below.
192+
*
193+
* @param proxy Proxy setting, typically a type (http, socks) and a socket address. A Proxy is an immutable object
194+
* <br>
195+
* <br>
196+
* <b>Example:</b><br>
197+
* <br>
198+
* <code>
199+
* java.net.Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", "proxyPort"));
200+
* java.net.Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("sl.theproxyvpn.io", 80)); Config
201+
* config = new Config(); config.setProxy(proxy);
202+
* </code>
203+
*/
204+
public void setProxy(Proxy proxy) {
205+
this.proxy = proxy;
206+
}
207+
208+
/**
209+
* Returns the Proxy instance
210+
*
211+
* @return Proxy
212+
*/
213+
public Proxy getProxy() {
214+
return this.proxy;
215+
}
216+
217+
/**
218+
* Manages reuse of HTTP and HTTP/2 connections for reduced network latency. HTTP requests that * share the same
219+
* {@link okhttp3.Address} may share a {@link okhttp3.Connection}. This class implements the policy * of which
220+
* connections to keep open for future use.
221+
*
222+
* @param maxIdleConnections the maxIdleConnections default value is 5
223+
* @param keepAliveDuration the keepAliveDuration default value is 5
224+
* @param timeUnit the timeUnit default value is TimeUnit. MINUTES
225+
* @return ConnectionPool
226+
*/
227+
public ConnectionPool connectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
228+
this.connectionPool = new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit);
229+
return this.connectionPool;
230+
}
231+
232+
protected String getEndpoint() {
233+
return endpoint + "/" + getVersion() + "/";
234+
}
235+
236+
protected void setEndpoint(String endpoint) {
237+
this.endpoint = endpoint;
238+
}
239+
182240

183241
}

contentstack/src/main/java/com/contentstack/sdk/Stack.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.json.JSONObject;
1010

1111
import java.io.UnsupportedEncodingException;
12+
import java.net.Proxy;
1213
import java.net.URLEncoder;
1314
import java.text.DateFormat;
1415
import java.text.SimpleDateFormat;
@@ -22,6 +23,11 @@
2223
import java.util.Objects;
2324
import java.util.TimeZone;
2425

26+
import okhttp3.ConnectionPool;
27+
import okhttp3.OkHttpClient;
28+
import retrofit2.Retrofit;
29+
import retrofit2.converter.gson.GsonConverterFactory;
30+
2531
/**
2632
* To fetch stack level information of your application from Contentstack server.
2733
* <p>
@@ -41,6 +47,7 @@ public class Stack implements INotifyClass {
4147
protected Config config;
4248
protected ArrayMap<String, Object> headerGroupApp;
4349
protected JSONObject syncParams = null;
50+
protected Retrofit retrofit;
4451
protected String skip = null;
4552
protected String limit = null;
4653
protected String localeCode;
@@ -87,10 +94,29 @@ protected void setConfig(Config config) {
8794
}
8895
}
8996
}
97+
String endpoint = config.PROTOCOL + config.URL;
98+
this.config.setEndpoint(endpoint);
99+
client(endpoint);
100+
101+
}
90102

103+
private void client(String endpoint) {
104+
Proxy proxy = this.config.getProxy();
105+
ConnectionPool pool = this.config.connectionPool;
106+
OkHttpClient client = new OkHttpClient.Builder()
107+
.proxy(proxy)
108+
.connectionPool(pool)
109+
.build();
110+
111+
Retrofit retrofit = new Retrofit.Builder().baseUrl(endpoint)
112+
.client(client)
113+
.build();
114+
115+
this.service = retrofit.create(APIService.class);
91116
}
92117

93118

119+
94120
/**
95121
* Represents a {@link ContentType}.<br>
96122
* Create {@link ContentType} instance.
@@ -163,6 +189,15 @@ public AssetLibrary assetLibrary() {
163189
return library;
164190
}
165191

192+
/**
193+
* Create {@link Taxonomy} instance.
194+
* @return
195+
*/
196+
public Taxonomy taxonomy(){
197+
return new Taxonomy(this.service,this.config,this.localHeader);
198+
}
199+
200+
166201
/**
167202
* Get stack application key
168203
*

0 commit comments

Comments
 (0)