Skip to content

fix: remove erroneous error if project id and sdk key are null. #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
import com.optimizely.ab.OptimizelyRuntimeException;
import com.optimizely.ab.android.datafile_handler.DefaultDatafileHandler;
import com.optimizely.ab.android.event_handler.DefaultEventHandler;
import com.optimizely.ab.android.shared.DatafileConfig;
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
import com.optimizely.ab.error.ErrorHandler;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;

import java.util.concurrent.TimeUnit;

import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;

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

@Test
public void testBuilderWithOutDatafileConfig() {
ErrorHandler errorHandler = new ErrorHandler() {
@Override
public <T extends OptimizelyRuntimeException> void handleError(T exception) throws T {
logger.error("Inside error handler", exception);
}
};

OptimizelyManager manager = OptimizelyManager.builder().withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getInstrumentation().getTargetContext()))
.withDatafileDownloadInterval(30L, TimeUnit.MINUTES)
.withEventDispatchInterval(30L, TimeUnit.MINUTES)
.withDatafileHandler(new DefaultDatafileHandler())
.withErrorHandler(errorHandler)
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getInstrumentation().getTargetContext()))
.withLogger(logger).build(InstrumentationRegistry.getInstrumentation().getTargetContext());

assertNull(manager);
}

@Test
public void testBuilderWithOutDatafileConfigWithSdkKey() {
ErrorHandler errorHandler = new ErrorHandler() {
@Override
public <T extends OptimizelyRuntimeException> void handleError(T exception) throws T {
logger.error("Inside error handler", exception);
}
};

OptimizelyManager manager = OptimizelyManager.builder().withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getInstrumentation().getTargetContext()))
.withDatafileDownloadInterval(30L, TimeUnit.MINUTES)
.withEventDispatchInterval(30L, TimeUnit.MINUTES)
.withDatafileHandler(new DefaultDatafileHandler())
.withErrorHandler(errorHandler)
.withSDKKey("sdkKey7")
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getInstrumentation().getTargetContext()))
.withLogger(logger).build(InstrumentationRegistry.getInstrumentation().getTargetContext());

assertNotNull(manager);
assertNotNull(manager.getDatafileHandler());
assertNotNull(manager.getUserProfileService());
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getInstrumentation().getTargetContext()));

manager.stop(InstrumentationRegistry.getInstrumentation().getTargetContext());
}

@Test
public void testBuilderWithDatafileConfig() {
ErrorHandler errorHandler = new ErrorHandler() {
@Override
public <T extends OptimizelyRuntimeException> void handleError(T exception) throws T {
logger.error("Inside error handler", exception);
}
};

OptimizelyManager manager = OptimizelyManager.builder().withUserProfileService(DefaultUserProfileService.newInstance(testProjectId, InstrumentationRegistry.getInstrumentation().getTargetContext()))
.withDatafileDownloadInterval(30L, TimeUnit.MINUTES)
.withEventDispatchInterval(30L, TimeUnit.MINUTES)
.withDatafileHandler(new DefaultDatafileHandler())
.withErrorHandler(errorHandler)
.withDatafileConfig(new DatafileConfig(null, "sdkKey7"))
.withEventHandler(DefaultEventHandler.getInstance(InstrumentationRegistry.getInstrumentation().getTargetContext()))
.withLogger(logger).build(InstrumentationRegistry.getInstrumentation().getTargetContext());

assertNotNull(manager);
assertNotNull(manager.getDatafileHandler());
assertNotNull(manager.getUserProfileService());
assertNotNull(manager.getEventHandler(InstrumentationRegistry.getInstrumentation().getTargetContext()));

manager.stop(InstrumentationRegistry.getInstrumentation().getTargetContext());
}

@Test
public void testBuilderWithOut() {
OptimizelyManager manager = OptimizelyManager.builder(testProjectId).build(InstrumentationRegistry.getInstrumentation().getTargetContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,11 @@ public OptimizelyManager build(Context context) {
}

if (datafileConfig == null) {
if (projectId == null && sdkKey == null) {
logger.error("ProjectId and SDKKey cannot both be null");
return null;
}

datafileConfig = new DatafileConfig(projectId, sdkKey);
}

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

if (userProfileService == null) {
DatafileConfig config = new DatafileConfig(projectId, sdkKey);
userProfileService = DefaultUserProfileService.newInstance(config.getKey(), context);
userProfileService = DefaultUserProfileService.newInstance(datafileConfig.getKey(), context);
}

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

}

if (projectId == null && sdkKey == null) {
logger.error("ProjectId and SDKKey cannot both be null");
return null;
}

return new OptimizelyManager(projectId, sdkKey,
datafileConfig,
logger,
Expand Down