Search in sources :

Example 71 with Database

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

the class MockDatabaseAdminServiceImplTest method dropDatabase.

@Test
public void dropDatabase() {
    createTestDb();
    Database db = client.getDatabase(TEST_DB_NAME);
    assertThat(db.getName()).isEqualTo(TEST_DB_NAME);
    client.dropDatabase(TEST_DB_NAME);
    exception.expect(ApiExceptionMatcher.forCode(StatusCode.Code.NOT_FOUND));
    client.getDatabase(TEST_DB_NAME);
}
Also used : Database(com.google.spanner.admin.database.v1.Database) Test(org.junit.Test)

Example 72 with Database

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

the class MockDatabaseAdminServiceImplTest method createTestDb.

private Database createTestDb() {
    CreateDatabaseRequest request = CreateDatabaseRequest.newBuilder().setCreateStatement("CREATE DATABASE `test-db`").addAllExtraStatements(Arrays.asList("CREATE TABLE FOO", "CREATE TABLE BAR")).setParent(TEST_PARENT).build();
    OperationFuture<Database, CreateDatabaseMetadata> op = client.createDatabaseOperationCallable().futureCall(request);
    try {
        return op.get();
    } catch (ExecutionException e) {
        if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
            throw (RuntimeException) e.getCause();
        }
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new CancelledException(e, FakeStatusCode.of(Code.CANCELLED), false);
    }
}
Also used : CreateDatabaseRequest(com.google.spanner.admin.database.v1.CreateDatabaseRequest) CancelledException(com.google.api.gax.rpc.CancelledException) Database(com.google.spanner.admin.database.v1.Database) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 73 with Database

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

the class MockDatabaseAdminServiceImplTest method createDatabase.

@Test
public void createDatabase() {
    Database db = createTestDb();
    assertThat(db.getName()).isEqualTo(TEST_DB_NAME);
}
Also used : Database(com.google.spanner.admin.database.v1.Database) Test(org.junit.Test)

Example 74 with Database

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

the class DatabaseAdminClientTest method restoreDatabaseTest4.

@Test
public void restoreDatabaseTest4() 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();
    Operation resultOperation = Operation.newBuilder().setName("restoreDatabaseTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
    mockDatabaseAdmin.addResponse(resultOperation);
    String parent = "parent-995424086";
    String databaseId = "databaseId1688905718";
    String backup = "backup-1396673086";
    Database actualResponse = client.restoreDatabaseAsync(parent, databaseId, backup).get();
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    RestoreDatabaseRequest actualRequest = ((RestoreDatabaseRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertEquals(databaseId, actualRequest.getDatabaseId());
    Assert.assertEquals(backup, actualRequest.getBackup());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) RestoreDatabaseRequest(com.google.spanner.admin.database.v1.RestoreDatabaseRequest) Database(com.google.spanner.admin.database.v1.Database) ArrayList(java.util.ArrayList) Operation(com.google.longrunning.Operation) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 75 with Database

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

the class DatabaseAdminClientTest method getDatabaseExceptionTest.

@Test
public void getDatabaseExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockDatabaseAdmin.addException(exception);
    try {
        DatabaseName name = DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]");
        client.getDatabase(name);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) DatabaseName(com.google.spanner.admin.database.v1.DatabaseName) 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