Search in sources :

Example 1 with SchemaName

use of com.google.pubsub.v1.SchemaName in project java-pubsub by googleapis.

the class SchemaServiceClient method deleteSchema.

// AUTO-GENERATED DOCUMENTATION AND METHOD.
/**
 * Deletes a schema.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
 *   SchemaName name = SchemaName.of("[PROJECT]", "[SCHEMA]");
 *   schemaServiceClient.deleteSchema(name);
 * }
 * }</pre>
 *
 * @param name Required. Name of the schema to delete. Format is
 *     `projects/{project}/schemas/{schema}`.
 * @throws com.google.api.gax.rpc.ApiException if the remote call fails
 */
public final void deleteSchema(SchemaName name) {
    DeleteSchemaRequest request = DeleteSchemaRequest.newBuilder().setName(name == null ? null : name.toString()).build();
    deleteSchema(request);
}
Also used : DeleteSchemaRequest(com.google.pubsub.v1.DeleteSchemaRequest)

Example 2 with SchemaName

use of com.google.pubsub.v1.SchemaName in project java-pubsub by googleapis.

the class SchemaServiceClientTest method getSchemaExceptionTest.

@Test
public void getSchemaExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSchemaService.addException(exception);
    try {
        SchemaName name = SchemaName.of("[PROJECT]", "[SCHEMA]");
        client.getSchema(name);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) SchemaName(com.google.pubsub.v1.SchemaName) Test(org.junit.Test)

Example 3 with SchemaName

use of com.google.pubsub.v1.SchemaName in project java-pubsub by googleapis.

the class SchemaServiceClientTest method getSchemaTest.

@Test
public void getSchemaTest() throws Exception {
    Schema expectedResponse = Schema.newBuilder().setName(SchemaName.of("[PROJECT]", "[SCHEMA]").toString()).setDefinition("definition-1014418093").build();
    mockSchemaService.addResponse(expectedResponse);
    SchemaName name = SchemaName.of("[PROJECT]", "[SCHEMA]");
    Schema actualResponse = client.getSchema(name);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSchemaService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    GetSchemaRequest actualRequest = ((GetSchemaRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Schema(com.google.pubsub.v1.Schema) GetSchemaRequest(com.google.pubsub.v1.GetSchemaRequest) SchemaName(com.google.pubsub.v1.SchemaName) Test(org.junit.Test)

Example 4 with SchemaName

use of com.google.pubsub.v1.SchemaName in project java-pubsub by googleapis.

the class SchemaServiceClientTest method deleteSchemaExceptionTest.

@Test
public void deleteSchemaExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSchemaService.addException(exception);
    try {
        SchemaName name = SchemaName.of("[PROJECT]", "[SCHEMA]");
        client.deleteSchema(name);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) SchemaName(com.google.pubsub.v1.SchemaName) Test(org.junit.Test)

Example 5 with SchemaName

use of com.google.pubsub.v1.SchemaName in project java-pubsub by googleapis.

the class CreateAvroSchemaExample method createAvroSchemaExample.

public static void createAvroSchemaExample(String projectId, String schemaId, String avscFile) throws IOException {
    ProjectName projectName = ProjectName.of(projectId);
    SchemaName schemaName = SchemaName.of(projectId, schemaId);
    // Read an Avro schema file formatted in JSON as a string.
    String avscSource = new String(Files.readAllBytes(Paths.get(avscFile)));
    try (SchemaServiceClient schemaServiceClient = SchemaServiceClient.create()) {
        Schema schema = schemaServiceClient.createSchema(projectName, Schema.newBuilder().setName(schemaName.toString()).setType(Schema.Type.AVRO).setDefinition(avscSource).build(), schemaId);
        System.out.println("Created a schema using an Avro schema:\n" + schema);
    } catch (AlreadyExistsException e) {
        System.out.println(schemaName + "already exists.");
    }
}
Also used : AlreadyExistsException(com.google.api.gax.rpc.AlreadyExistsException) ProjectName(com.google.pubsub.v1.ProjectName) Schema(com.google.pubsub.v1.Schema) SchemaServiceClient(com.google.cloud.pubsub.v1.SchemaServiceClient) SchemaName(com.google.pubsub.v1.SchemaName)

Aggregations

SchemaName (com.google.pubsub.v1.SchemaName)9 SchemaServiceClient (com.google.cloud.pubsub.v1.SchemaServiceClient)4 Schema (com.google.pubsub.v1.Schema)4 Test (org.junit.Test)4 AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)3 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)2 NotFoundException (com.google.api.gax.rpc.NotFoundException)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 DeleteSchemaRequest (com.google.pubsub.v1.DeleteSchemaRequest)2 ProjectName (com.google.pubsub.v1.ProjectName)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 TopicAdminClient (com.google.cloud.pubsub.v1.TopicAdminClient)1 Empty (com.google.protobuf.Empty)1 GetSchemaRequest (com.google.pubsub.v1.GetSchemaRequest)1 SchemaSettings (com.google.pubsub.v1.SchemaSettings)1 Topic (com.google.pubsub.v1.Topic)1 TopicName (com.google.pubsub.v1.TopicName)1