com.brightcove.test.upload.BatchIntegrationTest.java Source code

Java tutorial

Introduction

Here is the source code for com.brightcove.test.upload.BatchIntegrationTest.java

Source

/**
 * Copyright (C) 2011 Brightcove Inc. All Rights Reserved. No use, copying or distribution of this
 * work may be made except in accordance with a valid license agreement from Brightcove Inc. This
 * notice must be included on all copies, modifications and derivatives of this work.
 * 
 * Brightcove Inc MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. BRIGHTCOVE SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 * SOFTWARE OR ITS DERIVATIVES.
 * 
 * "Brightcove" is a registered trademark of Brightcove Inc.
 */
package com.brightcove.test.upload;

import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.httpclient.HttpException;
import org.json.JSONException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xml.sax.SAXException;

import com.brightcove.com.uploader.helper.EnvironmentRegistry;
import com.brightcove.com.uploader.verifier.ExistenceCheck;
import com.brightcove.com.uploader.verifier.ValidatableMannheimUpload;
import com.brightcove.com.zartan.serializerInterface.manheim.VideosToValidateSerializer;
import com.brightcove.common.logging.BrightcoveLog;
import com.brightcove.test.upload.helpers.IntegrationTestHelper;
import com.brightcove.uploader.config.Account;
import com.brightcove.uploader.config.Environment;
import com.brightcove.uploader.exception.BadEnvironmentException;
import com.brightcove.uploader.exception.MediaAPIError;
import com.brightcove.uploader.input.Batch;
import com.brightcove.uploader.input.MediaAPI;
import com.brightcove.uploader.upload.IngestFile;
import com.brightcove.uploader.upload.Options;

public class BatchIntegrationTest {
    private static Account mAccount;
    private static Environment mEnvironment;
    private static IngestFile mVideoFile;
    private static ExistenceCheck mExistenceCheck;
    private static Batch mBatch;
    private static boolean mDebug = true;
    private static Options uploadOpts;
    private BrightcoveLog mLog = BrightcoveLog.getLogger(this.getClass());

    @BeforeClass
    public static void setup() throws URISyntaxException, IOException {
        //TODO: Move this to a profile or props file
        mAccount = new Account("ppasad@brightcove.com", "joejoe", 1047755113001l);
        mAccount.setDefaultReadToken("gwgpmRGirrmqnCKRPbjBjtPC0RcUMeQ95vd6e9LY2mHWY1jBOkd7Zg..");
        mAccount.setDefaultWriteToken("gwgpmRGirrlphu06C-u8cZd_sytpDgJqLzTKVoh2McQq8fQ5cWe9cQ..");
        mAccount.setFtpLogin("qa");
        mAccount.setFtpPassword("Password!");
        Map<String, String> settings = new HashMap<String, String>();
        settings.put("drmEnabled", "true");
        mAccount.setSettings(settings);

        IntegrationTestHelper.loadEnvironments();

        String envName = System.getProperty("test.environment", "kong");
        mEnvironment = EnvironmentRegistry.getEnvironment(envName);
        mEnvironment.setFTPRootDir("/pub/batch/qa");

        //TODO: Move this to a profile or props file
        uploadOpts = new Options();
        uploadOpts.setMBR(true);
        mVideoFile = new IngestFile(findFileInClasspath("10sec.mp4").getAbsolutePath(), uploadOpts);
        mBatch = new Batch();
        mExistenceCheck = new ExistenceCheck();
        mExistenceCheck.setmEnvironment(mEnvironment);
    }

    private static File findFileInClasspath(String pFileName) throws URISyntaxException {
        URL url = ClassLoader.getSystemResource(pFileName);
        if (url == null) {
            return null;
        }
        return new File(url.toURI());

    }

