Search in sources :

Example 86 with Database

use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.

the class DatabaseAdminClientTest method getDatabaseTest2.

@Test
public void getDatabaseTest2() throws Exception {
    Database expectedResponse = Database.newBuilder().setName(DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]").toString()).setCreateTime(Timestamp.newBuilder().build()).setRestoreInfo(RestoreInfo.newBuilder().build()).setEncryptionConfig(EncryptionConfig.newBuilder().build()).addAllEncryptionInfo(new ArrayList<EncryptionInfo>()).setVersionRetentionPeriod("versionRetentionPeriod-629783929").setEarliestVersionTime(Timestamp.newBuilder().build()).setDefaultLeader("defaultLeader759009962").setDatabaseDialect(DatabaseDialect.forNumber(0)).build();
    mockDatabaseAdmin.addResponse(expectedResponse);
    String name = "name3373707";
    Database actualResponse = client.getDatabase(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetDatabaseRequest actualRequest = ((GetDatabaseRequest) actualRequests.get(0));
    Assert.assertEquals(name, actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : GetDatabaseRequest(com.google.spanner.admin.database.v1.GetDatabaseRequest) AbstractMessage(com.google.protobuf.AbstractMessage) Database(com.google.spanner.admin.database.v1.Database) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 87 with Database

use of com.google.spanner.admin.database.v1.Database 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 88 with Database

use of com.google.spanner.admin.database.v1.Database 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 89 with Database

use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.

the class ITDatabaseAdminTest method updateDdlRetry.

@Test
public void updateDdlRetry() throws Exception {
    String dbId = testHelper.getUniqueDatabaseId();
    String instanceId = testHelper.getInstanceId().getInstance();
    String statement1 = "CREATE TABLE T (\n" + "  K STRING(MAX),\n" + ") PRIMARY KEY(K)";
    OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
    Database db = op.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
    dbs.add(db);
    String statement2 = "CREATE TABLE T2 (\n" + "  K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
    OperationFuture<Void, UpdateDatabaseDdlMetadata> op1 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
    OperationFuture<Void, UpdateDatabaseDdlMetadata> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
    op1.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
    op2.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
    // Remove the progress list from the metadata before comparing, as there could be small
    // differences between the two in the reported progress depending on exactly when each
    // operation was fetched from the backend.
    UpdateDatabaseDdlMetadata metadata1 = op1.getMetadata().get().toBuilder().clearProgress().build();
    UpdateDatabaseDdlMetadata metadata2 = op2.getMetadata().get().toBuilder().clearProgress().build();
    assertThat(metadata1).isEqualTo(metadata2);
}
Also used : UpdateDatabaseDdlMetadata(com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata) Database(com.google.cloud.spanner.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 90 with Database

use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.

the class ITDatabaseTest method databaseDeletedTest.

@Test
public void databaseDeletedTest() throws Exception {
    // Create a test db, do a query, then delete it and verify that it returns
    // DatabaseNotFoundExceptions.
    Database db = env.getTestHelper().createTestDatabase();
    DatabaseClient client = env.getTestHelper().getClient().getDatabaseClient(db.getId());
    try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
        assertThat(rs.next()).isTrue();
        assertThat(rs.getLong(0)).isEqualTo(1L);
        assertThat(rs.next()).isFalse();
    }
    // Delete the database.
    db.drop();
    // We need to wait a little before Spanner actually starts sending DatabaseNotFound errors.
    ExponentialBackOff backoff = new ExponentialBackOff.Builder().setInitialIntervalMillis(1000).setMaxElapsedTimeMillis(65000).setMaxIntervalMillis(5000).build();
    DatabaseNotFoundException notFoundException = null;
    long millis = 0L;
    while ((millis = backoff.nextBackOffMillis()) != ExponentialBackOff.STOP) {
        Thread.sleep(millis);
        // Queries to this database should eventually return DatabaseNotFoundExceptions.
        try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
            rs.next();
        } catch (DatabaseNotFoundException e) {
            // This is what we expect.
            notFoundException = e;
            break;
        }
    }
    assertThat(notFoundException).isNotNull();
    // Now re-create a database with the same name.
    OperationFuture<Database, CreateDatabaseMetadata> op = env.getTestHelper().getClient().getDatabaseAdminClient().createDatabase(db.getId().getInstanceId().getInstance(), db.getId().getDatabase(), Collections.emptyList());
    Database newDb = op.get();
    // Queries using the same DatabaseClient should still return DatabaseNotFoundExceptions.
    try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
        rs.next();
        fail("Missing expected DatabaseNotFoundException");
    } catch (DatabaseNotFoundException e) {
    // This is what we expect.
    }
    // Now get a new DatabaseClient for the database. This should now result in a valid
    // DatabaseClient.
    DatabaseClient newClient = env.getTestHelper().getClient().getDatabaseClient(newDb.getId());
    try (ResultSet rs = newClient.singleUse().executeQuery(Statement.of("SELECT 1"))) {
        assertThat(rs.next()).isTrue();
        assertThat(rs.getLong(0)).isEqualTo(1L);
        assertThat(rs.next()).isFalse();
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) DatabaseNotFoundException(com.google.cloud.spanner.DatabaseNotFoundException) Database(com.google.cloud.spanner.Database) ResultSet(com.google.cloud.spanner.ResultSet) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)53 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)31 Database (com.google.cloud.spanner.Database)29 AbstractMessage (com.google.protobuf.AbstractMessage)24 ArrayList (java.util.ArrayList)24 ExecutionException (java.util.concurrent.ExecutionException)22 Database (com.google.spanner.admin.database.v1.Database)21 Operation (com.google.longrunning.Operation)18 ByteString (com.google.protobuf.ByteString)16 DatabaseAdminClient (com.google.cloud.spanner.DatabaseAdminClient)12 SpannerException (com.google.cloud.spanner.SpannerException)12 Database (org.molgenis.emx2.Database)12 CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)11 DatabaseName (com.google.spanner.admin.database.v1.DatabaseName)10 RestoreDatabaseMetadata (com.google.spanner.admin.database.v1.RestoreDatabaseMetadata)9 Timestamp (com.google.cloud.Timestamp)8 Spanner (com.google.cloud.spanner.Spanner)8 RestoreDatabaseRequest (com.google.spanner.admin.database.v1.RestoreDatabaseRequest)7 StatusRuntimeException (io.grpc.StatusRuntimeException)7 Backup (com.google.cloud.spanner.Backup)6