maheshbandal15 commented on code in PR #1310:
URL: https://github.com/apache/ranger/pull/1310#discussion_r4272636653


##########
hdfs-agent/src/test/java/org/apache/ranger/services/hdfs/RangerHdfsAuthorizerTest.java:
##########
@@ -240,6 +299,79 @@ private static INode createNode(String[] pathSegments, int 
i, String owner, Stri
         return mock;
     }
 
+    private static INode[] createPath(String path, boolean file, boolean 
exists) {
+        String[] segments = path.split("/");
+        INode[]  ret      = new INode[segments.length];
+
+        for (int i = 0; i < segments.length; i++) {
+            boolean isLast = i == segments.length - 1;
+
+            ret[i] = isLast && !exists ? null : createNode(segments, i, 
FILE_OWNER, FILE_GROUP, isLast && file);
+        }
+
+        return ret;
+    }
+
+    private static void runWithAuthzOptimizationEnabled(boolean enabled, 
ThrowingRunnable runnable) throws Exception {
+        boolean previous = setAuthzOptimizationEnabled(enabled);
+
+        try {
+            runnable.run();
+        } finally {
+            setAuthzOptimizationEnabled(previous);
+        }
+    }
+
+    private static boolean setAuthzOptimizationEnabled(boolean enabled) throws 
ReflectiveOperationException {
+        Field   pluginField = 
RangerHdfsAuthorizer.class.getDeclaredField("rangerPlugin");
+        Object  plugin;
+        Field   flagField;
+        boolean previous;
+
+        pluginField.setAccessible(true);
+        plugin    = pluginField.get(authorizer);
+        flagField = 
plugin.getClass().getDeclaredField("authzOptimizationEnabled");
+        flagField.setAccessible(true);
+        previous = flagField.getBoolean(plugin);
+        flagField.setBoolean(plugin, enabled);
+
+        return previous;
+    }

Review Comment:
   rangerPlugin & authzOptimizationEnabled are private field names and must be 
referenced exactly when using Java reflection to access private objects outside 
the class. This is similar to referencing a private object within the same 
class, since renaming/refactoring also requires updating its references.
   Another approach is creating a temp config file with OutputStreamWriter and 
passing it to the RangerHdfsAuthorizer() constructor, but that must be done at 
init and clutters test code when we toggle 
ranger.hdfs.authz.enable.optimization in each test. Calling it once in setup() 
makes that the default for all tests.
   Therefore the current approach is cleaner and more readable and lets us 
switch the optimization flag at runtime. This doesn’t affect production code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to