Skip to content
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
5 changes: 5 additions & 0 deletions OptimizelySDKCore/OptimizelySDKCore/OPTLYBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ typedef void (^OPTLYBuilderBlock)(OPTLYBuilder * _Nullable builder);
@property (nonatomic, readwrite, strong, nullable) id<OPTLYUserProfile> userProfile;
/// The datafile manager that will download the datafile for the manager
@property (nonatomic, readwrite, strong, nullable) id<OPTLYDatafileManager> datafileManager;
/// The client version
@property (nonatomic, strong, nonnull) NSString *clientVersion;
/// The client engine
@property (nonatomic, strong, nonnull) NSString *clientEngine;


/// Create an Optimizely Builder object.
+ (nullable instancetype)builderWithBlock:(nonnull OPTLYBuilderBlock)block;
Expand Down
2 changes: 2 additions & 0 deletions OptimizelySDKCore/OptimizelySDKCore/OPTLYBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ - (id)initWithBlock:(OPTLYBuilderBlock)block {
builder.datafile = self.datafile;
builder.logger = self.logger;
builder.errorHandler = self.errorHandler;
builder.clientEngine = self.clientEngine;
builder.clientVersion = self.clientVersion;
}];

if (_config == nil) {
Expand Down
15 changes: 5 additions & 10 deletions OptimizelySDKCore/OptimizelySDKCore/OPTLYProjectConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ NS_ASSUME_NONNULL_END
@property (nonatomic, strong, nullable) id<OPTLYLogger, Ignore> logger;
@property (nonatomic, strong, nullable) id<OPTLYErrorHandler, Ignore> errorHandler;

/// Returns the client type (e.g., objective-c-sdk-core, objective-c-sdk-iOS, objective-c-sdk-tvOS)
@property (nonatomic, strong, readonly, nonnull) NSString<Ignore> *clientEngine;
/// Returns the client version number
@property (nonatomic, strong, readonly, nonnull) NSString<Ignore> *clientVersion;

/**
* Initialize the Project Config from a builder block.
*/
Expand Down Expand Up @@ -124,14 +129,4 @@ NS_ASSUME_NONNULL_END
attributes:(nullable NSDictionary<NSString *,NSString *> *)attributes
bucketer:(nullable id<OPTLYBucketer>)bucketer;

/*
* Returns the client type (e.g., objective-c-sdk-core, objective-c-sdk-iOS, objective-c-sdk-tvOS)
*/
- (nonnull NSString *)clientEngine;

/*
* Returns the client version number
*/
- (nonnull NSString *)clientVersion;

@end
14 changes: 3 additions & 11 deletions OptimizelySDKCore/OptimizelySDKCore/OPTLYProjectConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#import "OPTLYVariable.h"
#import "OPTLYVariable.h"

NSString * const kClientEngine = @"objective-c-sdk-core";
NSString * const kExpectedDatafileVersion = @"3";

@interface OPTLYProjectConfig()
Expand Down Expand Up @@ -125,6 +124,9 @@ - (instancetype)initWithBuilder:(OPTLYProjectConfigBuilder *)builder {
[builder.errorHandler handleException:datafileException];
}

_clientEngine = builder.clientEngine;
_clientVersion = builder.clientVersion;

_errorHandler = (id<OPTLYErrorHandler, Ignore>)builder.errorHandler;
_logger = (id<OPTLYLogger, Ignore>)builder.logger;
return self;
Expand Down Expand Up @@ -415,14 +417,4 @@ - (OPTLYVariation *)getVariationForExperiment:(NSString *)experimentKey
return variation;
}

- (NSString *)clientEngine
{
return kClientEngine;
}

