Author: lquack
Date: Wed May  4 12:20:32 2016
New Revision: 1742257

URL: http://svn.apache.org/viewvc?rev=1742257&view=rev
Log:
QPID-7211: [Java Broker] Introduce request parameter to not transfer context 
variables

Modified:
    qpid/java/trunk/broker-plugins/management-http/pom.xml
    
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java
    
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
    
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java

Modified: qpid/java/trunk/broker-plugins/management-http/pom.xml
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/pom.xml?rev=1742257&r1=1742256&r2=1742257&view=diff
==============================================================================
--- qpid/java/trunk/broker-plugins/management-http/pom.xml (original)
+++ qpid/java/trunk/broker-plugins/management-http/pom.xml Wed May  4 12:20:32 
2016
@@ -126,6 +126,14 @@
       <scope>test</scope>
     </dependency>
 
+    <dependency>
+      <groupId>org.apache.qpid</groupId>
+      <artifactId>qpid-broker-core</artifactId>
+      <version>${project.version}</version>
+      <classifier>tests</classifier>
+      <scope>test</scope>
+    </dependency>
+
   </dependencies>
 
   <build>

Modified: 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java?rev=1742257&r1=1742256&r2=1742257&view=diff
==============================================================================
--- 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java
 (original)
+++ 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverter.java
 Wed May  4 12:20:32 2016
@@ -52,28 +52,20 @@ public class ConfiguredObjectToMapConver
 
     public Map<String, Object> convertObjectToMap(final ConfiguredObject<?> 
confObject,
                                                   Class<? extends 
ConfiguredObject> clazz,
-                                                  int depth,
-                                                  final boolean 
useActualValues,
-                                                  final boolean 
inheritedActuals,
-                                                  final boolean 
includeSystemContext,
-                                                  final boolean 
extractAsConfig,
-                                                  final int oversizeThreshold,
-                                                  final boolean 
isSecureTransport
+                                                  ConverterOptions 
converterOptions
                                                  )
     {
         Map<String, Object> object = new LinkedHashMap<>();
 
-        incorporateAttributesIntoMap(confObject, object, useActualValues, 
inheritedActuals, includeSystemContext,
-                                     extractAsConfig, oversizeThreshold, 
isSecureTransport);
-        if(!extractAsConfig)
+        incorporateAttributesIntoMap(confObject, object, converterOptions);
+        if(!converterOptions.isExtractAsConfig())
         {
             incorporateStatisticsIntoMap(confObject, object);
         }
 
-        if(depth > 0)
+        if(converterOptions.getDepth() > 0)
         {
-            incorporateChildrenIntoMap(confObject, clazz, depth, object, 
useActualValues, inheritedActuals,
-                                       includeSystemContext, extractAsConfig, 
oversizeThreshold, isSecureTransport);
+            incorporateChildrenIntoMap(confObject, clazz, object, 
converterOptions);
         }
         return object;
     }
