Search in sources :

Example 1 with DataMap

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 + "\"");
                }
            }
        }
    }
}
Also used : PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) DataSchema(com.linkedin.data.schema.DataSchema) Schema(org.apache.avro.Schema) SchemaParser(com.linkedin.data.schema.SchemaParser) PegasusSchemaParser(com.linkedin.data.schema.PegasusSchemaParser) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with DataMap

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);
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 3 with DataMap

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);
}
Also used : DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 4 with DataMap

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();
}
Also used : ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) DataMap(com.linkedin.data.DataMap)

Example 5 with DataMap

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));
    }
}
Also used : PathSegmentSyntaxException(com.linkedin.restli.internal.common.PathSegment.PathSegmentSyntaxException) DataMap(com.linkedin.data.DataMap)

Aggregations

DataMap (com.linkedin.data.DataMap)670 Test (org.testng.annotations.Test)322 DataList (com.linkedin.data.DataList)154 ByteString (com.linkedin.data.ByteString)127 HashMap (java.util.HashMap)72 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)70 Map (java.util.Map)67 DataSchema (com.linkedin.data.schema.DataSchema)62 TestUtil.dataMapFromString (com.linkedin.data.TestUtil.dataMapFromString)51 ArrayList (java.util.ArrayList)48 TestUtil.dataSchemaFromString (com.linkedin.data.TestUtil.dataSchemaFromString)47 RecordTemplate (com.linkedin.data.template.RecordTemplate)33 IOException (java.io.IOException)33 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)32 PathSpec (com.linkedin.data.schema.PathSpec)30 HashSet (java.util.HashSet)30 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)24 MapDataSchema (com.linkedin.data.schema.MapDataSchema)23 List (java.util.List)23 DataProvider (org.testng.annotations.DataProvider)23