use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestSchemaTranslator method testToAvroSchema.
private void testToAvroSchema(String schemaText, Object[] row) throws IOException {
boolean debug = false;
if (debug)
System.out.println(schemaText);
for (int i = 1; i < row.length; i++) {
Object[] modeInputs = (Object[]) row[i];
OptionalDefaultMode[] optionalDefaultModes = (OptionalDefaultMode[]) modeInputs[0];
Object expected = modeInputs[1];
for (EmbedSchemaMode embedSchemaMode : EmbedSchemaMode.values()) {
for (OptionalDefaultMode optionalDefaultMode : optionalDefaultModes) {
DataSchema schema = TestUtil.dataSchemaFromString(schemaText);
String preTranslateSchemaText = schema.toString();
Exception exc = null;
String avroTextFromSchema = null;
try {
avroTextFromSchema = SchemaTranslator.dataToAvroSchemaJson(schema, new DataToAvroSchemaTranslationOptions(optionalDefaultMode, JsonBuilder.Pretty.SPACES, embedSchemaMode));
if (debug) {
System.out.println("EmbeddedSchema: " + embedSchemaMode + ", OptionalDefaultMode: " + optionalDefaultMode + ", Avro Schema: " + avroTextFromSchema);
}
} catch (Exception e) {
exc = e;
if (debug) {
e.printStackTrace();
}
}
if (expected instanceof String) {
assertNull(exc);
String expectedAvroText = (String) expected;
if (embedSchemaMode == EmbedSchemaMode.ROOT_ONLY && hasEmbeddedSchema(schema)) {
// when embeddedSchema is enabled
// for map, array, enums. and records, we embed the original Pegasus schema
DataMap expectedAvroDataMap = TestUtil.dataMapFromString(expectedAvroText);
DataMap resultAvroDataMap = TestUtil.dataMapFromString(avroTextFromSchema);
Object dataProperty = resultAvroDataMap.remove(SchemaTranslator.DATA_PROPERTY);
assertEquals(resultAvroDataMap, expectedAvroDataMap);
// look for embedded schema
assertNotNull(dataProperty);
assertTrue(dataProperty instanceof DataMap);
Object schemaProperty = ((DataMap) dataProperty).get(SchemaTranslator.SCHEMA_PROPERTY);
assertNotNull(schemaProperty);
assertTrue(schemaProperty instanceof DataMap);
// make sure embedded schema is same as the original schema
PegasusSchemaParser schemaParser = TestUtil.schemaParserFromObjects(Arrays.asList(schemaProperty));
DataSchema embeddedSchema = schemaParser.topLevelDataSchemas().get(0);
assertEquals(embeddedSchema, schema.getDereferencedDataSchema());
// look for optional default mode
Object optionalDefaultModeProperty = ((DataMap) dataProperty).get(SchemaTranslator.OPTIONAL_DEFAULT_MODE_PROPERTY);
assertNotNull(optionalDefaultMode);
assertEquals(optionalDefaultModeProperty, optionalDefaultMode.toString());
} else {
// for unions and primitives, we never embed the pegasus schema
if (embedSchemaMode == EmbedSchemaMode.NONE && hasEmbeddedSchema(schema)) {
// make sure no embedded schema when
DataMap resultAvroDataMap = TestUtil.dataMapFromString(avroTextFromSchema);
assertFalse(resultAvroDataMap.containsKey(SchemaTranslator.DATA_PROPERTY));
}
assertEquals(avroTextFromSchema, expectedAvroText);
}
String postTranslateSchemaText = schema.toString();
assertEquals(preTranslateSchemaText, postTranslateSchemaText);
// make sure Avro accepts it
Schema avroSchema = Schema.parse(avroTextFromSchema);
if (debug)
System.out.println("AvroSchema: " + avroSchema);
SchemaParser parser = new SchemaParser();
ValidationOptions options = new ValidationOptions();
options.setAvroUnionMode(true);
parser.setValidationOptions(options);
parser.parse(avroTextFromSchema);
assertFalse(parser.hasError(), parser.errorMessage());
if (optionalDefaultMode == DataToAvroSchemaTranslationOptions.DEFAULT_OPTIONAL_DEFAULT_MODE) {
// use other dataToAvroSchemaJson
String avroSchema2Json = SchemaTranslator.dataToAvroSchemaJson(TestUtil.dataSchemaFromString(schemaText));
String avroSchema2JsonCompact = SchemaTranslator.dataToAvroSchemaJson(TestUtil.dataSchemaFromString(schemaText), new DataToAvroSchemaTranslationOptions());
assertEquals(avroSchema2Json, avroSchema2JsonCompact);
Schema avroSchema2 = Schema.parse(avroSchema2Json);
assertEquals(avroSchema2, avroSchema);
// use dataToAvroSchema
Schema avroSchema3 = SchemaTranslator.dataToAvroSchema(TestUtil.dataSchemaFromString(schemaText));
assertEquals(avroSchema3, avroSchema2);
}
if (modeInputs.length >= 4) {
// check if the translated default value is good by using it.
// writer schema and Avro JSON value should not include fields with default values.
String writerSchemaText = (String) modeInputs[2];
String avroValueJson = (String) modeInputs[3];
Schema writerSchema = Schema.parse(writerSchemaText);
GenericRecord genericRecord = genericRecordFromString(avroValueJson, writerSchema, avroSchema);
if (modeInputs.length >= 5) {
String genericRecordJson = (String) modeInputs[4];
String genericRecordAsString = genericRecord.toString();
DataMap expectedGenericRecord = TestUtil.dataMapFromString(genericRecordJson);
DataMap resultGenericRecord = TestUtil.dataMapFromString(genericRecordAsString);
assertEquals(resultGenericRecord, expectedGenericRecord);
}
}
if (embedSchemaMode == EmbedSchemaMode.ROOT_ONLY && hasEmbeddedSchema(schema)) {
// if embedded schema is enabled, translate Avro back to Pegasus schema.
// the output Pegasus schema should be exactly same the input schema
// taking into account typeref.
AvroToDataSchemaTranslationOptions avroToDataSchemaMode = new AvroToDataSchemaTranslationOptions(AvroToDataSchemaTranslationMode.VERIFY_EMBEDDED_SCHEMA);
DataSchema embeddedSchema = SchemaTranslator.avroToDataSchema(avroTextFromSchema, avroToDataSchemaMode);
assertEquals(embeddedSchema, schema.getDereferencedDataSchema());
}
} else {
Class<?> expectedExceptionClass = (Class<?>) expected;
String expectedString = (String) modeInputs[2];
assertNotNull(exc);
assertNull(avroTextFromSchema);
assertTrue(expectedExceptionClass.isInstance(exc));
assertTrue(exc.getMessage().contains(expectedString), "\"" + exc.getMessage() + "\" does not contain \"" + expectedString + "\"");
}
}
}
}
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestJacksonCodec method testNoStringIntern.
/**
* Test to make sure that field names are not interned.
*
* @throws IOException
*/
@Test
public void testNoStringIntern() throws IOException {
final String keyName = "testKey";
final String json = "{ \"" + keyName + "\" : 1 }";
final byte[] jsonAsBytes = json.getBytes(Data.UTF_8_CHARSET);
{
final JsonFactory jsonFactory = new JsonFactory();
final JacksonDataCodec codec = new JacksonDataCodec(jsonFactory);
// make sure intern field names is not enabled
assertFalse(jsonFactory.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
assertTrue(jsonFactory.isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES));
final DataMap map = codec.bytesToMap(jsonAsBytes);
final String key = map.keySet().iterator().next();
assertNotSame(key, keyName);
}
{
final JsonFactory jsonFactory = new JsonFactory();
final JacksonDataCodec codec = new JacksonDataCodec(jsonFactory);
// enable intern field names
jsonFactory.enable(JsonFactory.Feature.INTERN_FIELD_NAMES);
assertTrue(jsonFactory.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES));
assertTrue(jsonFactory.isEnabled(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES));
final DataMap map = codec.bytesToMap(jsonAsBytes);
final String key = map.keySet().iterator().next();
assertSame(key, keyName);
}
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestJacksonCodec method testLongKeyFromByteSource.
/**
* Prior to version 2.4.3, Jackson could not handle map keys >= 262146 bytes, if the data source is byte array.
* The issue is resolved in https://github.com/FasterXML/jackson-core/issues/152
*/
@Test
public void testLongKeyFromByteSource() throws IOException {
final StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.append("{\"");
for (int i = 0; i < 43691; ++i) {
jsonBuilder.append("6_byte");
}
jsonBuilder.append("\":0}");
final JacksonDataCodec codec = new JacksonDataCodec();
final DataMap map = codec.bytesToMap(jsonBuilder.toString().getBytes());
Assert.assertEquals(map.keySet().iterator().next().length(), 262146);
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class RestLiResponseHandler method buildResponse.
/**
* Build a RestResponse from PartialRestResponse and RoutingResult.
*
* @param routingResult
* {@link RoutingResult}
* @param partialResponse
* {@link PartialRestResponse}
* @return
*/
public RestResponse buildResponse(final RoutingResult routingResult, PartialRestResponse partialResponse) {
List<String> cookies = CookieUtil.encodeSetCookies(partialResponse.getCookies());
RestResponseBuilder builder = new RestResponseBuilder().setHeaders(partialResponse.getHeaders()).setCookies(cookies).setStatus(partialResponse.getStatus().getCode());
if (partialResponse.hasData()) {
DataMap dataMap = partialResponse.getDataMap();
String mimeType = ((ServerResourceContext) routingResult.getContext()).getResponseMimeType();
builder = encodeResult(mimeType, builder, dataMap);
}
return builder.build();
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class ArgumentUtils method parseCompoundKeyV2.
/**
*
* @param urlString {@link String} representation of the v2 compound key
* @param keys the {@link Key}s representing each part of the compound key.
* @return a {@link CompoundKey}
* @throws IllegalArgumentException if there are unexpected key parts in the urlString that are not in keys.
* @throws PathSegmentSyntaxException if the given string is not a valid encoded v2 compound key
*/
private static CompoundKey parseCompoundKeyV2(final String urlString, final Collection<Key> keys) throws PathSegmentSyntaxException, IllegalArgumentException {
DataMap dataMap;
// dataMap looks like keyName1: keyValue1, keyName2: keyValue2, ...
Object parsedObject = URIElementParser.parse(urlString);
if (parsedObject instanceof DataMap) {
dataMap = (DataMap) parsedObject;
return dataMapToCompoundKey(dataMap, keys);
} else {
throw new PathSegmentSyntaxException(String.format("input '%s' is not a valid CompoundKey", urlString));
}
}
Aggregations