@@ -82,15 +74,10 @@ public class ConfiguredObjectToMapConver
     private void incorporateAttributesIntoMap(
             final ConfiguredObject<?> confObject,
             Map<String, Object> object,
-            final boolean useActualValues,
-            final boolean inheritedActuals,
-            final boolean includeSystemContext,
-            final boolean extractAsConfig,
-            final int oversizeThreshold,
-            final boolean isSecureTransport)
+            ConverterOptions converterOptions)
     {
         // if extracting as config add a fake attribute for each secondary 
parent
-        if(extractAsConfig && 
confObject.getModel().getParentTypes(confObject.getCategoryClass()).size()>1)
+        if(converterOptions.isExtractAsConfig() && 
confObject.getModel().getParentTypes(confObject.getCategoryClass()).size()>1)
         {
             Iterator<Class<? extends ConfiguredObject>> parentClasses =
                     
confObject.getModel().getParentTypes(confObject.getCategoryClass()).iterator();
@@ -112,10 +99,10 @@ public class ConfiguredObjectToMapConver
 
         for(String name : confObject.getAttributeNames())
         {
-            if (!(extractAsConfig && 
CONFIG_EXCLUDED_ATTRIBUTES.contains(name)))
+            if (!(converterOptions.isExtractAsConfig() && 
CONFIG_EXCLUDED_ATTRIBUTES.contains(name)))
             {
                 Object value =
-                        useActualValues ? 
confObject.getActualAttributes().get(name) : confObject.getAttribute(name);
+                        converterOptions.isUseActualValues() ? 
confObject.getActualAttributes().get(name) : confObject.getAttribute(name);
                 if (value instanceof ConfiguredObject)
                 {
                     object.put(name, ((ConfiguredObject) value).getName());
@@ -123,17 +110,14 @@ public class ConfiguredObjectToMapConver
                 else if (ConfiguredObject.CONTEXT.equals(name))
                 {
                     Map<String, Object> contextValues = new HashMap<>();
-                    if (useActualValues)
+                    if (!converterOptions.isExcludeInheritedContext())
                     {
-                        collectContext(contextValues, confObject.getModel(), 
confObject, inheritedActuals);
-                    }
-                    else
-                    {
-                        for (String contextName : 
confObject.getContextKeys(!includeSystemContext))
-                        {
-                            contextValues.put(contextName, 
confObject.getContextValue(String.class, contextName));
-                        }
+                        
contextValues.putAll(confObject.getModel().getTypeRegistry().getDefaultContext());
+                        contextValues.putAll(System.getenv());
+                        contextValues.putAll((Map) System.getProperties());
                     }
+                    collectContext(contextValues, confObject, 
converterOptions);
+
                     if (!contextValues.isEmpty())
                     {
                         object.put(ConfiguredObject.CONTEXT, contextValues);
@@ -162,21 +146,21 @@ public class ConfiguredObjectToMapConver
                             .getAttributeTypes(confObject.getClass())
                             .get(name);
 
-                    if (attribute.isSecureValue(value) && !(isSecureTransport 
&& extractAsConfig))
+                    if (attribute.isSecureValue(value) && 
!(converterOptions.isSecureTransport() && converterOptions.isExtractAsConfig()))
                     {
                         // do not expose actual secure attribute value
                         // getAttribute() returns encoded value
                         value =  confObject.getAttribute(name);
                     }
 
-                    if(attribute.isOversized() && !extractAsConfig && 
!useActualValues)
+                    if(attribute.isOversized() && 
!converterOptions.isExtractAsConfig() && !converterOptions.isUseActualValues())
                     {
                         String valueString = String.valueOf(value);
-                        if(valueString.length() > oversizeThreshold)
+                        if(valueString.length() > 
converterOptions.getOversizeThreshold())
                         {
 
                             String replacementValue = 
"".equals(attribute.getOversizedAltText())
-                                    ? String.valueOf(value).substring(0, 
oversizeThreshold - 4) + "..."
+                                    ? String.valueOf(value).substring(0, 
converterOptions.getOversizeThreshold() - 4) + "..."
                                     : attribute.getOversizedAltText();
 
                             object.put(name, replacementValue);
@@ -191,7 +175,7 @@ public class ConfiguredObjectToMapConver
                         object.put(name, value);
                     }
                 }
-                else if (extractAsConfig)
+                else if (converterOptions.isExtractAsConfig())
                 {
                     ConfiguredObjectAttribute<?, ?> attribute = 
confObject.getModel()
                             .getTypeRegistry()
@@ -207,10 +191,11 @@ public class ConfiguredObjectToMapConver
         }
     }
 
-    private void collectContext(Map<String, Object> contextValues, Model 
model, ConfiguredObject<?> confObject, boolean inheritedContext)
+    private void collectContext(Map<String, Object> contextValues, 
ConfiguredObject<?> confObject, ConverterOptions options)
     {
+        Model model = confObject.getModel();
         Object value = 
confObject.getActualAttributes().get(ConfiguredObject.CONTEXT);
-        if (inheritedContext)
+        if (!options.isExcludeInheritedContext())
         {
             Collection<Class<? extends ConfiguredObject>> parents = 
model.getParentTypes(confObject.getCategoryClass());
             if(parents != null && !parents.isEmpty())
@@ -218,13 +203,23 @@ public class ConfiguredObjectToMapConver
                 ConfiguredObject parent = 
confObject.getParent(parents.iterator().next());
                 if(parent != null)
                 {
-                    collectContext(contextValues, model, parent, 
inheritedContext);
+                    collectContext(contextValues, parent, options);
                 }
             }
         }
         if (value instanceof Map)
         {
-            contextValues.putAll((Map<String,String>)value);
+            if (options.isUseActualValues())
+            {
+                contextValues.putAll((Map<String,String>)value);
+            }
+            else
+            {
+                for (String contextKey : ((Map<String,String>)value).keySet())
+                {
+                    contextValues.put(contextKey, 
confObject.getContextValue(String.class, contextKey));
+                }
+            }
         }
     }
 
@@ -244,14 +239,8 @@ public class ConfiguredObjectToMapConver
     private void incorporateChildrenIntoMap(
             final ConfiguredObject confObject,
             Class<? extends ConfiguredObject> clazz,
-            int depth,
             Map<String, Object> object,
-            final boolean useActualValues,
-            final boolean inheritedActuals,
-            final boolean includeSystemContext,
-            final boolean extractAsConfig,
-            final int oversizeThreshold,
-            final boolean isSecure)
+            ConverterOptions converterOptions)
     {
         List<Class<? extends ConfiguredObject>> childTypes = new 
ArrayList<>(confObject.getModel().getChildTypes(clazz));
 
@@ -263,9 +252,11 @@ public class ConfiguredObjectToMapConver
                 return o1.getSimpleName().compareTo(o2.getSimpleName());
             }
         });
+
+        ConverterOptions childConverterOptions = new 
ConverterOptions(converterOptions, converterOptions.getDepth() - 1);
         for(Class<? extends ConfiguredObject> childClass : childTypes)
         {
-            if(!(extractAsConfig && 
confObject.getModel().getParentTypes(childClass).iterator().next() != 
confObject.getCategoryClass()))
+            if(!(converterOptions.isExtractAsConfig() && 
confObject.getModel().getParentTypes(childClass).iterator().next() != 
confObject.getCategoryClass()))
             {
 
                 Collection children = confObject.getChildren(childClass);
@@ -283,19 +274,14 @@ public class ConfiguredObjectToMapConver
 
                     List<Map<String, Object>> childObjects = new ArrayList<>();
 
+
                     for (ConfiguredObject child : sortedChildren)
                     {
-                        if (!(extractAsConfig && !child.isDurable()))
+                        if (!(converterOptions.isExtractAsConfig() && 
!child.isDurable()))
                         {
                             childObjects.add(convertObjectToMap(child,
                                                                 childClass,
-                                                                depth - 1,
-                                                                
useActualValues,
-                                                                
inheritedActuals,
-                                                                
includeSystemContext,
-                                                                
extractAsConfig,
-                                                                
oversizeThreshold,
-                                                                isSecure));
+                                                                
childConverterOptions));
                         }
                     }
 
@@ -310,5 +296,68 @@ public class ConfiguredObjectToMapConver
     }
 
 
+    public static final class ConverterOptions
+    {
+        private final int _depth;
+        private final boolean _useActualValues;
+        private final boolean _extractAsConfig;
+        private final int _oversizeThreshold;
+        private final boolean _secureTransport;
+        private final boolean _excludeInheritedContext;
+
+        public ConverterOptions(ConverterOptions options, int depth)
+        {
+            this(depth,
+                 options.isUseActualValues(),
+                 options.isExtractAsConfig(),
+                 options.getOversizeThreshold(),
+                 options.isSecureTransport(),
+                 options.isExcludeInheritedContext());
+        }
 
+        public ConverterOptions(final int depth,
+                                final boolean useActualValues,
+                                final boolean extractAsConfig,
+                                final int oversizeThreshold,
+                                final boolean secureTransport,
+                                final boolean excludeInheritedContext)
+        {
+            _depth = depth;
+            _useActualValues = useActualValues;
+            _extractAsConfig = extractAsConfig;
+            _oversizeThreshold = oversizeThreshold;
+            _secureTransport = secureTransport;
+            _excludeInheritedContext = excludeInheritedContext;
+        }
+
+        public int getDepth()
+        {
+            return _depth;
+        }
+
+        public boolean isUseActualValues()
+        {
+            return _useActualValues;
+        }
+
+        public boolean isExtractAsConfig()
+        {
+            return _extractAsConfig;
+        }
+
+        public int getOversizeThreshold()
+        {
+            return _oversizeThreshold;
+        }
+
+        public boolean isSecureTransport()
+        {
+            return _secureTransport;
+        }
+
+        public boolean isExcludeInheritedContext()
+        {
+            return _excludeInheritedContext;
+        }
+    }
 }

