Skip to content

Commit 1ee5ba6

Browse files
committed
add cmab config
1 parent 4f077f3 commit 1ee5ba6

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,27 @@ public Builder withClientInfo(@Nullable String clientEngineName, @Nullable Strin
10681068
return this;
10691069
}
10701070

1071+
/**
1072+
* Override the default Cmab cache size (100).
1073+
* @param size the size
1074+
* @return this {@link Builder} instance
1075+
*/
1076+
public Builder withCmabCacheSize(int size) {
1077+
this.cmabCacheSize = size;
1078+
return this;
1079+
}
1080+
1081+
/**
1082+
* Override the default Cmab cache timeout (30 minutes).
1083+
* @param interval the interval
1084+
* @param timeUnit the time unit of the timeout argument
1085+
* @return this {@link Builder} instance
1086+
*/
1087+
public Builder withCmabCacheTimeout(int interval, TimeUnit timeUnit) {
1088+
this.cmabCacheTimeoutInSecs = (int) timeUnit.toSeconds(interval);
1089+
return this;
1090+
}
1091+
10711092
public Builder withCmabClient(CmabClient cmabClient) {
10721093
this.cmabClient = cmabClient;
10731094
return this;

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.optimizely.ab.android.odp.ODPEventClient;
2626
import com.optimizely.ab.android.odp.ODPSegmentClient;
2727
import com.optimizely.ab.android.odp.VuidManager;
28+
import com.optimizely.ab.android.sdk.cmab.CMABClient;
29+
import com.optimizely.ab.android.shared.Client;
2830
import com.optimizely.ab.android.shared.DatafileConfig;
2931
import com.optimizely.ab.android.user_profile.DefaultUserProfileService;
3032
import com.optimizely.ab.bucketing.UserProfileService;
@@ -465,4 +467,71 @@ public void testBuildWithVuidEnabled() throws Exception {
465467

466468
when(ODPManager.builder()).thenCallRealMethod();
467469
}
470+
471+
@Test
472+
public void testCmabServiceConfigurationValidation() throws Exception {
473+
// Custom configuration values
474+
int customCacheSize = 500;
475+
int customTimeoutMinutes = 45;
476+
int expectedTimeoutSeconds = customTimeoutMinutes * 60; // 45 min = 2700 sec
477+
CMABClient mockCmabClient = mock(CMABClient.class);
478+
479+
// Create mocks for the CMAB service creation chain
480+
Object mockDefaultLRUCache = PowerMockito.mock(Class.forName("com.optimizely.ab.cache.DefaultLRUCache"));
481+
Object mockCmabServiceOptions = PowerMockito.mock(Class.forName("com.optimizely.ab.cmab.CmabServiceOptions"));
482+
Object mockDefaultCmabService = PowerMockito.mock(Class.forName("com.optimizely.ab.cmab.DefaultCmabService"));
483+
484+
// Mock the construction chain with parameter validation
485+
whenNew(Class.forName("com.optimizely.ab.cache.DefaultLRUCache"))
486+
.thenReturn(mockDefaultLRUCache);
487+
488+
whenNew(Class.forName("com.optimizely.ab.cmab.CmabServiceOptions"))
489+
.thenReturn(mockCmabServiceOptions);
490+
491+
whenNew(Class.forName("com.optimizely.ab.cmab.DefaultCmabService"))
492+
.thenReturn(mockDefaultCmabService);
493+
494+
// Use PowerMock to verify OptimizelyManager constructor is called with CMAB service
495+
whenNew(OptimizelyManager.class).withAnyArguments().thenReturn(mock(OptimizelyManager.class));
496+
497+
OptimizelyManager manager = OptimizelyManager.builder(testProjectId)
498+
.withCmabCacheSize(customCacheSize)
499+
.withCmabCacheTimeout(customTimeoutMinutes, TimeUnit.MINUTES)
500+
.withCmabClient(mockCmabClient)
501+
.build(mockContext);
502+
503+
verifyNew(Class.forName("com.optimizely.ab.cache.DefaultLRUCache"))
504+
.withArguments(eq(customCacheSize), eq(expectedTimeoutSeconds));
505+
506+
verifyNew(Class.forName("com.optimizely.ab.cmab.CmabServiceOptions"))
507+
.withArguments(any(), eq(mockDefaultLRUCache), eq(mockCmabClient));
508+
509+
verifyNew(Class.forName("com.optimizely.ab.cmab.DefaultCmabService"))
510+
.withArguments(eq(mockCmabServiceOptions));
511+
512+
// Verify OptimizelyManager constructor was called with the mocked CMAB service
513+
verifyNew(OptimizelyManager.class).withArguments(
514+
any(), // projectId
515+
any(), // sdkKey
516+
any(), // datafileConfig
517+
any(), // logger
518+
anyLong(), // datafileDownloadInterval
519+
any(), // datafileHandler
520+
any(), // errorHandler
521+
anyLong(), // eventDispatchRetryInterval
522+
any(), // eventHandler
523+
any(), // eventProcessor
524+
any(), // userProfileService
525+
any(), // notificationCenter
526+
any(), // defaultDecideOptions
527+
any(), // odpManager
528+
eq(mockDefaultCmabService), // cmabService - Should be our mocked service
529+
any(), // vuid
530+
any(), // customSdkName
531+
any() // customSdkVersion
532+
);
533+
534+
assertNotNull("Manager should be created successfully", manager);
535+
}
536+
468537
}

0 commit comments

Comments
 (0)