Search in sources :

Example 6 with DbusEventPart

use of com.linkedin.databus.core.DbusEventPart in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_HappyPath.

/**
   * Verifies that getMetadata() returns the expected GenericRecord for the event's
   * metadata and that it has the expected fields and values in it.
   */
@Test
public void testGetMetadata_HappyPath() throws Exception {
    LOG.info("starting testGetMetadata_HappyPath()");
    // build the event's metadata and then the event
    DbusEventPart metadataPart = createMetadataPart();
    DbusEvent event = createEvent(metadataPart);
    // create a metadata schema set that correctly corresponds to the metadata
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    metadataSchemaSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, // METADATA_SCHEMA_VERSION
    metadataPart.getSchemaVersion(), // METADATA_SCHEMA_CHECKSUM
    new SchemaId(metadataPart.getSchemaDigest()), CORRECT_METADATA_SCHEMA, // preserve original string
    true);
    // now create the decoder and use it to extract and decode the event's metadata
    DbusEventAvroDecoder eventDecoder = createDecoder(metadataSchemaSet);
    try {
        GenericRecord reuse = null;
        GenericRecord decodedMetadata = eventDecoder.getMetadata(event, reuse);
        Assert.assertNotNull(decodedMetadata, "getMetadata() returned null GenericRecord;");
        Utf8 etag = (Utf8) decodedMetadata.get("etag");
        Assert.assertEquals(etag.toString(), "dunno what an etag is");
        Integer flags = (Integer) decodedMetadata.get("flags");
        Assert.assertEquals(flags, null, "expected flags to be null");
        Long expires = (Long) decodedMetadata.get("expires");
        Assert.assertNotNull(expires, "expected expires to have a value;");
        Assert.assertEquals(expires.longValue(), 1366150681);
        Utf8 nonexistentField = (Utf8) decodedMetadata.get("nonexistentField");
        Assert.assertNull(nonexistentField, "unexpected value for 'nonexistentField';");
    } catch (Exception ex) {
        Assert.fail("unexpected error decoding metadata: " + ex);
    }
    LOG.info("leaving testGetMetadata_HappyPath()");
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) SchemaId(com.linkedin.databus2.schemas.SchemaId) Utf8(org.apache.avro.util.Utf8) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 7 with DbusEventPart

use of com.linkedin.databus.core.DbusEventPart in project databus by linkedin.

the class DbusEventAvroDecoder method getMetadataSchema.

/**
   * Returns the single version of the metadata schema specified in the given event's header.
   * For INTERNAL USE ONLY (by Espresso and Databus).  This is not a stable API and may change
   * without warning!
   *
   * @param e DbusEvent
   * @return {AvroSchema, "metadata-source", version} tuple for given event 'e' with
   *         metadata-schema-id; null if event contains no metadata
   * @throws DatabusRuntimeException if event contains metadata but schema to decode it is missing
   */
public VersionedSchema getMetadataSchema(DbusEvent e) {
    DbusEventPart metadataPart = e.getPayloadMetadataPart();
    if (null == metadataPart) {
        LOG.debug("No metadata for event " + e);
        return null;
    }
    VersionedSchema schema = getMetadataSchema(metadataPart);
    if (null == schema) {
        throw new DatabusRuntimeException("No schema available to decode metadata for event " + e);
    }
    return schema;
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException)

Aggregations

DbusEventPart (com.linkedin.databus.core.DbusEventPart)7 DbusEventAvroDecoder (com.linkedin.databus.client.DbusEventAvroDecoder)4 DbusEvent (com.linkedin.databus.core.DbusEvent)3 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)3 VersionedSchemaSet (com.linkedin.databus2.schemas.VersionedSchemaSet)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 Test (org.testng.annotations.Test)3 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)2 SchemaId (com.linkedin.databus2.schemas.SchemaId)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IOException (java.io.IOException)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 ByteBuffer (java.nio.ByteBuffer)1 Date (java.util.Date)1 Utf8 (org.apache.avro.util.Utf8)1