
//
//  AliyunOSSTests.m
//  AliyunOSSiOSTests
//
//  Created by huaixu on 2018/1/18.
//  Copyright © 2018年 aliyun. All rights reserved.
//
#import "AliyunOSSTests.h"
@implementation AliyunOSSTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    [self setupContainer];
    [self setupClient];
    [self setupTestFiles];
    [self createBucket];
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
    [self deleteBucket];
    
}

- (void)setupClient {
    //    OSSAuthCredentialProvider *provider = [OSSAuthCredentialProvider new];
    OSSPlainTextAKSKPairCredentialProvider *provider = [[OSSPlainTextAKSKPairCredentialProvider alloc] initWithPlainTextAccessKey:OSS_ACCESSKEY_ID secretKey:OSS_SECRETKEY_ID];
    OSSClientConfiguration * conf = [OSSClientConfiguration new];
    conf.maxRetryCount = 2;
    conf.timeoutIntervalForRequest = 30;
    conf.timeoutIntervalForResource = 24 * 60 * 60;
    conf.maxConcurrentRequestCount = 5;
    
    // switches to another credential provider.
    _client = [[OSSClient alloc] initWithEndpoint:OSS_ENDPOINT
                               credentialProvider:provider
                              clientConfiguration:conf];
}

- (void)setupContainer{
    _fileNames = @[@"file1k", @"file10k", @"file100k", @"file1m", @"file5m", @"file10m", @"fileDirA/", @"fileDirB/"];
    _fileSizes = @[@1024, @10240, @102400, @(1024 * 1024 * 1), @(1024 * 1024 * 5), @(1024 * 1024 * 10), @1024, @1024];
}

- (void)setupTestFiles {
    NSFileManager * fm = [NSFileManager defaultManager];
    NSString * mainDir = [NSString oss_documentDirectory];
    
    for (int i = 0; i < [_fileNames count]; i++) {
        NSMutableData * basePart = [NSMutableData dataWithCapacity:1024];
        for (int j = 0; j < 1024/4; j++) {
            u_int32_t randomBit = j;// arc4random();
            [basePart appendBytes:(void*)&randomBit length:4];
        }
        NSString * name = [_fileNames objectAtIndex:i];
        long size = [[_fileSizes objectAtIndex:i] longValue];
        NSString * newFilePath = [mainDir stringByAppendingPathComponent:name];
        if ([fm fileExistsAtPath:newFilePath]) {
            [fm removeItemAtPath:newFilePath error:nil];
        }
        [fm createFileAtPath:newFilePath contents:nil attributes:nil];
        NSFileHandle * f = [NSFileHandle fileHandleForWritingAtPath:newFilePath];
        for (int k = 0; k < size/1024; k++) {
            [f writeData:basePart];
        }
        [f closeFile];
    }
    OSSLogDebug(@"main bundle: %@", mainDir);
}

- (void)createBucket {
    OSSCreateBucketRequest *createBucket1 = [OSSCreateBucketRequest new];
    createBucket1.bucketName = OSS_BUCKET_PUBLIC;
    [[_client createBucket:createBucket1] waitUntilFinished];
    
    OSSCreateBucketRequest *createBucket2 = [OSSCreateBucketRequest new];
    createBucket2.bucketName = OSS_BUCKET_PRIVATE;
    createBucket2.xOssACL = @"public-read-write";
    [[_client createBucket:createBucket2] waitUntilFinished];
}

- (void)deleteBucket {
    OSSDeleteBucketRequest *deleteBucket1 = [OSSDeleteBucketRequest new];
    deleteBucket1.bucketName = OSS_BUCKET_PUBLIC;
    [[_client deleteBucket:deleteBucket1] waitUntilFinished];
    
    OSSDeleteBucketRequest *deleteBucket2 = [OSSDeleteBucketRequest new];
    deleteBucket2.bucketName = OSS_BUCKET_PRIVATE;
    [[_client deleteBucket:deleteBucket2] waitUntilFinished];
}

@end

