PermissionPersistService add PageHelper.

This commit is contained in:
KomachiSion 2023-05-09 10:45:27 +08:00
parent cb65d53c7e
commit 6067779974
5 changed files with 81 additions and 66 deletions

View File

@ -20,9 +20,9 @@ import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.persistence.configuration.condition.ConditionOnEmbeddedStorage; import com.alibaba.nacos.persistence.configuration.condition.ConditionOnEmbeddedStorage;
import com.alibaba.nacos.persistence.model.Page; import com.alibaba.nacos.persistence.model.Page;
import com.alibaba.nacos.persistence.repository.PaginationHelper; import com.alibaba.nacos.persistence.repository.PaginationHelper;
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate; import com.alibaba.nacos.persistence.repository.embedded.EmbeddedPaginationHelperImpl;
import com.alibaba.nacos.config.server.service.repository.embedded.EmbeddedStoragePersistServiceImpl;
import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder; import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder;
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -45,16 +45,13 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
@Autowired @Autowired
private DatabaseOperate databaseOperate; private DatabaseOperate databaseOperate;
@Autowired
private EmbeddedStoragePersistServiceImpl persistService;
private static final String PATTERN_STR = "*"; private static final String PATTERN_STR = "*";
private static final String SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE = " ESCAPE '\\' "; private static final String SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE = " ESCAPE '\\' ";
@Override @Override
public Page<PermissionInfo> getPermissions(String role, int pageNo, int pageSize) { public Page<PermissionInfo> getPermissions(String role, int pageNo, int pageSize) {
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper(); PaginationHelper<PermissionInfo> helper = createPaginationHelper();
String sqlCountRows = "SELECT count(*) FROM permissions WHERE "; String sqlCountRows = "SELECT count(*) FROM permissions WHERE ";
@ -107,15 +104,15 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
EmbeddedStorageContextHolder.addSqlContext(sql, role, resource, action); EmbeddedStorageContextHolder.addSqlContext(sql, role, resource, action);
databaseOperate.blockUpdate(); databaseOperate.blockUpdate();
} }
@Override @Override
public Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize) { public Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize) {
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper(); PaginationHelper<PermissionInfo> helper = createPaginationHelper();
String sqlCountRows = "SELECT count(*) FROM permissions "; String sqlCountRows = "SELECT count(*) FROM permissions ";
String sqlFetchRows = "SELECT role,resource,action FROM permissions "; String sqlFetchRows = "SELECT role,resource,action FROM permissions ";
StringBuilder where = new StringBuilder(" WHERE 1=1"); StringBuilder where = new StringBuilder(" WHERE 1=1");
List<String> params = new ArrayList<>(); List<String> params = new ArrayList<>();
if (StringUtils.isNotBlank(role)) { if (StringUtils.isNotBlank(role)) {
@ -123,11 +120,11 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
where.append(SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE); where.append(SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(role)); params.add(generateLikeArgument(role));
} }
Page<PermissionInfo> pageInfo = helper Page<PermissionInfo> pageInfo = helper
.fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize, .fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize,
PERMISSION_ROW_MAPPER); PERMISSION_ROW_MAPPER);
if (pageInfo == null) { if (pageInfo == null) {
pageInfo = new Page<>(); pageInfo = new Page<>();
pageInfo.setTotalCount(0); pageInfo.setTotalCount(0);
@ -135,7 +132,7 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
} }
return pageInfo; return pageInfo;
} }
@Override @Override
public String generateLikeArgument(String s) { public String generateLikeArgument(String s) {
String underscore = "_"; String underscore = "_";
@ -150,5 +147,9 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
return s; return s;
} }
} }
@Override
public <E> PaginationHelper<E> createPaginationHelper() {
return new EmbeddedPaginationHelperImpl<>(databaseOperate);
}
} }

View File

