Search in sources :

Example 81 with Instance

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());
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) Instance(com.google.spanner.admin.instance.v1.Instance) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 82 with Instance

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.
    }
}
Also used : InstanceName(com.google.spanner.admin.instance.v1.InstanceName) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 83 with Instance

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());
}
Also used : Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) InstanceInfo(com.google.cloud.spanner.InstanceInfo) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Example 84 with Instance

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());
}
Also used : Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) InstanceInfo(com.google.cloud.spanner.InstanceInfo) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Database(com.google.cloud.spanner.Database) DatabaseAdminClient(com.google.cloud.spanner.DatabaseAdminClient) ExecutionException(java.util.concurrent.ExecutionException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Example 85 with Instance

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();
}
Also used : Random(java.util.Random) Instance(com.google.cloud.spanner.Instance) UpdateInstanceMetadata(com.google.spanner.admin.instance.v1.UpdateInstanceMetadata) Test(org.junit.Test) SerialIntegrationTest(com.google.cloud.spanner.SerialIntegrationTest)

Aggregations

Test (org.junit.Test)50 AbstractMessage (com.google.protobuf.AbstractMessage)19 ByteString (com.google.protobuf.ByteString)17 ExecutionException (java.util.concurrent.ExecutionException)16 Instance (com.google.cloud.spanner.Instance)15 StatusRuntimeException (io.grpc.StatusRuntimeException)14 Instance (com.google.cloud.compute.v1.Instance)11 InstancesClient (com.google.cloud.compute.v1.InstancesClient)11 CreateInstanceMetadata (com.google.spanner.admin.instance.v1.CreateInstanceMetadata)11 Instance (com.google.spanner.admin.instance.v1.Instance)11 Instance (com.google.bigtable.admin.v2.Instance)10 InstanceAdminClient (com.google.cloud.spanner.InstanceAdminClient)10 InstanceInfo (com.google.cloud.spanner.InstanceInfo)10 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)9 Instance (com.google.cloud.notebooks.v1beta1.Instance)9 Spanner (com.google.cloud.spanner.Spanner)8 ArrayList (java.util.ArrayList)8 Operation (com.google.cloud.compute.v1.Operation)7 FieldMask (com.google.protobuf.FieldMask)7 InstanceName (com.google.spanner.admin.instance.v1.InstanceName)7