|
| 1 | +/* |
| 2 | + * Copyright 2019 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.asset; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.cloud.ServiceOptions; |
| 22 | +import com.google.cloud.pubsub.v1.TopicAdminClient; |
| 23 | +import com.google.cloud.resourcemanager.ProjectInfo; |
| 24 | +import com.google.cloud.resourcemanager.ResourceManager; |
| 25 | +import com.google.cloud.resourcemanager.ResourceManagerOptions; |
| 26 | +import com.google.pubsub.v1.ProjectTopicName; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.PrintStream; |
| 29 | +import java.util.UUID; |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.AfterClass; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.FixMethodOrder; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.junit.runners.JUnit4; |
| 38 | +import org.junit.runners.MethodSorters; |
| 39 | + |
| 40 | +/** Tests for real time feed sample. */ |
| 41 | +@RunWith(JUnit4.class) |
| 42 | +@SuppressWarnings("checkstyle:abbreviationaswordinname") |
| 43 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 44 | +public class RealTimeFeed { |
| 45 | + private static final String topicId = "topicId"; |
| 46 | + private static final String feedId = UUID.randomUUID().toString(); |
| 47 | + private static final String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 48 | + private final String projectNumber = getProjectNumber(projectId); |
| 49 | + private final String feedName = String.format("projects/%s/feeds/%s", projectNumber, feedId); |
| 50 | + private final String[] assetNames = {UUID.randomUUID().toString()}; |
| 51 | + private static final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId); |
| 52 | + private ByteArrayOutputStream bout; |
| 53 | + |
| 54 | + private String getProjectNumber(String projectId) { |
| 55 | + ResourceManager resourceManager = ResourceManagerOptions.getDefaultInstance().getService(); |
| 56 | + ProjectInfo project = resourceManager.get(projectId); |
| 57 | + return Long.toString(project.getProjectNumber()); |
| 58 | + } |
| 59 | + |
| 60 | + @BeforeClass |
| 61 | + public static void createTopic() throws Exception { |
| 62 | + TopicAdminClient topicAdminClient = TopicAdminClient.create(); |
| 63 | + topicAdminClient.createTopic(topicName); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterClass |
| 67 | + public static void deleteTopic() throws Exception { |
| 68 | + TopicAdminClient topicAdminClient = TopicAdminClient.create(); |
| 69 | + topicAdminClient.deleteTopic(topicName); |
| 70 | + } |
| 71 | + |
| 72 | + @Before |
| 73 | + public void beforeTest() { |
| 74 | + bout = new ByteArrayOutputStream(); |
| 75 | + System.setOut(new PrintStream(bout)); |
| 76 | + } |
| 77 | + |
| 78 | + @After |
| 79 | + public void tearDown() { |
| 80 | + System.setOut(null); |
| 81 | + bout.reset(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void test1CreateFeedExample() throws Exception { |
| 86 | + CreateFeedExample.createFeed(assetNames, feedId, topicName.toString(), projectId); |
| 87 | + String got = bout.toString(); |
| 88 | + assertThat(got).contains("Feed created successfully: " + feedName); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void test2GetFeedExample() throws Exception { |
| 93 | + GetFeedExample.getFeed(feedName); |
| 94 | + String got = bout.toString(); |
| 95 | + assertThat(got).contains("Get a feed: " + feedName); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void test3ListFeedsExample() throws Exception { |
| 100 | + ListFeedsExample.listFeeds(projectId); |
| 101 | + String got = bout.toString(); |
| 102 | + assertThat(got).contains("Listed feeds under: " + projectId); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void test4UpdateFeedExample() throws Exception { |
| 107 | + UpdateFeedExample.updateFeed(feedName, topicName.toString()); |
| 108 | + String got = bout.toString(); |
| 109 | + assertThat(got).contains("Feed updated successfully: " + feedName); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void test5DeleteFeedExample() throws Exception { |
| 114 | + DeleteFeedExample.deleteFeed(feedName); |
| 115 | + String got = bout.toString(); |
| 116 | + assertThat(got).contains("Feed deleted"); |
| 117 | + } |
| 118 | +} |
0 commit comments