use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class InstanceAdminClientTest method createInstanceExceptionTest2.
@Test
public void createInstanceExceptionTest2() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockInstanceAdmin.addException(exception);
try {
String parent = "parent-995424086";
String instanceId = "instanceId902024336";
Instance instance = Instance.newBuilder().build();
client.createInstanceAsync(parent, instanceId, instance).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());
}
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class InstanceAdminClientTest method getInstanceExceptionTest.
@Test
public void getInstanceExceptionTest() throws Exception {
StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
mockInstanceAdmin.addException(exception);
try {
InstanceName name = InstanceName.of("[PROJECT]", "[INSTANCE]");
client.getInstance(name);
Assert.fail("No exception raised");
} catch (InvalidArgumentException e) {
// Expected exception.
}
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_bothSucceed.
@Test
public void testCreateInstanceAndDatabase_bothSucceed() 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()).thenReturn(mock(Database.class));
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
// Verify that both the instance and the database was created.
verify(instanceClient).createInstance(InstanceInfo.newBuilder(InstanceId.of("test-project", "test-instance")).setDisplayName("Automatically Generated Test Instance").setInstanceConfigId(InstanceConfigId.of("test-project", "emulator-config")).setNodeCount(1).build());
verify(databaseClient).createDatabase("test-instance", "test-database", ImmutableList.of());
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_bothFailWithAlreadyExists.
@Test
public void testCreateInstanceAndDatabase_bothFailWithAlreadyExists() 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 ExecutionException(SpannerExceptionFactory.newSpannerException(ErrorCode.ALREADY_EXISTS, "Instance already exists")));
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.ALREADY_EXISTS, "Database already exists")));
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
// Verify that both the instance and the database was created.
verify(instanceClient).createInstance(InstanceInfo.newBuilder(InstanceId.of("test-project", "test-instance")).setDisplayName("Automatically Generated Test Instance").setInstanceConfigId(InstanceConfigId.of("test-project", "emulator-config")).setNodeCount(1).build());
verify(databaseClient).createDatabase("test-instance", "test-database", ImmutableList.of());
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class ITInstanceAdminTest method updateInstanceViaEntity.
@Test
public void updateInstanceViaEntity() throws Exception {
assumeFalse("The emulator does not support updating instances", isUsingEmulator());
Instance instance = instanceClient.getInstance(env.getTestHelper().getInstanceId().getInstance());
String rand = new Random().nextInt() + "";
String newDisplayName = "instance test" + rand;
Instance toUpdate = instance.toBuilder().setDisplayName(newDisplayName).setNodeCount(instance.getNodeCount() + 1).build();
// Only update display name
OperationFuture<Instance, UpdateInstanceMetadata> op = toUpdate.update(InstanceInfo.InstanceField.DISPLAY_NAME);
Instance newInstance = op.get();
assertThat(newInstance.getNodeCount()).isEqualTo(instance.getNodeCount());
assertThat(newInstance.getDisplayName()).isEqualTo(newDisplayName);
Instance newInstanceFromGet = instance.reload();
assertThat(newInstanceFromGet).isEqualTo(newInstance);
toUpdate = newInstance.toBuilder().setDisplayName(instance.getDisplayName()).build();
toUpdate.update(InstanceInfo.InstanceField.DISPLAY_NAME).get();
}
Aggregations