Skip to content

Commit 13f4297

Browse files
stevenschewzhouxinyu
authored andcommitted
[ROCKETMQ-57] Add unit test for all commands
1 parent 712dec5 commit 13f4297

28 files changed

+2077
-44
lines changed

tools/src/test/java/org/apache/rocketmq/tools/admin/DefaultMQAdminExtTest.java

+45-41
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
import org.apache.rocketmq.remoting.exception.RemotingSendRequestException;
6969
import org.apache.rocketmq.remoting.exception.RemotingTimeoutException;
7070
import org.apache.rocketmq.tools.admin.api.MessageTrack;
71-
import org.junit.After;
72-
import org.junit.Before;
71+
import org.junit.AfterClass;
72+
import org.junit.BeforeClass;
7373
import org.junit.Test;
7474
import org.junit.runner.RunWith;
7575
import org.mockito.junit.MockitoJUnitRunner;
@@ -83,19 +83,20 @@
8383

8484
@RunWith(MockitoJUnitRunner.class)
8585
public class DefaultMQAdminExtTest {
86-
private DefaultMQAdminExtImpl defaultMQAdminExtImpl;
87-
private MQClientInstance mqClientInstance = MQClientManager.getInstance().getAndCreateMQClientInstance(new ClientConfig());
88-
private MQClientAPIImpl mQClientAPIImpl;
89-
private Properties properties = new Properties();
90-
private TopicList topicList = new TopicList();
91-
private TopicRouteData topicRouteData = new TopicRouteData();
92-
private KVTable kvTable = new KVTable();
93-
private ClusterInfo clusterInfo = new ClusterInfo();
94-
95-
@Before
96-
public void init() throws Exception {
86+
private static DefaultMQAdminExt defaultMQAdminExt;
87+
private static DefaultMQAdminExtImpl defaultMQAdminExtImpl;
88+
private static MQClientInstance mqClientInstance = MQClientManager.getInstance().getAndCreateMQClientInstance(new ClientConfig());
89+
private static MQClientAPIImpl mQClientAPIImpl;
90+
private static Properties properties = new Properties();
91+
private static TopicList topicList = new TopicList();
92+
private static TopicRouteData topicRouteData = new TopicRouteData();
93+
private static KVTable kvTable = new KVTable();
94+
private static ClusterInfo clusterInfo = new ClusterInfo();
95+
96+
@BeforeClass
97+
public static void init() throws Exception {
9798
mQClientAPIImpl = mock(MQClientAPIImpl.class);
98-
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt();
99+
defaultMQAdminExt = new DefaultMQAdminExt();
99100
defaultMQAdminExtImpl = new DefaultMQAdminExtImpl(defaultMQAdminExt, 1000);
100101

101102
Field field = DefaultMQAdminExtImpl.class.getDeclaredField("mqClientInstance");
@@ -104,6 +105,9 @@ public void init() throws Exception {
104105
field = MQClientInstance.class.getDeclaredField("mQClientAPIImpl");
105106
field.setAccessible(true);
106107
field.set(mqClientInstance, mQClientAPIImpl);
108+
field = DefaultMQAdminExt.class.getDeclaredField("defaultMQAdminExtImpl");
109+
field.setAccessible(true);
110+
field.set(defaultMQAdminExt, defaultMQAdminExtImpl);
107111

108112
properties.setProperty("maxMessageSize", "5000000");
109113
properties.setProperty("flushDelayOffsetInterval", "15000");
@@ -223,37 +227,37 @@ public void init() throws Exception {
223227
when(mQClientAPIImpl.getConsumerRunningInfo(anyString(), anyString(), anyString(), anyBoolean(), anyLong())).thenReturn(consumerRunningInfo);
224228
}
225229

226-
@After
227-
public void terminate() throws Exception {
230+
@AfterClass
231+
public static void terminate() throws Exception {
228232
if (defaultMQAdminExtImpl != null)
229-
defaultMQAdminExtImpl.shutdown();
233+
defaultMQAdminExt.shutdown();
230234
}
231235

232236
@Test
233237
public void testUpdateBrokerConfig() throws InterruptedException, RemotingConnectException, UnsupportedEncodingException, RemotingTimeoutException, MQBrokerException, RemotingSendRequestException {
234-
Properties result = defaultMQAdminExtImpl.getBrokerConfig("127.0.0.1:10911");
238+
Properties result = defaultMQAdminExt.getBrokerConfig("127.0.0.1:10911");
235239
assertThat(result.getProperty("maxMessageSize")).isEqualTo("5000000");
236240
assertThat(result.getProperty("flushDelayOffsetInterval")).isEqualTo("15000");
237241
assertThat(result.getProperty("serverSocketRcvBufSize")).isEqualTo("655350");
238242
}
239243

240244
@Test
241245
public void testFetchAllTopicList() throws RemotingException, MQClientException, InterruptedException {
242-
TopicList topicList = defaultMQAdminExtImpl.fetchAllTopicList();
246+
TopicList topicList = defaultMQAdminExt.fetchAllTopicList();
243247
assertThat(topicList.getTopicList().size()).isEqualTo(2);
244248
assertThat(topicList.getTopicList()).contains("topic_one");
245249
}
246250

247251
@Test
248252
public void testFetchBrokerRuntimeStats() throws InterruptedException, MQBrokerException, RemotingTimeoutException, RemotingSendRequestException, RemotingConnectException {
249-
KVTable brokerStats = defaultMQAdminExtImpl.fetchBrokerRuntimeStats("127.0.0.1:10911");
253+
KVTable brokerStats = defaultMQAdminExt.fetchBrokerRuntimeStats("127.0.0.1:10911");
250254
assertThat(brokerStats.getTable().get("id")).isEqualTo("1234");
251255
assertThat(brokerStats.getTable().get("brokerName")).isEqualTo("default-broker");
252256
}
253257

254258
@Test
255259
public void testExamineBrokerClusterInfo() throws InterruptedException, MQBrokerException, RemotingTimeoutException, RemotingSendRequestException, RemotingConnectException {
256-
ClusterInfo clusterInfo = defaultMQAdminExtImpl.examineBrokerClusterInfo();
260+
ClusterInfo clusterInfo = defaultMQAdminExt.examineBrokerClusterInfo();
257261
HashMap<String, BrokerData> brokerList = clusterInfo.getBrokerAddrTable();
258262
assertThat(brokerList.get("default-broker").getBrokerName()).isEqualTo("default-broker");
259263
assertThat(brokerList.containsKey("broker-test")).isTrue();
@@ -272,32 +276,32 @@ public void testExamineBrokerClusterInfo() throws InterruptedException, MQBroker
272276

273277
@Test
274278
public void testExamineConsumeStats() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
275-
ConsumeStats consumeStats = defaultMQAdminExtImpl.examineConsumeStats("default-consumer-group", "unit-test");
279+
ConsumeStats consumeStats = defaultMQAdminExt.examineConsumeStats("default-consumer-group", "unit-test");
276280
assertThat(consumeStats.getConsumeTps()).isEqualTo(1234);
277281
}
278282

279283
@Test
280284
public void testExamineConsumerConnectionInfo() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
281-
ConsumerConnection consumerConnection = defaultMQAdminExtImpl.examineConsumerConnectionInfo("default-consumer-group");
285+
ConsumerConnection consumerConnection = defaultMQAdminExt.examineConsumerConnectionInfo("default-consumer-group");
282286
assertThat(consumerConnection.getConsumeType()).isEqualTo(ConsumeType.CONSUME_PASSIVELY);
283287
assertThat(consumerConnection.getMessageModel()).isEqualTo(MessageModel.CLUSTERING);
284288
}
285289

286290
@Test
287291
public void testExamineProducerConnectionInfo() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
288-
ProducerConnection producerConnection = defaultMQAdminExtImpl.examineProducerConnectionInfo("default-producer-group", "unit-test");
292+
ProducerConnection producerConnection = defaultMQAdminExt.examineProducerConnectionInfo("default-producer-group", "unit-test");
289293
assertThat(producerConnection.getConnectionSet().size()).isEqualTo(1);
290294
}
291295

292296
@Test
293297
public void testWipeWritePermOfBroker() throws InterruptedException, RemotingCommandException, RemotingSendRequestException, RemotingTimeoutException, MQClientException, RemotingConnectException {
294-
int result = defaultMQAdminExtImpl.wipeWritePermOfBroker("127.0.0.1:9876", "default-broker");
298+
int result = defaultMQAdminExt.wipeWritePermOfBroker("127.0.0.1:9876", "default-broker");
295299
assertThat(result).isEqualTo(6);
296300
}
297301

298302
@Test
299303
public void testExamineTopicRouteInfo() throws RemotingException, MQClientException, InterruptedException {
300-
TopicRouteData topicRouteData = defaultMQAdminExtImpl.examineTopicRouteInfo("UnitTest");
304+
TopicRouteData topicRouteData = defaultMQAdminExt.examineTopicRouteInfo("UnitTest");
301305
assertThat(topicRouteData.getBrokerDatas().get(0).getBrokerName()).isEqualTo("default-broker");
302306
assertThat(topicRouteData.getBrokerDatas().get(0).getCluster()).isEqualTo("default-cluster");
303307
}
@@ -308,53 +312,53 @@ public void testGetNameServerAddressList() {
308312
result.add("default-name-one");
309313
result.add("default-name-two");
310314
when(mqClientInstance.getMQClientAPIImpl().getNameServerAddressList()).thenReturn(result);
311-
List<String> nameList = defaultMQAdminExtImpl.getNameServerAddressList();
315+
List<String> nameList = defaultMQAdminExt.getNameServerAddressList();
312316
assertThat(nameList.get(0)).isEqualTo("default-name-one");
313317
assertThat(nameList.get(1)).isEqualTo("default-name-two");
314318
}
315319

316320
@Test
317321
public void testPutKVConfig() throws RemotingException, MQClientException, InterruptedException {
318-
String topicConfig = defaultMQAdminExtImpl.getKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, "UnitTest");
322+
String topicConfig = defaultMQAdminExt.getKVConfig(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG, "UnitTest");
319323
assertThat(topicConfig).isEqualTo("topicListConfig");
320-
KVTable kvs = defaultMQAdminExtImpl.getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
324+
KVTable kvs = defaultMQAdminExt.getKVListByNamespace(NamesrvUtil.NAMESPACE_ORDER_TOPIC_CONFIG);
321325
assertThat(kvs.getTable().get("broker-name")).isEqualTo("broker-one");
322326
assertThat(kvs.getTable().get("cluster-name")).isEqualTo("default-cluster");
323327
}
324328

325329
@Test
326330
public void testQueryTopicConsumeByWho() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
327-
GroupList groupList = defaultMQAdminExtImpl.queryTopicConsumeByWho("UnitTest");
331+
GroupList groupList = defaultMQAdminExt.queryTopicConsumeByWho("UnitTest");
328332
assertThat(groupList.getGroupList().contains("consumer-group-two")).isTrue();
329333
}
330334

331335
@Test
332336
public void testQueryConsumeTimeSpan() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
333-
List<QueueTimeSpan> result = defaultMQAdminExtImpl.queryConsumeTimeSpan("unit-test", "default-broker-group");
337+
List<QueueTimeSpan> result = defaultMQAdminExt.queryConsumeTimeSpan("unit-test", "default-broker-group");
334338
assertThat(result.size()).isEqualTo(0);
335339
}
336340

