Java tutorial
/** * Copyright (C) 2012 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.com.zartan.verifier.video; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; import java.util.ArrayList; import java.util.List; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; import org.codehaus.jackson.JsonParser.NumberType; import org.json.JSONException; import org.junit.internal.runners.model.MultipleFailureException; import com.brightcove.com.uploader.verifier.UploadData; import com.brightcove.com.zartan.verifier.ZartanVerifier; import com.brightcove.com.zartan.verifier.ZartanVerifier.ZartanCheck; import com.brightcove.com.zartan.verifier.result.ResultEnum; import com.brightcove.common.logging.BrightcoveLog; import com.brightcove.uploader.exception.MediaAPIError; public class StreamingFLVURLVerifier extends ZartanVerifier { private class Handler extends URLStreamHandler { public static final int DEFAULT_RTSP_PORT = 554; @Override protected URLConnection openConnection(URL url) throws IOException { return null; } /** * @return the default RTSP port */ @Override protected int getDefaultPort() { return DEFAULT_RTSP_PORT; } } private BrightcoveLog mLog = BrightcoveLog.getLogger(this.getClass()); @Override public boolean isApplicable(UploadData pData) { //is JSON populated if (pData.getUploadMethod() != UploadData.UploadType.MGR && //check to see if this verification should be skipped, due to upload type. pData.getRtmpResponseJson() != null && pData.getmAccount().isStreaming()) { return true; } mLog.info(this.getClass().getName() + " is not applicable for this test."); return false; } @ZartanCheck(value = "Primary Rendition is delivered over rtmp") public ResultEnum assertPrimaryRenditionProtocolCorrect(UploadData upData) throws Throwable { URL u = getPrimaryRenditionUrl(upData); assertEquals("Protocol should be rtmp", "rtmp", u.getProtocol()); return ResultEnum.PASS; } @ZartanCheck(value = "Primary Rendition is the correct type") public ResultEnum assertPrimaryRenditionTypeCorrect(UploadData upData) throws Throwable { URL u = getPrimaryRenditionUrl(upData); if (upData.getmOptions().getTargetCodec().equals("MP4")) { assertTrue("type should be mp4 of " + u.getPath(), u.getPath().toLowerCase().endsWith("mp4")); } else { assertTrue("type should be flv of " + u.getPath(), u.getPath().toLowerCase().endsWith("flv")); } return ResultEnum.PASS; } @ZartanCheck(value = "Primary Rendition has no billing params") public ResultEnum assertPrimaryRenditionBillingCorrect(UploadData upData) throws Throwable { URL u = getPrimaryRenditionUrl(upData); assertTrue("Primary Rendition should have no billing params", !u.getQuery().contains("pubId=" + upData.getmAccount().getId())); return ResultEnum.PASS; } @ZartanCheck(value = "Primary Rendition is on the streaming cdn") public ResultEnum assertPrimaryRenditionCDNCorrect(UploadData upData) throws Throwable { URL u = getPrimaryRenditionUrl(upData); assertEquals("Primary Rendition should be on the streaming cdn", u.getHost(), upData.getmAccount().getStreamingCdn().getHostName()); return ResultEnum.PASS; } private URL getPrimaryRenditionUrl(UploadData upData) throws MalformedURLException { JsonNode actual = upData.getRtmpResponseJson().get("FLVURL"); String urlNodeText = actual.getTextValue(); assertNotNull(urlNodeText); URL u = new URL(null, urlNodeText, new Handler()); return u; } }