Skip to content

Commit

Permalink
[FSSDK-10761] fix: passed fsc for VUID optln (#564)
Browse files Browse the repository at this point in the history
FSC test cases passed for vuid optln
  • Loading branch information
muzahidul-opti authored Nov 25, 2024
1 parent 62ae49b commit c7460b2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
9 changes: 8 additions & 1 deletion Sources/ODP/OdpManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public class OdpManager {
cacheTimeoutInSecs: cacheTimeoutInSecs,
resourceTimeoutInSecs: timeoutForSegmentFetchInSecs)
self.eventManager = eventManager ?? OdpEventManager(sdkKey: sdkKey,
resourceTimeoutInSecs: timeoutForEventDispatchInSecs)
resourceTimeoutInSecs: timeoutForEventDispatchInSecs)

self.odpConfig = OdpConfig()
self.segmentManager.odpConfig = odpConfig
self.eventManager.odpConfig = odpConfig
Expand All @@ -80,6 +81,12 @@ public class OdpManager {
completionHandler: completionHandler)
}

func sendInitializedEvent(vuid: String) throws {
guard enabled else { throw OptimizelyError.odpNotEnabled }
guard odpConfig.eventQueueingAllowed else { throw OptimizelyError.odpNotIntegrated }
eventManager.sendInitializedEvent(vuid: vuid)
}

func identifyUser(userId: String) {
guard enabled else {
logger.d("ODP identify event is not dispatched (ODP disabled).")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Optimizely+Decide/OptimizelyClient+Decide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension OptimizelyClient {
/// - Parameter attributes: A map of attribute names to current user attribute values.
/// - Returns: An OptimizelyUserContext associated with this OptimizelyClient
public func createUserContext(attributes: [String: Any]? = nil) -> OptimizelyUserContext? {
guard enableVuid, let vuid = self.vuid else {
guard let vuid = self.vuid else {
logger.e("Vuid is not enabled or invalid VUID. User context not created.")
return nil
}
Expand Down
34 changes: 12 additions & 22 deletions Sources/Optimizely/OptimizelyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ open class OptimizelyClient: NSObject {
var decisionService: OPTDecisionService!
public var notificationCenter: OPTNotificationCenter?
public var odpManager: OdpManager!
private var vuidManager: VuidManager!
public var vuid: String?
let sdkSettings: OptimizelySdkSettings

// MARK: - Public interfaces
Expand Down Expand Up @@ -90,17 +90,19 @@ open class OptimizelyClient: NSObject {
self.sdkKey = sdkKey
self.sdkSettings = settings ?? OptimizelySdkSettings()
self.defaultDecideOptions = defaultDecideOptions ?? []

super.init()
self.vuidManager = VuidManager.shared
self.vuidManager.configure(enable: self.sdkSettings.enableVuid)
VuidManager.shared.configure(enable: self.sdkSettings.enableVuid)
if self.sdkSettings.enableVuid {
self.vuid = VuidManager.shared.vuid
}

self.odpManager = odpManager ?? OdpManager(sdkKey: sdkKey,
disable: sdkSettings.disableOdp,
cacheSize: sdkSettings.segmentsCacheSize,
cacheTimeoutInSecs: sdkSettings.segmentsCacheTimeoutInSecs,
timeoutForSegmentFetchInSecs: sdkSettings.timeoutForSegmentFetchInSecs,
timeoutForEventDispatchInSecs: sdkSettings.timeoutForOdpEventInSecs)
self.odpManager.vuid = vuidManager.vuid
let userProfileService = userProfileService ?? DefaultUserProfileService()
let logger = logger ?? DefaultLogger()
type(of: logger).logLevel = defaultLogLevel ?? .info
Expand All @@ -117,17 +119,10 @@ open class OptimizelyClient: NSObject {
self.datafileHandler = HandlerRegistryService.shared.injectDatafileHandler(sdkKey: self.sdkKey)
self.decisionService = HandlerRegistryService.shared.injectDecisionService(sdkKey: self.sdkKey)
self.notificationCenter = HandlerRegistryService.shared.injectNotificationCenter(sdkKey: self.sdkKey)

if let _vuid = self.vuidManager.vuid {
try? sendOdpEvent(type: Constants.ODP.eventType,
action: "client_initialized",
identifiers: [
Constants.ODP.keyForVuid: _vuid
],
data: [:])
if let _vuid = vuid {
self.odpManager.vuid = _vuid
sendInitializedEvent(vuid: _vuid)
}


logger.d("SDK Version: \(version)")
}

Expand Down Expand Up @@ -983,13 +978,8 @@ extension OptimizelyClient {
data: data)
}

/// the device vuid (read only)
public var vuid: String? {
return self.vuidManager.vuid
}

public var enableVuid: Bool {
return self.vuidManager.enable
func sendInitializedEvent(vuid: String) {
try? odpManager.sendInitializedEvent(vuid: vuid)
}

func identifyUserToOdp(userId: String) {
Expand Down
1 change: 0 additions & 1 deletion Sources/Optimizely/VuidManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class VuidManager {
private var _vuid: String = ""
private(set) var enable: Bool = false
let logger = OPTLoggerFactory.getLogger()

// a single vuid should be shared for all SDK instances
public static let shared = VuidManager()

Expand Down
24 changes: 19 additions & 5 deletions Tests/OptimizelyTests-APIs/OptimizelyClientTests_ODP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,28 @@ class OptimizelyClientTests_ODP: XCTestCase {
func testVuidEnabled() {
let settings = OptimizelySdkSettings(enableVuid: true)
optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, settings: settings)
XCTAssertTrue(optimizely.enableVuid)
XCTAssertNotNil(optimizely.vuid)
XCTAssert(optimizely.vuid!.starts(with: "vuid_"))
}

func testVuidDiabled() {
// Default client vuid diabled
XCTAssertFalse(optimizely.enableVuid)
XCTAssertNil(optimizely.vuid)
func test_register_vuid_autometically_when_enabled() {
let settings = OptimizelySdkSettings(enableVuid: true)
optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, settings: settings)
let eventManager = optimizely.odpManager.eventManager!
let evt = eventManager.eventQueue.getFirstItem()!

XCTAssertEqual("fullstack", evt.type)
XCTAssertEqual("client_initialized", evt.action)
XCTAssertEqual(["vuid": optimizely.vuid], evt.identifiers)
XCTAssertNotNil(optimizely.vuid)
}

func test_vuid_does_not_register_autometically_when_enabled_but_odp_disabled() {
let settings = OptimizelySdkSettings(disableOdp: true, enableVuid: true)
optimizely = OptimizelyClient(sdkKey: OTUtils.randomSdkKey, settings: settings)
XCTAssertNotNil(optimizely.vuid)
let eventManager = optimizely.odpManager.eventManager
XCTAssertNil(eventManager)
}

// MARK: - OdpConfig Update
Expand Down

0 comments on commit c7460b2

Please sign in to comment.