YHAssemblyAssistantLogicTests.m 11.3 KB
//
//  YHAssemblyAssistantLogicTests.m
//  YH_Analytics
//
//  Created by Zhou Rongjun on 15/4/22.
//  Copyright (c) 2015年 YOHO. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "OCMock.h"
#import <CoreLocation/CoreLocation.h>
#import "YHAssemblyAssistant.h"
#import "YHDevice.h"
#import "YHStatus.h"
#import "YHEvent.h"
#import "YHError.h"
#import "FakeLocationManager.h"

@interface YHAssemblyAssistant (XCTestCase)

@property (strong, nonatomic) NSString *eventFilePath;
@property (strong, nonatomic) NSString *eventFileName;
@property (strong, nonatomic) NSFileManager *fileManager; // 属性注入 DI
@property (strong, nonatomic) CLLocationManager *locationManager;

@property (strong, nonatomic, readonly) YHDevice *device;
@property (strong, nonatomic, readonly) YHStatus *currentStatus;
@property (strong, nonatomic, readonly) NSMutableDictionary *immediUploadItemDic;

- (BOOL)updateNetWorkStatus:(NSString *)status;
- (BOOL)updateLocation:(CLLocation *)location;
- (void)updateImmediUploadDic;

@end



@interface YHAssemblyAssistantLogicTests : XCTestCase {
    YHAssemblyAssistant *assembly;
    id partialMock;
    id mock;
}

@end

@implementation YHAssemblyAssistantLogicTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    NSLog(@"%@ setUp", self.name);
    assembly = [[YHAssemblyAssistant alloc] init];
    XCTAssertNotNil(assembly, @"Can't create a instance of YHAssemblyAssistant.");
    
    assembly.eventFilePath = @"/AppData/Library/Caches/YHLogSystem";
    assembly.eventFileName = @"/AppData/Library/Caches/YHLogSystem/LogSystem.plist";
    
    mock = OCMClassMock([NSFileManager class]);
    OCMStub([mock defaultManager]).andReturn(mock);
    assembly.fileManager = mock;
    
    partialMock = OCMPartialMock(assembly);
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    assembly = nil;
    mock = nil;
    [super tearDown];
    NSLog(@"%@ tearDown", self.name);
}

- (void)testCanPersistingSuccess {
    NSLog(@"%@ start", self.name);
    
    OCMExpect([mock fileExistsAtPath:[OCMArg any]]).andReturn(NO);
    OCMExpect([mock createDirectoryAtPath:[OCMArg any] withIntermediateDirectories:YES attributes:nil error:nil]).andReturn(YES);
    
    BOOL isCanPersisting = [assembly canPersisting];
    
    OCMVerifyAll(mock);
    XCTAssert(isCanPersisting, @"Data should be persisting when create directory success.");
    
    NSLog(@"%@ end", self.name);
}

- (void)testCanPersistingFailed {
    NSLog(@"%@ start", self.name);
    
    OCMExpect([mock fileExistsAtPath:[OCMArg any]]).andReturn(NO);
    OCMExpect([mock createDirectoryAtPath:[OCMArg any] withIntermediateDirectories:YES attributes:nil error:nil]).andReturn(NO);
    
    BOOL isCanPersisting = [assembly canPersisting];

    OCMVerifyAll(mock);
    XCTAssertFalse(isCanPersisting, @"Data should not be persisting when create directory failed.");
    
    NSLog(@"%@ end", self.name);
}

- (void)testCanPersistingExist {
    NSLog(@"%@ start", self.name);
    
    OCMExpect([mock fileExistsAtPath:[OCMArg any]]).andReturn(YES);
    [[mock reject] createDirectoryAtPath:[OCMArg any] withIntermediateDirectories:YES attributes:nil error:nil];
    
    BOOL isCanPersisting = [assembly canPersisting];
    
    OCMVerifyAll(mock);
    XCTAssert(isCanPersisting, @"Data should be persisting when directory exist.");
    
    NSLog(@"%@ end", self.name);
}