Modified: 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java?rev=1742257&r1=1742256&r2=1742257&view=diff
==============================================================================
--- 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
 (original)
+++ 
qpid/java/trunk/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/RestServlet.java
 Wed May  4 12:20:32 2016
@@ -51,12 +51,12 @@ import org.apache.qpid.server.model.Abst
 import org.apache.qpid.server.model.Broker;
 import org.apache.qpid.server.model.ConfiguredObject;
 import org.apache.qpid.server.model.ConfiguredObjectOperation;
+import org.apache.qpid.server.model.Content;
 import org.apache.qpid.server.model.IllegalStateTransitionException;
 import org.apache.qpid.server.model.IntegrityViolationException;
-import org.apache.qpid.server.model.Content;
 import org.apache.qpid.server.model.Model;
-import org.apache.qpid.server.util.urlstreamhandler.data.Handler;
 import org.apache.qpid.server.util.ServerScopedRuntimeException;
+import org.apache.qpid.server.util.urlstreamhandler.data.Handler;
 import org.apache.qpid.util.DataUrlUtils;
 
 public class RestServlet extends AbstractServlet
@@ -74,13 +74,13 @@ public class RestServlet extends Abstrac
     public static final String INCLUDE_SYS_CONTEXT_PARAM = "includeSysContext";
     public static final String INHERITED_ACTUALS_PARAM = "inheritedActuals";
     public static final String EXTRACT_INITIAL_CONFIG_PARAM = 
