diff --git a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImpl.java b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImpl.java index c9bb2c86b..a06f3a6bd 100644 --- a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImpl.java +++ b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImpl.java @@ -20,9 +20,9 @@ import com.alibaba.nacos.common.utils.StringUtils; import com.alibaba.nacos.persistence.configuration.condition.ConditionOnEmbeddedStorage; 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.EmbeddedPaginationHelperImpl; 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.context.annotation.Conditional; import org.springframework.stereotype.Component; @@ -45,16 +45,13 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe @Autowired private DatabaseOperate databaseOperate; - @Autowired - private EmbeddedStoragePersistServiceImpl persistService; - private static final String PATTERN_STR = "*"; - + private static final String SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE = " ESCAPE '\\' "; @Override public Page getPermissions(String role, int pageNo, int pageSize) { - PaginationHelper helper = persistService.createPaginationHelper(); + PaginationHelper helper = createPaginationHelper(); String sqlCountRows = "SELECT count(*) FROM permissions WHERE "; @@ -107,15 +104,15 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe EmbeddedStorageContextHolder.addSqlContext(sql, role, resource, action); databaseOperate.blockUpdate(); } - + @Override public Page findPermissionsLike4Page(String role, int pageNo, int pageSize) { - PaginationHelper helper = persistService.createPaginationHelper(); - + PaginationHelper helper = createPaginationHelper(); + String sqlCountRows = "SELECT count(*) FROM permissions "; - + String sqlFetchRows = "SELECT role,resource,action FROM permissions "; - + StringBuilder where = new StringBuilder(" WHERE 1=1"); List params = new ArrayList<>(); if (StringUtils.isNotBlank(role)) { @@ -123,11 +120,11 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe where.append(SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE); params.add(generateLikeArgument(role)); } - + Page pageInfo = helper .fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize, PERMISSION_ROW_MAPPER); - + if (pageInfo == null) { pageInfo = new Page<>(); pageInfo.setTotalCount(0); @@ -135,7 +132,7 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe } return pageInfo; } - + @Override public String generateLikeArgument(String s) { String underscore = "_"; @@ -150,5 +147,9 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe return s; } } - + + @Override + public PaginationHelper createPaginationHelper() { + return new EmbeddedPaginationHelperImpl<>(databaseOperate); + } } diff --git a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImpl.java b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImpl.java index 66c0cce6a..f92f95bed 100644 --- a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImpl.java +++ b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImpl.java @@ -17,12 +17,12 @@ package com.alibaba.nacos.plugin.auth.impl.persistence; 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.datasource.DynamicDataSource; import com.alibaba.nacos.persistence.model.Page; import com.alibaba.nacos.persistence.repository.PaginationHelper; -import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl; -import com.alibaba.nacos.config.server.utils.LogUtil; -import org.springframework.beans.factory.annotation.Autowired; +import com.alibaba.nacos.persistence.repository.extrnal.ExternalStoragePaginationHelperImpl; import org.springframework.context.annotation.Conditional; import org.springframework.jdbc.CannotGetJdbcConnectionException; import org.springframework.jdbc.core.JdbcTemplate; @@ -44,21 +44,18 @@ import static com.alibaba.nacos.plugin.auth.impl.persistence.AuthRowMapperManage @Component public class ExternalPermissionPersistServiceImpl implements PermissionPersistService { - @Autowired - private ExternalStoragePersistServiceImpl persistService; - private JdbcTemplate jt; - + private static final String PATTERN_STR = "*"; @PostConstruct protected void init() { - jt = persistService.getJdbcTemplate(); + jt = DynamicDataSource.getInstance().getDataSource().getJdbcTemplate(); } @Override public Page getPermissions(String role, int pageNo, int pageSize) { - PaginationHelper helper = persistService.createPaginationHelper(); + PaginationHelper helper = createPaginationHelper(); String sqlCountRows = "SELECT count(*) FROM permissions WHERE "; String sqlFetchRows = "SELECT role,resource,action FROM permissions WHERE "; @@ -128,40 +125,40 @@ public class ExternalPermissionPersistServiceImpl implements PermissionPersistSe throw e; } } - + @Override public Page findPermissionsLike4Page(String role, int pageNo, int pageSize) { - PaginationHelper helper = persistService.createPaginationHelper(); - + PaginationHelper helper = createPaginationHelper(); + String sqlCountRows = "SELECT count(*) FROM permissions "; String sqlFetchRows = "SELECT role,resource,action FROM permissions "; - + StringBuilder where = new StringBuilder(" WHERE 1=1"); List params = new ArrayList<>(); if (StringUtils.isNotBlank(role)) { where.append(" AND role LIKE ?"); params.add(generateLikeArgument(role)); } - + try { Page pageInfo = helper .fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize, PERMISSION_ROW_MAPPER); - + if (pageInfo == null) { pageInfo = new Page<>(); pageInfo.setTotalCount(0); pageInfo.setPageItems(new ArrayList<>()); } - + return pageInfo; - + } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e); throw e; } } - + @Override public String generateLikeArgument(String s) { String underscore = "_"; @@ -176,5 +173,9 @@ public class ExternalPermissionPersistServiceImpl implements PermissionPersistSe return s; } } - + + @Override + public PaginationHelper createPaginationHelper() { + return new ExternalStoragePaginationHelperImpl<>(jt); + } } diff --git a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/PermissionPersistService.java b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/PermissionPersistService.java index e0326c4a4..ca169d657 100644 --- a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/PermissionPersistService.java +++ b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/persistence/PermissionPersistService.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.plugin.auth.impl.persistence; import com.alibaba.nacos.persistence.model.Page; +import com.alibaba.nacos.persistence.repository.PaginationHelper; /** * Permission CRUD service. @@ -58,4 +59,12 @@ public interface PermissionPersistService { Page findPermissionsLike4Page(String role, int pageNo, int pageSize); String generateLikeArgument(String s); + + /** + * create Pagination utils. + * + * @param Generic object + * @return {@link PaginationHelper} + */ + PaginationHelper createPaginationHelper(); } diff --git a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImplTest.java b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImplTest.java index 1708a5c8d..ab81f8256 100644 --- a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImplTest.java +++ b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/EmbeddedPermissionPersistServiceImplTest.java @@ -17,10 +17,8 @@ package com.alibaba.nacos.plugin.auth.impl.persistence; 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.operate.DatabaseOperate; import com.alibaba.nacos.persistence.repository.embedded.sql.ModifyRequest; import org.junit.Assert; import org.junit.Before; @@ -33,32 +31,26 @@ import org.mockito.junit.MockitoJUnitRunner; import java.lang.reflect.Field; 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) public class EmbeddedPermissionPersistServiceImplTest { @Mock private DatabaseOperate databaseOperate; - @Mock - private PaginationHelper paginationHelper; - - @Mock - private EmbeddedStoragePersistServiceImpl embeddedStoragePersistService; - private EmbeddedPermissionPersistServiceImpl embeddedPermissionPersistService; @Before public void setUp() throws Exception { + when(databaseOperate.queryOne(any(String.class), any(Object[].class), eq(Integer.class))).thenReturn(0); embeddedPermissionPersistService = new EmbeddedPermissionPersistServiceImpl(); Class embeddedPermissionPersistServiceClass = EmbeddedPermissionPersistServiceImpl.class; Field databaseOperateF = embeddedPermissionPersistServiceClass.getDeclaredField("databaseOperate"); databaseOperateF.setAccessible(true); databaseOperateF.set(embeddedPermissionPersistService, databaseOperate); - - Field persistService = embeddedPermissionPersistServiceClass.getDeclaredField("persistService"); - persistService.setAccessible(true); - persistService.set(embeddedPermissionPersistService, embeddedStoragePersistService); - Mockito.when(embeddedStoragePersistService.createPaginationHelper()).thenReturn(paginationHelper); } @Test diff --git a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImplTest.java b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImplTest.java index 089bd6961..15aea1df7 100644 --- a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImplTest.java +++ b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/persistence/ExternalPermissionPersistServiceImplTest.java @@ -16,9 +16,11 @@ 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.repository.PaginationHelper; -import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -30,40 +32,50 @@ import org.springframework.jdbc.core.JdbcTemplate; 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) public class ExternalPermissionPersistServiceImplTest { - @Mock - private ExternalStoragePersistServiceImpl externalStoragePersistService; - @Mock private JdbcTemplate jdbcTemplate; @Mock - private PaginationHelper paginationHelper; + private DataSourceService dataSourceService; + + private boolean embeddedStorageCache; + + private DataSourceService dataSourceServiceCache; private ExternalPermissionPersistServiceImpl externalPermissionPersistService; @Before public void setUp() throws Exception { externalPermissionPersistService = new ExternalPermissionPersistServiceImpl(); - - Class externalPermissionPersistServiceClass = ExternalPermissionPersistServiceImpl.class; - Field persistServiceClassDeclaredField = externalPermissionPersistServiceClass - .getDeclaredField("persistService"); - persistServiceClassDeclaredField.setAccessible(true); - persistServiceClassDeclaredField.set(externalPermissionPersistService, externalStoragePersistService); - - Mockito.when(externalStoragePersistService.getJdbcTemplate()).thenReturn(jdbcTemplate); - Mockito.when(externalStoragePersistService.createPaginationHelper()).thenReturn(paginationHelper); - + when(jdbcTemplate.queryForObject(any(), any(), eq(Integer.class))).thenReturn(0); + when(dataSourceService.getJdbcTemplate()).thenReturn(jdbcTemplate); + embeddedStorageCache = DatasourceConfiguration.isEmbeddedStorage(); + DatasourceConfiguration.setEmbeddedStorage(false); + Field datasourceField = DynamicDataSource.class.getDeclaredField("basicDataSourceService"); + datasourceField.setAccessible(true); + dataSourceServiceCache = (DataSourceService) datasourceField.get(DynamicDataSource.getInstance()); + datasourceField.set(DynamicDataSource.getInstance(), dataSourceService); 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 public void testGetPermissions() { Page role = externalPermissionPersistService.getPermissions("role", 1, 10); - Assert.assertNotNull(role); }