Search in sources :

Example 61 with Schema

use of com.google.pubsub.v1.Schema in project DataflowTemplates by GoogleCloudPlatform.

the class PubSubChangeconsumerTest method testBasicRecordAndFilteredRecordInput.

@Test
public void testBasicRecordAndFilteredRecordInput() throws InterruptedException {
    DataCatalogSchemaManager dataCatalogMock = Mockito.mock(DataCatalogSchemaManager.class);
    Publisher pubsubMock = Mockito.mock(Publisher.class);
    Mockito.when(dataCatalogMock.updateSchemaForTable(Mockito.anyString(), Mockito.any(org.apache.beam.sdk.schemas.Schema.class))).thenReturn(Entry.newBuilder().build());
    Mockito.when(pubsubMock.publish(Mockito.any())).thenReturn(Mockito.mock(ApiFuture.class));
    PubSubChangeConsumer changeConsumer = new PubSubChangeConsumer(Sets.newHashSet("mainstance.cdcForDataflow.team_metadata", "table2"), dataCatalogMock, (input1, input2) -> pubsubMock);
    Schema keySchema = SchemaBuilder.struct().field("team", Schema.STRING_SCHEMA).build();
    Struct key = new Struct(keySchema).put("team", "team_PXHU");
    Schema valueAfterSchema = SchemaBuilder.struct().field("team", Schema.STRING_SCHEMA).field("city", Schema.STRING_SCHEMA).field("country", Schema.STRING_SCHEMA).field("year_founded", Schema.INT32_SCHEMA).field("some_timestamp", Schema.INT64_SCHEMA).build();
    Schema valueSchema = SchemaBuilder.struct().field("after", valueAfterSchema).field("op", Schema.STRING_SCHEMA).field("ts_ms", Schema.INT64_SCHEMA).build();
    Struct value = new Struct(valueSchema).put("op", "c").put("ts_ms", 1569287580660L).put("after", new Struct(valueAfterSchema).put("team", "team_PXHU").put("city", "Mexico").put("country", "Mexico as well").put("year_founded", 1916).put("some_timestamp", 123456579L));
    String topicName = "mainstance.cdcForDataflow.team_metadata";
    // We are going to pass two records to be checked, but only one of them belongs to a table that
    // is whitelisted, therefore, only one will be published to pubsub.
    List<SourceRecord> recordBatch = ImmutableList.of(new SourceRecord(ImmutableMap.of("server", "mainstance"), ImmutableMap.of("file", "mysql-bin.000023", "pos", 110489, "gtids", "36797132-a366-11e9-ac33-42010a800456:1-6407169", "row", 1, "snapshot", true), topicName, keySchema, key, valueSchema, value), new SourceRecord(ImmutableMap.of("server", "mainstance"), ImmutableMap.of("file", "mysql-bin.000023", "pos", 110490, "gtids", "36797132-a366-11e9-ac33-42010a800456:1-6407169", "row", 1, "snapshot", true), // A topic that was NOT whitelisted
    "NOTWHITELISTEDTOPIC!", keySchema, key, valueSchema, value));
    RecordCommitter mockCommitter = Mockito.mock(RecordCommitter.class);
    changeConsumer.handleBatch(recordBatch, mockCommitter);
    Mockito.verify(pubsubMock, Mockito.times(1)).publish(Mockito.any(PubsubMessage.class));
    Mockito.verify(mockCommitter).markProcessed(recordBatch.get(0));
    Mockito.verify(mockCommitter).markBatchFinished();
}
Also used : Schema(org.apache.kafka.connect.data.Schema) DataCatalogSchemaManager(com.google.cloud.dataflow.cdc.common.DataCatalogSchemaUtils.DataCatalogSchemaManager) RecordCommitter(io.debezium.embedded.EmbeddedEngine.RecordCommitter) Publisher(com.google.cloud.pubsub.v1.Publisher) SourceRecord(org.apache.kafka.connect.source.SourceRecord) PubsubMessage(com.google.pubsub.v1.PubsubMessage) Struct(org.apache.kafka.connect.data.Struct) ApiFuture(com.google.api.core.ApiFuture) Test(org.junit.jupiter.api.Test)