"extractInitialConfig";
+    public static final String EXCLUDE_INHERITED_CONTEXT_PARAM = 
"excludeInheritedContext";
 
     /**
      * Signifies that the agent wishes the servlet to set the 
Content-Disposition on the
      * response with the value attachment.  This filename will be derived from 
the parameter value.
      */
     public static final String CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM = 
"contentDispositionAttachmentFilename";
-
     public static final Set<String> RESERVED_PARAMS =
             new HashSet<>(Arrays.asList(DEPTH_PARAM,
                                         SORT_PARAM,
@@ -89,7 +89,8 @@ public class RestServlet extends Abstrac
                                         INCLUDE_SYS_CONTEXT_PARAM,
                                         EXTRACT_INITIAL_CONFIG_PARAM,
                                         INHERITED_ACTUALS_PARAM,
-                                        
CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM));
+                                        
CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM,
+                                        EXCLUDE_INHERITED_CONTEXT_PARAM));
     public static final int DEFAULT_DEPTH = 1;
     public static final int DEFAULT_OVERSIZE = 120;
 
@@ -378,25 +379,82 @@ public class RestServlet extends Abstrac
 
                 int depth;
                 boolean actuals;
-                boolean includeSystemContext;
-                boolean inheritedActuals;
                 int oversizeThreshold;
+                boolean excludeInheritedContext;
 
                 if (extractInitialConfig)
                 {
                     depth = Integer.MAX_VALUE;
                     oversizeThreshold = Integer.MAX_VALUE;
                     actuals = true;
-                    includeSystemContext = false;
-                    inheritedActuals = false;
+                    excludeInheritedContext = true;
                 }
                 else
                 {
                     depth = getIntParameterFromRequest(request, DEPTH_PARAM, 
DEFAULT_DEPTH);
                     oversizeThreshold = getIntParameterFromRequest(request, 
OVERSIZE_PARAM, DEFAULT_OVERSIZE);
                     actuals = getBooleanParameterFromRequest(request, 
ACTUALS_PARAM);
-                    includeSystemContext = 
getBooleanParameterFromRequest(request, INCLUDE_SYS_CONTEXT_PARAM);
-                    inheritedActuals = getBooleanParameterFromRequest(request, 
INHERITED_ACTUALS_PARAM);
+                    String includeSystemContextParameter = 
request.getParameter(INCLUDE_SYS_CONTEXT_PARAM);
+                    String inheritedActualsParameter = 
request.getParameter(INHERITED_ACTUALS_PARAM);
+                    String excludeInheritedContextParameter = 
request.getParameter(EXCLUDE_INHERITED_CONTEXT_PARAM);
+
+                    if (excludeInheritedContextParameter == null)
+                    {
+                        /* backward (pre v6.1) compatible behaviour */
+                        if (inheritedActualsParameter == null && 
includeSystemContextParameter == null)
+                        {
+                            excludeInheritedContext = actuals;
+                        }
+                        else if (inheritedActualsParameter != null && 
includeSystemContextParameter != null)
+                        {
+                            if (actuals)
+                            {
+                                excludeInheritedContext = 
!Boolean.parseBoolean(inheritedActualsParameter);
+                            }
+                            else
+                            {
+                                excludeInheritedContext = 
!Boolean.parseBoolean(includeSystemContextParameter);
+                            }
+                        }
+                        else if (inheritedActualsParameter != null)
+                        {
+                            if (actuals)
+                            {
+                                excludeInheritedContext = 
!Boolean.parseBoolean(inheritedActualsParameter);
+                            }
+                            else
+                            {
+                                excludeInheritedContext = false;
+                            }
+                        }
+                        else
+                        {
+                            if (actuals)
+                            {
+                                excludeInheritedContext = true;
+                            }
+                            else
+                            {
+                                excludeInheritedContext = 
!Boolean.parseBoolean(includeSystemContextParameter);
+                            }
+                        }
+                    }
+                    else
+                    {
+                        if (inheritedActualsParameter != null || 
includeSystemContextParameter != null)
+                        {
+                            sendJsonErrorResponse(request,
+                                                  response,
+                                                  SC_UNPROCESSABLE_ENTITY,
+                                                  String.format(
+                                                          "Parameter '%s' 
cannot be specified together with '%s' or '%s'",
+                                                          
EXCLUDE_INHERITED_CONTEXT_PARAM,
+                                                          
INHERITED_ACTUALS_PARAM,
+                                                          
INCLUDE_SYS_CONTEXT_PARAM));
+                            return;
+                        }
+                        excludeInheritedContext = 
Boolean.parseBoolean(excludeInheritedContextParameter);
+                    }
                 }
 
                 List<Map<String, Object>> output = new ArrayList<>();