- (NSString *)clientVersion
{
return OPTIMIZELY_SDK_CORE_VERSION;
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@ typedef void (^OPTLYProjectConfigBuilderBlock)(OPTLYProjectConfigBuilder * _Null
@property (nonatomic, strong, nullable) id<OPTLYLogger> logger;
/// the non optional datafile contents
@property (nonatomic, strong, nonnull) NSData *datafile;
/// The client version
@property (nonatomic, strong, nonnull) NSString *clientVersion;
/// The client engine
@property (nonatomic, strong, nonnull) NSString *clientEngine;


@end
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#import "OPTLYProjectConfigBuilder.h"

NSString * const kClientEngine = @"objective-c-sdk-core";

@implementation OPTLYProjectConfigBuilder

+ (nullable instancetype)builderWithBlock:(nonnull OPTLYProjectConfigBuilderBlock)block {
Expand All @@ -30,6 +32,12 @@ - (id)initWithBlock:(OPTLYProjectConfigBuilderBlock)block {
self = [super init];
if (self != nil) {
block(self);
if (!_clientEngine) {
_clientEngine = kClientEngine;
}
if (!_clientVersion) {
_clientVersion = OPTIMIZELY_SDK_CORE_VERSION;
}
if (!_datafile) {
return nil;
}
Expand Down
32 changes: 29 additions & 3 deletions OptimizelySDKCore/OptimizelySDKCoreTests/OPTLYBuilderTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@
#import "OPTLYErrorHandler.h"
#import "OPTLYEventDispatcher.h"
#import "OPTLYLogger.h"
#import "OPTLYProjectConfig.h"
#import "OPTLYTestHelper.h"

// static data from datafile
static NSString * const kDataModelDatafileName = @"datafile_6372300739";
static NSData *datafile;

@interface OPTLYBuilderTest : XCTestCase

@end

@implementation OPTLYBuilderTest

+ (void)setUp {
datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDataModelDatafileName];
}

- (void)testBuilderRequiresDatafile {
Optimizely *optimizely = [Optimizely initWithBuilderBlock:^(OPTLYBuilder *builder) {

Expand All @@ -48,10 +54,13 @@ - (void)testBuilderBuildsDefaults {
XCTAssertNotNil(optimizely.eventBuilder);
XCTAssertNotNil(optimizely.eventDispatcher);
XCTAssertNotNil(optimizely.logger);
XCTAssertNotNil(optimizely.config.clientEngine);
XCTAssertNotNil(optimizely.config.clientVersion);
XCTAssertEqualObjects(optimizely.config.clientEngine, @"objective-c-sdk-core");
XCTAssertEqualObjects(optimizely.config.clientVersion, OPTIMIZELY_SDK_CORE_VERSION);
}

- (void)testBuilderCanAssignErrorHandler {
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDataModelDatafileName];
OPTLYErrorHandlerDefault *errorHandler = [[OPTLYErrorHandlerDefault alloc] init];

Optimizely *defaultOptimizely = [Optimizely initWithBuilderBlock:^(OPTLYBuilder *builder) {
Expand All @@ -70,7 +79,6 @@ - (void)testBuilderCanAssignErrorHandler {
}

- (void)testBuilderCanAssignEventDispatcher {
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDataModelDatafileName];
id<OPTLYEventDispatcher> eventDispatcher = [[NSObject alloc] init];

Optimizely *defaultOptimizely = [Optimizely initWithBuilderBlock:^(OPTLYBuilder *builder) {
Expand All @@ -89,7 +97,6 @@ - (void)testBuilderCanAssignEventDispatcher {
}

- (void)testBuilderCanAssignLogger {
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDataModelDatafileName];
OPTLYLoggerDefault *logger = [[OPTLYLoggerDefault alloc] init];

Optimizely *defaultOptimizely = [Optimizely initWithBuilderBlock:^(OPTLYBuilder *builder) {
Expand Down Expand Up @@ -119,4 +126,23 @@ - (void)testBuilderReturnsNilWithBadDatafile {
XCTAssertNil(optimizely);
}

/**
* Make sure the OPTLYBuilder can pass the client engine and version properly to the OPTLYProjectConfig initialization.
*/
- (void)testBuilderCanPassClientEngineAndVersionToProjectConfig {
NSString *clientEngine = @"clientEngine";
NSString *clientVersion = @"clientVersion";

Optimizely *optimizely = [Optimizely initWithBuilderBlock:^(OPTLYBuilder * _Nullable builder) {
builder.datafile = datafile;
builder.clientEngine = clientEngine;
builder.clientVersion = clientVersion;
}];

XCTAssertNotNil(optimizely);
XCTAssertNotNil(optimizely.config);
XCTAssertEqualObjects(optimizely.config.clientEngine, clientEngine);
XCTAssertEqualObjects(optimizely.config.clientVersion, clientVersion);
}

@end
20 changes: 20 additions & 0 deletions OptimizelySDKCore/OptimizelySDKCoreTests/OPTLYProjectConfigTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ - (void)testInitWithBuilderBlock
XCTAssertNotNil(projectConfig, @"project config should not be nil.");
XCTAssertNotNil(projectConfig.logger, @"logger should not be nil.");
XCTAssertNotNil(projectConfig.errorHandler, @"error handler should not be nil.");
XCTAssertEqualObjects(projectConfig.clientEngine, @"objective-c-sdk-core");
XCTAssertEqualObjects(projectConfig.clientVersion, OPTIMIZELY_SDK_CORE_VERSION);
}

/**
* Make sure we can pass in different values for client engine and client version to override the defaults.
*/
- (void)testClientEngineAndClientVersionAreConfigurable {
NSData *datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDataModelDatafileName];
NSString *clientEngine = @"clientEngine";
NSString *clientVersion = @"clientVersion";

OPTLYProjectConfig *projectConfig = [OPTLYProjectConfig initWithBuilderBlock:^(OPTLYProjectConfigBuilder * _Nullable builder) {
builder.datafile = datafile;
builder.clientEngine = clientEngine;
builder.clientVersion = clientVersion;
}];
XCTAssertNotNil(projectConfig);
XCTAssertEqualObjects(projectConfig.clientEngine, clientEngine);
XCTAssertEqualObjects(projectConfig.clientVersion, clientVersion);
}

- (void)testInitWithBuilderBlockNoDatafile
Expand Down
4 changes: 4 additions & 0 deletions OptimizelySDKShared/OptimizelySDKShared/OPTLYClientBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ typedef void (^OPTLYClientBuilderBlock)(OPTLYClientBuilder * _Nonnull builder);
@property (nonatomic, readwrite, strong, nullable) id<OPTLYLogger> logger;
/// User profile to be used by the Optimizely instance to store user-specific data.
@property (nonatomic, readwrite, strong, nullable) id<OPTLYUserProfile> userProfile;
/// The client version
@property (nonatomic, strong, nonnull) NSString *clientVersion;
/// The client engine
@property (nonatomic, strong, nonnull) NSString *clientEngine;

/// Create an Optimizely Client object.
+ (nonnull instancetype)builderWithBlock:(nonnull OPTLYClientBuilderBlock)block;
Expand Down
2 changes: 2 additions & 0 deletions OptimizelySDKShared/OptimizelySDKShared/OPTLYClientBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ - (id)initWithBlock:(OPTLYClientBuilderBlock)block {
builder.eventDispatcher = _eventDispatcher;
builder.logger = _logger;
builder.userProfile = _userProfile;
builder.clientEngine = _clientEngine;
builder.clientVersion = _clientVersion;
}];
_logger = _optimizely.logger;
if (!_logger) {
Expand Down
10 changes: 10 additions & 0 deletions OptimizelySDKShared/OptimizelySDKShared/OPTLYManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
@property (nonatomic, readonly, strong, nullable) id<OPTLYLogger> logger;
/// User profile to be used by the client to store user-specific data.
@property (nonatomic, readonly, strong, nullable) id<OPTLYUserProfile> userProfile;
/// The client engine
@property (nonatomic, readonly, strong, nonnull) NSString *clientEngine;
/// The client version
@property (nonatomic, readonly, strong, nonnull) NSString *clientVersion;

/**
* Init with builder block
Expand All @@ -43,6 +47,12 @@
*/
+ (nullable instancetype)initWithBuilderBlock:(nonnull OPTLYManagerBuilderBlock)block;

/**
* Init with a builder
* @param builder The Optimizely Manager Builder where datafile manager, event dispatcher, and other configurations will be set.
* @return OPTLYManager instance
*/
+ (instancetype)initWithBuilder:(OPTLYManagerBuilder *)builder;
/*
* Synchronous call that would retrieve the datafile from local cache. If it fails to load from local cache it will return a dummy instance
*/
Expand Down
10 changes: 9 additions & 1 deletion OptimizelySDKShared/OptimizelySDKShared/OPTLYManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ @interface OPTLYManager()
@implementation OPTLYManager

+ (instancetype)initWithBuilderBlock:(OPTLYManagerBuilderBlock)block {
return [[self alloc] initWithBuilder:[OPTLYManagerBuilder builderWithBlock:block]];
return [OPTLYManager initWithBuilder:[OPTLYManagerBuilder builderWithBlock:block]];
}

+ (instancetype)initWithBuilder:(OPTLYManagerBuilder *)builder {
return [[self alloc] initWithBuilder:builder];
}

- (instancetype)init {
Expand All @@ -54,6 +58,8 @@ - (instancetype)initWithBuilder:(OPTLYManagerBuilder *)builder {
_logger = builder.logger;
_projectId = builder.projectId;
_userProfile = builder.userProfile;
_clientEngine = builder.clientEngine;
_clientVersion = builder.clientVersion;
}
return self;
}
Expand Down Expand Up @@ -127,6 +133,8 @@ - (OPTLYClient *)initializeClientWithManagerSettingsAndDatafile:(NSData *)datafi
builder.eventDispatcher = self.eventDispatcher;
builder.logger = self.logger;
builder.userProfile = self.userProfile;
builder.clientEngine = self.clientEngine;
builder.clientVersion = self.clientVersion;
}];
return client;
}
Expand Down
4 changes: 4 additions & 0 deletions OptimizelySDKShared/OptimizelySDKShared/OPTLYManagerBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ typedef void (^OPTLYManagerBuilderBlock)(OPTLYManagerBuilder * _Nullable builder
@property (nonatomic, readwrite, strong, nullable) id<OPTLYLogger> logger;
/// User profile to be used by the client to store user-specific data.
@property (nonatomic, readwrite, strong, nullable) id<OPTLYUserProfile> userProfile;
/// The client engine
@property (nonatomic, readwrite, strong, nonnull) NSString *clientEngine;
/// The client version
@property (nonatomic, readwrite, strong, nonnull) NSString *clientVersion;

/// Create the Optimizely Manager object.
+ (nullable instancetype)builderWithBlock:(nonnull OPTLYManagerBuilderBlock)block;
Expand Down
44 changes: 14 additions & 30 deletions OptimizelySDKShared/OptimizelySDKShared/OPTLYManagerBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,23 @@ - (id)initWithBlock:(OPTLYManagerBuilderBlock)block {
self = [super init];
if (self != nil) {
block(self);
if (![OPTLYDatafileManagerUtility conformsToOPTLYDatafileManagerProtocol:[self.datafileManager class]]) {
if (!self.datafileManager) {
self.datafileManager = [[OPTLYDatafileManagerBasic alloc] init];
}
else if (![OPTLYDatafileManagerUtility conformsToOPTLYDatafileManagerProtocol:[self.datafileManager class]]) {
return nil;
}
}
return self;
}

- (id<OPTLYDatafileManager>)datafileManager {
if (!_datafileManager) {
_datafileManager = [[OPTLYDatafileManagerBasic alloc] init];
}
return _datafileManager;
}

- (id<OPTLYErrorHandler>)errorHandler {
if (!_errorHandler) {
_errorHandler = [[OPTLYErrorHandlerNoOp alloc] init];
}
return _errorHandler;
}

- (id<OPTLYEventDispatcher>)eventDispatcher {
if (!_eventDispatcher) {
_eventDispatcher = [[OPTLYEventDispatcherBasic alloc] init];
}
return _eventDispatcher;
}

- (id<OPTLYLogger>)logger {
if (!_logger) {
_logger = [[OPTLYLoggerDefault alloc] init];
if (!self.errorHandler) {
self.errorHandler = [[OPTLYErrorHandlerNoOp alloc] init];
}
if (!self.eventDispatcher) {
self.eventDispatcher = [[OPTLYEventDispatcherBasic alloc] init];
}
if (!self.logger) {
self.logger = [[OPTLYLoggerDefault alloc] init];
}
}
return _logger;
return self;
}

@end
21 changes: 21 additions & 0 deletions OptimizelySDKShared/OptimizelySDKSharedTests/OPTLYClientTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#import "OPTLYClient.h"
#import <OptimizelySDKCore/OPTLYLogger.h>
#import <OptimizelySDKCore/OPTLYProjectConfig.h>

// static datafile name
static NSString *const kDatamodelDatafileName = @"datafile_6372300739";
Expand Down Expand Up @@ -48,4 +49,24 @@ - (void)testClientBuildsOptimizelyDefaults {
XCTAssertNotNil(client.logger);
}

/**
* Make sure the OPTLYClient Builder can pass the client engine and client version through to optimizely core and then to optimizely project config
*/
- (void)testClientPassesThroughClientEngineAndVersion {
NSString *clientEngine = @"clientEngine";
NSString *clientVersion = @"clientVersion";
OPTLYClient *client = [OPTLYClient initWithBuilderBlock:^(OPTLYClientBuilder * _Nonnull builder) {
builder.datafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName];
builder.clientEngine = clientEngine;
builder.clientVersion = clientVersion;
}];
XCTAssertNotNil(client);
XCTAssertNotNil(client.optimizely);
XCTAssertNotNil(client.optimizely.config);
XCTAssertNotNil(client.optimizely.config.clientEngine);
XCTAssertNotNil(client.optimizely.config.clientVersion);
XCTAssertEqualObjects(client.optimizely.config.clientEngine, clientEngine);
XCTAssertEqualObjects(client.optimizely.config.clientVersion, clientVersion);
}

@end
Loading