/**
 * Copyright 2012 Microsoft Corporation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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.
 */

package com.microsoft.windowsazure.services.media;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.microsoft.windowsazure.services.core.ExponentialRetryPolicy;
import com.microsoft.windowsazure.services.core.RetryPolicyFilter;
import com.microsoft.windowsazure.services.media.models.AccessPolicyInfo;
import com.microsoft.windowsazure.services.media.models.AccessPolicyPermission;
import com.microsoft.windowsazure.services.media.models.AssetInfo;
import com.microsoft.windowsazure.services.media.models.CreateAccessPolicyOptions;
import com.microsoft.windowsazure.services.media.models.CreateAssetOptions;
import com.microsoft.windowsazure.services.media.models.CreateLocatorOptions;
import com.microsoft.windowsazure.services.media.models.LocatorInfo;
import com.microsoft.windowsazure.services.media.models.LocatorType;

/**
 * Testing uploading in various permutations.
 * 
 */
public class UploadingIntegrationTest extends IntegrationTestBase {
    private static ArrayList<String> locatorIds;

    @BeforeClass
    public static void init() {
        locatorIds = new ArrayList<String>();
    }

    @AfterClass
    public static void cleanup() throws Exception {
        MediaContract service = MediaService.create(config);
        deleteLocators(service);
        deleteAccessPolicies(service, "upload");
        deleteAssets(service, "upload");
    }

    private static void deleteLocators(MediaContract service) throws Exception {
        for (String locatorId : locatorIds) {
            service.deleteLocator(locatorId);
        }
    }

    private static void deleteAccessPolicies(MediaContract service, String prefix) throws Exception {
        List<AccessPolicyInfo> policies = service.listAccessPolicies();
        for (AccessPolicyInfo policy : policies) {
            if (policy.getName().startsWith(prefix)) {
                service.deleteAccessPolicy(policy.getId());
            }
        }
    }

    private static void deleteAssets(MediaContract service, String prefix) throws Exception {
        List<AssetInfo> assets = service.listAssets();
        for (AssetInfo asset : assets) {
            if (asset.getName().startsWith(prefix)) {
                service.deleteAsset(asset.getId());
            }
        }
    }

    @Test
    public void canUploadBlockBlob() throws Exception {
        MediaContract service = MediaService.create(config);

        AssetInfo asset = service.createAsset(new CreateAssetOptions().setName("uploadBlockBlobAsset"));

        AccessPolicyInfo policy = service.createAccessPolicy("uploadWritePolicy", 10,
                new CreateAccessPolicyOptions().addPermissions(AccessPolicyPermission.WRITE));

        Date now = new Date();
        Date fiveMinutesAgo = new Date();
        Date tenMinutesFromNow = new Date();

        fiveMinutesAgo.setTime(now.getTime() - (5 * 60 * 1000));
        tenMinutesFromNow.setTime(now.getTime() + (10 * 60 * 1000));

        LocatorInfo locator = service.createLocator(policy.getId(), asset.getId(), LocatorType.SAS,
                new CreateLocatorOptions().setStartTime(fiveMinutesAgo).setExpirationDateTime(tenMinutesFromNow));

        locatorIds.add(locator.getId());

        WritableBlobContainerContract blobWriter = MediaService.createBlobWriter(locator);

        ExponentialRetryPolicy retryPolicy = new ExponentialRetryPolicy(5000, 5, new int[] { 400, 404 });
        blobWriter = blobWriter.withFilter(new RetryPolicyFilter(retryPolicy));

        InputStream blobContent = new ByteArrayInputStream(new byte[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 });

        blobWriter.createBlockBlob("uploadBlockBlobTest", blobContent);
    }
}

@@ -733,124 +733,14 @@
 til.
-ArrayList;%0Aimport java.util.Date;%0Aimport java.util.List;%0A%0Aimport org.junit.AfterClass;%0Aimport org.junit.BeforeClass;
+Date;%0A
 %0Aimp
@@ -1625,1306 +1625,8 @@
 se %7B
-%0A    private static ArrayList%3CString%3E locatorIds;%0A%0A    @BeforeClass%0A    public static void init() %7B%0A        locatorIds = new ArrayList%3CString%3E();%0A    %7D%0A%0A    @AfterClass%0A    public static void cleanup() throws Exception %7B%0A        MediaContract service = MediaService.create(config);%0A        deleteLocators(service);%0A        deleteAccessPolicies(service, %22upload%22);%0A        deleteAssets(service, %22upload%22);%0A    %7D%0A%0A    private static void deleteLocators(MediaContract service) throws Exception %7B%0A        for (String locatorId : locatorIds) %7B%0A            service.deleteLocator(locatorId);%0A        %7D%0A    %7D%0A%0A    private static void deleteAccessPolicies(MediaContract service, String prefix) throws Exception %7B%0A        List%3CAccessPolicyInfo%3E policies = service.listAccessPolicies();%0A        for (AccessPolicyInfo policy : policies) %7B%0A            if (policy.getName().startsWith(prefix)) %7B%0A                service.deleteAccessPolicy(policy.getId());%0A            %7D%0A        %7D%0A    %7D%0A%0A    private static void deleteAssets(MediaContract service, String prefix) throws Exception %7B%0A        List%3CAssetInfo%3E assets = service.listAssets();%0A        for (AssetInfo asset : assets) %7B%0A            if (asset.getName().startsWith(prefix)) %7B%0A                service.deleteAsset(asset.getId());%0A            %7D%0A        %7D%0A    %7D
 %0A%0A  
@@ -2513,50 +2513,8 @@
 );%0A%0A
-        locatorIds.add(locator.getId());%0A%0A
     

Remove code from UIT that duplicated base class code