@@ -404,7 +462,13 @@ public class RestServlet extends Abstrac
                 {
 
                     
output.add(_objectConverter.convertObjectToMap(configuredObject, 
getConfiguredClass(),
-                            depth, actuals, inheritedActuals, 
includeSystemContext, extractInitialConfig, oversizeThreshold, 
request.isSecure()));
+                                                                   new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                           
depth,
+                                                                           
actuals,
+                                                                           
extractInitialConfig,
+                                                                           
oversizeThreshold,
+                                                                           
request.isSecure(),
+                                                                           
excludeInheritedContext)));
                 }
 
 
@@ -647,10 +711,18 @@ public class RestServlet extends Abstrac
         }
         else
         {
+            final ConfiguredObjectToMapConverter.ConverterOptions 
converterOptions =
+                    new 
ConfiguredObjectToMapConverter.ConverterOptions(DEFAULT_DEPTH,
+                                                                        false,
+                                                                        false,
+                                                                        
DEFAULT_OVERSIZE,
+                                                                        
request.isSecure(),
+                                                                        true);
             if 
(ConfiguredObject.class.isAssignableFrom(operation.getReturnType()))
             {
-                returnVal = 
_objectConverter.convertObjectToMap((ConfiguredObject<?>) returnVal, 
operation.getReturnType(), DEFAULT_DEPTH,
-                                                                false, false, 
false, false, DEFAULT_OVERSIZE, request.isSecure());
+                returnVal = 
_objectConverter.convertObjectToMap((ConfiguredObject<?>) returnVal,
+                                                                
operation.getReturnType(),
+                                                                
converterOptions);
             }
             else if (returnsCollectionOfConfiguredObjects(operation))
             {
@@ -658,8 +730,8 @@ public class RestServlet extends Abstrac
                 for (Object configuredObject : (Collection)returnVal)
                 {
                     
output.add(_objectConverter.convertObjectToMap((ConfiguredObject<?>) 
configuredObject,
-                               getCollectionMemberType((ParameterizedType) 
operation.getGenericReturnType()),
-                               DEFAULT_DEPTH, false, false, false, false, 
DEFAULT_OVERSIZE, request.isSecure()));
+                                                                   
getCollectionMemberType((ParameterizedType) operation.getGenericReturnType()),
+                                                                   
converterOptions));
                 }
                 returnVal = output;
             }
@@ -1020,7 +1092,16 @@ public class RestServlet extends Abstrac
 
     private boolean getBooleanParameterFromRequest(HttpServletRequest request, 
final String paramName)
     {
-        return Boolean.parseBoolean(request.getParameter(paramName));
+        return getBooleanParameterFromRequest(request, paramName, false);
     }
 
+    private boolean getBooleanParameterFromRequest(HttpServletRequest request, 
final String paramName, final boolean defaultValue)
+    {
+        String value = request.getParameter(paramName);
+        if (value == null)
+        {
+            return defaultValue;
+        }
+        return Boolean.parseBoolean(value);
+    }
 }

Modified: 
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java?rev=1742257&r1=1742256&r2=1742257&view=diff
==============================================================================
--- 
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java
 (original)
+++ 
qpid/java/trunk/broker-plugins/management-http/src/test/java/org/apache/qpid/server/management/plugin/servlet/rest/ConfiguredObjectToMapConverterTest.java
 Wed May  4 12:20:32 2016
@@ -41,6 +41,9 @@ import org.apache.qpid.server.model.Conf
 import org.apache.qpid.server.model.ConfiguredObjectMethodAttribute;
 import org.apache.qpid.server.model.ConfiguredObjectTypeRegistry;
 import org.apache.qpid.server.model.Model;
+import org.apache.qpid.server.model.testmodels.hierarchy.TestCar;
+import org.apache.qpid.server.model.testmodels.hierarchy.TestElecEngineImpl;
+import org.apache.qpid.server.model.testmodels.hierarchy.TestEngine;
 import org.apache.qpid.test.utils.QpidTestCase;
 
 public class ConfiguredObjectToMapConverterTest extends QpidTestCase
@@ -63,13 +66,13 @@ public class ConfiguredObjectToMapConver
 
         Map<String, Object> resultMap = 
_converter.convertObjectToMap(_configuredObject,
                                                                       
ConfiguredObject.class,
-                                                                      0,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      120,
-                                                                      false);
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
0,
+                                                                              
false,
+                                                                              
false,
+                                                                              
120,
+                                                                              
false,
+                                                                              
false));
         Map<String, Object> statsAsMap = (Map<String, Object>) 
resultMap.get(STATISTICS_MAP_KEY);
         assertNotNull("Statistics should be part of map", statsAsMap);
         assertEquals("Unexpected number of statistics", 1, statsAsMap.size());
