Skip to content

Commit d79ec8f

Browse files
Add setLocale method to Asset class and corresponding test case
1 parent 608073d commit d79ec8f

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

contentstack/src/androidTest/java/com/contentstack/sdk/AssetTestCase.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ public void onCompletion(ResponseType responseType, Error error) {
124124
latch.await(5, TimeUnit.SECONDS);
125125
}
126126

127+
@Test
128+
public void test_setLocale_fetch() throws InterruptedException {
129+
final CountDownLatch latch = new CountDownLatch(1);
130+
final Asset asset = stack.asset(assetUid);
131+
asset.setLocale("en-us");
132+
asset.fetch(new FetchResultCallback() {
133+
@Override
134+
public void onCompletion(ResponseType responseType, Error error) {
135+
assertNotNull(asset.getAssetUid());
136+
latch.countDown();
137+
}
138+
});
139+
latch.await(5, TimeUnit.SECONDS);
140+
assertEquals("Query was not completed in time", 0, latch.getCount());
141+
}
142+
127143
@Test
128144
public void test_include_branch() {
129145
final Asset asset = stack.asset(assetUid);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,4 +616,22 @@ public Asset includeBranch() {
616616
return this;
617617
}
618618

619+
/**
620+
* <p>
621+
* <br><br><b>Example :</b><br>
622+
* <pre class="prettyprint">
623+
* Asset asset = asset.setLocale("en-hi");
624+
* </pre>
625+
* </p>
626+
*/
627+
public Asset setLocale(String locale) {
628+
if (locale != null) {
629+
try {
630+
urlQueries.put("locale", locale);
631+
} catch (JSONException e) {
632+
Log.e(TAG, e.getLocalizedMessage());
633+
}
634+
}
635+
return this;
636+
}
619637
}

contentstack/src/test/java/com/contentstack/sdk/TestAssetAdvanced.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,37 @@ public void testAddParamOverwrite() {
189189
assertNotNull(asset);
190190
}
191191

192+
// ==================== SET LOCALE Tests ====================
193+
194+
@Test
195+
public void testSetLocale() {
196+
Asset result = asset.setLocale("en-us");
197+
assertNotNull(result);
198+
assertSame(asset, result);
199+
assertEquals("en-us", asset.urlQueries.optString("locale", ""));
200+
}
201+
202+
@Test
203+
public void testSetLocaleReturnsThis() {
204+
Asset result = asset.setLocale("en-hi");
205+
assertSame(asset, result);
206+
}
207+
208+
@Test
209+
public void testSetLocaleWithNull() {
210+
asset.setLocale("en-us");
211+
Asset result = asset.setLocale(null);
212+
assertSame(asset, result);
213+
assertEquals("en-us", asset.urlQueries.optString("locale", ""));
214+
}
215+
216+
@Test
217+
public void testSetLocaleChainedWithFetch() {
218+
asset.setLocale("en-us").includeFallback();
219+
assertTrue(asset.urlQueries.has("locale"));
220+
assertEquals("en-us", asset.urlQueries.optString("locale", ""));
221+
}
222+
192223
// ==================== GET METHODS Tests ====================
193224

194225
@Test

0 commit comments

Comments
 (0)