From 809519f9d8baa8522d73dfcb7a590c4373480f93 Mon Sep 17 00:00:00 2001 From: Christian Amend Date: Wed, 2 Apr 2014 17:21:18 +0200 Subject: [PATCH] [OLINGO-168] Tests for metadata serialization --- .../core/edm/AbstractEdmComplexType.java | 63 +++--- .../core/edm/AbstractEdmEntityType.java | 16 +- .../edm/AbstractEdmNavigationProperty.java | 17 +- .../core/edm/AbstractEdmStructuredType.java | 198 +++++++++--------- .../olingo/commons/core/edm/EdmTypeImpl.java | 45 ++-- .../core/edm/provider/EdmComplexTypeImpl.java | 2 - .../core/edm/provider/EdmEntityTypeImpl.java | 45 ++-- .../edm/provider/EdmComplexTypeImplTest.java | 12 +- .../edm/provider/EdmEntityTypeImplTest.java | 11 +- .../core/edm/provider/EdmSchemaImplTest.java | 6 +- .../serializer/xml/MetadataDocumentTest.java | 197 ++++++++++++++++- 11 files changed, 418 insertions(+), 194 deletions(-) diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmComplexType.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmComplexType.java index 732f29fea..541058255 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmComplexType.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmComplexType.java @@ -1,18 +1,18 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -27,29 +27,36 @@ import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; public abstract class AbstractEdmComplexType extends AbstractEdmStructuredType implements EdmComplexType { - public AbstractEdmComplexType( - final Edm edm, - final FullQualifiedName fqn, - final FullQualifiedName baseTypeName) { - super(edm, fqn, EdmTypeKind.COMPLEX, baseTypeName); - } + public AbstractEdmComplexType( + final Edm edm, + final FullQualifiedName typeName, + final FullQualifiedName baseTypeName) { + super(edm, typeName, EdmTypeKind.COMPLEX, baseTypeName); + } - @Override - protected EdmStructuredType buildBaseType(final FullQualifiedName baseTypeName) { - // TODO: check for comment - EdmComplexType baseType = null; - if (baseTypeName != null) { - baseType = edm.getComplexType(baseTypeName); - if (baseType == null) { - throw new EdmException("Can't find base type with name: " + baseTypeName + " for complex type: " - + getName()); - } - } - return baseType; + @Override + protected EdmStructuredType buildBaseType(final FullQualifiedName baseTypeName) { + EdmComplexType baseType = null; + if (baseTypeName != null) { + baseType = edm.getComplexType(baseTypeName); + if (baseType == null) { + throw new EdmException("Can't find base type with name: " + baseTypeName + " for complex type: " + + getName()); + } } + return baseType; + } - @Override - public EdmComplexType getBaseType() { - return (EdmComplexType) baseType; + @Override + public EdmComplexType getBaseType() { + checkBaseType(); + return (EdmComplexType) baseType; + } + + @Override + protected void checkBaseType() { + if (baseTypeName != null && baseType == null) { + baseType = buildBaseType(baseTypeName); } + } } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmEntityType.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmEntityType.java index eb9d170b4..60af07aaa 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmEntityType.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmEntityType.java @@ -34,19 +34,15 @@ import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; public abstract class AbstractEdmEntityType extends AbstractEdmStructuredType implements EdmEntityType { private final boolean hasStream; - protected EdmEntityType entityBaseType; - private final List keyPredicateNames = new ArrayList(); - private final Map keyPropertyRefs = new LinkedHashMap(); - private List keyPropertyRefsList; - protected AbstractEdmEntityType(final Edm edm, final FullQualifiedName fqn, final FullQualifiedName baseTypeName, + protected AbstractEdmEntityType(final Edm edm, final FullQualifiedName typeName, final FullQualifiedName baseTypeName, final boolean hashStream) { - super(edm, fqn, EdmTypeKind.ENTITY, baseTypeName); + super(edm, typeName, EdmTypeKind.ENTITY, baseTypeName); this.hasStream = hashStream; } @@ -76,11 +72,13 @@ public abstract class AbstractEdmEntityType extends AbstractEdmStructuredType im @Override public EdmEntityType getBaseType() { + checkBaseType(); return entityBaseType; } @Override public List getKeyPredicateNames() { + checkBaseType(); if (keyPredicateNames.isEmpty() && baseType != null) { return entityBaseType.getKeyPredicateNames(); } @@ -89,6 +87,7 @@ public abstract class AbstractEdmEntityType extends AbstractEdmStructuredType im @Override public List getKeyPropertyRefs() { + checkBaseType(); if (keyPropertyRefsList == null) { keyPropertyRefsList = new ArrayList(keyPropertyRefs.values()); } @@ -100,6 +99,7 @@ public abstract class AbstractEdmEntityType extends AbstractEdmStructuredType im @Override public EdmKeyPropertyRef getKeyPropertyRef(final String keyPredicateName) { + checkBaseType(); final EdmKeyPropertyRef edmKeyPropertyRef = keyPropertyRefs.get(keyPredicateName); if (edmKeyPropertyRef == null && entityBaseType != null) { return entityBaseType.getKeyPropertyRef(keyPredicateName); @@ -111,4 +111,8 @@ public abstract class AbstractEdmEntityType extends AbstractEdmStructuredType im public boolean hasStream() { return hasStream; } + + protected void checkBaseType() { + //Current Client implementation doesn`t need this so I implemented an empty body here. + } } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmNavigationProperty.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmNavigationProperty.java index dca80bd2e..0d97ba645 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmNavigationProperty.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmNavigationProperty.java @@ -1,18 +1,18 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -62,7 +62,8 @@ public abstract class AbstractEdmNavigationProperty extends EdmElementImpl imple for (String element : split) { property = type.getNavigationProperty(element); if (property == null) { - throw new EdmException("Cannot find property with name: " + element + " at type " + type.getName()); + throw new EdmException("Cannot find navigation property with name: " + element + + " at type " + type.getName()); } type = (EdmStructuredType) property.getType(); } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmStructuredType.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmStructuredType.java index a807052bb..a4cd47dc4 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmStructuredType.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/AbstractEdmStructuredType.java @@ -1,18 +1,18 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -34,99 +34,105 @@ import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; public abstract class AbstractEdmStructuredType extends EdmTypeImpl implements EdmStructuredType { - protected EdmStructuredType baseType; + protected EdmStructuredType baseType; + protected FullQualifiedName baseTypeName; + private List propertyNames; + private List navigationPropertyNames; - private List propertyNames; + public AbstractEdmStructuredType( + final Edm edm, + final FullQualifiedName typeName, + final EdmTypeKind kind, + final FullQualifiedName baseTypeName) { - private List navigationPropertyNames; + super(edm, typeName, kind); + this.baseTypeName = baseTypeName; + } - public AbstractEdmStructuredType( - final Edm edm, - final FullQualifiedName fqn, - final EdmTypeKind kind, - final FullQualifiedName baseTypeName) { + protected abstract EdmStructuredType buildBaseType(FullQualifiedName baseTypeName); - super(edm, fqn, kind); + protected abstract Map getProperties(); + + protected abstract Map getNavigationProperties(); + + protected abstract void checkBaseType(); + + @Override + public List getPropertyNames() { + if (propertyNames == null) { + propertyNames = new ArrayList(); + checkBaseType(); + if (baseType != null) { + propertyNames.addAll(baseType.getPropertyNames()); + } + propertyNames.addAll(getProperties().keySet()); + } + return propertyNames; + } + + @Override + public List getNavigationPropertyNames() { + if (navigationPropertyNames == null) { + navigationPropertyNames = new ArrayList(); + checkBaseType(); + if (baseType != null) { + navigationPropertyNames.addAll(baseType.getNavigationPropertyNames()); + } + navigationPropertyNames.addAll(getNavigationProperties().keySet()); + } + return navigationPropertyNames; + } + + @Override + public EdmElement getProperty(final String name) { + EdmElement property = getStructuralProperty(name); + if (property == null) { + property = getNavigationProperty(name); + } + return property; + } + + @Override + public EdmProperty getStructuralProperty(final String name) { + EdmProperty property = null; + checkBaseType(); + if (baseType != null) { + property = baseType.getStructuralProperty(name); + } + if (property == null) { + property = getProperties().get(name); + } + return property; + } + + @Override + public EdmNavigationProperty getNavigationProperty(final String name) { + EdmNavigationProperty property = null; + checkBaseType(); + if (baseType != null) { + property = baseType.getNavigationProperty(name); + } + if (property == null) { + property = getNavigationProperties().get(name); + } + return property; + } + + @Override + public boolean compatibleTo(final EdmType targetType) { + EdmStructuredType sourceType = this; + if (targetType == null) { + throw new EdmException("Target type must not be null"); + } + while (!sourceType.getName().equals(targetType.getName()) + || !sourceType.getNamespace().equals(targetType.getNamespace())) { + + sourceType = sourceType.getBaseType(); + if (sourceType == null) { + return false; + } } - protected abstract EdmStructuredType buildBaseType(FullQualifiedName baseTypeName); - - protected abstract Map getProperties(); - - protected abstract Map getNavigationProperties(); - - @Override - public List getPropertyNames() { - if (propertyNames == null) { - propertyNames = new ArrayList(); - if (baseType != null) { - propertyNames.addAll(baseType.getPropertyNames()); - } - propertyNames.addAll(getProperties().keySet()); - } - return propertyNames; - } - - @Override - public List getNavigationPropertyNames() { - if (navigationPropertyNames == null) { - navigationPropertyNames = new ArrayList(); - if (baseType != null) { - navigationPropertyNames.addAll(baseType.getNavigationPropertyNames()); - } - navigationPropertyNames.addAll(getNavigationProperties().keySet()); - } - return navigationPropertyNames; - } - - @Override - public EdmElement getProperty(final String name) { - EdmElement property = getStructuralProperty(name); - if (property == null) { - property = getNavigationProperty(name); - } - return property; - } - - @Override - public EdmProperty getStructuralProperty(final String name) { - EdmProperty property = null; - if (baseType != null) { - property = baseType.getStructuralProperty(name); - } - if (property == null) { - property = getProperties().get(name); - } - return property; - } - - @Override - public EdmNavigationProperty getNavigationProperty(final String name) { - EdmNavigationProperty property = null; - if (baseType != null) { - property = baseType.getNavigationProperty(name); - } - if (property == null) { - property = getNavigationProperties().get(name); - } - return property; - } - - @Override - public boolean compatibleTo(final EdmType targetType) { - EdmStructuredType sourceType = this; - if (targetType == null) { - throw new EdmException("Target type must not be null"); - } - while (!sourceType.getName().equals(targetType.getName()) - || !sourceType.getNamespace().equals(targetType.getNamespace())) { - - sourceType = sourceType.getBaseType(); - if (sourceType == null) { - return false; - } - } - - return true; - } + return true; + } } diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeImpl.java index ed20afa95..6b9e7cb5f 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeImpl.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmTypeImpl.java @@ -1,18 +1,18 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file + * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the + * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ @@ -25,23 +25,22 @@ import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; public class EdmTypeImpl extends EdmNamedImpl implements EdmType { - protected final FullQualifiedName fqn; + protected final FullQualifiedName typeName; + protected final EdmTypeKind kind; - protected final EdmTypeKind kind; + public EdmTypeImpl(final Edm edm, final FullQualifiedName typeName, final EdmTypeKind kind) { + super(edm, typeName.getName()); + this.typeName = typeName; + this.kind = kind; + } - public EdmTypeImpl(final Edm edm, final FullQualifiedName fqn, final EdmTypeKind kind) { - super(edm, fqn.getName()); - this.fqn = fqn; - this.kind = kind; - } + @Override + public String getNamespace() { + return typeName.getNamespace(); + } - @Override - public String getNamespace() { - return fqn.getNamespace(); - } - - @Override - public EdmTypeKind getKind() { - return kind; - } + @Override + public EdmTypeKind getKind() { + return kind; + } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImpl.java index c818c5804..34df6700f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImpl.java @@ -36,8 +36,6 @@ public class EdmComplexTypeImpl extends AbstractEdmComplexType { final Edm edm, final FullQualifiedName name, final ComplexType complexType) { final EdmComplexTypeImpl instance = new EdmComplexTypeImpl(edm, name, complexType); - instance.baseType = instance.buildBaseType(complexType.getBaseType()); - return instance; } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImpl.java index 774af987b..021d3bd01 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImpl.java @@ -36,34 +36,19 @@ import org.apache.olingo.server.api.edm.provider.PropertyRef; public class EdmEntityTypeImpl extends AbstractEdmEntityType { private final EdmStructuredTypeHelper helper; + private EntityType entityType; + private boolean baseTypeChecked = false; public static EdmEntityTypeImpl getInstance(final Edm edm, final FullQualifiedName name, final EntityType entityType) { final EdmEntityTypeImpl instance = new EdmEntityTypeImpl(edm, name, entityType); - instance.baseType = instance.buildBaseType(entityType.getBaseType()); - - if (instance.baseType == null) { - instance.entityBaseType = null; - - final List key = entityType.getKey(); - if (key != null) { - final List edmKey = new ArrayList(); - for (PropertyRef ref : key) { - edmKey.add(new EdmKeyPropertyRefImpl(instance, ref)); - } - instance.setEdmKeyPropertyRef(edmKey); - } - } else { - instance.entityBaseType = (EdmEntityType) instance.baseType; - } - return instance; } private EdmEntityTypeImpl(final Edm edm, final FullQualifiedName name, final EntityType entityType) { super(edm, name, entityType.getBaseType(), entityType.hasStream()); - + this.entityType = entityType; helper = new EdmStructuredTypeHelperImpl(edm, entityType); } @@ -77,4 +62,28 @@ public class EdmEntityTypeImpl extends AbstractEdmEntityType { return helper.getNavigationProperties(); } + @Override + protected void checkBaseType() { + if (!baseTypeChecked) { + if (baseTypeName != null) { + baseType = buildBaseType(baseTypeName); + } + if (baseType == null) { + entityBaseType = null; + + final List key = entityType.getKey(); + if (key != null) { + final List edmKey = new ArrayList(); + for (PropertyRef ref : key) { + edmKey.add(new EdmKeyPropertyRefImpl(this, ref)); + } + setEdmKeyPropertyRef(edmKey); + } + } else { + entityBaseType = (EdmEntityType) baseType; + } + baseTypeChecked = true; + } + } + } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java index 5db5e9890..cc3586d0f 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java @@ -59,7 +59,7 @@ public class EdmComplexTypeImplTest { List baseNavigationProperties = new ArrayList(); baseNavigationProperties.add(new NavigationProperty().setName("nav1")); baseComplexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(baseProperties) - .setNavigationProperties(baseNavigationProperties); + .setNavigationProperties(baseNavigationProperties); when(provider.getComplexType(baseName)).thenReturn(baseComplexType); baseType = EdmComplexTypeImpl.getInstance(edm, baseName, baseComplexType); @@ -71,7 +71,7 @@ public class EdmComplexTypeImplTest { List navigationProperties = new ArrayList(); navigationProperties.add(new NavigationProperty().setName("nav2")); complexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(properties) - .setNavigationProperties(navigationProperties); + .setNavigationProperties(navigationProperties); when(provider.getComplexType(name)).thenReturn(complexType); type = EdmComplexTypeImpl.getInstance(edm, name, complexType); @@ -155,10 +155,12 @@ public class EdmComplexTypeImplTest { EdmProvider provider = mock(EdmProvider.class); EdmProviderImpl edm = new EdmProviderImpl(provider); FullQualifiedName typeWithNonexistingBaseTypeName = new FullQualifiedName("namespace", "typeName"); - ComplexType complexTypeForNonexistingBaseType - = new ComplexType().setBaseType(new FullQualifiedName("wrong", "wrong")); + ComplexType complexTypeForNonexistingBaseType = + new ComplexType().setBaseType(new FullQualifiedName("wrong", "wrong")); complexTypeForNonexistingBaseType.setName("typeName"); when(provider.getComplexType(typeWithNonexistingBaseTypeName)).thenReturn(complexTypeForNonexistingBaseType); - EdmComplexTypeImpl.getInstance(edm, typeWithNonexistingBaseTypeName, complexTypeForNonexistingBaseType); + EdmComplexTypeImpl instance = + EdmComplexTypeImpl.getInstance(edm, typeWithNonexistingBaseTypeName, complexTypeForNonexistingBaseType); + instance.getBaseType(); } } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java index eeb8313a5..91a601898 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java @@ -96,21 +96,21 @@ public class EdmEntityTypeImplTest { typeWithComplexKeyProvider.setName(typeWithComplexKeyName.getName()); List typeWithComplexKeyProperties = new ArrayList(); typeWithComplexKeyProperties.add(new Property().setName("Id").setType( - EdmPrimitiveTypeKind.String.getFullQualifiedName())); + EdmPrimitiveTypeKind.String.getFullQualifiedName())); List complexTypeProperties = new ArrayList(); complexTypeProperties.add(new Property().setName("ComplexPropName").setType( - EdmPrimitiveTypeKind.String.getFullQualifiedName())); + EdmPrimitiveTypeKind.String.getFullQualifiedName())); FullQualifiedName complexTypeName = new FullQualifiedName("namespace", "complexTypeName"); when(provider.getComplexType(complexTypeName)).thenReturn( - new ComplexType().setName("complexTypeName").setProperties(complexTypeProperties)); + new ComplexType().setName("complexTypeName").setProperties(complexTypeProperties)); typeWithComplexKeyProperties.add(new Property().setName("Comp").setType(complexTypeName)); typeWithComplexKeyProvider.setProperties(typeWithComplexKeyProperties); List keyForTypeWithComplexKey = new ArrayList(); keyForTypeWithComplexKey.add(new PropertyRef().setPropertyName("Id")); keyForTypeWithComplexKey.add(new PropertyRef().setPropertyName("ComplexPropName").setAlias("alias").setPath( - "Comp/ComplexPropName")); + "Comp/ComplexPropName")); typeWithComplexKeyProvider.setKey(keyForTypeWithComplexKey); when(provider.getEntityType(typeWithComplexKeyName)).thenReturn(typeWithComplexKeyProvider); @@ -255,7 +255,8 @@ public class EdmEntityTypeImplTest { public void invalidBaseType() { EdmProviderImpl edm = mock(EdmProviderImpl.class); EntityType entityType = new EntityType().setName("n").setBaseType(new FullQualifiedName("wrong", "wrong")); - EdmEntityTypeImpl.getInstance(edm, new FullQualifiedName("n", "n"), entityType); + EdmEntityTypeImpl instance = EdmEntityTypeImpl.getInstance(edm, new FullQualifiedName("n", "n"), entityType); + instance.getBaseType(); } @Test diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmSchemaImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmSchemaImplTest.java index f227796d9..5575fefa9 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmSchemaImplTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmSchemaImplTest.java @@ -322,12 +322,14 @@ public class EdmSchemaImplTest { List entityTypes = new ArrayList(); entityTypes.add(new EntityType().setName("entityType1")); - entityTypes.add(new EntityType().setName("entityType2")); + entityTypes.add(new EntityType().setName("entityType2") + .setBaseType(new FullQualifiedName("namespace", "entityType1"))); providerSchema.setEntityTypes(entityTypes); List complexTypes = new ArrayList(); complexTypes.add(new ComplexType().setName("complexType1")); - complexTypes.add(new ComplexType().setName("complexType2")); + complexTypes.add(new ComplexType().setName("complexType2").setBaseType( + new FullQualifiedName("namespace", "complexType1"))); providerSchema.setComplexTypes(complexTypes); List actions = new ArrayList(); diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java index 9d08914fb..6a1936451 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java @@ -18,13 +18,41 @@ */ package org.apache.olingo.server.core.serializer.xml; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.olingo.commons.api.ODataException; import org.apache.olingo.commons.api.ODataRuntimeException; import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.Target; import org.apache.olingo.server.api.ODataServer; +import org.apache.olingo.server.api.edm.provider.Action; +import org.apache.olingo.server.api.edm.provider.ActionImport; +import org.apache.olingo.server.api.edm.provider.ComplexType; +import org.apache.olingo.server.api.edm.provider.EdmProvider; +import org.apache.olingo.server.api.edm.provider.EntityContainer; +import org.apache.olingo.server.api.edm.provider.EntitySet; +import org.apache.olingo.server.api.edm.provider.EntityType; +import org.apache.olingo.server.api.edm.provider.EnumMember; +import org.apache.olingo.server.api.edm.provider.EnumType; +import org.apache.olingo.server.api.edm.provider.Function; +import org.apache.olingo.server.api.edm.provider.FunctionImport; +import org.apache.olingo.server.api.edm.provider.NavigationProperty; +import org.apache.olingo.server.api.edm.provider.NavigationPropertyBinding; +import org.apache.olingo.server.api.edm.provider.Parameter; +import org.apache.olingo.server.api.edm.provider.Property; +import org.apache.olingo.server.api.edm.provider.ReturnType; +import org.apache.olingo.server.api.edm.provider.Schema; +import org.apache.olingo.server.api.edm.provider.Singleton; +import org.apache.olingo.server.api.edm.provider.TypeDefinition; import org.apache.olingo.server.api.serializer.ODataFormat; import org.apache.olingo.server.api.serializer.ODataSerializer; import org.apache.olingo.server.core.edm.provider.EdmProviderImpl; @@ -47,11 +75,178 @@ public class MetadataDocumentTest { serializer.metadataDocument(edm); } + @Test + public void writeMetadataWithLocalTestEdm() { + ODataSerializer serializer = ODataServer.newInstance().getSerializer(ODataFormat.XML); + Edm edm = new EdmProviderImpl(new TestMetadataProvider()); + InputStream metadata = serializer.metadataDocument(edm); + assertNotNull(metadata); + String metadataString = StringUtils.inputStreamToString(metadata, false); + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString.contains("")); + + assertTrue(metadataString.contains("")); + + assertTrue(metadataString + .contains("" + + "" + + "")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString.contains("")); + + assertTrue(metadataString + .contains("")); + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString.contains("")); + + assertTrue(metadataString + .contains("")); + + assertTrue(metadataString.contains("")); + } + @Test public void writeMetadataWithTechnicalScenario() { ODataSerializer serializer = ODataServer.newInstance().getSerializer(ODataFormat.XML); EdmProviderImpl edm = new EdmProviderImpl(new EdmTechProvider()); InputStream metadata = serializer.metadataDocument(edm); - StringUtils.inputStreamToString(metadata, false); + assertNotNull(metadata); + // The technical scenario is too big to verify. We are content for now to make sure we can serialize it. + // System.out.println(StringUtils.inputStreamToString(metadata, false)); + } + + private class TestMetadataProvider extends EdmProvider { + + @Override + public List getSchemas() throws ODataException { + Property p1 = new Property().setName("P1").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName()); + String ns = "namespace"; + NavigationProperty n1 = new NavigationProperty().setName("N1") + .setType(new FullQualifiedName(ns, "ETBaseName")).setNullable(true).setPartner("N1"); + Property p2 = new Property().setName("P2").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName()); + NavigationProperty n2 = new NavigationProperty().setName("N2") + .setType(new FullQualifiedName(ns, "ETDerivedName")).setNullable(true).setPartner("N2"); + Schema schema = new Schema().setNamespace(ns).setAlias("alias"); + List complexTypes = new ArrayList(); + schema.setComplexTypes(complexTypes); + ComplexType ctBase = + new ComplexType().setName("CTBaseName").setProperties(Arrays.asList(p1)).setNavigationProperties( + Arrays.asList(n1)); + complexTypes.add(ctBase); + ComplexType ctDerived = + new ComplexType().setName("CTDerivedName").setBaseType(new FullQualifiedName(ns, "CTBaseName")) + .setProperties(Arrays.asList(p2)).setNavigationProperties(Arrays.asList(n2)); + complexTypes.add(ctDerived); + + List entityTypes = new ArrayList(); + schema.setEntityTypes(entityTypes); + EntityType etBase = + new EntityType().setName("ETBaseName").setProperties(Arrays.asList(p1)).setNavigationProperties( + Arrays.asList(n1)); + entityTypes.add(etBase); + EntityType etDerived = + new EntityType().setName("ETDerivedName").setBaseType(new FullQualifiedName(ns, "ETBaseName")) + .setProperties(Arrays.asList(p2)).setNavigationProperties(Arrays.asList(n2)); + entityTypes.add(etDerived); + + List actions = new ArrayList(); + schema.setActions(actions); + // TODO:EntitySetPath + actions.add((new Action().setName("ActionWOParameter"))); + List parameters = new ArrayList(); + parameters.add(new Parameter().setName("param").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName())); + parameters.add(new Parameter().setName("param2").setType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName()) + .setCollection(true)); + actions.add(new Action().setName("ActionName").setBound(true).setParameters(parameters).setReturnType( + new ReturnType().setType(new FullQualifiedName(ns, "CTBaseName")))); + + List functions = new ArrayList(); + schema.setFunctions(functions); + functions.add((new Function().setName("FunctionWOParameter") + .setReturnType(new ReturnType().setType(new FullQualifiedName(ns, "CTBaseName"))))); + functions.add(new Function().setName("FunctionName").setBound(true).setParameters(parameters).setReturnType( + new ReturnType().setType(new FullQualifiedName(ns, "CTBaseName")))); + + List enumTypes = new ArrayList(); + schema.setEnumTypes(enumTypes); + List members = new ArrayList(); + members.add(new EnumMember().setName("member").setValue("1")); + enumTypes.add(new EnumType().setName("EnumName").setFlags(true).setMembers(members)); + + List typeDefinitions = new ArrayList(); + schema.setTypeDefinitions(typeDefinitions); + typeDefinitions.add(new TypeDefinition().setName("typeDef") + .setUnderlyingType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName())); + + EntityContainer container = new EntityContainer().setName("container"); + schema.setEntityContainer(container); + + List actionImports = new ArrayList(); + container.setActionImports(actionImports); + actionImports.add(new ActionImport().setName("actionImport").setAction( + new FullQualifiedName(ns, "ActionWOParameter")).setEntitySet( + new Target().setEntityContainer(new FullQualifiedName(ns, "container")).setTargetName("EntitySetName"))); + + List functionImports = new ArrayList(); + container.setFunctionImports(functionImports); + functionImports.add(new FunctionImport().setName("actionImport").setFunction( + new FullQualifiedName(ns, "FunctionName")).setEntitySet( + new Target().setEntityContainer(new FullQualifiedName(ns, "container")).setTargetName("EntitySetName"))); + + List entitySets = new ArrayList(); + container.setEntitySets(entitySets); + List nPB = new ArrayList(); + nPB.add(new NavigationPropertyBinding().setPath("N1").setTarget( + new Target().setEntityContainer(new FullQualifiedName(ns, "container")).setTargetName("EntitySetName"))); + entitySets.add(new EntitySet().setName("EntitySetName").setType(new FullQualifiedName(ns, "ETBaseName")) + .setNavigationPropertyBindings(nPB)); + + List singletons = new ArrayList(); + container.setSingletons(singletons); + singletons.add(new Singleton().setName("SingletonName").setType(new FullQualifiedName(ns, "ETBaseName")) + .setNavigationPropertyBindings(nPB)); + + List schemas = new ArrayList(); + schemas.add(schema); + return schemas; + } } }