Search in sources :

Example 21 with StringArray

use of com.linkedin.data.template.StringArray in project rest.li by linkedin.

the class TestParameterDefaultValue method testWrappedArray.

@Test
public void testWrappedArray() {
    Object result;
    result = test("[\"Hello\", \"World\"]", StringArray.class);
    Assert.assertEquals(result, new StringArray(Arrays.asList("Hello", "World")));
    Assert.assertSame(result.getClass(), StringArray.class);
    result = test("[false, true]", BooleanArray.class);
    Assert.assertEquals(result, new BooleanArray(Arrays.asList(false, true)));
    Assert.assertSame(result.getClass(), BooleanArray.class);
    result = test("[1, 2, 3]", IntegerArray.class);
    Assert.assertEquals(result, new IntegerArray(Arrays.asList(1, 2, 3)));
    Assert.assertSame(result.getClass(), IntegerArray.class);
    result = test("[1.1, 2.2, 3.3]", IntegerArray.class);
    Assert.assertEquals(result, new IntegerArray(Arrays.asList(1, 2, 3)));
    Assert.assertSame(result.getClass(), IntegerArray.class);
    result = test("[2, 3, 4]", LongArray.class);
    Assert.assertEquals(result, new LongArray(Arrays.asList(2L, 3L, 4L)));
    Assert.assertSame(result.getClass(), LongArray.class);
    result = test("[1.1, 2.2, 3.3]", FloatArray.class);
    Assert.assertEquals(result, new FloatArray(Arrays.asList(1.1F, 2.2F, 3.3F)));
    Assert.assertSame(result.getClass(), FloatArray.class);
    result = test("[2.2, 3.3, 4.4]", DoubleArray.class);
    Assert.assertEquals(result, new DoubleArray(Arrays.asList(2.2D, 3.3D, 4.4D)));
    Assert.assertSame(result.getClass(), DoubleArray.class);
    result = test("[\"APPLE\", \"BANANA\"]", EnumFruitsArray.class);
    Assert.assertEquals(result, new EnumFruitsArray(Arrays.asList(EnumFruits.APPLE, EnumFruits.BANANA)));
    Assert.assertSame(result.getClass(), EnumFruitsArray.class);
    result = test("[" + _bytes16Quoted + ", " + _bytes16Quoted + "]", BytesArray.class);
    Assert.assertEquals(result, new BytesArray(Arrays.asList(ByteString.copyAvroString(_bytes16, true), ByteString.copyAvroString(_bytes16, true))));
    Assert.assertSame(result.getClass(), BytesArray.class);
    result = test("[" + _bytes16Quoted + ", " + _bytes16Quoted + "]", FixedMD5Array.class);
    Assert.assertEquals(result, new FixedMD5Array(Arrays.asList(new FixedMD5(_bytes16), new FixedMD5(_bytes16))));
    Assert.assertSame(result.getClass(), FixedMD5Array.class);
    result = test("[{\"string\": \"String in union\"}, {\"int\": 1}]", ArrayTest.UnionArrayArray.class);
    final ArrayTest.UnionArray fixture1 = new ArrayTest.UnionArray();
    fixture1.setString("String in union");
    final ArrayTest.UnionArray fixture2 = new ArrayTest.UnionArray();
    fixture2.setInt(1);
    Assert.assertEquals(result, new ArrayTest.UnionArrayArray(Arrays.asList(fixture1, fixture2)));
    Assert.assertSame(result.getClass(), ArrayTest.UnionArrayArray.class);
    result = test("[{\"location\": \"Sunnyvale\"}, {\"location\": \"Mountain View\"}]", RecordBarArray.class);
    final DataMap dataFixture1 = new DataMap();
    final DataMap dataFixture2 = new DataMap();
    dataFixture1.put("location", "Sunnyvale");
    dataFixture2.put("location", "Mountain View");
    Assert.assertEquals(result, new RecordBarArray(Arrays.asList(new RecordBar(dataFixture1), new RecordBar(dataFixture2))));
    Assert.assertSame(result.getClass(), RecordBarArray.class);
}
Also used : BytesArray(com.linkedin.data.template.BytesArray) ArrayTest(com.linkedin.pegasus.generator.test.ArrayTest) RecordBar(com.linkedin.pegasus.generator.test.RecordBar) FixedMD5(com.linkedin.pegasus.generator.test.FixedMD5) BooleanArray(com.linkedin.data.template.BooleanArray) IntegerArray(com.linkedin.data.template.IntegerArray) DataMap(com.linkedin.data.DataMap) LongArray(com.linkedin.data.template.LongArray) FloatArray(com.linkedin.data.template.FloatArray) RecordBarArray(com.linkedin.pegasus.generator.test.RecordBarArray) StringArray(com.linkedin.data.template.StringArray) EnumFruitsArray(com.linkedin.pegasus.generator.test.EnumFruitsArray) FixedMD5Array(com.linkedin.pegasus.generator.test.FixedMD5Array) DoubleArray(com.linkedin.data.template.DoubleArray) Test(org.testng.annotations.Test) ArrayTest(com.linkedin.pegasus.generator.test.ArrayTest)