@ -17,12 +17,12 @@
package com.alibaba.nacos.plugin.auth.impl.persistence; package com.alibaba.nacos.plugin.auth.impl.persistence;
import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.persistence.configuration.condition.ConditionOnExternalStorage; import com.alibaba.nacos.persistence.configuration.condition.ConditionOnExternalStorage;
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
import com.alibaba.nacos.persistence.model.Page; import com.alibaba.nacos.persistence.model.Page;
import com.alibaba.nacos.persistence.repository.PaginationHelper; import com.alibaba.nacos.persistence.repository.PaginationHelper;
import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl; import com.alibaba.nacos.persistence.repository.extrnal.ExternalStoragePaginationHelperImpl;
import com.alibaba.nacos.config.server.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.jdbc.CannotGetJdbcConnectionException; import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
@ -44,21 +44,18 @@ import static com.alibaba.nacos.plugin.auth.impl.persistence.AuthRowMapperManage
@Component @Component
public class ExternalPermissionPersistServiceImpl implements PermissionPersistService { public class ExternalPermissionPersistServiceImpl implements PermissionPersistService {
@Autowired
private ExternalStoragePersistServiceImpl persistService;
private JdbcTemplate jt; private JdbcTemplate jt;
private static final String PATTERN_STR = "*"; private static final String PATTERN_STR = "*";
@PostConstruct @PostConstruct
protected void init() { protected void init() {
jt = persistService.getJdbcTemplate(); jt = DynamicDataSource.getInstance().getDataSource().getJdbcTemplate();
} }
@Override @Override
public Page<PermissionInfo> getPermissions(String role, int pageNo, int pageSize) { public Page<PermissionInfo> getPermissions(String role, int pageNo, int pageSize) {
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper(); PaginationHelper<PermissionInfo> helper = createPaginationHelper();
String sqlCountRows = "SELECT count(*) FROM permissions WHERE "; String sqlCountRows = "SELECT count(*) FROM permissions WHERE ";
String sqlFetchRows = "SELECT role,resource,action FROM permissions WHERE "; String sqlFetchRows = "SELECT role,resource,action FROM permissions WHERE ";
@ -128,40 +125,40 @@ public class ExternalPermissionPersistServiceImpl implements PermissionPersistSe
throw e; throw e;
} }
} }
@Override @Override
public Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize) { public Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize) {
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper(); PaginationHelper<PermissionInfo> helper = createPaginationHelper();
String sqlCountRows = "SELECT count(*) FROM permissions "; String sqlCountRows = "SELECT count(*) FROM permissions ";
String sqlFetchRows = "SELECT role,resource,action FROM permissions "; String sqlFetchRows = "SELECT role,resource,action FROM permissions ";
StringBuilder where = new StringBuilder(" WHERE 1=1"); StringBuilder where = new StringBuilder(" WHERE 1=1");
List<String> params = new ArrayList<>(); List<String> params = new ArrayList<>();
if (StringUtils.isNotBlank(role)) { if (StringUtils.isNotBlank(role)) {
where.append(" AND role LIKE ?"); where.append(" AND role LIKE ?");
params.add(generateLikeArgument(role)); params.add(generateLikeArgument(role));
} }
try { try {
Page<PermissionInfo> pageInfo = helper Page<PermissionInfo> pageInfo = helper
.fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize, .fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize,
PERMISSION_ROW_MAPPER); PERMISSION_ROW_MAPPER);
if (pageInfo == null) { if (pageInfo == null) {
pageInfo = new Page<>(); pageInfo = new Page<>();
pageInfo.setTotalCount(0); pageInfo.setTotalCount(0);
pageInfo.setPageItems(new ArrayList<>()); pageInfo.setPageItems(new ArrayList<>());
} }
return pageInfo; return pageInfo;
} catch (CannotGetJdbcConnectionException e) { } catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e); LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
throw e; throw e;
} }
} }
@Override @Override
public String generateLikeArgument(String s) { public String generateLikeArgument(String s) {
String underscore = "_"; String underscore = "_";
@ -176,5 +173,9 @@ public class ExternalPermissionPersistServiceImpl implements PermissionPersistSe
return s; return s;
} }
} }
@Override
public <E> PaginationHelper<E> createPaginationHelper() {
return new ExternalStoragePaginationHelperImpl<>(jt);
}
} }

View File

@ -17,6 +17,7 @@
package com.alibaba.nacos.plugin.auth.impl.persistence; package com.alibaba.nacos.plugin.auth.impl.persistence;
import com.alibaba.nacos.persistence.model.Page; import com.alibaba.nacos.persistence.model.Page;
import com.alibaba.nacos.persistence.repository.PaginationHelper;
/** /**
* Permission CRUD service. * Permission CRUD service.
@ -58,4 +59,12 @@ public interface PermissionPersistService {
Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize); Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize);
String generateLikeArgument(String s); String generateLikeArgument(String s);
/**
* create Pagination utils.
*
* @param <E> Generic object
* @return {@link PaginationHelper}
*/
<E> PaginationHelper<E> createPaginationHelper();
} }

View File

