use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class InstanceAdminClientTest method deleteInstanceTest.
@Test
public void deleteInstanceTest() throws Exception {
Empty expectedResponse = Empty.newBuilder().build();
mockInstanceAdmin.addResponse(expectedResponse);
InstanceName name = InstanceName.of("[PROJECT]", "[INSTANCE]");
client.deleteInstance(name);
List<AbstractMessage> actualRequests = mockInstanceAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
DeleteInstanceRequest actualRequest = ((DeleteInstanceRequest) actualRequests.get(0));
Assert.assertEquals(name.toString(), actualRequest.getName());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class InstanceAdminClientTest method listInstancesTest.
@Test
public void listInstancesTest() throws Exception {
Instance responsesElement = Instance.newBuilder().build();
ListInstancesResponse expectedResponse = ListInstancesResponse.newBuilder().setNextPageToken("").addAllInstances(Arrays.asList(responsesElement)).build();
mockInstanceAdmin.addResponse(expectedResponse);
ProjectName parent = ProjectName.of("[PROJECT]");
ListInstancesPagedResponse pagedListResponse = client.listInstances(parent);
List<Instance> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getInstancesList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockInstanceAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListInstancesRequest actualRequest = ((ListInstancesRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesInterruptsOnDatabaseCreation.
@Test
public void testCreateInstanceAndDatabase_propagatesInterruptsOnDatabaseCreation() throws InterruptedException, ExecutionException {
Spanner spanner = mock(Spanner.class);
SpannerOptions options = mock(SpannerOptions.class);
when(spanner.getOptions()).thenReturn(options);
when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
when(instanceOperationFuture.get()).thenReturn(mock(Instance.class));
DatabaseAdminClient databaseClient = mock(DatabaseAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Database, CreateDatabaseMetadata> databaseOperationFuture = mock(OperationFuture.class);
when(spanner.getDatabaseAdminClient()).thenReturn(databaseClient);
when(databaseClient.createDatabase(eq("test-instance"), eq("test-database"), eq(ImmutableList.of()))).thenReturn(databaseOperationFuture);
when(databaseOperationFuture.get()).thenThrow(new InterruptedException());
try {
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.CANCELLED, e.getErrorCode());
}
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesInterruptsOnInstanceCreation.
@Test
public void testCreateInstanceAndDatabase_propagatesInterruptsOnInstanceCreation() throws InterruptedException, ExecutionException {
Spanner spanner = mock(Spanner.class);
SpannerOptions options = mock(SpannerOptions.class);
when(spanner.getOptions()).thenReturn(options);
when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
when(instanceOperationFuture.get()).thenThrow(new InterruptedException());
try {
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.CANCELLED, e.getErrorCode());
}
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesOtherErrorsOnDatabaseCreation.
@Test
public void testCreateInstanceAndDatabase_propagatesOtherErrorsOnDatabaseCreation() throws InterruptedException, ExecutionException {
Spanner spanner = mock(Spanner.class);
SpannerOptions options = mock(SpannerOptions.class);
when(spanner.getOptions()).thenReturn(options);
when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
when(instanceOperationFuture.get()).thenReturn(mock(Instance.class));
DatabaseAdminClient databaseClient = mock(DatabaseAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Database, CreateDatabaseMetadata> databaseOperationFuture = mock(OperationFuture.class);
when(spanner.getDatabaseAdminClient()).thenReturn(databaseClient);
when(databaseClient.createDatabase(eq("test-instance"), eq("test-database"), eq(ImmutableList.of()))).thenReturn(databaseOperationFuture);
when(databaseOperationFuture.get()).thenThrow(new ExecutionException(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid database options")));
try {
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.INVALID_ARGUMENT, e.getErrorCode());
}
}
Aggregations