实现core包下AppService,AuthorityService,QuotaService接口单元测试 & TopicQuotaData中bug修改

This commit is contained in:
xuguang
2021-12-07 14:08:09 +08:00
parent 60d038fe46
commit 8086ef355b
5 changed files with 415 additions and 37 deletions

View File

@@ -29,10 +29,10 @@ public class TopicQuotaData {
public static TopicQuotaData getClientData(Long producerByteRate, Long consumerByteRate) {
TopicQuotaData clientData = new TopicQuotaData();
if (!ValidateUtils.isNull(producerByteRate) && consumerByteRate != -1) {
if (!ValidateUtils.isNull(consumerByteRate) && consumerByteRate != -1) {
clientData.setConsumer_byte_rate(consumerByteRate.toString());
}
if (!ValidateUtils.isNull(consumerByteRate) && producerByteRate != -1) {
if (!ValidateUtils.isNull(producerByteRate) && producerByteRate != -1) {
clientData.setProducer_byte_rate(producerByteRate.toString());
}
return clientData;

View File

@@ -7,4 +7,5 @@ import com.xiaojukeji.kafka.manager.service.config.BaseTest;
* @Date 2021/12/6
*/
public class ConfigServiceTest extends BaseTest {
}

View File

@@ -23,9 +23,10 @@ public class AppServiceTest extends BaseTest {
@Autowired
private AppService appService;
private AppDO getAppDO() {
@DataProvider(name = "provideAppDO")
public static Object[][] provideAppDO() {
AppDO appDO = new AppDO();
appDO.setId(100L);
appDO.setId(4L);
appDO.setAppId("testAppId");
appDO.setName("testApp");
appDO.setPassword("testApp");
@@ -33,36 +34,38 @@ public class AppServiceTest extends BaseTest {
appDO.setApplicant("admin");
appDO.setPrincipals("admin");
appDO.setDescription("testApp");
appDO.setCreateTime(new Date());
appDO.setModifyTime(new Date());
return appDO;
appDO.setCreateTime(new Date(1638786493173L));
appDO.setModifyTime(new Date(1638786493173L));
return new Object[][] {{appDO}};
}
private AppDTO getAppDTO() {
@DataProvider(name = "provideAppDTO")
public Object[][] provideAppDTO() {
AppDTO appDTO = new AppDTO();
appDTO.setAppId("testAppId");
appDTO.setName("testApp");
appDTO.setPrincipals("admin");
appDTO.setDescription("testApp");
return appDTO;
return new Object[][] {{appDTO}};
}
@Test
public void addAppTest() {
addApp2SuccessTest();
addApp2DuplicateKeyTest();
@Test(dataProvider = "provideAppDO")
public void addAppTest(AppDO appDO) {
// 测试app添加成功
addApp2SuccessTest(appDO);
// 测试app添加失败键重复
addApp2DuplicateKeyTest(appDO);
// 测试app添加失败
addApp2MysqlErrorTest();
}
@Rollback(false)
private void addApp2SuccessTest() {
AppDO appDO = getAppDO();
private void addApp2SuccessTest(AppDO appDO) {
ResultStatus addAppResult = appService.addApp(appDO, "admin");
Assert.assertEquals(addAppResult.getCode(), ResultStatus.SUCCESS.getCode());
}
private void addApp2DuplicateKeyTest() {
AppDO appDO = getAppDO();
private void addApp2DuplicateKeyTest(AppDO appDO) {
ResultStatus addAppResult = appService.addApp(appDO, "admin");
Assert.assertEquals(addAppResult.getCode(), ResultStatus.RESOURCE_ALREADY_EXISTED.getCode());
}
@@ -73,15 +76,16 @@ public class AppServiceTest extends BaseTest {
Assert.assertEquals(addAppResult.getCode(), ResultStatus.MYSQL_ERROR.getCode());
}
@Test
public void deleteAppTest() {
deleteApp2SuccessTest();
@Test(dataProvider = "provideAppDO")
public void deleteAppTest(AppDO appDO) {
// 测试删除app成功
deleteApp2SuccessTest(appDO);
// 测试删除app失败
deleteApp2FailureTest();
}
@Rollback()
private void deleteApp2SuccessTest() {
AppDO appDO = getAppDO();
private void deleteApp2SuccessTest(AppDO appDO) {
int result = appService.deleteApp(appDO, "admin");
Assert.assertEquals(result, 1);
}
@@ -92,11 +96,14 @@ public class AppServiceTest extends BaseTest {
Assert.assertEquals(result, 0);
}
@Test
public void updateByAppIdTest() {
@Test(dataProvider = "provideAppDTO")
public void updateByAppIdTest(AppDTO appDTO) {
// 测试更新app时app不存在
updateByAppId2AppNotExistTest();
updateByAppId2UserWithoutAuthorityTest();
updateByAppId2SucessTest();
// 测试更新app时用户无权限
updateByAppId2UserWithoutAuthorityTest(appDTO);
// 测试更新app成功
updateByAppId2SucessTest(appDTO);
}
private void updateByAppId2AppNotExistTest() {
@@ -104,26 +111,28 @@ public class AppServiceTest extends BaseTest {
Assert.assertEquals(result.getCode(), ResultStatus.APP_NOT_EXIST.getCode());
}
private void updateByAppId2UserWithoutAuthorityTest() {
ResultStatus result = appService.updateByAppId(getAppDTO(), "xxx", false);
private void updateByAppId2UserWithoutAuthorityTest(AppDTO appDTO) {
ResultStatus result = appService.updateByAppId(appDTO, "xxx", false);
Assert.assertEquals(result.getCode(), ResultStatus.USER_WITHOUT_AUTHORITY.getCode());
}
@Rollback()
private void updateByAppId2SucessTest() {
ResultStatus result1 = appService.updateByAppId(getAppDTO(), "admin", false);
private void updateByAppId2SucessTest(AppDTO appDTO) {
ResultStatus result1 = appService.updateByAppId(appDTO, "admin", false);
Assert.assertEquals(result1.getCode(), ResultStatus.SUCCESS.getCode());
ResultStatus result2 = appService.updateByAppId(getAppDTO(), "admin", true);
ResultStatus result2 = appService.updateByAppId(appDTO, "admin", true);
Assert.assertEquals(result2.getCode(), ResultStatus.SUCCESS.getCode());
ResultStatus result3 = appService.updateByAppId(getAppDTO(), "xxx", true);
ResultStatus result3 = appService.updateByAppId(appDTO, "xxx", true);
Assert.assertEquals(result3.getCode(), ResultStatus.SUCCESS.getCode());
}
@Test
public void getAppByUserAndIdTest() {
// 测试查询app为空
getAppByUserAndId2NullTest();
// 测试查询app成功
getAppByUserAndId2SuccessTest();
}
@@ -142,28 +151,50 @@ public class AppServiceTest extends BaseTest {
@Test
public void verifyAppIdByPassword2DSucessTest() {
// 测试验证app成功
boolean result = appService.verifyAppIdByPassword("dkm_admin", "km_kMl4N8as1Kp0CCY");
Assert.assertTrue(result);
}
@Test(dataProvider = "provideAppIdAndPassword")
private void verifyAppIdByPassword2False(String appId, String password) {
public void verifyAppIdByPassword2False(String appId, String password) {
// 测试验证app失败情况
boolean result = appService.verifyAppIdByPassword(appId, password);
Assert.assertFalse(result);
}
@DataProvider(name = "provideAppIdAndPassword")
private Object[][] provideAppIdAndPassword() {
public Object[][] provideAppIdAndPassword() {
return new Object[][] {{"", ""}, {"dkm_admin", ""}, {"xxx", "km_kMl4N8as1Kp0CCY"}, {"dkm_admin", "xxx"}};
}
@Test
public void getAppTopicDTOList2Test() {
// 测试获取的集合为空
getAppTopicDTOList2EmptyList();
// 测试查询成功,且集合不为空
getAppTopicDTOList2Success();
// TODO 查询的其他分支
}
private void getAppTopicDTOList2EmptyList() {
List<AppTopicDTO> result = appService.getAppTopicDTOList("xxx", true);
Assert.assertTrue(result.isEmpty());
List<AppTopicDTO> result1 = appService.getAppTopicDTOList("xxx", true);
Assert.assertTrue(result1.isEmpty());
List<AppTopicDTO> result2 = appService.getAppTopicDTOList("dkm_admin", false);
Assert.assertTrue(result2.isEmpty());
List<AppTopicDTO> result3 = appService.getAppTopicDTOList("testAppId", true);
Assert.assertTrue(result3.isEmpty());
List<AppTopicDTO> result4 = appService.getAppTopicDTOList("testAppId", false);
Assert.assertTrue(result4.isEmpty());
}
private void getAppTopicDTOList2Success() {
List<AppTopicDTO> result = appService.getAppTopicDTOList("dkm_admin", true);
Assert.assertFalse(result.isEmpty());
}
}

View File

@@ -0,0 +1,196 @@
package com.xiaojukeji.kafka.manager.service.service.gateway;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.pojo.gateway.AuthorityDO;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.testng.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author xuguang
* @Date 2021/12/6
*/
public class AuthorityServiceTest extends BaseTest {
@Autowired
private AuthorityService authorityService;
@DataProvider(name = "provideAuthorityDO")
public Object[][] provideAuthorityDO() {
AuthorityDO authorityDO = new AuthorityDO();
authorityDO.setId(4L);
authorityDO.setAppId("testAppId");
authorityDO.setClusterId(1L);
authorityDO.setTopicName("moduleTest");
authorityDO.setAccess(2);
authorityDO.setCreateTime(new Date(1638786493173L));
authorityDO.setModifyTime(new Date(1638786493173L));
return new Object[][] {{authorityDO}};
}
@Test(dataProvider = "provideAuthorityDO")
@Rollback(value = false)
public void addAuthorityTest(AuthorityDO authorityDO) {
// 测试新增权限对象
addNewAuthority(authorityDO);
// 测试新旧对象权限一致
newAccessEqualOldAccessTest(authorityDO);
// 测试在原有对象上新增权限
addNewAuthorityAccessTest(authorityDO);
// 测试新增权限失败
addNewAuthority2Failure();
}
private void addNewAuthority(AuthorityDO authorityDO) {
int result = authorityService.addAuthority(authorityDO);
Assert.assertEquals( 1, result);
}
private void newAccessEqualOldAccessTest(AuthorityDO authorityDO) {
int result = authorityService.addAuthority(authorityDO);
Assert.assertEquals( 0, result);
}
private void addNewAuthorityAccessTest(AuthorityDO authorityDO) {
authorityDO.setAccess(3);
int result = authorityService.addAuthority(authorityDO);
Assert.assertEquals( 1, result);
}
private void addNewAuthority2Failure() {
int result = authorityService.addAuthority(new AuthorityDO());
Assert.assertEquals( 0, result);
}
public void deleteSpecifiedAccess() {
// 测试删除权限对象时无该对象
deleteSpecifiedAccess2AuthorityNotExist();
// 测试删除权限对象时参数错误, 传入的access为3数据库中为2
deleteSpecifiedAccess2ParamIllegal();
// 测试删除权限对象成功传入的access为3数据库中为3
deleteSpecifiedAccess2Success();
}
private void deleteSpecifiedAccess2AuthorityNotExist() {
ResultStatus result = authorityService.deleteSpecifiedAccess("xxx", 1L, "moduleTest", 2, "admin");
Assert.assertEquals(ResultStatus.AUTHORITY_NOT_EXIST.getCode(), result.getCode());
}
private void deleteSpecifiedAccess2ParamIllegal() {
ResultStatus result = authorityService.deleteSpecifiedAccess("dkm_admin", 1L, "xgTest", 3, "admin");
Assert.assertEquals(ResultStatus.PARAM_ILLEGAL.getCode(), result.getCode());
}
private void deleteSpecifiedAccess2Success() {
ResultStatus result = authorityService.deleteSpecifiedAccess("dkm_admin", 1L, "xgTest", 3, "admin");
Assert.assertEquals(ResultStatus.SUCCESS.getCode(), result.getCode());
}
@Test(dataProvider = "provideAuthorityDO")
public void getAuthorityTest(AuthorityDO authorityDO) {
// 测试查询成功
getAuthority2SuccessTest(authorityDO);
// 测试查询为null
getAuthority2NullTest(authorityDO);
}
private void getAuthority2SuccessTest(AuthorityDO authorityDO) {
AuthorityDO result = authorityService.getAuthority(1L, "moduleTest", "testAppId");
Assert.assertEquals(result.toString(), authorityDO.toString());
}
private void getAuthority2NullTest(AuthorityDO authorityDO) {
AuthorityDO result = authorityService.getAuthority(10L, "moduleTest", "testAppId");
Assert.assertNull(result);
}
@Test(dataProvider = "provideAuthorityDO")
public void getAuthorityByTopic(AuthorityDO authorityDO) {
// 测试查询成功
getAuthorityByTopic2SuccessTest(authorityDO);
// 测试查询为null
getAuthorityByTopic2NullTest();
}
private void getAuthorityByTopic2SuccessTest(AuthorityDO authorityDO) {
List<AuthorityDO> result = authorityService.getAuthorityByTopic(1L, "moduleTest");
Assert.assertEquals(result.size(), 1);
Assert.assertEquals(result.get(0).toString(), authorityDO.toString());
}
private void getAuthorityByTopic2NullTest() {
List<AuthorityDO> result = authorityService.getAuthorityByTopic(10L, "moduleTest");
Assert.assertTrue(result.isEmpty());
}
@Test(dataProvider = "provideAuthorityDO")
public void getAuthorityByAppIdTest(AuthorityDO authorityDO) {
// 测试查询成功
getAuthorityByAppId2SuccessTest(authorityDO);
// 测试查询为null
getAuthorityByAppId2NullTest();
}
private void getAuthorityByAppId2SuccessTest(AuthorityDO authorityDO) {
List<AuthorityDO> result = authorityService.getAuthority("testAppId");
Assert.assertEquals(result.size(), 1);
Assert.assertEquals(result.get(0).toString(), authorityDO.toString());
}
private void getAuthorityByAppId2NullTest() {
List<AuthorityDO> result = authorityService.getAuthority("xxx");
Assert.assertTrue(result.isEmpty());
}
@Test
public void listAllTest() {
List<AuthorityDO> result = authorityService.listAll();
Assert.assertEquals(result.size(), 2);
}
@Test
public void addAuthorityAndQuotaTest(AuthorityDO authorityDO) {
}
private void addAuthorityAndQuota2SuccessTest(AuthorityDO authorityDO) {
}
@Test
public void getAllAuthorityTest() {
Map<String, Map<Long, Map<String, AuthorityDO>>> allAuthority = authorityService.getAllAuthority();
Assert.assertEquals(allAuthority.size(), 2);
}
@Test
public void deleteAuthorityByTopicTest() {
// 测试查询成功
deleteAuthorityByTopic2SuccessTest();
// 测试查询为null
deleteAuthorityByTopic2FailureTest();
}
private void deleteAuthorityByTopic2SuccessTest() {
int result = authorityService.deleteAuthorityByTopic(1L, "moduleTest");
Assert.assertEquals(result, 1);
}
private void deleteAuthorityByTopic2FailureTest() {
int result = authorityService.deleteAuthorityByTopic(10L, "moduleTest");
Assert.assertEquals(result, 0);
}
}

View File

@@ -0,0 +1,150 @@
package com.xiaojukeji.kafka.manager.service.service.gateway;
import com.xiaojukeji.kafka.manager.common.entity.ResultStatus;
import com.xiaojukeji.kafka.manager.common.entity.ao.gateway.TopicQuota;
import com.xiaojukeji.kafka.manager.service.config.BaseTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* @author xuguang
* @Date 2021/12/6
*/
public class QuotaServiceTest extends BaseTest {
@Autowired
private QuotaService quotaService;
@DataProvider(name = "provideTopicQuota")
public static Object[][] provideTopicQuota() {
TopicQuota topicQuotaDO = new TopicQuota();
topicQuotaDO.setAppId("testAppId");
topicQuotaDO.setClusterId(1L);
topicQuotaDO.setTopicName("moduleTest");
topicQuotaDO.setProduceQuota(100000L);
topicQuotaDO.setConsumeQuota(100000L);
return new Object[][] {{topicQuotaDO}};
}
@Test(dataProvider = "provideTopicQuota")
public void addTopicQuotaTest(TopicQuota topicQuotaDO) {
// 测试新增成功
addTopicQuota2SuccessTest(topicQuotaDO);
// 测试新增失败
addTopicQuota2FailureTest(topicQuotaDO);
}
private void addTopicQuota2SuccessTest(TopicQuota topicQuotaDO) {
int result = quotaService.addTopicQuota(topicQuotaDO);
Assert.assertEquals(result, 1);
}
private void addTopicQuota2FailureTest(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(10L);
int result = quotaService.addTopicQuota(topicQuotaDO);
Assert.assertEquals(result, 0);
}
@Test(dataProvider = "provideTopicQuota")
public void addTopicQuotaWithAccessTest(TopicQuota topicQuotaDO) {
// 测试新增成功
addTopicQuotaWithAccess2SuccessTest(topicQuotaDO);
// 测试新增失败
addTopicQuotaWithAccess2FailureTest(topicQuotaDO);
}
private void addTopicQuotaWithAccess2SuccessTest(TopicQuota topicQuotaDO) {
int result = quotaService.addTopicQuota(topicQuotaDO, 2);
Assert.assertEquals(result, 1);
}
private void addTopicQuotaWithAccess2FailureTest(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(10L);
int result = quotaService.addTopicQuota(topicQuotaDO, 2);
Assert.assertEquals(result, 0);
}
@Test(dataProvider = "provideTopicQuota")
public void getQuotaFromZkTest(TopicQuota topicQuotaDO) {
// 测试查询成功
getQuotaFromZk2SuccessTest(topicQuotaDO);
// 测试查询失败
getQuotaFromZk2FailureTest();
}
private void getQuotaFromZk2SuccessTest(TopicQuota topicQuotaDO) {
TopicQuota result = quotaService.getQuotaFromZk(1L, "moduleTest", "testAppId");
Assert.assertNotNull(result);
Assert.assertEquals(result.toString(), topicQuotaDO.toString());
}
private void getQuotaFromZk2FailureTest() {
TopicQuota result = quotaService.getQuotaFromZk(10L, "moduleTest", "testAppId");
Assert.assertNull(result);
}
@Test
public void modifyProduceQuotaTest() {
// 测试修改成功
modifyProduceQuota2SuccessTest();
// 测试修改失败
modifyProduceQuota2FailureTest();
}
private void modifyProduceQuota2SuccessTest() {
Boolean result = quotaService.modifyProduceQuota(1L, "moduleTest", "testAppId", 100L);
Assert.assertTrue(result);
}
private void modifyProduceQuota2FailureTest() {
Boolean result1 = quotaService.modifyProduceQuota(10L, "moduleTest", "testAppId", 100L);
Assert.assertFalse(result1);
}
@Test(dataProvider = "provideTopicQuota")
public void addTopicQuotaByAuthorityTest(TopicQuota topicQuotaDO) {
// 测试新增时,无相应集群异常
addTopicQuotaByAuthority2ClusterNotExistTest(topicQuotaDO);
// 测试新增时,无权限异常
addTopicQuotaByAuthority2UserWithoutAuthority1Test(topicQuotaDO);
// 测试新增时,无权限异常,修改数据库access为0测试
addTopicQuotaByAuthority2UserWithoutAuthority2Test(topicQuotaDO);
// 测试新增成功包含三个流程access为123时通过数据库修改
addTopicQuotaByAuthority2SuccessTest(topicQuotaDO);
// 测试新增时无法写入zk异常(关闭zk)包含三个流程access为123时通过数据库修改
addTopicQuotaByAuthority2ZookeeperWriteFailedTest(topicQuotaDO);
}
private void addTopicQuotaByAuthority2SuccessTest(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(7L);
ResultStatus resultStatus = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void addTopicQuotaByAuthority2ClusterNotExistTest(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(10L);
ResultStatus resultStatus = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.CLUSTER_NOT_EXIST.getCode());
}
private void addTopicQuotaByAuthority2UserWithoutAuthority1Test(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(7L);
topicQuotaDO.setTopicName("xxx");
ResultStatus resultStatus1 = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus1.getCode(), ResultStatus.USER_WITHOUT_AUTHORITY.getCode());
}
private void addTopicQuotaByAuthority2UserWithoutAuthority2Test(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(7L);
ResultStatus resultStatus = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.SUCCESS.getCode());
}
private void addTopicQuotaByAuthority2ZookeeperWriteFailedTest(TopicQuota topicQuotaDO) {
topicQuotaDO.setClusterId(7L);
ResultStatus resultStatus = quotaService.addTopicQuotaByAuthority(topicQuotaDO);
Assert.assertEquals(resultStatus.getCode(), ResultStatus.ZOOKEEPER_WRITE_FAILED.getCode());
}
}