Skip to content

Commit bcce349

Browse files
Add unit tests for ContentTypesCallback and FetchResultCallback, verifying onRequestFinish and onRequestFail methods, ensuring correct handling of responses and errors, and testing the always callable functionality to enhance test coverage.
1 parent d469052 commit bcce349

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.contentstack.sdk;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Unit tests for ContentTypesCallback.
9+
*/
10+
public class TestContentTypesCallback {
11+
12+
/**
13+
* Simple concrete implementation for testing.
14+
*/
15+
private static class TestCallback extends ContentTypesCallback {
16+
17+
ContentTypesModel lastContentTypesModel;
18+
Error lastError;
19+
boolean onCompletionCalled = false;
20+
boolean alwaysCalled = false;
21+
22+
@Override
23+
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
24+
onCompletionCalled = true;
25+
lastContentTypesModel = contentTypesModel;
26+
lastError = error;
27+
}
28+
29+
@Override
30+
public void always() {
31+
alwaysCalled = true;
32+
}
33+
}
34+
35+
@Test
36+
public void testOnRequestFinishCallsOnCompletionWithModelAndNullError() {
37+
TestCallback callback = new TestCallback();
38+
// Assuming ContentTypesModel has a public no-arg constructor
39+
ContentTypesModel model = new ContentTypesModel();
40+
41+
callback.onRequestFinish(model);
42+
43+
assertTrue(callback.onCompletionCalled);
44+
assertEquals(model, callback.lastContentTypesModel);
45+
assertNull(callback.lastError);
46+
assertFalse(callback.alwaysCalled);
47+
}
48+
49+
@Test
50+
public void testOnRequestFailCallsOnCompletionWithErrorAndNullModel() {
51+
TestCallback callback = new TestCallback();
52+
Error error = new Error(); // SDK Error with no-arg ctor
53+
54+
callback.onRequestFail(ResponseType.NETWORK, error);
55+
56+
assertTrue(callback.onCompletionCalled);
57+
assertNull(callback.lastContentTypesModel);
58+
assertEquals(error, callback.lastError);
59+
assertFalse(callback.alwaysCalled);
60+
}
61+
62+
@Test
63+
public void testAlwaysCanBeOverridden() {
64+
TestCallback callback = new TestCallback();
65+
66+
callback.always();
67+
68+
assertTrue(callback.alwaysCalled);
69+
assertFalse(callback.onCompletionCalled);
70+
assertNull(callback.lastContentTypesModel);
71+
assertNull(callback.lastError);
72+
}
73+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.contentstack.sdk;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Unit tests for FetchResultCallback.
9+
*/
10+
public class TestFetchResultCallback {
11+
12+
private static class TestCallback extends FetchResultCallback {
13+
14+
ResponseType lastResponseType;
15+
Error lastError;
16+
boolean onCompletionCalled = false;
17+
boolean alwaysCalled = false;
18+
19+
@Override
20+
public void onCompletion(ResponseType responseType, Error error) {
21+
onCompletionCalled = true;
22+
lastResponseType = responseType;
23+
lastError = error;
24+
}
25+
26+
@Override
27+
public void always() {
28+
alwaysCalled = true;
29+
}
30+
}
31+
32+
@Test
33+
public void testOnRequestFinishCallsOnCompletionWithNullError() {
34+
TestCallback callback = new TestCallback();
35+
ResponseType responseType = ResponseType.NETWORK; // or CACHE, etc.
36+
37+
callback.onRequestFinish(responseType);
38+
39+
assertTrue(callback.onCompletionCalled);
40+
assertEquals(responseType, callback.lastResponseType);
41+
assertNull(callback.lastError); // success => null error
42+
assertFalse(callback.alwaysCalled);
43+
}
44+
45+
@Test
46+
public void testOnRequestFailCallsOnCompletionWithError() {
47+
TestCallback callback = new TestCallback();
48+
ResponseType responseType = ResponseType.NETWORK;
49+
Error error = new Error(); // SDK Error has no-arg ctor
50+
51+
callback.onRequestFail(responseType, error);
52+
53+
assertTrue(callback.onCompletionCalled);
54+
assertEquals(responseType, callback.lastResponseType);
55+
assertEquals(error, callback.lastError);
56+
assertFalse(callback.alwaysCalled);
57+
}
58+
59+
@Test
60+
public void testAlwaysOverrideCallable() {
61+
TestCallback callback = new TestCallback();
62+
63+
callback.always();
64+
65+
assertTrue(callback.alwaysCalled);
66+
// onCompletion should not be touched here
67+
assertFalse(callback.onCompletionCalled);
68+
assertNull(callback.lastResponseType);
69+
assertNull(callback.lastError);
70+
}
71+
}

0 commit comments

Comments
 (0)