diff options
author | NicoleYarroch <nicole@livio.io> | 2017-08-17 14:33:23 -0400 |
---|---|---|
committer | NicoleYarroch <nicole@livio.io> | 2017-08-17 14:33:23 -0400 |
commit | 3bb84bb6dd8116256c2982e3aa7dc39bb86b6b61 (patch) | |
tree | b7d5c7fe66083aeada3bc380a9ea67ae4a6cb845 /SmartDeviceLinkTests | |
parent | fc6fadfe68b32aa539f189c9e478ee751159481f (diff) | |
download | sdl_ios-3bb84bb6dd8116256c2982e3aa7dc39bb86b6b61.tar.gz |
Test cases for uploading multiple files working
Signed-off-by: NicoleYarroch <nicole@livio.io>
Diffstat (limited to 'SmartDeviceLinkTests')
-rw-r--r-- | SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m | 1017 | ||||
-rw-r--r-- | SmartDeviceLinkTests/SDLFileManagerSpecMultipleFilesSpec.m | 402 |
2 files changed, 751 insertions, 668 deletions
diff --git a/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m b/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m index 898f4866d..4fe792788 100644 --- a/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m +++ b/SmartDeviceLinkTests/DevAPISpecs/SDLFileManagerSpec.m @@ -33,319 +33,383 @@ SDLFileManagerState *const SDLFileManagerStateReady = @"Ready"; QuickSpecBegin(SDLFileManagerSpec) -describe(@"Uploading multiple files", ^{ - __block TestMultipleFilesConnectionManager *testConnectionManager = nil; +describe(@"SDLFileManager", ^{ + __block TestConnectionManager *testConnectionManager = nil; __block SDLFileManager *testFileManager = nil; - __block NSUInteger initialSpaceAvailable = 0; - - __block NSMutableArray<NSString *> *testFileNames = nil; - __block NSMutableArray<SDLFile *> *testSDLFiles = nil; - __block NSMutableArray<NSData *> *testFileData = nil; - - __block NSUInteger testFileCount = 0; - __block SDLListFilesResponse *testListFilesResponse = nil; - // __block SDLPutFileResponse *testUploadFileResponse = nil; + __block NSUInteger initialSpaceAvailable = 250; beforeEach(^{ - testConnectionManager = [[TestMultipleFilesConnectionManager alloc] init]; + testConnectionManager = [[TestConnectionManager alloc] init]; testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager]; - initialSpaceAvailable = 9666; + testFileManager.suspended = YES; + }); + + describe(@"before starting", ^{ + it(@"should be in the shutdown state", ^{ + expect(testFileManager.currentState).to(match(SDLFileManagerStateShutdown)); + }); + + it(@"bytesAvailable should be 0", ^{ + expect(@(testFileManager.bytesAvailable)).to(equal(@0)); + }); - testListFilesResponse = [[SDLListFilesResponse alloc] init]; - testListFilesResponse.success = @YES; - testListFilesResponse.spaceAvailable = @(initialSpaceAvailable); - testListFilesResponse.filenames = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil]; + it(@"remoteFileNames should be empty", ^{ + expect(testFileManager.remoteFileNames).to(beEmpty()); + }); - testFileNames = [NSMutableArray array]; - testFileData = [NSMutableArray array]; - testSDLFiles = [NSMutableArray array]; + it(@"should have no pending operations", ^{ + expect(testFileManager.pendingTransactions).to(beEmpty()); + }); }); - context(@"After uploading", ^{ + describe(@"after receiving a start message", ^{ + __block BOOL startupSuccess = NO; + __block NSError *startupError = nil; + beforeEach(^{ - waitUntil(^(void (^done)(void)){ - [testFileManager startWithCompletionHandler:^(BOOL success, NSError * _Nullable error) { - done(); - }]; + [testFileManager startWithCompletionHandler:^(BOOL success, NSError * _Nullable error) { + startupSuccess = success; + startupError = error; + }]; - // Need to wait state machine transitions to complete before sending testListFilesResponse - [NSThread sleepForTimeInterval:0.3]; + testFileManager.suspended = NO; - [testConnectionManager respondToLastRequestWithResponse:testListFilesResponse]; - }); - - expect(testFileManager.currentState).to(equal(SDLFileManagerStateReady)); + [NSThread sleepForTimeInterval:0.1]; }); - context(@"When all files are uploaded successfully, no error is returned", ^{ - beforeEach(^{ - // testConnectionManager.multipleFileResponse = [[SDLPutFileResponse alloc] init]; - // testConnectionManager.multipleFileResponse.success = @(YES); - // testConnectionManager.multipleFileResponse.spaceAvailable = @(234); - // NSLog(@"response is: %@", [testConnectionManager.multipleFileResponse success]); - }); + it(@"should have queued a ListFiles request", ^{ + expect(testFileManager.pendingTransactions).to(haveCount(@1)); + expect(testFileManager.pendingTransactions.firstObject).to(beAnInstanceOf([SDLListFilesOperation class])); + }); - // it(@"should upload 1 small file from memory", ^{ - // testFileCount = 1; - // - // NSString *fileName = [NSString stringWithFormat:@"Test Small File Memory %d", 0]; - // NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; - // SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; - // file.overwrite = true; - // - // [testFileNames addObject: fileName]; - // [testFileData addObject:fileData]; - // [testSDLFiles addObject:file]; - // }); - // - // it(@"should upload 1 large file from disk", ^{ - // testFileCount = 1; - // - // NSString *imageName = @"testImage"; - // NSString *imageFilePath = [[NSBundle bundleForClass:[self class]] pathForResource:imageName ofType:@"png"]; - // NSURL *imageFileURL = [[NSURL alloc] initFileURLWithPath:imageFilePath]; - // - // NSString *fileName = [NSString stringWithFormat:@"Test Large File Disk %d", 0]; - // SDLFile *file = [SDLFile fileAtFileURL:imageFileURL name:fileName]; - // file.overwrite = true; - // - // [testFileNames addObject: fileName]; - // [testFileData addObject: [[NSData alloc] initWithContentsOfURL:imageFileURL]]; - // [testSDLFiles addObject:file]; - // }); - // - // it(@"should upload multiple small files from memory", ^{ - // testFileCount = 5; - // for(int i = 0; i < testFileCount; i += 1) { - // NSString *fileName = [NSString stringWithFormat:@"Test Files %d", i]; - // NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; - // SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; - // file.overwrite = true; - // [testFileNames addObject: fileName]; - // [testFileData addObject:fileData]; - // [testSDLFiles addObject:file]; - // } - // }); - // - // it(@"should upload a large number of small files from memory", ^{ - // testFileCount = 500; - // for(int i = 0; i < testFileCount; i += 1) { - // NSString *fileName = [NSString stringWithFormat:@"Test Files %d", i]; - // NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; - // SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; - // file.overwrite = true; - // [testFileNames addObject: fileName]; - // [testFileData addObject:fileData]; - // [testSDLFiles addObject:file]; - // } - // }); - // - // it(@"should upload 5 small files from disk", ^{ - // testFileCount = 5; - // NSURL *imageFileURL = nil; - // - // for(int i = 0; i < testFileCount; i += 1) { - // NSString *imageName = [NSString stringWithFormat:@"testImage%d", i]; - // NSString *imageFilePath = [[NSBundle bundleForClass:[self class]] pathForResource:imageName ofType:@"png"]; - // imageFileURL = [[NSURL alloc] initFileURLWithPath:imageFilePath]; - // - // NSString *fileName = [NSString stringWithFormat:@"TestMultipleLargeFilesDisk%d", i]; - // SDLFile *file = [SDLFile fileAtFileURL:imageFileURL name:fileName]; - // - // file.overwrite = true; - // [testFileNames addObject: fileName]; - // [testFileData addObject:[[NSData alloc] initWithContentsOfURL:imageFileURL]]; - // [testSDLFiles addObject:file]; - // } - // }); - // - // it(@"should upload multiple files from memory and on disk", ^{ - // testFileCount = 10; - // for(int i = 0; i < testFileCount; i += 1) { - // NSString *fileName = [NSString stringWithFormat:@"TestMultipleSDiskAndMemory%d", i]; - // NSData *fileData = nil; - // SDLFile *testFile = nil; - // - // if (i < 5) { - // NSString *textFileName = [NSString stringWithFormat:@"testImageA%d", i]; - // NSString *textFilePath = [[NSBundle bundleForClass:[self class]] pathForResource:textFileName ofType:@"png"]; - // NSURL *textFileURL = [[NSURL alloc] initFileURLWithPath:textFilePath]; - // - // fileData = [[NSData alloc] initWithContentsOfURL:textFileURL]; - // testFile = [SDLFile fileAtFileURL:textFileURL name:fileName]; - // } else { - // fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; - // testFile = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; - // } - // - // testFile.overwrite = true; - // [testFileNames addObject: fileName]; - // [testFileData addObject:fileData]; - // [testSDLFiles addObject:testFile]; - // } - // }); - - afterEach(^{ - waitUntilTimeout(10, ^(void (^done)(void)){ - [testFileManager uploadFiles:testSDLFiles completionHandler:^(NSError * _Nullable error) { - expect(error).to(beNil()); - done(); - }]; - }); - }); + it(@"should be in the fetching initial list state", ^{ + expect(testFileManager.currentState).to(match(SDLFileManagerStateFetchingInitialList)); }); + describe(@"after receiving a ListFiles response", ^{ + __block SDLListFilesResponse *testListFilesResponse = nil; + __block NSSet<NSString *> *testInitialFileNames = nil; - context(@"When files are not uploaded successfully", ^{ - __block NSMutableDictionary *testResponses; + beforeEach(^{ + testInitialFileNames = [NSSet setWithArray:@[@"testFile1", @"testFile2", @"testFile3"]]; - __block NSNumber *expectedSpaceLeft; - __block NSMutableDictionary *expectedFailedUploads; - __block NSError *expectedError = nil; - __block NSMutableArray *expectedSuccessfulFileNames; + testListFilesResponse = [[SDLListFilesResponse alloc] init]; + testListFilesResponse.success = @YES; + testListFilesResponse.spaceAvailable = @(initialSpaceAvailable); + testListFilesResponse.filenames = [NSArray arrayWithArray:[testInitialFileNames allObjects]]; - beforeEach(^{ - testResponses = [[NSMutableDictionary alloc] init]; - expectedFailedUploads = [[NSMutableDictionary alloc] init]; - expectedSuccessfulFileNames = [NSMutableArray array]; + [testConnectionManager respondToLastRequestWithResponse:testListFilesResponse]; }); - it(@"should return an error when all files fail", ^{ - testFileCount = 5; - for(int i = 0; i < testFileCount; i += 1) { - NSString *fileName = [NSString stringWithFormat:@"Test Files Unsuccessful %d", i]; - NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; - SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; - file.overwrite = true; - [testFileNames addObject: fileName]; - [testFileData addObject:fileData]; - [testSDLFiles addObject:file]; - - // Failed response - SDLPutFileResponse *response = [[SDLPutFileResponse alloc] init]; - response.spaceAvailable = @(333); - response.success = @NO; - - // Failed error - NSString *responseErrorDescription = [NSString stringWithFormat:@"file upload failed: %d", i]; - NSString *responseErrorReason = [NSString stringWithFormat:@"some error reason: %d", i]; - NSError *responseError = [NSError sdl_lifecycle_unknownRemoteErrorWithDescription:responseErrorDescription andReason:responseErrorReason]; - - TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; - testResponses[fileName] = testResponse; - expectedFailedUploads[fileName] = responseError; - } - - testConnectionManager.responses = testResponses; - expectedError = [NSError sdl_fileManager_unableToUploadError:expectedFailedUploads]; - expectedSpaceLeft = [[NSNumber alloc] initWithInteger:initialSpaceAvailable]; + it(@"the file manager should be in the correct state", ^{ + expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); + expect(testFileManager.remoteFileNames).toEventually(equal(testInitialFileNames)); + expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable))); }); - it(@"should return an error when the first file fails", ^{ - testFileCount = 6; - NSInteger space = initialSpaceAvailable; - for(int i = 0; i < testFileCount; i += 1) { - NSString *fileName = [NSString stringWithFormat:@"Test Files Unsuccessful %d", i]; - NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; - SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; - file.overwrite = true; - [testFileNames addObject: fileName]; - [testFileData addObject:fileData]; - [testSDLFiles addObject:file]; - - SDLPutFileResponse *response = [[SDLPutFileResponse alloc] init]; - NSError *responseError = nil; - if (i == 0) { - // Failed response - response.spaceAvailable = @(space); - response.success = @NO; - - // Failed error - NSString *responseErrorDescription = [NSString stringWithFormat:@"file upload failed: %d", i]; - NSString *responseErrorReason = [NSString stringWithFormat:@"some error reason: %d", i]; - responseError = [NSError sdl_lifecycle_unknownRemoteErrorWithDescription:responseErrorDescription andReason:responseErrorReason]; - - expectedFailedUploads[fileName] = responseError; - } else { - // Successful response - space -= 1; - response.spaceAvailable = @(space); - response.success = @YES; - - // No error - responseError = nil; - - [expectedSuccessfulFileNames addObject:fileName]; - } - - TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; - testResponses[fileName] = testResponse; - } - - testConnectionManager.responses = testResponses; - expectedError = [NSError sdl_fileManager_unableToUploadError:expectedFailedUploads]; - expectedSpaceLeft = [[NSNumber alloc] initWithInteger:space]; - }); + describe(@"deleting a file", ^{ + __block BOOL completionSuccess = NO; + __block NSUInteger completionBytesAvailable = 0; + __block NSError *completionError = nil; + + context(@"when the file is unknown", ^{ + beforeEach(^{ + NSString *someUnknownFileName = @"Some Unknown File Name"; + [testFileManager deleteRemoteFileWithName:someUnknownFileName completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { + completionSuccess = success; + completionBytesAvailable = bytesAvailable; + completionError = error; + }]; + + [NSThread sleepForTimeInterval:0.1]; + }); + + it(@"should return the correct data", ^{ + expect(@(completionSuccess)).toEventually(equal(@NO)); + expect(@(completionBytesAvailable)).toEventually(equal(@250)); + expect(completionError).toEventually(equal([NSError sdl_fileManager_noKnownFileError])); + }); + + it(@"should not have deleted any files in the file manager", ^{ + expect(testFileManager.remoteFileNames).toEventually(haveCount(@(testInitialFileNames.count))); + }); + }); - it(@"should return an error when the last file fails", ^{ - testFileCount = 6; - NSInteger space = initialSpaceAvailable; - for(int i = 0; i < testFileCount; i += 1) { - NSString *fileName = [NSString stringWithFormat:@"Test Files Unsuccessful %d", i]; - NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; - SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; - file.overwrite = true; - [testFileNames addObject: fileName]; - [testFileData addObject:fileData]; - [testSDLFiles addObject:file]; - - SDLPutFileResponse *response = [[SDLPutFileResponse alloc] init]; - NSError *responseError = nil; - if (i == testFileCount - 1) { - // Failed response - response.spaceAvailable = @(space); - response.success = @NO; - - // Failed error - NSString *responseErrorDescription = [NSString stringWithFormat:@"file upload failed: %d", i]; - NSString *responseErrorReason = [NSString stringWithFormat:@"some error reason: %d", i]; - responseError = [NSError sdl_lifecycle_unknownRemoteErrorWithDescription:responseErrorDescription andReason:responseErrorReason]; - - expectedFailedUploads[fileName] = responseError; - } else { - // Successful response - space -= 5; - response.spaceAvailable = @(space); - response.success = @YES; - - // No error - responseError = nil; - - [expectedSuccessfulFileNames addObject:fileName]; - } - - TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; - testResponses[fileName] = testResponse; - } - - testConnectionManager.responses = testResponses; - expectedError = [NSError sdl_fileManager_unableToUploadError:expectedFailedUploads]; - expectedSpaceLeft = [[NSNumber alloc] initWithInteger:space]; + context(@"when the file is known", ^{ + __block NSUInteger newSpaceAvailable = 600; + __block NSString *someKnownFileName = nil; + __block BOOL completionSuccess = NO; + __block NSUInteger completionBytesAvailable = 0; + __block NSError *completionError = nil; + + beforeEach(^{ + someKnownFileName = [testInitialFileNames anyObject]; + [testFileManager deleteRemoteFileWithName:someKnownFileName completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { + completionSuccess = success; + completionBytesAvailable = bytesAvailable; + completionError = error; + }]; + + SDLDeleteFileResponse *deleteResponse = [[SDLDeleteFileResponse alloc] init]; + deleteResponse.success = @YES; + deleteResponse.spaceAvailable = @(newSpaceAvailable); + + [NSThread sleepForTimeInterval:0.1]; + + [testConnectionManager respondToLastRequestWithResponse:deleteResponse]; + }); + + it(@"should return the correct data", ^{ + expect(@(completionSuccess)).to(equal(@YES)); + expect(@(completionBytesAvailable)).to(equal(@(newSpaceAvailable))); + expect(@(testFileManager.bytesAvailable)).to(equal(@(newSpaceAvailable))); + expect(completionError).to(beNil()); + }); + + it(@"should have removed the file from the file manager", ^{ + expect(testFileManager.remoteFileNames).toNot(contain(someKnownFileName)); + }); + }); }); - afterEach(^{ - waitUntilTimeout(10, ^(void (^done)(void)){ - [testFileManager uploadFiles:testSDLFiles completionHandler:^(NSError * _Nullable error) { - expect(error).to(equal(expectedError)); - done(); - }]; + describe(@"uploading a new file", ^{ + __block NSString *testFileName = nil; + __block SDLFile *testUploadFile = nil; + __block BOOL completionSuccess = NO; + __block NSUInteger completionBytesAvailable = 0; + __block NSError *completionError = nil; + + __block SDLPutFile *sentPutFile = nil; + __block NSData *testFileData = nil; + + context(@"when there is a remote file named the same thing", ^{ + beforeEach(^{ + testFileName = [testInitialFileNames anyObject]; + testFileData = [@"someData" dataUsingEncoding:NSUTF8StringEncoding]; + testUploadFile = [SDLFile fileWithData:testFileData name:testFileName fileExtension:@"bin"]; + }); + + context(@"when the file's overwrite property is YES", ^{ + beforeEach(^{ + testUploadFile.overwrite = YES; + + [testFileManager uploadFile:testUploadFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { + completionSuccess = success; + completionBytesAvailable = bytesAvailable; + completionError = error; + }]; + + [NSThread sleepForTimeInterval:0.1]; + + sentPutFile = testConnectionManager.receivedRequests.lastObject; + }); + + it(@"should set the file manager state to be waiting", ^{ + expect(testFileManager.currentState).to(match(SDLFileManagerStateReady)); + }); + + it(@"should create a putfile with the correct data", ^{ + expect(sentPutFile.length).to(equal(@(testFileData.length))); + expect(sentPutFile.bulkData).to(equal(testFileData)); + expect(sentPutFile.fileType).to(match(SDLFileTypeBinary)); + }); + + context(@"when the response returns without error", ^{ + __block SDLPutFileResponse *testResponse = nil; + __block NSNumber *testResponseSuccess = nil; + __block NSNumber *testResponseBytesAvailable = nil; + + beforeEach(^{ + testResponseBytesAvailable = @750; + testResponseSuccess = @YES; + + testResponse = [[SDLPutFileResponse alloc] init]; + testResponse.success = testResponseSuccess; + testResponse.spaceAvailable = testResponseBytesAvailable; + + [testConnectionManager respondToLastRequestWithResponse:testResponse]; + }); + + it(@"should set the file manager data correctly", ^{ + expect(@(testFileManager.bytesAvailable)).toEventually(equal(testResponseBytesAvailable)); + expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); + expect(testFileManager.remoteFileNames).toEventually(contain(testFileName)); + }); + + it(@"should call the completion handler with the correct data", ^{ + expect(@(completionBytesAvailable)).toEventually(equal(testResponseBytesAvailable)); + expect(@(completionSuccess)).toEventually(equal(@YES)); + expect(completionError).to(beNil()); + }); + }); + + context(@"when the connection returns failure", ^{ + __block SDLPutFileResponse *testResponse = nil; + __block NSNumber *testResponseBytesAvailable = nil; + __block NSNumber *testResponseSuccess = nil; + + beforeEach(^{ + testResponseBytesAvailable = @750; + testResponseSuccess = @NO; + + testResponse = [[SDLPutFileResponse alloc] init]; + testResponse.spaceAvailable = testResponseBytesAvailable; + testResponse.success = testResponseSuccess; + + [testConnectionManager respondToLastRequestWithResponse:testResponse]; + }); + + it(@"should set the file manager data correctly", ^{ + expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable))); + expect(testFileManager.remoteFileNames).toEventually(contain(testFileName)); + expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); + }); + + it(@"should call the completion handler with the correct data", ^{ + expect(@(completionBytesAvailable)).to(equal(@0)); + expect(@(completionSuccess)).to(equal(testResponseSuccess)); + expect(completionError).toEventually(beNil()); + }); + }); + + context(@"when the connection errors without a response", ^{ + beforeEach(^{ + [testConnectionManager respondToLastRequestWithResponse:nil error:[NSError sdl_lifecycle_notReadyError]]; + }); + + it(@"should have the correct file manager state", ^{ + expect(testFileManager.remoteFileNames).to(contain(testFileName)); + expect(testFileManager.currentState).to(match(SDLFileManagerStateReady)); + }); + + it(@"should call the completion handler with correct data", ^{ + expect(completionError).toEventually(equal([NSError sdl_lifecycle_notReadyError])); + }); + }); + }); + + context(@"when allow overwrite is false", ^{ + __block SDLRPCRequest *lastRequest = nil; + + beforeEach(^{ + testUploadFile.overwrite = NO; + + [testFileManager uploadFile:testUploadFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { + completionSuccess = success; + completionBytesAvailable = bytesAvailable; + completionError = error; + }]; + + [NSThread sleepForTimeInterval:0.1]; + + lastRequest = testConnectionManager.receivedRequests.lastObject; + }); + + it(@"should have called the completion handler with correct data", ^{ + expect(lastRequest).toNot(beAnInstanceOf([SDLPutFile class])); + expect(@(completionSuccess)).to(equal(@NO)); + expect(@(completionBytesAvailable)).to(equal(@(testFileManager.bytesAvailable))); + expect(completionError).to(equal([NSError sdl_fileManager_cannotOverwriteError])); + }); + }); }); - expect(@(testFileManager.bytesAvailable)).to(equal(expectedSpaceLeft)); - expect(testFileManager.currentState).to(match(SDLFileManagerStateReady)); - for(int i = 0; i < expectedSuccessfulFileNames.count; i += 1) { - expect(testFileManager.remoteFileNames).to(contain(expectedSuccessfulFileNames[i])); - } + context(@"when there is not a remote file named the same thing", ^{ + beforeEach(^{ + testFileName = @"not a test file"; + testFileData = [@"someData" dataUsingEncoding:NSUTF8StringEncoding]; + testUploadFile = [SDLFile fileWithData:testFileData name:testFileName fileExtension:@"bin"]; + + [testFileManager uploadFile:testUploadFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { + completionSuccess = success; + completionBytesAvailable = bytesAvailable; + completionError = error; + }]; + + [NSThread sleepForTimeInterval:0.1]; + + sentPutFile = (SDLPutFile *)testConnectionManager.receivedRequests.lastObject; + }); + + it(@"should not have testFileName in the files set", ^{ + expect(testInitialFileNames).toNot(contain(testFileName)); + }); + + context(@"when the connection returns without error", ^{ + __block SDLPutFileResponse *testResponse = nil; + __block NSNumber *testResponseSuccess = nil; + __block NSNumber *testResponseBytesAvailable = nil; + + beforeEach(^{ + testResponseBytesAvailable = @750; + testResponseSuccess = @YES; + + testResponse = [[SDLPutFileResponse alloc] init]; + testResponse.success = testResponseSuccess; + testResponse.spaceAvailable = testResponseBytesAvailable; + + [testConnectionManager respondToLastRequestWithResponse:testResponse]; + }); + + it(@"should set the file manager state correctly", ^{ + expect(@(testFileManager.bytesAvailable)).toEventually(equal(testResponseBytesAvailable)); + expect(testFileManager.remoteFileNames).toEventually(contain(testFileName)); + expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); + }); + + it(@"should call the completion handler with the correct data", ^{ + expect(@(completionBytesAvailable)).toEventually(equal(testResponseBytesAvailable)); + expect(@(completionSuccess)).toEventually(equal(@YES)); + expect(completionError).to(beNil()); + }); + }); + + context(@"when the connection returns failure", ^{ + __block SDLPutFileResponse *testResponse = nil; + __block NSNumber *testResponseBytesAvailable = nil; + __block NSNumber *testResponseSuccess = nil; + + beforeEach(^{ + testResponseBytesAvailable = @750; + testResponseSuccess = @NO; + + testResponse = [[SDLPutFileResponse alloc] init]; + testResponse.spaceAvailable = testResponseBytesAvailable; + testResponse.success = testResponseSuccess; + + testFileManager.accessibilityHint = @"This doesn't matter"; + + [testConnectionManager respondToLastRequestWithResponse:testResponse]; + }); + + it(@"should set the file manager state correctly", ^{ + expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable))); + expect(testFileManager.remoteFileNames).toEventuallyNot(contain(testFileName)); + expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); + }); + + it(@"should call the completion handler with the correct data", ^{ + expect(@(completionBytesAvailable)).to(equal(@0)); + expect(@(completionSuccess)).to(equal(testResponseSuccess)); + expect(completionError).toEventually(beNil()); + }); + }); + + context(@"when the connection errors without a response", ^{ + beforeEach(^{ + [testConnectionManager respondToLastRequestWithResponse:nil error:[NSError sdl_lifecycle_notReadyError]]; + }); + + it(@"should set the file manager state correctly", ^{ + expect(testFileManager.remoteFileNames).toNot(contain(testFileName)); + expect(testFileManager.currentState).to(match(SDLFileManagerStateReady)); + }); + + it(@"should call the completion handler with nil error", ^{ + expect(completionError).toEventually(equal([NSError sdl_lifecycle_notReadyError])); + }); + }); + }); }); }); @@ -355,387 +419,4 @@ describe(@"Uploading multiple files", ^{ }); }); -//describe(@"SDLFileManager", ^{ -// __block TestConnectionManager *testConnectionManager = nil; -// __block SDLFileManager *testFileManager = nil; -// __block NSUInteger initialSpaceAvailable = 250; -// -// beforeEach(^{ -// testConnectionManager = [[TestConnectionManager alloc] init]; -// testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager]; -// testFileManager.suspended = YES; -// }); -// -// describe(@"before starting", ^{ -// it(@"should be in the shutdown state", ^{ -// expect(testFileManager.currentState).to(match(SDLFileManagerStateShutdown)); -// }); -// -// it(@"bytesAvailable should be 0", ^{ -// expect(@(testFileManager.bytesAvailable)).to(equal(@0)); -// }); -// -// it(@"remoteFileNames should be empty", ^{ -// expect(testFileManager.remoteFileNames).to(beEmpty()); -// }); -// -// it(@"should have no pending operations", ^{ -// expect(testFileManager.pendingTransactions).to(beEmpty()); -// }); -// }); -// -// describe(@"after receiving a start message", ^{ -// __block BOOL startupSuccess = NO; -// __block NSError *startupError = nil; -// -// beforeEach(^{ -// [testFileManager startWithCompletionHandler:^(BOOL success, NSError * _Nullable error) { -// startupSuccess = success; -// startupError = error; -// }]; -// -// testFileManager.suspended = NO; -// -// [NSThread sleepForTimeInterval:0.1]; -// }); -// -// it(@"should have queued a ListFiles request", ^{ -// expect(testFileManager.pendingTransactions).to(haveCount(@1)); -// expect(testFileManager.pendingTransactions.firstObject).to(beAnInstanceOf([SDLListFilesOperation class])); -// }); -// -// it(@"should be in the fetching initial list state", ^{ -// expect(testFileManager.currentState).to(match(SDLFileManagerStateFetchingInitialList)); -// }); -// -// describe(@"after receiving a ListFiles response", ^{ -// __block SDLListFilesResponse *testListFilesResponse = nil; -// __block NSSet<NSString *> *testInitialFileNames = nil; -// -// beforeEach(^{ -// testInitialFileNames = [NSSet setWithArray:@[@"testFile1", @"testFile2", @"testFile3"]]; -// -// testListFilesResponse = [[SDLListFilesResponse alloc] init]; -// testListFilesResponse.success = @YES; -// testListFilesResponse.spaceAvailable = @(initialSpaceAvailable); -// testListFilesResponse.filenames = [NSArray arrayWithArray:[testInitialFileNames allObjects]]; -// -// [testConnectionManager respondToLastRequestWithResponse:testListFilesResponse]; -// }); -// -// it(@"the file manager should be in the correct state", ^{ -// expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); -// expect(testFileManager.remoteFileNames).toEventually(equal(testInitialFileNames)); -// expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable))); -// }); -// -// describe(@"deleting a file", ^{ -// __block BOOL completionSuccess = NO; -// __block NSUInteger completionBytesAvailable = 0; -// __block NSError *completionError = nil; -// -// context(@"when the file is unknown", ^{ -// beforeEach(^{ -// NSString *someUnknownFileName = @"Some Unknown File Name"; -// [testFileManager deleteRemoteFileWithName:someUnknownFileName completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { -// completionSuccess = success; -// completionBytesAvailable = bytesAvailable; -// completionError = error; -// }]; -// -// [NSThread sleepForTimeInterval:0.1]; -// }); -// -// it(@"should return the correct data", ^{ -// expect(@(completionSuccess)).toEventually(equal(@NO)); -// expect(@(completionBytesAvailable)).toEventually(equal(@250)); -// expect(completionError).toEventually(equal([NSError sdl_fileManager_noKnownFileError])); -// }); -// -// it(@"should not have deleted any files in the file manager", ^{ -// expect(testFileManager.remoteFileNames).toEventually(haveCount(@(testInitialFileNames.count))); -// }); -// }); -// -// context(@"when the file is known", ^{ -// __block NSUInteger newSpaceAvailable = 600; -// __block NSString *someKnownFileName = nil; -// __block BOOL completionSuccess = NO; -// __block NSUInteger completionBytesAvailable = 0; -// __block NSError *completionError = nil; -// -// beforeEach(^{ -// someKnownFileName = [testInitialFileNames anyObject]; -// [testFileManager deleteRemoteFileWithName:someKnownFileName completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { -// completionSuccess = success; -// completionBytesAvailable = bytesAvailable; -// completionError = error; -// }]; -// -// SDLDeleteFileResponse *deleteResponse = [[SDLDeleteFileResponse alloc] init]; -// deleteResponse.success = @YES; -// deleteResponse.spaceAvailable = @(newSpaceAvailable); -// -// [NSThread sleepForTimeInterval:0.1]; -// -// [testConnectionManager respondToLastRequestWithResponse:deleteResponse]; -// }); -// -// it(@"should return the correct data", ^{ -// expect(@(completionSuccess)).to(equal(@YES)); -// expect(@(completionBytesAvailable)).to(equal(@(newSpaceAvailable))); -// expect(@(testFileManager.bytesAvailable)).to(equal(@(newSpaceAvailable))); -// expect(completionError).to(beNil()); -// }); -// -// it(@"should have removed the file from the file manager", ^{ -// expect(testFileManager.remoteFileNames).toNot(contain(someKnownFileName)); -// }); -// }); -// }); -// -// describe(@"uploading a new file", ^{ -// __block NSString *testFileName = nil; -// __block SDLFile *testUploadFile = nil; -// __block BOOL completionSuccess = NO; -// __block NSUInteger completionBytesAvailable = 0; -// __block NSError *completionError = nil; -// -// __block SDLPutFile *sentPutFile = nil; -// __block NSData *testFileData = nil; -// -// context(@"when there is a remote file named the same thing", ^{ -// beforeEach(^{ -// testFileName = [testInitialFileNames anyObject]; -// testFileData = [@"someData" dataUsingEncoding:NSUTF8StringEncoding]; -// testUploadFile = [SDLFile fileWithData:testFileData name:testFileName fileExtension:@"bin"]; -// }); -// -// context(@"when the file's overwrite property is YES", ^{ -// beforeEach(^{ -// testUploadFile.overwrite = YES; -// -// [testFileManager uploadFile:testUploadFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { -// completionSuccess = success; -// completionBytesAvailable = bytesAvailable; -// completionError = error; -// }]; -// -// [NSThread sleepForTimeInterval:0.1]; -// -// sentPutFile = testConnectionManager.receivedRequests.lastObject; -// }); -// -// it(@"should set the file manager state to be waiting", ^{ -// expect(testFileManager.currentState).to(match(SDLFileManagerStateReady)); -// }); -// -// it(@"should create a putfile with the correct data", ^{ -// expect(sentPutFile.length).to(equal(@(testFileData.length))); -// expect(sentPutFile.bulkData).to(equal(testFileData)); -// expect(sentPutFile.fileType).to(match(SDLFileTypeBinary)); -// }); -// -// context(@"when the response returns without error", ^{ -// __block SDLPutFileResponse *testResponse = nil; -// __block NSNumber *testResponseSuccess = nil; -// __block NSNumber *testResponseBytesAvailable = nil; -// -// beforeEach(^{ -// testResponseBytesAvailable = @750; -// testResponseSuccess = @YES; -// -// testResponse = [[SDLPutFileResponse alloc] init]; -// testResponse.success = testResponseSuccess; -// testResponse.spaceAvailable = testResponseBytesAvailable; -// -// [testConnectionManager respondToLastRequestWithResponse:testResponse]; -// }); -// -// it(@"should set the file manager data correctly", ^{ -// expect(@(testFileManager.bytesAvailable)).toEventually(equal(testResponseBytesAvailable)); -// expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); -// expect(testFileManager.remoteFileNames).toEventually(contain(testFileName)); -// }); -// -// it(@"should call the completion handler with the correct data", ^{ -// expect(@(completionBytesAvailable)).toEventually(equal(testResponseBytesAvailable)); -// expect(@(completionSuccess)).toEventually(equal(@YES)); -// expect(completionError).to(beNil()); -// }); -// }); -// -// context(@"when the connection returns failure", ^{ -// __block SDLPutFileResponse *testResponse = nil; -// __block NSNumber *testResponseBytesAvailable = nil; -// __block NSNumber *testResponseSuccess = nil; -// -// beforeEach(^{ -// testResponseBytesAvailable = @750; -// testResponseSuccess = @NO; -// -// testResponse = [[SDLPutFileResponse alloc] init]; -// testResponse.spaceAvailable = testResponseBytesAvailable; -// testResponse.success = testResponseSuccess; -// -// [testConnectionManager respondToLastRequestWithResponse:testResponse]; -// }); -// -// it(@"should set the file manager data correctly", ^{ -// expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable))); -// expect(testFileManager.remoteFileNames).toEventually(contain(testFileName)); -// expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); -// }); -// -// it(@"should call the completion handler with the correct data", ^{ -// expect(@(completionBytesAvailable)).to(equal(@0)); -// expect(@(completionSuccess)).to(equal(testResponseSuccess)); -// expect(completionError).toEventually(beNil()); -// }); -// }); -// -// context(@"when the connection errors without a response", ^{ -// beforeEach(^{ -// [testConnectionManager respondToLastRequestWithResponse:nil error:[NSError sdl_lifecycle_notReadyError]]; -// }); -// -// it(@"should have the correct file manager state", ^{ -// expect(testFileManager.remoteFileNames).to(contain(testFileName)); -// expect(testFileManager.currentState).to(match(SDLFileManagerStateReady)); -// }); -// -// it(@"should call the completion handler with correct data", ^{ -// expect(completionError).toEventually(equal([NSError sdl_lifecycle_notReadyError])); -// }); -// }); -// }); -// -// context(@"when allow overwrite is false", ^{ -// __block SDLRPCRequest *lastRequest = nil; -// -// beforeEach(^{ -// testUploadFile.overwrite = NO; -// -// [testFileManager uploadFile:testUploadFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { -// completionSuccess = success; -// completionBytesAvailable = bytesAvailable; -// completionError = error; -// }]; -// -// [NSThread sleepForTimeInterval:0.1]; -// -// lastRequest = testConnectionManager.receivedRequests.lastObject; -// }); -// -// it(@"should have called the completion handler with correct data", ^{ -// expect(lastRequest).toNot(beAnInstanceOf([SDLPutFile class])); -// expect(@(completionSuccess)).to(equal(@NO)); -// expect(@(completionBytesAvailable)).to(equal(@(testFileManager.bytesAvailable))); -// expect(completionError).to(equal([NSError sdl_fileManager_cannotOverwriteError])); -// }); -// }); -// }); -// -// context(@"when there is not a remote file named the same thing", ^{ -// beforeEach(^{ -// testFileName = @"not a test file"; -// testFileData = [@"someData" dataUsingEncoding:NSUTF8StringEncoding]; -// testUploadFile = [SDLFile fileWithData:testFileData name:testFileName fileExtension:@"bin"]; -// -// [testFileManager uploadFile:testUploadFile completionHandler:^(BOOL success, NSUInteger bytesAvailable, NSError * _Nullable error) { -// completionSuccess = success; -// completionBytesAvailable = bytesAvailable; -// completionError = error; -// }]; -// -// [NSThread sleepForTimeInterval:0.1]; -// -// sentPutFile = (SDLPutFile *)testConnectionManager.receivedRequests.lastObject; -// }); -// -// it(@"should not have testFileName in the files set", ^{ -// expect(testInitialFileNames).toNot(contain(testFileName)); -// }); -// -// context(@"when the connection returns without error", ^{ -// __block SDLPutFileResponse *testResponse = nil; -// __block NSNumber *testResponseSuccess = nil; -// __block NSNumber *testResponseBytesAvailable = nil; -// -// beforeEach(^{ -// testResponseBytesAvailable = @750; -// testResponseSuccess = @YES; -// -// testResponse = [[SDLPutFileResponse alloc] init]; -// testResponse.success = testResponseSuccess; -// testResponse.spaceAvailable = testResponseBytesAvailable; -// -// [testConnectionManager respondToLastRequestWithResponse:testResponse]; -// }); -// -// it(@"should set the file manager state correctly", ^{ -// expect(@(testFileManager.bytesAvailable)).toEventually(equal(testResponseBytesAvailable)); -// expect(testFileManager.remoteFileNames).toEventually(contain(testFileName)); -// expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); -// }); -// -// it(@"should call the completion handler with the correct data", ^{ -// expect(@(completionBytesAvailable)).toEventually(equal(testResponseBytesAvailable)); -// expect(@(completionSuccess)).toEventually(equal(@YES)); -// expect(completionError).to(beNil()); -// }); -// }); -// -// context(@"when the connection returns failure", ^{ -// __block SDLPutFileResponse *testResponse = nil; -// __block NSNumber *testResponseBytesAvailable = nil; -// __block NSNumber *testResponseSuccess = nil; -// -// beforeEach(^{ -// testResponseBytesAvailable = @750; -// testResponseSuccess = @NO; -// -// testResponse = [[SDLPutFileResponse alloc] init]; -// testResponse.spaceAvailable = testResponseBytesAvailable; -// testResponse.success = testResponseSuccess; -// -// testFileManager.accessibilityHint = @"This doesn't matter"; -// -// [testConnectionManager respondToLastRequestWithResponse:testResponse]; -// }); -// -// it(@"should set the file manager state correctly", ^{ -// expect(@(testFileManager.bytesAvailable)).toEventually(equal(@(initialSpaceAvailable))); -// expect(testFileManager.remoteFileNames).toEventuallyNot(contain(testFileName)); -// expect(testFileManager.currentState).toEventually(match(SDLFileManagerStateReady)); -// }); -// -// it(@"should call the completion handler with the correct data", ^{ -// expect(@(completionBytesAvailable)).to(equal(@0)); -// expect(@(completionSuccess)).to(equal(testResponseSuccess)); -// expect(completionError).toEventually(beNil()); -// }); -// }); -// -// context(@"when the connection errors without a response", ^{ -// beforeEach(^{ -// [testConnectionManager respondToLastRequestWithResponse:nil error:[NSError sdl_lifecycle_notReadyError]]; -// }); -// -// it(@"should set the file manager state correctly", ^{ -// expect(testFileManager.remoteFileNames).toNot(contain(testFileName)); -// expect(testFileManager.currentState).to(match(SDLFileManagerStateReady)); -// }); -// -// it(@"should call the completion handler with nil error", ^{ -// expect(completionError).toEventually(equal([NSError sdl_lifecycle_notReadyError])); -// }); -// }); -// }); -// }); -// }); -// }); -//}); - - QuickSpecEnd diff --git a/SmartDeviceLinkTests/SDLFileManagerSpecMultipleFilesSpec.m b/SmartDeviceLinkTests/SDLFileManagerSpecMultipleFilesSpec.m new file mode 100644 index 000000000..83c62f68f --- /dev/null +++ b/SmartDeviceLinkTests/SDLFileManagerSpecMultipleFilesSpec.m @@ -0,0 +1,402 @@ +// +// SDLFileManagerSpecMultipleFilesSpec.m +// SmartDeviceLink-iOS +// +// Created by Nicole on 8/17/17. +// Copyright © 2017 smartdevicelink. All rights reserved. +// + +#import <Quick/Quick.h> +#import <Nimble/Nimble.h> + +#import "SDLDeleteFileResponse.h" +#import "SDLError.h" +#import "SDLFile.h" +#import "SDLFileManager.h" +#import "SDLFileType.h" +#import "SDLListFiles.h" +#import "SDLListFilesOperation.h" +#import "SDLListFilesResponse.h" +#import "SDLNotificationConstants.h" +#import "SDLPutFile.h" +#import "SDLPutFileResponse.h" +#import "SDLRPCResponse.h" +#import "SDLUploadFileOperation.h" +#import "TestMultipleFilesConnectionManager.h" +#import "TestResponse.h" + +QuickSpecBegin(SDLFileManagerSpecMultipleFilesSpec) + +describe(@"Uploading multiple files", ^{ + __block TestMultipleFilesConnectionManager *testConnectionManager = nil; + __block SDLFileManager *testFileManager = nil; + __block NSUInteger initialSpaceAvailable = 0; + __block SDLListFilesResponse *testListFilesResponse = nil; + __block NSMutableArray<SDLFile *> *testSDLFiles = nil; + __block NSMutableArray *expectedSuccessfulFileNames; + __block NSNumber *expectedSpaceLeft = nil; + + beforeEach(^{ + testConnectionManager = [[TestMultipleFilesConnectionManager alloc] init]; + testFileManager = [[SDLFileManager alloc] initWithConnectionManager:testConnectionManager]; + initialSpaceAvailable = 9666; + + testListFilesResponse = [[SDLListFilesResponse alloc] init]; + testListFilesResponse.success = @YES; + testListFilesResponse.spaceAvailable = @(initialSpaceAvailable); + testListFilesResponse.filenames = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", nil]; + + testSDLFiles = [NSMutableArray array]; + expectedSuccessfulFileNames = [NSMutableArray array]; + }); + + context(@"After uploading", ^{ + beforeEach(^{ + waitUntil(^(void (^done)(void)){ + [testFileManager startWithCompletionHandler:^(BOOL success, NSError * _Nullable error) { + done(); + }]; + + // Need to wait state machine transitions to complete before sending testListFilesResponse + [NSThread sleepForTimeInterval:0.3]; + + [testConnectionManager respondToLastRequestWithResponse:testListFilesResponse]; + }); + }); + + context(@"When all files are uploaded successfully", ^{ + __block SDLPutFileResponse *response; + __block NSError *responseError = nil; + __block NSMutableDictionary *testResponses; + + beforeEach(^{ + testResponses = [[NSMutableDictionary alloc] init]; + + // Successful response + response = [[SDLPutFileResponse alloc] init]; + response.success = @YES; + }); + + it(@"should upload 1 small file from memory without error", ^{ + NSString *fileName = [NSString stringWithFormat:@"Test Small File Memory %d", 0]; + NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + file.overwrite = true; + + [testSDLFiles addObject:file]; + + expectedSpaceLeft = @55; + response.spaceAvailable = expectedSpaceLeft; + + [expectedSuccessfulFileNames addObject:fileName]; + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + testConnectionManager.responses = testResponses; + }); + + it(@"should upload 1 large file from disk without error", ^{ + NSString *imageName = @"testImage"; + NSString *imageFilePath = [[NSBundle bundleForClass:[self class]] pathForResource:imageName ofType:@"png"]; + NSURL *imageFileURL = [[NSURL alloc] initFileURLWithPath:imageFilePath]; + + NSString *fileName = [NSString stringWithFormat:@"Test Large File Disk %d", 0]; + SDLFile *file = [SDLFile fileAtFileURL:imageFileURL name:fileName]; + file.overwrite = true; + + [testSDLFiles addObject:file]; + + expectedSpaceLeft = @44; + response.spaceAvailable = expectedSpaceLeft; + + [expectedSuccessfulFileNames addObject:fileName]; + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + testConnectionManager.responses = testResponses; + }); + + it(@"should upload multiple small files from memory without error", ^{ + NSInteger space = initialSpaceAvailable; + for(int i = 0; i < 5; i += 1) { + NSString *fileName = [NSString stringWithFormat:@"Test Files %d", i]; + NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + file.overwrite = true; + [testSDLFiles addObject:file]; + + space -= 10; + response.spaceAvailable = @(space); + + [expectedSuccessfulFileNames addObject:fileName]; + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + } + + expectedSpaceLeft = @(space); + testConnectionManager.responses = testResponses; + }); + + it(@"should upload a large number of small files from memory without error", ^{ + NSInteger space = initialSpaceAvailable; + for(int i = 0; i < 500; i += 1) { + NSString *fileName = [NSString stringWithFormat:@"Test Files %d", i]; + NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + file.overwrite = true; + [testSDLFiles addObject:file]; + + space -= 10; + response.spaceAvailable = @(space); + + [expectedSuccessfulFileNames addObject:fileName]; + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + } + + expectedSpaceLeft = @(space); + testConnectionManager.responses = testResponses; + }); + + it(@"should upload 5 small files from disk without error", ^{ + NSURL *imageFileURL = nil; + NSInteger space = initialSpaceAvailable; + for(int i = 0; i < 5; i += 1) { + NSString *imageName = [NSString stringWithFormat:@"testImage%d", i]; + NSString *imageFilePath = [[NSBundle bundleForClass:[self class]] pathForResource:imageName ofType:@"png"]; + imageFileURL = [[NSURL alloc] initFileURLWithPath:imageFilePath]; + + NSString *fileName = [NSString stringWithFormat:@"TestMultipleLargeFilesDisk%d", i]; + SDLFile *file = [SDLFile fileAtFileURL:imageFileURL name:fileName]; + + file.overwrite = true; + [testSDLFiles addObject:file]; + + space -= 10; + response.spaceAvailable = @(space); + + [expectedSuccessfulFileNames addObject:fileName]; + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + } + expectedSpaceLeft = @(space); + testConnectionManager.responses = testResponses; + }); + + it(@"should upload multiple files from memory and on disk without error", ^{ + NSInteger space = initialSpaceAvailable; + for(int i = 0; i < 10; i += 1) { + NSString *fileName = [NSString stringWithFormat:@"TestMultipleSDiskAndMemory%d", i]; + NSData *fileData = nil; + SDLFile *testFile = nil; + + if (i < 5) { + NSString *textFileName = [NSString stringWithFormat:@"testImageA%d", i]; + NSString *textFilePath = [[NSBundle bundleForClass:[self class]] pathForResource:textFileName ofType:@"png"]; + NSURL *textFileURL = [[NSURL alloc] initFileURLWithPath:textFilePath]; + + fileData = [[NSData alloc] initWithContentsOfURL:textFileURL]; + testFile = [SDLFile fileAtFileURL:textFileURL name:fileName]; + } else { + fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + testFile = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + } + + testFile.overwrite = true; + [testSDLFiles addObject:testFile]; + + space -= 10; + response.spaceAvailable = @(space); + + [expectedSuccessfulFileNames addObject:fileName]; + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + } + expectedSpaceLeft = @(space); + testConnectionManager.responses = testResponses; + }); + + afterEach(^{ + waitUntilTimeout(10, ^(void (^done)(void)){ + [testFileManager uploadFiles:testSDLFiles completionHandler:^(NSError * _Nullable error) { + expect(error).to(beNil()); + done(); + }]; + }); + }); + }); + + context(@"When files are not uploaded successfully", ^{ + __block NSMutableDictionary *testResponses; + __block NSMutableDictionary *expectedFailedUploads; + __block NSError *expectedError = nil; + + beforeEach(^{ + testResponses = [[NSMutableDictionary alloc] init]; + expectedFailedUploads = [[NSMutableDictionary alloc] init]; + }); + + it(@"should return an error when all files fail", ^{ + for(int i = 0; i < 5; i += 1) { + NSString *fileName = [NSString stringWithFormat:@"Test Files Unsuccessful %d", i]; + NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + file.overwrite = true; + [testSDLFiles addObject:file]; + + // Failed response + SDLPutFileResponse *response = [[SDLPutFileResponse alloc] init]; + response.spaceAvailable = @(initialSpaceAvailable); + response.success = @NO; + + // Failed error + NSString *responseErrorDescription = [NSString stringWithFormat:@"file upload failed: %d", i]; + NSString *responseErrorReason = [NSString stringWithFormat:@"some error reason: %d", i]; + NSError *responseError = [NSError sdl_lifecycle_unknownRemoteErrorWithDescription:responseErrorDescription andReason:responseErrorReason]; + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + expectedFailedUploads[fileName] = responseError; + } + + testConnectionManager.responses = testResponses; + expectedError = [NSError sdl_fileManager_unableToUploadError:expectedFailedUploads]; + expectedSpaceLeft = [[NSNumber alloc] initWithInteger:initialSpaceAvailable]; + }); + + it(@"should return an error when the first file fails", ^{ + NSInteger space = initialSpaceAvailable; + for(int i = 0; i < 6; i += 1) { + NSString *fileName = [NSString stringWithFormat:@"Test Files Unsuccessful %d", i]; + NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + file.overwrite = true; + [testSDLFiles addObject:file]; + + SDLPutFileResponse *response = [[SDLPutFileResponse alloc] init]; + NSError *responseError = nil; + if (i == 0) { + // Failed response + response.spaceAvailable = @(space); + response.success = @NO; + + // Failed error + NSString *responseErrorDescription = [NSString stringWithFormat:@"file upload failed: %d", i]; + NSString *responseErrorReason = [NSString stringWithFormat:@"some error reason: %d", i]; + responseError = [NSError sdl_lifecycle_unknownRemoteErrorWithDescription:responseErrorDescription andReason:responseErrorReason]; + + expectedFailedUploads[fileName] = responseError; + } else { + // Successful response + space -= 1; + response.spaceAvailable = @(space); + response.success = @YES; + + // No error + responseError = nil; + + [expectedSuccessfulFileNames addObject:fileName]; + } + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + } + + testConnectionManager.responses = testResponses; + expectedError = [NSError sdl_fileManager_unableToUploadError:expectedFailedUploads]; + expectedSpaceLeft = [[NSNumber alloc] initWithInteger:space]; + }); + + it(@"should return an error when the last file fails", ^{ + NSInteger space = initialSpaceAvailable; + for(int i = 0; i < 100; i += 1) { + NSString *fileName = [NSString stringWithFormat:@"Test Files Unsuccessful %d", i]; + NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + file.overwrite = true; + [testSDLFiles addObject:file]; + + SDLPutFileResponse *response = [[SDLPutFileResponse alloc] init]; + NSError *responseError = nil; + if (i == 99) { + // Failed response + response.spaceAvailable = @(space); + response.success = @NO; + + // Failed error + NSString *responseErrorDescription = [NSString stringWithFormat:@"file upload failed: %d", i]; + NSString *responseErrorReason = [NSString stringWithFormat:@"some error reason: %d", i]; + responseError = [NSError sdl_lifecycle_unknownRemoteErrorWithDescription:responseErrorDescription andReason:responseErrorReason]; + + expectedFailedUploads[fileName] = responseError; + } else { + // Successful response + space -= 5; + response.spaceAvailable = @(space); + response.success = @YES; + + // No error + responseError = nil; + + [expectedSuccessfulFileNames addObject:fileName]; + } + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + } + + testConnectionManager.responses = testResponses; + expectedError = [NSError sdl_fileManager_unableToUploadError:expectedFailedUploads]; + expectedSpaceLeft = [[NSNumber alloc] initWithInteger:space]; + }); + + it(@"should return an error if 0 files passed", ^{ + expectedError = [NSError sdl_fileManager_noFilesError]; + expectedSpaceLeft = [[NSNumber alloc] initWithInteger:initialSpaceAvailable]; + }); + + it(@"should return an error if remote files contains same name", ^{ + SDLPutFileResponse *response = [[SDLPutFileResponse alloc] init]; + NSError *responseError = nil; + + NSString *fileName = testListFilesResponse.filenames.firstObject; + NSData *fileData = [@"someTextData" dataUsingEncoding:NSUTF8StringEncoding]; + SDLFile *file = [SDLFile fileWithData:fileData name:fileName fileExtension:@"bin"]; + file.overwrite = false; + [testSDLFiles addObject:file]; + + response.spaceAvailable = @(initialSpaceAvailable); + response.success = @NO; + + TestResponse *testResponse = [[TestResponse alloc] initWithResponse:response error:responseError]; + testResponses[fileName] = testResponse; + + testConnectionManager.responses = testResponses; + expectedFailedUploads[fileName] = [NSError sdl_fileManager_cannotOverwriteError]; + expectedError = [NSError sdl_fileManager_unableToUploadError:expectedFailedUploads]; + expectedSpaceLeft = @(initialSpaceAvailable); + }); + + afterEach(^{ + waitUntilTimeout(10, ^(void (^done)(void)){ + [testFileManager uploadFiles:testSDLFiles completionHandler:^(NSError * _Nullable error) { + expect(error).to(equal(expectedError)); + done(); + }]; + }); + }); + }); + + afterEach(^{ + expect(@(testFileManager.bytesAvailable)).to(equal(expectedSpaceLeft)); + for(int i = 0; i < expectedSuccessfulFileNames.count; i += 1) { + expect(testFileManager.remoteFileNames).to(contain(expectedSuccessfulFileNames[i])); + } + }); + }); +}); + +QuickSpecEnd |