[OLINGO-786] Move edm tests to commons core Tests for the edm only depend on the commons part so they should be there as well.
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/9c1981c4 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/9c1981c4 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/9c1981c4 Branch: refs/heads/olingo786 Commit: 9c1981c410388cf0fdb035730222fbcde7f52465 Parents: 546cf34 Author: Christian Amend <[email protected]> Authored: Thu Oct 8 13:45:16 2015 +0200 Committer: Christian Amend <[email protected]> Committed: Thu Oct 8 13:45:16 2015 +0200 ---------------------------------------------------------------------- .../core/edm/provider/AbstractEdmNamedTest.java | 59 +++ .../core/edm/provider/EdmActionImplTest.java | 156 ++++++++ .../edm/provider/EdmActionImportImplTest.java | 104 +++++ .../edm/provider/EdmComplexTypeImplTest.java | 169 ++++++++ .../provider/EdmEntityContainerImplTest.java | 299 ++++++++++++++ .../core/edm/provider/EdmEntitySetImplTest.java | 83 ++++ .../edm/provider/EdmEntityTypeImplTest.java | 391 +++++++++++++++++++ .../server/core/edm/provider/EdmEnumTest.java | 372 ++++++++++++++++++ .../core/edm/provider/EdmFunctionImplTest.java | 78 ++++ .../edm/provider/EdmFunctionImportImplTest.java | 88 +++++ .../edm/provider/EdmKeyPropertyRefImplTest.java | 141 +++++++ .../core/edm/provider/EdmMappingTest.java | 72 ++++ .../core/edm/provider/EdmMemberImplTest.java | 40 ++ .../provider/EdmNavigationPropertyImplTest.java | 146 +++++++ .../core/edm/provider/EdmParameterImplTest.java | 143 +++++++ .../core/edm/provider/EdmPropertyImplTest.java | 158 ++++++++ .../EdmProviderImplOverloadingTest.java | 199 ++++++++++ .../core/edm/provider/EdmProviderImplTest.java | 225 +++++++++++ .../edm/provider/EdmReturnTypeImplTest.java | 134 +++++++ .../core/edm/provider/EdmSchemaImplTest.java | 363 +++++++++++++++++ .../core/edm/provider/EdmSingletonImplTest.java | 131 +++++++ .../edm/provider/EdmTypeDefinitionImplTest.java | 77 ++++ .../core/edm/provider/EdmTypeImplTest.java | 62 +++ .../core/edm/provider/AbstractEdmNamedTest.java | 59 --- .../core/edm/provider/EdmActionImplTest.java | 156 -------- .../edm/provider/EdmActionImportImplTest.java | 104 ----- .../edm/provider/EdmComplexTypeImplTest.java | 169 -------- .../provider/EdmEntityContainerImplTest.java | 299 -------------- .../core/edm/provider/EdmEntitySetImplTest.java | 83 ---- .../edm/provider/EdmEntityTypeImplTest.java | 391 ------------------- .../server/core/edm/provider/EdmEnumTest.java | 372 ------------------ .../core/edm/provider/EdmFunctionImplTest.java | 78 ---- .../edm/provider/EdmFunctionImportImplTest.java | 88 ----- .../edm/provider/EdmKeyPropertyRefImplTest.java | 141 ------- .../core/edm/provider/EdmMappingTest.java | 72 ---- .../core/edm/provider/EdmMemberImplTest.java | 40 -- .../provider/EdmNavigationPropertyImplTest.java | 146 ------- .../core/edm/provider/EdmParameterImplTest.java | 143 ------- .../core/edm/provider/EdmPropertyImplTest.java | 158 -------- .../EdmProviderImplOverloadingTest.java | 199 ---------- .../core/edm/provider/EdmProviderImplTest.java | 225 ----------- .../edm/provider/EdmReturnTypeImplTest.java | 134 ------- .../core/edm/provider/EdmSchemaImplTest.java | 363 ----------------- .../core/edm/provider/EdmSingletonImplTest.java | 131 ------- .../edm/provider/EdmTypeDefinitionImplTest.java | 77 ---- .../core/edm/provider/EdmTypeImplTest.java | 62 --- 46 files changed, 3690 insertions(+), 3690 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/AbstractEdmNamedTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/AbstractEdmNamedTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/AbstractEdmNamedTest.java new file mode 100644 index 0000000..5e12b7c --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/AbstractEdmNamedTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Arrays; +import java.util.List; + +import org.apache.olingo.commons.api.edm.EdmAnnotatable; +import org.apache.olingo.commons.api.edm.EdmNamed; +import org.apache.olingo.commons.api.edm.provider.CsdlAnnotatable; +import org.apache.olingo.commons.api.edm.provider.CsdlAnnotation; +import org.apache.olingo.commons.core.edm.AbstractEdmNamed; +import org.junit.Test; + +public class AbstractEdmNamedTest { + + @Test + public void getNameTest() { + EdmNamed obj = new EdmNamedImplTester("Name"); + assertEquals("Name", obj.getName()); + EdmAnnotatable an = (EdmAnnotatable) obj; + assertNotNull(an.getAnnotations().get(0)); + } + + private class EdmNamedImplTester extends AbstractEdmNamed { + + public EdmNamedImplTester(final String name) { + super(null, name, new AnnoTester()); + } + } + + private class AnnoTester implements CsdlAnnotatable { + @Override + public List<CsdlAnnotation> getAnnotations() { + CsdlAnnotation annotation = new CsdlAnnotation(); + annotation.setTerm("NS.SimpleTerm"); + return Arrays.asList(annotation); + } + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImplTest.java new file mode 100644 index 0000000..86bd9f4 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImplTest.java @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.commons.api.edm.EdmAction; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmParameter; +import org.apache.olingo.commons.api.edm.EdmSingleton; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; +import org.apache.olingo.commons.api.edm.provider.CsdlAction; +import org.apache.olingo.commons.api.edm.provider.CsdlParameter; +import org.apache.olingo.commons.api.edm.provider.CsdlReturnType; +import org.apache.olingo.commons.core.edm.EdmActionImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.junit.Before; +import org.junit.Test; + +public class EdmActionImplTest { + + private EdmAction actionImpl1; + private EdmAction actionImpl2; + private EdmAction actionImpl3; + + @Before + public void setup() { + EdmProviderImpl provider = mock(EdmProviderImpl.class); + List<CsdlParameter> parameters = new ArrayList<CsdlParameter>(); + parameters.add(new CsdlParameter().setName("Id").setType(new FullQualifiedName("namespace", "name"))); + FullQualifiedName action1Name = new FullQualifiedName("namespace", "action1"); + CsdlAction action1 = new CsdlAction().setName("action1").setBound(true).setParameters(parameters); + actionImpl1 = new EdmActionImpl(provider, action1Name, action1); + + FullQualifiedName action2Name = new FullQualifiedName("namespace", "action2"); + FullQualifiedName returnTypeName = new FullQualifiedName("Edm", "String"); + CsdlReturnType returnType = new CsdlReturnType().setType(returnTypeName); + CsdlAction action2 = new CsdlAction().setName("action2").setParameters(parameters).setReturnType(returnType); + actionImpl2 = new EdmActionImpl(provider, action2Name, action2); + + FullQualifiedName action3Name = new FullQualifiedName("namespace", "action3"); + CsdlAction action3 = + new CsdlAction().setName("action3").setParameters(parameters).setReturnType(returnType).setEntitySetPath( + "path/Id"); + actionImpl3 = new EdmActionImpl(provider, action3Name, action3); + } + + @Test + public void action1BasicMethodCalls() { + assertTrue(actionImpl1.isBound()); + assertEquals(EdmTypeKind.ACTION, actionImpl1.getKind()); + assertNull(actionImpl1.getReturnType()); + // assertEquals("returnName", actionImpl1.getReturnType().getType().getName()); + assertNotNull(actionImpl1.getParameterNames()); + + for (String name : actionImpl1.getParameterNames()) { + EdmParameter parameter = actionImpl1.getParameter(name); + assertNotNull(parameter); + assertEquals(name, parameter.getName()); + } + + assertNull(actionImpl1.getReturnedEntitySet(null)); + assertNull(actionImpl1.getReturnedEntitySet(mock(EdmEntitySet.class))); + } + + @Test + public void action2BasicMethodCalls() { + assertFalse(actionImpl2.isBound()); + assertEquals(EdmTypeKind.ACTION, actionImpl2.getKind()); + assertEquals("String", actionImpl2.getReturnType().getType().getName()); + assertNotNull(actionImpl2.getParameterNames()); + + for (String name : actionImpl2.getParameterNames()) { + EdmParameter parameter = actionImpl2.getParameter(name); + assertNotNull(parameter); + assertEquals(name, parameter.getName()); + } + + assertNull(actionImpl2.getReturnedEntitySet(null)); + assertNull(actionImpl2.getReturnedEntitySet(mock(EdmEntitySet.class))); + } + + @Test + public void action3BasicMethodCalls() { + assertFalse(actionImpl3.isBound()); + assertEquals(EdmTypeKind.ACTION, actionImpl3.getKind()); + assertEquals("String", actionImpl3.getReturnType().getType().getName()); + assertNotNull(actionImpl3.getParameterNames()); + + for (String name : actionImpl3.getParameterNames()) { + EdmParameter parameter = actionImpl3.getParameter(name); + assertNotNull(parameter); + assertEquals(name, parameter.getName()); + } + + actionImpl3.getReturnedEntitySet(null); + } + + @Test + public void action3getReturnedEntitySetWithEntitySet() { + EdmEntitySet set = mock(EdmEntitySet.class); + when(set.getRelatedBindingTarget("path/Id")).thenReturn(set); + + EdmEntitySet returnedEntitySet = actionImpl3.getReturnedEntitySet(set); + + assertEquals(set, returnedEntitySet); + } + + @Test(expected = EdmException.class) + public void action3getReturnedEntitySetWithNullReturn() { + EdmEntitySet set = mock(EdmEntitySet.class); + when(set.getRelatedBindingTarget("path")).thenReturn(null); + + actionImpl3.getReturnedEntitySet(set); + fail(); + } + + @Test(expected = EdmException.class) + public void action3getReturnedEntitySetWithSingleton() { + EdmSingleton singleton = mock(EdmSingleton.class); + EdmEntitySet set = mock(EdmEntitySet.class); + when(set.getRelatedBindingTarget("path")).thenReturn(singleton); + + actionImpl3.getReturnedEntitySet(set); + fail(); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImportImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImportImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImportImplTest.java new file mode 100644 index 0000000..02824f4 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmActionImportImplTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.olingo.commons.api.edm.EdmAction; +import org.apache.olingo.commons.api.edm.EdmActionImport; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlActionImport; +import org.apache.olingo.commons.core.edm.EdmActionImportImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.junit.Before; +import org.junit.Test; + +public class EdmActionImportImplTest { + + EdmEntityContainer container; + + EdmActionImport actionImport; + + private EdmAction action; + + private EdmEntitySet entitySet; + + @Before + public void setup() { + FullQualifiedName actionFqn = new FullQualifiedName("namespace", "actionName"); + FullQualifiedName entityContainerFqn = new FullQualifiedName("namespace", "containerName"); + String target = entityContainerFqn.getFullQualifiedNameAsString() + "/entitySetName"; + CsdlActionImport providerActionImport = + new CsdlActionImport().setName("actionImportName").setAction(actionFqn).setEntitySet(target); + + EdmProviderImpl edm = mock(EdmProviderImpl.class); + container = mock(EdmEntityContainer.class); + when(edm.getEntityContainer(entityContainerFqn)).thenReturn(container); + action = mock(EdmAction.class); + when(edm.getUnboundAction(actionFqn)).thenReturn(action); + + entitySet = mock(EdmEntitySet.class); + when(container.getEntitySet("entitySetName")).thenReturn(entitySet); + actionImport = new EdmActionImportImpl(edm, container, providerActionImport); + } + + @Test + public void simpleActionTest() { + assertEquals("actionImportName", actionImport.getName()); + assertTrue(container == actionImport.getEntityContainer()); + assertTrue(action == actionImport.getUnboundAction()); + } + + @Test + public void getReturnedEntitySet() { + EdmEntitySet returnedEntitySet = actionImport.getReturnedEntitySet(); + assertNotNull(returnedEntitySet); + assertTrue(returnedEntitySet == entitySet); + + // Chaching + assertTrue(returnedEntitySet == actionImport.getReturnedEntitySet()); + } + + @Test(expected = EdmException.class) + public void getReturnedEntitySetNonExistingContainer() { + String target = "alias.nonexisting/Es"; + CsdlActionImport providerActionImport = new CsdlActionImport().setName("actionImportName").setEntitySet(target); + EdmActionImport actionImport = + new EdmActionImportImpl(mock(EdmProviderImpl.class), container, providerActionImport); + actionImport.getReturnedEntitySet(); + } + + @Test(expected = EdmException.class) + public void getReturnedEntitySetNonExistingEntitySet() { + String target = "nonExisting"; + CsdlActionImport providerActionImport = new CsdlActionImport().setName("actionImportName").setEntitySet(target); + EdmProviderImpl edm = mock(EdmProviderImpl.class); + when(edm.getEntityContainer(null)).thenReturn(container); + EdmActionImport actionImport = new EdmActionImportImpl(edm, container, providerActionImport); + actionImport.getReturnedEntitySet(); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java new file mode 100644 index 0000000..b97c995 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmComplexTypeImplTest.java @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.commons.api.edm.EdmComplexType; +import org.apache.olingo.commons.api.edm.EdmElement; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlComplexType; +import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlNavigationProperty; +import org.apache.olingo.commons.api.edm.provider.CsdlProperty; +import org.apache.olingo.commons.core.edm.EdmComplexTypeImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.junit.Before; +import org.junit.Test; + +public class EdmComplexTypeImplTest { + + private EdmComplexType baseType; + + private EdmComplexType type; + + @Before + public void setupTypes() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + + FullQualifiedName baseName = new FullQualifiedName("namespace", "BaseTypeName"); + CsdlComplexType baseComplexType = new CsdlComplexType(); + List<CsdlProperty> baseProperties = new ArrayList<CsdlProperty>(); + baseProperties.add(new CsdlProperty().setName("prop1").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + List<CsdlNavigationProperty> baseNavigationProperties = new ArrayList<CsdlNavigationProperty>(); + baseNavigationProperties.add(new CsdlNavigationProperty().setName("nav1")); + baseComplexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(baseProperties) + .setNavigationProperties(baseNavigationProperties); + when(provider.getComplexType(baseName)).thenReturn(baseComplexType); + + baseType = new EdmComplexTypeImpl(edm, baseName, baseComplexType); + + FullQualifiedName name = new FullQualifiedName("namespace", "typeName"); + CsdlComplexType complexType = new CsdlComplexType().setBaseType(baseName); + List<CsdlProperty> properties = new ArrayList<CsdlProperty>(); + properties.add(new CsdlProperty().setName("prop2").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + List<CsdlNavigationProperty> navigationProperties = new ArrayList<CsdlNavigationProperty>(); + navigationProperties.add(new CsdlNavigationProperty().setName("nav2")); + complexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(properties) + .setNavigationProperties(navigationProperties); + when(provider.getComplexType(name)).thenReturn(complexType); + + type = new EdmComplexTypeImpl(edm, name, complexType); + } + + @Test + public void noPropertiesAndNoNavPropertiesMustNotResultInException() { + EdmProviderImpl edm = mock(EdmProviderImpl.class); + CsdlComplexType complexType = new CsdlComplexType().setName("n"); + new EdmComplexTypeImpl(edm, new FullQualifiedName("n", "n"), complexType); + } + + @Test + public void typeMustBeCompatibletoBasetype() { + assertTrue(type.compatibleTo(baseType)); + } + + @Test + public void baseTypeMustNotBeCompatibleToType() { + assertFalse(baseType.compatibleTo(type)); + } + + @Test(expected = EdmException.class) + public void nullForCompatibleTypeMustResultInEdmException() { + assertFalse(type.compatibleTo(null)); + } + + @Test + public void getBaseType() { + assertNull(baseType.getBaseType()); + assertNotNull(type.getBaseType()); + } + + @Test + public void propertiesBehaviour() { + List<String> propertyNames = baseType.getPropertyNames(); + assertEquals(1, propertyNames.size()); + assertEquals("prop1", baseType.getProperty("prop1").getName()); + } + + @Test + public void propertiesBehaviourWithBaseType() { + List<String> propertyNames = type.getPropertyNames(); + assertEquals(2, propertyNames.size()); + assertEquals("prop1", type.getProperty("prop1").getName()); + assertEquals("prop2", type.getProperty("prop2").getName()); + } + + @Test + public void navigationPropertiesBehaviour() { + List<String> navigationPropertyNames = baseType.getNavigationPropertyNames(); + assertEquals(1, navigationPropertyNames.size()); + assertEquals("nav1", baseType.getProperty("nav1").getName()); + } + + @Test + public void navigationPropertiesBehaviourWithBaseType() { + List<String> navigationPropertyNames = type.getNavigationPropertyNames(); + assertEquals(2, navigationPropertyNames.size()); + assertEquals("nav1", type.getProperty("nav1").getName()); + assertEquals("nav2", type.getProperty("nav2").getName()); + } + + @Test + public void propertyCaching() { + EdmElement property = type.getProperty("prop1"); + assertTrue(property == type.getProperty("prop1")); + + property = type.getProperty("prop2"); + assertTrue(property == type.getProperty("prop2")); + + property = type.getProperty("nav1"); + assertTrue(property == type.getProperty("nav1")); + + property = type.getProperty("nav2"); + assertTrue(property == type.getProperty("nav2")); + } + + @Test(expected = EdmException.class) + public void nonExistingBaseType() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + FullQualifiedName typeWithNonexistingBaseTypeName = new FullQualifiedName("namespace", "typeName"); + CsdlComplexType complexTypeForNonexistingBaseType = + new CsdlComplexType().setBaseType(new FullQualifiedName("wrong", "wrong")); + complexTypeForNonexistingBaseType.setName("typeName"); + when(provider.getComplexType(typeWithNonexistingBaseTypeName)).thenReturn(complexTypeForNonexistingBaseType); + EdmComplexTypeImpl instance = + new EdmComplexTypeImpl(edm, typeWithNonexistingBaseTypeName, complexTypeForNonexistingBaseType); + instance.getBaseType(); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityContainerImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityContainerImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityContainerImplTest.java new file mode 100644 index 0000000..82aa145 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityContainerImplTest.java @@ -0,0 +1,299 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.commons.api.ex.ODataException; +import org.apache.olingo.commons.api.edm.EdmActionImport; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmFunctionImport; +import org.apache.olingo.commons.api.edm.EdmSingleton; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlAbstractEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlActionImport; +import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainer; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainerInfo; +import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet; +import org.apache.olingo.commons.api.edm.provider.CsdlFunctionImport; +import org.apache.olingo.commons.api.edm.provider.CsdlSingleton; +import org.apache.olingo.commons.core.edm.EdmEntityContainerImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.junit.Before; +import org.junit.Test; + +public class EdmEntityContainerImplTest { + + EdmEntityContainer container; + + @Before + public void setup() { + CsdlEdmProvider provider = new CustomProvider(); + EdmProviderImpl edm = new EdmProviderImpl(provider); + CsdlEntityContainerInfo entityContainerInfo = + new CsdlEntityContainerInfo().setContainerName(new FullQualifiedName("space", "name")); + container = new EdmEntityContainerImpl(edm, provider, entityContainerInfo); + } + + @Test + public void getAllEntitySetInitial() { + List<EdmEntitySet> entitySets = container.getEntitySets(); + assertNotNull(entitySets); + assertEquals(2, entitySets.size()); + } + + @Test + public void getAllEntitySetsAfterOneWasAlreadyLoaded() { + container.getEntitySet("entitySetName"); + List<EdmEntitySet> entitySets = container.getEntitySets(); + assertNotNull(entitySets); + assertEquals(2, entitySets.size()); + } + + @Test + public void getAllSingletonsInitial() { + List<EdmSingleton> singletons = container.getSingletons(); + assertNotNull(singletons); + assertEquals(2, singletons.size()); + } + + @Test + public void getAllSingletonsAfterOneWasAlreadyLoaded() { + container.getSingleton("singletonName"); + List<EdmSingleton> singletons = container.getSingletons(); + assertNotNull(singletons); + assertEquals(2, singletons.size()); + } + + @Test + public void getAllActionImportsInitial() { + List<EdmActionImport> actionImports = container.getActionImports(); + assertNotNull(actionImports); + assertEquals(2, actionImports.size()); + } + + @Test + public void getAllActionImportsAfterOneWasAlreadyLoaded() { + container.getActionImport("actionImportName"); + List<EdmActionImport> actionImports = container.getActionImports(); + assertNotNull(actionImports); + assertEquals(2, actionImports.size()); + } + + @Test + public void getAllFunctionImportsInitial() { + List<EdmFunctionImport> functionImports = container.getFunctionImports(); + assertNotNull(functionImports); + assertEquals(2, functionImports.size()); + } + + @Test + public void getAllFunctionImportsAfterOneWasAlreadyLoaded() { + container.getFunctionImport("functionImportName"); + List<EdmFunctionImport> functionImports = container.getFunctionImports(); + assertNotNull(functionImports); + assertEquals(2, functionImports.size()); + } + + @Test + public void checkEdmExceptionConversion() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + FullQualifiedName containerName = new FullQualifiedName("space", "name"); + when(provider.getEntitySet(containerName, null)).thenThrow(new ODataException("msg")); + when(provider.getSingleton(containerName, null)).thenThrow(new ODataException("msg")); + when(provider.getFunctionImport(containerName, null)).thenThrow(new ODataException("msg")); + when(provider.getActionImport(containerName, null)).thenThrow(new ODataException("msg")); + EdmProviderImpl edm = new EdmProviderImpl(provider); + CsdlEntityContainerInfo entityContainerInfo = + new CsdlEntityContainerInfo().setContainerName(containerName); + EdmEntityContainer container = new EdmEntityContainerImpl(edm, provider, entityContainerInfo); + boolean thrown = false; + try { + container.getEntitySet(null); + } catch (EdmException e) { + thrown = true; + } + if (!thrown) { + fail("Expected EdmException not thrown"); + } + try { + container.getSingleton(null); + } catch (EdmException e) { + thrown = true; + } + if (!thrown) { + fail("Expected EdmException not thrown"); + } + try { + container.getActionImport(null); + } catch (EdmException e) { + thrown = true; + } + if (!thrown) { + fail("Expected EdmException not thrown"); + } + try { + container.getFunctionImport(null); + } catch (EdmException e) { + thrown = true; + } + if (!thrown) { + fail("Expected EdmException not thrown"); + } + } + + @Test + public void simpleContainerGetter() { + assertEquals("name", container.getName()); + assertEquals("space", container.getNamespace()); + } + + @Test + public void getExistingFunctionImport() { + EdmFunctionImport functionImport = container.getFunctionImport("functionImportName"); + assertNotNull(functionImport); + assertEquals("functionImportName", functionImport.getName()); + // Caching + assertTrue(functionImport == container.getFunctionImport("functionImportName")); + } + + @Test + public void getNonExistingFunctionImport() { + assertNull(container.getFunctionImport(null)); + } + + @Test + public void getExistingActionImport() { + EdmActionImport actionImport = container.getActionImport("actionImportName"); + assertNotNull(actionImport); + assertEquals("actionImportName", actionImport.getName()); + // Caching + assertTrue(actionImport == container.getActionImport("actionImportName")); + } + + @Test + public void getNonExistingActionImport() { + assertNull(container.getActionImport(null)); + } + + @Test + public void getExistingSingleton() { + EdmSingleton singleton = container.getSingleton("singletonName"); + assertNotNull(singleton); + assertEquals("singletonName", singleton.getName()); + // Caching + assertTrue(singleton == container.getSingleton("singletonName")); + } + + @Test + public void getNonExistingSingleton() { + assertNull(container.getSingleton(null)); + } + + @Test + public void getExistingEntitySet() { + EdmEntitySet entitySet = container.getEntitySet("entitySetName"); + assertNotNull(entitySet); + assertEquals("entitySetName", entitySet.getName()); + // Caching + assertTrue(entitySet == container.getEntitySet("entitySetName")); + } + + @Test + public void getNonExistingEntitySet() { + assertNull(container.getEntitySet(null)); + } + + private class CustomProvider extends CsdlAbstractEdmProvider { + @Override + public CsdlEntitySet getEntitySet(final FullQualifiedName entityContainer, final String entitySetName) + throws ODataException { + if (entitySetName != null) { + return new CsdlEntitySet().setName("entitySetName"); + } + return null; + } + + @Override + public CsdlSingleton getSingleton(final FullQualifiedName entityContainer, final String singletonName) + throws ODataException { + if (singletonName != null) { + return new CsdlSingleton().setName("singletonName"); + } + return null; + } + + @Override + public CsdlActionImport getActionImport(final FullQualifiedName entityContainer, final String actionImportName) + throws ODataException { + if (actionImportName != null) { + return new CsdlActionImport().setName("actionImportName"); + } + return null; + } + + @Override + public CsdlFunctionImport getFunctionImport(final FullQualifiedName entityContainer, + final String functionImportName) + throws ODataException { + if (functionImportName != null) { + return new CsdlFunctionImport().setName("functionImportName"); + } + return null; + } + + @Override + public CsdlEntityContainer getEntityContainer() throws ODataException { + CsdlEntityContainer container = new CsdlEntityContainer(); + List<CsdlEntitySet> entitySets = new ArrayList<CsdlEntitySet>(); + entitySets.add(new CsdlEntitySet().setName("entitySetName")); + entitySets.add(new CsdlEntitySet().setName("entitySetName2")); + container.setEntitySets(entitySets); + + List<CsdlSingleton> singletons = new ArrayList<CsdlSingleton>(); + singletons.add(new CsdlSingleton().setName("singletonName")); + singletons.add(new CsdlSingleton().setName("singletonName2")); + container.setSingletons(singletons); + + List<CsdlActionImport> actionImports = new ArrayList<CsdlActionImport>(); + actionImports.add(new CsdlActionImport().setName("actionImportName")); + actionImports.add(new CsdlActionImport().setName("actionImportName2")); + container.setActionImports(actionImports); + + List<CsdlFunctionImport> functionImports = new ArrayList<CsdlFunctionImport>(); + functionImports.add(new CsdlFunctionImport().setName("functionImportName")); + functionImports.add(new CsdlFunctionImport().setName("functionImportName2")); + container.setFunctionImports(functionImports); + + return container; + } + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntitySetImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntitySetImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntitySetImplTest.java new file mode 100644 index 0000000..b80e59d --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntitySetImplTest.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; + +import org.apache.olingo.commons.api.edm.EdmBindingTarget; +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainerInfo; +import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityType; +import org.apache.olingo.commons.api.edm.provider.CsdlNavigationPropertyBinding; +import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef; +import org.apache.olingo.commons.core.edm.EdmEntityContainerImpl; +import org.apache.olingo.commons.core.edm.EdmEntitySetImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.junit.Test; + +public class EdmEntitySetImplTest { + + @Test + public void entitySet() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + + final FullQualifiedName typeName = new FullQualifiedName("ns", "entityType"); + final CsdlEntityType entityTypeProvider = new CsdlEntityType() + .setName(typeName.getName()) + .setKey(Arrays.asList(new CsdlPropertyRef().setName("Id"))); + when(provider.getEntityType(typeName)).thenReturn(entityTypeProvider); + + final FullQualifiedName containerName = new FullQualifiedName("ns", "container"); + final CsdlEntityContainerInfo containerInfo = new CsdlEntityContainerInfo().setContainerName(containerName); + when(provider.getEntityContainerInfo(containerName)).thenReturn(containerInfo); + final EdmEntityContainer entityContainer = new EdmEntityContainerImpl(edm, provider, containerInfo); + + final String entitySetName = "entitySet"; + final CsdlEntitySet entitySetProvider = new CsdlEntitySet() + .setName(entitySetName) + .setType(typeName) + .setIncludeInServiceDocument(true) + .setNavigationPropertyBindings(Arrays.asList( + new CsdlNavigationPropertyBinding().setPath("path") + .setTarget(containerName.getFullQualifiedNameAsString() + "/" + entitySetName))); + when(provider.getEntitySet(containerName, entitySetName)).thenReturn(entitySetProvider); + + final EdmEntitySet entitySet = new EdmEntitySetImpl(edm, entityContainer, entitySetProvider); + assertEquals(entitySetName, entityContainer.getEntitySet(entitySetName).getName()); + assertEquals(entitySetName, entitySet.getName()); + final EdmEntityType entityType = entitySet.getEntityType(); + assertEquals(typeName.getNamespace(), entityType.getNamespace()); + assertEquals(typeName.getName(), entityType.getName()); + assertEquals(entityContainer, entitySet.getEntityContainer()); + assertNull(entitySet.getRelatedBindingTarget(null)); + final EdmBindingTarget target = entitySet.getRelatedBindingTarget("path"); + assertEquals(entitySetName, target.getName()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java new file mode 100644 index 0000000..ac36a8a --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEntityTypeImplTest.java @@ -0,0 +1,391 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.commons.api.edm.EdmComplexType; +import org.apache.olingo.commons.api.edm.EdmElement; +import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmKeyPropertyRef; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.EdmProperty; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlComplexType; +import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityType; +import org.apache.olingo.commons.api.edm.provider.CsdlNavigationProperty; +import org.apache.olingo.commons.api.edm.provider.CsdlProperty; +import org.apache.olingo.commons.api.edm.provider.CsdlPropertyRef; +import org.apache.olingo.commons.core.edm.EdmEntityTypeImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.junit.Before; +import org.junit.Test; + +public class EdmEntityTypeImplTest { + + private EdmEntityType baseType; + + private EdmEntityType typeWithBaseType; + + private EdmEntityType typeWithComplexKey; + + @Before + public void setupTypes() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + + FullQualifiedName baseName = new FullQualifiedName("namespace", "BaseTypeName"); + CsdlEntityType baseType = new CsdlEntityType(); + baseType.setName(baseName.getName()); + List<CsdlProperty> properties = new ArrayList<CsdlProperty>(); + properties.add(new CsdlProperty().setName("Id").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + properties.add(new CsdlProperty().setName("Name").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + baseType.setProperties(properties); + List<CsdlPropertyRef> key = new ArrayList<CsdlPropertyRef>(); + key.add(new CsdlPropertyRef().setName("Id")); + baseType.setKey(key); + List<CsdlNavigationProperty> navigationProperties = new ArrayList<CsdlNavigationProperty>(); + navigationProperties.add(new CsdlNavigationProperty().setName("nav1")); + baseType.setNavigationProperties(navigationProperties); + when(provider.getEntityType(baseName)).thenReturn(baseType); + + this.baseType = new EdmEntityTypeImpl(edm, baseName, baseType); + + FullQualifiedName typeName = new FullQualifiedName("namespace", "typeName"); + CsdlEntityType type = new CsdlEntityType(); + type.setName(typeName.getName()); + type.setBaseType(baseName); + List<CsdlProperty> typeProperties = new ArrayList<CsdlProperty>(); + typeProperties.add(new CsdlProperty().setName("address").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + typeProperties.add(new CsdlProperty().setName("email").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + type.setProperties(typeProperties); + List<CsdlNavigationProperty> typeNavigationProperties = new ArrayList<CsdlNavigationProperty>(); + typeNavigationProperties.add(new CsdlNavigationProperty().setName("nav2")); + type.setNavigationProperties(typeNavigationProperties); + when(provider.getEntityType(typeName)).thenReturn(type); + + typeWithBaseType = new EdmEntityTypeImpl(edm, typeName, type); + + FullQualifiedName typeWithComplexKeyName = new FullQualifiedName("namespace", "typeName"); + CsdlEntityType typeWithComplexKeyProvider = new CsdlEntityType(); + typeWithComplexKeyProvider.setName(typeWithComplexKeyName.getName()); + List<CsdlProperty> typeWithComplexKeyProperties = new ArrayList<CsdlProperty>(); + typeWithComplexKeyProperties.add(new CsdlProperty().setName("Id").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + + List<CsdlProperty> complexTypeProperties = new ArrayList<CsdlProperty>(); + complexTypeProperties.add(new CsdlProperty().setName("ComplexPropName").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + FullQualifiedName complexTypeName = new FullQualifiedName("namespace", "complexTypeName"); + when(provider.getComplexType(complexTypeName)).thenReturn( + new CsdlComplexType().setName("complexTypeName").setProperties(complexTypeProperties)); + + typeWithComplexKeyProperties.add(new CsdlProperty().setName("Comp").setType(complexTypeName)); + typeWithComplexKeyProvider.setProperties(typeWithComplexKeyProperties); + List<CsdlPropertyRef> keyForTypeWithComplexKey = new ArrayList<CsdlPropertyRef>(); + keyForTypeWithComplexKey.add(new CsdlPropertyRef().setName("Id")); + keyForTypeWithComplexKey.add(new CsdlPropertyRef().setName("Comp/ComplexPropName").setAlias("alias")); + typeWithComplexKeyProvider.setKey(keyForTypeWithComplexKey); + when(provider.getEntityType(typeWithComplexKeyName)).thenReturn(typeWithComplexKeyProvider); + + typeWithComplexKey = new EdmEntityTypeImpl(edm, typeWithComplexKeyName, typeWithComplexKeyProvider); + } + + @Test + public void testAbstractBaseTypeWithoutKey() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + + FullQualifiedName baseName = new FullQualifiedName("namespace", "BaseTypeName"); + CsdlEntityType baseType = new CsdlEntityType(); + baseType.setName(baseName.getName()); + List<CsdlProperty> properties = new ArrayList<CsdlProperty>(); + properties.add(new CsdlProperty().setName("Id").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + properties.add(new CsdlProperty().setName("Name").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + baseType.setProperties(properties); + List<CsdlNavigationProperty> navigationProperties = new ArrayList<CsdlNavigationProperty>(); + navigationProperties.add(new CsdlNavigationProperty().setName("nav1")); + baseType.setNavigationProperties(navigationProperties); + when(provider.getEntityType(baseName)).thenReturn(baseType); + baseType.setAbstract(true); + EdmEntityType edmAbstarctBaseType = new EdmEntityTypeImpl(edm, baseName, baseType); + + assertEquals(2, edmAbstarctBaseType.getPropertyNames().size()); + assertEquals("Id", edmAbstarctBaseType.getPropertyNames().get(0)); + assertEquals("Name", edmAbstarctBaseType.getPropertyNames().get(1)); + + FullQualifiedName typeName = new FullQualifiedName("namespace", "typeName"); + CsdlEntityType type = new CsdlEntityType(); + type.setName(typeName.getName()); + type.setBaseType(baseName); + List<CsdlProperty> typeProperties = new ArrayList<CsdlProperty>(); + typeProperties.add(new CsdlProperty().setName("address").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + typeProperties.add(new CsdlProperty().setName("email").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + type.setProperties(typeProperties); + List<CsdlPropertyRef> key = new ArrayList<CsdlPropertyRef>(); + key.add(new CsdlPropertyRef().setName("email")); + type.setKey(key); + List<CsdlNavigationProperty> typeNavigationProperties = new ArrayList<CsdlNavigationProperty>(); + typeNavigationProperties.add(new CsdlNavigationProperty().setName("nav2")); + type.setNavigationProperties(typeNavigationProperties); + when(provider.getEntityType(typeName)).thenReturn(type); + + EdmEntityType edmType = new EdmEntityTypeImpl(edm, typeName, type); + + assertNotNull(edmType.getBaseType()); + assertEquals(2, edmAbstarctBaseType.getPropertyNames().size()); + + assertEquals(1, edmType.getKeyPropertyRefs().size()); + assertEquals("email", edmType.getKeyPredicateNames().get(0)); + + assertEquals(4, edmType.getPropertyNames().size()); + assertEquals("Id", edmType.getPropertyNames().get(0)); + assertEquals("Name", edmType.getPropertyNames().get(1)); + assertEquals("address", edmType.getPropertyNames().get(2)); + assertEquals("email", edmType.getPropertyNames().get(3)); + + assertEquals(2, edmType.getNavigationPropertyNames().size()); + assertEquals("nav1", edmType.getNavigationPropertyNames().get(0)); + assertEquals("nav2", edmType.getNavigationPropertyNames().get(1)); + } + + @Test + public void testAbstractBaseTypeWithtKey() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + + FullQualifiedName baseName = new FullQualifiedName("namespace", "BaseTypeName"); + CsdlEntityType baseType = new CsdlEntityType(); + baseType.setName(baseName.getName()); + List<CsdlProperty> properties = new ArrayList<CsdlProperty>(); + properties.add(new CsdlProperty().setName("Id").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + properties.add(new CsdlProperty().setName("Name").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + baseType.setProperties(properties); + List<CsdlPropertyRef> key = new ArrayList<CsdlPropertyRef>(); + key.add(new CsdlPropertyRef().setName("Id")); + baseType.setKey(key); + List<CsdlNavigationProperty> navigationProperties = new ArrayList<CsdlNavigationProperty>(); + navigationProperties.add(new CsdlNavigationProperty().setName("nav1")); + baseType.setNavigationProperties(navigationProperties); + when(provider.getEntityType(baseName)).thenReturn(baseType); + baseType.setAbstract(true); + EdmEntityType edmAbstarctBaseType = new EdmEntityTypeImpl(edm, baseName, baseType); + + FullQualifiedName typeName = new FullQualifiedName("namespace", "typeName"); + CsdlEntityType type = new CsdlEntityType(); + type.setName(typeName.getName()); + type.setBaseType(baseName); + List<CsdlProperty> typeProperties = new ArrayList<CsdlProperty>(); + typeProperties.add(new CsdlProperty().setName("address").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + typeProperties.add(new CsdlProperty().setName("email").setType( + EdmPrimitiveTypeKind.String.getFullQualifiedName())); + type.setProperties(typeProperties); + List<CsdlNavigationProperty> typeNavigationProperties = new ArrayList<CsdlNavigationProperty>(); + typeNavigationProperties.add(new CsdlNavigationProperty().setName("nav2")); + type.setNavigationProperties(typeNavigationProperties); + when(provider.getEntityType(typeName)).thenReturn(type); + EdmEntityType edmType = new EdmEntityTypeImpl(edm, typeName, type); + + assertNotNull(edmType.getBaseType()); + assertEquals(2, edmAbstarctBaseType.getPropertyNames().size()); + + assertEquals(1, edmType.getKeyPropertyRefs().size()); + assertEquals("Id", edmType.getKeyPredicateNames().get(0)); + + assertEquals(4, edmType.getPropertyNames().size()); + assertEquals("Id", edmType.getPropertyNames().get(0)); + assertEquals("Name", edmType.getPropertyNames().get(1)); + assertEquals("address", edmType.getPropertyNames().get(2)); + assertEquals("email", edmType.getPropertyNames().get(3)); + + assertEquals(2, edmType.getNavigationPropertyNames().size()); + assertEquals("nav1", edmType.getNavigationPropertyNames().get(0)); + assertEquals("nav2", edmType.getNavigationPropertyNames().get(1)); + } + + @Test + public void hasStream() { + assertFalse(typeWithBaseType.hasStream()); + } + + @Test + public void complexKeyWithAlias() { + List<String> keyPredicateNames = typeWithComplexKey.getKeyPredicateNames(); + assertEquals(2, keyPredicateNames.size()); + assertEquals("Id", keyPredicateNames.get(0)); + assertEquals("alias", keyPredicateNames.get(1)); + + EdmKeyPropertyRef keyPropertyRef = typeWithComplexKey.getKeyPropertyRef("Id"); + assertNotNull(keyPropertyRef); + assertEquals("Id", keyPropertyRef.getName()); + assertNull(keyPropertyRef.getAlias()); + EdmProperty keyProperty = keyPropertyRef.getProperty(); + assertNotNull(keyProperty); + assertEquals(typeWithComplexKey.getProperty("Id"), keyProperty); + + keyPropertyRef = typeWithComplexKey.getKeyPropertyRef("alias"); + assertNotNull(keyPropertyRef); + assertEquals("Comp/ComplexPropName", keyPropertyRef.getName()); + assertEquals("alias", keyPropertyRef.getAlias()); + + keyProperty = keyPropertyRef.getProperty(); + assertNotNull(keyProperty); + EdmElement complexProperty = typeWithComplexKey.getProperty("Comp"); + EdmComplexType complexType = (EdmComplexType) complexProperty.getType(); + assertNotNull(complexType); + assertEquals(complexType.getProperty("ComplexPropName"), keyProperty); + } + + @Test + public void keyBehaviour() { + List<String> keyPredicateNames = baseType.getKeyPredicateNames(); + assertEquals(1, keyPredicateNames.size()); + assertEquals("Id", keyPredicateNames.get(0)); + + EdmKeyPropertyRef keyPropertyRef = baseType.getKeyPropertyRef("Id"); + assertNotNull(keyPropertyRef); + assertEquals("Id", keyPropertyRef.getName()); + assertNull(keyPropertyRef.getAlias()); + + EdmProperty keyProperty = keyPropertyRef.getProperty(); + assertNotNull(keyProperty); + assertEquals(baseType.getProperty("Id"), keyProperty); + + List<EdmKeyPropertyRef> keyPropertyRefs = baseType.getKeyPropertyRefs(); + assertNotNull(keyPropertyRefs); + assertEquals(1, keyPropertyRefs.size()); + assertEquals("Id", keyPropertyRefs.get(0).getName()); + } + + @Test + public void keyBehaviourWithBasetype() { + List<String> keyPredicateNames = typeWithBaseType.getKeyPredicateNames(); + assertEquals(1, keyPredicateNames.size()); + assertEquals("Id", keyPredicateNames.get(0)); + + EdmKeyPropertyRef keyPropertyRef = typeWithBaseType.getKeyPropertyRef("Id"); + assertNotNull(keyPropertyRef); + assertEquals("Id", keyPropertyRef.getName()); + assertNull(keyPropertyRef.getAlias()); + + List<EdmKeyPropertyRef> keyPropertyRefs = typeWithBaseType.getKeyPropertyRefs(); + assertNotNull(keyPropertyRefs); + assertEquals(1, keyPropertyRefs.size()); + assertEquals("Id", keyPropertyRefs.get(0).getName()); + for (int i = 0; i < keyPropertyRefs.size(); i++) { + assertEquals(keyPropertyRefs.get(i).getName(), typeWithBaseType.getKeyPropertyRefs().get(i).getName()); + } + } + + @Test + public void getBaseType() { + assertNull(baseType.getBaseType()); + assertNotNull(typeWithBaseType.getBaseType()); + } + + @Test + public void propertiesBehaviour() { + List<String> propertyNames = baseType.getPropertyNames(); + assertEquals(2, propertyNames.size()); + assertEquals("Id", baseType.getProperty("Id").getName()); + assertEquals("Name", baseType.getProperty("Name").getName()); + } + + @Test + public void propertiesBehaviourWithBaseType() { + List<String> propertyNames = typeWithBaseType.getPropertyNames(); + assertEquals(4, propertyNames.size()); + assertEquals("Id", typeWithBaseType.getProperty("Id").getName()); + assertEquals("Name", typeWithBaseType.getProperty("Name").getName()); + assertEquals("address", typeWithBaseType.getProperty("address").getName()); + assertEquals("email", typeWithBaseType.getProperty("email").getName()); + } + + @Test + public void navigationPropertiesBehaviour() { + List<String> navigationPropertyNames = baseType.getNavigationPropertyNames(); + assertEquals(1, navigationPropertyNames.size()); + assertEquals("nav1", baseType.getProperty("nav1").getName()); + } + + @Test + public void navigationPropertiesBehaviourWithBaseType() { + List<String> navigationPropertyNames = typeWithBaseType.getNavigationPropertyNames(); + assertEquals(2, navigationPropertyNames.size()); + assertEquals("nav1", typeWithBaseType.getProperty("nav1").getName()); + assertEquals("nav2", typeWithBaseType.getProperty("nav2").getName()); + } + + @Test + public void propertyCaching() { + EdmElement property = typeWithBaseType.getProperty("Id"); + assertTrue(property == typeWithBaseType.getProperty("Id")); + + property = typeWithBaseType.getProperty("address"); + assertTrue(property == typeWithBaseType.getProperty("address")); + + property = typeWithBaseType.getProperty("nav1"); + assertTrue(property == typeWithBaseType.getProperty("nav1")); + + property = typeWithBaseType.getProperty("nav2"); + assertTrue(property == typeWithBaseType.getProperty("nav2")); + } + + @Test + public void abstractTypeDoesNotNeedKey() { + EdmProviderImpl edm = mock(EdmProviderImpl.class); + CsdlEntityType entityType = new CsdlEntityType().setName("n").setAbstract(true); + new EdmEntityTypeImpl(edm, new FullQualifiedName("n", "n"), entityType); + } + + @Test(expected = EdmException.class) + public void invalidBaseType() { + EdmProviderImpl edm = mock(EdmProviderImpl.class); + CsdlEntityType entityType = new CsdlEntityType().setName("n").setBaseType(new FullQualifiedName("wrong", "wrong")); + EdmEntityTypeImpl instance = new EdmEntityTypeImpl(edm, new FullQualifiedName("n", "n"), entityType); + instance.getBaseType(); + } + + @Test + public void abstractTypeWithAbstractBaseTypeDoesNotNeedKey() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + FullQualifiedName baseName = new FullQualifiedName("n", "base"); + when(provider.getEntityType(baseName)).thenReturn(new CsdlEntityType().setName("base").setAbstract(true)); + CsdlEntityType entityType = new CsdlEntityType().setName("n").setAbstract(true).setBaseType(baseName); + new EdmEntityTypeImpl(edm, new FullQualifiedName("n", "n"), entityType); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java new file mode 100644 index 0000000..25dcb03 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmEnumTest.java @@ -0,0 +1,372 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +import java.util.Arrays; +import java.util.List; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmEnumType; +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; +import org.apache.olingo.commons.api.edm.provider.CsdlEnumMember; +import org.apache.olingo.commons.api.edm.provider.CsdlEnumType; +import org.apache.olingo.commons.core.edm.EdmEnumTypeImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; +import org.junit.Test; +import org.mockito.Mockito; + +public class EdmEnumTest { + + private final EdmEnumType instance; + private final EdmEnumType otherInstance; + private final EdmEnumType nonFlagsInstance; + private final EdmEnumType int16EnumType; + private final EdmEnumType int32EnumType; + private final EdmEnumType int32FlagType; + + public EdmEnumTest() { + final List<CsdlEnumMember> memberList = Arrays.asList( + new CsdlEnumMember().setName("first").setValue("1"), + new CsdlEnumMember().setName("second").setValue("64")); + + final FullQualifiedName enumName = new FullQualifiedName("namespace", "name"); + + instance = new EdmEnumTypeImpl(mock(EdmProviderImpl.class), enumName, + new CsdlEnumType().setName("name").setMembers(memberList).setFlags(true) + .setUnderlyingType(EdmPrimitiveTypeKind.SByte.getFullQualifiedName())); + + otherInstance = new EdmEnumTypeImpl(mock(EdmProviderImpl.class), enumName, + new CsdlEnumType().setName("name").setMembers(memberList).setFlags(true) + .setUnderlyingType(EdmPrimitiveTypeKind.SByte.getFullQualifiedName())); + + nonFlagsInstance = new EdmEnumTypeImpl(mock(EdmProviderImpl.class), enumName, + new CsdlEnumType().setName("name").setMembers(memberList).setFlags(false) + .setUnderlyingType(EdmPrimitiveTypeKind.SByte.getFullQualifiedName())); + + int16EnumType = new EdmEnumTypeImpl(Mockito.mock(Edm.class), + new FullQualifiedName("testNamespace", "testName"), new CsdlEnumType() + .setName("MyEnum") + .setFlags(false) + .setUnderlyingType(EdmPrimitiveTypeKind.Int16.getFullQualifiedName()) + .setMembers( + Arrays.asList( + new CsdlEnumMember().setName("A") + .setValue("0"), + new CsdlEnumMember().setName("B") + .setValue("1"), + new CsdlEnumMember().setName("C") + .setValue("2")))); + + int32EnumType = + new EdmEnumTypeImpl(Mockito.mock(Edm.class), + new FullQualifiedName("testNamespace", "testName"), new CsdlEnumType() + .setName("MyEnum") + .setFlags(false) + .setUnderlyingType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName()) + .setMembers( + Arrays + .asList(new CsdlEnumMember().setName("A").setValue("0"), new CsdlEnumMember().setName("B") + .setValue("1"), + new CsdlEnumMember().setName("C").setValue("2")))); + + int32FlagType = + new EdmEnumTypeImpl(Mockito.mock(Edm.class), + new FullQualifiedName("testNamespace", "testName"), new CsdlEnumType() + .setName("MyEnum") + .setFlags(true) + .setUnderlyingType(EdmPrimitiveTypeKind.Int32.getFullQualifiedName()) + .setMembers( + Arrays + .asList(new CsdlEnumMember().setName("A").setValue("2"), new CsdlEnumMember().setName("B") + .setValue("4"), + new CsdlEnumMember().setName("C").setValue("8")))); + } + + @Test + public void nameSpace() throws Exception { + assertEquals("namespace", instance.getNamespace()); + } + + @Test + public void name() throws Exception { + assertEquals("name", instance.getName()); + } + + @Test + public void kind() throws Exception { + assertEquals(EdmTypeKind.ENUM, instance.getKind()); + } + + @Test + public void compatibility() { + assertTrue(instance.isCompatible(instance)); + assertTrue(instance.isCompatible(otherInstance)); + assertFalse(instance.isCompatible(instance.getUnderlyingType())); + } + + @Test + public void defaultType() throws Exception { + assertEquals(Byte.class, instance.getDefaultType()); + EdmEnumType instance = new EdmEnumTypeImpl(Mockito.mock(Edm.class), + new FullQualifiedName("testNamespace", "testName"), + new CsdlEnumType() + .setName("MyEnum")); + assertEquals(Integer.class, instance.getUnderlyingType().getDefaultType()); + } + + @Test + public void members() throws Exception { + assertArrayEquals(new String[] { "first", "second" }, instance.getMemberNames().toArray()); + assertEquals("64", instance.getMember("second").getValue()); + assertNull(instance.getMember("notExisting")); + } + + @Test + public void underlyingType() throws Exception { + assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.SByte), instance.getUnderlyingType()); + } + + @Test + public void validate() throws Exception { + assertTrue(instance.validate(null, null, null, null, null, null)); + assertTrue(instance.validate(null, true, null, null, null, null)); + assertFalse(instance.validate(null, false, null, null, null, null)); + assertFalse(instance.validate("", null, null, null, null, null)); + assertFalse(instance.validate("something", null, null, null, null, null)); + + assertTrue(instance.validate("second", null, null, null, null, null)); + assertTrue(instance.validate("first,second", null, null, null, null, null)); + assertTrue(instance.validate("64", null, null, null, null, null)); + assertTrue(instance.validate("1,64", null, null, null, null, null)); + } + + @Test + public void toUriLiteral() throws Exception { + assertNull(instance.toUriLiteral(null)); + assertEquals("namespace.name'first'", instance.toUriLiteral("first")); + } + + @Test + public void fromUriLiteral() throws Exception { + assertNull(instance.fromUriLiteral(null)); + assertEquals("first", instance.fromUriLiteral("namespace.name'first'")); + + expectErrorInFromUriLiteral(instance, ""); + expectErrorInFromUriLiteral(instance, "name'first'"); + expectErrorInFromUriLiteral(instance, "namespace.name'first"); + expectErrorInFromUriLiteral(instance, "namespace.namespace'first"); + } + + @Test + public void valueToString() throws Exception { + assertNull(instance.valueToString(null, null, null, null, null, null)); + assertNull(instance.valueToString(null, true, null, null, null, null)); + assertEquals("first", instance.valueToString(1, null, null, null, null, null)); + assertEquals("first", instance.valueToString((byte) 1, null, null, null, null, null)); + assertEquals("first", instance.valueToString((short) 1, null, null, null, null, null)); + assertEquals("second", instance.valueToString(Integer.valueOf(64), null, null, null, null, null)); + assertEquals("second", instance.valueToString(64L, null, null, null, null, null)); + assertEquals("first,second", instance.valueToString(65, null, null, null, null, null)); + + expectNullErrorInValueToString(instance); + expectContentErrorInValueToString(instance, 3); + expectTypeErrorInValueToString(instance, 1.0); + + assertEquals("A", int32EnumType.valueToString(0, false, 0, 0, 0, false)); + assertEquals("B", int32EnumType.valueToString(1, false, 0, 0, 0, false)); + assertEquals("C", int32EnumType.valueToString(2, false, 0, 0, 0, false)); + + assertEquals("A", int16EnumType.valueToString(0, false, 0, 0, 0, false)); + assertEquals("B", int16EnumType.valueToString(1, false, 0, 0, 0, false)); + assertEquals("C", int16EnumType.valueToString(2, false, 0, 0, 0, false)); + + assertEquals("A", int32FlagType.valueToString(2, false, 0, 0, 0, false)); + assertEquals("B", int32FlagType.valueToString(4, false, 0, 0, 0, false)); + assertEquals("C", int32FlagType.valueToString(8, false, 0, 0, 0, false)); + assertEquals("A,B", int32FlagType.valueToString(0x2 + 0x4, false, 0, 0, 0, false)); + assertEquals("B,C", int32FlagType.valueToString(0x4 + 0x8, false, 0, 0, 0, false)); + } + + @Test + public void valueOfString() throws Exception { + assertNull(instance.valueOfString(null, null, null, null, null, null, Byte.class)); + assertNull(instance.valueOfString(null, true, null, null, null, null, Byte.class)); + assertEquals(Short.valueOf((short) 1), instance.valueOfString("1", null, null, null, null, null, Short.class)); + assertEquals(Integer.valueOf(1), instance.valueOfString("1", null, null, null, null, null, Integer.class)); + assertEquals(Long.valueOf(64L), instance.valueOfString("64", null, null, null, null, null, Long.class)); + assertEquals(Long.valueOf(1), instance.valueOfString("first", null, null, null, null, null, Long.class)); + assertEquals(Byte.valueOf((byte) 65), instance.valueOfString("first,64", null, null, null, null, null, Byte.class)); + assertEquals(Integer.valueOf(1), instance.valueOfString("1,1,first", null, null, null, null, null, Integer.class)); + + assertEquals(Integer.valueOf(1), nonFlagsInstance.valueOfString("1", null, null, null, null, null, Integer.class)); + expectContentErrorInValueOfString(nonFlagsInstance, "1,64"); + + expectNullErrorInValueOfString(instance); + expectContentErrorInValueOfString(instance, "2"); + expectContentErrorInValueOfString(instance, "1,"); + expectContentErrorInValueOfString(instance, ",1"); + expectTypeErrorInValueOfString(instance, "1"); + + assertEquals(Integer.valueOf(0), int32EnumType.valueOfString("A", null, null, null, null, null, Integer.class)); + assertEquals(Integer.valueOf(1), int32EnumType.valueOfString("B", null, null, null, null, null, Integer.class)); + assertEquals(Integer.valueOf(2), int32EnumType.valueOfString("C", null, null, null, null, null, Integer.class)); + + assertEquals(Integer.valueOf(0), int16EnumType.valueOfString("A", null, null, null, null, null, Integer.class)); + assertEquals(Integer.valueOf(1), int16EnumType.valueOfString("B", null, null, null, null, null, Integer.class)); + assertEquals(Integer.valueOf(2), int16EnumType.valueOfString("C", null, null, null, null, null, Integer.class)); + + assertEquals(Integer.valueOf(2), int32FlagType.valueOfString("A", null, null, null, null, null, Integer.class)); + assertEquals(Integer.valueOf(4), int32FlagType.valueOfString("B", null, null, null, null, null, Integer.class)); + assertEquals(Integer.valueOf(8), int32FlagType.valueOfString("C", null, null, null, null, null, Integer.class)); + assertEquals(Integer.valueOf(0x2 + 0x4), int32FlagType.valueOfString("A,B", null, null, null, null, null, + Integer.class)); + assertEquals(Integer.valueOf(0x4 + 0x8), int32FlagType.valueOfString("B,C", null, null, null, null, null, + Integer.class)); + assertEquals(Integer.valueOf(0x2 + 0x4), int32FlagType.valueOfString("B,A", null, null, null, null, null, + Integer.class)); + } + + private void expectErrorInValueToString(final EdmEnumType instance, + final Object value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, final Boolean isUnicode, + final String message) { + try { + instance.valueToString(value, isNullable, maxLength, precision, scale, isUnicode); + fail("Expected exception not thrown"); + } catch (final EdmPrimitiveTypeException e) { + assertNotNull(e.getLocalizedMessage()); + assertThat(e.getLocalizedMessage(), containsString(message)); + } + } + + private void expectErrorInUnderlyingType( + final EdmPrimitiveTypeKind underlyingType, + final String message) { + try { + new EdmEnumTypeImpl(Mockito.mock(Edm.class), + new FullQualifiedName("testNamespace", "testName"), + new CsdlEnumType() + .setName("MyEnum") + .setFlags(false) + .setUnderlyingType(underlyingType.getFullQualifiedName()) + .setMembers( + Arrays.asList( + new CsdlEnumMember().setName("A") + .setValue("0")))); + fail("Expected exception not thrown"); + } catch (final EdmException e) { + assertNotNull(e.getLocalizedMessage()); + assertThat(e.getLocalizedMessage(), containsString(message)); + } + } + + @Test + public void unsupportedUnderlyingType() throws Exception { + // Test some random unsupported types + expectErrorInUnderlyingType(EdmPrimitiveTypeKind.Date, ""); + expectErrorInUnderlyingType(EdmPrimitiveTypeKind.Geography, ""); + expectErrorInUnderlyingType(EdmPrimitiveTypeKind.Guid, ""); + } + + @Test + public void outOfRangeValueToString() throws Exception { + expectErrorInValueToString(int16EnumType, Integer.MAX_VALUE, null, null, null, null, null, ""); + } + + protected void expectErrorInFromUriLiteral(final EdmPrimitiveType instance, final String value) { + try { + instance.fromUriLiteral(value); + fail("Expected exception not thrown"); + } catch (final EdmPrimitiveTypeException e) { + assertNotNull(e.getLocalizedMessage()); + assertThat(e.getLocalizedMessage(), containsString("' has illegal content.")); + } + } + + private void expectErrorInValueToString(final EdmPrimitiveType instance, + final Object value, final Boolean isNullable, final Integer maxLength, + final Integer precision, final Integer scale, final Boolean isUnicode, + final String message) { + try { + instance.valueToString(value, isNullable, maxLength, precision, scale, isUnicode); + fail("Expected exception not thrown"); + } catch (final EdmPrimitiveTypeException e) { + assertNotNull(e.getLocalizedMessage()); + assertThat(e.getLocalizedMessage(), containsString(message)); + } + } + + protected void expectNullErrorInValueToString(final EdmPrimitiveType instance) { + expectErrorInValueToString(instance, null, false, null, null, null, null, "The value NULL is not allowed."); + } + + protected void expectTypeErrorInValueToString(final EdmPrimitiveType instance, final Object value) { + expectErrorInValueToString(instance, value, null, null, null, null, null, "value type"); + } + + protected void expectContentErrorInValueToString(final EdmPrimitiveType instance, final Object value) { + expectErrorInValueToString(instance, value, null, null, null, null, null, "' is not valid."); + } + + private void expectErrorInValueOfString(final EdmPrimitiveType instance, + final String value, final Boolean isNullable, final Integer maxLength, final Integer precision, + final Integer scale, final Boolean isUnicode, final Class<?> returnType, + final String message) { + + try { + instance.valueOfString(value, isNullable, maxLength, precision, scale, isUnicode, returnType); + fail("Expected exception not thrown"); + } catch (final EdmPrimitiveTypeException e) { + assertNotNull(e.getLocalizedMessage()); + assertThat(e.getLocalizedMessage(), containsString(message)); + } + } + + protected void expectTypeErrorInValueOfString(final EdmPrimitiveType instance, final String value) { + expectErrorInValueOfString(instance, value, null, null, null, null, null, Class.class, + "The value type class java.lang.Class is not supported."); + } + + protected void expectContentErrorInValueOfString(final EdmPrimitiveType instance, final String value) { + expectErrorInValueOfString(instance, value, null, null, null, null, null, instance.getDefaultType(), + "illegal content"); + } + + protected void expectNullErrorInValueOfString(final EdmPrimitiveType instance) { + expectErrorInValueOfString(instance, null, false, null, null, null, null, instance.getDefaultType(), + "The literal 'null' is not allowed."); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImplTest.java new file mode 100644 index 0000000..c122cb8 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImplTest.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +import org.apache.olingo.commons.api.edm.EdmException; +import org.apache.olingo.commons.api.edm.EdmFunction; +import org.apache.olingo.commons.api.edm.EdmReturnType; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlFunction; +import org.apache.olingo.commons.api.edm.provider.CsdlReturnType; +import org.apache.olingo.commons.core.edm.EdmFunctionImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.junit.Before; +import org.junit.Test; + +public class EdmFunctionImplTest { + + private EdmFunction functionImpl1; + private EdmFunction functionImpl2; + + @Before + public void setupFunctions() { + EdmProviderImpl provider = mock(EdmProviderImpl.class); + + CsdlFunction function1 = new CsdlFunction().setReturnType( + new CsdlReturnType().setType(new FullQualifiedName("Edm", "String"))); + functionImpl1 = new EdmFunctionImpl(provider, new FullQualifiedName("namespace", "name"), function1); + CsdlFunction function2 = new CsdlFunction().setComposable(true); + functionImpl2 = new EdmFunctionImpl(provider, new FullQualifiedName("namespace", "name"), function2); + } + + @Test + public void isComposableDefaultFalse() { + assertFalse(functionImpl1.isComposable()); + } + + @Test + public void isComposableSetToTrue() { + assertTrue(functionImpl2.isComposable()); + } + + @Test + public void existingReturnTypeGetsReturned() { + EdmReturnType returnType = functionImpl1.getReturnType(); + assertNotNull(returnType); + assertEquals("String", returnType.getType().getName()); + } + + @Test(expected = EdmException.class) + public void nonExistingReturnTypeResultsInException() { + functionImpl2.getReturnType(); + fail(); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9c1981c4/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImportImplTest.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImportImplTest.java b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImportImplTest.java new file mode 100644 index 0000000..2131881 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/server/core/edm/provider/EdmFunctionImportImplTest.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * 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 + * + * 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 + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Arrays; +import java.util.Collections; + +import org.apache.olingo.commons.api.edm.EdmEntityContainer; +import org.apache.olingo.commons.api.edm.EdmFunction; +import org.apache.olingo.commons.api.edm.EdmFunctionImport; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider; +import org.apache.olingo.commons.api.edm.provider.CsdlEntityContainerInfo; +import org.apache.olingo.commons.api.edm.provider.CsdlFunction; +import org.apache.olingo.commons.api.edm.provider.CsdlFunctionImport; +import org.apache.olingo.commons.api.edm.provider.CsdlParameter; +import org.apache.olingo.commons.api.edm.provider.CsdlReturnType; +import org.apache.olingo.commons.core.edm.EdmEntityContainerImpl; +import org.apache.olingo.commons.core.edm.EdmFunctionImportImpl; +import org.apache.olingo.commons.core.edm.EdmProviderImpl; +import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; +import org.junit.Test; + +public class EdmFunctionImportImplTest { + + @Test + public void functionImport() throws Exception { + CsdlEdmProvider provider = mock(CsdlEdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + + final FullQualifiedName functionName = new FullQualifiedName("ns", "function"); + final CsdlFunction functionProvider = new CsdlFunction() + .setName(functionName.getName()) + .setParameters(Collections.<CsdlParameter> emptyList()) + .setBound(false) + .setComposable(false) + .setReturnType(new CsdlReturnType().setType(EdmPrimitiveTypeKind.Boolean.getFullQualifiedName())); + when(provider.getFunctions(functionName)).thenReturn(Arrays.asList(functionProvider)); + + final FullQualifiedName containerName = new FullQualifiedName("ns", "container"); + final CsdlEntityContainerInfo containerInfo = new CsdlEntityContainerInfo().setContainerName(containerName); + when(provider.getEntityContainerInfo(containerName)).thenReturn(containerInfo); + final EdmEntityContainer entityContainer = new EdmEntityContainerImpl(edm, provider, containerInfo); + + final String functionImportName = "functionImport"; + final CsdlFunctionImport functionImportProvider = new CsdlFunctionImport() + .setName(functionImportName) + .setFunction(functionName) + .setIncludeInServiceDocument(true); + when(provider.getFunctionImport(containerName, functionImportName)).thenReturn(functionImportProvider); + + final EdmFunctionImport functionImport = new EdmFunctionImportImpl(edm, entityContainer, functionImportProvider); + assertEquals(functionImportName, entityContainer.getFunctionImport(functionImportName).getName()); + assertEquals("functionImport", functionImport.getName()); + final EdmFunction function = functionImport.getUnboundFunction(Collections.<String> emptyList()); + assertEquals(functionName.getNamespace(), function.getNamespace()); + assertEquals(functionName.getName(), function.getName()); + assertFalse(function.isBound()); + assertFalse(function.isComposable()); + assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean), + function.getReturnType().getType()); + assertEquals(entityContainer, functionImport.getEntityContainer()); + assertNull(functionImport.getReturnedEntitySet()); + } +}
