|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.livestream; |
| 18 | + |
| 19 | +import static org.hamcrest.CoreMatchers.containsString; |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | + |
| 23 | +import com.google.api.gax.rpc.NotFoundException; |
| 24 | +import java.io.ByteArrayOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.PrintStream; |
| 27 | +import java.util.UUID; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | +import java.util.concurrent.TimeoutException; |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.BeforeClass; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.JUnit4; |
| 36 | + |
| 37 | +@RunWith(JUnit4.class) |
| 38 | +public class CreateChannelEventTest { |
| 39 | + |
| 40 | + private static final String LOCATION = "us-central1"; |
| 41 | + private static String PROJECT_ID; |
| 42 | + private static final String CHANNEL_ID = |
| 43 | + "my-channel-" + UUID.randomUUID().toString().substring(0, 25); |
| 44 | + private static final String INPUT_ID = |
| 45 | + "my-input-" + UUID.randomUUID().toString().substring(0, 25); |
| 46 | + private static final String EVENT_ID = |
| 47 | + "my-channel-event-" + UUID.randomUUID().toString().substring(0, 25); |
| 48 | + private static String EVENT_NAME; |
| 49 | + private static final String OUTPUT_URI = "gs://my-bucket/my-output-folder/"; |
| 50 | + |
| 51 | + private static PrintStream originalOut; |
| 52 | + private ByteArrayOutputStream bout; |
| 53 | + |
| 54 | + private static String requireEnvVar(String varName) { |
| 55 | + String varValue = System.getenv(varName); |
| 56 | + assertNotNull( |
| 57 | + String.format("Environment variable '%s' is required to perform these tests.", varName)); |
| 58 | + return varValue; |
| 59 | + } |
| 60 | + |
| 61 | + @BeforeClass |
| 62 | + public static void checkRequirements() { |
| 63 | + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); |
| 64 | + PROJECT_ID = requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 65 | + } |
| 66 | + |
| 67 | + @Before |
| 68 | + public void beforeTest() |
| 69 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 70 | + // Clean up old resources in the test project. |
| 71 | + TestUtils.cleanAllStale(PROJECT_ID, LOCATION); |
| 72 | + |
| 73 | + originalOut = System.out; |
| 74 | + bout = new ByteArrayOutputStream(); |
| 75 | + System.setOut(new PrintStream(bout)); |
| 76 | + |
| 77 | + EVENT_NAME = |
| 78 | + String.format( |
| 79 | + "projects/%s/locations/%s/channels/%s/events/%s", |
| 80 | + PROJECT_ID, LOCATION, CHANNEL_ID, EVENT_ID); |
| 81 | + try { |
| 82 | + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); |
| 83 | + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { |
| 84 | + // Don't worry if the channel doesn't already exist. |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); |
| 89 | + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { |
| 90 | + // Don't worry if the input doesn't already exist. |
| 91 | + } |
| 92 | + |
| 93 | + CreateInput.createInput(PROJECT_ID, LOCATION, INPUT_ID); |
| 94 | + CreateChannel.createChannel(PROJECT_ID, LOCATION, CHANNEL_ID, INPUT_ID, OUTPUT_URI); |
| 95 | + StartChannel.startChannel(PROJECT_ID, LOCATION, CHANNEL_ID); |
| 96 | + |
| 97 | + bout.reset(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void test_CreateChannelEvent() throws Exception { |
| 102 | + CreateChannelEvent.createChannelEvent(PROJECT_ID, LOCATION, CHANNEL_ID, EVENT_ID); |
| 103 | + String output = bout.toString(); |
| 104 | + assertThat(output, containsString(EVENT_NAME)); |
| 105 | + bout.reset(); |
| 106 | + } |
| 107 | + |
| 108 | + @After |
| 109 | + public void tearDown() |
| 110 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 111 | + |
| 112 | + StopChannel.stopChannel(PROJECT_ID, LOCATION, CHANNEL_ID); |
| 113 | + DeleteChannelEvent.deleteChannelEvent(PROJECT_ID, LOCATION, CHANNEL_ID, EVENT_ID); |
| 114 | + try { |
| 115 | + DeleteChannel.deleteChannel(PROJECT_ID, LOCATION, CHANNEL_ID); |
| 116 | + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { |
| 117 | + System.out.printf(String.valueOf(e)); |
| 118 | + } |
| 119 | + |
| 120 | + try { |
| 121 | + DeleteInput.deleteInput(PROJECT_ID, LOCATION, INPUT_ID); |
| 122 | + } catch (NotFoundException | InterruptedException | ExecutionException | TimeoutException e) { |
| 123 | + System.out.printf(String.valueOf(e)); |
| 124 | + } |
| 125 | + System.setOut(originalOut); |
| 126 | + bout.reset(); |
| 127 | + } |
| 128 | +} |
0 commit comments