@Override public String getName() { return completedSnapshot.getName(); }
@Override public ResponseT get() throws ExecutionException, InterruptedException { return (ResponseT) pollingFuture.get().getResponse(); }
/** Example to update the database DDL. */ // [TARGET updateDatabaseDdl(String, String, Iterable, String)] // [VARIABLE my_instance_id] // [VARIABLE my_database_id] public void updateDatabaseDdl(String instanceId, String databaseId) { // [START updateDatabaseDdl] try { dbAdminClient .updateDatabaseDdl( instanceId, databaseId, Arrays.asList("ALTER TABLE Albums ADD COLUMN MarketingBudget INT64"), null) .get(); } catch (ExecutionException e) { throw (SpannerException) e.getCause(); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e); } // [END updateDatabaseDdl] }
completedSnapshot.isDone(), "given snapshot must already be completed"); final ApiFuture<MetadataT> metadataFuture = ApiFutures.immediateFuture((MetadataT) completedSnapshot.getMetadata()); final ApiFuture<OperationSnapshot> initialFuture = ApiFutures.immediateFuture(completedSnapshot);
@Override public OperationFuture<Database, CreateDatabaseMetadata> createDatabase( String instanceId, String databaseId, Iterable<String> statements) throws SpannerException { // CreateDatabase() is not idempotent, so we're not retrying this request. String instanceName = getInstanceName(instanceId); String createStatement = "CREATE DATABASE `" + databaseId + "`"; OperationFuture<com.google.spanner.admin.database.v1.Database, CreateDatabaseMetadata> rawOperationFuture = rpc.createDatabase(instanceName, createStatement, statements); return new OperationFutureImpl<Database, CreateDatabaseMetadata>( rawOperationFuture.getPollingFuture(), rawOperationFuture.getInitialFuture(), new ApiFunction<OperationSnapshot, Database>() { @Override public Database apply(OperationSnapshot snapshot) { return Database.fromProto( ProtoOperationTransformers.ResponseTransformer.create( com.google.spanner.admin.database.v1.Database.class) .apply(snapshot), DatabaseAdminClientImpl.this); } }, ProtoOperationTransformers.MetadataTransformer.create(CreateDatabaseMetadata.class), new ApiFunction<Exception, Database>() { @Override public Database apply(Exception e) { throw SpannerExceptionFactory.newSpannerException(e); } }); }
private <ReqT, RespT, MetaT> void mockOperationResult( OperationCallable<ReqT, RespT, MetaT> callable, ReqT request, RespT response) { OperationSnapshot operationSnapshot = FakeOperationSnapshot.newBuilder() .setDone(true) .setErrorCode(GrpcStatusCode.of(Code.OK)) .setName("fake-name") .setResponse(response) .build(); OperationFuture<RespT, MetaT> operationFuture = OperationFutures.immediateOperationFuture(operationSnapshot); Mockito.when(callable.futureCall(request)).thenReturn(operationFuture); }
Database db; try { db = op.get(); } catch (ExecutionException e) { throw (SpannerException) e.getCause();
@Override public OperationFuture<Void, UpdateDatabaseDdlMetadata> updateDatabaseDdl( final String instanceId, final String databaseId, final Iterable<String> statements, @Nullable String operationId) throws SpannerException { final String dbName = getDatabaseName(instanceId, databaseId); final String opId = operationId != null ? operationId : randomOperationId(); OperationFuture<Empty, UpdateDatabaseDdlMetadata> rawOperationFuture = rpc.updateDatabaseDdl(dbName, statements, opId); return new OperationFutureImpl<Void, UpdateDatabaseDdlMetadata>( rawOperationFuture.getPollingFuture(), rawOperationFuture.getInitialFuture(), new ApiFunction<OperationSnapshot, Void>() { @Override public Void apply(OperationSnapshot snapshot) { ProtoOperationTransformers.ResponseTransformer.create(Empty.class).apply(snapshot); return null; } }, ProtoOperationTransformers.MetadataTransformer.create(UpdateDatabaseDdlMetadata.class), new ApiFunction<Exception, Void>() { @Override public Void apply(Exception e) { throw SpannerExceptionFactory.newSpannerException(e); } }); }
public static void executeNoCatch() throws Exception { try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { String inputUri = "gs://demomaker/cat.mp4"; Feature featuresElement = Feature.LABEL_DETECTION; List<Feature> features = Arrays.asList(featuresElement); AnnotateVideoResponse response = client.annotateVideoAsync(inputUri, features).get(); } }
rawOperationFuture = rpc.updateInstance(instance.toProto(), fieldMask); return new OperationFutureImpl<Instance, UpdateInstanceMetadata>( rawOperationFuture.getPollingFuture(), rawOperationFuture.getInitialFuture(), new ApiFunction<OperationSnapshot, Instance>() { @Override
public static void executeNoCatch() throws Exception { try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { String inputUri = "gs://demomaker/cat.mp4"; Feature featuresElement = Feature.LABEL_DETECTION; List<Feature> features = Arrays.asList(featuresElement); AnnotateVideoResponse response = client.annotateVideoAsync(inputUri, features).get(); } }
@Override public OperationFuture<Instance, CreateInstanceMetadata> createInstance(InstanceInfo instance) throws SpannerException { String projectName = PROJECT_NAME_TEMPLATE.instantiate("project", projectId); OperationFuture<com.google.spanner.admin.instance.v1.Instance, CreateInstanceMetadata> rawOperationFuture = rpc.createInstance(projectName, instance.getId().getInstance(), instance.toProto()); return new OperationFutureImpl<Instance, CreateInstanceMetadata>( rawOperationFuture.getPollingFuture(), rawOperationFuture.getInitialFuture(), new ApiFunction<OperationSnapshot, Instance>() { @Override public Instance apply(OperationSnapshot snapshot) { return Instance.fromProto( ProtoOperationTransformers.ResponseTransformer.create( com.google.spanner.admin.instance.v1.Instance.class) .apply(snapshot), InstanceAdminClientImpl.this, dbClient); } }, ProtoOperationTransformers.MetadataTransformer.create(CreateInstanceMetadata.class), new ApiFunction<Exception, Instance>() { @Override public Instance apply(Exception e) { throw SpannerExceptionFactory.newSpannerException(e); } }); }
public static void executeNoCatch() throws Exception { try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { String inputUri = "gs://demomaker/cat.mp4"; Feature featuresElement = Feature.LABEL_DETECTION; List<Feature> features = Arrays.asList(featuresElement); AnnotateVideoResponse response = client.annotateVideoAsync(inputUri, features).get(); } }
/** Example to create an instance. */ public void createInstance( final String my_instance_id, final String my_config_id, final String my_client_project) { // [START instance_admin_client_create_instance] final String instanceId = my_instance_id; final String configId = my_config_id; final String clientProject = my_client_project; OperationFuture<Instance, CreateInstanceMetadata> op = instanceAdminClient.createInstance( InstanceInfo.newBuilder(InstanceId.of(clientProject, instanceId)) .setInstanceConfigId(InstanceConfigId.of(clientProject, configId)) .setDisplayName(instanceId) .setNodeCount(1) .build()); try { op.get(); } catch (ExecutionException e) { throw (SpannerException) e.getCause(); } catch (InterruptedException e) { throw SpannerExceptionFactory.propagateInterrupt(e); } // [END instance_admin_client_create_instance] }
@Test @SuppressWarnings("all") public void deleteClusterExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockClusterController.addException(exception); try { String projectId = "projectId-1969970175"; String region = "region-934795532"; String clusterName = "clusterName-1018081872"; client.deleteClusterAsync(projectId, region, clusterName).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test @SuppressWarnings("all") public void deleteClusterExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockClusterController.addException(exception); try { String projectId = "projectId-1969970175"; String region = "region-934795532"; String clusterName = "clusterName-1018081872"; client.deleteClusterAsync(projectId, region, clusterName).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test @SuppressWarnings("all") public void trainAgentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockAgents.addException(exception); try { ProjectName parent = ProjectName.of("[PROJECT]"); client.trainAgentAsync(parent).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test @SuppressWarnings("all") public void trainAgentExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockAgents.addException(exception); try { ProjectName parent = ProjectName.of("[PROJECT]"); client.trainAgentAsync(parent).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test @SuppressWarnings("all") public void runAssetDiscoveryExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockSecurityCenter.addException(exception); try { OrganizationName parent = OrganizationName.of("[ORGANIZATION]"); client.runAssetDiscoveryAsync(parent).get(); Assert.fail("No exception raised"); } catch (ExecutionException e) { Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); } }
@Test public void longrunningRecognize() throws Exception { LongRunningRecognizeResponse response = speechClient.longRunningRecognizeAsync(config(), audio()).get(); Truth.assertThat(response.getResultsCount()).isGreaterThan(0); Truth.assertThat(response.getResults(0).getAlternativesCount()).isGreaterThan(0); String text = response.getResults(0).getAlternatives(0).getTranscript(); Truth.assertThat(text).isEqualTo("hello"); }