337341
@Test
338342
public void testCleanExpiredConsumerQueue() throws InterruptedException, RemotingTimeoutException, MQClientException, RemotingSendRequestException, RemotingConnectException {
339-
boolean result = defaultMQAdminExtImpl.cleanExpiredConsumerQueue("default-cluster");
343+
boolean result = defaultMQAdminExt.cleanExpiredConsumerQueue("default-cluster");
340344
assertThat(result).isFalse();
341345
}
342346

343347
@Test
344348
public void testCleanExpiredConsumerQueueByAddr() throws InterruptedException, RemotingTimeoutException, MQClientException, RemotingSendRequestException, RemotingConnectException {
345-
boolean clean = defaultMQAdminExtImpl.cleanExpiredConsumerQueueByAddr("127.0.0.1:10911");
349+
boolean clean = defaultMQAdminExt.cleanExpiredConsumerQueueByAddr("127.0.0.1:10911");
346350
assertThat(clean).isTrue();
347351
}
348352

349353
@Test
350354
public void testCleanUnusedTopic() throws InterruptedException, RemotingTimeoutException, MQClientException, RemotingSendRequestException, RemotingConnectException {
351-
boolean result = defaultMQAdminExtImpl.cleanUnusedTopic("default-cluster");
355+
boolean result = defaultMQAdminExt.cleanUnusedTopic("default-cluster");
352356
assertThat(result).isFalse();
353357
}
354358

