Search in sources :

Example 11 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class RecordWithMetadataToEnvelopedRecordWithMetadataTest method testSuccessWithInferredPrintableByteArray.

@Test
public void testSuccessWithInferredPrintableByteArray() throws DataConversionException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    byte[] record = "abrac\\adabra".getBytes(StandardCharsets.UTF_8);
    Metadata md = new Metadata();
    md.getGlobalMetadata().setContentType("application/binary");
    md.getGlobalMetadata().addTransferEncoding("base64");
    RecordWithMetadataToEnvelopedRecordWithMetadata converter = new RecordWithMetadataToEnvelopedRecordWithMetadata();
    Iterator<RecordWithMetadata<byte[]>> recordWithMetadataIterator = converter.convertRecord("", new RecordWithMetadata<>(record, md), null).iterator();
    RecordWithMetadata recordWithMetadata = recordWithMetadataIterator.next();
    JsonNode parsedElement = objectMapper.readValue((byte[]) recordWithMetadata.getRecord(), JsonNode.class);
    Assert.assertEquals(parsedElement.get("r").getTextValue(), new String(record, StandardCharsets.UTF_8));
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) Metadata(org.apache.gobblin.metadata.types.Metadata) RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Example 12 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class EnvelopedRecordWithMetadataToRecordWithMetadataTest method testSuccessWithRecord.

@Test
public void testSuccessWithRecord() throws DataConversionException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    String innerRecord = "abracadabra";
    // Build the input record
    HashMap<String, Object> map = new HashMap<>();
    map.put("r", innerRecord);
    Metadata md = new Metadata();
    md.getRecordMetadata().put("test1", "test2");
    map.put("rMd", md);
    JsonNode jsonNode = objectMapper.valueToTree(map);
    RecordWithMetadata<byte[]> inputRecord = new RecordWithMetadata<>(jsonNode.toString().getBytes(), null);
    EnvelopedRecordWithMetadataToRecordWithMetadata converter = new EnvelopedRecordWithMetadataToRecordWithMetadata();
    Iterator<RecordWithMetadata<?>> iterator = converter.convertRecord(null, inputRecord, null).iterator();
    Assert.assertTrue(iterator.hasNext());
    RecordWithMetadata<?> outputRecord = iterator.next();
    Assert.assertEquals(outputRecord.getRecord(), innerRecord);
    Assert.assertEquals(outputRecord.getMetadata().getRecordMetadata().get("test1"), "test2");
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) HashMap(java.util.HashMap) Metadata(org.apache.gobblin.metadata.types.Metadata) RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Example 13 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class EnvelopedRecordWithMetadataToRecordWithMetadataTest method testFailureWithoutRecord.

@Test(expectedExceptions = DataConversionException.class, expectedExceptionsMessageRegExp = "Input data does not have record.")
public void testFailureWithoutRecord() throws DataConversionException, IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    // Build the input record without data
    HashMap<String, Object> map = new HashMap<>();
    Metadata md = new Metadata();
    md.getRecordMetadata().put("test1", "test2");
    map.put("rMd", md);
    JsonNode jsonNode = objectMapper.valueToTree(map);
    RecordWithMetadata<byte[]> inputRecord = new RecordWithMetadata<>(jsonNode.toString().getBytes(), null);
    EnvelopedRecordWithMetadataToRecordWithMetadata converter = new EnvelopedRecordWithMetadataToRecordWithMetadata();
    converter.convertRecord(null, inputRecord, null);
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) HashMap(java.util.HashMap) Metadata(org.apache.gobblin.metadata.types.Metadata) RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) JsonNode(org.codehaus.jackson.JsonNode) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.testng.annotations.Test)

Example 14 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class MetadataConverterWrapperTest method testConvertsMetadataNoOutput.

@Test
public void testConvertsMetadataNoOutput() throws DataConversionException {
    DummyConverter dummyConverter = new DummyConverter(0);
    MetadataConverterWrapper<String, String, String, String> wrapper = new MetadataConverterWrapper<>(dummyConverter);
    Iterable<RecordWithMetadata<String>> records = wrapper.convertRecord("foo", new RecordWithMetadata<String>("bar", buildMetadata(1)), new WorkUnitState());
    Assert.assertFalse(records.iterator().hasNext());
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) Test(org.testng.annotations.Test)

Example 15 with RecordWithMetadata

use of org.apache.gobblin.type.RecordWithMetadata in project incubator-gobblin by apache.

the class MetadataConverterWrapperTest method testAcceptsRawRecords.

@Test
public void testAcceptsRawRecords() throws DataConversionException {
    final int numRecordsToReturn = 1;
    DummyConverter dummyConverter = new DummyConverter(numRecordsToReturn);
    MetadataConverterWrapper<String, String, String, String> wrapper = new MetadataConverterWrapper<>(dummyConverter);
    Iterable<RecordWithMetadata<String>> records = wrapper.convertRecord("foo", "bar", new WorkUnitState());
    Iterator<RecordWithMetadata<String>> recordsIt = records.iterator();
    RecordWithMetadata<String> record = recordsIt.next();
    Assert.assertFalse(recordsIt.hasNext());
    Assert.assertEquals(record.getRecord(), "converted0");
    Assert.assertEquals(record.getMetadata().getGlobalMetadata().getId(), "0");
}
Also used : RecordWithMetadata(org.apache.gobblin.type.RecordWithMetadata) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) Test(org.testng.annotations.Test)

Aggregations

RecordWithMetadata (org.apache.gobblin.type.RecordWithMetadata)19 Metadata (org.apache.gobblin.metadata.types.Metadata)13 Test (org.testng.annotations.Test)11 JsonNode (org.codehaus.jackson.JsonNode)7 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)6 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)6 HashMap (java.util.HashMap)3 GlobalMetadata (org.apache.gobblin.metadata.types.GlobalMetadata)3 BeforeTest (org.testng.annotations.BeforeTest)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 JsonParser (org.codehaus.jackson.JsonParser)1