Skip to content

Commit 51df4d4

Browse files
committed
create new topic if needed
1 parent 7e052cf commit 51df4d4

File tree

1 file changed

+10
-0
lines changed
  • container-registry/container-analysis/src/test/java/com/example/containeranalysis

1 file changed

+10
-0
lines changed

container-registry/container-analysis/src/test/java/com/example/containeranalysis/SamplesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import com.google.cloud.devtools.containeranalysis.v1.ContainerAnalysisClient;
2424
import com.google.cloud.pubsub.v1.Subscriber;
2525
import com.google.cloud.pubsub.v1.SubscriptionAdminClient;
26+
import com.google.cloud.pubsub.v1.TopicAdminClient;
2627
import com.google.pubsub.v1.ProjectSubscriptionName;
28+
import com.google.pubsub.v1.ProjectTopicName;
2729
import io.grafeas.v1.DiscoveryNote;
2830
import io.grafeas.v1.DiscoveryOccurrence;
2931
import io.grafeas.v1.DiscoveryOccurrence.AnalysisStatus;
@@ -202,6 +204,14 @@ public void testOccurrencesForNote() throws Exception {
202204
public void testPubSub() throws Exception {
203205
int newCount;
204206
int tries;
207+
// create new topic and subscription if needed
208+
try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
209+
String topicId = "container-analysis-occurrences-v1";
210+
ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId);
211+
topicAdminClient.createTopic(topicName);
212+
} catch (StatusRuntimeException e) {
213+
System.out.println("Topic already exists");
214+
}
205215
ProjectSubscriptionName subName = ProjectSubscriptionName.of(PROJECT_ID, subId);
206216
try {
207217
Subscriptions.createOccurrenceSubscription(subId, PROJECT_ID);

0 commit comments

Comments
 (0)