|
25 | 25 | import com.optimizely.ab.android.odp.ODPEventClient; |
26 | 26 | import com.optimizely.ab.android.odp.ODPSegmentClient; |
27 | 27 | import com.optimizely.ab.android.odp.VuidManager; |
| 28 | +import com.optimizely.ab.android.sdk.cmab.CMABClient; |
| 29 | +import com.optimizely.ab.android.shared.Client; |
28 | 30 | import com.optimizely.ab.android.shared.DatafileConfig; |
29 | 31 | import com.optimizely.ab.android.user_profile.DefaultUserProfileService; |
30 | 32 | import com.optimizely.ab.bucketing.UserProfileService; |
@@ -465,4 +467,71 @@ public void testBuildWithVuidEnabled() throws Exception { |
465 | 467 |
|
466 | 468 | when(ODPManager.builder()).thenCallRealMethod(); |
467 | 469 | } |
| 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 | + |
468 | 537 | } |
0 commit comments