// // Copyright 2010-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). // You may not use this file except in compliance with the License. // A copy of the License is located at // // http://aws.amazon.com/apache2.0 // // or in the "license" file accompanying this file. This file is distributed // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either // express or implied. See the License for the specific language governing // permissions and limitations under the License. // #import #import #import "OCMock.h" #import "AWSTestUtility.h" #import "AWSIoTDataService.h" static id mockNetworking = nil; @interface AWSGeneralIoTDataTests : XCTestCase @end @implementation AWSGeneralIoTDataTests - (void)setUp { [super setUp]; [AWSTestUtility setupFakeCognitoCredentialsProvider]; mockNetworking = OCMClassMock([AWSNetworking class]); AWSTask *errorTask = [AWSTask taskWithError:[NSError errorWithDomain:@"OCMockExpectedNetworkingError" code:8848 userInfo:nil]]; OCMStub([mockNetworking sendRequest:[OCMArg isKindOfClass:[AWSNetworkingRequest class]]]).andReturn(errorTask); } - (void)tearDown { [super tearDown]; } - (void)testConstructors { NSString *key = @"testIoTDataConstructors"; XCTAssertNotNil([AWSIoTData defaultIoTData]); XCTAssertEqual([[AWSIoTData defaultIoTData] class], [AWSIoTData class]); XCTAssertNil([AWSIoTData IoTDataForKey:key]); AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionSAEast1 credentialsProvider:[AWSServiceManager defaultServiceManager].defaultServiceConfiguration.credentialsProvider]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; XCTAssertNotNil([AWSIoTData IoTDataForKey:key]); XCTAssertEqual([[AWSIoTData IoTDataForKey:key] class], [AWSIoTData class]); XCTAssertEqual([AWSIoTData IoTDataForKey:key].configuration.regionType, AWSRegionSAEast1); [AWSIoTData removeIoTDataForKey:key]; XCTAssertNil([AWSIoTData IoTDataForKey:key]); } - (void)testDeleteThingShadow { NSString *key = @"testDeleteThingShadow"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; [[[[AWSIoTData IoTDataForKey:key] deleteThingShadow:[AWSIoTDataDeleteThingShadowRequest new]] continueWithBlock:^id(AWSTask *task) { XCTAssertNotNil(task.error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", task.error.domain); XCTAssertEqual(8848, task.error.code); XCTAssertNil(task.result); return nil; }] waitUntilFinished]; OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testDeleteThingShadowCompletionHandler { NSString *key = @"testDeleteThingShadow"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[AWSIoTData IoTDataForKey:key] deleteThingShadow:[AWSIoTDataDeleteThingShadowRequest new] completionHandler:^(AWSIoTDataDeleteThingShadowResponse* _Nullable response, NSError * _Nullable error) { XCTAssertNotNil(error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", error.domain); XCTAssertEqual(8848, error.code); XCTAssertNil(response); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int)(2.0 * NSEC_PER_SEC))); OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testGetRetainedMessage { NSString *key = @"testGetRetainedMessage"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; [[[[AWSIoTData IoTDataForKey:key] getRetainedMessage:[AWSIoTDataGetRetainedMessageRequest new]] continueWithBlock:^id(AWSTask *task) { XCTAssertNotNil(task.error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", task.error.domain); XCTAssertEqual(8848, task.error.code); XCTAssertNil(task.result); return nil; }] waitUntilFinished]; OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testGetRetainedMessageCompletionHandler { NSString *key = @"testGetRetainedMessage"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[AWSIoTData IoTDataForKey:key] getRetainedMessage:[AWSIoTDataGetRetainedMessageRequest new] completionHandler:^(AWSIoTDataGetRetainedMessageResponse* _Nullable response, NSError * _Nullable error) { XCTAssertNotNil(error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", error.domain); XCTAssertEqual(8848, error.code); XCTAssertNil(response); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int)(2.0 * NSEC_PER_SEC))); OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testGetThingShadow { NSString *key = @"testGetThingShadow"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; [[[[AWSIoTData IoTDataForKey:key] getThingShadow:[AWSIoTDataGetThingShadowRequest new]] continueWithBlock:^id(AWSTask *task) { XCTAssertNotNil(task.error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", task.error.domain); XCTAssertEqual(8848, task.error.code); XCTAssertNil(task.result); return nil; }] waitUntilFinished]; OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testGetThingShadowCompletionHandler { NSString *key = @"testGetThingShadow"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[AWSIoTData IoTDataForKey:key] getThingShadow:[AWSIoTDataGetThingShadowRequest new] completionHandler:^(AWSIoTDataGetThingShadowResponse* _Nullable response, NSError * _Nullable error) { XCTAssertNotNil(error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", error.domain); XCTAssertEqual(8848, error.code); XCTAssertNil(response); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int)(2.0 * NSEC_PER_SEC))); OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testListNamedShadowsForThing { NSString *key = @"testListNamedShadowsForThing"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; [[[[AWSIoTData IoTDataForKey:key] listNamedShadowsForThing:[AWSIoTDataListNamedShadowsForThingRequest new]] continueWithBlock:^id(AWSTask *task) { XCTAssertNotNil(task.error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", task.error.domain); XCTAssertEqual(8848, task.error.code); XCTAssertNil(task.result); return nil; }] waitUntilFinished]; OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testListNamedShadowsForThingCompletionHandler { NSString *key = @"testListNamedShadowsForThing"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[AWSIoTData IoTDataForKey:key] listNamedShadowsForThing:[AWSIoTDataListNamedShadowsForThingRequest new] completionHandler:^(AWSIoTDataListNamedShadowsForThingResponse* _Nullable response, NSError * _Nullable error) { XCTAssertNotNil(error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", error.domain); XCTAssertEqual(8848, error.code); XCTAssertNil(response); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int)(2.0 * NSEC_PER_SEC))); OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testListRetainedMessages { NSString *key = @"testListRetainedMessages"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; [[[[AWSIoTData IoTDataForKey:key] listRetainedMessages:[AWSIoTDataListRetainedMessagesRequest new]] continueWithBlock:^id(AWSTask *task) { XCTAssertNotNil(task.error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", task.error.domain); XCTAssertEqual(8848, task.error.code); XCTAssertNil(task.result); return nil; }] waitUntilFinished]; OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testListRetainedMessagesCompletionHandler { NSString *key = @"testListRetainedMessages"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[AWSIoTData IoTDataForKey:key] listRetainedMessages:[AWSIoTDataListRetainedMessagesRequest new] completionHandler:^(AWSIoTDataListRetainedMessagesResponse* _Nullable response, NSError * _Nullable error) { XCTAssertNotNil(error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", error.domain); XCTAssertEqual(8848, error.code); XCTAssertNil(response); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int)(2.0 * NSEC_PER_SEC))); OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testPublish { NSString *key = @"testPublish"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; [[[[AWSIoTData IoTDataForKey:key] publish:[AWSIoTDataPublishRequest new]] continueWithBlock:^id(AWSTask *task) { XCTAssertNotNil(task.error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", task.error.domain); XCTAssertEqual(8848, task.error.code); XCTAssertNil(task.result); return nil; }] waitUntilFinished]; OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testPublishCompletionHandler { NSString *key = @"testPublish"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[AWSIoTData IoTDataForKey:key] publish:[AWSIoTDataPublishRequest new] completionHandler:^(NSError * _Nullable error) { XCTAssertNotNil(error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", error.domain); XCTAssertEqual(8848, error.code); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int)(2.0 * NSEC_PER_SEC))); OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testUpdateThingShadow { NSString *key = @"testUpdateThingShadow"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; [[[[AWSIoTData IoTDataForKey:key] updateThingShadow:[AWSIoTDataUpdateThingShadowRequest new]] continueWithBlock:^id(AWSTask *task) { XCTAssertNotNil(task.error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", task.error.domain); XCTAssertEqual(8848, task.error.code); XCTAssertNil(task.result); return nil; }] waitUntilFinished]; OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } - (void)testUpdateThingShadowCompletionHandler { NSString *key = @"testUpdateThingShadow"; AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil]; [AWSIoTData registerIoTDataWithConfiguration:configuration forKey:key]; AWSIoTData *awsClient = [AWSIoTData IoTDataForKey:key]; XCTAssertNotNil(awsClient); XCTAssertNotNil(mockNetworking); [awsClient setValue:mockNetworking forKey:@"networking"]; dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [[AWSIoTData IoTDataForKey:key] updateThingShadow:[AWSIoTDataUpdateThingShadowRequest new] completionHandler:^(AWSIoTDataUpdateThingShadowResponse* _Nullable response, NSError * _Nullable error) { XCTAssertNotNil(error); XCTAssertEqualObjects(@"OCMockExpectedNetworkingError", error.domain); XCTAssertEqual(8848, error.code); XCTAssertNil(response); dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int)(2.0 * NSEC_PER_SEC))); OCMVerify([mockNetworking sendRequest:[OCMArg isNotNil]]); [AWSIoTData removeIoTDataForKey:key]; } @end