diff --git a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKey.java b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKey.java index 03e5f17ec..c33569411 100644 --- a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKey.java +++ b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKey.java @@ -18,6 +18,7 @@ ******************************************************************************/ package org.apache.olingo.odata4.commons.core.edm; +import org.apache.olingo.odata4.commons.api.edm.EdmException; import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName; public class ActionMapKey { @@ -27,6 +28,10 @@ public class ActionMapKey { public ActionMapKey(final FullQualifiedName actionName, final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection) { + if (actionName == null || bindingParameterTypeName == null || isBindingParameterCollection == null) { + throw new EdmException( + "Action name, binding parameter type and binding parameter collection must not be null for bound actions"); + } this.actionName = actionName; this.bindingParameterTypeName = bindingParameterTypeName; this.isBindingParameterCollection = isBindingParameterCollection; @@ -34,20 +39,8 @@ public class ActionMapKey { @Override public int hashCode() { - String forHash = actionName.toString(); - - if (bindingParameterTypeName != null) { - forHash = forHash + bindingParameterTypeName.toString(); - } else { - forHash = forHash + "TypeNull"; - } - - if (isBindingParameterCollection != null) { - forHash = forHash + isBindingParameterCollection.toString(); - } else { - forHash = forHash + "CollectionNull"; - } - + String forHash = + actionName.toString() + bindingParameterTypeName.toString() + isBindingParameterCollection.toString(); return forHash.hashCode(); } @@ -60,16 +53,9 @@ public class ActionMapKey { return false; } final ActionMapKey other = (ActionMapKey) obj; - - if (actionName.equals(other.actionName)) { - if ((bindingParameterTypeName == null && other.bindingParameterTypeName == null) - || (bindingParameterTypeName != null && bindingParameterTypeName.equals(other.bindingParameterTypeName))) { - if ((isBindingParameterCollection == null && other.isBindingParameterCollection == null) - || (isBindingParameterCollection != null && isBindingParameterCollection - .equals(other.isBindingParameterCollection))) { - return true; - } - } + if (actionName.equals(other.actionName) && bindingParameterTypeName.equals(other.bindingParameterTypeName) + && isBindingParameterCollection.equals(other.isBindingParameterCollection)) { + return true; } return false; } diff --git a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/EdmImpl.java b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/EdmImpl.java index d647df43f..b7d8ed888 100644 --- a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/EdmImpl.java +++ b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/EdmImpl.java @@ -42,8 +42,10 @@ public abstract class EdmImpl implements Edm { new HashMap(); private final Map entityTypes = new HashMap(); private final Map complexTypes = new HashMap(); - private final Map actions = new HashMap(); - private final Map functions = new HashMap(); + private final Map unboundActions = new HashMap(); + private final Map unboundFunctions = new HashMap(); + private final Map boundActions = new HashMap(); + private final Map boundFunctions = new HashMap(); private EdmServiceMetadata serviceMetadata; private Map aliasToNamespaceInfo; @@ -119,16 +121,27 @@ public abstract class EdmImpl implements Edm { public EdmAction getAction(final FullQualifiedName actionName, final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection) { FullQualifiedName actionFqn = resolvePossibleAlias(actionName); - FullQualifiedName bindingParameterTypeFqn = resolvePossibleAlias(bindingParameterTypeName); - ActionMapKey key = new ActionMapKey(actionFqn, bindingParameterTypeFqn, isBindingParameterCollection); - EdmAction action = actions.get(key); - if (action == null) { - action = createAction(actionFqn, bindingParameterTypeFqn, isBindingParameterCollection); - if (action != null) { - actions.put(key, action); + if (bindingParameterTypeName == null) { + EdmAction action = unboundActions.get(actionName); + if (action == null) { + action = createUnboundAction(actionFqn); + if (action != null) { + unboundActions.put(actionName, action); + } } + return action; + } else { + FullQualifiedName bindingParameterTypeFqn = resolvePossibleAlias(bindingParameterTypeName); + ActionMapKey key = new ActionMapKey(actionFqn, bindingParameterTypeFqn, isBindingParameterCollection); + EdmAction action = boundActions.get(key); + if (action == null) { + action = createBoundAction(actionFqn, bindingParameterTypeFqn, isBindingParameterCollection); + if (action != null) { + boundActions.put(key, action); + } + } + return action; } - return action; } @Override @@ -136,18 +149,31 @@ public abstract class EdmImpl implements Edm { final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection, final List parameterNames) { FullQualifiedName functionFqn = resolvePossibleAlias(functionName); - FullQualifiedName bindingParameterTypeFqn = resolvePossibleAlias(bindingParameterTypeName); - FunctionMapKey key = - new FunctionMapKey(functionFqn, bindingParameterTypeFqn, isBindingParameterCollection, parameterNames); - EdmFunction function = functions.get(key); - if (function == null) { - function = createFunction(functionFqn, bindingParameterTypeFqn, isBindingParameterCollection, - parameterNames); - if (function != null) { - functions.put(key, function); + if(bindingParameterTypeName == null){ + FunctionMapKey key = + new FunctionMapKey(functionFqn, bindingParameterTypeName, isBindingParameterCollection, parameterNames); + EdmFunction function = unboundFunctions.get(key); + if (function == null) { + function = createUnboundFunction(functionFqn, parameterNames); + if (function != null) { + unboundFunctions.put(key, function); + } } + return function; + }else{ + FullQualifiedName bindingParameterTypeFqn = resolvePossibleAlias(bindingParameterTypeName); + FunctionMapKey key = + new FunctionMapKey(functionFqn, bindingParameterTypeFqn, isBindingParameterCollection, parameterNames); + EdmFunction function = boundFunctions.get(key); + if (function == null) { + function = createBoundFunction(functionFqn, bindingParameterTypeFqn, isBindingParameterCollection, + parameterNames); + if (function != null) { + boundFunctions.put(key, function); + } + } + return function; } - return function; } @Override @@ -187,13 +213,16 @@ public abstract class EdmImpl implements Edm { protected abstract EdmComplexType createComplexType(FullQualifiedName complexTypeName); - protected abstract EdmAction createAction(FullQualifiedName actionName, FullQualifiedName bindingParameterTypeName, + protected abstract EdmAction createUnboundAction(FullQualifiedName actionName); + + protected abstract EdmFunction createUnboundFunction(FullQualifiedName functionName, List parameterNames); + + protected abstract EdmAction createBoundAction(FullQualifiedName actionName, FullQualifiedName bindingParameterTypeName, Boolean isBindingParameterCollection); - protected abstract EdmFunction createFunction(FullQualifiedName functionName, + protected abstract EdmFunction createBoundFunction(FullQualifiedName functionName, FullQualifiedName bindingParameterTypeName, Boolean isBindingParameterCollection, List parameterNames); protected abstract EdmServiceMetadata createServiceMetadata(); - } diff --git a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/FunctionMapKey.java b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/FunctionMapKey.java index e98d57cda..e97f4c04f 100644 --- a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/FunctionMapKey.java +++ b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/FunctionMapKey.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.apache.olingo.odata4.commons.api.edm.EdmException; import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName; public class FunctionMapKey { @@ -34,6 +35,10 @@ public class FunctionMapKey { public FunctionMapKey(final FullQualifiedName functionName, final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection, final List parameterNames) { this.functionName = functionName; + if (bindingParameterTypeName != null && isBindingParameterCollection == null) { + throw new EdmException( + "Indicator that the bindingparameter is a collection must not be null if its an bound function."); + } this.bindingParameterTypeName = bindingParameterTypeName; this.isBindingParameterCollection = isBindingParameterCollection; if (parameterNames != null) { diff --git a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImpl.java b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImpl.java index f6a82b84e..6db932d78 100644 --- a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImpl.java +++ b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImpl.java @@ -19,6 +19,7 @@ package org.apache.olingo.odata4.commons.core.edm.provider; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,6 +50,8 @@ import org.apache.olingo.odata4.commons.core.edm.EdmImpl; public class EdmProviderImpl extends EdmImpl { private final EdmProvider provider; + private final Map> actionsMap = new HashMap>(); + private final Map> functionsMap = new HashMap>(); public EdmProviderImpl(final EdmProvider provider) { this.provider = provider; @@ -121,110 +124,76 @@ public class EdmProviderImpl extends EdmImpl { } @Override - public EdmAction createAction(final FullQualifiedName actionName, final FullQualifiedName bindingParameterTypeName, - final Boolean isBindingParameterCollection) { - + public EdmAction createBoundAction(final FullQualifiedName actionName, + final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection) { try { - List actions = provider.getActions(actionName); - if (actions != null) { - EdmActionImpl actionImpl = null; - if (bindingParameterTypeName == null) { - // Search for first unbound action - for (Action action : actions) { - if (action.isBound() == false) { - actionImpl = new EdmActionImpl(this, actionName, action); - break; - } - } + List actions = actionsMap.get(actionName); + if (actions == null) { + actions = provider.getActions(actionName); + if (actions != null) { + actionsMap.put(actionName, actions); } else { - // Search for bound action where binding parameter matches - boolean isCollection = false; - if (isBindingParameterCollection == null) { - isCollection = false; - } else { - isCollection = isBindingParameterCollection; - } - for (Action action : actions) { - if (action.isBound() == true) { - List parameters = action.getParameters(); - Parameter parameter = parameters.get(0); - if (bindingParameterTypeName.equals(parameter.getType()) && isCollection == parameter.isCollection()) { - actionImpl = new EdmActionImpl(this, actionName, action); - break; - } - - } - } + return null; } - - return actionImpl; } - return null; + EdmActionImpl actionImpl = null; + // Search for bound action where binding parameter matches + for (Action action : actions) { + if (action.isBound() == true) { + List parameters = action.getParameters(); + Parameter parameter = parameters.get(0); + if (bindingParameterTypeName.equals(parameter.getType()) + && isBindingParameterCollection.booleanValue() == parameter.isCollection()) { + actionImpl = new EdmActionImpl(this, actionName, action); + break; + } + + } + } + return actionImpl; } catch (ODataException e) { throw new EdmException(e); } } @Override - public EdmFunction createFunction(final FullQualifiedName functionName, + public EdmFunction createBoundFunction(final FullQualifiedName functionName, final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection, final List parameterNames) { try { - List functions = provider.getFunctions(functionName); - if (functions != null) { - EdmFunctionImpl functionImpl = null; - // TODO: Should we throw an edmexception when parameternames is null? - if (bindingParameterTypeName == null) { - // Search for matching unbound function - for (Function function : functions) { - if (function.isBound() == false) { - List parameters = function.getParameters(); - // TODO add check for parameters == null; - if (parameterNames.size() == parameters.size()) { - List functionParameterNames = new ArrayList(); - for (Parameter parameter : parameters) { - functionParameterNames.add(parameter.getName()); - } - - if (parameterNames.containsAll(functionParameterNames)) { - functionImpl = new EdmFunctionImpl(this, functionName, function); - break; - } - } - } - } + List functions = functionsMap.get(functionName); + if (functions == null) { + functions = provider.getFunctions(functionName); + if (functions != null) { + functionsMap.put(functionName, functions); } else { - // Search for matching bound function - boolean isCollection = false; - if (isBindingParameterCollection == null) { - isCollection = false; - } else { - isCollection = isBindingParameterCollection; + return null; + } + } + EdmFunctionImpl functionImpl = null; + for (Function function : functions) { + if (function.isBound() == true) { + List parameters = function.getParameters(); + if (parameters == null || parameters.size() == 0) { + throw new EdmException("No parameter specified for bound function: " + functionName); } - for (Function function : functions) { - if (function.isBound() == true) { - List parameters = function.getParameters(); - Parameter bindingParameter = parameters.get(0); - if (bindingParameterTypeName.equals(bindingParameter.getType()) - && isCollection == bindingParameter.isCollection()) { - // bindingparameter type matches now only parameter names have to match - List functionParameterNames = new ArrayList(); - for (int i = 1; i < parameters.size(); i++) { - functionParameterNames.add(parameters.get(i).getName()); - } - - if (parameterNames.containsAll(functionParameterNames)) { - functionImpl = new EdmFunctionImpl(this, functionName, function); - break; - } + Parameter bindingParameter = parameters.get(0); + if (bindingParameterTypeName.equals(bindingParameter.getType()) + && isBindingParameterCollection.booleanValue() == bindingParameter.isCollection()) { + if (parameterNames.size() == parameters.size()) { + List functionParameterNames = new ArrayList(); + for (Parameter parameter : parameters) { + functionParameterNames.add(parameter.getName()); + } + if (parameterNames.containsAll(functionParameterNames)) { + functionImpl = new EdmFunctionImpl(this, functionName, function); + break; } } } } - - return functionImpl; } - return null; + return functionImpl; } catch (ODataException e) { throw new EdmException(e); } @@ -251,4 +220,72 @@ public class EdmProviderImpl extends EdmImpl { return aliasToNamespaceInfos; } + @Override + protected EdmAction createUnboundAction(FullQualifiedName actionName) { + try { + List actions = actionsMap.get(actionName); + if (actions == null) { + actions = provider.getActions(actionName); + if (actions != null) { + actionsMap.put(actionName, actions); + } else { + return null; + } + } + EdmActionImpl actionImpl = null; + // Search for first unbound action + for (Action action : actions) { + if (action.isBound() == false) { + actionImpl = new EdmActionImpl(this, actionName, action); + break; + } + } + return actionImpl; + } catch (ODataException e) { + throw new EdmException(e); + } + } + + @Override + protected EdmFunction createUnboundFunction(FullQualifiedName functionName, List parameterNames) { + try { + List functions = functionsMap.get(functionName); + if (functions == null) { + functions = provider.getFunctions(functionName); + if (functions != null) { + functionsMap.put(functionName, functions); + } else { + return null; + } + } + List parameterNamesCopy = parameterNames; + if (parameterNamesCopy == null) { + parameterNamesCopy = Collections.emptyList(); + } + EdmFunctionImpl functionImpl = null; + for (Function function : functions) { + if (function.isBound() == false) { + List providerParameters = function.getParameters(); + if (providerParameters == null) { + providerParameters = Collections.emptyList(); + } + if (parameterNamesCopy.size() == providerParameters.size()) { + List functionParameterNames = new ArrayList(); + for (Parameter parameter : providerParameters) { + functionParameterNames.add(parameter.getName()); + } + + if (parameterNamesCopy.containsAll(functionParameterNames)) { + functionImpl = new EdmFunctionImpl(this, functionName, function); + break; + } + } + } + } + return functionImpl; + } catch (ODataException e) { + throw new EdmException(e); + } + } + } diff --git a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmStructuralTypeImpl.java b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmStructuralTypeImpl.java index e0a24652b..0961a2f0c 100644 --- a/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmStructuralTypeImpl.java +++ b/odata4-lib/odata4-commons-core/src/main/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmStructuralTypeImpl.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import org.apache.olingo.odata4.commons.api.edm.EdmElement; +import org.apache.olingo.odata4.commons.api.edm.EdmException; import org.apache.olingo.odata4.commons.api.edm.EdmStructuralType; import org.apache.olingo.odata4.commons.api.edm.EdmType; import org.apache.olingo.odata4.commons.api.edm.constants.EdmTypeKind; @@ -32,7 +33,6 @@ import org.apache.olingo.odata4.commons.api.edm.provider.NavigationProperty; import org.apache.olingo.odata4.commons.api.edm.provider.Property; import org.apache.olingo.odata4.commons.api.edm.provider.StructuralType; - public abstract class EdmStructuralTypeImpl extends EdmTypeImpl implements EdmStructuralType { private final Map properties = new HashMap(); @@ -107,12 +107,13 @@ public abstract class EdmStructuralTypeImpl extends EdmTypeImpl implements EdmSt } return navigationPropertyNames; } - @Override public boolean compatibleTo(EdmType targetType) { EdmStructuralType sourceType = this; - + if (targetType == null) { + throw new EdmException("Target type must not be null"); + } while (sourceType.getName() != targetType.getName() || sourceType.getNamespace() != targetType.getNamespace()) { sourceType = sourceType.getBaseType(); diff --git a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKeyTest.java b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKeyTest.java index 54dbb1dbf..a740db2ea 100644 --- a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKeyTest.java +++ b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/ActionMapKeyTest.java @@ -20,7 +20,9 @@ package org.apache.olingo.odata4.commons.core.edm; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.fail; +import org.apache.olingo.odata4.commons.api.edm.EdmException; import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName; import org.junit.Test; @@ -29,56 +31,69 @@ public class ActionMapKeyTest { private final FullQualifiedName fqn = new FullQualifiedName("namespace", "name"); private final FullQualifiedName fqnType = new FullQualifiedName("namespace2", "name2"); + @Test + public void invalidParametersTest() { + createAndCheckForEdmException(null, null, null); + createAndCheckForEdmException(fqn, null, null); + createAndCheckForEdmException(fqn, fqnType, null); + createAndCheckForEdmException(fqn, null, true); + createAndCheckForEdmException(null, fqnType, true); + createAndCheckForEdmException(null, fqnType, null); + createAndCheckForEdmException(null, null, true); + + } + + private void createAndCheckForEdmException(FullQualifiedName fqn, FullQualifiedName typeName, Boolean collection) { + try { + new ActionMapKey(fqn, typeName, collection); + } catch (EdmException e) { + return; + } + fail("EdmException expected for parameters: " + fqn + " " + typeName + " " + collection); + } + @Test public void testEqualsMethod() { - ActionMapKey key1 = new ActionMapKey(fqn, null, null); - ActionMapKey someKey = new ActionMapKey(fqn, null, null); - assertEquals(key1, someKey); + ActionMapKey key; + ActionMapKey someKey; - key1 = new ActionMapKey(fqn, null, new Boolean(true)); - someKey = new ActionMapKey(fqn, null, true); - assertEquals(key1, someKey); - - key1 = new ActionMapKey(fqn, fqnType, false); + key = new ActionMapKey(fqn, fqnType, false); someKey = new ActionMapKey(fqn, fqnType, false); - assertEquals(key1, someKey); + assertEquals(key, someKey); - key1 = new ActionMapKey(fqn, fqnType, null); - someKey = new ActionMapKey(fqn, fqnType, null); - assertEquals(key1, someKey); - - key1 = new ActionMapKey(fqn, fqnType, true); - someKey = new ActionMapKey(fqn, fqnType, null); - assertNotSame(key1, someKey); - - key1 = new ActionMapKey(fqn, fqnType, true); + key = new ActionMapKey(fqn, fqnType, new Boolean(false)); someKey = new ActionMapKey(fqn, fqnType, false); - assertNotSame(key1, someKey); + assertEquals(key, someKey); - key1 = new ActionMapKey(fqn, null, true); + key = new ActionMapKey(fqn, fqnType, true); someKey = new ActionMapKey(fqn, fqnType, false); - assertNotSame(key1, someKey); + assertNotSame(key, someKey); - key1 = new ActionMapKey(fqn, null, true); - someKey = new ActionMapKey(fqn, null, false); - assertNotSame(key1, someKey); + key = new ActionMapKey(fqn, fqnType, true); + someKey = new ActionMapKey(fqn, fqnType, new Boolean(false)); + assertNotSame(key, someKey); } @Test public void testHashMethod() { - ActionMapKey key1 = new ActionMapKey(fqn, null, null); - ActionMapKey someKey = new ActionMapKey(fqn, null, null); - assertEquals(key1.hashCode(), someKey.hashCode()); - - key1 = new ActionMapKey(fqn, null, new Boolean(true)); - someKey = new ActionMapKey(fqn, null, true); - assertEquals(key1.hashCode(), someKey.hashCode()); - - someKey = new ActionMapKey(fqn, fqnType, true); - assertNotSame(key1.hashCode(), someKey.hashCode()); + ActionMapKey key; + ActionMapKey someKey; + key = new ActionMapKey(fqn, fqnType, false); someKey = new ActionMapKey(fqn, fqnType, false); - assertNotSame(key1.hashCode(), someKey.hashCode()); + assertEquals(key.hashCode(), someKey.hashCode()); + + key = new ActionMapKey(fqn, fqnType, new Boolean(false)); + someKey = new ActionMapKey(fqn, fqnType, false); + assertEquals(key.hashCode(), someKey.hashCode()); + + key = new ActionMapKey(fqn, fqnType, true); + someKey = new ActionMapKey(fqn, fqnType, false); + assertNotSame(key.hashCode(), someKey.hashCode()); + + key = new ActionMapKey(fqn, fqnType, true); + someKey = new ActionMapKey(fqn, fqnType, new Boolean(false)); + assertNotSame(key.hashCode(), someKey.hashCode()); } } diff --git a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCachingTest.java b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCachingTest.java index a117ac850..26dfc6ef1 100644 --- a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCachingTest.java +++ b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCachingTest.java @@ -21,7 +21,6 @@ package org.apache.olingo.odata4.commons.core.edm; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -142,7 +141,7 @@ public class EdmImplCachingTest { } @Test - public void cacheActionSimple() { + public void cacheUnboundAction() { EdmAction action = edm.getAction(NAME1, null, null); assertNotNull(action); @@ -158,37 +157,24 @@ public class EdmImplCachingTest { } @Test - public void cacheActionComlex() { - EdmAction action = edm.getAction(NAME1, null, null); + public void cacheBoundAction() { + EdmAction action = edm.getAction(NAME1, NAME2, true); assertNotNull(action); - EdmAction cachedAction = edm.getAction(NAME1, null, true); - assertNull(cachedAction); + EdmAction cachedAction = edm.getAction(NAME1, NAME2, true); + assertNotNull(cachedAction); - cachedAction = edm.getAction(NAME1, null, false); - assertNull(cachedAction); + assertTrue(action == cachedAction); + assertEquals(action, cachedAction); - cachedAction = edm.getAction(NAME1, NAME2, null); - assertNull(cachedAction); + EdmAction action2 = edm.getAction(NAME2, NAME2, true); + assertNotNull(action2); + assertNotSame(action, action2); - cachedAction = edm.getAction(NAME1, NAME2, true); - assertNull(cachedAction); - - cachedAction = edm.getAction(NAME1, NAME2, false); - assertNull(cachedAction); - - cachedAction = edm.getAction(NAME1, NAME1, null); - assertNull(cachedAction); - - cachedAction = edm.getAction(NAME1, NAME1, true); - assertNull(cachedAction); - - cachedAction = edm.getAction(NAME1, NAME1, false); - assertNull(cachedAction); } @Test - public void cacheFunctionSimple() { + public void cacheUnboundFunctionNoParameters() { EdmFunction function = edm.getFunction(NAME1, null, null, null); assertNotNull(function); @@ -205,35 +191,43 @@ public class EdmImplCachingTest { } @Test - public void cacheFunctionComplex() { - EdmFunction function = edm.getFunction(NAME1, null, null, null); + public void cacheBoundFunction() { + EdmFunction function = edm.getFunction(NAME1, NAME2, true, new ArrayList()); assertNotNull(function); - EdmFunction cachedfunction = edm.getFunction(NAME1, null, false, null); - assertNull(cachedfunction); + EdmFunction cachedfunction = edm.getFunction(NAME1, NAME2, true, new ArrayList()); + assertNotNull(cachedfunction); - cachedfunction = edm.getFunction(NAME1, null, true, null); - assertNull(cachedfunction); + assertTrue(function == cachedfunction); + assertEquals(function, cachedfunction); - cachedfunction = edm.getFunction(NAME1, null, new Boolean(true), null); - assertNull(cachedfunction); + EdmFunction function2 = edm.getFunction(NAME2, NAME2, true, new ArrayList()); + assertNotNull(function2); - cachedfunction = edm.getFunction(NAME1, NAME2, null, null); - assertNull(cachedfunction); - - cachedfunction = edm.getFunction(NAME1, NAME2, true, null); - assertNull(cachedfunction); - - cachedfunction = edm.getFunction(NAME1, NAME2, false, null); - assertNull(cachedfunction); - - cachedfunction = edm.getFunction(NAME1, null, null, new ArrayList()); - assertNull(cachedfunction); + assertNotSame(function, function2); } @Test - public void cacheFunctionComplexWithListContent() { + public void cacheUnboundFunctionWithParameters() { + ArrayList parameters1 = new ArrayList(); + parameters1.add("A"); + parameters1.add("B"); + EdmFunction function = edm.getFunction(NAME1, NAME2, true, parameters1); + assertNotNull(function); + ArrayList parameters2 = new ArrayList(); + parameters2.add("B"); + parameters2.add("A"); + EdmFunction cachedfunction = edm.getFunction(NAME1, NAME2, true, parameters2); + assertNotNull(cachedfunction); + + assertTrue(function == cachedfunction); + assertEquals(function, cachedfunction); + + EdmFunction function2 = edm.getFunction(NAME2, NAME2, true, new ArrayList()); + assertNotNull(function2); + + assertNotSame(function, function2); } @Test @@ -312,9 +306,9 @@ public class EdmImplCachingTest { } @Override - public EdmAction createAction(final FullQualifiedName fqn, final FullQualifiedName bindingParameterTypeName, + public EdmAction createBoundAction(final FullQualifiedName fqn, final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection) { - if (NAME1.equals(fqn) && bindingParameterTypeName == null && isBindingParameterCollection == null) { + if (NAME1.equals(fqn)) { EdmAction action = mock(EdmAction.class); when(action.getNamespace()).thenReturn(fqn.getNamespace()); when(action.getName()).thenReturn(fqn.getName()); @@ -329,10 +323,10 @@ public class EdmImplCachingTest { } @Override - public EdmFunction createFunction(final FullQualifiedName fqn, final FullQualifiedName bindingParameterTypeName, + public EdmFunction createBoundFunction(final FullQualifiedName fqn, + final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection, final List bindingParameterNames) { - if (NAME1.equals(fqn) && bindingParameterTypeName == null && isBindingParameterCollection == null - && bindingParameterNames == null) { + if (NAME1.equals(fqn)) { EdmFunction function = mock(EdmFunction.class); when(function.getNamespace()).thenReturn(fqn.getNamespace()); when(function.getName()).thenReturn(fqn.getName()); @@ -355,5 +349,37 @@ public class EdmImplCachingTest { protected Map createAliasToNamespaceInfo() { return new HashMap(); } + + @Override + protected EdmAction createUnboundAction(FullQualifiedName fqn) { + if (NAME1.equals(fqn)) { + EdmAction action = mock(EdmAction.class); + when(action.getNamespace()).thenReturn(fqn.getNamespace()); + when(action.getName()).thenReturn(fqn.getName()); + return action; + } else if (NAME2.equals(fqn)) { + EdmAction action = mock(EdmAction.class); + when(action.getNamespace()).thenReturn(fqn.getNamespace()); + when(action.getName()).thenReturn(fqn.getName()); + return action; + } + return null; + } + + @Override + protected EdmFunction createUnboundFunction(FullQualifiedName fqn, List parameterNames) { + if (NAME1.equals(fqn)) { + EdmFunction function = mock(EdmFunction.class); + when(function.getNamespace()).thenReturn(fqn.getNamespace()); + when(function.getName()).thenReturn(fqn.getName()); + return function; + } else if (NAME2.equals(fqn)) { + EdmFunction function = mock(EdmFunction.class); + when(function.getNamespace()).thenReturn(fqn.getNamespace()); + when(function.getName()).thenReturn(fqn.getName()); + return function; + } + return null; + } } } diff --git a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCallCreateTest.java b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCallCreateTest.java index 533869e66..46dd32e35 100644 --- a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCallCreateTest.java +++ b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/EdmImplCallCreateTest.java @@ -20,10 +20,12 @@ package org.apache.olingo.odata4.commons.core.edm; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -108,6 +110,13 @@ public class EdmImplCallCreateTest { assertNotNull(action); assertEquals(FQN.getNamespace(), action.getNamespace()); assertEquals(FQN.getName(), action.getName()); + + EdmAction action2 = edm.getAction(FQN, FQN, true); + assertNotNull(action2); + assertEquals(FQN.getNamespace(), action2.getNamespace()); + assertEquals(FQN.getName(), action2.getName()); + + assertNotSame(action, action2); assertNull(edm.getAction(WRONG_FQN, null, null)); } @@ -118,6 +127,13 @@ public class EdmImplCallCreateTest { assertNotNull(function); assertEquals(FQN.getNamespace(), function.getNamespace()); assertEquals(FQN.getName(), function.getName()); + + EdmFunction function2 = edm.getFunction(FQN, FQN, true, new ArrayList()); + assertNotNull(function2); + assertEquals(FQN.getNamespace(), function2.getNamespace()); + assertEquals(FQN.getName(), function2.getName()); + + assertNotSame(function, function2); assertNull(edm.getFunction(WRONG_FQN, null, null, null)); } @@ -189,7 +205,7 @@ public class EdmImplCallCreateTest { } @Override - public EdmAction createAction(final FullQualifiedName fqn, final FullQualifiedName bindingParameterTypeName, + public EdmAction createBoundAction(final FullQualifiedName fqn, final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection) { if (FQN.getNamespace().equals(fqn.getNamespace()) && FQN.getName().equals(fqn.getName())) { EdmAction action = mock(EdmAction.class); @@ -201,7 +217,7 @@ public class EdmImplCallCreateTest { } @Override - public EdmFunction createFunction(final FullQualifiedName fqn, final FullQualifiedName bindingParameterTypeName, + public EdmFunction createBoundFunction(final FullQualifiedName fqn, final FullQualifiedName bindingParameterTypeName, final Boolean isBindingParameterCollection, final List bindingParameterNames) { if (FQN.getNamespace().equals(fqn.getNamespace()) && FQN.getName().equals(fqn.getName())) { EdmFunction function = mock(EdmFunction.class); @@ -221,5 +237,27 @@ public class EdmImplCallCreateTest { protected Map createAliasToNamespaceInfo() { return new HashMap(); } + + @Override + protected EdmAction createUnboundAction(FullQualifiedName fqn) { + if (FQN.getNamespace().equals(fqn.getNamespace()) && FQN.getName().equals(fqn.getName())) { + EdmAction action = mock(EdmAction.class); + when(action.getNamespace()).thenReturn(fqn.getNamespace()); + when(action.getName()).thenReturn(fqn.getName()); + return action; + } + return null; + } + + @Override + protected EdmFunction createUnboundFunction(FullQualifiedName fqn, List parameterNames) { + if (FQN.getNamespace().equals(fqn.getNamespace()) && FQN.getName().equals(fqn.getName())) { + EdmFunction function = mock(EdmFunction.class); + when(function.getNamespace()).thenReturn(fqn.getNamespace()); + when(function.getName()).thenReturn(fqn.getName()); + return function; + } + return null; + } } } diff --git a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmComplexTypeImplTest.java b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmComplexTypeImplTest.java index 389247615..47f3e3304 100644 --- a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmComplexTypeImplTest.java +++ b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmComplexTypeImplTest.java @@ -19,6 +19,7 @@ package org.apache.olingo.odata4.commons.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; @@ -75,6 +76,21 @@ public class EdmComplexTypeImplTest { type = new EdmComplexTypeImpl(edm, name, 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()); diff --git a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImplOverloadingTest.java b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImplOverloadingTest.java new file mode 100644 index 000000000..0ea82dedb --- /dev/null +++ b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImplOverloadingTest.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.odata4.commons.core.edm.provider; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.odata4.commons.api.edm.Edm; +import org.apache.olingo.odata4.commons.api.edm.EdmAction; +import org.apache.olingo.odata4.commons.api.edm.EdmFunction; +import org.apache.olingo.odata4.commons.api.edm.provider.Action; +import org.apache.olingo.odata4.commons.api.edm.provider.EdmProvider; +import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName; +import org.apache.olingo.odata4.commons.api.edm.provider.Function; +import org.apache.olingo.odata4.commons.api.edm.provider.Parameter; +import org.junit.Before; +import org.junit.Test; + +public class EdmProviderImplOverloadingTest { + + private Edm edm; + private final FullQualifiedName FQN = new FullQualifiedName("testNamespace", "testName"); + private final FullQualifiedName WRONG_FQN = new FullQualifiedName("wrong", "wrong"); + + @Before + public void setup() throws Exception { + EdmProvider provider = mock(EdmProvider.class); + + Action action = new Action().setName(FQN.getName()); + List actions = new ArrayList(); + actions.add(action); + when(provider.getActions(FQN)).thenReturn(actions); + + Function function = new Function().setName(FQN.getName()).setParameters(new ArrayList()); + List functions = new ArrayList(); + functions.add(function); + when(provider.getFunctions(FQN)).thenReturn(functions); + edm = new EdmProviderImpl(provider); + } + + @Test + public void simpleActionGet() { + EdmAction action = edm.getAction(FQN, null, null); + assertNotNull(action); + assertEquals(FQN.getNamespace(), action.getNamespace()); + assertEquals(FQN.getName(), action.getName()); + + assertNull(edm.getAction(WRONG_FQN, null, null)); + } + + @Test + public void simpleFunctionGet() { + EdmFunction function = edm.getFunction(FQN, null, null, new ArrayList()); + assertNotNull(function); + assertEquals(FQN.getNamespace(), function.getNamespace()); + assertEquals(FQN.getName(), function.getName()); + + assertNull(edm.getFunction(WRONG_FQN, null, null, new ArrayList())); + } + +} diff --git a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImplTest.java b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImplTest.java index 362aea5b0..6bd6ed31a 100644 --- a/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImplTest.java +++ b/odata4-lib/odata4-commons-core/src/test/java/org/apache/olingo/odata4/commons/core/edm/provider/EdmProviderImplTest.java @@ -21,31 +21,32 @@ package org.apache.olingo.odata4.commons.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.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import org.apache.olingo.odata4.commons.api.edm.Edm; -import org.apache.olingo.odata4.commons.api.edm.EdmAction; import org.apache.olingo.odata4.commons.api.edm.EdmComplexType; import org.apache.olingo.odata4.commons.api.edm.EdmEntityContainer; import org.apache.olingo.odata4.commons.api.edm.EdmEntityType; import org.apache.olingo.odata4.commons.api.edm.EdmEnumType; -import org.apache.olingo.odata4.commons.api.edm.EdmFunction; +import org.apache.olingo.odata4.commons.api.edm.EdmException; import org.apache.olingo.odata4.commons.api.edm.EdmTypeDefinition; -import org.apache.olingo.odata4.commons.api.edm.provider.Action; +import org.apache.olingo.odata4.commons.api.edm.provider.AliasInfo; import org.apache.olingo.odata4.commons.api.edm.provider.ComplexType; import org.apache.olingo.odata4.commons.api.edm.provider.EdmProvider; import org.apache.olingo.odata4.commons.api.edm.provider.EntityContainerInfo; import org.apache.olingo.odata4.commons.api.edm.provider.EntityType; import org.apache.olingo.odata4.commons.api.edm.provider.EnumType; import org.apache.olingo.odata4.commons.api.edm.provider.FullQualifiedName; -import org.apache.olingo.odata4.commons.api.edm.provider.Function; -import org.apache.olingo.odata4.commons.api.edm.provider.Parameter; import org.apache.olingo.odata4.commons.api.edm.provider.PropertyRef; import org.apache.olingo.odata4.commons.api.edm.provider.TypeDefinition; +import org.apache.olingo.odata4.commons.api.exception.ODataException; import org.junit.Before; import org.junit.Test; @@ -75,19 +76,69 @@ public class EdmProviderImplTest { ComplexType complexType = new ComplexType().setName(FQN.getName()); when(provider.getComplexType(FQN)).thenReturn(complexType); - Action action = new Action().setName(FQN.getName()); - List actions = new ArrayList(); - actions.add(action); - when(provider.getActions(FQN)).thenReturn(actions); - - Function function = new Function().setName(FQN.getName()).setParameters(new ArrayList()); - List functions = new ArrayList(); - functions.add(function); - when(provider.getFunctions(FQN)).thenReturn(functions); + List aliasInfos = new ArrayList(); + aliasInfos.add(new AliasInfo().setAlias("alias").setNamespace("namespace")); + when(provider.getAliasInfos()).thenReturn(aliasInfos); edm = new EdmProviderImpl(provider); } + @Test + public void convertExceptionsTest() throws Exception{ + EdmProvider localProvider = mock(EdmProvider.class); + FullQualifiedName fqn = new FullQualifiedName("namespace", "name"); + when(localProvider.getEntityContainerInfo(fqn)).thenThrow(new ODataException("msg")); + when(localProvider.getEnumType(fqn)).thenThrow(new ODataException("msg")); + when(localProvider.getTypeDefinition(fqn)).thenThrow(new ODataException("msg")); + when(localProvider.getEntityType(fqn)).thenThrow(new ODataException("msg")); + when(localProvider.getComplexType(fqn)).thenThrow(new ODataException("msg")); + when(localProvider.getActions(fqn)).thenThrow(new ODataException("msg")); + when(localProvider.getFunctions(fqn)).thenThrow(new ODataException("msg")); + + Edm localEdm = new EdmProviderImpl(localProvider); + + callMethodAndExpectEdmException(localEdm, "getEntityContainer"); + callMethodAndExpectEdmException(localEdm, "getEnumType"); + callMethodAndExpectEdmException(localEdm, "getTypeDefinition"); + callMethodAndExpectEdmException(localEdm, "getEntityType"); + callMethodAndExpectEdmException(localEdm, "getComplexType"); + + //seperate because of signature + try { + localEdm.getAction(fqn, null, null); + } catch (EdmException e) { + assertEquals("org.apache.olingo.odata4.commons.api.exception.ODataException: msg", e.getMessage()); + } + + try { + localEdm.getFunction(fqn, null, null, null); + } catch (EdmException e) { + assertEquals("org.apache.olingo.odata4.commons.api.exception.ODataException: msg", e.getMessage()); + } + } + + private void callMethodAndExpectEdmException(Edm localEdm, String methodName) throws Exception { + Method method = localEdm.getClass().getMethod(methodName, FullQualifiedName.class); + try { + method.invoke(localEdm, new FullQualifiedName("namespace", "name")); + } catch (InvocationTargetException e) { + Throwable cause = e.getCause(); + if(cause instanceof EdmException){ + return; + } + } + fail("EdmException expected for method: " + methodName); + } + + @Test(expected = EdmException.class) + public void convertExceptionsAliasTest() throws Exception{ + EdmProvider localProvider = mock(EdmProvider.class); + when(localProvider.getAliasInfos()).thenThrow(new ODataException("msg")); + + Edm localEdm = new EdmProviderImpl(localProvider); + localEdm.getEntityContainer(null); + } + @Test public void getEntityContainer() { EdmEntityContainer entityContainer = edm.getEntityContainer(FQN); @@ -143,26 +194,6 @@ public class EdmProviderImplTest { assertNull(edm.getComplexType(WRONG_FQN)); } - @Test - public void getAction() { - EdmAction action = edm.getAction(FQN, null, null); - assertNotNull(action); - assertEquals(FQN.getNamespace(), action.getNamespace()); - assertEquals(FQN.getName(), action.getName()); - - assertNull(edm.getAction(WRONG_FQN, null, null)); - } - - @Test - public void getFunction() { - EdmFunction function = edm.getFunction(FQN, null, null, new ArrayList()); - assertNotNull(function); - assertEquals(FQN.getNamespace(), function.getNamespace()); - assertEquals(FQN.getName(), function.getName()); - - assertNull(edm.getFunction(WRONG_FQN, null, null, new ArrayList())); - } - @Test public void getServiceMetadata() { assertNotNull(edm.getServiceMetadata());