355359
@Test
356360
public void testGetConsumerRunningInfo() throws RemotingException, MQClientException, InterruptedException {
357-
ConsumerRunningInfo consumerRunningInfo = defaultMQAdminExtImpl.getConsumerRunningInfo("consumer-group", "cid_123", false);
361+
ConsumerRunningInfo consumerRunningInfo = defaultMQAdminExt.getConsumerRunningInfo("consumer-group", "cid_123", false);
358362
assertThat(consumerRunningInfo.getJstack()).isEqualTo("test");
359363
}
360364

@@ -363,25 +367,25 @@ public void testMessageTrackDetail() throws InterruptedException, RemotingExcept
363367
MessageExt messageExt = new MessageExt();
364368
messageExt.setMsgId("msgId");
365369
messageExt.setTopic("unit-test");
366-
List<MessageTrack> messageTrackList = defaultMQAdminExtImpl.messageTrackDetail(messageExt);
370+
List<MessageTrack> messageTrackList = defaultMQAdminExt.messageTrackDetail(messageExt);
367371
assertThat(messageTrackList.size()).isEqualTo(2);
368372
}
369373

370374
@Test
371375
public void testGetConsumeStatus() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
372-
Map<String, Map<MessageQueue, Long>> result = defaultMQAdminExtImpl.getConsumeStatus("unit-test", "default-broker-group", "127.0.0.1:10911");
376+
Map<String, Map<MessageQueue, Long>> result = defaultMQAdminExt.getConsumeStatus("unit-test", "default-broker-group", "127.0.0.1:10911");
373377
assertThat(result.size()).isEqualTo(0);
374378
}
375379

