1818import org .mockito .Mockito ;
1919import org .mockito .MockitoAnnotations ;
2020import org .springframework .beans .factory .annotation .Autowired ;
21+ import org .springframework .beans .factory .annotation .Value ;
2122import org .springframework .dao .DuplicateKeyException ;
2223import org .testng .Assert ;
2324import org .testng .annotations .BeforeMethod ;
3435 * @Date 2021/12/8
3536 */
3637public class ClusterServiceTest extends BaseTest {
38+ @ Value ("${test.phyCluster.id}" )
39+ private Long REAL_CLUSTER_ID_IN_MYSQL ;
3740
38- private final static Long REAL_CLUSTER_ID_IN_MYSQL = 1L ;
41+ @ Value ("${test.broker.id1}" )
42+ private Integer REAL_BROKER_ID_IN_ZK ;
3943
40- private final static Integer REAL_BROKER_ID_IN_ZK = 1 ;
44+ @ Value ("${test.phyCluster.name}" )
45+ private String REAL_PHYSICAL_CLUSTER_NAME ;
4146
42- private final static String REAL_PHYSICAL_CLUSTER_NAME = "LogiKM_moduleTest" ;
47+ @ Value ("${test.ZK.address}" )
48+ private String ZOOKEEPER_ADDRESS ;
4349
44- // private final static String ZOOKEEPER_ADDRESS = "10.190.46.198:2181,10.190.14.237:2181,10.190.50.65:2181/xg";
45- private final static String ZOOKEEPER_ADDRESS = "10.190.12.242:2181,10.190.25.160:2181,10.190.25.41:2181/wyc" ;
46-
47- // private final static String BOOTSTRAP_SERVERS = "10.190.46.198:9093,10.190.14.237:9093,10.190.50.65:9093";
48- private final static String BOOTSTRAP_SERVERS = "10.190.12.242:9093,10.190.25.160:9093,10.190.25.41:9093" ;
50+ @ Value ("${test.ZK.bootstrap-servers}" )
51+ private String BOOTSTRAP_SERVERS ;
4952
5053 private final static String SECURITY_PROTOCOL = "{ \t \" security.protocol\" : \" SASL_PLAINTEXT\" , \t \" sasl.mechanism\" : \" PLAIN\" , \t \" sasl.jaas.config\" : \" org.apache.kafka.common.security.plain.PlainLoginModule required username=\\ \" dkm_admin\\ \" password=\\ \" km_kMl4N8as1Kp0CCY\\ \" ;\" }" ;
5154
@@ -86,8 +89,7 @@ public void setup() {
8689 MockitoAnnotations .initMocks (this );
8790 }
8891
89- @ DataProvider (name = "provideClusterDO" )
90- public static Object [][] provideClusterDO () {
92+ private ClusterDO getClusterDO1 () {
9193 ClusterDO clusterDO = new ClusterDO ();
9294 clusterDO .setId (3L );
9395 clusterDO .setClusterName (REAL_PHYSICAL_CLUSTER_NAME );
@@ -97,28 +99,26 @@ public static Object[][] provideClusterDO() {
9799 clusterDO .setStatus (1 );
98100 clusterDO .setGmtCreate (new Date ());
99101 clusterDO .setGmtModify (new Date ());
100- return new Object [][] {{ clusterDO }} ;
102+ return clusterDO ;
101103 }
102104
103- @ DataProvider (name = "provideClusterMetricsDO" )
104- public static Object [][] provideClusterMetricsDO () {
105+ private ClusterMetricsDO getClusterMetricsDO () {
105106 ClusterMetricsDO clusterMetricsDO = new ClusterMetricsDO ();
106107 clusterMetricsDO .setId (10L );
107108 clusterMetricsDO .setClusterId (REAL_CLUSTER_ID_IN_MYSQL );
108109 clusterMetricsDO .setMetrics ("{\" PartitionNum\" :52,\" BrokerNum\" :0,\" CreateTime\" :1638235221102,\" TopicNum\" :2}" );
109110 clusterMetricsDO .setGmtCreate (new Date ());
110- return new Object [][] {{ clusterMetricsDO }} ;
111+ return clusterMetricsDO ;
111112 }
112113
113- @ DataProvider (name = "provideControllerDO" )
114- public static Object [][] provideControllerDO () {
114+ private ControllerDO getControllerDO () {
115115 ControllerDO controllerDO = new ControllerDO ();
116116 controllerDO .setClusterId (REAL_CLUSTER_ID_IN_MYSQL );
117117 controllerDO .setBrokerId (REAL_BROKER_ID_IN_ZK );
118118 controllerDO .setHost ("127.0.0.1" );
119119 controllerDO .setTimestamp (0L );
120120 controllerDO .setVersion (1 );
121- return new Object [][] {{ controllerDO }} ;
121+ return controllerDO ;
122122 }
123123
124124 private Map <Long , Integer > getRegionNum () {
@@ -146,8 +146,9 @@ private ClusterDO getClusterDO() {
146146 return clusterDO ;
147147 }
148148
149- @ Test (dataProvider = "provideClusterDO" , description = "测试新增物理集群" )
150- public void addNewTest (ClusterDO clusterDO ) {
149+ @ Test (description = "测试新增物理集群" )
150+ public void addNewTest () {
151+ ClusterDO clusterDO = getClusterDO1 ();
151152 // 测试新增物理集群成功
152153 addNew2SuccessTest (clusterDO );
153154 // 测试新增物理集群时键重复
@@ -185,25 +186,28 @@ public void addNew2DuplicateKeyTest(ClusterDO clusterDO) {
185186 Assert .assertEquals (result .getCode (), ResultStatus .RESOURCE_ALREADY_EXISTED .getCode ());
186187 }
187188
188- @ Test (dataProvider = "provideClusterDO" , description = "测试修改物理集群" )
189- public void updateById (ClusterDO clusterDO ) {
189+ @ Test (description = "测试修改物理集群" )
190+ public void updateById () {
191+ ClusterDO clusterDO = getClusterDO1 ();
190192 // 测试修改物理集群时参数有误
191193 updateById2ParamIllegalTest (clusterDO );
192194 // 测试修改物理集群时,集群不存在
193195 updateById2ClusterNotExistTest (clusterDO );
194196 }
195197
196- @ Test (dataProvider = "provideClusterDO" , description = "测试修改物理集群时,mysqlError" )
197- public void updateById2mysqlErrorTest (ClusterDO clusterDO ) {
198+ @ Test (description = "测试修改物理集群时,mysqlError" )
199+ public void updateById2mysqlErrorTest () {
200+ ClusterDO clusterDO = getClusterDO1 ();
198201 Mockito .when (clusterDao .getById (Mockito .any ())).thenReturn (clusterDO );
199202 Mockito .when (operateRecordService .insert (Mockito .any (), Mockito .any (), Mockito .any (), Mockito .any (), Mockito .any ())).thenReturn (1 );
200203 Mockito .when (clusterDao .updateById (Mockito .any ())).thenReturn (0 );
201204 ResultStatus result1 = clusterService .updateById (clusterDO , "admin" );
202205 Assert .assertEquals (result1 .getCode (), ResultStatus .MYSQL_ERROR .getCode ());
203206 }
204207
205- @ Test (dataProvider = "provideClusterDO" , description = "测试修改物理集群成功" )
206- public void updateById2SuccessTest (ClusterDO clusterDO ) {
208+ @ Test (description = "测试修改物理集群成功" )
209+ public void updateById2SuccessTest () {
210+ ClusterDO clusterDO = getClusterDO1 ();
207211 Mockito .when (clusterDao .getById (Mockito .any ())).thenReturn (clusterDO );
208212 Mockito .when (clusterDao .updateById (Mockito .any ())).thenReturn (1 );
209213 clusterDO .setJmxProperties ("jmx" );
@@ -225,16 +229,18 @@ private void updateById2ClusterNotExistTest(ClusterDO clusterDO) {
225229 Assert .assertEquals (result1 .getCode (), ResultStatus .CLUSTER_NOT_EXIST .getCode ());
226230 }
227231
228- @ Test (dataProvider = "provideClusterDO" )
229- public void updateById2ChangeZookeeperForbiddenTest (ClusterDO clusterDO ) {
232+ @ Test ()
233+ public void updateById2ChangeZookeeperForbiddenTest () {
234+ ClusterDO clusterDO = getClusterDO1 ();
230235 ClusterDO clusterDO1 = getClusterDO ();
231236 Mockito .when (clusterDao .getById (Mockito .any ())).thenReturn (clusterDO );
232237 ResultStatus result1 = clusterService .updateById (clusterDO1 , "admin" );
233238 Assert .assertEquals (result1 .getCode (), ResultStatus .CHANGE_ZOOKEEPER_FORBIDDEN .getCode ());
234239 }
235240
236- @ Test (dataProvider = "provideClusterDO" , description = "测试修改物理集群状态" )
237- public void modifyStatusTest (ClusterDO clusterDO ) {
241+ @ Test ( description = "测试修改物理集群状态" )
242+ public void modifyStatusTest () {
243+ ClusterDO clusterDO = getClusterDO1 ();
238244 // 测试修改物理集群状态时参数有误
239245 modifyStatus2ParamIllegalTest ();
240246 // 测试修改物理集群状态时,集群不存在
@@ -264,8 +270,9 @@ public void modifyStatus2SuccessTest(ClusterDO clusterDO) {
264270 Assert .assertEquals (result1 .getCode (), ResultStatus .SUCCESS .getCode ());
265271 }
266272
267- @ Test (dataProvider = "provideClusterDO" , description = "参数needDetail为false" )
268- public void getClusterDetailDTOListWithFalseNeedDetailTest (ClusterDO clusterDO ) {
273+ @ Test (description = "参数needDetail为false" )
274+ public void getClusterDetailDTOListWithFalseNeedDetailTest () {
275+ ClusterDO clusterDO = getClusterDO1 ();
269276 Mockito .when (clusterDao .listAll ()).thenReturn (Arrays .asList (clusterDO ));
270277 String kafkaVersion = "2.7" ;
271278 when (physicalClusterMetadataManager .getKafkaVersionFromCache (Mockito .anyLong ())).thenReturn (kafkaVersion );
@@ -278,8 +285,9 @@ public void getClusterDetailDTOListWithFalseNeedDetailTest(ClusterDO clusterDO)
278285 clusterDetailDTO .getKafkaVersion ().equals (kafkaVersion )));
279286 }
280287
281- @ Test (dataProvider = "provideClusterDO" , description = "参数needDetail为true" )
282- public void getClusterDetailDTOListWithTrueNeedDetailTest (ClusterDO clusterDO ) {
288+ @ Test (description = "参数needDetail为true" )
289+ public void getClusterDetailDTOListWithTrueNeedDetailTest () {
290+ ClusterDO clusterDO = getClusterDO1 ();
283291 Mockito .when (clusterDao .listAll ()).thenReturn (Arrays .asList (clusterDO ));
284292 Mockito .when (regionService .getRegionNum ()).thenReturn (getRegionNum ());
285293 Mockito .when (consumerService .getConsumerGroupNumMap (Mockito .any ())).thenReturn (getConsumerGroupNumMap ());
@@ -298,8 +306,9 @@ public void getClusterName2EmptyTest() {
298306 Assert .assertEquals (clusterName .toString (), new ClusterNameDTO ().toString ());
299307 }
300308
301- @ Test (dataProvider = "provideClusterDO" , description = "测试获取ClusterNameDTO成功" )
302- public void getClusterName2SuccessTest (ClusterDO clusterDO ) {
309+ @ Test (description = "测试获取ClusterNameDTO成功" )
310+ public void getClusterName2SuccessTest () {
311+ ClusterDO clusterDO = getClusterDO1 ();
303312 clusterService .addNew (clusterDO , "admin" );
304313
305314 LogicalClusterDO logicalClusterDO = new LogicalClusterDO ();
@@ -322,8 +331,9 @@ public void deleteById2OperationForbiddenTest() {
322331 Assert .assertEquals (resultStatus .getCode (), ResultStatus .OPERATION_FORBIDDEN .getCode ());
323332 }
324333
325- @ Test (dataProvider = "provideClusterDO" , description = "测试删除集群成功" )
326- public void deleteById2SuccessTest (ClusterDO clusterDO ) {
334+ @ Test (description = "测试删除集群成功" )
335+ public void deleteById2SuccessTest () {
336+ ClusterDO clusterDO = getClusterDO1 ();
327337 when (regionService .getByClusterId (Mockito .anyLong ())).thenReturn (Collections .emptyList ());
328338 Mockito .when (operateRecordService .insert (Mockito .any (), Mockito .any (), Mockito .any (), Mockito .any (), Mockito .any ())).thenReturn (1 );
329339 Mockito .when (clusterDao .deleteById (Mockito .any ())).thenReturn (1 );
0 commit comments