Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose getter and setter of aviatorEval for more customization #359

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}
Loading