From 0d0d8323b82d8234be2a3f5880bd14763e4acd6b Mon Sep 17 00:00:00 2001 From: Nick Cook Date: Tue, 31 May 2022 17:48:49 -0700 Subject: [PATCH 1/2] feat: add Video Stitcher VOD session code samples and tests --- .../example/stitcher/CreateVodSession.java | 61 +++++++++++ .../example/stitcher/GetVodAdTagDetail.java | 59 ++++++++++ .../com/example/stitcher/GetVodSession.java | 55 ++++++++++ .../example/stitcher/GetVodStitchDetail.java | 59 ++++++++++ .../example/stitcher/ListVodAdTagDetails.java | 60 ++++++++++ .../stitcher/ListVodStitchDetails.java | 60 ++++++++++ .../stitcher/CreateVodSessionTest.java | 85 +++++++++++++++ .../stitcher/GetVodAdTagDetailTest.java | 103 ++++++++++++++++++ .../example/stitcher/GetVodSessionTest.java | 90 +++++++++++++++ .../stitcher/GetVodStitchDetailTest.java | 103 ++++++++++++++++++ .../stitcher/ListVodAdTagDetailsTest.java | 90 +++++++++++++++ .../stitcher/ListVodStitchDetailsTest.java | 90 +++++++++++++++ 12 files changed, 915 insertions(+) create mode 100644 media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java create mode 100644 media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java create mode 100644 media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java create mode 100644 media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java create mode 100644 media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java create mode 100644 media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java create mode 100644 media/stitcher/src/test/java/com/example/stitcher/CreateVodSessionTest.java create mode 100644 media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java create mode 100644 media/stitcher/src/test/java/com/example/stitcher/GetVodSessionTest.java create mode 100644 media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java create mode 100644 media/stitcher/src/test/java/com/example/stitcher/ListVodAdTagDetailsTest.java create mode 100644 media/stitcher/src/test/java/com/example/stitcher/ListVodStitchDetailsTest.java diff --git a/media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java b/media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java new file mode 100644 index 00000000000..c4a5692a3a8 --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java @@ -0,0 +1,61 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +// [START video_stitcher_create_vod_session] + +import com.google.cloud.video.stitcher.v1.CreateVodSessionRequest; +import com.google.cloud.video.stitcher.v1.LocationName; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodSession; +import java.io.IOException; + +public class CreateVodSession { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + // Uri of the media to stitch; this URI must reference either an MPEG-DASH + // manifest (.mpd) file or an M3U playlist manifest (.m3u8) file. + String sourceUri = "https://storage.googleapis.com/my-bucket/main.mpd"; + // See VMAP Pre-roll + // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) + String adTagUri = "https://pubads.g.doubleclick.net/gampad/ads..."; + createVodSession(projectId, location, sourceUri, adTagUri); + } + + public static void createVodSession( + String projectId, String location, String sourceUri, String adTagUri) throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + CreateVodSessionRequest createVodSessionRequest = + CreateVodSessionRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setVodSession( + VodSession.newBuilder().setSourceUri(sourceUri).setAdTagUri(adTagUri).build()) + .build(); + + VodSession response = videoStitcherServiceClient.createVodSession(createVodSessionRequest); + System.out.println("Created VOD session: " + response.getName()); + } + } +} +// [END video_stitcher_create_vod_session] diff --git a/media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java b/media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java new file mode 100644 index 00000000000..78013862120 --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java @@ -0,0 +1,59 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +// [START video_stitcher_get_vod_ad_tag_detail] + +import com.google.cloud.video.stitcher.v1.GetVodAdTagDetailRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodAdTagDetail; +import com.google.cloud.video.stitcher.v1.VodAdTagDetailName; +import java.io.IOException; + +public class GetVodAdTagDetail { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String sessionId = "my-session-id"; + String adTagDetailId = "my-ad-tag-id"; + + getVodAdTagDetail(projectId, location, sessionId, adTagDetailId); + } + + public static void getVodAdTagDetail( + String projectId, String location, String sessionId, String adTagDetailId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + GetVodAdTagDetailRequest getVodAdTagDetailRequest = + GetVodAdTagDetailRequest.newBuilder() + .setName( + VodAdTagDetailName.of(projectId, location, sessionId, adTagDetailId).toString()) + .build(); + + VodAdTagDetail response = + videoStitcherServiceClient.getVodAdTagDetail(getVodAdTagDetailRequest); + System.out.println("VOD ad tag detail: " + response.getName()); + } + } +} +// [END video_stitcher_get_vod_ad_tag_detail] diff --git a/media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java b/media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java new file mode 100644 index 00000000000..04928701dc7 --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java @@ -0,0 +1,55 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +// [START video_stitcher_get_vod_session] + +import com.google.cloud.video.stitcher.v1.GetVodSessionRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodSession; +import com.google.cloud.video.stitcher.v1.VodSessionName; +import java.io.IOException; + +public class GetVodSession { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String sessionId = "my-session-id"; + + getVodSession(projectId, location, sessionId); + } + + public static void getVodSession(String projectId, String location, String sessionId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + GetVodSessionRequest getVodSessionRequest = + GetVodSessionRequest.newBuilder() + .setName(VodSessionName.of(projectId, location, sessionId).toString()) + .build(); + + VodSession response = videoStitcherServiceClient.getVodSession(getVodSessionRequest); + System.out.println("VOD session: " + response.getName()); + } + } +} +// [END video_stitcher_get_vod_session] diff --git a/media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java b/media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java new file mode 100644 index 00000000000..71333f682c6 --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java @@ -0,0 +1,59 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +// [START video_stitcher_get_vod_stitch_detail] + +import com.google.cloud.video.stitcher.v1.GetVodStitchDetailRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodStitchDetail; +import com.google.cloud.video.stitcher.v1.VodStitchDetailName; +import java.io.IOException; + +public class GetVodStitchDetail { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String sessionId = "my-session-id"; + String stitchDetailId = "my-stitch-id"; + + getVodStitchDetail(projectId, location, sessionId, stitchDetailId); + } + + public static void getVodStitchDetail( + String projectId, String location, String sessionId, String stitchDetailId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + GetVodStitchDetailRequest getVodStitchDetailRequest = + GetVodStitchDetailRequest.newBuilder() + .setName( + VodStitchDetailName.of(projectId, location, sessionId, stitchDetailId).toString()) + .build(); + + VodStitchDetail response = + videoStitcherServiceClient.getVodStitchDetail(getVodStitchDetailRequest); + System.out.println("VOD stitch detail: " + response.getName()); + } + } +} +// [END video_stitcher_get_vod_stitch_detail] diff --git a/media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java b/media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java new file mode 100644 index 00000000000..db1195f364f --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java @@ -0,0 +1,60 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +// [START video_stitcher_list_vod_ad_tag_details] + +import com.google.cloud.video.stitcher.v1.ListVodAdTagDetailsRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodAdTagDetail; +import com.google.cloud.video.stitcher.v1.VodSessionName; +import java.io.IOException; + +public class ListVodAdTagDetails { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String sessionId = "my-session-id"; + + listVodAdTagDetails(projectId, location, sessionId); + } + + public static void listVodAdTagDetails(String projectId, String location, String sessionId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + ListVodAdTagDetailsRequest listVodAdTagDetailsRequest = + ListVodAdTagDetailsRequest.newBuilder() + .setParent(VodSessionName.of(projectId, location, sessionId).toString()) + .build(); + + VideoStitcherServiceClient.ListVodAdTagDetailsPagedResponse response = + videoStitcherServiceClient.listVodAdTagDetails(listVodAdTagDetailsRequest); + System.out.println("VOD ad tag details:"); + + for (VodAdTagDetail adTagDetail : response.iterateAll()) { + System.out.println(adTagDetail.toString()); + } + } + } +} +// [END video_stitcher_list_vod_ad_tag_details] diff --git a/media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java b/media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java new file mode 100644 index 00000000000..437e224c280 --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java @@ -0,0 +1,60 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +// [START video_stitcher_list_vod_stitch_details] + +import com.google.cloud.video.stitcher.v1.ListVodStitchDetailsRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodSessionName; +import com.google.cloud.video.stitcher.v1.VodStitchDetail; +import java.io.IOException; + +public class ListVodStitchDetails { + + public static void main(String[] args) throws Exception { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project-id"; + String location = "us-central1"; + String sessionId = "my-session-id"; + + listVodStitchDetails(projectId, location, sessionId); + } + + public static void listVodStitchDetails(String projectId, String location, String sessionId) + throws IOException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + ListVodStitchDetailsRequest listVodStitchDetailsRequest = + ListVodStitchDetailsRequest.newBuilder() + .setParent(VodSessionName.of(projectId, location, sessionId).toString()) + .build(); + + VideoStitcherServiceClient.ListVodStitchDetailsPagedResponse response = + videoStitcherServiceClient.listVodStitchDetails(listVodStitchDetailsRequest); + System.out.println("VOD stitch details:"); + + for (VodStitchDetail stitchDetail : response.iterateAll()) { + System.out.println(stitchDetail.toString()); + } + } + } +} +// [END video_stitcher_list_vod_stitch_details] diff --git a/media/stitcher/src/test/java/com/example/stitcher/CreateVodSessionTest.java b/media/stitcher/src/test/java/com/example/stitcher/CreateVodSessionTest.java new file mode 100644 index 00000000000..440bf9552e9 --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/CreateVodSessionTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateVodSessionTest { + + private static final String LOCATION = "us-central1"; + private static final String VOD_URI = + "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.m3u8"; + // VMAP Pre-roll + // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) + private static final String VOD_AD_TAG_URI = + "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator="; + private static String PROJECT_ID; + private static String SESSION_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() throws IOException { + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + // Project number is always returned in the VOD session name + SESSION_NAME = String.format("locations/%s/vodSessions/", LOCATION); + bout.reset(); + } + + @Test + public void test_CreateVodSession() throws IOException { + CreateVodSession.createVodSession(PROJECT_ID, LOCATION, VOD_URI, VOD_AD_TAG_URI); + String output = bout.toString(); + assertThat(output, containsString(SESSION_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + // No delete method for a VOD session + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java b/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java new file mode 100644 index 00000000000..d5ca5436fe5 --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetVodAdTagDetailTest { + + private static final String LOCATION = "us-central1"; + private static final String VOD_URI = + "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.m3u8"; + // VMAP Pre-roll + // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) + private static final String VOD_AD_TAG_URI = + "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator="; + private static String PROJECT_ID; + private static String SESSION_ID; + private static String AD_TAG_DETAIL_ID; + private static String AD_TAG_DETAIL_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() throws IOException { + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CreateVodSession.createVodSession(PROJECT_ID, LOCATION, VOD_URI, VOD_AD_TAG_URI); + String output = bout.toString(); + String[] arr = output.split("/"); + SESSION_ID = arr[arr.length - 1].replace("\n", ""); + String SESSION_NAME = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); + bout.reset(); + + ListVodAdTagDetails.listVodAdTagDetails(PROJECT_ID, LOCATION, SESSION_ID); + Matcher idMatcher = + Pattern.compile("name: \"projects/.*/" + SESSION_NAME + "vodAdTagDetails/(.*)\"") + .matcher(bout.toString()); + if (idMatcher.find()) { + AD_TAG_DETAIL_ID = idMatcher.group(1); + AD_TAG_DETAIL_NAME = SESSION_NAME + "vodAdTagDetails/" + AD_TAG_DETAIL_ID; + } + bout.reset(); + } + + @Test + public void test_GetVodAdTagDetailTest() throws IOException { + GetVodAdTagDetail.getVodAdTagDetail(PROJECT_ID, LOCATION, SESSION_ID, AD_TAG_DETAIL_ID); + String output = bout.toString(); + assertThat(output, containsString(AD_TAG_DETAIL_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + // No delete method for a VOD session + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/stitcher/src/test/java/com/example/stitcher/GetVodSessionTest.java b/media/stitcher/src/test/java/com/example/stitcher/GetVodSessionTest.java new file mode 100644 index 00000000000..8a758312269 --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/GetVodSessionTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetVodSessionTest { + + private static final String LOCATION = "us-central1"; + private static final String VOD_URI = + "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.m3u8"; + // VMAP Pre-roll + // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) + private static final String VOD_AD_TAG_URI = + "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator="; + private static String PROJECT_ID; + private static String SESSION_ID; + private static String SESSION_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() throws IOException { + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CreateVodSession.createVodSession(PROJECT_ID, LOCATION, VOD_URI, VOD_AD_TAG_URI); + String output = bout.toString(); + String[] arr = output.split("/"); + SESSION_ID = arr[arr.length - 1].replace("\n", ""); + SESSION_NAME = String.format("locations/%s/vodSessions/%s", LOCATION, SESSION_ID); + bout.reset(); + } + + @Test + public void test_GetVodSession() throws IOException { + GetVodSession.getVodSession(PROJECT_ID, LOCATION, SESSION_ID); + String output = bout.toString(); + assertThat(output, containsString(SESSION_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + // No delete method for a VOD session + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java b/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java new file mode 100644 index 00000000000..883e0c93b99 --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetVodStitchDetailTest { + + private static final String LOCATION = "us-central1"; + private static final String VOD_URI = + "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.m3u8"; + // VMAP Pre-roll + // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) + private static final String VOD_AD_TAG_URI = + "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator="; + private static String PROJECT_ID; + private static String SESSION_ID; + private static String STITCH_DETAIL_ID; + private static String STITCH_DETAIL_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() throws IOException { + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CreateVodSession.createVodSession(PROJECT_ID, LOCATION, VOD_URI, VOD_AD_TAG_URI); + String output = bout.toString(); + String[] arr = output.split("/"); + SESSION_ID = arr[arr.length - 1].replace("\n", ""); + String SESSION_NAME = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); + bout.reset(); + + ListVodStitchDetails.listVodStitchDetails(PROJECT_ID, LOCATION, SESSION_ID); + Matcher idMatcher = + Pattern.compile("name: \"projects/.*/" + SESSION_NAME + "vodStitchDetails/(.*)\"") + .matcher(bout.toString()); + if (idMatcher.find()) { + STITCH_DETAIL_ID = idMatcher.group(1); + STITCH_DETAIL_NAME = SESSION_NAME + "vodStitchDetails/" + STITCH_DETAIL_ID; + } + bout.reset(); + } + + @Test + public void test_GetVodStitchDetailTest() throws IOException { + GetVodStitchDetail.getVodStitchDetail(PROJECT_ID, LOCATION, SESSION_ID, STITCH_DETAIL_ID); + String output = bout.toString(); + assertThat(output, containsString(STITCH_DETAIL_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + // No delete method for a VOD session + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/stitcher/src/test/java/com/example/stitcher/ListVodAdTagDetailsTest.java b/media/stitcher/src/test/java/com/example/stitcher/ListVodAdTagDetailsTest.java new file mode 100644 index 00000000000..108b96d5b8a --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/ListVodAdTagDetailsTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListVodAdTagDetailsTest { + + private static final String LOCATION = "us-central1"; + private static final String VOD_URI = + "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.m3u8"; + // VMAP Pre-roll + // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) + private static final String VOD_AD_TAG_URI = + "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator="; + private static String PROJECT_ID; + private static String SESSION_ID; + private static String SESSION_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() throws IOException { + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CreateVodSession.createVodSession(PROJECT_ID, LOCATION, VOD_URI, VOD_AD_TAG_URI); + String output = bout.toString(); + String[] arr = output.split("/"); + SESSION_ID = arr[arr.length - 1].replace("\n", ""); + SESSION_NAME = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); + bout.reset(); + } + + @Test + public void test_ListVodAdTagDetailsTest() throws IOException { + ListVodAdTagDetails.listVodAdTagDetails(PROJECT_ID, LOCATION, SESSION_ID); + String output = bout.toString(); + assertThat(output, containsString(SESSION_NAME.concat("vodAdTagDetails/"))); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + // No delete method for a VOD session + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/stitcher/src/test/java/com/example/stitcher/ListVodStitchDetailsTest.java b/media/stitcher/src/test/java/com/example/stitcher/ListVodStitchDetailsTest.java new file mode 100644 index 00000000000..b0a9cb42629 --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/ListVodStitchDetailsTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2022 Google LLC + * + * 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.example.stitcher; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListVodStitchDetailsTest { + + private static final String LOCATION = "us-central1"; + private static final String VOD_URI = + "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.m3u8"; + // VMAP Pre-roll + // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) + private static final String VOD_AD_TAG_URI = + "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/vmap_ad_samples&sz=640x480&cust_params=sample_ar%3Dpreonly&ciu_szs=300x250%2C728x90&gdfp_req=1&ad_rule=1&output=vmap&unviewed_position_start=1&env=vp&impl=s&correlator="; + private static String PROJECT_ID; + private static String SESSION_ID; + private static String SESSION_NAME; + private static PrintStream originalOut; + private ByteArrayOutputStream bout; + + private static String requireEnvVar(String varName) { + String varValue = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' is required to perform these tests.", varName)); + return varValue; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void beforeTest() throws IOException { + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + CreateVodSession.createVodSession(PROJECT_ID, LOCATION, VOD_URI, VOD_AD_TAG_URI); + String output = bout.toString(); + String[] arr = output.split("/"); + SESSION_ID = arr[arr.length - 1].replace("\n", ""); + SESSION_NAME = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); + bout.reset(); + } + + @Test + public void test_ListVodStitchDetailsTest() throws IOException { + ListVodStitchDetails.listVodStitchDetails(PROJECT_ID, LOCATION, SESSION_ID); + String output = bout.toString(); + assertThat(output, containsString(SESSION_NAME.concat("vodStitchDetails/"))); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + // No delete method for a VOD session + System.setOut(originalOut); + bout.reset(); + } +} From 60bc46e0675efdb8c2ff4bbfabb407bdadbe56b5 Mon Sep 17 00:00:00 2001 From: Nick Cook Date: Tue, 31 May 2022 19:07:29 -0700 Subject: [PATCH 2/2] fix lint error --- .../java/com/example/stitcher/GetVodAdTagDetailTest.java | 6 +++--- .../java/com/example/stitcher/GetVodStitchDetailTest.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java b/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java index d5ca5436fe5..86ba23a7860 100644 --- a/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java +++ b/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java @@ -72,16 +72,16 @@ public void beforeTest() throws IOException { String output = bout.toString(); String[] arr = output.split("/"); SESSION_ID = arr[arr.length - 1].replace("\n", ""); - String SESSION_NAME = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); + String sessionName = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); bout.reset(); ListVodAdTagDetails.listVodAdTagDetails(PROJECT_ID, LOCATION, SESSION_ID); Matcher idMatcher = - Pattern.compile("name: \"projects/.*/" + SESSION_NAME + "vodAdTagDetails/(.*)\"") + Pattern.compile("name: \"projects/.*/" + sessionName + "vodAdTagDetails/(.*)\"") .matcher(bout.toString()); if (idMatcher.find()) { AD_TAG_DETAIL_ID = idMatcher.group(1); - AD_TAG_DETAIL_NAME = SESSION_NAME + "vodAdTagDetails/" + AD_TAG_DETAIL_ID; + AD_TAG_DETAIL_NAME = sessionName + "vodAdTagDetails/" + AD_TAG_DETAIL_ID; } bout.reset(); } diff --git a/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java b/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java index 883e0c93b99..757334ee962 100644 --- a/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java +++ b/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java @@ -72,16 +72,16 @@ public void beforeTest() throws IOException { String output = bout.toString(); String[] arr = output.split("/"); SESSION_ID = arr[arr.length - 1].replace("\n", ""); - String SESSION_NAME = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); + String sessionName = String.format("locations/%s/vodSessions/%s/", LOCATION, SESSION_ID); bout.reset(); ListVodStitchDetails.listVodStitchDetails(PROJECT_ID, LOCATION, SESSION_ID); Matcher idMatcher = - Pattern.compile("name: \"projects/.*/" + SESSION_NAME + "vodStitchDetails/(.*)\"") + Pattern.compile("name: \"projects/.*/" + sessionName + "vodStitchDetails/(.*)\"") .matcher(bout.toString()); if (idMatcher.find()) { STITCH_DETAIL_ID = idMatcher.group(1); - STITCH_DETAIL_NAME = SESSION_NAME + "vodStitchDetails/" + STITCH_DETAIL_ID; + STITCH_DETAIL_NAME = sessionName + "vodStitchDetails/" + STITCH_DETAIL_ID; } bout.reset(); }