From b5df865872a84378416962495dc24ee896d54610 Mon Sep 17 00:00:00 2001 From: HasonHuang <258831020@qq.com> Date: Sun, 17 Sep 2023 17:37:18 +0800 Subject: [PATCH 1/2] feat: Expose getter and setter of aviatorEval for more customization --- .../org/casbin/jcasbin/main/CoreEnforcer.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java b/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java index 567b8045..3e102323 100644 --- a/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java +++ b/src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java @@ -149,6 +149,24 @@ public void setModel(Model model) { fm = FunctionMap.loadFunctionMap(); } + /** + * set the aviator evaluator + * + * @param evaluator aviator evaluator + */ + public void setAviatorEvaluator(AviatorEvaluatorInstance evaluator) { + this.aviatorEval = Objects.requireNonNull(evaluator, "The aviator evaluator cannot be null."); + } + + /** + * gets the current Aviator Evaluator instance + * + * @return Aviator Evaluator instance of enforcer + */ + public AviatorEvaluatorInstance getAviatorEval() { + return aviatorEval; + } + /** * getAdapter gets the current adapter. * From 38425199a11bdb5b919a63ff14e2f15b60adafb9 Mon Sep 17 00:00:00 2001 From: HasonHuang <258831020@qq.com> Date: Sun, 17 Sep 2023 19:24:01 +0800 Subject: [PATCH 2/2] test: Add test cases for setAviatorEvaluator method --- .../jcasbin/main/ManagementAPIUnitTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java b/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java index 69d55d65..e1f1a576 100644 --- a/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java +++ b/src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java @@ -14,6 +14,9 @@ package org.casbin.jcasbin.main; +import com.googlecode.aviator.AviatorEvaluator; +import com.googlecode.aviator.AviatorEvaluatorInstance; +import org.junit.Assert; import org.junit.Test; import java.util.List; @@ -203,4 +206,26 @@ public void testModifyGroupingPolicyAPI() { testGetUsers(e, "data2_admin", asList()); testGetUsers(e, "data3_admin", asList("eve")); } + + @Test + public void should_throwsNullPointException_when_setAviatorEvaluator_given_nullInstance() { + // given + AviatorEvaluatorInstance instance = null; + Enforcer enforcer = new Enforcer(); + // when + Assert.assertThrows("The aviator evaluator cannot be null.", NullPointerException.class, + () -> enforcer.setAviatorEvaluator(instance)); + } + + @Test + public void should_true_when_setAviatorEvaluator_given_customInstance() { + // given + AviatorEvaluatorInstance instance = AviatorEvaluator.newInstance(); + Enforcer enforcer = new Enforcer(); + // when + enforcer.setAviatorEvaluator(instance); + // then + Assert.assertEquals(instance, enforcer.getAviatorEval()); + } + }