@@ -86,13 +89,13 @@ public class ConfiguredObjectToMapConver
 
         Map<String, Object> resultMap = 
_converter.convertObjectToMap(_configuredObject,
                                                                       
ConfiguredObject.class,
-                                                                      0,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      120,
-                                                                      false);
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
0,
+                                                                              
false,
+                                                                              
false,
+                                                                              
120,
+                                                                              
false,
+                                                                              
false));
         assertEquals("Unexpected number of attributes", 1, resultMap.size());
         assertEquals("Unexpected attribute value", attributeValue, 
resultMap.get(attributeName));
     }
@@ -111,13 +114,13 @@ public class ConfiguredObjectToMapConver
 
         Map<String, Object> resultMap = 
_converter.convertObjectToMap(_configuredObject,
                                                                       
ConfiguredObject.class,
-                                                                      0,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      120,
-                                                                      false);
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
0,
+                                                                              
false,
+                                                                              
false,
+                                                                              
120,
+                                                                              
false,
+                                                                              
false));
         assertEquals("Unexpected number of attributes", 1, resultMap.size());
         assertEquals("Unexpected attribute value", 
"attributeConfiguredObjectName", resultMap.get(attributeName));
     }
@@ -137,13 +140,13 @@ public class ConfiguredObjectToMapConver
 
         Map<String, Object> resultMap = 
_converter.convertObjectToMap(_configuredObject,
                                                                       
ConfiguredObject.class,
-                                                                      1,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      120,
-                                                                      false);
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
1,
+                                                                              
false,
+                                                                              
false,
+                                                                              
120,
+                                                                              
false,
+                                                                              
false));
         assertEquals("Unexpected parent map size", 1, resultMap.size());
 
         final List<Map<String, Object>> childList = (List<Map<String, 
Object>>) resultMap.get("testchilds");
@@ -182,13 +185,13 @@ public class ConfiguredObjectToMapConver
 
         Map<String, Object> resultMap = 
_converter.convertObjectToMap(_configuredObject,
                                                                       
ConfiguredObject.class,
-                                                                      1,
-                                                                      true,
-                                                                      false,
-                                                                      false,
-                                                                      false,
-                                                                      120,
-                                                                      false);
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
1,
+                                                                              
true,
+                                                                              
false,
+                                                                              
120,
+                                                                              
false,
+                                                                              
true));
         assertEquals("Unexpected parent map size", 2, resultMap.size());
         assertEquals("Incorrect context", 
resultMap.get(ConfiguredObject.CONTEXT), actualContext);
         List<Map<String, Object>> childList = (List<Map<String, Object>>) 
resultMap.get("testchilds");
@@ -201,18 +204,16 @@ public class ConfiguredObjectToMapConver
 
         resultMap = _converter.convertObjectToMap(_configuredObject,
                                                   ConfiguredObject.class,
-                                                  1,
-                                                  false,
-                                                  false,
-                                                  false,
-                                                  false,
-                                                  120,
-                                                  false);
+                                                  new 
ConfiguredObjectToMapConverter.ConverterOptions(1,
+                                                                               
                       false,
+                                                                               
                       false,
+                                                                               
                       120,
+                                                                               
                       false,
+                                                                               
                       true));
         assertEquals("Unexpected parent map size", 2, resultMap.size());
-        Map<String, Object> inheritedContext = new HashMap<>();
-        inheritedContext.put("key","value");
-        inheritedContext.put("inheritedkey","foo");
-        assertEquals("Incorrect context", inheritedContext, 
resultMap.get(ConfiguredObject.CONTEXT));
+        Map<String, Object> effectiveContext = new HashMap<>();
+        effectiveContext.put("key","value");
+        assertEquals("Incorrect context", effectiveContext, 
resultMap.get(ConfiguredObject.CONTEXT));
         childList = (List<Map<String, Object>>) resultMap.get("testchilds");
         assertEquals("Unexpected number of children", 1, childList.size());
         childMap = childList.get(0);
@@ -243,14 +244,14 @@ public class ConfiguredObjectToMapConver
 
 
          Map<String, Object> resultMap = 
_converter.convertObjectToMap(_configuredObject,
-                                                                            
ConfiguredObject.class,
-                                                                            1,
-                                                                            
false,
-                                                                            
false,
-                                                                            
false,
-                                                                            
false,
-                                                                            20,
-                                                                            
false);
+                                                                       
ConfiguredObject.class,
+                                                                       new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                               
1,
+                                                                               
false,
+                                                                               
false,
+                                                                               
20,
+                                                                               
false,
+                                                                               
false));
         Object children = resultMap.get("testchilds");
         assertNotNull(children);
         assertTrue(children instanceof Collection);
