Skip to content

Commit

Permalink
Merge pull request #8 from tangyang9464/select-db
Browse files Browse the repository at this point in the history
feat: add select db support
  • Loading branch information
hsluoyz authored Sep 29, 2021
2 parents 714af60 + 91433d7 commit dbfa188
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/maven-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 12
node-version: 14

- name: Sematic Release
run: |
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/casbin/adapter/RedisAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ public void close() {
jedis.close();
}

public void selectDb(int dbIndex) {
if(jedis != null) {
jedis.select(dbIndex);
}
}

private void newRedisAdapter(String host, int port, String key, String password) {
this.key = key;

Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/casbin/adapter/RedisAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ public void testAdapter() {
redisAdapter.close();
}

@Test
public void testSelectDb() {
Enforcer enforcer = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
redisAdapter = new RedisAdapter(host, port);
// save policy to db 0
redisAdapter.savePolicy(enforcer.getModel());
// select db to 1
redisAdapter.selectDb(1);
// add policy to db 1
redisAdapter.addPolicy("p", "p", Arrays.asList("paul", "data2", "read"));
enforcer.clearPolicy();
redisAdapter.loadPolicy(enforcer.getModel());
testGetPolicy(enforcer, Arrays.asList(Arrays.asList("paul", "data2", "read")));
}

private void testGetPolicy(Enforcer e, List<List<String>> res) {
List<List<String>> policies = e.getPolicy();
Assert.assertEquals(res, policies);
Expand Down

0 comments on commit dbfa188

Please sign in to comment.