Skip to content

Commit 554cc84

Browse files
wangjoshuahalda-optimizely
authored andcommitted
add test for initialization of client
1 parent a1f3029 commit 554cc84

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

OptimizelySDKShared/OptimizelySDKSharedTests/OPTLYManagerTest.m

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
***************************************************************************/
1616

1717
#import <XCTest/XCTest.h>
18+
#import <OHHTTPStubs/OHHTTPStubs.h>
1819
#import "OPTLYTestHelper.h"
1920

21+
#import <OptimizelySDKCore/OPTLYProjectConfig.h>
2022
#import "OPTLYManager.h"
2123
#import "OPTLYClient.h"
2224

2325
// static datafile name
2426
static NSString *const kDefaultDatafileFileName = @"datafile_6372300739";
2527
static NSString *const kProjectId = @"6372300739";
28+
static NSString *const kAlternateDatafilename = @"validator_whitelisting_test_datafile";
2629
static NSData *kDefaultDatafile;
30+
static NSData *kAlternateDatafile;
2731

2832
@interface OPTLYManagerTest : XCTestCase
2933

@@ -34,6 +38,126 @@ @implementation OPTLYManagerTest
3438
+ (void)setUp {
3539
[super setUp];
3640
kDefaultDatafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kDefaultDatafileFileName];
41+
kAlternateDatafile = [OPTLYTestHelper loadJSONDatafileIntoDataObject:kAlternateDatafilename];
3742
}
3843

44+
- (void)testInitializeClientWithoutDatafileReturnsDummy {
45+
// initialize manager without datafile
46+
OPTLYManager *manager = [OPTLYManager initWithBuilderBlock:^(OPTLYManagerBuilder * _Nullable builder) {
47+
builder.projectId = kProjectId;
48+
}];
49+
50+
// make sure the manager is initialized correctly
51+
XCTAssertNotNil(manager);
52+
XCTAssertNotNil(manager.projectId);
53+
XCTAssertNil(manager.datafile);
54+
55+
// try to initialize client
56+
OPTLYClient *client = [manager initializeClient];
57+
58+
// make sure we get a dummy client back
59+
XCTAssertNotNil(client);
60+
XCTAssertNil(client.optimizely);
61+
XCTAssertNotNil(client.logger);
62+
}
63+
64+
- (void)testInitializeClientWithDefaults {
65+
// initialize manager
66+
OPTLYManager *manager = [OPTLYManager initWithBuilderBlock:^(OPTLYManagerBuilder * _Nullable builder) {
67+
builder.datafile = kDefaultDatafile;
68+
builder.projectId = kProjectId;
69+
}];
70+
71+
// make sure manager is initialized correctly
72+
XCTAssertNotNil(manager);
73+
XCTAssertNotNil(manager.datafile);
74+
XCTAssertEqual(manager.datafile, kDefaultDatafile);
75+
76+
// initialize client
77+
OPTLYClient *client = [manager initializeClient];
78+
79+
// test client initialization
80+
XCTAssertNotNil(client);
81+
XCTAssertNotNil(client.optimizely);
82+
XCTAssertNotNil(client.logger);
83+
84+
[self checkConfigIsUsingDefaultDatafile:client.optimizely.config];
85+
}
86+
87+
- (void)testInitializeClientWithCustomDatafile {
88+
// initialize manager
89+
OPTLYManager *manager = [OPTLYManager initWithBuilderBlock:^(OPTLYManagerBuilder * _Nullable builder) {
90+
builder.datafile = kDefaultDatafile;
91+
builder.projectId = kProjectId;
92+
}];
93+
94+
// make sure manager is initialized correctly
95+
XCTAssertNotNil(manager);
96+
XCTAssertNotNil(manager.datafile);
97+
XCTAssertEqual(manager.datafile, kDefaultDatafile);
98+
99+
// initialize client
100+
OPTLYClient *client = [manager initializeClientWithDatafile:kAlternateDatafile];
101+
102+
// test client initialization
103+
XCTAssertNotNil(client);
104+
XCTAssertNotNil(client.optimizely);
105+
XCTAssertNotNil(client.logger);
106+
107+
[self checkConfigIsUsingAlternativeDatafile:client.optimizely.config];
108+
}
109+
110+
- (void)testInitializeClientAsync {
111+
// initialize manager
112+
OPTLYManager *manager = [OPTLYManager initWithBuilderBlock:^(OPTLYManagerBuilder * _Nullable builder) {
113+
builder.datafile = kDefaultDatafile;
114+
builder.projectId = kProjectId;
115+
}];
116+
117+
// make sure manager is initialized correctly
118+
XCTAssertNotNil(manager);
119+
XCTAssertNotNil(manager.datafile);
120+
XCTAssertEqual(manager.datafile, kDefaultDatafile);
121+
122+
// stub network call
123+
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
124+
return [request.URL.host isEqualToString:@"cdn.optimizely.com"];
125+
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
126+
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
127+
return [OHHTTPStubsResponse responseWithData:kAlternateDatafile
128+
statusCode:200
129+
headers:@{@"Content-Type":@"application/json"}];
130+
}];
131+
132+
// setup async expectation
133+
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testInitializeClientAsync"];
134+
// initialize client
135+
[manager initializeClientWithCallback:^(NSError * _Nullable error, OPTLYClient * _Nullable client) {
136+
137+
// check client in callback
138+
XCTAssertNotNil(client);
139+
XCTAssertNotNil(client.optimizely, @"Client needs to have an optimizely instance");
140+
XCTAssertNotNil(client.logger);
141+
[self checkConfigIsUsingAlternativeDatafile:client.optimizely.config];
142+
[expectation fulfill];
143+
}];
144+
145+
// wait for async start to finish
146+
[self waitForExpectationsWithTimeout:2 handler:nil];
147+
}
148+
149+
- (void)checkConfigIsUsingDefaultDatafile: (OPTLYProjectConfig *)config {
150+
XCTAssertEqualObjects(config.revision, @"58");
151+
XCTAssertEqualObjects(config.projectId, @"6372300739");
152+
XCTAssertEqualObjects(config.accountId, @"6365361536");
153+
}
154+
155+
- (void)checkConfigIsUsingAlternativeDatafile: (OPTLYProjectConfig *)config {
156+
XCTAssertEqualObjects(config.revision, @"6");
157+
XCTAssertEqualObjects(config.projectId, @"7519590183");
158+
XCTAssertEqualObjects(config.accountId, @"3244610124");
159+
}
160+
161+
162+
39163
@end

0 commit comments

Comments
 (0)