Skip to content

Commit

Permalink
feat: Expose getter and setter of aviatorEval for more customization (#…
Browse files Browse the repository at this point in the history
…359)

* feat: Expose getter and setter of aviatorEval for more customization

* test: Add test cases for setAviatorEvaluator method
  • Loading branch information
HasonHuang authored Sep 17, 2023
1 parent a2067cc commit 7297b01
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/casbin/jcasbin/main/CoreEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/casbin/jcasbin/main/ManagementAPIUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

}

0 comments on commit 7297b01

Please sign in to comment.