Example 62 with Schema

use of com.google.pubsub.v1.Schema in project DataflowTemplates by GoogleCloudPlatform.

the class SchemaUtils method fromBeamField.

private static ColumnSchema fromBeamField(org.apache.beam.sdk.schemas.Schema.Field beamField) {
    ColumnSchema.Builder columnBuilder = ColumnSchema.newBuilder();
    if (beamField.getType().getNullable()) {
        columnBuilder.setMode("NULLABLE");
    } else {
        columnBuilder.setMode("REQUIRED");
    }
    if (beamField.getType().getTypeName() == TypeName.ROW) {
        String columnType = "STRUCT";
        Schema subSchema = fromBeamSchema(beamField.getType().getRowSchema());
        return columnBuilder.setColumn(beamField.getName()).setType(columnType).addAllSubcolumns(subSchema.getColumnsList()).build();
    } else if (LOGICAL_FIELD_TYPES.inverse().containsKey(beamField.getType())) {
        String columnType = LOGICAL_FIELD_TYPES.inverse().get(beamField.getType());
        return columnBuilder.setColumn(beamField.getName()).setType(columnType).build();
    } else {
        String columnType = FIELD_TYPE_NAMES.inverse().get(beamField.getType().getTypeName());
        // TODO(pabloem): Include other characteristics of the field (e.g. required/nullable/etc).
        return columnBuilder.setColumn(beamField.getName()).setType(columnType).build();
    }
}
Also used : ColumnSchema(com.google.cloud.datacatalog.v1beta1.ColumnSchema) Schema(com.google.cloud.datacatalog.v1beta1.Schema) ColumnSchema(com.google.cloud.datacatalog.v1beta1.ColumnSchema)

Example 63 with Schema

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

the class SchemaServiceClientTest method createSchemaExceptionTest2.

@Test
public void createSchemaExceptionTest2() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSchemaService.addException(exception);
    try {
        String parent = "parent-995424086";
        Schema schema = Schema.newBuilder().build();
        String schemaId = "schemaId-697673060";
        client.createSchema(parent, schema, schemaId);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) Schema(com.google.pubsub.v1.Schema) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 64 with Schema

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

the class SchemaServiceClientTest method validateSchemaExceptionTest2.

@Test
public void validateSchemaExceptionTest2() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSchemaService.addException(exception);
    try {
        String parent = "parent-995424086";
        Schema schema = Schema.newBuilder().build();
        client.validateSchema(parent, schema);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) Schema(com.google.pubsub.v1.Schema) StatusRuntimeException(io.grpc.StatusRuntimeException) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 65 with Schema

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

the class SchemaServiceClientTest method createSchemaTest2.

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

Aggregations

Test (org.junit.Test)65 Schema (com.google.pubsub.v1.Schema)38 Schema (org.molgenis.emx2.Schema)38 ByteString (com.google.protobuf.ByteString)19 SchemaServiceClient (com.google.cloud.pubsub.v1.SchemaServiceClient)18 AbstractMessage (com.google.protobuf.AbstractMessage)18 QName (javax.xml.namespace.QName)16 SchemaName (com.google.pubsub.v1.SchemaName)15 File (java.io.File)15 Schema (org.geosdi.geoplatform.xml.xsd.v2001.Schema)15 ProjectName (com.google.pubsub.v1.ProjectName)14 IOException (java.io.IOException)14 URL (java.net.URL)14 LayerSchemaDTO (org.geosdi.geoplatform.connector.wfs.response.LayerSchemaDTO)14 StringWriter (java.io.StringWriter)13 Schema (org.oasisopen.odata.csdl.v4.Schema)13 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)12 StatusRuntimeException (io.grpc.StatusRuntimeException)12 Schema (com.reprezen.kaizen.oasparser.model3.Schema)11 JAXBElement (javax.xml.bind.JAXBElement)10