|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// This code is licensed under the MIT License. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files(the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions : |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | + |
| 26 | +#import <XCTest/XCTest.h> |
| 27 | +#import "MSIDLastRequestTelemetry+Tests.h" |
| 28 | +#import "MSIDAADTokenRequestServerTelemetry.h" |
| 29 | +#import "MSIDTestSwizzle.h" |
| 30 | +#import "MSIDTestContext.h" |
| 31 | + |
| 32 | +@interface MSIDAADTokenRequestServerTelemetryTests : XCTestCase |
| 33 | + |
| 34 | +@property (nonatomic) MSIDTestContext *context; |
| 35 | + |
| 36 | +@end |
| 37 | + |
| 38 | +@implementation MSIDAADTokenRequestServerTelemetryTests |
| 39 | + |
| 40 | +- (void)setUp { |
| 41 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 42 | + [MSIDTestSwizzle reset]; |
| 43 | + __auto_type context = [MSIDTestContext new]; |
| 44 | + context.correlationId = [[NSUUID alloc] initWithUUIDString:@"00000000-0000-0000-0000-000000000001"]; |
| 45 | + self.context = context; |
| 46 | +} |
| 47 | + |
| 48 | +- (void)tearDown { |
| 49 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 50 | + [MSIDTestSwizzle reset]; |
| 51 | +} |
| 52 | + |
| 53 | +- (void)test_whenNoError_passIn_handleErrorWithContext_shouldResetTelemetry |
| 54 | +{ |
| 55 | + MSIDAADTokenRequestServerTelemetry *telemetry = [MSIDAADTokenRequestServerTelemetry new]; |
| 56 | + MSIDLastRequestTelemetry *telemetryObject = [MSIDLastRequestTelemetry sharedInstance]; |
| 57 | + [telemetry setValue:telemetryObject forKey:@"lastRequestTelemetry"]; |
| 58 | + |
| 59 | + XCTestExpectation *expectation = [self expectationWithDescription:@"resetTelemetry triggerred"]; |
| 60 | + [MSIDTestSwizzle instanceMethod:@selector(resetTelemetry) |
| 61 | + class:[MSIDLastRequestTelemetry class] |
| 62 | + block:(id)^(void) |
| 63 | + { |
| 64 | + [expectation fulfill]; |
| 65 | + |
| 66 | + }]; |
| 67 | + |
| 68 | + [MSIDTestSwizzle instanceMethod:@selector(addErrorInfo:) |
| 69 | + class:[MSIDLastRequestTelemetry class] |
| 70 | + block:(id)^(void) |
| 71 | + { |
| 72 | + XCTFail(@"addErrorInfo should not be triggerred"); |
| 73 | + }]; |
| 74 | + |
| 75 | + [telemetry handleError:nil context:self.context]; |
| 76 | + |
| 77 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 78 | +} |
| 79 | + |
| 80 | +- (void)test_whenError_passIn_handleErrorWithContext_shouldAddErrorInfo |
| 81 | +{ |
| 82 | + MSIDAADTokenRequestServerTelemetry *telemetry = [MSIDAADTokenRequestServerTelemetry new]; |
| 83 | + MSIDLastRequestTelemetry *telemetryObject = [MSIDLastRequestTelemetry sharedInstance]; |
| 84 | + [telemetry setValue:telemetryObject forKey:@"lastRequestTelemetry"]; |
| 85 | + |
| 86 | + XCTestExpectation *expectation = [self expectationWithDescription:@"addErrorInfo triggerred"]; |
| 87 | + [MSIDTestSwizzle instanceMethod:@selector(resetTelemetry) |
| 88 | + class:[MSIDLastRequestTelemetry class] |
| 89 | + block:(id)^(void) |
| 90 | + { |
| 91 | + XCTFail(@"resetTelemetry should not be triggerred"); |
| 92 | + }]; |
| 93 | + |
| 94 | + [MSIDTestSwizzle instanceMethod:@selector(addErrorInfo:) |
| 95 | + class:[MSIDLastRequestTelemetry class] |
| 96 | + block:(id)^(void) |
| 97 | + { |
| 98 | + [expectation fulfill]; |
| 99 | + }]; |
| 100 | + |
| 101 | + [telemetry handleError:[NSError new] context:self.context]; |
| 102 | + |
| 103 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 104 | +} |
| 105 | + |
| 106 | +- (void)test_whenNoError_passIn_handleErrorWithMessageAndContext_shouldAddErrorInfo |
| 107 | +{ |
| 108 | + MSIDAADTokenRequestServerTelemetry *telemetry = [MSIDAADTokenRequestServerTelemetry new]; |
| 109 | + MSIDLastRequestTelemetry *telemetryObject = [MSIDLastRequestTelemetry sharedInstance]; |
| 110 | + [telemetry setValue:telemetryObject forKey:@"lastRequestTelemetry"]; |
| 111 | + |
| 112 | + XCTestExpectation *expectation = [self expectationWithDescription:@"resetTelemetry triggerred"]; |
| 113 | + [MSIDTestSwizzle instanceMethod:@selector(resetTelemetry) |
| 114 | + class:[MSIDLastRequestTelemetry class] |
| 115 | + block:(id)^(void) |
| 116 | + { |
| 117 | + XCTFail(@"resetTelemetry should not be triggerred"); |
| 118 | + }]; |
| 119 | + |
| 120 | + [MSIDTestSwizzle instanceMethod:@selector(addErrorInfo:) |
| 121 | + class:[MSIDLastRequestTelemetry class] |
| 122 | + block:(id)^(void) |
| 123 | + { |
| 124 | + [expectation fulfill]; |
| 125 | + }]; |
| 126 | + |
| 127 | + [telemetry handleError:nil errorString:@"error string" context:self.context]; |
| 128 | + |
| 129 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 130 | +} |
| 131 | + |
| 132 | +- (void)test_whenError_passIn_handleErrorWithMessageAndContext_shouldAddErrorInfo |
| 133 | +{ |
| 134 | + MSIDAADTokenRequestServerTelemetry *telemetry = [MSIDAADTokenRequestServerTelemetry new]; |
| 135 | + MSIDLastRequestTelemetry *telemetryObject = [MSIDLastRequestTelemetry sharedInstance]; |
| 136 | + [telemetry setValue:telemetryObject forKey:@"lastRequestTelemetry"]; |
| 137 | + |
| 138 | + XCTestExpectation *expectation = [self expectationWithDescription:@"resetTelemetry triggerred"]; |
| 139 | + [MSIDTestSwizzle instanceMethod:@selector(resetTelemetry) |
| 140 | + class:[MSIDLastRequestTelemetry class] |
| 141 | + block:(id)^(void) |
| 142 | + { |
| 143 | + XCTFail(@"resetTelemetry should not be triggerred"); |
| 144 | + }]; |
| 145 | + |
| 146 | + [MSIDTestSwizzle instanceMethod:@selector(addErrorInfo:) |
| 147 | + class:[MSIDLastRequestTelemetry class] |
| 148 | + block:(id)^(void) |
| 149 | + { |
| 150 | + [expectation fulfill]; |
| 151 | + }]; |
| 152 | + |
| 153 | + [telemetry handleError:[NSError new] errorString:@"error string" context:self.context]; |
| 154 | + |
| 155 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 156 | +} |
| 157 | + |
| 158 | +@end |
0 commit comments