diff --git a/media/livestream/pom.xml b/media/livestream/pom.xml new file mode 100644 index 00000000000..4916e6439cc --- /dev/null +++ b/media/livestream/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + com.example + livestream + 1.0-SNAPSHOT + jar + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + 11 + 11 + + + + + + com.google.cloud + libraries-bom + 25.2.0 + pom + import + + + + + + com.google.cloud + google-cloud-live-stream + 0.3.0 + + + com.google.cloud + google-cloud-core + compile + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + + junit + junit + 4.13.2 + test + + + \ No newline at end of file diff --git a/media/livestream/src/main/java/com/example/livestream/CreateInput.java b/media/livestream/src/main/java/com/example/livestream/CreateInput.java new file mode 100644 index 00000000000..e0c821cf9f8 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/CreateInput.java @@ -0,0 +1,63 @@ +/* + * 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.livestream; + +// [START livestream_create_input] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.video.livestream.v1.CreateInputRequest; +import com.google.cloud.video.livestream.v1.Input; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.LocationName; +import com.google.cloud.video.livestream.v1.OperationMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class CreateInput { + + 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 inputId = "my-input-id"; + + createInput(projectId, location, inputId); + } + + public static void createInput(String projectId, String location, String inputId) + throws InterruptedException, ExecutionException, TimeoutException, 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 (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + var createInputRequest = + CreateInputRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .setInputId(inputId) + .setInput(Input.newBuilder().setType(Input.Type.RTMP_PUSH).build()) + .build(); + + OperationFuture call = + livestreamServiceClient.createInputAsync(createInputRequest); + Input result = call.get(1, TimeUnit.MINUTES); + System.out.println("Input: " + result.getName()); + } + } +} +// [END livestream_create_input] diff --git a/media/livestream/src/main/java/com/example/livestream/DeleteInput.java b/media/livestream/src/main/java/com/example/livestream/DeleteInput.java new file mode 100644 index 00000000000..803bd571686 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/DeleteInput.java @@ -0,0 +1,56 @@ +/* + * 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.livestream; + +// [START livestream_delete_input] + +import com.google.cloud.video.livestream.v1.DeleteInputRequest; +import com.google.cloud.video.livestream.v1.InputName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class DeleteInput { + + 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 inputId = "my-input-id"; + + deleteInput(projectId, location, inputId); + } + + public static void deleteInput(String projectId, String location, String inputId) + throws InterruptedException, ExecutionException, TimeoutException, 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 (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + var deleteInputRequest = + DeleteInputRequest.newBuilder() + .setName(InputName.of(projectId, location, inputId).toString()) + .build(); + + livestreamServiceClient.deleteInputAsync(deleteInputRequest).get(1, TimeUnit.MINUTES); + System.out.println("Deleted input"); + } + } +} +// [END livestream_delete_input] diff --git a/media/livestream/src/main/java/com/example/livestream/GetInput.java b/media/livestream/src/main/java/com/example/livestream/GetInput.java new file mode 100644 index 00000000000..063060b5396 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/GetInput.java @@ -0,0 +1,49 @@ +/* + * 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.livestream; + +// [START livestream_get_input] + +import com.google.cloud.video.livestream.v1.Input; +import com.google.cloud.video.livestream.v1.InputName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import java.io.IOException; + +public class GetInput { + + 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 inputId = "my-input-id"; + + getInput(projectId, location, inputId); + } + + public static void getInput(String projectId, String location, String inputId) + 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 (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + InputName name = InputName.of(projectId, location, inputId); + Input response = livestreamServiceClient.getInput(name); + System.out.println("Input: " + response.getName()); + } + } +} +// [END livestream_get_input] diff --git a/media/livestream/src/main/java/com/example/livestream/ListInputs.java b/media/livestream/src/main/java/com/example/livestream/ListInputs.java new file mode 100644 index 00000000000..5ede37384e8 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/ListInputs.java @@ -0,0 +1,57 @@ +/* + * 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.livestream; + +// [START livestream_list_inputs] + +import com.google.cloud.video.livestream.v1.Input; +import com.google.cloud.video.livestream.v1.ListInputsRequest; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.LocationName; +import java.io.IOException; + +public class ListInputs { + + 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"; + + listInputs(projectId, location); + } + + public static void listInputs(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. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + var listInputsRequest = + ListInputsRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .build(); + + LivestreamServiceClient.ListInputsPagedResponse response = + livestreamServiceClient.listInputs(listInputsRequest); + System.out.println("Inputs:"); + + for (Input input : response.iterateAll()) { + System.out.println(input.getName()); + } + } + } +} +// [END livestream_list_inputs] diff --git a/media/livestream/src/main/java/com/example/livestream/UpdateInput.java b/media/livestream/src/main/java/com/example/livestream/UpdateInput.java new file mode 100644 index 00000000000..a718c8a3595 --- /dev/null +++ b/media/livestream/src/main/java/com/example/livestream/UpdateInput.java @@ -0,0 +1,72 @@ +/* + * 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.livestream; + +// [START livestream_update_input] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.video.livestream.v1.Input; +import com.google.cloud.video.livestream.v1.InputName; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.OperationMetadata; +import com.google.cloud.video.livestream.v1.PreprocessingConfig; +import com.google.cloud.video.livestream.v1.PreprocessingConfig.Crop; +import com.google.cloud.video.livestream.v1.UpdateInputRequest; +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 UpdateInput { + + 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 inputId = "my-input-id"; + + updateInput(projectId, location, inputId); + } + + public static void updateInput(String projectId, String location, String inputId) + throws InterruptedException, ExecutionException, TimeoutException, 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 (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + var updateInputRequest = + UpdateInputRequest.newBuilder() + .setInput( + Input.newBuilder() + .setName(InputName.of(projectId, location, inputId).toString()) + .setPreprocessingConfig( + PreprocessingConfig.newBuilder() + .setCrop(Crop.newBuilder().setTopPixels(5).setBottomPixels(5).build()) + .build()) + .build()) + .setUpdateMask(FieldMask.newBuilder().addPaths("preprocessing_config").build()) + .build(); + + OperationFuture call = + livestreamServiceClient.updateInputAsync(updateInputRequest); + Input result = call.get(1, TimeUnit.MINUTES); + System.out.println("Updated input: " + result.getName()); + } + } +} +// [END livestream_update_input] diff --git a/media/livestream/src/test/java/com/example/livestream/CreateInputTest.java b/media/livestream/src/test/java/com/example/livestream/CreateInputTest.java new file mode 100644 index 00000000000..3e90dfb11dc --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/CreateInputTest.java @@ -0,0 +1,97 @@ +/* + * 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.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +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 CreateInputTest { + + private static final String LOCATION = "us-central1"; + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + + private static String PROJECT_ID; + private static String INPUT_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 { + // Clean up old inputs in the test project. + TestUtils.cleanStaleInputs(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + INPUT_NAME = + String.format("projects/%s/locations/%s/inputs/%s", PROJECT_ID, LOCATION, INPUT_ID); + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + bout.reset(); + } + + @Test + public void test_CreateInput() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + String output = bout.toString(); + assertThat(output, containsString(INPUT_NAME)); + bout.reset(); + } + + @After + public void tearDown() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/DeleteInputTest.java b/media/livestream/src/test/java/com/example/livestream/DeleteInputTest.java new file mode 100644 index 00000000000..0642f7ab7d0 --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/DeleteInputTest.java @@ -0,0 +1,94 @@ +/* + * 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.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +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 DeleteInputTest { + + private static final String LOCATION = "us-central1"; + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + + private static String PROJECT_ID; + 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, ExecutionException, InterruptedException, TimeoutException { + // Clean up old inputs in the test project. + TestUtils.cleanStaleInputs(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + bout.reset(); + } + + @Test + public void test_DeleteInput() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + String output = bout.toString(); + assertThat(output, containsString("Deleted input")); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/GetInputTest.java b/media/livestream/src/test/java/com/example/livestream/GetInputTest.java new file mode 100644 index 00000000000..8f02bb46d83 --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/GetInputTest.java @@ -0,0 +1,100 @@ +/* + * 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.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +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 GetInputTest { + + private static final String LOCATION = "us-central1"; + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static String PROJECT_ID; + private static String INPUT_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, ExecutionException, InterruptedException, TimeoutException { + // Clean up old inputs in the test project. + TestUtils.cleanStaleInputs(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + INPUT_NAME = + String.format("projects/%s/locations/%s/inputs/%s", PROJECT_ID, LOCATION, INPUT_ID); + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + bout.reset(); + } + + @Test + public void test_GetInput() throws IOException { + GetInput.getInput(PROJECT_ID, LOCATION, INPUT_ID); + String output = bout.toString(); + assertThat(output, containsString(INPUT_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/ListInputsTest.java b/media/livestream/src/test/java/com/example/livestream/ListInputsTest.java new file mode 100644 index 00000000000..f7256c1c32f --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/ListInputsTest.java @@ -0,0 +1,100 @@ +/* + * 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.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +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 ListInputsTest { + + private static final String LOCATION = "us-central1"; + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static String PROJECT_ID; + private static String INPUT_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, ExecutionException, InterruptedException, TimeoutException { + // Clean up old inputs in the test project. + TestUtils.cleanStaleInputs(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + INPUT_NAME = + String.format("projects/%s/locations/%s/inputs/%s", PROJECT_ID, LOCATION, INPUT_ID); + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + bout.reset(); + } + + @Test + public void test_ListInputs() throws Exception { + ListInputs.listInputs(PROJECT_ID, LOCATION); + String output = bout.toString(); + assertThat(output, containsString(INPUT_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +} diff --git a/media/livestream/src/test/java/com/example/livestream/TestUtils.java b/media/livestream/src/test/java/com/example/livestream/TestUtils.java new file mode 100644 index 00000000000..5d01ac28df5 --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/TestUtils.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.livestream; + +import com.google.api.gax.rpc.NotFoundException; +import com.google.cloud.video.livestream.v1.DeleteInputRequest; +import com.google.cloud.video.livestream.v1.Input; +import com.google.cloud.video.livestream.v1.ListInputsRequest; +import com.google.cloud.video.livestream.v1.LivestreamServiceClient; +import com.google.cloud.video.livestream.v1.LocationName; +import java.io.IOException; +import java.time.Instant; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class TestUtils { + + private static final int DELETION_THRESHOLD_TIME_HOURS_IN_SECONDS = 10800; // 3 hours + + public static void cleanStaleInputs(String projectId, String location) { + try (LivestreamServiceClient livestreamServiceClient = LivestreamServiceClient.create()) { + var listInputsRequest = + ListInputsRequest.newBuilder() + .setParent(LocationName.of(projectId, location).toString()) + .build(); + + LivestreamServiceClient.ListInputsPagedResponse response = + livestreamServiceClient.listInputs(listInputsRequest); + + for (Input input : response.iterateAll()) { + if (input.getCreateTime().getSeconds() + < Instant.now().getEpochSecond() - DELETION_THRESHOLD_TIME_HOURS_IN_SECONDS) { + var deleteInputRequest = + DeleteInputRequest.newBuilder() + .setName(input.getName()) + .build(); + livestreamServiceClient.deleteInputAsync(deleteInputRequest).get(1, TimeUnit.MINUTES); + } + } + } catch (IOException e) { + e.printStackTrace(); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/media/livestream/src/test/java/com/example/livestream/UpdateInputTest.java b/media/livestream/src/test/java/com/example/livestream/UpdateInputTest.java new file mode 100644 index 00000000000..65c64c65ce3 --- /dev/null +++ b/media/livestream/src/test/java/com/example/livestream/UpdateInputTest.java @@ -0,0 +1,101 @@ +/* + * 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.livestream; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +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 UpdateInputTest { + + private static final String LOCATION = "us-central1"; + private static final String INPUT_ID = + "my-input-" + UUID.randomUUID().toString().substring(0, 25); + private static String PROJECT_ID; + private static String INPUT_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, ExecutionException, InterruptedException, TimeoutException { + // Clean up old inputs in the test project. + TestUtils.cleanStaleInputs(PROJECT_ID, LOCATION); + + originalOut = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + + INPUT_NAME = + String.format("projects/%s/locations/%s/inputs/%s", PROJECT_ID, LOCATION, INPUT_ID); + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + // Don't worry if the input doesn't already exist. + } + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); + bout.reset(); + } + + @Test + public void test_UpdateInput() + throws IOException, ExecutionException, InterruptedException, TimeoutException { + UpdateInput.updateInput(PROJECT_ID, LOCATION, INPUT_ID); + String output = bout.toString(); + assertThat(output, containsString(INPUT_NAME)); + bout.reset(); + } + + @After + public void tearDown() throws IOException { + try { + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { + System.out.printf(String.valueOf(e)); + } + System.setOut(originalOut); + bout.reset(); + } +}