- (void)testGetAllEventCountInitValueEqualZero {
    NSLog(@"%@ start", self.name);
    
    XCTAssertEqual([assembly getAllEventCount], 0, @"allEventCount should be zero initially.");
    
    NSLog(@"%@ end", self.name);
}

- (void)testGetUploadDataSuccess {
    NSLog(@"%@ start", self.name);
    
    NSDictionary *uploadDataDic = @{@"key" : @"value"};
    id dicMock = OCMClassMock([NSDictionary class]);
    OCMExpect([dicMock dictionaryWithContentsOfFile:[OCMArg any]]).andReturn(uploadDataDic);
    
    NSDictionary *returnDic = [assembly getUploadData];
    
    XCTAssertEqualObjects(returnDic, uploadDataDic, @"event file doesn't exist or data doesn't store by Dictionary format.");
    OCMVerifyAll(dicMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testGetUploadDataFailed {
    NSLog(@"%@ start", self.name);
    
    id dicMock = OCMClassMock([NSDictionary class]);
    OCMExpect([dicMock dictionaryWithContentsOfFile:[OCMArg any]]).andReturn(nil);
    
    NSDictionary *returnDic = [assembly getUploadData];
    
    XCTAssertNil(returnDic, @"it should return nil when event file doesn't exist or data doesn't store by Dictionary format.");
    OCMVerifyAll(dicMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testPrepareImmediUploadDic {
    NSLog(@"%@ start", self.name);
    
    [assembly prepareImmediUploadDic:@"appId" sessionId:@"sessionId"];
    
    NSDictionary *deviceJsonDic = [assembly.immediUploadItemDic objectForKey:JsonKeyDataTypeDevice];
    NSMutableArray *statusJsonArray = [assembly.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];
    NSDictionary *currentJsonDic = [assembly.currentStatus jsonDictionary];
    
    XCTAssertNotNil(deviceJsonDic, @"Should contain device JsonDic after prepare immediUploadDic.");
    XCTAssertNotNil(statusJsonArray, @"Should contain statusJsonDic Array after prepare immediUploadDic.");
    XCTAssertEqualObjects([statusJsonArray objectAtIndex:0], currentJsonDic);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUpdateImmediUploadDicFailed {
    NSLog(@"%@ start", self.name);
    
    XCTAssertThrows([assembly updateImmediUploadDic]);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUpdateImmediUploadDicSuccess {
    NSLog(@"%@ start", self.name);
    
    [assembly prepareImmediUploadDic:@"appId" sessionId:@"sessionId"];
    [assembly updateImmediUploadDic];
    
    NSMutableArray *statusJsonArray = [assembly.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];
    
    NSDictionary *currentJsonDic = [assembly.currentStatus jsonDictionary];
    
    XCTAssertNotNil(statusJsonArray, @"Should contain statusJsonDic Array after prepare immediUploadDic.");
    XCTAssertEqualObjects([statusJsonArray objectAtIndex:0], currentJsonDic);
    
    NSLog(@"%@ end", self.name);
}

- (void)testSaveItemDataWithInputNil {
    NSLog(@"%@ start", self.name);
    
    [[partialMock reject] canPersisting];
    
    XCTAssertThrows([assembly saveItemData:nil]);
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testSaveItemDataWithInputInvalid {
    NSLog(@"%@ start", self.name);
    
    [[partialMock reject] canPersisting];
    
    XCTAssertThrows([assembly saveItemData:@"invalid"]);
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testSaveItemDataWithCannotPersist {
    NSLog(@"%@ start", self.name);
    
    YHAnalyItemData *data = [[YHAnalyItemData alloc] init];

    OCMExpect([partialMock canPersisting]).andReturn(NO);
    
    XCTAssertThrows([assembly saveItemData:data]);
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testSaveItemDataWithDevice {
    NSLog(@"%@ start", self.name);
    
    YHDevice *data = [[YHDevice alloc] init];
    
    OCMExpect([partialMock canPersisting]).andReturn(YES);
    
    XCTAssertThrows([assembly saveItemData:data]);
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testSaveItemDataWithStatus {
    NSLog(@"%@ start", self.name);
    
    YHStatus *data = [[YHStatus alloc] init];

    OCMExpect([partialMock canPersisting]).andReturn(YES);
    
    XCTAssertThrows([assembly saveItemData:data]);
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testSaveItemDataWithEvent {
    NSLog(@"%@ start", self.name);
    
    YHEvent *data = [[YHEvent alloc] init];

    OCMExpect([partialMock canPersisting]).andReturn(YES);
    
    XCTAssertNoThrow([assembly saveItemData:data]);
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testSaveItemDataWithError {
    NSLog(@"%@ start", self.name);
    
    YHError *data = [[YHError alloc] init];

    OCMExpect([partialMock canPersisting]).andReturn(YES);
    
    XCTAssertNoThrow([assembly saveItemData:data]);
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUploadImmedilyWithEvent {
    NSLog(@"%@ start", self.name);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUploadImmedilyWithError {
    NSLog(@"%@ start", self.name);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUploadDiskDataSuccess {
    NSLog(@"%@ start", self.name);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUploadDiskDataFailed {
    NSLog(@"%@ start", self.name);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUpdateLocationSuccess {
    NSLog(@"%@ start", self.name);
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
    NSString *lo = [[NSNumber numberWithDouble:location.coordinate.longitude] stringValue];
    NSString *la = [[NSNumber numberWithDouble:location.coordinate.latitude] stringValue];
    
    BOOL bReturn = [assembly updateLocation:location];
    
    XCTAssert(bReturn);
    XCTAssert([assembly.currentStatus.lo isEqualToString:lo]);
    XCTAssert([assembly.currentStatus.la isEqualToString:la]);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUpdateLocationFailed {
    NSLog(@"%@ start", self.name);
    
    CLLocation *location = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
    BOOL bReturn1 = [assembly updateLocation:location];
    BOOL bReturn2 = [assembly updateLocation:location];
    
    XCTAssert(bReturn1);
    XCTAssertFalse(bReturn2);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUpdateLocationWithInputNil {
    NSLog(@"%@ start", self.name);
    
    XCTAssertThrows([assembly updateLocation:nil]);
    
    NSLog(@"%@ end", self.name);
}

- (void)testUpdateLocationWithInputInvalid {
    NSLog(@"%@ start", self.name);
    
    XCTAssertThrows([assembly updateLocation:@"Invalid"]);

    NSLog(@"%@ end", self.name);
}

#pragma mark - Test Location Sevice

- (void)testLocationUpdateSuccess {
    NSLog(@"%@ start", self.name);
    
    assembly.locationManager = (CLLocationManager *)[FakeLocationManager new];
    assembly.locationManager.delegate = assembly;
    CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
    
    [[[partialMock expect] andReturnValue:@YES] updateLocation:newLocation];
    OCMExpect([partialMock updateImmediUploadDic]);

    [assembly prepareImmediUploadDic:@"appId" sessionId:@"seesionId"];
    [(FakeLocationManager *)assembly.locationManager updateLocations:@[newLocation]];
    
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}

- (void)testLocationUpdateFailed {
    NSLog(@"%@ start", self.name);
    
    assembly.locationManager = (CLLocationManager *)[FakeLocationManager new];
    assembly.locationManager.delegate = assembly;
    CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:20.0f longitude:120.0f];
    
    [[[partialMock expect] andReturnValue:@NO] updateLocation:newLocation];
    [[partialMock reject] updateImmediUploadDic];
    
    [assembly prepareImmediUploadDic:@"appId" sessionId:@"seesionId"];
    [(FakeLocationManager *)assembly.locationManager updateLocations:@[newLocation]];
    
    OCMVerifyAll(partialMock);
    
    NSLog(@"%@ end", self.name);
}
@end