@@ -263,13 +264,12 @@ public class ConfiguredObjectToMapConver
 
         resultMap = _converter.convertObjectToMap(_configuredObject,
                                                   ConfiguredObject.class,
-                                                  1,
-                                                  false,
-                                                  false,
-                                                  false,
-                                                  false,
-                                                  8,
-                                                  false);
+                                                  new 
ConfiguredObjectToMapConverter.ConverterOptions(1,
+                                                                               
                       false,
+                                                                               
                       false,
+                                                                               
                       8,
+                                                                               
                       false,
+                                                                               
                       false));
 
         children = resultMap.get("testchilds");
         assertNotNull(children);
@@ -286,13 +286,12 @@ public class ConfiguredObjectToMapConver
 
         resultMap = _converter.convertObjectToMap(_configuredObject,
                                                   ConfiguredObject.class,
-                                                  1,
-                                                  false,
-                                                  false,
-                                                  false,
-                                                  false,
-                                                  8,
-                                                  false);
+                                                  new 
ConfiguredObjectToMapConverter.ConverterOptions(1,
+                                                                               
                       false,
+                                                                               
                       false,
+                                                                               
                       8,
+                                                                               
                       false,
+                                                                               
                       false));
 
         children = resultMap.get("testchilds");
         assertNotNull(children);
@@ -331,14 +330,14 @@ public class ConfiguredObjectToMapConver
         when(mockChild.isDurable()).thenReturn(true);
 
         Map<String, Object> resultMap = 
_converter.convertObjectToMap(_configuredObject,
-                ConfiguredObject.class,
-                1,
-                false,
-                false,
-                false,
-                false,
-                20,
-                false);
+                                                                      
ConfiguredObject.class,
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
1,
+                                                                              
false,
+                                                                              
false,
+                                                                              
20,
+                                                                              
false,
+                                                                              
false));
         Object children = resultMap.get("testchilds");
         assertNotNull(children);
         assertTrue(children instanceof Collection);
@@ -348,14 +347,13 @@ public class ConfiguredObjectToMapConver
         assertEquals("*****", ((Map) attrs).get("secureAttribute"));
 
         resultMap = _converter.convertObjectToMap(_configuredObject,
-                ConfiguredObject.class,
-                1,
-                true,
-                true,
-                false,
-                true,
-                20,
-                true);
+                                                  ConfiguredObject.class,
+                                                  new 
ConfiguredObjectToMapConverter.ConverterOptions(1,
+                                                                               
                       true,
+                                                                               
                       true,
+                                                                               
                       20,
+                                                                               
                       true,
+                                                                               
                       false));
 
         children = resultMap.get("testchilds");
         assertNotNull(children);
@@ -366,14 +364,13 @@ public class ConfiguredObjectToMapConver
         assertEquals("secret", ((Map) attrs).get("secureAttribute"));
 
         resultMap = _converter.convertObjectToMap(_configuredObject,
-                ConfiguredObject.class,
-                1,
-                true,
-                true,
-                false,
-                false,
-                20,
-                true);
+                                                  ConfiguredObject.class,
+                                                  new 
ConfiguredObjectToMapConverter.ConverterOptions(1,
+                                                                               
                       true,
+                                                                               
                       false,
+                                                                               
                       20,
+                                                                               
                       true,
+                                                                               
                       false));
 
         children = resultMap.get("testchilds");
         assertNotNull(children);
@@ -384,6 +381,114 @@ public class ConfiguredObjectToMapConver
         assertEquals("*****", ((Map) attrs).get("secureAttribute"));
     }
 
+    public void testExcludeInheritedContext()
+    {
+        Model model = 
org.apache.qpid.server.model.testmodels.hierarchy.TestModel.getInstance();
+        final Map<String, Object> carAttributes = new HashMap<>();
+        carAttributes.put(ConfiguredObject.NAME, "myCar");
+        carAttributes.put(ConfiguredObject.CONTEXT, 
Collections.singletonMap("parentTest", "parentTestValue"));
+        TestCar car = model.getObjectFactory().create(TestCar.class,
+                                                      carAttributes);
+        final Map<String, Object> engineAttributes = new HashMap<>();
+        engineAttributes.put(ConfiguredObject.NAME, "myEngine");
+        engineAttributes.put(ConfiguredObject.TYPE, 
TestElecEngineImpl.TEST_ELEC_ENGINE_TYPE);
+        engineAttributes.put(ConfiguredObject.CONTEXT, 
Collections.singletonMap("test", "testValue"));
+        TestEngine engine = (TestEngine) car.createChild(TestEngine.class, 
engineAttributes);
+
+        Map<String, Object> resultMap = _converter.convertObjectToMap(engine,
+                                                                      
ConfiguredObject.class,
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
1,
+                                                                              
false,
+                                                                              
false,
+                                                                              
0,
+                                                                              
false,
+                                                                              
false));
+        Object contextValue = resultMap.get("context");
+        assertTrue("Unexpected type of context", contextValue instanceof Map);
+        assertTrue("Unexpected size of context", ((Map)contextValue).size() > 
1);
+        assertEquals("Unexpected context content", "testValue", 
((Map)contextValue).get("test"));
+        assertEquals("Unexpected context content", "parentTestValue", 
((Map)contextValue).get("parentTest"));
+
+        resultMap = _converter.convertObjectToMap(engine,
+                                                  ConfiguredObject.class,
+                                                  new 
ConfiguredObjectToMapConverter.ConverterOptions(1,
+                                                                               
                       false,
+                                                                               
                       false,
+                                                                               
                       0,
+                                                                               
                       false,
+                                                                               
                       true));
