Skip to content

Commit

Permalink
feat: upgraded spring and polish (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamid646m committed Oct 11, 2022
1 parent e7a0507 commit 49f4dd9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 37 deletions.
34 changes: 23 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import org.springframework.boot.gradle.plugin.SpringBootPlugin

plugins {
id 'org.springframework.boot' version '2.3.5.RELEASE'
id 'io.github.gradle-nexus.publish-plugin' version '1.1.0'
id 'java'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.7.4' apply false
id 'io.github.gradle-nexus.publish-plugin' version '1.1.0'
}

apply plugin: 'io.spring.dependency-management'
Expand All @@ -12,8 +15,10 @@ group = 'org.casbin'
sourceCompatibility = '1.8'
compileJava.options.encoding = 'UTF-8'

bootJar.enabled = false
jar.enabled = true
jar {
enabled = true
archivesBaseName = 'casbin-spring-boot-starter'
}

repositories {
mavenLocal()
Expand All @@ -26,17 +31,24 @@ configurations {
}
}

dependencyManagement {
imports {
mavenBom SpringBootPlugin.BOM_COORDINATES
}
}

dependencies {
compileOnly 'org.casbin:jcasbin:1.21.0'
runtimeOnly 'com.h2database:h2:2.1.210'
compileOnly 'org.casbin:jcasbin:1.30.1'
compileOnly 'org.springframework.boot:spring-boot-starter-data-redis'
runtimeOnly 'com.h2database:h2:2.1.214'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.casbin:jcasbin-redis-watcher:1.4.1'
implementation 'org.casbin:jdbc-adapter:2.3.3'

annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

compileOnly 'org.springframework.boot:spring-boot-starter-data-redis'
testImplementation 'org.springframework.boot:spring-boot-starter-data-redis'
testRuntimeOnly 'mysql:mysql-connector-java:5.1.47'
testRuntimeOnly 'mysql:mysql-connector-java:8.0.30'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

implementation 'org.casbin:jcasbin-redis-watcher:1.4.0'
implementation 'org.casbin:jdbc-adapter:2.3.0'
testImplementation 'org.assertj:assertj-core'
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class CasbinAutoConfiguration {
@ConditionalOnMissingBean
public Adapter autoConfigFileAdapter(CasbinProperties properties) {
// if the file storage is chosen and the policy file location is set correctly, then create a file adapter
if (!StringUtils.isEmpty(properties.getPolicy())) {
if (StringUtils.hasText(properties.getPolicy())) {
try (InputStream policyInputStream = properties.getPolicyInputStream()) {
return new FileAdapter(policyInputStream);
} catch (Exception ignored) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/casbin/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
* @author fangzhengjin
Expand Down Expand Up @@ -44,7 +44,7 @@ public static InputStream getFileAsInputStream(String filePath) {
File file = getFile(filePath);
try {
if (file != null && file.exists()) {
return new FileInputStream(file);
return Files.newInputStream(file.toPath());
}
Resource resource = new DefaultResourceLoader().getResource(filePath);
if (resource.exists()) {
Expand Down
34 changes: 19 additions & 15 deletions src/test/java/org/casbin/EnforcerTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package org.casbin;

import org.assertj.core.api.Assert;
import org.casbin.jcasbin.main.Enforcer;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author fangzhengjin
Expand All @@ -17,14 +21,14 @@
* @date 2019-4-06 17:31
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class EnforcerTest {
@ExtendWith(SpringExtension.class)
class EnforcerTest {

@Autowired
private Enforcer enforcer;

@Test
public void test1() {
void test1() {
// user rights
enforcer.addPolicy("alice", "data1", "read");
enforcer.addPolicy("bob", "data2", "write");
Expand All @@ -35,11 +39,11 @@ public void test1() {
enforcer.addGroupingPolicy("alice", "data2_admin");

// Does bob have read access to data1? It must not be.
Assert.assertFalse(enforcer.enforce("bob", "data1", "read"));
assertFalse(enforcer.enforce("bob", "data1", "read"));

// Whether alice has read and write access to data2, it must be possible, otherwise, why is authorization used?
Assert.assertTrue(enforcer.enforce("alice", "data2", "read"));
Assert.assertTrue(enforcer.enforce("alice", "data2", "write"));
assertTrue(enforcer.enforce("alice", "data2", "read"));
assertTrue(enforcer.enforce("alice", "data2", "write"));

// repeat adding P strategy
enforcer.addPolicy("data2_admin", "data2", "write");
Expand All @@ -48,16 +52,16 @@ public void test1() {
// complete storage, built-in duplicate item filtering
enforcer.savePolicy();
// verify that duplicates are merged
Assert.assertEquals(4, enforcer.getNamedPolicy("p").size());
assertEquals(4, enforcer.getNamedPolicy("p").size());
}

@Test
public void test2() {
void test2() {
enforcer.clearPolicy();

Assert.assertFalse(enforcer.enforce("bob", "data1", "read"));
Assert.assertFalse(enforcer.enforce("alice", "data2", "read"));
Assert.assertFalse(enforcer.enforce("alice", "data2", "write"));
assertFalse(enforcer.enforce("bob", "data1", "read"));
assertFalse(enforcer.enforce("alice", "data2", "read"));
assertFalse(enforcer.enforce("alice", "data2", "write"));

}
}
17 changes: 9 additions & 8 deletions src/test/java/org/casbin/adapter/JdbcAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.FilteredAdapter;
import org.casbin.jcasbin.persist.file_adapter.FilteredAdapter.Filter;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.casbin.jcasbin.main.CoreEnforcer.newModel;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

/**
* @author shy
Expand All @@ -26,7 +27,7 @@
* @date 2020/12/24 18:10
*/
@SpringBootTest
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
public class JdbcAdapterTest {

@Resource
Expand Down Expand Up @@ -63,7 +64,7 @@ public void testLoadFilteredPolicy() {
// only policy rules that match the filter should be loaded,
// so the result is different from the loadPolicyResult.
this.filteredAdapter.loadFilteredPolicy(this.model, filter);
Assert.assertNotEquals(this.loadPolicyResult, this.model.savePolicyToText());
assertNotEquals(this.loadPolicyResult, this.model.savePolicyToText());

init();
getLoadPolicyResult();
Expand All @@ -82,7 +83,7 @@ public void testLoadFilteredPolicy() {
// there are no policy rules that match the filter,
// so the result is same as the loadPolicyResult.
this.filteredAdapter.loadFilteredPolicy(this.model, filter);
Assert.assertEquals(this.loadPolicyResult, this.model.savePolicyToText());
assertEquals(this.loadPolicyResult, this.model.savePolicyToText());
}

/**
Expand All @@ -96,7 +97,7 @@ public void testLoadFilteredPolicyEmptyFilter() {

// the filter is null, so the result is same as the loadPolicyResult.
this.filteredAdapter.loadFilteredPolicy(this.model, null);
Assert.assertEquals(this.loadPolicyResult, this.model.savePolicyToText());
assertEquals(this.loadPolicyResult, this.model.savePolicyToText());
}

/**
Expand Down

0 comments on commit 49f4dd9

Please sign in to comment.