|
| 1 | +// Copyright 2024 The casbin Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package org.casbin.jcasbin.main; |
| 16 | + |
| 17 | +import org.casbin.jcasbin.persist.Watcher; |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | +import java.util.concurrent.atomic.AtomicInteger; |
| 23 | +import java.util.function.Consumer; |
| 24 | + |
| 25 | +import static org.junit.Assert.fail; |
| 26 | + |
| 27 | +public class WatcherTest { |
| 28 | + public static class SampleWatcher implements Watcher { |
| 29 | + Consumer<String> callback; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void setUpdateCallback(Runnable runnable) { |
| 33 | + callback = (s) -> { |
| 34 | + runnable.run(); |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void setUpdateCallback(Consumer<String> func) { |
| 40 | + callback = func; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void update() { |
| 45 | + if (callback != null) { |
| 46 | + callback.accept(""); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testSetWatcher() { |
| 54 | + Enforcer enforcer = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv"); |
| 55 | + AtomicBoolean status = new AtomicBoolean(false); |
| 56 | + SampleWatcher thisIsTestWatcher = new SampleWatcher(); |
| 57 | + enforcer.setWatcher(thisIsTestWatcher); |
| 58 | + enforcer.watcher.setUpdateCallback((s) -> { |
| 59 | + status.set(true); |
| 60 | + }); |
| 61 | + enforcer.savePolicy();//calls watcher.Update() |
| 62 | + Assert.assertTrue(status.get()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testSelfModify() { |
| 67 | + Enforcer enforcer = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv"); |
| 68 | + SampleWatcher thisIsTestWatcher = new SampleWatcher(); |
| 69 | + enforcer.setWatcher(thisIsTestWatcher); |
| 70 | + AtomicInteger called = new AtomicInteger(-1); |
| 71 | + enforcer.watcher.setUpdateCallback((s) -> { |
| 72 | + called.set(1); |
| 73 | + }); |
| 74 | + boolean r = enforcer.addPolicy("eva", "data", "read");//calls watcher.Update() |
| 75 | + if (!r) { |
| 76 | + fail("addPolicy error"); |
| 77 | + } |
| 78 | + if (called.get() != 1) { |
| 79 | + fail("callback should be called"); |
| 80 | + } |
| 81 | + //todo SelfAddPolicy |
| 82 | + } |
| 83 | +} |
0 commit comments