Example 22 with StringArray

use of com.linkedin.data.template.StringArray in project rest.li by linkedin.

the class RestLiResourceRelationship method findDataModels.

private void findDataModels() {
    final ResourceSchemaVisitior visitor = new BaseResourceSchemaVisitor() {

        @Override
        public void visitResourceSchema(VisitContext visitContext, ResourceSchema resourceSchema) {
            final String schema = resourceSchema.getSchema();
            // ActionSet resources do not have a schema
            if (schema != null) {
                final NamedDataSchema schemaSchema = extractSchema(schema);
                if (schemaSchema != null) {
                    connectSchemaToResource(visitContext, schemaSchema);
                }
            }
        }

        @Override
        public void visitCollectionResource(VisitContext visitContext, CollectionSchema collectionSchema) {
            final IdentifierSchema id = collectionSchema.getIdentifier();
            final NamedDataSchema typeSchema = extractSchema(id.getType());
            if (typeSchema != null) {
                connectSchemaToResource(visitContext, typeSchema);
            }
            final String params = id.getParams();
            if (params != null) {
                final NamedDataSchema paramsSchema = extractSchema(params);
                if (paramsSchema != null) {
                    connectSchemaToResource(visitContext, paramsSchema);
                }
            }
        }

        @Override
        public void visitAssociationResource(VisitContext visitContext, AssociationSchema associationSchema) {
            for (AssocKeySchema key : associationSchema.getAssocKeys()) {
                final NamedDataSchema keyTypeSchema = extractSchema(key.getType());
                if (keyTypeSchema != null) {
                    connectSchemaToResource(visitContext, keyTypeSchema);
                }
            }
        }

        @Override
        public void visitParameter(VisitContext visitContext, RecordTemplate parentResource, Object parentMethodSchema, ParameterSchema parameterSchema) {
            String parameterTypeString = parameterSchema.getType();
            if (// the parameter type field contains a inline schema, so we traverse into it
            isInlineSchema(parameterTypeString)) {
                visitInlineSchema(visitContext, parameterTypeString);
            } else {
                final NamedDataSchema schema;
                // grab the schema name from it
                if (parameterSchema.hasItems()) {
                    schema = extractSchema(parameterSchema.getItems());
                } else // the only remaining possibility is that the type field contains the name of a data schema
                {
                    schema = extractSchema(parameterTypeString);
                }
                if (schema != null) {
                    connectSchemaToResource(visitContext, schema);
                }
            }
        }

        @Override
        public void visitFinder(VisitContext visitContext, RecordTemplate parentResource, FinderSchema finderSchema) {
            final MetadataSchema metadata = finderSchema.getMetadata();
            if (metadata != null) {
                final NamedDataSchema metadataTypeSchema = extractSchema(metadata.getType());
                if (metadataTypeSchema != null) {
                    connectSchemaToResource(visitContext, metadataTypeSchema);
                }
            }
        }

        @Override
        public void visitAction(VisitContext visitContext, RecordTemplate parentResource, ResourceLevel resourceLevel, ActionSchema actionSchema) {
            final String returns = actionSchema.getReturns();
            if (returns != null) {
                if (// the parameter type field contains a inline schema, so we traverse into it
                isInlineSchema(returns)) {
                    visitInlineSchema(visitContext, returns);
                } else // otherwise the type field contains the name of a data schema
                {
                    final NamedDataSchema returnsSchema = extractSchema(returns);
                    if (returnsSchema != null) {
                        connectSchemaToResource(visitContext, returnsSchema);
                    }
                }
            }
            final StringArray throwsArray = actionSchema.getThrows();
            if (throwsArray != null) {
                for (String errorName : throwsArray) {
                    final NamedDataSchema errorSchema = extractSchema(errorName);
                    if (errorSchema != null) {
                        connectSchemaToResource(visitContext, errorSchema);
                    }
                }
            }
        }

        private boolean isInlineSchema(String schemaString) {
            return schemaString.startsWith("{");
        }

        private void visitInlineSchema(VisitContext visitContext, String schemaString) {
            DataSchema schema = DataTemplateUtil.parseSchema(schemaString, _schemaResolver);
            if (schema instanceof ArrayDataSchema) {
                DataSchema itemSchema = ((ArrayDataSchema) schema).getItems();
                if (itemSchema instanceof NamedDataSchema) {
                    connectSchemaToResource(visitContext, (NamedDataSchema) itemSchema);
                }
            }
            if (schema instanceof MapDataSchema) {
                DataSchema valueSchema = ((MapDataSchema) schema).getValues();
                if (valueSchema instanceof NamedDataSchema) {
                    connectSchemaToResource(visitContext, (NamedDataSchema) valueSchema);
                }
            }
        }

        private void connectSchemaToResource(VisitContext visitContext, final NamedDataSchema schema) {
            final Node<NamedDataSchema> schemaNode = _relationships.get(schema);
            _dataModels.put(schema.getFullName(), schema);
            final DataSchemaTraverse traveler = new DataSchemaTraverse();
            traveler.traverse(schema, new DataSchemaTraverse.Callback() {

                @Override
                public void callback(List<String> path, DataSchema nestedSchema) {
                    if (nestedSchema instanceof RecordDataSchema && nestedSchema != schema) {
                        final RecordDataSchema nestedRecordSchema = (RecordDataSchema) nestedSchema;
                        _dataModels.put(nestedRecordSchema.getFullName(), nestedRecordSchema);
                        final Node<RecordDataSchema> node = _relationships.get(nestedRecordSchema);
                        schemaNode.addAdjacentNode(node);
                    }
                }
            });
            final Node<ResourceSchema> resourceNode = _relationships.get(visitContext.getParentSchema());
            resourceNode.addAdjacentNode(schemaNode);
            schemaNode.addAdjacentNode(resourceNode);
        }
    };
    ResourceSchemaCollection.visitResources(_resourceSchemas.getResources().values(), visitor);
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) ResourceLevel(com.linkedin.restli.server.ResourceLevel) MapDataSchema(com.linkedin.data.schema.MapDataSchema) ParameterSchema(com.linkedin.restli.restspec.ParameterSchema) FinderSchema(com.linkedin.restli.restspec.FinderSchema) StringArray(com.linkedin.data.template.StringArray) IdentifierSchema(com.linkedin.restli.restspec.IdentifierSchema) RecordTemplate(com.linkedin.data.template.RecordTemplate) AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) MetadataSchema(com.linkedin.restli.restspec.MetadataSchema) ActionSchema(com.linkedin.restli.restspec.ActionSchema) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataSchema(com.linkedin.data.schema.DataSchema) MapDataSchema(com.linkedin.data.schema.MapDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) ArrayDataSchema(com.linkedin.data.schema.ArrayDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataSchemaTraverse(com.linkedin.data.schema.DataSchemaTraverse)

Example 23 with StringArray

use of com.linkedin.data.template.StringArray in project rest.li by linkedin.

the class ResourceCompatibilityChecker method checkFinderSchema.

private void checkFinderSchema(FinderSchema prevRec, FinderSchema currRec) {
    checkEqualSingleValue(prevRec.schema().getField("name"), prevRec.getName(GetMode.DEFAULT), currRec.getName(GetMode.DEFAULT));
    checkDoc(prevRec.schema().getField("doc"), prevRec.getDoc(GetMode.DEFAULT), currRec.getDoc(GetMode.DEFAULT));
    checkAnnotationsMap(prevRec.schema().getField("annotations"), prevRec.getAnnotations(GetMode.DEFAULT), currRec.getAnnotations(GetMode.DEFAULT));
    checkParameterArrayField(prevRec.schema().getField("parameters"), prevRec.getParameters(GetMode.DEFAULT), currRec.getParameters(GetMode.DEFAULT));
    checkComplexField(prevRec.schema().getField("metadata"), prevRec.getMetadata(GetMode.DEFAULT), currRec.getMetadata(GetMode.DEFAULT));
    checkPagingSupport(prevRec.isPagingSupported(GetMode.DEFAULT), currRec.isPagingSupported(GetMode.DEFAULT));
    final String prevAssocKey = prevRec.getAssocKey(GetMode.DEFAULT);
    final String currAssocKey = currRec.getAssocKey(GetMode.DEFAULT);
    final StringArray prevAssocKeys = prevRec.getAssocKeys(GetMode.DEFAULT);
    final StringArray currAssocKeys = currRec.getAssocKeys(GetMode.DEFAULT);
    // assocKey and assocKeys are mutually exclusive
    assert ((prevAssocKey == null || prevAssocKeys == null) && (currAssocKey == null || currAssocKeys == null));
    if (prevAssocKeys == null && currAssocKeys == null) {
        checkEqualSingleValue(prevRec.schema().getField("assocKey"), prevAssocKey, currAssocKey);
    } else if (prevAssocKey == null && currAssocKey == null) {
        checkEqualSingleValue(prevRec.schema().getField("assocKeys"), prevAssocKeys, currAssocKeys);
    } else if (prevAssocKeys == null) {
        // upgrade case
        final StringArray upgradedPrevAssocKeys = new StringArray();
        upgradedPrevAssocKeys.add(prevAssocKey);
        checkEqualSingleValue(prevRec.schema().getField("assocKey"), upgradedPrevAssocKeys, currAssocKeys);
    } else {
        // downgrade case
        _infoMap.addRestSpecInfo("assocKeys", CompatibilityInfo.Type.FINDER_ASSOCKEYS_DOWNGRADE, _infoPath);
    }
}
Also used : StringArray(com.linkedin.data.template.StringArray)

Example 24 with StringArray

use of com.linkedin.data.template.StringArray in project rest.li by linkedin.

the class TestResourceCompatibilityChecker method testFailSimpleFile.

@Test
public void testFailSimpleFile() throws IOException {
    final Collection<CompatibilityInfo> resourceTestErrors = new HashSet<CompatibilityInfo>();
    final Collection<CompatibilityInfo> modelTestErrors = new HashSet<CompatibilityInfo>();
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "supports"), CompatibilityInfo.Type.ARRAY_NOT_CONTAIN, new StringArray(Arrays.asList("delete", "get"))));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "methods"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "delete"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "methods", "get", "parameters", "param1", "type"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from string to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "actions", "oneAction", "parameters", "bitfield", "items"), CompatibilityInfo.Type.TYPE_ERROR, "schema type changed from boolean to int"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "actions", "oneAction", "parameters"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "someString"));
    resourceTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "simple", "actions", "oneAction", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_REQUIRED, "someStringNew"));
    modelTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("com.linkedin.greetings.api.Greeting"), CompatibilityInfo.Type.TYPE_BREAKS_NEW_READER, "new record added required fields newField"));
    modelTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("com.linkedin.greetings.api.Greeting"), CompatibilityInfo.Type.TYPE_BREAKS_OLD_READER, "new record removed required fields message"));
    modelTestErrors.add(new CompatibilityInfo(Arrays.<Object>asList("com.linkedin.greetings.api.Greeting", "id", "string"), CompatibilityInfo.Type.TYPE_BREAKS_NEW_AND_OLD_READERS, "schema type changed from long to string"));
    final ResourceSchema prevResource = idlToResource(IDLS_SUFFIX + PREV_SIMPLE_FILE);
    final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_SIMPLE_FAIL_FILE);
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, incompatSchemaResolver);
    Assert.assertFalse(checker.check(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> resourceIncompatible = new HashSet<CompatibilityInfo>(checker.getInfoMap().getRestSpecIncompatibles());
    for (CompatibilityInfo te : resourceTestErrors) {
        Assert.assertTrue(resourceIncompatible.contains(te), "Reported resource incompatibles should contain: " + te.toString());
        resourceIncompatible.remove(te);
    }
    Assert.assertTrue(resourceIncompatible.isEmpty());
    final Collection<CompatibilityInfo> modelIncompatible = new HashSet<CompatibilityInfo>(checker.getInfoMap().getModelIncompatibles());
    for (CompatibilityInfo te : modelTestErrors) {
        Assert.assertTrue(modelIncompatible.contains(te), "Reported model incompatibles should contain: " + te.toString());
        modelIncompatible.remove(te);
    }
    Assert.assertTrue(modelIncompatible.isEmpty());
// ignore compatibles
}
Also used : ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) StringArray(com.linkedin.data.template.StringArray) CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 25 with StringArray

