Java Code Examples for com.google.cloud.pubsub.v1.TopicAdminClient#create()

The following examples show how to use com.google.cloud.pubsub.v1.TopicAdminClient#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateListDevices() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));
  Assert.assertTrue(got.contains("Found"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 2
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateGetDevice() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));
  Assert.assertTrue(got.contains("Retrieving device"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 3
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteRsaDevice() throws Exception {
  final String deviceName = "rsa-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDeviceStates(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 4
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateDeleteEsDevice() throws Exception {
  final String deviceName = "es-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithEs256(
      deviceName, ES_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.getDeviceStates(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Created device: {"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 5
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Creates a topic and grants the IoT service account access. */
protected static Topic createIotTopic(String projectId, String topicId) throws Exception {
  // Create a new topic
  final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    final Topic topic = topicAdminClient.createTopic(topicName);
    final String topicString = topicName.toString();
    // add role -> members binding
    // create updated policy
    topicAdminClient.setIamPolicy(
        topicString,
        com.google.iam.v1.Policy.newBuilder(topicAdminClient.getIamPolicy(topicString))
            .addBindings(
                Binding.newBuilder()
                    .addMembers("serviceAccount:[email protected]")
                    .setRole(Role.owner().toString())
                    .build())
            .build());

    System.out.println("Setup topic / policy for: " + topic.getName());
    return topic;
  }
}
 
Example 6
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateConfigureDevice() throws Exception {
  final String deviceName = "rsa-device-config";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.setDeviceConfiguration(
      deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID, "some-test-data", 0L);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("Updated: 2"));

  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 7
Source File: Connection.java    From heroic with Apache License 2.0 5 votes vote down vote up
void createTopic() throws IOException {
    log.info("Creating topic {}", topicName);
    TopicAdminClient topicAdminClient = TopicAdminClient.create(
        TopicAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build()
    );
    try {
        topicAdminClient.createTopic(topicName);
    } catch (AlreadyExistsException e) {
        log.info("Topic already exists");
    }
}
 
Example 8
Source File: InspectTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  // Create a new topic
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.createTopic(topicName);
  }

  // Create a new subscription
  try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
    subscriptionAdminClient
        .createSubscription(subscriptionName, topicName, PushConfig.getDefaultInstance(), 0);
  }
}
 
Example 9
Source File: PubSubAdmin.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
/**
 * This constructor instantiates TopicAdminClient and SubscriptionAdminClient with all their
 * defaults and the provided credentials provider.
 * @param projectIdProvider the project id provider to use
 * @param credentialsProvider the credentials provider to use
 * @throws IOException thrown when there are errors in contacting Google Cloud Pub/Sub
 */
public PubSubAdmin(GcpProjectIdProvider projectIdProvider,
		CredentialsProvider credentialsProvider) throws IOException {
	this(projectIdProvider,
			TopicAdminClient.create(
					TopicAdminSettings.newBuilder()
							.setCredentialsProvider(credentialsProvider)
							.build()),
			SubscriptionAdminClient.create(
					SubscriptionAdminSettings.newBuilder()
					.setCredentialsProvider(credentialsProvider)
					.build()));
}
 
Example 10
Source File: UsePubSubEmulatorSnippet.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
  // [START pubsub_use_emulator]
  String hostport = System.getenv("PUBSUB_EMULATOR_HOST");
  ManagedChannel channel = ManagedChannelBuilder.forTarget(hostport).usePlaintext().build();
  try {
    TransportChannelProvider channelProvider =
        FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
    CredentialsProvider credentialsProvider = NoCredentialsProvider.create();

    // Set the channel and credentials provider when creating a `TopicAdminClient`.
    // Similarly for SubscriptionAdminClient
    TopicAdminClient topicClient =
        TopicAdminClient.create(
            TopicAdminSettings.newBuilder()
                .setTransportChannelProvider(channelProvider)
                .setCredentialsProvider(credentialsProvider)
                .build());

    TopicName topicName = TopicName.of("my-project-id", "my-topic-id");
    // Set the channel and credentials provider when creating a `Publisher`.
    // Similarly for Subscriber
    Publisher publisher =
        Publisher.newBuilder(topicName)
            .setChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build();
  } finally {
    channel.shutdown();
  }
  // [END pubsub_use_emulator]
}
 
Example 11
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public TopicAdminClient getTopicAdminClient() throws IOException {
	if (topicClient == null) {
		TopicAdminSettings topicAdminSettings = TopicAdminSettings.newBuilder()
			.setTransportChannelProvider(channelProvider)
			.setCredentialsProvider(NoCredentialsProvider.create())
			.build();
		topicClient = TopicAdminClient.create(topicAdminSettings);
	}
	return topicClient;
}
 
Example 12
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpDeviceEvent() throws Exception {
  final String deviceName = "rsa-device-http-event";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-num_messages=1",
    "-message_type=event",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.HttpExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("200"));
  Assert.assertTrue(got.contains("OK"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 13
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testMqttDeviceEvents() throws Exception {
  final String deviceName = "rsa-device-mqtt-events";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.listDevices(PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-message_type=events",
    "-num_messages=1",
    "-algorithm=RS256"
  };
  com.example.cloud.iot.examples.MqttExample.main(testArgs);
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  //
  // Finished loop successfully. Goodbye!

  Assert.assertTrue(got.contains("Publishing events message 1"));
  Assert.assertTrue(got.contains("Finished loop successfully. Goodbye!"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 14
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp()  {

    String projectId = ServiceOptions.getDefaultProjectId();
	try {

		//export GRPC_PROXY_EXP=localhost:3128
		HttpHost proxy = new HttpHost("127.0.0.1",3128);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
					
		httpClient.addRequestInterceptor(new HttpRequestInterceptor(){            
			@Override
			public void process(org.apache.http.HttpRequest request, HttpContext context) throws HttpException, IOException {
					//if (request.getRequestLine().getMethod().equals("CONNECT"))                 
					//   request.addHeader(new BasicHeader("Proxy-Authorization","Basic dXNlcjE6dXNlcjE="));
				}
			});
		
		mHttpTransport =  new ApacheHttpTransport(httpClient);		

		HttpTransportFactory hf = new HttpTransportFactory(){
			@Override
			public HttpTransport create() {
				return mHttpTransport;
			}
		};            
		
		credential = GoogleCredentials.getApplicationDefault(hf);

		CredentialsProvider credentialsProvider =  new GoogleCredentialsProvider(){
			public List<String> getScopesToApply(){
				return Arrays.asList("https://www.googleapis.com/auth/pubsub");
			   }

			public Credentials getCredentials()  {
				return credential;
			}
		};

		TopicAdminSettings topicAdminSettings =
		     TopicAdminSettings.newBuilder().setCredentialsProvider(credentialsProvider)
				 .build();
				 
		 TopicAdminClient topicAdminClient =
		     TopicAdminClient.create(topicAdminSettings);
		
		//TopicAdminClient topicAdminClient = TopicAdminClient.create();
		ProjectName project = ProjectName.create(projectId);
		for (Topic element : topicAdminClient.listTopics(project).iterateAll()) 
	  		System.out.println(element.getName());
	
	} catch (Exception ex) 
	{
		System.out.println("ERROR " + ex);
	}
  }
 
Example 15
Source File: TestApp.java    From gcpsamples with Apache License 2.0 4 votes vote down vote up
public TestApp() {
	try
	{
		/*
		// For GoogleAPIs
		HttpTransport httpTransport = new NetHttpTransport();             
		JacksonFactory jsonFactory = new JacksonFactory();
		//ComputeCredential credential = new ComputeCredential.Builder(httpTransport, jsonFactory).build();	
		GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);				            
		if (credential.createScopedRequired())
		    credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));           				            
		Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
		            .setApplicationName("oauth client")   
		            .build();				            
		Userinfoplus ui = service.userinfo().get().execute();
		System.out.println(ui.getEmail());
		*/

         // Using Google Cloud APIs
	  Storage storage_service = StorageOptions.newBuilder()
		.build()
		.getService();	
	  for (Bucket b : storage_service.list().iterateAll()){
		  System.out.println(b);
	  }

         // String cred_file = "/path/to/cred.json";
	  //GoogleCredentials creds = GoogleCredentials.fromStream(new FileInputStream(cred_file));	
	  GoogleCredentials creds = GoogleCredentials.getApplicationDefault();	  	  
	  FixedCredentialsProvider credentialsProvider = FixedCredentialsProvider.create(creds);
	  
	  ///ManagedChannel channel = ManagedChannelBuilder.forTarget("pubsub.googleapis.com:443").build();
         //TransportChannelProvider channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));	
	  
	  TransportChannelProvider channelProvider = TopicAdminSettings.defaultTransportChannelProvider();

	  TopicAdminClient topicClient =
		  TopicAdminClient.create(
			  TopicAdminSettings.newBuilder()
				  .setTransportChannelProvider(channelProvider)
				  .setCredentialsProvider(credentialsProvider)
				  .build());

	  ListTopicsRequest listTopicsRequest =
						ListTopicsRequest.newBuilder()
							.setProject(ProjectName.format("your_project"))
							.build();
	  ListTopicsPagedResponse response = topicClient.listTopics(listTopicsRequest);
	  Iterable<Topic> topics = response.iterateAll();
	  for (Topic topic : topics) 
		 System.out.println(topic);
	 		  

	} 
	catch (Exception ex) {
		System.out.println("Error:  " + ex);
	}
}
 
Example 16
Source File: RealTimeFeed.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@AfterClass
public static void deleteTopic() throws Exception {
  TopicAdminClient topicAdminClient = TopicAdminClient.create();
  topicAdminClient.deleteTopic(topicName);
}
 
Example 17
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Ignore
@Test
public void testErrorTopic() throws Exception {
  final String gatewayName = "rsa-listen-gateway-test";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createGateway(
      PROJECT_ID, CLOUD_REGION, REGISTRY_ID, gatewayName, RSA_PATH, "RS256");
  MqttClient client =
      MqttExample.startMqtt(
          "mqtt.googleapis.com",
          (short) 443,
          PROJECT_ID,
          CLOUD_REGION,
          REGISTRY_ID,
          gatewayName,
          PKCS_PATH,
          "RS256");

  Thread deviceThread =
      new Thread() {
        public void run() {
          try {
            MqttExample.attachDeviceToGateway(client, "garbage-device");
            MqttExample.attachCallback(client, "garbage-device");
          } catch (Exception e) {
            // TODO: Fail
            StringBuilder builder = new StringBuilder();
            builder.append("Failure on exception: ").append(e);
            System.out.println(builder);
          }
        }
      };

  deviceThread.start();
  Thread.sleep(4000);

  String got = bout.toString(StandardCharsets.UTF_8.name());
  Assert.assertTrue(got.contains("error_type"));

  // Clean up
  DeviceRegistryExample.deleteDevice(gatewayName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 18
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Ignore
@Test
public void testGatewayListenForDevice() throws Exception {
  final String gatewayName = "rsa-listen-gateway";
  final String deviceName = "rsa-listen-device";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createGateway(
      PROJECT_ID, CLOUD_REGION, REGISTRY_ID, gatewayName, RSA_PATH, "RS256");
  DeviceRegistryExample.createDevice(PROJECT_ID, CLOUD_REGION, REGISTRY_ID, deviceName);
  DeviceRegistryExample.bindDeviceToGateway(
      PROJECT_ID, CLOUD_REGION, REGISTRY_ID, deviceName, gatewayName);

  Thread deviceThread =
      new Thread() {
        public void run() {
          try {
            MqttExample.listenForConfigMessages(
                "mqtt.googleapis.com",
                (short) 443,
                PROJECT_ID,
                CLOUD_REGION,
                REGISTRY_ID,
                gatewayName,
                PKCS_PATH,
                "RS256",
                deviceName);
          } catch (Exception e) {
            // TODO: Fail
            System.out.println("Failure on Exception");
          }
        }
      };
  deviceThread.start();
  Thread.sleep(3000); // Give the device a chance to connect / receive configurations
  deviceThread.join();

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  System.out.println(got);
  Assert.assertTrue(got.contains("Payload"));

  // Clean up
  DeviceRegistryExample.unbindDeviceFromGateway(
      PROJECT_ID, CLOUD_REGION, REGISTRY_ID, deviceName, gatewayName);
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteDevice(gatewayName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}
 
Example 19
Source File: RealTimeFeed.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void createTopic() throws Exception {
  TopicAdminClient topicAdminClient = TopicAdminClient.create();
  topicAdminClient.createTopic(topicName);
}
 
Example 20
Source File: ManagerIT.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Ignore
@Test
public void testMqttDeviceCommand() throws Exception {
  final String deviceName = "rsa-device-mqtt-commands";
  topic = DeviceRegistryExample.createIotTopic(PROJECT_ID, TOPIC_ID);
  DeviceRegistryExample.createRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID, TOPIC_ID);
  DeviceRegistryExample.createDeviceWithRs256(
      deviceName, RSA_PATH, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);

  // Device bootstrapped, time to connect and run.
  String[] testArgs = {
    "-project_id=" + PROJECT_ID,
    "-registry_id=" + REGISTRY_ID,
    "-cloud_region=" + CLOUD_REGION,
    "-device_id=" + deviceName,
    "-private_key_file=" + PKCS_PATH,
    "-wait_time=" + 10,
    "-algorithm=RS256"
  };

  Thread deviceThread =
      new Thread() {
        public void run() {
          try {
            com.example.cloud.iot.examples.MqttExample.main(testArgs);
          } catch (Exception e) {
            // TODO: Fail
            System.out.println("Failure on Exception");
          }
        }
      };
  deviceThread.start();

  Thread.sleep(500); // Give the device a chance to connect
  com.example.cloud.iot.examples.DeviceRegistryExample.sendCommand(
      deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID, "me want cookie!");

  deviceThread.join();
  // End device test.

  // Assertions
  String got = bout.toString(StandardCharsets.UTF_8.name());
  System.out.println(got);
  Assert.assertTrue(got.contains("Finished loop successfully."));
  Assert.assertTrue(got.contains("me want cookie"));
  Assert.assertFalse(got.contains("Failure on Exception"));

  // Clean up
  DeviceRegistryExample.deleteDevice(deviceName, PROJECT_ID, CLOUD_REGION, REGISTRY_ID);
  DeviceRegistryExample.deleteRegistry(CLOUD_REGION, PROJECT_ID, REGISTRY_ID);
  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    topicAdminClient.deleteTopic(ProjectTopicName.of(PROJECT_ID, TOPIC_ID));
  }
}