Skip to content

Commit 4704fed

Browse files
fix: remove erroneous error if project id and sdk key are null. (#358)
* remove erroneous error if project id and sdk key are null. move check to datafileConfig check. * add some more unit tests for builder with datafile config and sdk key
1 parent 8c37f7e commit 4704fed

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerBuilderTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@
2121
import com.optimizely.ab.OptimizelyRuntimeException;
2222
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
2323
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
24+
import com.optimizely.ab.android.shared.DatafileConfig;
2425
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
2526
import com.optimizely.ab.error.ErrorHandler;
2627
import org.junit.Before;
2728
import org.junit.Test;
2829
import org.junit.runner.RunWith;
2930
import org.slf4j.Logger;
31+
32+
import java.util.concurrent.TimeUnit;
33+
3034
import static junit.framework.Assert.assertNotNull;
35+
import static org.junit.Assert.assertNull;
3136
import static org.mockito.Mockito.mock;
3237

3338
@RunWith(AndroidJUnit4.class)
@@ -66,6 +71,78 @@ public <T extends OptimizelyRuntimeException> void handleError(T exception) thro
6671
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getInstrumentation().getTargetContext()));
6772
}
6873

74+
@Test
75+
public void testBuilderWithOutDatafileConfig() {
76+
ErrorHandler errorHandler = new ErrorHandler() {
77+
@Override
78+
public <T extends OptimizelyRuntimeException> void handleError(T exception) throws T {
79+
logger.error("Inside error handler", exception);
80+
}
81+
};
82+
83+
OptimizelyManager manager = OptimizelyManager.builder().withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getInstrumentation().getTargetContext()))
84+
.withDatafileDownloadInterval(30L, TimeUnit.MINUTES)
85+
.withEventDispatchInterval(30L, TimeUnit.MINUTES)
86+
.withDatafileHandler(new DefaultDatafileHandler())
87+
.withErrorHandler(errorHandler)
88+
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getInstrumentation().getTargetContext()))
89+
.withLogger(logger).build(InstrumentationRegistry.getInstrumentation().getTargetContext());
90+
91+
assertNull(manager);
92+
}
93+
94+
@Test
95+
public void testBuilderWithOutDatafileConfigWithSdkKey() {
96+
ErrorHandler errorHandler = new ErrorHandler() {
97+
@Override
98+
public <T extends OptimizelyRuntimeException> void handleError(T exception) throws T {
99+
logger.error("Inside error handler", exception);
100+
}
101+
};
102+
103+
OptimizelyManager manager = OptimizelyManager.builder().withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getInstrumentation().getTargetContext()))
104+
.withDatafileDownloadInterval(30L, TimeUnit.MINUTES)
105+
.withEventDispatchInterval(30L, TimeUnit.MINUTES)
106+
.withDatafileHandler(new DefaultDatafileHandler())
107+
.withErrorHandler(errorHandler)
108+
.withSDKKey("sdkKey7")
109+
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getInstrumentation().getTargetContext()))
110+
.withLogger(logger).build(InstrumentationRegistry.getInstrumentation().getTargetContext());
111+
112+
assertNotNull(manager);
113+
assertNotNull(manager.getDatafileHandler());
114+
assertNotNull(manager.getUserProfileService());
115+
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getInstrumentation().getTargetContext()));
116+
117+
manager.stop(InstrumentationRegistry.getInstrumentation().getTargetContext());
118+
}
119+
120+
@Test
121+
public void testBuilderWithDatafileConfig() {
122+
ErrorHandler errorHandler = new ErrorHandler() {
123+
@Override
124+
public <T extends OptimizelyRuntimeException> void handleError(T exception) throws T {
125+
logger.error("Inside error handler", exception);
126+
}
127+
};
128+
129+
OptimizelyManager manager = OptimizelyManager.builder().withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getInstrumentation().getTargetContext()))
130+
.withDatafileDownloadInterval(30L, TimeUnit.MINUTES)
131+
.withEventDispatchInterval(30L, TimeUnit.MINUTES)
132+
.withDatafileHandler(new DefaultDatafileHandler())
133+
.withErrorHandler(errorHandler)
134+
.withDatafileConfig(new DatafileConfig(null, "sdkKey7"))
135+
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getInstrumentation().getTargetContext()))
136+
.withLogger(logger).build(InstrumentationRegistry.getInstrumentation().getTargetContext());
137+
138+
assertNotNull(manager);
139+
assertNotNull(manager.getDatafileHandler());
140+
assertNotNull(manager.getUserProfileService());
141+
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getInstrumentation().getTargetContext()));
142+
143+
manager.stop(InstrumentationRegistry.getInstrumentation().getTargetContext());
144+
}
145+
69146
@Test
70147
public void testBuilderWithOut() {
71148
OptimizelyManager manager = OptimizelyManager.builder(testProjectId).build(InstrumentationRegistry.getInstrumentation().getTargetContext());

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,11 @@ public OptimizelyManager build(Context context) {
907907
}
908908

909909
if (datafileConfig == null) {
910+
if (projectId == null && sdkKey == null) {
911+
logger.error("ProjectId and SDKKey cannot both be null");
912+
return null;
913+
}
914+
910915
datafileConfig = new DatafileConfig(projectId, sdkKey);
911916
}
912917

@@ -915,8 +920,7 @@ public OptimizelyManager build(Context context) {
915920
}
916921

917922
if (userProfileService == null) {
918-
DatafileConfig config = new DatafileConfig(projectId, sdkKey);
919-
userProfileService = DefaultUserProfileService.newInstance(config.getKey(), context);
923+
userProfileService = DefaultUserProfileService.newInstance(datafileConfig.getKey(), context);
920924
}
921925

922926
if (eventHandler == null) {
@@ -936,11 +940,6 @@ public OptimizelyManager build(Context context) {
936940

937941
}
938942

939-
if (projectId == null && sdkKey == null) {
940-
logger.error("ProjectId and SDKKey cannot both be null");
941-
return null;
942-
}
943-
944943
return new OptimizelyManager(projectId, sdkKey,
945944
datafileConfig,
946945
logger,

0 commit comments

Comments
 (0)