use of com.linkedin.data.template.StringArray in project rest.li by linkedin.

the class TestResourceCompatibilityChecker method testFailAssociationFile.

@Test
public void testFailAssociationFile() throws IOException {
    final AssocKeySchema prevAssocKey = new AssocKeySchema();
    prevAssocKey.setName("key1");
    prevAssocKey.setType("string");
    final Collection<CompatibilityInfo> testErrors = new HashSet<CompatibilityInfo>();
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "assocKeys"), CompatibilityInfo.Type.ARRAY_NOT_EQUAL, new AssocKeySchemaArray(Arrays.asList(prevAssocKey))));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "supports"), CompatibilityInfo.Type.ARRAY_NOT_CONTAIN, new StringArray(Arrays.asList("create", "get"))));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "methods", "create", "parameters"), CompatibilityInfo.Type.PARAMETER_NEW_REQUIRED, "data"));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "methods"), CompatibilityInfo.Type.ARRAY_MISSING_ELEMENT, "get"));
    testErrors.add(new CompatibilityInfo(Arrays.<Object>asList("", "association", "entity", "path"), CompatibilityInfo.Type.VALUE_NOT_EQUAL, "/greetings/assoc/{greetingsId}", "/greetings/association/{greetingsId}"));
    final ResourceSchema prevResource = idlToResource(IDLS_SUFFIX + PREV_ASSOC_FILE);
    final ResourceSchema currResource = idlToResource(IDLS_SUFFIX + CURR_ASSOC_FAIL_FILE);
    ResourceCompatibilityChecker checker = new ResourceCompatibilityChecker(prevResource, prevSchemaResolver, currResource, prevSchemaResolver);
    Assert.assertFalse(checker.check(CompatibilityLevel.BACKWARDS));
    final Collection<CompatibilityInfo> incompatibles = new HashSet<CompatibilityInfo>(checker.getInfoMap().getIncompatibles());
    for (CompatibilityInfo te : testErrors) {
        Assert.assertTrue(incompatibles.contains(te), "Reported incompatibles should contain: " + te.toString());
        incompatibles.remove(te);
    }
    Assert.assertTrue(incompatibles.isEmpty());
}
Also used : AssocKeySchemaArray(com.linkedin.restli.restspec.AssocKeySchemaArray) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) StringArray(com.linkedin.data.template.StringArray) CompatibilityInfo(com.linkedin.restli.tools.idlcheck.CompatibilityInfo) AssocKeySchema(com.linkedin.restli.restspec.AssocKeySchema) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

StringArray (com.linkedin.data.template.StringArray)25 Test (org.testng.annotations.Test)12 ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)5 HashMap (java.util.HashMap)5 DataMap (com.linkedin.data.DataMap)4 ArrayList (java.util.ArrayList)4 ResourceSchema (com.linkedin.restli.restspec.ResourceSchema)3 D2LoadBalancerStrategyProperties (com.linkedin.d2.D2LoadBalancerStrategyProperties)2 com.linkedin.d2.hashConfigType (com.linkedin.d2.hashConfigType)2 com.linkedin.d2.quarantineInfo (com.linkedin.d2.quarantineInfo)2 DataList (com.linkedin.data.DataList)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2 DataSchema (com.linkedin.data.schema.DataSchema)2 MapDataSchema (com.linkedin.data.schema.MapDataSchema)2 ValidationOptions (com.linkedin.data.schema.validation.ValidationOptions)2 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)2 BooleanArray (com.linkedin.data.template.BooleanArray)2 BytesArray (com.linkedin.data.template.BytesArray)2 DoubleArray (com.linkedin.data.template.DoubleArray)2 FloatArray (com.linkedin.data.template.FloatArray)2