[OLINGO-786] Enhance Edm Tests

This commit is contained in:
Christian Amend 2015-10-12 15:59:33 +02:00
parent d9d33abe85
commit 5d66c9cf18
17 changed files with 370 additions and 51 deletions

View File

@ -192,9 +192,10 @@ public abstract class AbstractEdm implements Edm {
@Override
public EdmAction getUnboundAction(final FullQualifiedName actionName) {
EdmAction action = unboundActions.get(actionName);
final FullQualifiedName fqn = resolvePossibleAlias(actionName);
EdmAction action = unboundActions.get(fqn);
if (action == null) {
action = createUnboundAction(resolvePossibleAlias(actionName));
action = createUnboundAction(fqn);
if (action != null) {
unboundActions.put(actionName, action);
}

View File

@ -116,8 +116,8 @@ public class EdmNavigationPropertyImpl extends AbstractEdmNamed implements EdmEl
}
}
referentialConstraints = referentialConstraintsLocal;
referentialConstraints = Collections.unmodifiableList(referentialConstraintsLocal);
}
return Collections.unmodifiableList(referentialConstraints);
return referentialConstraints;
}
}

View File

@ -65,7 +65,9 @@ public class EdmTermImpl extends AbstractEdmNamed implements EdmTerm {
? typeInfo.getEnumType()
: typeInfo.isComplexType()
? typeInfo.getComplexType()
: null;
: typeInfo.isEntityType()
? typeInfo.getEntityType()
: null;
if (termType == null) {
throw new EdmException("Cannot find type with name: " + typeInfo.getFullQualifiedName());
}

View File

@ -29,7 +29,6 @@ import org.junit.Test;
public class ActionMapKeyTest {
private final FullQualifiedName fqn = new FullQualifiedName("namespace", "name");
private final FullQualifiedName fqnType = new FullQualifiedName("namespace2", "name2");
@Test
@ -53,6 +52,23 @@ public class ActionMapKeyTest {
}
fail("EdmException expected for parameters: " + fqn + " " + typeName + " " + collection);
}
@Test
public void testNotEquals() {
ActionMapKey key;
ActionMapKey someKey;
key = new ActionMapKey(fqn, fqnType, false);
someKey = new ActionMapKey(fqnType, fqnType, false);
assertNotSame(key, someKey);
key = new ActionMapKey(fqn, fqnType, false);
someKey = new ActionMapKey(fqnType, fqnType, true);
assertNotSame(key, someKey);
key = new ActionMapKey(fqn, fqnType, false);
assertNotSame(key, null);
}
@Test
public void testEqualsMethod() {
@ -60,6 +76,8 @@ public class ActionMapKeyTest {
ActionMapKey someKey;
key = new ActionMapKey(fqn, fqnType, false);
assertEquals(key, key);
someKey = new ActionMapKey(fqn, fqnType, false);
assertEquals(key, someKey);

View File

@ -135,38 +135,25 @@ public class EdmEntityContainerImplTest {
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");
} catch (EdmException e) {
}
try {
container.getSingleton(null);
} catch (EdmException e) {
thrown = true;
}
if (!thrown) {
fail("Expected EdmException not thrown");
} catch (EdmException e) {
}
try {
container.getActionImport(null);
} catch (EdmException e) {
thrown = true;
}
if (!thrown) {
fail("Expected EdmException not thrown");
} catch (EdmException e) {
}
try {
container.getFunctionImport(null);
} catch (EdmException e) {
thrown = true;
}
if (!thrown) {
fail("Expected EdmException not thrown");
} catch (EdmException e) {
}
}
@ -174,6 +161,7 @@ public class EdmEntityContainerImplTest {
public void simpleContainerGetter() {
assertEquals("name", container.getName());
assertEquals("space", container.getNamespace());
assertEquals(new FullQualifiedName("space.name"), container.getFullQualifiedName());
}
@Test

View File

@ -19,7 +19,10 @@
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.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -29,6 +32,7 @@ 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.EdmException;
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;
@ -63,7 +67,6 @@ public class EdmEntitySetImplTest {
final CsdlEntitySet entitySetProvider = new CsdlEntitySet()
.setName(entitySetName)
.setType(typeName)
.setIncludeInServiceDocument(true)
.setNavigationPropertyBindings(Arrays.asList(
new CsdlNavigationPropertyBinding().setPath("path")
.setTarget(containerName.getFullQualifiedNameAsString() + "/" + entitySetName)));
@ -79,5 +82,35 @@ public class EdmEntitySetImplTest {
assertNull(entitySet.getRelatedBindingTarget(null));
final EdmBindingTarget target = entitySet.getRelatedBindingTarget("path");
assertEquals(entitySetName, target.getName());
assertTrue(entitySet.isIncludeInServiceDocument());
}
@Test
public void entitySetIncludeInServiceDocumentFalseAndInvalidType() throws Exception {
CsdlEdmProvider provider = mock(CsdlEdmProvider.class);
EdmProviderImpl edm = new EdmProviderImpl(provider);
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("invalid.invalid")
.setIncludeInServiceDocument(false);
when(provider.getEntitySet(containerName, entitySetName)).thenReturn(entitySetProvider);
final EdmEntitySet entitySet = new EdmEntitySetImpl(edm, entityContainer, entitySetProvider);
assertFalse(entitySet.isIncludeInServiceDocument());
try {
entitySet.getEntityType();
fail("Expected an EdmException");
} catch (EdmException e) {
assertEquals("Can´t find entity type: invalid.invalid for entity set or singleton: " + entitySetName, e
.getMessage());
}
}
}

View File

@ -378,6 +378,11 @@ public class EdmEntityTypeImplTest {
instance.getBaseType();
}
@Test
public void openTypeDefaultIsFalse() {
assertFalse(baseType.isOpenType());
}
@Test
public void abstractTypeWithAbstractBaseTypeDoesNotNeedKey() throws Exception {
CsdlEdmProvider provider = mock(CsdlEdmProvider.class);

View File

@ -20,12 +20,15 @@ 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.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.olingo.commons.api.edm.EdmEntityContainer;
import org.apache.olingo.commons.api.edm.EdmFunction;
@ -75,14 +78,21 @@ public class EdmFunctionImportImplTest {
final EdmFunctionImport functionImport = new EdmFunctionImportImpl(edm, entityContainer, functionImportProvider);
assertEquals(functionImportName, entityContainer.getFunctionImport(functionImportName).getName());
assertEquals("functionImport", functionImport.getName());
assertEquals(new FullQualifiedName("ns", functionImportName), functionImport.getFullQualifiedName());
assertTrue(functionImport.isIncludeInServiceDocument());
final EdmFunction function = functionImport.getUnboundFunction(Collections.<String> emptyList());
assertEquals(functionName.getNamespace(), function.getNamespace());
assertEquals(functionName.getName(), function.getName());
assertEquals(functionName, function.getFullQualifiedName());
assertFalse(function.isBound());
assertFalse(function.isComposable());
assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Boolean),
function.getReturnType().getType());
assertEquals(entityContainer, functionImport.getEntityContainer());
assertNull(functionImport.getReturnedEntitySet());
List<EdmFunction> functions = functionImport.getUnboundFunctions();
assertNotNull(functions);
assertEquals(1, functions.size());
}
}

View File

@ -32,6 +32,7 @@ import java.util.List;
import org.apache.olingo.commons.api.edm.EdmException;
import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
import org.apache.olingo.commons.api.edm.EdmReferentialConstraint;
import org.apache.olingo.commons.api.edm.EdmType;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.constants.EdmTypeKind;
@ -66,6 +67,7 @@ public class EdmNavigationPropertyImplTest {
assertEquals("entity", type.getName());
assertNull(property.getReferencingPropertyName("referencedPropertyName"));
assertNull(property.getPartner());
assertFalse(property.containsTarget());
// Test caching
EdmType cachedType = property.getType();
@ -83,13 +85,22 @@ public class EdmNavigationPropertyImplTest {
CsdlNavigationProperty propertyProvider = new CsdlNavigationProperty();
propertyProvider.setType(entityTypeName);
propertyProvider.setNullable(false);
propertyProvider.setContainsTarget(true);
List<CsdlReferentialConstraint> referentialConstraints = new ArrayList<CsdlReferentialConstraint>();
referentialConstraints.add(new CsdlReferentialConstraint().setProperty("property").setReferencedProperty(
"referencedProperty"));
propertyProvider.setReferentialConstraints(referentialConstraints);
EdmNavigationProperty property = new EdmNavigationPropertyImpl(edm, propertyProvider);
assertEquals("property", property.getReferencingPropertyName("referencedProperty"));
assertNull(property.getReferencingPropertyName("wrong"));
assertTrue(property.containsTarget());
assertNotNull(property.getReferentialConstraints());
List<EdmReferentialConstraint> edmReferentialConstraints = property.getReferentialConstraints();
assertEquals(1, edmReferentialConstraints.size());
assertTrue(edmReferentialConstraints == property.getReferentialConstraints());
}
@Test

View File

@ -120,6 +120,7 @@ public class EdmParameterImplTest {
assertEquals(Integer.valueOf(12), parameter.getScale());
assertEquals(Integer.valueOf(128), parameter.getMaxLength());
assertFalse(parameter.isNullable());
assertNull(parameter.getSrid());
}
@Test(expected = EdmException.class)

View File

@ -154,5 +154,6 @@ public class EdmPropertyImplTest {
assertTrue(property.isUnicode());
assertFalse(property.isNullable());
assertEquals("x", property.getDefaultValue());
assertNull(property.getSrid());
}
}

View File

@ -32,6 +32,7 @@ import java.util.List;
import org.apache.olingo.commons.api.ex.ODataException;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmAnnotations;
import org.apache.olingo.commons.api.edm.EdmComplexType;
import org.apache.olingo.commons.api.edm.EdmEntityContainer;
import org.apache.olingo.commons.api.edm.EdmEntityType;
@ -40,6 +41,7 @@ import org.apache.olingo.commons.api.edm.EdmException;
import org.apache.olingo.commons.api.edm.EdmTypeDefinition;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.provider.CsdlAliasInfo;
import org.apache.olingo.commons.api.edm.provider.CsdlAnnotations;
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.CsdlEntityContainerInfo;
@ -81,6 +83,10 @@ public class EdmProviderImplTest {
aliasInfos.add(new CsdlAliasInfo().setAlias("alias").setNamespace("namespace"));
when(provider.getAliasInfos()).thenReturn(aliasInfos);
CsdlAnnotations annotationsGroup = new CsdlAnnotations();
annotationsGroup.setTarget("FQN.FQN");
when(provider.getAnnotationsGroup(FQN, null)).thenReturn(annotationsGroup);
edm = new EdmProviderImpl(provider);
}
@ -112,6 +118,7 @@ public class EdmProviderImplTest {
when(localProvider.getComplexType(fqn)).thenThrow(new ODataException("msg"));
when(localProvider.getActions(fqn)).thenThrow(new ODataException("msg"));
when(localProvider.getFunctions(fqn)).thenThrow(new ODataException("msg"));
when(localProvider.getAnnotationsGroup(fqn, null)).thenThrow(new ODataException("msg"));
Edm localEdm = new EdmProviderImpl(localProvider);
@ -124,23 +131,34 @@ public class EdmProviderImplTest {
// seperate because of signature
try {
localEdm.getUnboundAction(fqn);
fail("Expeced an EdmException");
} catch (EdmException e) {
assertEquals("org.apache.olingo.commons.api.ex.ODataException: msg", e.getMessage());
}
try {
localEdm.getUnboundFunction(fqn, null);
fail("Expeced an EdmException");
} catch (EdmException e) {
assertEquals("org.apache.olingo.commons.api.ex.ODataException: msg", e.getMessage());
}
try {
localEdm.getBoundAction(fqn, fqn, true);
fail("Expeced an EdmException");
} catch (EdmException e) {
assertEquals("org.apache.olingo.commons.api.ex.ODataException: msg", e.getMessage());
}
try {
localEdm.getBoundFunction(fqn, fqn, true, null);
fail("Expeced an EdmException");
} catch (EdmException e) {
assertEquals("org.apache.olingo.commons.api.ex.ODataException: msg", e.getMessage());
}
try {
localEdm.getAnnotationGroup(fqn, null);
fail("Expeced an EdmException");
} catch (EdmException e) {
assertEquals("org.apache.olingo.commons.api.ex.ODataException: msg", e.getMessage());
}
@ -222,4 +240,12 @@ public class EdmProviderImplTest {
assertNull(edm.getComplexType(WRONG_FQN));
}
@Test
public void getAnnotations() {
EdmAnnotations annotationGroup = edm.getAnnotationGroup(FQN, null);
assertNotNull(annotationGroup);
assertNull(edm.getAnnotationGroup(WRONG_FQN, null));
}
}

View File

@ -0,0 +1,53 @@
/*
* 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 org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmReferentialConstraint;
import org.apache.olingo.commons.api.edm.provider.CsdlReferentialConstraint;
import org.apache.olingo.commons.core.edm.EdmReferentialConstraintImpl;
import org.junit.Test;
public class EdmReferentialConstraintTest {
@Test
public void initialConstraint() {
CsdlReferentialConstraint constraint = new CsdlReferentialConstraint();
EdmReferentialConstraint edmConstraint = new EdmReferentialConstraintImpl(mock(Edm.class), constraint);
assertNull(edmConstraint.getPropertyName());
assertNull(edmConstraint.getReferencedPropertyName());
}
@Test
public void basicConstraint() {
CsdlReferentialConstraint constraint = new CsdlReferentialConstraint();
constraint.setProperty("PropertyName");
constraint.setReferencedProperty("referencedProperty");
EdmReferentialConstraint edmConstraint = new EdmReferentialConstraintImpl(mock(Edm.class), constraint);
assertEquals("PropertyName", edmConstraint.getPropertyName());
assertEquals("referencedProperty", edmConstraint.getReferencedPropertyName());
}
}

View File

@ -55,6 +55,7 @@ public class EdmReturnTypeImplTest {
assertNull(typeImpl.getPrecision());
assertNull(typeImpl.getMaxLength());
assertNull(typeImpl.getScale());
assertNull(typeImpl.getSrid());
}
@Test
@ -64,8 +65,10 @@ public class EdmReturnTypeImplTest {
EdmReturnType typeImpl = new EdmReturnTypeImpl(mock(EdmProviderImpl.class), providerType);
assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.String), typeImpl.getType());
EdmType cachedType = typeImpl.getType();
assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.String), cachedType);
assertTrue(typeImpl.isCollection());
assertTrue(cachedType == typeImpl.getType());
}
@Test(expected = EdmException.class)

View File

@ -27,10 +27,10 @@ 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.Edm;
import org.apache.olingo.commons.api.edm.EdmAction;
import org.apache.olingo.commons.api.edm.EdmActionImport;
import org.apache.olingo.commons.api.edm.EdmAnnotations;
import org.apache.olingo.commons.api.edm.EdmComplexType;
import org.apache.olingo.commons.api.edm.EdmEntityContainer;
import org.apache.olingo.commons.api.edm.EdmEntitySet;
@ -43,10 +43,10 @@ import org.apache.olingo.commons.api.edm.EdmSchema;
import org.apache.olingo.commons.api.edm.EdmSingleton;
import org.apache.olingo.commons.api.edm.EdmTypeDefinition;
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.CsdlAction;
import org.apache.olingo.commons.api.edm.provider.CsdlActionImport;
import org.apache.olingo.commons.api.edm.provider.CsdlAliasInfo;
import org.apache.olingo.commons.api.edm.provider.CsdlAnnotations;
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.CsdlEntityContainer;
@ -60,6 +60,7 @@ import org.apache.olingo.commons.api.edm.provider.CsdlSchema;
import org.apache.olingo.commons.api.edm.provider.CsdlSingleton;
import org.apache.olingo.commons.api.edm.provider.CsdlTerm;
import org.apache.olingo.commons.api.edm.provider.CsdlTypeDefinition;
import org.apache.olingo.commons.api.ex.ODataException;
import org.apache.olingo.commons.core.edm.EdmProviderImpl;
import org.junit.Before;
import org.junit.Test;
@ -68,6 +69,8 @@ public class EdmSchemaImplTest {
private EdmSchema schema;
private Edm edm;
public static final String NAMESPACE = "org.namespace";
public static final String ALIAS = "alias";
@Before
public void before() {
@ -108,7 +111,8 @@ public class EdmSchemaImplTest {
assertEquals(2, typeDefinitions.size());
for (EdmTypeDefinition def : typeDefinitions) {
assertTrue(def == edm.getTypeDefinition(new FullQualifiedName("org.namespace", def.getName())));
assertTrue(def == edm.getTypeDefinition(new FullQualifiedName(NAMESPACE, def.getName())));
assertTrue(def == edm.getTypeDefinition(new FullQualifiedName(ALIAS, def.getName())));
}
}
@ -119,7 +123,8 @@ public class EdmSchemaImplTest {
assertEquals(2, enumTypes.size());
for (EdmEnumType enumType : enumTypes) {
assertTrue(enumType == edm.getEnumType(new FullQualifiedName("org.namespace", enumType.getName())));
assertTrue(enumType == edm.getEnumType(new FullQualifiedName(NAMESPACE, enumType.getName())));
assertTrue(enumType == edm.getEnumType(new FullQualifiedName(ALIAS, enumType.getName())));
}
}
@ -130,7 +135,8 @@ public class EdmSchemaImplTest {
assertEquals(2, entityTypes.size());
for (EdmEntityType entityType : entityTypes) {
assertTrue(entityType == edm.getEntityType(new FullQualifiedName("org.namespace", entityType.getName())));
assertTrue(entityType == edm.getEntityType(new FullQualifiedName(NAMESPACE, entityType.getName())));
assertTrue(entityType == edm.getEntityType(new FullQualifiedName(ALIAS, entityType.getName())));
}
}
@ -141,7 +147,8 @@ public class EdmSchemaImplTest {
assertEquals(2, complexTypes.size());
for (EdmComplexType complexType : complexTypes) {
assertTrue(complexType == edm.getComplexType(new FullQualifiedName("org.namespace", complexType.getName())));
assertTrue(complexType == edm.getComplexType(new FullQualifiedName(NAMESPACE, complexType.getName())));
assertTrue(complexType == edm.getComplexType(new FullQualifiedName(ALIAS, complexType.getName())));
}
}
@ -152,7 +159,8 @@ public class EdmSchemaImplTest {
assertEquals(2, actions.size());
for (EdmAction action : actions) {
assertTrue(action == edm.getUnboundAction(new FullQualifiedName("org.namespace", action.getName())));
assertTrue(action == edm.getUnboundAction(new FullQualifiedName(NAMESPACE, action.getName())));
assertTrue(action == edm.getUnboundAction(new FullQualifiedName(ALIAS, action.getName())));
}
}
@ -163,11 +171,29 @@ public class EdmSchemaImplTest {
assertEquals(2, functions.size());
for (EdmFunction function : functions) {
FullQualifiedName functionName = new FullQualifiedName("org.namespace", function.getName());
FullQualifiedName functionName = new FullQualifiedName(NAMESPACE, function.getName());
assertTrue(function == edm.getUnboundFunction(functionName, null));
functionName = new FullQualifiedName(ALIAS, function.getName());
assertTrue(function == edm.getUnboundFunction(functionName, null));
}
}
@Test
public void getAnnotationGroups() {
List<EdmAnnotations> annotationGroups = schema.getAnnotationGroups();
assertNotNull(annotationGroups);
assertEquals(2, annotationGroups.size());
for (EdmAnnotations annotationGroup : annotationGroups) {
FullQualifiedName targetName = new FullQualifiedName(annotationGroup.getTargetPath());
assertTrue(annotationGroup == edm.getAnnotationGroup(targetName, null));
targetName = new FullQualifiedName(annotationGroup.getTargetPath().replace(NAMESPACE, ALIAS));
assertTrue(annotationGroup == edm.getAnnotationGroup(targetName, null));
}
}
@Test
public void getContainer() {
EdmEntityContainer container = schema.getEntityContainer();
@ -205,10 +231,7 @@ public class EdmSchemaImplTest {
assertTrue(container == edm.getEntityContainer(null));
}
private class LocalProvider extends CsdlAbstractEdmProvider {
private static final String ALIAS = "alias";
private static final String NAMESPACE = "org.namespace";
private class LocalProvider implements CsdlEdmProvider {
@Override
public CsdlEnumType getEnumType(final FullQualifiedName enumTypeName) throws ODataException {
@ -350,6 +373,17 @@ public class EdmSchemaImplTest {
functions.add(new CsdlFunction().setName("function1"));
functions.add(new CsdlFunction().setName("function2"));
providerSchema.setFunctions(functions);
List<CsdlAnnotations> annotationGroups = new ArrayList<CsdlAnnotations>();
annotationGroups.add(new CsdlAnnotations().setTarget(NAMESPACE + ".entityType1"));
annotationGroups.add(new CsdlAnnotations().setTarget(NAMESPACE + ".entityType2"));
providerSchema.setAnnotationsGroup(annotationGroups);
List<CsdlTerm> terms = new ArrayList<CsdlTerm>();
terms.add(new CsdlTerm().setName("term1").setType("Edm.String"));
terms.add(new CsdlTerm().setName("term2").setType("Edm.String"));
providerSchema.setTerms(terms);
ArrayList<CsdlSchema> schemas = new ArrayList<CsdlSchema>();
schemas.add(providerSchema);
return schemas;
@ -359,5 +393,10 @@ public class EdmSchemaImplTest {
public CsdlEntityContainer getEntityContainer() throws ODataException {
throw new RuntimeException("Provider must not be called in the schema case");
}
@Override
public CsdlAnnotations getAnnotationsGroup(FullQualifiedName targetName, String qualifier) throws ODataException {
throw new RuntimeException("Provider must not be called in the schema case");
}
}
}

View File

@ -23,14 +23,22 @@ 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.Arrays;
import java.util.List;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmComplexType;
import org.apache.olingo.commons.api.edm.EdmEntityType;
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.EdmTerm;
import org.apache.olingo.commons.api.edm.EdmTypeDefinition;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.TargetType;
import org.apache.olingo.commons.api.edm.provider.CsdlAnnotation;
@ -68,32 +76,32 @@ public class EdmTermImplTest {
derivedCsdlTerm.setAppliesTo(appliesTo);
List<CsdlAnnotation> csdlAnnotations = new ArrayList<CsdlAnnotation>();
csdlAnnotations.add(new CsdlAnnotation().setTerm("name1"));
derivedCsdlTerm.setAnnotations(csdlAnnotations );
derivedCsdlTerm.setAnnotations(csdlAnnotations);
derivedCsdlTerm.setNullable(false);
derivedCsdlTerm.setMaxLength(new Integer(15));
derivedCsdlTerm.setDefaultValue("abc");
derivedCsdlTerm.setPrecision(new Integer(14));
derivedCsdlTerm.setScale(new Integer(13));
when(provider.getTerm(derivedTermName)).thenReturn(derivedCsdlTerm);
derivedTerm = new EdmTermImpl(edm, "namespace", derivedCsdlTerm);
}
@Test
public void termBasics() throws Exception {
assertEquals("name1", initialTerm.getName());
assertEquals(new FullQualifiedName("namespace", "name1"), initialTerm.getFullQualifiedName());
assertTrue(initialTerm.getAnnotations().isEmpty());
assertTrue(initialTerm.getAppliesTo().isEmpty());
assertNull(initialTerm.getBaseTerm());
EdmPrimitiveType type = (EdmPrimitiveType) initialTerm.getType();
assertEquals(type.getName(), "String");
//initial facets
// initial facets
assertTrue(initialTerm.isNullable());
assertNull(initialTerm.getDefaultValue());
assertNull(initialTerm.getMaxLength());
@ -101,12 +109,12 @@ public class EdmTermImplTest {
assertNull(initialTerm.getScale());
assertNull(initialTerm.getSrid());
}
@Test
@Test
public void derivedTermTest() {
assertEquals("name2", derivedTerm.getName());
assertEquals(new FullQualifiedName("namespace", "name2"), derivedTerm.getFullQualifiedName());
assertNotNull(derivedTerm.getBaseTerm());
assertEquals("name1", derivedTerm.getBaseTerm().getName());
assertFalse(derivedTerm.getAnnotations().isEmpty());
@ -116,8 +124,8 @@ public class EdmTermImplTest {
EdmPrimitiveType type = (EdmPrimitiveType) derivedTerm.getType();
assertEquals(type.getName(), "String");
//set facets
// set facets
assertFalse(derivedTerm.isNullable());
assertEquals("abc", derivedTerm.getDefaultValue());
assertEquals(new Integer(15), derivedTerm.getMaxLength());
@ -126,4 +134,103 @@ public class EdmTermImplTest {
assertNull(derivedTerm.getSrid());
}
@Test
public void termWithTypeDef() {
CsdlTerm csdlTerm = new CsdlTerm();
FullQualifiedName csdlTerm1Name = new FullQualifiedName("namespace", "name1");
csdlTerm.setName(csdlTerm1Name.getName());
String namespaceAndName = "mySchema.TypeDef";
String name = "TypeDef";
csdlTerm.setType(namespaceAndName);
Edm edm = mock(Edm.class);
EdmTypeDefinition typeMock = mock(EdmTypeDefinition.class);
when(typeMock.getName()).thenReturn(name);
when(edm.getTypeDefinition(new FullQualifiedName(namespaceAndName))).thenReturn(typeMock);
EdmTerm localTerm = new EdmTermImpl(edm, "namespace", csdlTerm);
assertNotNull(localTerm.getType());
assertEquals(name, localTerm.getType().getName());
}
@Test
public void termWithEnumType() {
CsdlTerm csdlTerm = new CsdlTerm();
FullQualifiedName csdlTerm1Name = new FullQualifiedName("namespace", "name1");
csdlTerm.setName(csdlTerm1Name.getName());
String namespaceAndName = "mySchema.Enum";
String name = "Enum";
csdlTerm.setType(namespaceAndName);
Edm edm = mock(Edm.class);
EdmEnumType typeMock = mock(EdmEnumType.class);
when(typeMock.getName()).thenReturn(name);
when(edm.getEnumType(new FullQualifiedName(namespaceAndName))).thenReturn(typeMock);
EdmTerm localTerm = new EdmTermImpl(edm, "namespace", csdlTerm);
assertNotNull(localTerm.getType());
assertEquals(name, localTerm.getType().getName());
}
@Test
public void termWithComplexType() {
CsdlTerm csdlTerm = new CsdlTerm();
FullQualifiedName csdlTerm1Name = new FullQualifiedName("namespace", "name1");
csdlTerm.setName(csdlTerm1Name.getName());
String namespaceAndName = "mySchema.Complex";
String name = "Complex";
csdlTerm.setType(namespaceAndName);
Edm edm = mock(Edm.class);
EdmComplexType typeMock = mock(EdmComplexType.class);
when(typeMock.getName()).thenReturn(name);
when(edm.getComplexType(new FullQualifiedName(namespaceAndName))).thenReturn(typeMock);
EdmTerm localTerm = new EdmTermImpl(edm, "namespace", csdlTerm);
assertNotNull(localTerm.getType());
assertEquals(name, localTerm.getType().getName());
}
@Test
public void termWithEntityType() {
CsdlTerm csdlTerm = new CsdlTerm();
FullQualifiedName csdlTerm1Name = new FullQualifiedName("namespace", "name1");
csdlTerm.setName(csdlTerm1Name.getName());
String namespaceAndName = "mySchema.Entity";
String name = "Entity";
csdlTerm.setType(namespaceAndName);
Edm edm = mock(Edm.class);
EdmEntityType typeMock = mock(EdmEntityType.class);
when(typeMock.getName()).thenReturn(name);
when(edm.getEntityType(new FullQualifiedName(namespaceAndName))).thenReturn(typeMock);
EdmTerm localTerm = new EdmTermImpl(edm, "namespace", csdlTerm);
assertNotNull(localTerm.getType());
assertEquals(name, localTerm.getType().getName());
}
@Test
public void invalidType() {
CsdlTerm csdlTerm = new CsdlTerm();
FullQualifiedName csdlTerm1Name = new FullQualifiedName("namespace", "name1");
csdlTerm.setName(csdlTerm1Name.getName());
csdlTerm.setType("invalid.invalid");
EdmTerm localTerm = new EdmTermImpl(mock(Edm.class), "namespace", csdlTerm);
try {
localTerm.getType();
fail("Expected an EdmException");
} catch (EdmException e) {
assertEquals("Cannot find type with name: invalid.invalid", e.getMessage());
}
}
@Test
public void invalidAppliesToContent() {
CsdlTerm csdlTerm = new CsdlTerm();
FullQualifiedName csdlTerm1Name = new FullQualifiedName("namespace", "name1");
csdlTerm.setName(csdlTerm1Name.getName());
csdlTerm.setType("Edm.String");
csdlTerm.setAppliesTo(Arrays.asList("Invalid"));
EdmTerm localTerm = new EdmTermImpl(mock(Edm.class), "namespace", csdlTerm);
try {
localTerm.getAppliesTo();
fail("Expected an EdmException");
} catch (EdmException e) {
assertEquals("Invalid AppliesTo value: Invalid", e.getMessage());
}
}
}

View File

@ -21,6 +21,7 @@ package org.apache.olingo.server.core.edm.provider;
import static org.junit.Assert.assertEquals;
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 org.apache.olingo.commons.api.edm.EdmException;
@ -46,6 +47,7 @@ public class EdmTypeDefinitionImplTest {
assertEquals("name", typeDefImpl.getName());
assertEquals("namespace", typeDefImpl.getNamespace());
assertEquals(new FullQualifiedName("namespace.name"), typeDefImpl.getFullQualifiedName());
assertEquals(String.class, typeDefImpl.getDefaultType());
assertEquals(EdmTypeKind.DEFINITION, typeDefImpl.getKind());
assertEquals(EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.String), typeDefImpl.getUnderlyingType());
@ -63,15 +65,34 @@ public class EdmTypeDefinitionImplTest {
assertNull(typeDefImpl.getPrecision());
assertNull(typeDefImpl.getScale());
assertTrue(typeDefImpl.isUnicode());
assertNull(typeDefImpl.getSrid());
}
@Test(expected = EdmException.class)
@Test
public void invalidTypeResultsInEdmException() throws Exception {
FullQualifiedName typeDefName = new FullQualifiedName("namespace", "name");
CsdlTypeDefinition providerTypeDef =
new CsdlTypeDefinition().setName("typeDef").setUnderlyingType(new FullQualifiedName("wrong", "wrong"));
EdmTypeDefinitionImpl def = new EdmTypeDefinitionImpl(mock(EdmProviderImpl.class), typeDefName, providerTypeDef);
def.getUnderlyingType();
try {
def.getUnderlyingType();
fail("Expected an EdmException");
} catch (EdmException e) {
assertEquals("Invalid underlying type: wrong.wrong", e.getMessage());
}
}
@Test
public void nullTypeResultsInEdmException() throws Exception {
FullQualifiedName typeDefName = new FullQualifiedName("namespace", "name");
CsdlTypeDefinition providerTypeDef = new CsdlTypeDefinition().setName("typeDef");
EdmTypeDefinitionImpl def = new EdmTypeDefinitionImpl(mock(EdmProviderImpl.class), typeDefName, providerTypeDef);
try {
def.getUnderlyingType();
fail("Expected an EdmException");
} catch (EdmException e) {
assertEquals("Underlying Type for type definition: namespace.name must not be null.", e.getMessage());
}
}
}