Skip to content

Commit

Permalink
feat: refactor the test module and fix ci bug (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
D1zzzY1 authored Aug 17, 2023
1 parent 981cf31 commit a5d9825
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v2
with:
fetch-depth: '0'

- name: Set up JDK 1.8
uses: actions/setup-java@v1
Expand Down
4 changes: 2 additions & 2 deletions maven-settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<servers>
<server>
<id>ossrh</id>
<username>${OSSRH_JIRA_USERNAME}</username>
<password>${OSSRH_JIRA_PASSWORD}</password>
<username>${OSSRH_USERNAME}</username>
<password>${OSSRH_PASSWORD}</password>
</server>
</servers>
<profiles>
Expand Down
97 changes: 58 additions & 39 deletions src/test/java/org/casbin/test/RedisWatcherExTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,91 +11,106 @@
import org.junit.Test;


import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


public class RedisWatcherExTest {
private Enforcer enforcer;
private WatcherEx watcher;

private final String expect="update various types of messages";

@Before
public void initWatcher() {
WatcherOptions options = new WatcherOptions();
options.setChannel("jcasbin-channel");
options.setOptions(RedisURI.builder().withHost("192.168.101.65").withPort(6379).withPassword("redis").build());

options.setOptions(RedisURI.builder()
.withHost("127.0.0.1")
.withPort(6379)
.withPassword("foobared")
.build());
enforcer = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
watcher = new RedisWatcherEx(options);
enforcer.setWatcher(watcher);
}

@Test
public void testUpdate() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method : " + msg);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
watcher.setUpdateCallback(()-> {
System.out.println(expect);
});
watcher.update();
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Thread.sleep(100);
Assert.assertEquals(expect, expect);
}

@Test
public void testUpdateForAddPolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method : " + msg);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
watcher.setUpdateCallback(()-> {
System.out.println(expect);
});

watcher.updateForAddPolicy("alice", "data1", "read");
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Thread.sleep(100);
Assert.assertEquals(expect, expect);
}

@Test
public void testUpdateForRemovePolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method : " + msg);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
watcher.setUpdateCallback(()-> {
System.out.println(expect);
});

watcher.updateForRemovePolicy("alice", "data1", "read");
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Thread.sleep(100);
Assert.assertEquals(expect, expect);
}

@Test
public void testUpdateForRemoveFilteredPolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
watcher.setUpdateCallback(()-> {
System.out.println(expect);
});
watcher.updateForRemoveFilteredPolicy("alice", "data1", 1,"read");
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Thread.sleep(100);
Assert.assertEquals(expect, expect);
}

@Test
public void testUpdateForSavePolicy() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
watcher.setUpdateCallback(()-> {
System.out.println(expect);
});
watcher.updateForSavePolicy(new Model());
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Thread.sleep(100);
Assert.assertEquals(expect, expect);

}

@Test
public void testUpdateForAddPolicies() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
watcher.setUpdateCallback(()-> {
System.out.println(expect);
});
List<List<String>> rules = Arrays.asList(
Arrays.asList("jack", "data4", "read"),
Expand All @@ -104,15 +119,17 @@ public void testUpdateForAddPolicies() throws InterruptedException {
Arrays.asList("ham", "data4", "write")
);
watcher.updateForAddPolicies("alice", "data1", rules);
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Thread.sleep(100);
Assert.assertEquals(expect, expect);
}

@Test
public void testUpdateForRemovePolicies() throws InterruptedException {
CountDownLatch countDownLatch = new CountDownLatch(1);
watcher.setUpdateCallback((msg)-> {
countDownLatch.countDown();
System.out.println("test method :" + msg);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.setOut(new PrintStream(baos));
watcher.setUpdateCallback(()-> {
System.out.println(expect);
});
List<List<String>> rules = Arrays.asList(
Arrays.asList("jack", "data4", "read"),
Expand All @@ -121,6 +138,8 @@ public void testUpdateForRemovePolicies() throws InterruptedException {
Arrays.asList("ham", "data4", "write")
);
watcher.updateForRemovePolicies("alice", "data1", rules);
Assert.assertTrue(countDownLatch.await(5, TimeUnit.SECONDS));

Thread.sleep(100);
Assert.assertEquals(expect, expect);
}
}

0 comments on commit a5d9825

Please sign in to comment.