Skip to content

Commit

Permalink
Using ServiceOptions.getDefaultProjectId() for project-id (#560)
Browse files Browse the repository at this point in the history
* Using ServiceOptions.getDefaultProjectId() for project-id
  • Loading branch information
jabubake authored Mar 14, 2017
1 parent 9dd4313 commit 9cebf6c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
22 changes: 12 additions & 10 deletions pubsub/cloud-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,33 @@

[Google Cloud Pub/Sub][pubsub] is a fully-managed real-time messaging service that allows you to
send and receive messages between independent applications.
These sample Java applications demonstrate how to access the Pub/Sub API using
This sample Java application demonstrates how to access the Pub/Sub API using
the [Google Cloud Client Library for Java][google-cloud-java].

[pubsub]: https://cloud.google.com/pubsub/
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java

## Quickstart

Install [Maven](http://maven.apache.org/).
#### Setup
- Install [Maven](http://maven.apache.org/) <p>
- Install the [Google Cloud SDK](https://cloud.google.com/sdk/) and run :

Build your project with:

mvn clean package -DskipTests
gcloud config set project [YOUR PROJECT ID]

## Testing

To run the tests for this sample, first set the `GOOGLE_CLOUD_PROJECT`
environment variable.
- Build your project with:

export GOOGLE_CLOUD_PROJECT=my-project

Then run the tests with Maven.
mvn clean package -DskipTests

#### Testing

Run the tests with Maven.

mvn clean verify

### Creating a new topic (using the quickstart sample)
#### Creating a new topic (using the quickstart sample)

mvn exec:java -Dexec.mainClass=com.example.pubsub.QuickstartSample
2 changes: 1 addition & 1 deletion pubsub/cloud-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>0.9.2-alpha</version>
<version>0.9.4-alpha</version>
</dependency>

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
// [START pubsub_quickstart]
// Imports the Google Cloud client library

import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.spi.v1.PublisherClient;
import com.google.pubsub.v1.TopicName;

public class QuickstartSample {

public static void main(String... args) throws Exception {

// Your Google Cloud Platform project ID
String projectId = ServiceOptions.getDefaultProjectId();

// Your topic ID
String topicId = "my-new-topic";

// Create a new topic
String projectId = args[0];
TopicName topic = TopicName.create(projectId, "my-new-topic");
TopicName topic = TopicName.create(projectId, topicId);
try (PublisherClient publisherClient = PublisherClient.create()) {
publisherClient.createTopic(topic);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.ServiceOptions;
import com.google.cloud.pubsub.spi.v1.PublisherClient;
import com.google.pubsub.v1.TopicName;

Expand All @@ -40,11 +41,11 @@ public class QuickstartSampleIT {

private ByteArrayOutputStream bout;
private PrintStream out;
private String projectId;

private void deleteTestTopic(String projectId) throws Exception {
private void deleteTestTopic() throws Exception {
try (PublisherClient publisherClient = PublisherClient.create()) {
publisherClient.deleteTopic(TopicName.create(projectId, "my-new-topic"));
publisherClient.deleteTopic(
TopicName.create(ServiceOptions.getDefaultProjectId(), "my-new-topic"));
} catch (IOException e) {
System.err.println("Error deleting topic " + e.getMessage());
}
Expand All @@ -55,10 +56,8 @@ public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
assertThat(projectId).isNotNull();
try {
deleteTestTopic(projectId);
deleteTestTopic();
} catch (Exception e) {
//empty catch block
}
Expand All @@ -67,12 +66,12 @@ public void setUp() {
@After
public void tearDown() throws Exception {
System.setOut(null);
deleteTestTopic(projectId);
deleteTestTopic();
}

@Test
public void testQuickstart() throws Exception {
QuickstartSample.main(projectId);
QuickstartSample.main();
String got = bout.toString();
assertThat(got).contains("my-new-topic created.");
}
Expand Down

0 comments on commit 9cebf6c

Please sign in to comment.