1919#import " FABMockApplicationIdentifierModel.h"
2020#import " FIRCLSApplication.h"
2121#include " FIRCLSConstants.h"
22+ #include " FIRCLSDataCollectionToken.h"
2223#include " FIRCLSDefines.h"
2324#include " FIRCLSFileManager.h"
25+ #include " FIRCLSInternalReport.h"
26+ #include " FIRCLSMockFileManager.h"
27+ #include " FIRCLSMockNetworkClient.h"
2428#include " FIRCLSMockSettings.h"
2529#include " FIRCLSSettings.h"
2630#include " FIRMockGDTCoreTransport.h"
@@ -31,9 +35,13 @@ @interface FIRCLSReportUploaderTests
3135 : XCTestCase <FIRCLSReportUploaderDelegate, FIRCLSReportUploaderDataSource>
3236
3337@property (nonatomic , strong ) FIRCLSReportUploader *uploader;
34- @property (nonatomic , strong ) FIRCLSFileManager *fileManager;
38+ @property (nonatomic , strong ) FIRCLSMockFileManager *fileManager;
3539@property (nonatomic , strong ) NSOperationQueue *queue;
36- @property (nonatomic , strong ) NSURL *url;
40+ @property (nonatomic , strong ) FIRCLSMockNetworkClient *networkClient;
41+
42+ // Add mock prefix to names as there are naming conflicts with FIRCLSReportUploaderDelegate
43+ @property (nonatomic , strong ) FIRMockGDTCORTransport *mockDataTransport;
44+ @property (nonatomic , strong ) FIRCLSMockSettings *mockSettings;
3745
3846@end
3947
@@ -42,21 +50,24 @@ @implementation FIRCLSReportUploaderTests
4250- (void )setUp {
4351 [super setUp ];
4452
53+ FABMockApplicationIdentifierModel *appIDModel = [[FABMockApplicationIdentifierModel alloc ] init ];
4554 self.queue = [NSOperationQueue new ];
46-
47- self.fileManager = [[FIRCLSFileManager alloc ] init ];
55+ self.mockSettings = [[FIRCLSMockSettings alloc ] initWithFileManager: self .fileManager
56+ appIDModel: appIDModel];
57+ self.mockSettings .fetchedBundleID = self.bundleIdentifier ;
58+ self.networkClient = [[FIRCLSMockNetworkClient alloc ] initWithQueue: self .queue
59+ fileManager: self .fileManager
60+ delegate: nil ];
61+ self.fileManager = [[FIRCLSMockFileManager alloc ] init ];
4862 self.uploader = [[FIRCLSReportUploader alloc ] initWithQueue: self .queue
4963 delegate: self
5064 dataSource: self
51- client: nil
52- fileManager: nil
65+ client: self .networkClient
66+ fileManager: self .fileManager
5367 analytics: nil ];
54-
55- // glue together a string that will work for both platforms
56- NSString *urlString =
57- [NSString stringWithFormat: @" %@ /sdk-api/v1/platforms/%@ /apps/%@ /reports" , TestEndpoint,
58- FIRCLSApplicationGetPlatform (), self .bundleIdentifier];
59- self.url = [NSURL URLWithString: urlString];
68+ self.mockDataTransport = [[FIRMockGDTCORTransport alloc ] initWithMappingID: @" mappingID"
69+ transformers: nil
70+ target: 1206 ];
6071}
6172
6273- (void )tearDown {
@@ -65,8 +76,153 @@ - (void)tearDown {
6576 [super tearDown ];
6677}
6778
79+ #pragma mark - Tests
80+
6881- (void )testURLGeneration {
69- XCTAssertEqualObjects ([self .uploader reportURL ], _url);
82+ NSString *urlString =
83+ [NSString stringWithFormat: @" %@ /sdk-api/v1/platforms/%@ /apps/%@ /reports" , TestEndpoint,
84+ FIRCLSApplicationGetPlatform (), self .bundleIdentifier];
85+ NSURL *url = [NSURL URLWithString: urlString];
86+
87+ XCTAssertEqualObjects ([self .uploader reportURL ], url);
88+ }
89+
90+ - (void )testPrepareReport {
91+ NSString *path = [self .fileManager.activePath stringByAppendingPathComponent: @" pkg_uuid" ];
92+ FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc ] initWithPath: path];
93+ self.mockSettings .orgID = @" orgID" ;
94+ self.mockSettings .shouldUseNewReportEndpoint = YES ;
95+ self.fileManager .moveItemAtPathResult = [NSNumber numberWithInt: 1 ];
96+
97+ [self .uploader prepareAndSubmitReport: report
98+ dataCollectionToken: FIRCLSDataCollectionToken.validToken
99+ asUrgent: YES
100+ withProcessing: YES ];
101+
102+ // Verify with the last move operation is from processing -> prepared
103+ XCTAssertTrue (
104+ [self .fileManager.moveItemAtPath_destDir containsString: self .fileManager.preparedPath]);
105+ }
106+
107+ - (void )testPrepareLegacyReport {
108+ NSString *path = [self .fileManager.activePath stringByAppendingPathComponent: @" pkg_uuid" ];
109+ FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc ] initWithPath: path];
110+ self.mockSettings .orgID = @" orgID" ;
111+ self.mockSettings .shouldUseNewReportEndpoint = NO ;
112+ self.fileManager .moveItemAtPathResult = [NSNumber numberWithInt: 1 ];
113+
114+ [self .uploader prepareAndSubmitReport: report
115+ dataCollectionToken: FIRCLSDataCollectionToken.validToken
116+ asUrgent: YES
117+ withProcessing: YES ];
118+
119+ // Verify with the last move operation is from active -> processing for the legacy workflow
120+ // FIRCLSPackageReportOperation will then move the report from processing -> prepared-legacy
121+ XCTAssertTrue (
122+ [self .fileManager.moveItemAtPath_destDir containsString: self .fileManager.processingPath]);
123+ }
124+
125+ - (void )testUploadPackagedReportWithPath {
126+ [self runUploadPackagedReportWithUrgency: NO ];
127+ }
128+
129+ - (void )testUploadPackagedReportWithLegacyPath {
130+ [self runUploadPackagedReportLegacyWithUrgency: NO ];
131+ }
132+
133+ - (void )testUrgentUploadPackagedReportWithPath {
134+ [self runUploadPackagedReportWithUrgency: YES ];
135+ }
136+
137+ - (void )testUrgentUploadPackagedReportWithLegacyPath {
138+ [self runUploadPackagedReportLegacyWithUrgency: YES ];
139+ }
140+
141+ - (void )testUploadPackagedReportWithMismatchPathAndSettings {
142+ [self setUpForLegacyUpload ];
143+
144+ BOOL success = [self .uploader uploadPackagedReportAtPath: [self packagePath ]
145+ dataCollectionToken: FIRCLSDataCollectionToken.validToken
146+ asUrgent: NO ];
147+ XCTAssertFalse (success);
148+ XCTAssertNil (self.mockDataTransport .sendDataEvent_event );
149+ XCTAssertNil (self.networkClient .startUploadRequest );
150+ }
151+
152+ - (void )testUploadPackagedReportWithoutDataCollectionToken {
153+ [self setUpForUpload ];
154+
155+ BOOL success = [self .uploader uploadPackagedReportAtPath: [self packagePath ]
156+ dataCollectionToken: nil
157+ asUrgent: NO ];
158+ XCTAssertFalse (success);
159+ XCTAssertNil (self.mockDataTransport .sendDataEvent_event );
160+ XCTAssertNil (self.networkClient .startUploadRequest );
161+ }
162+
163+ - (void )testUploadPackagedReportNotGDTWritten {
164+ [self setUpForUpload ];
165+ self.mockDataTransport .sendDataEvent_wasWritten = NO ;
166+
167+ [self .uploader uploadPackagedReportAtPath: [self packagePath ] dataCollectionToken: nil asUrgent: NO ];
168+
169+ // Did not delete report
170+ XCTAssertNil (self.fileManager .removedItemAtPath_path );
171+ }
172+
173+ - (void )testUploadPackagedReportGDTError {
174+ [self setUpForUpload ];
175+ self.mockDataTransport .sendDataEvent_error = [[NSError alloc ] initWithDomain: @" domain"
176+ code: 1
177+ userInfo: nil ];
178+
179+ [self .uploader uploadPackagedReportAtPath: [self packagePath ] dataCollectionToken: nil asUrgent: NO ];
180+
181+ // Did not delete report
182+ XCTAssertNil (self.fileManager .removedItemAtPath_path );
183+ }
184+
185+ #pragma mark - Helper functions
186+
187+ - (NSString *)packagePath {
188+ return [self .fileManager.preparedPath stringByAppendingPathComponent: @" pkg_uuid" ];
189+ }
190+
191+ - (void )runUploadPackagedReportWithUrgency : (BOOL )urgent {
192+ [self setUpForUpload ];
193+
194+ BOOL success = [self .uploader uploadPackagedReportAtPath: [self packagePath ]
195+ dataCollectionToken: FIRCLSDataCollectionToken.validToken
196+ asUrgent: urgent];
197+ XCTAssertTrue (success);
198+ XCTAssertNotNil (self.mockDataTransport .sendDataEvent_event );
199+ XCTAssertNil (self.networkClient .startUploadRequest );
200+ XCTAssertEqualObjects (self.fileManager .removedItemAtPath_path , [self packagePath ]);
201+ }
202+
203+ - (void )runUploadPackagedReportLegacyWithUrgency : (BOOL )urgent {
204+ NSString *packagePath =
205+ [self .fileManager.legacyPreparedPath stringByAppendingPathComponent: @" pkg_uuid" ];
206+
207+ [self setUpForLegacyUpload ];
208+
209+ BOOL success = [self .uploader uploadPackagedReportAtPath: packagePath
210+ dataCollectionToken: FIRCLSDataCollectionToken.validToken
211+ asUrgent: urgent];
212+ XCTAssertTrue (success);
213+ XCTAssertNil (self.mockDataTransport .sendDataEvent_event );
214+ XCTAssertNotNil (self.networkClient .startUploadRequest );
215+ }
216+
217+ - (void )setUpForUpload {
218+ self.mockSettings .shouldUseNewReportEndpoint = YES ;
219+ self.mockDataTransport .sendDataEvent_wasWritten = YES ;
220+ }
221+
222+ - (void )setUpForLegacyUpload {
223+ self.mockSettings .shouldUseNewReportEndpoint = NO ;
224+ self.mockDataTransport .sendDataEvent_wasWritten = YES ;
225+ self.fileManager .fileSizeAtPathResult = [NSNumber numberWithInt: 1 ];
70226}
71227
72228#pragma mark - FIRCLSReportUploaderDelegate
@@ -76,6 +232,8 @@ - (void)didCompletePackageSubmission:(NSString *)path
76232 error : (NSError *)error {
77233}
78234
235+ #pragma mark - FIRCLSReportUploaderDataSource
236+
79237- (NSString *)bundleIdentifier {
80238 return @" com.test.TestApp" ;
81239}
@@ -84,16 +242,12 @@ - (NSString *)googleAppID {
84242 return @" someGoogleAppId" ;
85243}
86244
87- - (FIRCLSSettings *)settings {
88- FABMockApplicationIdentifierModel *appIDModel = [[FABMockApplicationIdentifierModel alloc ] init ];
89- FIRCLSMockSettings *settings = [[FIRCLSMockSettings alloc ] initWithFileManager: self .fileManager
90- appIDModel: appIDModel];
91- settings.fetchedBundleID = self.bundleIdentifier ;
92- return settings;
245+ - (GDTCORTransport *)googleTransport {
246+ return self.mockDataTransport ;
93247}
94248
95- - (GDTCORTransport *)googleTransport {
96- return [[FIRMockGDTCORTransport alloc ] initWithMappingID: @" mappingID " transformers: nil target: 0 ] ;
249+ - (FIRCLSSettings *)settings {
250+ return self. mockSettings ;
97251}
98252
99253- (void )didCompleteAllSubmissions {
0 commit comments