diff --git a/media/stitcher/pom.xml b/media/stitcher/pom.xml index 61c88e4baeb..6d6d5ee0f6b 100644 --- a/media/stitcher/pom.xml +++ b/media/stitcher/pom.xml @@ -42,7 +42,7 @@ com.google.cloud libraries-bom - 26.32.0 + 26.42.0 pom import diff --git a/media/stitcher/src/main/java/com/example/stitcher/CreateVodConfig.java b/media/stitcher/src/main/java/com/example/stitcher/CreateVodConfig.java new file mode 100644 index 00000000000..3a9ef21d58a --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/CreateVodConfig.java @@ -0,0 +1,76 @@ +/* + * Copyright 2024 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 videostitcher_create_vod_config] + +import com.google.cloud.video.stitcher.v1.CreateVodConfigRequest; +import com.google.cloud.video.stitcher.v1.LocationName; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodConfig; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateVodConfig { + + private static final int TIMEOUT_IN_MINUTES = 2; + + 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 vodConfigId = "my-vod-config-id"; + // URI of the VOD stream 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..."; + + createVodConfig(projectId, location, vodConfigId, sourceUri, adTagUri); + } + + // Creates a video on demand (VOD) config. VOD configs are used to configure VOD + // sessions. For more information, see + // https://cloud.google.com/video-stitcher/docs/how-to/managing-vod-configs. + public static VodConfig createVodConfig( + String projectId, String location, String vodConfigId, String sourceUri, String adTagUri) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + CreateVodConfigRequest createVodConfigRequest = + CreateVodConfigRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setVodConfigId(vodConfigId) + .setVodConfig( + VodConfig.newBuilder().setSourceUri(sourceUri).setAdTagUri(adTagUri).build()) + .build(); + + VodConfig response = + videoStitcherServiceClient + .createVodConfigAsync(createVodConfigRequest) + .get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES); + System.out.println("Created new VOD config: " + response.getName()); + return response; + } + } +} +// [END videostitcher_create_vod_config] diff --git a/media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java b/media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java index 0e96d2b1dcf..7076c90835e 100644 --- a/media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java +++ b/media/stitcher/src/main/java/com/example/stitcher/CreateVodSession.java @@ -22,6 +22,7 @@ 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.VodConfigName; import com.google.cloud.video.stitcher.v1.VodSession; import java.io.IOException; @@ -31,20 +32,16 @@ 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); + String vodConfigId = "my-vod-config-id"; + + createVodSession(projectId, location, vodConfigId); } - public static void createVodSession( - String projectId, String location, String sourceUri, String adTagUri) throws IOException { + // Creates a video on demand (VOD) session using the parameters in the designated VOD config. + public static VodSession createVodSession(String projectId, String location, String vodConfigId) + 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. In this example, try-with-resources is used - // which automatically calls close() on the client to clean up resources. + // once, and can be reused for multiple requests. try (VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create()) { CreateVodSessionRequest createVodSessionRequest = @@ -52,14 +49,14 @@ public static void createVodSession( .setParent(LocationName.of(projectId, location).toString()) .setVodSession( VodSession.newBuilder() - .setSourceUri(sourceUri) - .setAdTagUri(adTagUri) + .setVodConfig(VodConfigName.format(projectId, location, vodConfigId)) .setAdTracking(AdTracking.SERVER) .build()) .build(); VodSession response = videoStitcherServiceClient.createVodSession(createVodSessionRequest); System.out.println("Created VOD session: " + response.getName()); + return response; } } } diff --git a/media/stitcher/src/main/java/com/example/stitcher/DeleteVodConfig.java b/media/stitcher/src/main/java/com/example/stitcher/DeleteVodConfig.java new file mode 100644 index 00000000000..dc9a85baa40 --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/DeleteVodConfig.java @@ -0,0 +1,61 @@ +/* + * Copyright 2024 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 videostitcher_delete_vod_config] + +import com.google.cloud.video.stitcher.v1.DeleteVodConfigRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodConfigName; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class DeleteVodConfig { + + private static final int TIMEOUT_IN_MINUTES = 2; + + 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 vodConfigId = "my-vod-config-id"; + + deleteVodConfig(projectId, location, vodConfigId); + } + + // Deletes a video on demand (VOD) config. + public static void deleteVodConfig(String projectId, String location, String vodConfigId) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + DeleteVodConfigRequest deleteVodConfigRequest = + DeleteVodConfigRequest.newBuilder() + .setName(VodConfigName.of(projectId, location, vodConfigId).toString()) + .build(); + + videoStitcherServiceClient + .deleteVodConfigAsync(deleteVodConfigRequest) + .get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES); + System.out.println("Deleted VOD config"); + } + } +} +// [END videostitcher_delete_vod_config] diff --git a/media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java b/media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java index c810372a710..cd876752737 100644 --- a/media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java +++ b/media/stitcher/src/main/java/com/example/stitcher/GetVodAdTagDetail.java @@ -36,12 +36,12 @@ public static void main(String[] args) throws Exception { getVodAdTagDetail(projectId, location, sessionId, adTagDetailId); } - public static void getVodAdTagDetail( + // Gets an ad tag detail for a video on demand (VOD) session. + public static VodAdTagDetail 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. In this example, try-with-resources is used - // which automatically calls close() on the client to clean up resources. + // once, and can be reused for multiple requests. try (VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create()) { GetVodAdTagDetailRequest getVodAdTagDetailRequest = @@ -53,6 +53,7 @@ public static void getVodAdTagDetail( VodAdTagDetail response = videoStitcherServiceClient.getVodAdTagDetail(getVodAdTagDetailRequest); System.out.println("VOD ad tag detail: " + response.getName()); + return response; } } } diff --git a/media/stitcher/src/main/java/com/example/stitcher/GetVodConfig.java b/media/stitcher/src/main/java/com/example/stitcher/GetVodConfig.java new file mode 100644 index 00000000000..a89d24f8b71 --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/GetVodConfig.java @@ -0,0 +1,56 @@ +/* + * Copyright 2024 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 videostitcher_get_vod_config] + +import com.google.cloud.video.stitcher.v1.GetVodConfigRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodConfig; +import com.google.cloud.video.stitcher.v1.VodConfigName; +import java.io.IOException; + +public class GetVodConfig { + + 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 vodConfigId = "my-vod-config-id"; + + getVodConfig(projectId, location, vodConfigId); + } + + // Gets a video on demand (VOD) config. + public static VodConfig getVodConfig(String projectId, String location, String vodConfigId) + 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. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + GetVodConfigRequest getVodConfigRequest = + GetVodConfigRequest.newBuilder() + .setName(VodConfigName.of(projectId, location, vodConfigId).toString()) + .build(); + + VodConfig response = videoStitcherServiceClient.getVodConfig(getVodConfigRequest); + System.out.println("VOD config: " + response.getName()); + return response; + } + } +} +// [END videostitcher_get_vod_config] diff --git a/media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java b/media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java index 8d1a0c2416c..a0f19cf7614 100644 --- a/media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java +++ b/media/stitcher/src/main/java/com/example/stitcher/GetVodSession.java @@ -35,11 +35,11 @@ public static void main(String[] args) throws Exception { getVodSession(projectId, location, sessionId); } - public static void getVodSession(String projectId, String location, String sessionId) + // Gets a video on demand (VOD) session. + public static VodSession 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. In this example, try-with-resources is used - // which automatically calls close() on the client to clean up resources. + // once, and can be reused for multiple requests. try (VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create()) { GetVodSessionRequest getVodSessionRequest = @@ -49,6 +49,7 @@ public static void getVodSession(String projectId, String location, String sessi VodSession response = videoStitcherServiceClient.getVodSession(getVodSessionRequest); System.out.println("VOD session: " + response.getName()); + return response; } } } diff --git a/media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java b/media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java index 8c701f0e68e..e3518119364 100644 --- a/media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java +++ b/media/stitcher/src/main/java/com/example/stitcher/GetVodStitchDetail.java @@ -36,12 +36,12 @@ public static void main(String[] args) throws Exception { getVodStitchDetail(projectId, location, sessionId, stitchDetailId); } - public static void getVodStitchDetail( + // Gets a stitch detail for a video on demand (VOD) session. + public static VodStitchDetail 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. In this example, try-with-resources is used - // which automatically calls close() on the client to clean up resources. + // once, and can be reused for multiple requests. try (VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create()) { GetVodStitchDetailRequest getVodStitchDetailRequest = @@ -53,6 +53,7 @@ public static void getVodStitchDetail( VodStitchDetail response = videoStitcherServiceClient.getVodStitchDetail(getVodStitchDetailRequest); System.out.println("VOD stitch detail: " + response.getName()); + return response; } } } diff --git a/media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java b/media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java index 5ebb865d228..c9325c995b5 100644 --- a/media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java +++ b/media/stitcher/src/main/java/com/example/stitcher/ListVodAdTagDetails.java @@ -20,6 +20,7 @@ import com.google.cloud.video.stitcher.v1.ListVodAdTagDetailsRequest; import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient.ListVodAdTagDetailsPagedResponse; import com.google.cloud.video.stitcher.v1.VodAdTagDetail; import com.google.cloud.video.stitcher.v1.VodSessionName; import java.io.IOException; @@ -35,11 +36,11 @@ public static void main(String[] args) throws Exception { listVodAdTagDetails(projectId, location, sessionId); } - public static void listVodAdTagDetails(String projectId, String location, String sessionId) - throws IOException { + // Lists the ad tag details for a video on demand (VOD) session. + public static ListVodAdTagDetailsPagedResponse 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. In this example, try-with-resources is used - // which automatically calls close() on the client to clean up resources. + // once, and can be reused for multiple requests. try (VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create()) { ListVodAdTagDetailsRequest listVodAdTagDetailsRequest = @@ -49,11 +50,12 @@ public static void listVodAdTagDetails(String projectId, String location, String VideoStitcherServiceClient.ListVodAdTagDetailsPagedResponse response = videoStitcherServiceClient.listVodAdTagDetails(listVodAdTagDetailsRequest); - System.out.println("VOD ad tag details:"); + System.out.println("VOD ad tag details:"); for (VodAdTagDetail adTagDetail : response.iterateAll()) { System.out.println(adTagDetail.toString()); } + return response; } } } diff --git a/media/stitcher/src/main/java/com/example/stitcher/ListVodConfigs.java b/media/stitcher/src/main/java/com/example/stitcher/ListVodConfigs.java new file mode 100644 index 00000000000..3e4678c929f --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/ListVodConfigs.java @@ -0,0 +1,61 @@ +/* + * Copyright 2024 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 videostitcher_list_vod_configs] + +import com.google.cloud.video.stitcher.v1.ListVodConfigsRequest; +import com.google.cloud.video.stitcher.v1.LocationName; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient.ListVodConfigsPagedResponse; +import com.google.cloud.video.stitcher.v1.VodConfig; +import java.io.IOException; + +public class ListVodConfigs { + + 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"; + + listVodConfigs(projectId, location); + } + + // Lists all the video on demand (VOD) configs for a given project and locatin. + public static ListVodConfigsPagedResponse listVodConfigs(String projectId, String location) + 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. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + ListVodConfigsRequest listVodConfigsRequest = + ListVodConfigsRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .build(); + + VideoStitcherServiceClient.ListVodConfigsPagedResponse response = + videoStitcherServiceClient.listVodConfigs(listVodConfigsRequest); + + System.out.println("VOD configs:"); + for (VodConfig vodConfig : response.iterateAll()) { + System.out.println(vodConfig.getName()); + } + return response; + } + } +} +// [END videostitcher_list_vod_configs] diff --git a/media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java b/media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java index d73eb7bd3a7..c8ad79c1422 100644 --- a/media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java +++ b/media/stitcher/src/main/java/com/example/stitcher/ListVodStitchDetails.java @@ -20,6 +20,7 @@ import com.google.cloud.video.stitcher.v1.ListVodStitchDetailsRequest; import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient.ListVodStitchDetailsPagedResponse; import com.google.cloud.video.stitcher.v1.VodSessionName; import com.google.cloud.video.stitcher.v1.VodStitchDetail; import java.io.IOException; @@ -35,11 +36,11 @@ public static void main(String[] args) throws Exception { listVodStitchDetails(projectId, location, sessionId); } - public static void listVodStitchDetails(String projectId, String location, String sessionId) - throws IOException { + // Lists the VOD stitch details for a video on demand (VOD) session. + public static ListVodStitchDetailsPagedResponse 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. In this example, try-with-resources is used - // which automatically calls close() on the client to clean up resources. + // once, and can be reused for multiple requests. try (VideoStitcherServiceClient videoStitcherServiceClient = VideoStitcherServiceClient.create()) { ListVodStitchDetailsRequest listVodStitchDetailsRequest = @@ -49,11 +50,12 @@ public static void listVodStitchDetails(String projectId, String location, Strin VideoStitcherServiceClient.ListVodStitchDetailsPagedResponse response = videoStitcherServiceClient.listVodStitchDetails(listVodStitchDetailsRequest); - System.out.println("VOD stitch details:"); + System.out.println("VOD stitch details:"); for (VodStitchDetail stitchDetail : response.iterateAll()) { System.out.println(stitchDetail.toString()); } + return response; } } } diff --git a/media/stitcher/src/main/java/com/example/stitcher/UpdateVodConfig.java b/media/stitcher/src/main/java/com/example/stitcher/UpdateVodConfig.java new file mode 100644 index 00000000000..4bcd279e0ed --- /dev/null +++ b/media/stitcher/src/main/java/com/example/stitcher/UpdateVodConfig.java @@ -0,0 +1,76 @@ +/* + * Copyright 2024 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 videostitcher_update_vod_config] + +import com.google.cloud.video.stitcher.v1.UpdateVodConfigRequest; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodConfig; +import com.google.cloud.video.stitcher.v1.VodConfigName; +import com.google.protobuf.FieldMask; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class UpdateVodConfig { + + private static final int TIMEOUT_IN_MINUTES = 2; + + 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 vodConfigId = "my-vod-config-id"; + // Updated URI of the VOD stream 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"; + + updateVodConfig(projectId, location, vodConfigId, sourceUri); + } + + // Updates the source URI in a video on demand (VOD) config. + public static VodConfig updateVodConfig( + String projectId, String location, String vodConfigId, String sourceUri) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + UpdateVodConfigRequest updateVodConfigRequest = + UpdateVodConfigRequest.newBuilder() + .setVodConfig( + VodConfig.newBuilder() + .setName(VodConfigName.of(projectId, location, vodConfigId).toString()) + .setSourceUri(sourceUri) + .build()) + // Set the update mask to the sourceUri field in the existing VOD config. You must set + // the mask to the field you want to update. + .setUpdateMask(FieldMask.newBuilder().addPaths("sourceUri").build()) + .build(); + + VodConfig response = + videoStitcherServiceClient + .updateVodConfigAsync(updateVodConfigRequest) + .get(TIMEOUT_IN_MINUTES, TimeUnit.MINUTES); + System.out.println("Updated VOD config: " + response.getName()); + return response; + } + } +} +// [END videostitcher_update_vod_config] diff --git a/media/stitcher/src/test/java/com/example/stitcher/CreateVodSessionTest.java b/media/stitcher/src/test/java/com/example/stitcher/CreateVodSessionTest.java deleted file mode 100644 index a44a6748ae1..00000000000 --- a/media/stitcher/src/test/java/com/example/stitcher/CreateVodSessionTest.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; -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.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class CreateVodSessionTest { - - @Rule - public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); - 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/", TestUtils.LOCATION); - bout.reset(); - } - - @Test - public void test_CreateVodSession() throws IOException { - CreateVodSession.createVodSession(PROJECT_ID, TestUtils.LOCATION, TestUtils.VOD_URI, - TestUtils.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 deleted file mode 100644 index 95227816fc5..00000000000 --- a/media/stitcher/src/test/java/com/example/stitcher/GetVodAdTagDetailTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; -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.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GetVodAdTagDetailTest { - - @Rule - public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); - 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, TestUtils.LOCATION, TestUtils.VOD_URI, - TestUtils.VOD_AD_TAG_URI); - String output = bout.toString(); - String[] arr = output.split("/"); - SESSION_ID = arr[arr.length - 1].replace("\n", ""); - String sessionName = String.format("locations/%s/vodSessions/%s/", TestUtils.LOCATION, - SESSION_ID); - bout.reset(); - - ListVodAdTagDetails.listVodAdTagDetails(PROJECT_ID, TestUtils.LOCATION, SESSION_ID); - Matcher idMatcher = - Pattern.compile("name: \"projects/.*/" + sessionName + "vodAdTagDetails/(.*)\"") - .matcher(bout.toString()); - if (idMatcher.find()) { - AD_TAG_DETAIL_ID = idMatcher.group(1); - AD_TAG_DETAIL_NAME = sessionName + "vodAdTagDetails/" + AD_TAG_DETAIL_ID; - } - bout.reset(); - } - - @Test - public void test_GetVodAdTagDetailTest() throws IOException { - GetVodAdTagDetail.getVodAdTagDetail(PROJECT_ID, TestUtils.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 deleted file mode 100644 index 8124805b7f7..00000000000 --- a/media/stitcher/src/test/java/com/example/stitcher/GetVodSessionTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; -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.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GetVodSessionTest { - - @Rule - public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); - 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, TestUtils.LOCATION, TestUtils.VOD_URI, - TestUtils.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", TestUtils.LOCATION, SESSION_ID); - bout.reset(); - } - - @Test - public void test_GetVodSession() throws IOException { - GetVodSession.getVodSession(PROJECT_ID, TestUtils.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 deleted file mode 100644 index 7bf52e05f47..00000000000 --- a/media/stitcher/src/test/java/com/example/stitcher/GetVodStitchDetailTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; -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.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class GetVodStitchDetailTest { - - @Rule - public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); - 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, TestUtils.LOCATION, TestUtils.VOD_URI, - TestUtils.VOD_AD_TAG_URI); - String output = bout.toString(); - String[] arr = output.split("/"); - SESSION_ID = arr[arr.length - 1].replace("\n", ""); - String sessionName = String.format("locations/%s/vodSessions/%s/", TestUtils.LOCATION, - SESSION_ID); - bout.reset(); - - ListVodStitchDetails.listVodStitchDetails(PROJECT_ID, TestUtils.LOCATION, SESSION_ID); - Matcher idMatcher = - Pattern.compile("name: \"projects/.*/" + sessionName + "vodStitchDetails/(.*)\"") - .matcher(bout.toString()); - if (idMatcher.find()) { - STITCH_DETAIL_ID = idMatcher.group(1); - STITCH_DETAIL_NAME = sessionName + "vodStitchDetails/" + STITCH_DETAIL_ID; - } - bout.reset(); - } - - @Test - public void test_GetVodStitchDetailTest() throws IOException { - GetVodStitchDetail.getVodStitchDetail(PROJECT_ID, TestUtils.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 deleted file mode 100644 index a7eb10d3c6c..00000000000 --- a/media/stitcher/src/test/java/com/example/stitcher/ListVodAdTagDetailsTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; -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.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class ListVodAdTagDetailsTest { - - @Rule - public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); - 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, TestUtils.LOCATION, TestUtils.VOD_URI, - TestUtils.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/", TestUtils.LOCATION, SESSION_ID); - bout.reset(); - } - - @Test - public void test_ListVodAdTagDetailsTest() throws IOException { - ListVodAdTagDetails.listVodAdTagDetails(PROJECT_ID, TestUtils.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 deleted file mode 100644 index 502b31f96f6..00000000000 --- a/media/stitcher/src/test/java/com/example/stitcher/ListVodStitchDetailsTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; -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.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class ListVodStitchDetailsTest { - - @Rule - public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); - 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, TestUtils.LOCATION, TestUtils.VOD_URI, - TestUtils.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/", TestUtils.LOCATION, SESSION_ID); - bout.reset(); - } - - @Test - public void test_ListVodStitchDetailsTest() throws IOException { - ListVodStitchDetails.listVodStitchDetails(PROJECT_ID, TestUtils.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(); - } -} diff --git a/media/stitcher/src/test/java/com/example/stitcher/TestUtils.java b/media/stitcher/src/test/java/com/example/stitcher/TestUtils.java index fc91d964733..ac66dbdbc72 100644 --- a/media/stitcher/src/test/java/com/example/stitcher/TestUtils.java +++ b/media/stitcher/src/test/java/com/example/stitcher/TestUtils.java @@ -20,10 +20,12 @@ import com.google.cloud.video.stitcher.v1.ListCdnKeysRequest; import com.google.cloud.video.stitcher.v1.ListLiveConfigsRequest; import com.google.cloud.video.stitcher.v1.ListSlatesRequest; +import com.google.cloud.video.stitcher.v1.ListVodConfigsRequest; import com.google.cloud.video.stitcher.v1.LiveConfig; import com.google.cloud.video.stitcher.v1.LocationName; import com.google.cloud.video.stitcher.v1.Slate; import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient; +import com.google.cloud.video.stitcher.v1.VodConfig; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -41,6 +43,7 @@ public class TestUtils { public static final String SLATE_ID_PREFIX = "slate-"; public static final String CDN_KEY_ID_PREFIX = "cdn-key-"; public static final String LIVE_CONFIG_ID_PREFIX = "live-config-"; + public static final String VOD_CONFIG_ID_PREFIX = "vod-config-"; public static final String HOSTNAME = "cdn.example.com"; public static final String UPDATED_HOSTNAME = "updated.example.com"; @@ -60,6 +63,8 @@ public class TestUtils { "https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator="; public static final String VOD_URI = "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.m3u8"; + public static final String UPDATED_VOD_URI = + "https://storage.googleapis.com/cloud-samples-data/media/hls-vod/manifest.mpd"; // VMAP Pre-roll // (https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/tags) public static final String VOD_AD_TAG_URI = @@ -142,7 +147,38 @@ public static void cleanStaleLiveConfigs(String projectId, String location) thro long createEpochSec = Long.parseLong(createTime); if (createEpochSec < Instant.now().getEpochSecond() - DELETION_THRESHOLD_TIME_HOURS_IN_SECONDS) { - videoStitcherServiceClient.deleteLiveConfigAsync(liveConfig.getName()) + videoStitcherServiceClient + .deleteLiveConfigAsync(liveConfig.getName()) + .get(2, TimeUnit.MINUTES); + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + // Clean up old test VOD configs. + public static void cleanStaleVodConfigs(String projectId, String location) throws IOException { + try (VideoStitcherServiceClient videoStitcherServiceClient = + VideoStitcherServiceClient.create()) { + ListVodConfigsRequest listVodConfigsRequest = + ListVodConfigsRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .build(); + + VideoStitcherServiceClient.ListVodConfigsPagedResponse response = + videoStitcherServiceClient.listVodConfigs(listVodConfigsRequest); + + for (VodConfig vodConfig : response.iterateAll()) { + Matcher matcher = Pattern.compile(VOD_CONFIG_ID_PREFIX).matcher(vodConfig.getName()); + if (matcher.find()) { + String createTime = vodConfig.getName().substring(matcher.end()).trim(); + long createEpochSec = Long.parseLong(createTime); + if (createEpochSec + < Instant.now().getEpochSecond() - DELETION_THRESHOLD_TIME_HOURS_IN_SECONDS) { + videoStitcherServiceClient + .deleteVodConfigAsync(vodConfig.getName()) .get(2, TimeUnit.MINUTES); } } @@ -218,4 +254,14 @@ public static String getLiveConfigId() { LIVE_CONFIG_ID_PREFIX, Instant.now().getEpochSecond()); } + + // Get a VOD config ID that includes a creation timestamp. Add some randomness in case tests are + // run in parallel. + public static String getVodConfigId() { + return String.format( + "test-%s-%s%s", + UUID.randomUUID().toString().substring(0, 15), + VOD_CONFIG_ID_PREFIX, + Instant.now().getEpochSecond()); + } } diff --git a/media/stitcher/src/test/java/com/example/stitcher/VodConfigTest.java b/media/stitcher/src/test/java/com/example/stitcher/VodConfigTest.java new file mode 100644 index 00000000000..b72b75d2494 --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/VodConfigTest.java @@ -0,0 +1,124 @@ +/* + * Copyright 2024 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient.ListVodConfigsPagedResponse; +import com.google.cloud.video.stitcher.v1.VodConfig; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class VodConfigTest { + + @Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); + private static final String VOD_CONFIG_ID = TestUtils.getVodConfigId(); + private static String VOD_CONFIG_NAME; + private static String PROJECT_ID; + private static PrintStream originalOut; + private static 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 beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + TestUtils.cleanStaleVodConfigs(PROJECT_ID, TestUtils.LOCATION); + + VOD_CONFIG_NAME = + String.format("locations/%s/vodConfigs/%s", TestUtils.LOCATION, VOD_CONFIG_ID); + VodConfig response = + CreateVodConfig.createVodConfig( + PROJECT_ID, + TestUtils.LOCATION, + VOD_CONFIG_ID, + TestUtils.VOD_URI, + TestUtils.VOD_AD_TAG_URI); + assertThat(response.getName(), containsString(VOD_CONFIG_NAME)); + } + + @Test + public void testGetVodConfig() throws IOException { + VodConfig response = GetVodConfig.getVodConfig(PROJECT_ID, TestUtils.LOCATION, VOD_CONFIG_ID); + assertThat(response.getName(), containsString(VOD_CONFIG_NAME)); + } + + @Test + public void testListVodConfigs() throws IOException { + ListVodConfigsPagedResponse response = + ListVodConfigs.listVodConfigs(PROJECT_ID, TestUtils.LOCATION); + Boolean pass = false; + for (VodConfig vodConfig : response.iterateAll()) { + if (vodConfig.getName().contains(VOD_CONFIG_NAME)) { + pass = true; + break; + } + } + assert (pass); + } + + @Test + public void testUpdateVodConfig() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + VodConfig response = + UpdateVodConfig.updateVodConfig( + PROJECT_ID, TestUtils.LOCATION, VOD_CONFIG_ID, TestUtils.UPDATED_VOD_URI); + assertThat(response.getName(), containsString(VOD_CONFIG_NAME)); + assertThat(response.getSourceUri(), containsString(TestUtils.UPDATED_VOD_URI)); + } + + @After + public void tearDown() { + bout.reset(); + } + + @AfterClass + public static void afterTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + DeleteVodConfig.deleteVodConfig(PROJECT_ID, TestUtils.LOCATION, VOD_CONFIG_ID); + String deleteResponse = bout.toString(); + assertThat(deleteResponse, containsString("Deleted VOD config")); + System.out.flush(); + System.setOut(originalOut); + } +} diff --git a/media/stitcher/src/test/java/com/example/stitcher/VodSessionTest.java b/media/stitcher/src/test/java/com/example/stitcher/VodSessionTest.java new file mode 100644 index 00000000000..fc1fd4bf82e --- /dev/null +++ b/media/stitcher/src/test/java/com/example/stitcher/VodSessionTest.java @@ -0,0 +1,183 @@ +/* + * Copyright 2024 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 com.google.cloud.testing.junit4.MultipleAttemptsRule; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient.ListVodAdTagDetailsPagedResponse; +import com.google.cloud.video.stitcher.v1.VideoStitcherServiceClient.ListVodStitchDetailsPagedResponse; +import com.google.cloud.video.stitcher.v1.VodAdTagDetail; +import com.google.cloud.video.stitcher.v1.VodConfig; +import com.google.cloud.video.stitcher.v1.VodSession; +import com.google.cloud.video.stitcher.v1.VodStitchDetail; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class VodSessionTest { + + @Rule public final MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(5); + private static final String VOD_CONFIG_ID = TestUtils.getVodConfigId(); + private static String VOD_CONFIG_NAME; + private static String VOD_SESSION_NAME_PREFIX; + private static String VOD_SESSION_NAME; + private static String SESSION_ID; + private static String AD_TAG_DETAIL_NAME; + private static String AD_TAG_DETAIL_ID; + private static String STITCH_DETAIL_NAME; + private static String STITCH_DETAIL_ID; + private static String PROJECT_ID; + private static PrintStream originalOut; + private static 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 beforeTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + TestUtils.cleanStaleVodConfigs(PROJECT_ID, TestUtils.LOCATION); + + VOD_CONFIG_NAME = + String.format("locations/%s/vodConfigs/%s", TestUtils.LOCATION, VOD_CONFIG_ID); + VodConfig configResponse = + CreateVodConfig.createVodConfig( + PROJECT_ID, + TestUtils.LOCATION, + VOD_CONFIG_ID, + TestUtils.VOD_URI, + TestUtils.VOD_AD_TAG_URI); + assertThat(configResponse.getName(), containsString(VOD_CONFIG_NAME)); + + // Session IDs are autogenerated. + VOD_SESSION_NAME_PREFIX = String.format("locations/%s/vodSessions/", TestUtils.LOCATION); + VodSession sessionResponse = + CreateVodSession.createVodSession(PROJECT_ID, TestUtils.LOCATION, VOD_CONFIG_ID); + assertThat(sessionResponse.getName(), containsString(VOD_SESSION_NAME_PREFIX)); + + VOD_SESSION_NAME = sessionResponse.getName(); + String[] id = VOD_SESSION_NAME.split("/"); + SESSION_ID = id[id.length - 1]; + + ListVodAdTagDetailsPagedResponse adtagResponse = + ListVodAdTagDetails.listVodAdTagDetails(PROJECT_ID, TestUtils.LOCATION, SESSION_ID); + for (VodAdTagDetail vodAdTagDetail : adtagResponse.iterateAll()) { + AD_TAG_DETAIL_NAME = vodAdTagDetail.getName(); + } + id = AD_TAG_DETAIL_NAME.split("/"); + AD_TAG_DETAIL_ID = id[id.length - 1]; + + ListVodStitchDetailsPagedResponse stitchResponse = + ListVodStitchDetails.listVodStitchDetails(PROJECT_ID, TestUtils.LOCATION, SESSION_ID); + for (VodStitchDetail vodStitchDetail : stitchResponse.iterateAll()) { + STITCH_DETAIL_NAME = vodStitchDetail.getName(); + } + id = STITCH_DETAIL_NAME.split("/"); + STITCH_DETAIL_ID = id[id.length - 1]; + } + + @Test + public void testGetVodSession() throws IOException { + VodSession response = GetVodSession.getVodSession(PROJECT_ID, TestUtils.LOCATION, SESSION_ID); + assertThat(response.getName(), containsString(VOD_SESSION_NAME)); + } + + @Test + public void testListVodAdTagDetailsTest() throws IOException { + ListVodAdTagDetailsPagedResponse response = + ListVodAdTagDetails.listVodAdTagDetails(PROJECT_ID, TestUtils.LOCATION, SESSION_ID); + + Boolean pass = false; + for (VodAdTagDetail vodAdTagDetail : response.iterateAll()) { + if (vodAdTagDetail.getName().contains(VOD_SESSION_NAME.concat("/vodAdTagDetails/"))) { + pass = true; + break; + } + } + assert (pass); + } + + @Test + public void testGetVodAdTagDetailTest() throws IOException { + VodAdTagDetail response = + GetVodAdTagDetail.getVodAdTagDetail( + PROJECT_ID, TestUtils.LOCATION, SESSION_ID, AD_TAG_DETAIL_ID); + assertThat(response.getName(), containsString(AD_TAG_DETAIL_NAME)); + } + + @Test + public void testListVodStitchDetailsTest() throws IOException { + ListVodStitchDetailsPagedResponse response = + ListVodStitchDetails.listVodStitchDetails(PROJECT_ID, TestUtils.LOCATION, SESSION_ID); + Boolean pass = false; + for (VodStitchDetail vodStitchDetail : response.iterateAll()) { + if (vodStitchDetail.getName().contains(VOD_SESSION_NAME.concat("/vodStitchDetails/"))) { + pass = true; + break; + } + } + assert (pass); + } + + @Test + public void testGetVodStitchDetailTest() throws IOException { + VodStitchDetail response = + GetVodStitchDetail.getVodStitchDetail( + PROJECT_ID, TestUtils.LOCATION, SESSION_ID, STITCH_DETAIL_ID); + assertThat(response.getName(), containsString(STITCH_DETAIL_NAME)); + } + + @After + public void tearDown() { + bout.reset(); + } + + @AfterClass + public static void afterTest() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // No delete method for VOD sessions + DeleteVodConfig.deleteVodConfig(PROJECT_ID, TestUtils.LOCATION, VOD_CONFIG_ID); + String deleteResponse = bout.toString(); + assertThat(deleteResponse, containsString("Deleted VOD config")); + System.out.flush(); + System.setOut(originalOut); + } +}