@ -17,10 +17,8 @@
package com.alibaba.nacos.plugin.auth.impl.persistence; package com.alibaba.nacos.plugin.auth.impl.persistence;
import com.alibaba.nacos.persistence.model.Page; import com.alibaba.nacos.persistence.model.Page;
import com.alibaba.nacos.persistence.repository.PaginationHelper;
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
import com.alibaba.nacos.config.server.service.repository.embedded.EmbeddedStoragePersistServiceImpl;
import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder; import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder;
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
import com.alibaba.nacos.persistence.repository.embedded.sql.ModifyRequest; import com.alibaba.nacos.persistence.repository.embedded.sql.ModifyRequest;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -33,32 +31,26 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class EmbeddedPermissionPersistServiceImplTest { public class EmbeddedPermissionPersistServiceImplTest {
@Mock @Mock
private DatabaseOperate databaseOperate; private DatabaseOperate databaseOperate;
@Mock
private PaginationHelper paginationHelper;
@Mock
private EmbeddedStoragePersistServiceImpl embeddedStoragePersistService;
private EmbeddedPermissionPersistServiceImpl embeddedPermissionPersistService; private EmbeddedPermissionPersistServiceImpl embeddedPermissionPersistService;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
when(databaseOperate.queryOne(any(String.class), any(Object[].class), eq(Integer.class))).thenReturn(0);
embeddedPermissionPersistService = new EmbeddedPermissionPersistServiceImpl(); embeddedPermissionPersistService = new EmbeddedPermissionPersistServiceImpl();
Class<EmbeddedPermissionPersistServiceImpl> embeddedPermissionPersistServiceClass = EmbeddedPermissionPersistServiceImpl.class; Class<EmbeddedPermissionPersistServiceImpl> embeddedPermissionPersistServiceClass = EmbeddedPermissionPersistServiceImpl.class;
Field databaseOperateF = embeddedPermissionPersistServiceClass.getDeclaredField("databaseOperate"); Field databaseOperateF = embeddedPermissionPersistServiceClass.getDeclaredField("databaseOperate");
databaseOperateF.setAccessible(true); databaseOperateF.setAccessible(true);
databaseOperateF.set(embeddedPermissionPersistService, databaseOperate); databaseOperateF.set(embeddedPermissionPersistService, databaseOperate);
Field persistService = embeddedPermissionPersistServiceClass.getDeclaredField("persistService");
persistService.setAccessible(true);
persistService.set(embeddedPermissionPersistService, embeddedStoragePersistService);
Mockito.when(embeddedStoragePersistService.createPaginationHelper()).thenReturn(paginationHelper);
} }
@Test @Test

View File

@ -16,9 +16,11 @@
package com.alibaba.nacos.plugin.auth.impl.persistence; package com.alibaba.nacos.plugin.auth.impl.persistence;
import com.alibaba.nacos.persistence.configuration.DatasourceConfiguration;
import com.alibaba.nacos.persistence.datasource.DataSourceService;
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
import com.alibaba.nacos.persistence.model.Page; import com.alibaba.nacos.persistence.model.Page;
import com.alibaba.nacos.persistence.repository.PaginationHelper; import org.junit.After;
import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -30,40 +32,50 @@ import org.springframework.jdbc.core.JdbcTemplate;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ExternalPermissionPersistServiceImplTest { public class ExternalPermissionPersistServiceImplTest {
@Mock
private ExternalStoragePersistServiceImpl externalStoragePersistService;
@Mock @Mock
private JdbcTemplate jdbcTemplate; private JdbcTemplate jdbcTemplate;
@Mock @Mock
private PaginationHelper paginationHelper; private DataSourceService dataSourceService;
private boolean embeddedStorageCache;
private DataSourceService dataSourceServiceCache;
private ExternalPermissionPersistServiceImpl externalPermissionPersistService; private ExternalPermissionPersistServiceImpl externalPermissionPersistService;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
externalPermissionPersistService = new ExternalPermissionPersistServiceImpl(); externalPermissionPersistService = new ExternalPermissionPersistServiceImpl();
when(jdbcTemplate.queryForObject(any(), any(), eq(Integer.class))).thenReturn(0);
Class<ExternalPermissionPersistServiceImpl> externalPermissionPersistServiceClass = ExternalPermissionPersistServiceImpl.class; when(dataSourceService.getJdbcTemplate()).thenReturn(jdbcTemplate);
Field persistServiceClassDeclaredField = externalPermissionPersistServiceClass embeddedStorageCache = DatasourceConfiguration.isEmbeddedStorage();
.getDeclaredField("persistService"); DatasourceConfiguration.setEmbeddedStorage(false);
persistServiceClassDeclaredField.setAccessible(true); Field datasourceField = DynamicDataSource.class.getDeclaredField("basicDataSourceService");
persistServiceClassDeclaredField.set(externalPermissionPersistService, externalStoragePersistService); datasourceField.setAccessible(true);
dataSourceServiceCache = (DataSourceService) datasourceField.get(DynamicDataSource.getInstance());
Mockito.when(externalStoragePersistService.getJdbcTemplate()).thenReturn(jdbcTemplate); datasourceField.set(DynamicDataSource.getInstance(), dataSourceService);
Mockito.when(externalStoragePersistService.createPaginationHelper()).thenReturn(paginationHelper);
externalPermissionPersistService.init(); externalPermissionPersistService.init();
} }
@After
public void tearDown() throws NoSuchFieldException, IllegalAccessException {
DatasourceConfiguration.setEmbeddedStorage(embeddedStorageCache);
Field datasourceField = DynamicDataSource.class.getDeclaredField("basicDataSourceService");
datasourceField.setAccessible(true);
datasourceField.set(DynamicDataSource.getInstance(), dataSourceServiceCache);
}
@Test @Test
public void testGetPermissions() { public void testGetPermissions() {
Page<PermissionInfo> role = externalPermissionPersistService.getPermissions("role", 1, 10); Page<PermissionInfo> role = externalPermissionPersistService.getPermissions("role", 1, 10);
Assert.assertNotNull(role); Assert.assertNotNull(role);
} }