    private void upload(Long timeout)
            throws MediaAPIError, URISyntaxException, JSONException, BadEnvironmentException, HttpException,
            IOException, ParserConfigurationException, SAXException, InterruptedException {
        //upload a file
        ArrayList<ValidatableMannheimUpload> jobsToValidate = new ArrayList<ValidatableMannheimUpload>();

        String videoRefId = mBatch.createVideo(mVideoFile, mAccount, mEnvironment);
        mLog.info("Batch Uploading the video having refId " + videoRefId);
        if (!mVideoFile.isAssetOnly()) {
            mLog.debug("\n VideoId Ref Id: " + videoRefId);
            assertNotNull(videoRefId);
            MediaAPI mMediaApi = new MediaAPI();
            URI targetURL = new URL("http", mEnvironment.getAPIServer(), mEnvironment.getAPIPort(),
                    "/services/post").toURI();
            ValidatableMannheimUpload upload = new ValidatableMannheimUpload("notMann", mAccount, mVideoFile);
            upload.setUploadDate(System.currentTimeMillis());
            jobsToValidate.add(upload);
            VideosToValidateSerializer serializer = new VideosToValidateSerializer();
            serializer.setValidatableMannheimUpload(jobsToValidate);
            serializer.writeArtifact("videosForValidation.json");
        }

    }

    @Test
    public void uploadMBR_mp4() throws MediaAPIError, URISyntaxException, JSONException, BadEnvironmentException,
            HttpException, IOException, ParserConfigurationException, SAXException, InterruptedException {
        mVideoFile = new IngestFile(findFileInClasspath("10sec.mp4").getAbsolutePath(), uploadOpts);
        upload(1200000L);
    }

    @Test
    public void uploadMBR_flv() throws MediaAPIError, URISyntaxException, JSONException, BadEnvironmentException,
            HttpException, IOException, ParserConfigurationException, SAXException, InterruptedException {
        mVideoFile = new IngestFile(findFileInClasspath("10sec.mp4").getAbsolutePath(), uploadOpts);
        mVideoFile.getOptions().setTargetCodec("FLV"); //careful; mVideoFile is static and not thread-safe, so set it back when you're done and watch out for concurrency.        
        try {
            upload(1200000L);
        } finally {
            mVideoFile.getOptions().setTargetCodec("MP4");
        }
    }

    @Test
    public void uploadSBR_assetOnly()
            throws MediaAPIError, URISyntaxException, JSONException, BadEnvironmentException, HttpException,
            IOException, ParserConfigurationException, SAXException, InterruptedException {
        mVideoFile = new IngestFile(findFileInClasspath("10sec.mp4").getAbsolutePath(), mVideoFile.getOptions(),
                true);
        mVideoFile.getOptions().setMBR(false); //careful; mVideoFile is static and not thread-safe, so set it back when you're done and watch out for concurrency.

        try {
            upload(1200000L);
        } finally {
            mVideoFile.getOptions().setMBR(true);
            mVideoFile = new IngestFile(findFileInClasspath("10sec.mp4").getAbsolutePath(),
                    mVideoFile.getOptions());
        }
    }

    public void uploadSBR_large() throws MediaAPIError, URISyntaxException, JSONException, BadEnvironmentException,
            HttpException, IOException, ParserConfigurationException, SAXException, InterruptedException {
        // The file size is greater than Integer.MAX_VALUE
        String largeFileName = "HOW_TO_BETTER_PROTECT_YOUR_ASSE.m4v";

        mVideoFile = new IngestFile(findFileInClasspath(largeFileName).getAbsolutePath(), mVideoFile.getOptions());
        mVideoFile.getOptions().setMBR(false); //careful; mVideoFile is static and not thread-safe, so set it back when you're done and watch out for concurrency.

        try {
            upload(5000000L);
        } finally {
            mVideoFile.getOptions().setMBR(true);
            mVideoFile = new IngestFile(findFileInClasspath("10sec.mp4").getAbsolutePath(),
                    mVideoFile.getOptions());
        }
    }
}