Search in sources :

Example 31 with Instance

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()));
}
Also used : InstanceName(com.google.spanner.admin.instance.v1.InstanceName) Empty(com.google.protobuf.Empty) AbstractMessage(com.google.protobuf.AbstractMessage) DeleteInstanceRequest(com.google.spanner.admin.instance.v1.DeleteInstanceRequest) Test(org.junit.Test)

Example 32 with Instance

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()));
}
Also used : ListInstancesResponse(com.google.spanner.admin.instance.v1.ListInstancesResponse) AbstractMessage(com.google.protobuf.AbstractMessage) Instance(com.google.spanner.admin.instance.v1.Instance) ProjectName(com.google.spanner.admin.instance.v1.ProjectName) ListInstancesRequest(com.google.spanner.admin.instance.v1.ListInstancesRequest) ListInstancesPagedResponse(com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient.ListInstancesPagedResponse) Test(org.junit.Test)

Example 33 with Instance

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());
    }
}
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) SpannerException(com.google.cloud.spanner.SpannerException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Example 34 with Instance

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

Example 35 with Instance

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());
    }
}
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) SpannerException(com.google.cloud.spanner.SpannerException) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

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