+        contextValue = resultMap.get("context");
+        assertTrue("Unexpected type of context", contextValue instanceof Map);
+        assertEquals("Unexpected size of context", 1, 
((Map)contextValue).size());
+        assertEquals("Unexpected context content","testValue", 
((Map)contextValue).get("test"));
+
+
+
+
+
+        /*
+
+
+
+//        Model model = createTestModel();
+        ConfiguredObjectTypeRegistry typeRegistry = model.getTypeRegistry();
+        Map<String, ConfiguredObjectAttribute<?, ?>> attributeTypes = 
typeRegistry.getAttributeTypes(TestChild.class);
+        ConfiguredObjectAttribute contextAttribute = 
mock(ConfiguredObjectMethodAttribute.class);
+        when(attributeTypes.get(eq("context"))).thenReturn(contextAttribute);
+
+        TestChild mockChild = mock(TestChild.class);
+        when(mockChild.getModel()).thenReturn(model);
+        when(mockChild.getCategoryClass()).thenReturn(TestChild.class);
+        
when(mockChild.getParent(Matchers.<Class>any())).thenReturn(_configuredObject);
+        when(_configuredObject.getModel()).thenReturn(model);
+        
when(_configuredObject.getChildren(TestChild.class)).thenReturn(Arrays.asList(mockChild));
+        
when(model.getParentTypes(TestChild.class)).thenReturn(Collections.<Class<? 
extends ConfiguredObject>>singleton(TestChild.class));
+        when(_configuredObject.getCategoryClass()).thenReturn(TestChild.class);
+
+
+        // set encoded value
+        final Map<String,String> context = new HashMap<>();
+        context.put("test", "testValue");
+        configureMockToReturnOneAttribute(mockChild, "context", context);
+        
when(mockChild.getContextKeys(anyBoolean())).thenReturn(context.keySet());
+        when(mockChild.getContextValue(String.class, 
"test")).thenReturn(context.get("test"));
+
+        final Map<String, String> parentContext = new HashMap<>();
+        parentContext.put("parentTest", "parentTestValue");
+        configureMockToReturnOneAttribute(_configuredObject, "context", 
parentContext);
+        
when(_configuredObject.getContextKeys(anyBoolean())).thenReturn(parentContext.keySet());
+        when(_configuredObject.getContextValue(String.class, 
"parentTest")).thenReturn(parentContext.get("parentTest"));
+
+        Map<String, Object> resultMap = 
_converter.convertObjectToMap(mockChild,
+                                                                      
ConfiguredObject.class,
+                                                                      new 
ConfiguredObjectToMapConverter.ConverterOptions(
+                                                                              
1,
+                                                                              
false,
+                                                                              
false,
+                                                                              
0,
+                                                                              
false,
+                                                                              
false));
+        Object contextValue = resultMap.get("context");
+        assertTrue("Unexpected type of context", contextValue instanceof Map);
+        assertEquals("Unexpected size of context", 2, 
((Map)contextValue).size());
+        assertEquals("Unexpected context content", "testValue", 
((Map)contextValue).get("test"));
+        assertEquals("Unexpected context content", "parentTestValue", 
((Map)contextValue).get("parentTest"));
+
+        resultMap = _converter.convertObjectToMap(mockChild,
+                                                  ConfiguredObject.class,
+                                                  new 
ConfiguredObjectToMapConverter.ConverterOptions(1,
+                                                                               
                       false,
+                                                                               
                       false,
+                                                                               
                       0,
+                                                                               
                       false,
+                                                                               
                       true));
+        contextValue = resultMap.get("context");
+        assertTrue("Unexpected type of context", contextValue instanceof Map);
+        assertEquals("Unexpected size of context", 1, 
((Map)contextValue).size());
+        assertEquals("Unexpected context content","testValue", 
((Map)contextValue).get("test"));*/
+    }
+
     private Model createTestModel()
     {
         Model model = mock(Model.class);



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to