|
| 1 | +// |
| 2 | +// Copyright 2022, Optimizely, Inc. and contributors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import Foundation |
| 18 | +import XCTest |
| 19 | + |
| 20 | +class OptimizelyClientTests_Cmab_Config: XCTestCase { |
| 21 | + |
| 22 | + var optimizely: OptimizelyClient! |
| 23 | + |
| 24 | + override func setUp() { |
| 25 | + super.setUp() |
| 26 | + |
| 27 | + let datafile = OTUtils.loadJSONDatafile("decide_audience_segments")! |
| 28 | + optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey) |
| 29 | + try! optimizely.start(datafile: datafile) |
| 30 | + } |
| 31 | + |
| 32 | + override func tearDown() { |
| 33 | + Utils.sdkVersion = OPTIMIZELYSDKVERSION |
| 34 | + Utils.swiftSdkClientName = "swift-sdk" |
| 35 | + } |
| 36 | + |
| 37 | + // MARK: - ODP configuration |
| 38 | + |
| 39 | + func test_config_default() { |
| 40 | + let optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey) |
| 41 | + let cmabService = ((optimizely.decisionService as! DefaultDecisionService).cmabService as! DefaultCmabService) |
| 42 | + let cmabCache = cmabService.cmabCache |
| 43 | + let cmabClient = cmabService.cmabClient as! DefaultCmabClient |
| 44 | + XCTAssertEqual(100, cmabCache.maxSize) |
| 45 | + XCTAssertEqual(30 * 60, cmabCache.timeoutInSecs) |
| 46 | + XCTAssertEqual("https://prediction.cmab.optimizely.com/predict/%@", cmabClient.predictionEndpoint) |
| 47 | + } |
| 48 | + |
| 49 | + func test_cmab_custom_config() { |
| 50 | + var cmabConfig = CmabConfig(cacheSize: 50, cacheTimeoutInSecs: 120) |
| 51 | + var optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, cmabConfig: cmabConfig) |
| 52 | + var cmabService = ((optimizely.decisionService as! DefaultDecisionService).cmabService as! DefaultCmabService) |
| 53 | + var cmabCache = cmabService.cmabCache |
| 54 | + var cmabClient = cmabService.cmabClient as! DefaultCmabClient |
| 55 | + XCTAssertEqual(50, cmabCache.maxSize) |
| 56 | + XCTAssertEqual(120, cmabCache.timeoutInSecs) |
| 57 | + XCTAssertEqual("https://prediction.cmab.optimizely.com/predict/rule_123", cmabClient.getUrl(ruleId: "rule_123")?.absoluteString) |
| 58 | + |
| 59 | + cmabConfig = CmabConfig(cacheSize: 50, cacheTimeoutInSecs: -10, predictionEndpoint: "http://demo.cmab.com/%@/predict") |
| 60 | + optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, cmabConfig: cmabConfig) |
| 61 | + cmabService = ((optimizely.decisionService as! DefaultDecisionService).cmabService as! DefaultCmabService) |
| 62 | + cmabCache = cmabService.cmabCache |
| 63 | + cmabClient = cmabService.cmabClient as! DefaultCmabClient |
| 64 | + XCTAssertEqual(50, cmabCache.maxSize) |
| 65 | + XCTAssertEqual(1800, cmabCache.timeoutInSecs) |
| 66 | + XCTAssertEqual("http://demo.cmab.com/rule_1234/predict", cmabClient.getUrl(ruleId: "rule_1234")?.absoluteString) |
| 67 | + |
| 68 | + cmabConfig = CmabConfig(predictionEndpoint: "http://fowardslash.com/predict/%@/v1/") |
| 69 | + optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, cmabConfig: cmabConfig) |
| 70 | + cmabService = ((optimizely.decisionService as! DefaultDecisionService).cmabService as! DefaultCmabService) |
| 71 | + cmabCache = cmabService.cmabCache |
| 72 | + cmabClient = cmabService.cmabClient as! DefaultCmabClient |
| 73 | + XCTAssertEqual(100, cmabCache.maxSize) |
| 74 | + XCTAssertEqual(1800, cmabCache.timeoutInSecs) |
| 75 | + XCTAssertEqual("http://fowardslash.com/predict/rule-12345/v1/", cmabClient.getUrl(ruleId: "rule-12345")?.absoluteString) |
| 76 | + } |
| 77 | +} |
0 commit comments