376380
@Test
377381
public void testGetTopicClusterList() throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
378-
Set<String> result = defaultMQAdminExtImpl.getTopicClusterList("unit-test");
382+
Set<String> result = defaultMQAdminExt.getTopicClusterList("unit-test");
379383
assertThat(result.size()).isEqualTo(0);
380384
}
381385

382386
@Test
383387
public void testGetClusterList() throws InterruptedException, RemotingTimeoutException, MQClientException, RemotingSendRequestException, RemotingConnectException {
384-
Set<String> clusterlist = defaultMQAdminExtImpl.getClusterList("UnitTest");
388+
Set<String> clusterlist = defaultMQAdminExt.getClusterList("UnitTest");
385389
assertThat(clusterlist.contains("default-cluster-one")).isTrue();
386390
assertThat(clusterlist.contains("default-cluster-two")).isTrue();
387391
}
@@ -391,13 +395,13 @@ public void testFetchConsumeStatsInBroker() throws InterruptedException, Remotin
391395
ConsumeStatsList result = new ConsumeStatsList();
392396
result.setBrokerAddr("127.0.0.1:10911");
393397
when(mqClientInstance.getMQClientAPIImpl().fetchConsumeStatsInBroker("127.0.0.1:10911", false, 10000)).thenReturn(result);
394-
ConsumeStatsList consumeStatsList = defaultMQAdminExtImpl.fetchConsumeStatsInBroker("127.0.0.1:10911", false, 10000);
398+
ConsumeStatsList consumeStatsList = defaultMQAdminExt.fetchConsumeStatsInBroker("127.0.0.1:10911", false, 10000);
395399
assertThat(consumeStatsList.getBrokerAddr()).isEqualTo("127.0.0.1:10911");
396400
}
397401

398402
@Test
399403
public void testGetAllSubscriptionGroup() throws InterruptedException, MQBrokerException, RemotingTimeoutException, RemotingSendRequestException, RemotingConnectException {
400-
SubscriptionGroupWrapper subscriptionGroupWrapper = defaultMQAdminExtImpl.getAllSubscriptionGroup("127.0.0.1:10911", 10000);
404+
SubscriptionGroupWrapper subscriptionGroupWrapper = defaultMQAdminExt.getAllSubscriptionGroup("127.0.0.1:10911", 10000);
401405
assertThat(subscriptionGroupWrapper.getSubscriptionGroupTable().get("Consumer-group-one").getBrokerId()).isEqualTo(1234);
402406
assertThat(subscriptionGroupWrapper.getSubscriptionGroupTable().get("Consumer-group-one").getGroupName()).isEqualTo("Consumer-group-one");
403407
assertThat(subscriptionGroupWrapper.getSubscriptionGroupTable().get("Consumer-group-one").isConsumeBroadcastEnable()).isTrue();

tools/src/test/java/org/apache/rocketmq/tools/command/CommandUtilTest.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
*/
1717
package org.apache.rocketmq.tools.command;
1818

19+
import java.lang.reflect.Field;
20+
import java.util.HashMap;
21+
import java.util.HashSet;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Set;
1925
import org.apache.rocketmq.client.ClientConfig;
2026
import org.apache.rocketmq.client.exception.MQBrokerException;
2127
import org.apache.rocketmq.client.exception.MQClientException;
@@ -33,9 +39,6 @@
3339
import org.junit.Before;
3440
import org.junit.Test;
3541

36-
import java.lang.reflect.Field;
37-
import java.util.*;
38-
3942
import static org.assertj.core.api.Assertions.assertThat;
4043
import static org.mockito.ArgumentMatchers.anyLong;
4144
import static org.mockito.ArgumentMatchers.anyString;

0 commit comments

Comments
 (0)