mirror of
https://github.com/apache/rocketmq-dashboard.git
synced 2025-09-10 11:40:01 +08:00
@@ -17,12 +17,20 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.route.BrokerData;
|
||||
import org.mockito.internal.util.MockUtil;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class BaseTest {
|
||||
/**
|
||||
* Inject the corresponding mock class automatically
|
||||
@@ -69,4 +77,20 @@ public class BaseTest {
|
||||
ReflectionUtils.doWithFields(leafClass, fc);
|
||||
return fields;
|
||||
}
|
||||
|
||||
protected ClusterInfo getClusterInfo() {
|
||||
ClusterInfo clusterInfo = new ClusterInfo();
|
||||
Map<String, Set<String>> clusterAddrTable = new HashMap<>();
|
||||
clusterAddrTable.put("DefaultCluster", new HashSet<>(Arrays.asList("broker-a")));
|
||||
Map<String, BrokerData> brokerAddrTable = new HashMap<>();
|
||||
BrokerData brokerData = new BrokerData();
|
||||
brokerData.setBrokerName("broker-a");
|
||||
HashMap<Long, String> brokerNameTable = new HashMap<>();
|
||||
brokerNameTable.put(0L, "localhost:10911");
|
||||
brokerData.setBrokerAddrs(brokerNameTable);
|
||||
brokerAddrTable.put("broker-a", brokerData);
|
||||
clusterInfo.setBrokerAddrTable(brokerAddrTable);
|
||||
clusterInfo.setClusterAddrTable(clusterAddrTable);
|
||||
return clusterInfo;
|
||||
}
|
||||
}
|
||||
|
@@ -17,26 +17,45 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard.admin;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.apache.rocketmq.dashboard.aspect.admin.MQAdminAspect;
|
||||
import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
|
||||
import org.apache.rocketmq.tools.admin.MQAdminExt;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class MQAdminAspectTest {
|
||||
|
||||
@Mock
|
||||
private RMQConfigure rmqConfigure;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(rmqConfigure.isLoginRequired()).thenReturn(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAroundMQAdminMethod() throws Throwable {
|
||||
MQAdminAspect mqAdminAspect = new MQAdminAspect();
|
||||
Field field = mqAdminAspect.getClass().getDeclaredField("rmqConfigure");
|
||||
field.setAccessible(true);
|
||||
field.set(mqAdminAspect, rmqConfigure);
|
||||
ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
|
||||
MethodSignature signature = mock(MethodSignature.class);
|
||||
Method method = mock(Method.class);
|
||||
@@ -44,16 +63,39 @@ public class MQAdminAspectTest {
|
||||
when(joinPoint.getSignature()).thenReturn(signature);
|
||||
|
||||
GenericObjectPool<MQAdminExt> mqAdminExtPool = mock(GenericObjectPool.class);
|
||||
// 1. Mock borrowObject() 行为:第一次抛异常,第二次返回 DefaultMQAdminExt
|
||||
when(mqAdminExtPool.borrowObject())
|
||||
.thenThrow(new RuntimeException("borrowObject exception"))
|
||||
.thenReturn(new DefaultMQAdminExt());
|
||||
doNothing().doThrow(new RuntimeException("returnObject exception"))
|
||||
|
||||
// 2. Mock returnObject() 行为:第一次什么都不做,第二次抛异常
|
||||
doNothing().when(mqAdminExtPool).returnObject(any());
|
||||
doThrow(new RuntimeException("returnObject exception"))
|
||||
.when(mqAdminExtPool).returnObject(any());
|
||||
Field field = mqAdminAspect.getClass().getDeclaredField("mqAdminExtPool");
|
||||
|
||||
// 3. 通过反射注入 Mock 对象
|
||||
field = mqAdminAspect.getClass().getDeclaredField("mqAdminExtPool");
|
||||
field.setAccessible(true);
|
||||
field.set(mqAdminAspect, mqAdminExtPool);
|
||||
// exception
|
||||
|
||||
// 4. 第一次调用 aroundMQAdminMethod,预期 borrowObject() 抛异常
|
||||
try {
|
||||
mqAdminAspect.aroundMQAdminMethod(joinPoint);
|
||||
fail("Expected RuntimeException but no exception was thrown");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("borrowObject exception", e.getMessage());
|
||||
}
|
||||
|
||||
// 5. 第二次调用 aroundMQAdminMethod,预期 borrowObject() 成功,但 returnObject() 抛异常
|
||||
try {
|
||||
mqAdminAspect.aroundMQAdminMethod(joinPoint);
|
||||
fail("Expected RuntimeException but no exception was thrown");
|
||||
} catch (RuntimeException e) {
|
||||
assertEquals("returnObject exception", e.getMessage());
|
||||
}
|
||||
|
||||
// 6. 验证 borrowObject() 和 returnObject() 各调用了两次
|
||||
verify(mqAdminExtPool, times(2)).borrowObject();
|
||||
verify(mqAdminExtPool, times(1)).returnObject(any());
|
||||
}
|
||||
}
|
||||
|
@@ -19,27 +19,25 @@ package org.apache.rocketmq.dashboard.admin;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import org.apache.rocketmq.client.QueryResult;
|
||||
import org.apache.rocketmq.client.exception.MQBrokerException;
|
||||
import org.apache.rocketmq.client.impl.MQAdminImpl;
|
||||
import org.apache.rocketmq.client.impl.MQClientAPIImpl;
|
||||
import org.apache.rocketmq.client.impl.factory.MQClientInstance;
|
||||
import org.apache.rocketmq.common.PlainAccessConfig;
|
||||
import org.apache.rocketmq.common.TopicConfig;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.dashboard.service.client.MQAdminExtImpl;
|
||||
import org.apache.rocketmq.dashboard.service.client.MQAdminInstance;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.RemotingClient;
|
||||
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
|
||||
import org.apache.rocketmq.remoting.protocol.RemotingSerializable;
|
||||
import org.apache.rocketmq.remoting.protocol.ResponseCode;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.ConsumeStats;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.RollbackStats;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.TopicStatsTable;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.remoting.protocol.ResponseCode;
|
||||
import org.apache.rocketmq.remoting.protocol.body.BrokerStatsData;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumeMessageDirectlyResult;
|
||||
@@ -55,12 +53,6 @@ import org.apache.rocketmq.remoting.protocol.body.TopicConfigSerializeWrapper;
|
||||
import org.apache.rocketmq.remoting.protocol.body.TopicList;
|
||||
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
|
||||
import org.apache.rocketmq.remoting.protocol.subscription.SubscriptionGroupConfig;
|
||||
import org.apache.rocketmq.dashboard.service.client.MQAdminExtImpl;
|
||||
import org.apache.rocketmq.dashboard.service.client.MQAdminInstance;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.RemotingClient;
|
||||
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
|
||||
import org.apache.rocketmq.remoting.protocol.RemotingSerializable;
|
||||
import org.apache.rocketmq.store.stats.BrokerStatsManager;
|
||||
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
|
||||
import org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl;
|
||||
@@ -74,22 +66,25 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class MQAdminExtImplTest {
|
||||
@@ -154,35 +149,6 @@ public class MQAdminExtImplTest {
|
||||
mqAdminExtImpl.createAndUpdateTopicConfig(brokerAddr, new TopicConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePlainAccessConfig() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
mqAdminExtImpl.deletePlainAccessConfig(brokerAddr, "rocketmq");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateGlobalWhiteAddrConfig() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
mqAdminExtImpl.updateGlobalWhiteAddrConfig(brokerAddr, "192.168.*.*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAndUpdatePlainAccessConfig() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
mqAdminExtImpl.createAndUpdatePlainAccessConfig(brokerAddr, new PlainAccessConfig());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExamineBrokerClusterAclVersionInfo() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
assertNull(mqAdminExtImpl.examineBrokerClusterAclVersionInfo(brokerAddr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExamineBrokerClusterAclConfig() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
assertNull(mqAdminExtImpl.examineBrokerClusterAclConfig(brokerAddr));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryConsumerStatus() throws Exception {
|
||||
@@ -226,18 +192,15 @@ public class MQAdminExtImplTest {
|
||||
public void testExamineTopicConfig() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
|
||||
// Create valid TopicConfigSerializeWrapper with topic_test entry
|
||||
TopicConfigSerializeWrapper wrapper = new TopicConfigSerializeWrapper();
|
||||
ConcurrentMap<String, TopicConfig> topicConfigTable = new ConcurrentHashMap<>();
|
||||
// Create valid TopicConfigSerializeWrapper with topictest entry
|
||||
TopicConfig config = new TopicConfig();
|
||||
config.setTopicName("topic_test");
|
||||
topicConfigTable.put("topic_test", config);
|
||||
wrapper.setTopicConfigTable(topicConfigTable);
|
||||
|
||||
|
||||
// Create successful response
|
||||
RemotingCommand successResponse = RemotingCommand.createResponseCommand(null);
|
||||
successResponse.setCode(ResponseCode.SUCCESS);
|
||||
successResponse.setBody(RemotingSerializable.encode(wrapper));
|
||||
successResponse.setBody(RemotingSerializable.encode(config));
|
||||
|
||||
// Mock the remote invocation
|
||||
when(remotingClient.invokeSync(eq(brokerAddr), any(RemotingCommand.class), anyLong()))
|
||||
@@ -249,6 +212,7 @@ public class MQAdminExtImplTest {
|
||||
Assert.assertEquals("topic_test", topicConfig.getTopicName());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testExamineTopicStats() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
@@ -257,7 +221,7 @@ public class MQAdminExtImplTest {
|
||||
}
|
||||
TopicStatsTable topicStatsTable = mqAdminExtImpl.examineTopicStats("topic_test");
|
||||
Assert.assertNotNull(topicStatsTable);
|
||||
Assert.assertEquals(topicStatsTable.getOffsetTable().size(), 1);
|
||||
Assert.assertEquals(1, topicStatsTable.getOffsetTable().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -347,7 +311,7 @@ public class MQAdminExtImplTest {
|
||||
public void testGetNameServerAddressList() {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
{
|
||||
when(defaultMQAdminExt.getNameServerAddressList()).thenReturn(Lists.asList("127.0.0.1:9876", new String[] {"127.0.0.2:9876"}));
|
||||
when(defaultMQAdminExt.getNameServerAddressList()).thenReturn(Lists.asList("127.0.0.1:9876", new String[]{"127.0.0.2:9876"}));
|
||||
}
|
||||
List<String> list = mqAdminExtImpl.getNameServerAddressList();
|
||||
Assert.assertEquals(list.size(), 2);
|
||||
@@ -535,12 +499,10 @@ public class MQAdminExtImplTest {
|
||||
public void testConsumeMessageDirectly() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
{
|
||||
when(defaultMQAdminExt.consumeMessageDirectly(anyString(), anyString(), anyString())).thenReturn(new ConsumeMessageDirectlyResult());
|
||||
|
||||
when(defaultMQAdminExt.consumeMessageDirectly(anyString(), anyString(), anyString(), anyString())).thenReturn(new ConsumeMessageDirectlyResult());
|
||||
}
|
||||
ConsumeMessageDirectlyResult result1 = mqAdminExtImpl.consumeMessageDirectly("group_test", "", "7F000001ACC018B4AAC2116AF6500000");
|
||||
ConsumeMessageDirectlyResult result2 = mqAdminExtImpl.consumeMessageDirectly("group_test", "", "topic_test", "7F000001ACC018B4AAC2116AF6500000");
|
||||
Assert.assertNotNull(result1);
|
||||
Assert.assertNotNull(result2);
|
||||
}
|
||||
|
||||
@@ -616,15 +578,6 @@ public class MQAdminExtImplTest {
|
||||
Assert.assertEquals(storeTime, 1628495765398L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testViewMessage() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
{
|
||||
when(defaultMQAdminExt.viewMessage(anyString())).thenReturn(new MessageExt());
|
||||
}
|
||||
MessageExt messageExt = mqAdminExtImpl.viewMessage("7F000001ACC018B4AAC2116AF6500000");
|
||||
Assert.assertNotNull(messageExt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQueryMessage() throws Exception {
|
||||
@@ -666,15 +619,6 @@ public class MQAdminExtImplTest {
|
||||
Assert.assertNotNull(timeSpans);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testViewMessage2() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
{
|
||||
when(MQAdminInstance.threadLocalMqClientInstance().getMQAdminImpl()).thenReturn(mock(MQAdminImpl.class));
|
||||
when(defaultMQAdminExt.viewMessage(anyString())).thenThrow(new RuntimeException("viewMessage exception"));
|
||||
}
|
||||
mqAdminExtImpl.viewMessage("topic_test", "7F000001ACC018B4AAC2116AF6500000");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetBrokerConfig() throws Exception {
|
||||
@@ -788,7 +732,6 @@ public class MQAdminExtImplTest {
|
||||
@Test
|
||||
public void testResumeCheckHalfMessage() throws Exception {
|
||||
assertNotNull(mqAdminExtImpl);
|
||||
Assert.assertFalse(mqAdminExtImpl.resumeCheckHalfMessage("7F000001ACC018B4AAC2116AF6500000"));
|
||||
Assert.assertFalse(mqAdminExtImpl.resumeCheckHalfMessage("topic_test", "7F000001ACC018B4AAC2116AF6500000"));
|
||||
}
|
||||
|
||||
|
@@ -18,56 +18,41 @@
|
||||
package org.apache.rocketmq.dashboard.config;
|
||||
|
||||
import org.apache.rocketmq.dashboard.BaseTest;
|
||||
import org.apache.rocketmq.dashboard.interceptor.AuthInterceptor;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AuthWebMVCConfigurerAdapterTest extends BaseTest {
|
||||
|
||||
@InjectMocks
|
||||
private AuthWebMVCConfigurerAdapter authWebMVCConfigurerAdapter;
|
||||
|
||||
@Mock
|
||||
private RMQConfigure configure;
|
||||
|
||||
@Mock
|
||||
private AuthInterceptor authInterceptor;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void addInterceptors() {
|
||||
Mockito.when(configure.isLoginRequired()).thenReturn(true);
|
||||
InterceptorRegistry registry = new InterceptorRegistry();
|
||||
Assertions.assertDoesNotThrow(() -> authWebMVCConfigurerAdapter.addInterceptors(registry));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addArgumentResolvers() {
|
||||
List<HandlerMethodArgumentResolver> argumentResolvers = Lists.newArrayList();
|
||||
authWebMVCConfigurerAdapter.addArgumentResolvers(argumentResolvers);
|
||||
Assertions.assertEquals(1, argumentResolvers.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addViewControllers() {
|
||||
ViewControllerRegistry registry = new ViewControllerRegistry(new ClassPathXmlApplicationContext());
|
||||
Assertions.assertDoesNotThrow(() -> authWebMVCConfigurerAdapter.addViewControllers(registry));
|
||||
}
|
||||
// @InjectMocks
|
||||
// private AuthWebMVCConfigurerAdapter authWebMVCConfigurerAdapter;
|
||||
//
|
||||
// @Mock
|
||||
// private RMQConfigure configure;
|
||||
//
|
||||
// @Mock
|
||||
// private AuthInterceptor authInterceptor;
|
||||
//
|
||||
// @Before
|
||||
// public void init() throws Exception {
|
||||
// MockitoAnnotations.initMocks(this);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Test
|
||||
// public void addInterceptors() {
|
||||
// Mockito.when(configure.isLoginRequired()).thenReturn(true);
|
||||
// InterceptorRegistry registry = new InterceptorRegistry();
|
||||
// Assertions.assertDoesNotThrow(() -> authWebMVCConfigurerAdapter.addInterceptors(registry));
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void addArgumentResolvers() {
|
||||
// List<HandlerMethodArgumentResolver> argumentResolvers = Lists.newArrayList();
|
||||
// authWebMVCConfigurerAdapter.addArgumentResolvers(argumentResolvers);
|
||||
// Assertions.assertEquals(1, argumentResolvers.size());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void addViewControllers() {
|
||||
// ViewControllerRegistry registry = new ViewControllerRegistry(new ClassPathXmlApplicationContext());
|
||||
// Assertions.assertDoesNotThrow(() -> authWebMVCConfigurerAdapter.addViewControllers(registry));
|
||||
// }
|
||||
}
|
@@ -16,11 +16,12 @@
|
||||
*/
|
||||
package org.apache.rocketmq.dashboard.config;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CollectExecutorConfigTest {
|
||||
|
||||
|
@@ -18,13 +18,14 @@
|
||||
package org.apache.rocketmq.dashboard.config;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.io.File;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.web.server.ErrorPage;
|
||||
import org.springframework.boot.web.server.ErrorPageRegistrar;
|
||||
import org.springframework.boot.web.server.ErrorPageRegistry;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class RMQConfigureTest {
|
||||
|
||||
private RMQConfigure rmqConfigure = new RMQConfigure();
|
||||
@@ -40,7 +41,8 @@ public class RMQConfigureTest {
|
||||
rmqConfigure.setLoginRequired(true);
|
||||
rmqConfigure.setNamesrvAddr("127.0.0.1:9876");
|
||||
rmqConfigure.setTimeoutMillis(3000L);
|
||||
rmqConfigure.setNamesrvAddrs(Lists.asList("127.0.0.1:9876", new String[] {"127.0.0.2:9876"}));
|
||||
rmqConfigure.setNamesrvAddrs(Lists.asList("127.0.0.1:9876", new String[]{"127.0.0.2:9876"}));
|
||||
rmqConfigure.setAuthMode("file");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -58,6 +60,7 @@ public class RMQConfigureTest {
|
||||
Assert.assertEquals(rmqConfigure.getNamesrvAddr(), "127.0.0.1:9876");
|
||||
Assert.assertEquals(rmqConfigure.getNamesrvAddrs().size(), 2);
|
||||
Assert.assertEquals(rmqConfigure.getTimeoutMillis().longValue(), 3000L);
|
||||
Assert.assertEquals(rmqConfigure.getAuthMode(), "file");
|
||||
ErrorPageRegistrar registrar = rmqConfigure.errorPageRegistrar();
|
||||
registrar.registerErrorPages(new ErrorPageRegistry() {
|
||||
@Override
|
||||
|
@@ -16,353 +16,228 @@
|
||||
*/
|
||||
package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import org.apache.rocketmq.common.AclConfig;
|
||||
import org.apache.rocketmq.common.PlainAccessConfig;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.AclRequest;
|
||||
|
||||
import org.apache.rocketmq.auth.authentication.enums.UserStatus;
|
||||
import org.apache.rocketmq.auth.authentication.enums.UserType;
|
||||
import org.apache.rocketmq.auth.authorization.enums.Decision;
|
||||
import org.apache.rocketmq.dashboard.model.Policy;
|
||||
import org.apache.rocketmq.dashboard.model.PolicyRequest;
|
||||
import org.apache.rocketmq.dashboard.model.request.UserCreateRequest;
|
||||
import org.apache.rocketmq.dashboard.model.request.UserInfoParam;
|
||||
import org.apache.rocketmq.dashboard.model.request.UserUpdateRequest;
|
||||
import org.apache.rocketmq.dashboard.service.impl.AclServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.dashboard.support.GlobalExceptionHandler;
|
||||
import org.apache.rocketmq.remoting.protocol.body.AclInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.UserInfo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class AclControllerTest extends BaseControllerTest {
|
||||
|
||||
@Mock
|
||||
private AclServiceImpl aclService;
|
||||
|
||||
@InjectMocks
|
||||
private AclController aclController;
|
||||
|
||||
@Spy
|
||||
private AclServiceImpl aclService;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
AclConfig aclConfig = MockObjectUtil.createAclConfig();
|
||||
when(mqAdminExt.examineBrokerClusterAclConfig(anyString())).thenReturn(aclConfig);
|
||||
ClusterInfo clusterInfo = MockObjectUtil.createClusterInfo();
|
||||
when(mqAdminExt.examineBrokerClusterInfo()).thenReturn(clusterInfo);
|
||||
doNothing().when(mqAdminExt).createAndUpdatePlainAccessConfig(anyString(), any(PlainAccessConfig.class));
|
||||
doNothing().when(mqAdminExt).deletePlainAccessConfig(anyString(), anyString());
|
||||
doNothing().when(mqAdminExt).updateGlobalWhiteAddrConfig(anyString(), anyString());
|
||||
public void init() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(aclController).setControllerAdvice(GlobalExceptionHandler.class).build();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testListUsers() {
|
||||
// Prepare test data
|
||||
String brokerAddress = "localhost:10911";
|
||||
List<UserInfo> expectedUsers = Arrays.asList(
|
||||
UserInfo.of("user1", "password1", "super"),
|
||||
UserInfo.of("user2", "password2", "super")
|
||||
);
|
||||
|
||||
// Mock service behavior
|
||||
when(aclService.listUsers(brokerAddress)).thenReturn(expectedUsers);
|
||||
|
||||
// Call controller method
|
||||
List<UserInfo> result = aclController.listUsers(brokerAddress);
|
||||
|
||||
// Verify
|
||||
assertEquals(expectedUsers, result);
|
||||
verify(aclService, times(1)).listUsers(brokerAddress);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEnableAcl() throws Exception {
|
||||
final String url = "/acl/enable.query";
|
||||
// 1. disable acl.
|
||||
requestBuilder = MockMvcRequestBuilders.get(url);
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(false));
|
||||
public void testListUsersWithoutBrokerAddress() {
|
||||
// Prepare test data
|
||||
List<UserInfo> expectedUsers = Arrays.asList(
|
||||
UserInfo.of("user1", "password1", "super")
|
||||
);
|
||||
|
||||
// 2.enable acl.
|
||||
super.mockRmqConfigure();
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
// Mock service behavior
|
||||
when(aclService.listUsers(null)).thenReturn(expectedUsers);
|
||||
// Call controller method
|
||||
List<UserInfo> result = aclController.listUsers(null);
|
||||
// Verify
|
||||
assertEquals(expectedUsers, result);
|
||||
verify(aclService, times(1)).listUsers(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAclConfig() throws Exception {
|
||||
final String url = "/acl/config.query";
|
||||
public void testListAcls() {
|
||||
// Prepare test data
|
||||
String brokerAddress = "localhost:9092";
|
||||
String searchParam = "user1";
|
||||
Object expectedAcls = Arrays.asList(
|
||||
AclInfo.of("user1", List.of("READ", "test"), List.of("TOPIC:test"), List.of("localhost:10911"), Decision.ALLOW.getName())
|
||||
);
|
||||
|
||||
// 1. broker addr table is not empty.
|
||||
ClusterInfo clusterInfo = MockObjectUtil.createClusterInfo();
|
||||
when(mqAdminExt.examineBrokerClusterInfo()).thenReturn(clusterInfo);
|
||||
requestBuilder = MockMvcRequestBuilders.get(url);
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").isMap())
|
||||
.andExpect(jsonPath("$.data.globalWhiteAddrs").isNotEmpty())
|
||||
.andExpect(jsonPath("$.data.plainAccessConfigs").isNotEmpty())
|
||||
.andExpect(jsonPath("$.data.plainAccessConfigs[0].secretKey").isNotEmpty());
|
||||
// Mock service behavior
|
||||
when(aclService.listAcls(brokerAddress, searchParam)).thenReturn(expectedAcls);
|
||||
|
||||
// 2. broker addr table is empty.
|
||||
clusterInfo.getBrokerAddrTable().clear();
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").isMap())
|
||||
.andExpect(jsonPath("$.data.globalWhiteAddrs").isEmpty())
|
||||
.andExpect(jsonPath("$.data.plainAccessConfigs").isEmpty());
|
||||
// Call controller method
|
||||
Object result = aclController.listAcls(brokerAddress, searchParam);
|
||||
|
||||
// 3. login required and user info is null.
|
||||
when(configure.isLoginRequired()).thenReturn(true);
|
||||
when(mqAdminExt.examineBrokerClusterInfo()).thenReturn(MockObjectUtil.createClusterInfo());
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").isMap())
|
||||
.andExpect(jsonPath("$.data.globalWhiteAddrs").isNotEmpty())
|
||||
.andExpect(jsonPath("$.data.plainAccessConfigs").isNotEmpty())
|
||||
.andExpect(jsonPath("$.data.plainAccessConfigs[0].secretKey").isEmpty());
|
||||
// 4. login required, but user is not admin. emmmm, Mockito may can not mock static method.
|
||||
// Verify
|
||||
assertEquals(expectedAcls, result);
|
||||
verify(aclService, times(1)).listAcls(brokerAddress, searchParam);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAclConfig() throws Exception {
|
||||
final String url = "/acl/add.do";
|
||||
PlainAccessConfig accessConfig = new PlainAccessConfig();
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
public void testCreateAcl() {
|
||||
// Prepare test data
|
||||
PolicyRequest request = new PolicyRequest();
|
||||
request.setBrokerAddress("localhost:9092");
|
||||
request.setSubject("user1");
|
||||
request.setPolicies(List.of(
|
||||
new Policy()
|
||||
));
|
||||
|
||||
// 1. access key is null.
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").exists());
|
||||
// Call controller method
|
||||
Object result = aclController.createAcl(request);
|
||||
|
||||
// 2. secret key is null.
|
||||
accessConfig.setAccessKey("test-access-key");
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").exists());
|
||||
|
||||
ClusterInfo clusterInfo = MockObjectUtil.createClusterInfo();
|
||||
when(mqAdminExt.examineBrokerClusterInfo()).thenReturn(clusterInfo);
|
||||
|
||||
// 3. add if the access key not exist.
|
||||
accessConfig.setSecretKey("12345678");
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
|
||||
// 4. add failed if the access key is existed.
|
||||
accessConfig.setAccessKey("rocketmq2");
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").exists());
|
||||
|
||||
// 5. add failed if there is no alive broker.
|
||||
clusterInfo.getBrokerAddrTable().clear();
|
||||
accessConfig.setAccessKey("test-access-key");
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").exists());
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).createAcl(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteAclConfig() throws Exception {
|
||||
final String url = "/acl/delete.do";
|
||||
PlainAccessConfig accessConfig = new PlainAccessConfig();
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
public void testDeleteUser() {
|
||||
// Prepare test data
|
||||
String brokerAddress = "localhost:9092";
|
||||
String username = "user1";
|
||||
|
||||
// 1. access key is null.
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").exists());
|
||||
// Call controller method
|
||||
Object result = aclController.deleteUser(brokerAddress, username);
|
||||
|
||||
// 2. access key is not null.
|
||||
accessConfig.setAccessKey("rocketmq");
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).deleteUser(brokerAddress, username);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateAclConfig() throws Exception {
|
||||
final String url = "/acl/update.do";
|
||||
PlainAccessConfig accessConfig = new PlainAccessConfig();
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
public void testDeleteUserWithoutBrokerAddress() {
|
||||
// Prepare test data
|
||||
String username = "user1";
|
||||
|
||||
// 1. secret key is null.
|
||||
accessConfig.setAccessKey("rocketmq");
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").exists());
|
||||
// Call controller method
|
||||
Object result = aclController.deleteUser(null, username);
|
||||
|
||||
// 2. update.
|
||||
accessConfig.setSecretKey("abcdefghjkl");
|
||||
requestBuilder.content(JSON.toJSONString(accessConfig));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).deleteUser(null, username);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAclTopicConfig() throws Exception {
|
||||
final String url = "/acl/topic/add.do";
|
||||
AclRequest request = new AclRequest();
|
||||
request.setConfig(createDefaultPlainAccessConfig());
|
||||
public void testUpdateUser() {
|
||||
// Prepare test data
|
||||
UserUpdateRequest request = new UserUpdateRequest();
|
||||
request.setBrokerAddress("localhost:9092");
|
||||
request.setUserInfo(new UserInfoParam("user1", "newPassword", UserStatus.ENABLE.getName(), UserType.SUPER.getName()));
|
||||
|
||||
// 1. if not exist.
|
||||
request.setTopicPerm("test_topic=PUB");
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Call controller method
|
||||
Object result = aclController.updateUser(request);
|
||||
|
||||
// 2. if exist.
|
||||
request.setTopicPerm("topicA=PUB");
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
|
||||
// 3. if access key not exist.
|
||||
request.getConfig().setAccessKey("test_access_key123");
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).updateUser(request.getBrokerAddress(), request.getUserInfo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddAclGroupConfig() throws Exception {
|
||||
final String url = "/acl/group/add.do";
|
||||
AclRequest request = new AclRequest();
|
||||
request.setConfig(createDefaultPlainAccessConfig());
|
||||
public void testCreateUser() {
|
||||
// Prepare test data
|
||||
UserCreateRequest request = new UserCreateRequest();
|
||||
request.setBrokerAddress("localhost:9092");
|
||||
request.setUserInfo(new UserInfoParam("user1", "newPassword", UserStatus.ENABLE.getName(), UserType.SUPER.getName()));
|
||||
|
||||
// 1. if not exist.
|
||||
request.setGroupPerm("test_consumer=PUB|SUB");
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Call controller method
|
||||
Object result = aclController.createUser(request);
|
||||
|
||||
// 2. if exist.
|
||||
request.setGroupPerm("groupA=PUB|SUB");
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
|
||||
// 3. if access key not exist.
|
||||
request.getConfig().setAccessKey("test_access_key123");
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).createUser(request.getBrokerAddress(), request.getUserInfo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeletePermConfig() throws Exception {
|
||||
final String url = "/acl/perm/delete.do";
|
||||
AclRequest request = new AclRequest();
|
||||
request.setConfig(createDefaultPlainAccessConfig());
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
public void testDeleteAcl() {
|
||||
// Prepare test data
|
||||
String brokerAddress = "localhost:9092";
|
||||
String subject = "user1";
|
||||
String resource = "TOPIC:test";
|
||||
|
||||
// if access key not exist.
|
||||
request.getConfig().setAccessKey("test_access_key123");
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Call controller method
|
||||
Object result = aclController.deleteAcl(brokerAddress, subject, resource);
|
||||
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).deleteAcl(brokerAddress, subject, resource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSyncConfig() throws Exception {
|
||||
final String url = "/acl/sync.do";
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
requestBuilder.content(JSON.toJSONString(createDefaultPlainAccessConfig()));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
public void testDeleteAclWithoutBrokerAddressAndResource() {
|
||||
// Prepare test data
|
||||
String subject = "user1";
|
||||
|
||||
// Call controller method
|
||||
Object result = aclController.deleteAcl(null, subject, null);
|
||||
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).deleteAcl(null, subject, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddWhiteList() throws Exception {
|
||||
final String url = "/acl/white/list/add.do";
|
||||
List<String> whiteList = Lists.newArrayList("192.168.0.1");
|
||||
public void testUpdateAcl() {
|
||||
// Prepare test data
|
||||
PolicyRequest request = new PolicyRequest();
|
||||
request.setBrokerAddress("localhost:9092");
|
||||
request.setSubject("user1");
|
||||
request.setPolicies(List.of(
|
||||
new Policy()
|
||||
));
|
||||
|
||||
// 1. if global white list is not null.
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
requestBuilder.content(JSON.toJSONString(whiteList));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Call controller method
|
||||
Object result = aclController.updateAcl(request);
|
||||
|
||||
// 2. if global white list is null.
|
||||
AclConfig aclConfig = MockObjectUtil.createAclConfig();
|
||||
aclConfig.setGlobalWhiteAddrs(null);
|
||||
when(mqAdminExt.examineBrokerClusterAclConfig(anyString())).thenReturn(aclConfig);
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
// Verify
|
||||
assertEquals(true, result);
|
||||
verify(aclService, times(1)).updateAcl(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteWhiteAddr() throws Exception {
|
||||
final String url = "/acl/white/list/delete.do";
|
||||
requestBuilder = MockMvcRequestBuilders.delete(url);
|
||||
requestBuilder.param("request", "localhost");
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSynchronizeWhiteList() throws Exception {
|
||||
final String url = "/acl/white/list/sync.do";
|
||||
List<String> whiteList = Lists.newArrayList();
|
||||
|
||||
// 1. if white list for syncing is empty.
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.contentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
requestBuilder.content(JSON.toJSONString(whiteList));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").exists());
|
||||
|
||||
// 2. if white list for syncing is not empty.
|
||||
whiteList.add("localhost");
|
||||
requestBuilder.content(JSON.toJSONString(whiteList));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(0));
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return aclController;
|
||||
}
|
||||
|
||||
private PlainAccessConfig createDefaultPlainAccessConfig() {
|
||||
PlainAccessConfig config = new PlainAccessConfig();
|
||||
config.setAdmin(false);
|
||||
config.setAccessKey("rocketmq");
|
||||
config.setSecretKey("123456789");
|
||||
config.setDefaultGroupPerm("SUB");
|
||||
config.setDefaultTopicPerm("DENY");
|
||||
config.setTopicPerms(Lists.newArrayList("topicA=DENY", "topicB=PUB|SUB"));
|
||||
config.setGroupPerms(Lists.newArrayList("groupA=DENY", "groupB=PUB|SUB"));
|
||||
|
||||
return config;
|
||||
}
|
||||
}
|
@@ -23,6 +23,8 @@ import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.dashboard.support.GlobalExceptionHandler;
|
||||
import org.apache.rocketmq.dashboard.support.GlobalRestfulResponseBodyAdvice;
|
||||
import org.apache.rocketmq.dashboard.util.MyPrintingResultHandler;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.route.BrokerData;
|
||||
import org.apache.rocketmq.tools.admin.MQAdminExt;
|
||||
import org.junit.Before;
|
||||
import org.mockito.Mock;
|
||||
@@ -32,6 +34,12 @@ import org.springframework.test.web.servlet.ResultActions;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
@@ -16,17 +16,18 @@
|
||||
*/
|
||||
package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.KVTable;
|
||||
import org.apache.rocketmq.dashboard.service.impl.ClusterServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.KVTable;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
|
@@ -19,44 +19,43 @@ package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.dashboard.model.QueueStatInfo;
|
||||
import org.apache.rocketmq.dashboard.model.TopicConsumerInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.ConsumerConfigInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.DeleteSubGroupRequest;
|
||||
import org.apache.rocketmq.dashboard.model.request.ResetOffsetRequest;
|
||||
import org.apache.rocketmq.dashboard.service.ClusterInfoService;
|
||||
import org.apache.rocketmq.dashboard.service.impl.ConsumerServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.protocol.ResponseCode;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.ConsumeStats;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.RollbackStats;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.remoting.protocol.ResponseCode;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.Connection;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumerConnection;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumerRunningInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.SubscriptionGroupWrapper;
|
||||
import org.apache.rocketmq.remoting.protocol.heartbeat.ConsumeType;
|
||||
import org.apache.rocketmq.remoting.protocol.heartbeat.MessageModel;
|
||||
import org.apache.rocketmq.remoting.protocol.subscription.SubscriptionGroupConfig;
|
||||
import org.apache.rocketmq.dashboard.model.request.ConsumerConfigInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.DeleteSubGroupRequest;
|
||||
import org.apache.rocketmq.dashboard.model.request.ResetOffsetRequest;
|
||||
import org.apache.rocketmq.dashboard.service.impl.ConsumerServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.dashboard.model.TopicConsumerInfo;
|
||||
import org.apache.rocketmq.dashboard.model.QueueStatInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.Connection;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -71,12 +70,18 @@ public class ConsumerControllerTest extends BaseControllerTest {
|
||||
@Spy
|
||||
private ConsumerServiceImpl consumerService;
|
||||
|
||||
@Mock
|
||||
private ClusterInfoService clusterInfoService;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
// 2. mock ClusterInfo data
|
||||
ClusterInfo mockClusterInfo = getClusterInfo();
|
||||
when(clusterInfoService.get()).thenReturn(mockClusterInfo);
|
||||
consumerService.afterPropertiesSet();
|
||||
super.mockRmqConfigure();
|
||||
ClusterInfo clusterInfo = MockObjectUtil.createClusterInfo();
|
||||
when(mqAdminExt.examineBrokerClusterInfo()).thenReturn(clusterInfo);
|
||||
// ClusterInfo clusterInfo = MockObjectUtil.createClusterInfo();
|
||||
// when(mqAdminExt.examineBrokerClusterInfo()).thenReturn(clusterInfo);
|
||||
SubscriptionGroupWrapper wrapper = MockObjectUtil.createSubscriptionGroupWrapper();
|
||||
when(mqAdminExt.getAllSubscriptionGroup(anyString(), anyLong())).thenReturn(wrapper);
|
||||
ConsumeStats stats = MockObjectUtil.createConsumeStats();
|
||||
@@ -109,6 +114,7 @@ public class ConsumerControllerTest extends BaseControllerTest {
|
||||
@Test
|
||||
public void testGroupQuery() throws Exception {
|
||||
final String url = "/consumer/group.query";
|
||||
|
||||
requestBuilder = MockMvcRequestBuilders.get(url);
|
||||
requestBuilder.param("consumerGroup", "group_test");
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
@@ -177,6 +183,10 @@ public class ConsumerControllerTest extends BaseControllerTest {
|
||||
|
||||
@Test
|
||||
public void testExamineSubscriptionGroupConfig() throws Exception {
|
||||
ClusterInfo mockClusterInfo = getClusterInfo();
|
||||
{
|
||||
when(clusterInfoService.get()).thenReturn(mockClusterInfo);
|
||||
}
|
||||
final String url = "/consumer/examineSubscriptionGroupConfig.query";
|
||||
requestBuilder = MockMvcRequestBuilders.get(url);
|
||||
requestBuilder.param("consumerGroup", "group_test");
|
||||
@@ -188,7 +198,9 @@ public class ConsumerControllerTest extends BaseControllerTest {
|
||||
@Test
|
||||
public void testDelete() throws Exception {
|
||||
final String url = "/consumer/deleteSubGroup.do";
|
||||
ClusterInfo mockClusterInfo = getClusterInfo();
|
||||
{
|
||||
when(clusterInfoService.get()).thenReturn(mockClusterInfo);
|
||||
doNothing().when(mqAdminExt).deleteSubscriptionGroup(any(), anyString());
|
||||
doNothing().when(mqAdminExt).deleteTopicInBroker(any(), anyString());
|
||||
doNothing().when(mqAdminExt).deleteTopicInNameServer(any(), anyString());
|
||||
@@ -214,7 +226,6 @@ public class ConsumerControllerTest extends BaseControllerTest {
|
||||
requestBuilder.content(JSON.toJSONString(consumerConfigInfo));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
performErrorExpect(perform);
|
||||
|
||||
{
|
||||
doNothing().when(mqAdminExt).createAndUpdateSubscriptionGroupConfig(anyString(), any());
|
||||
}
|
||||
@@ -233,6 +244,7 @@ public class ConsumerControllerTest extends BaseControllerTest {
|
||||
.andExpect(jsonPath("$.data").value(true));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testQueryConsumerByTopic() throws Exception {
|
||||
// Prepare test data
|
||||
@@ -313,7 +325,8 @@ public class ConsumerControllerTest extends BaseControllerTest {
|
||||
.andExpect(jsonPath("$.data.jstack").value("test"));
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return consumerController;
|
||||
}
|
||||
}
|
||||
|
@@ -18,14 +18,6 @@ package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.Files;
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.rocketmq.dashboard.service.impl.DashboardCollectServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.service.impl.DashboardServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.JsonUtil;
|
||||
@@ -36,6 +28,15 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
@@ -18,13 +18,8 @@ package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.common.MixAll;
|
||||
import org.apache.rocketmq.remoting.protocol.ResponseCode;
|
||||
import org.apache.rocketmq.remoting.protocol.body.CMResult;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumeMessageDirectlyResult;
|
||||
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
|
||||
import org.apache.rocketmq.dashboard.model.DlqMessageRequest;
|
||||
import org.apache.rocketmq.dashboard.model.MessagePage;
|
||||
import org.apache.rocketmq.dashboard.model.MessageView;
|
||||
@@ -32,6 +27,10 @@ import org.apache.rocketmq.dashboard.model.request.MessageQuery;
|
||||
import org.apache.rocketmq.dashboard.service.impl.DlqMessageServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.service.impl.MessageServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.protocol.ResponseCode;
|
||||
import org.apache.rocketmq.remoting.protocol.body.CMResult;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumeMessageDirectlyResult;
|
||||
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
@@ -41,6 +40,8 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -172,7 +173,8 @@ public class DlqMessageControllerTest extends BaseControllerTest {
|
||||
.andExpect(content().contentType("application/vnd.ms-excel;charset=utf-8"));
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return dlqMessageController;
|
||||
}
|
||||
}
|
||||
|
@@ -16,20 +16,26 @@
|
||||
*/
|
||||
package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import org.apache.rocketmq.dashboard.model.User;
|
||||
import org.apache.rocketmq.dashboard.service.impl.UserServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.service.strategy.UserContext;
|
||||
import org.apache.rocketmq.dashboard.service.strategy.UserStrategy;
|
||||
import org.apache.rocketmq.dashboard.support.GlobalExceptionHandler;
|
||||
import org.apache.rocketmq.dashboard.util.WebUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@@ -38,19 +44,27 @@ public class LoginControllerTest extends BaseControllerTest {
|
||||
@InjectMocks
|
||||
private LoginController loginController;
|
||||
|
||||
@Spy
|
||||
@Mock
|
||||
private UserServiceImpl userService;
|
||||
|
||||
@Spy
|
||||
private UserContext userContext;
|
||||
|
||||
@Spy
|
||||
private UserStrategy userStrategy;
|
||||
|
||||
private String contextPath = "/rocketmq-console";
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
super.mockRmqConfigure();
|
||||
when(configure.isLoginRequired()).thenReturn(true);
|
||||
when(configure.getRocketMqDashboardDataPath()).thenReturn("");
|
||||
Field contextPathField = ReflectionUtils.findField(LoginController.class, "contextPath");
|
||||
ReflectionUtils.makeAccessible(contextPathField);
|
||||
ReflectionUtils.setField(contextPathField, loginController, contextPath);
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(loginController).setControllerAdvice(GlobalExceptionHandler.class).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -60,57 +74,56 @@ public class LoginControllerTest extends BaseControllerTest {
|
||||
requestBuilder.sessionAttr(WebUtil.USER_NAME, "admin");
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.logined").value(true))
|
||||
.andExpect(jsonPath("$.data.loginRequired").value(true));
|
||||
.andExpect(jsonPath("$.logined").value(true))
|
||||
.andExpect(jsonPath("$.loginRequired").value(true));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testLogin() throws Exception {
|
||||
final String url = "/login/login.do";
|
||||
final String username = "admin";
|
||||
final String rightPwd = "admin";
|
||||
final String wrongPwd = "rocketmq";
|
||||
{
|
||||
UserServiceImpl.FileBasedUserInfoStore store
|
||||
= new UserServiceImpl.FileBasedUserInfoStore(configure);
|
||||
User user = store.queryByName(username);
|
||||
Assert.assertNotNull(user);
|
||||
Assert.assertEquals(user.getPassword(), rightPwd);
|
||||
ReflectionTestUtils.setField(userService, "fileBasedUserInfoStore", store);
|
||||
}
|
||||
|
||||
// 模拟 userService.queryByName 方法返回一个用户
|
||||
User user = new User("admin", "admin", 1);
|
||||
user.setPassword(rightPwd);
|
||||
|
||||
|
||||
// 1、login fail
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.param("username", username)
|
||||
.param("password", wrongPwd);
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform = mockMvc.perform(post(url)
|
||||
.param("username", username)
|
||||
.param("password", wrongPwd));
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").doesNotExist())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").value("Bad username or password!"));
|
||||
|
||||
// 2、login success
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder.param("username", username)
|
||||
.param("password", rightPwd);
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.contextPath").value(contextPath));
|
||||
when(userService.queryByUsernameAndPassword(username, rightPwd)).thenReturn(user);
|
||||
|
||||
// 2、login success
|
||||
perform = mockMvc.perform(post(url)
|
||||
.param("username", username)
|
||||
.param("password", rightPwd));
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.contextPath").value(contextPath));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogout() throws Exception {
|
||||
final String url = "/login/logout.do";
|
||||
requestBuilder = MockMvcRequestBuilders.post(url);
|
||||
requestBuilder = post(url);
|
||||
requestBuilder.sessionAttr(WebUtil.USER_NAME, "root");
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data").value(contextPath));
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return loginController;
|
||||
}
|
||||
}
|
||||
|
@@ -18,11 +18,6 @@ package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.cache.Cache;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.apache.rocketmq.client.QueryResult;
|
||||
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
|
||||
import org.apache.rocketmq.client.consumer.PullResult;
|
||||
@@ -32,28 +27,33 @@ import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.common.message.MessageClientIDSetter;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.remoting.protocol.body.CMResult;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumeMessageDirectlyResult;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumerConnection;
|
||||
import org.apache.rocketmq.dashboard.model.QueueOffsetInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.MessageQuery;
|
||||
import org.apache.rocketmq.dashboard.service.impl.MessageServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.support.AutoCloseConsumerWrapper;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.RPCHook;
|
||||
import org.apache.rocketmq.remoting.protocol.body.CMResult;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumeMessageDirectlyResult;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumerConnection;
|
||||
import org.apache.rocketmq.tools.admin.api.MessageTrack;
|
||||
import org.apache.rocketmq.tools.admin.api.TrackType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
@@ -71,6 +71,9 @@ public class MessageControllerTest extends BaseControllerTest {
|
||||
|
||||
private DefaultMQPullConsumer defaultMQPullConsumer;
|
||||
|
||||
@Mock
|
||||
private AutoCloseConsumerWrapper autoCloseConsumerWrapper;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
super.mockRmqConfigure();
|
||||
@@ -89,7 +92,6 @@ public class MessageControllerTest extends BaseControllerTest {
|
||||
when(pullResult.getNextBeginOffset()).thenReturn(Long.MAX_VALUE);
|
||||
when(pullResult.getPullStatus()).thenReturn(PullStatus.FOUND);
|
||||
when(pullResult.getMsgFoundList()).thenReturn(wrappers);
|
||||
when(messageService.buildDefaultMQPullConsumer(any(), anyBoolean())).thenReturn(defaultMQPullConsumer);
|
||||
|
||||
// Ensure searchOffset returns values that make sense for the test times
|
||||
when(defaultMQPullConsumer.searchOffset(any(MessageQueue.class), anyLong())).thenAnswer(invocation -> {
|
||||
@@ -115,6 +117,7 @@ public class MessageControllerTest extends BaseControllerTest {
|
||||
// Override the previous mock to ensure the test finds messages
|
||||
when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt()))
|
||||
.thenReturn(pullResultWithMessages);
|
||||
when(autoCloseConsumerWrapper.getConsumer(any(RPCHook.class), anyBoolean())).thenReturn(defaultMQPullConsumer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,7 +272,8 @@ public class MessageControllerTest extends BaseControllerTest {
|
||||
.andExpect(jsonPath("$.data.consumeResult").value(CMResult.CR_LATER.name()));
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return messageController;
|
||||
}
|
||||
}
|
||||
|
@@ -16,8 +16,6 @@
|
||||
*/
|
||||
package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.rocketmq.client.QueryResult;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.client.trace.TraceType;
|
||||
@@ -33,6 +31,9 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
@@ -135,7 +136,8 @@ public class MessageTraceControllerTest extends BaseControllerTest {
|
||||
.andExpect(jsonPath("$.data.messageTraceViews", hasSize(4)));
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return messageTraceController;
|
||||
}
|
||||
}
|
||||
|
@@ -17,9 +17,6 @@
|
||||
package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.apache.rocketmq.common.MixAll;
|
||||
import org.apache.rocketmq.dashboard.model.ConsumerMonitorConfig;
|
||||
import org.apache.rocketmq.dashboard.service.impl.MonitorServiceImpl;
|
||||
@@ -33,6 +30,10 @@ import org.mockito.Spy;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
@@ -135,7 +136,8 @@ public class MonitorControllerTest extends BaseControllerTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return monitorController;
|
||||
}
|
||||
}
|
||||
|
@@ -44,7 +44,8 @@ public class NamesvrControllerTest extends BaseControllerTest {
|
||||
Assert.assertEquals(namesrvAddr, "127.0.0.1:9876");
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return namesvrController;
|
||||
}
|
||||
}
|
||||
|
@@ -17,8 +17,6 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.apache.rocketmq.dashboard.service.checker.RocketMqChecker;
|
||||
import org.apache.rocketmq.dashboard.service.checker.impl.ClusterHealthCheckerImpl;
|
||||
@@ -34,6 +32,9 @@ import org.mockito.Spy;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
|
@@ -17,28 +17,33 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard.controller;
|
||||
|
||||
import java.util.HashSet;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.remoting.protocol.body.Connection;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ProducerConnection;
|
||||
import org.apache.rocketmq.dashboard.interceptor.AuthInterceptor;
|
||||
import org.apache.rocketmq.dashboard.service.impl.LoginServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.service.impl.ProducerServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.service.strategy.UserContext;
|
||||
import org.apache.rocketmq.dashboard.support.GlobalExceptionHandler;
|
||||
import org.apache.rocketmq.dashboard.support.GlobalRestfulResponseBodyAdvice;
|
||||
import org.apache.rocketmq.dashboard.util.MyPrintingResultHandler;
|
||||
import org.apache.rocketmq.dashboard.util.WebUtil;
|
||||
import org.apache.rocketmq.remoting.protocol.LanguageCode;
|
||||
import org.apache.rocketmq.remoting.protocol.body.Connection;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ProducerConnection;
|
||||
import org.apache.rocketmq.remoting.protocol.body.UserInfo;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
@@ -52,9 +57,17 @@ public class ProducerControllerTest extends BaseControllerTest {
|
||||
@Spy
|
||||
private ProducerServiceImpl producerService;
|
||||
|
||||
@Override protected MockMvc createMockMvc() {
|
||||
@Spy
|
||||
private LoginServiceImpl loginService;
|
||||
|
||||
@Mock
|
||||
private UserContext userContext;
|
||||
|
||||
@Override
|
||||
protected MockMvc createMockMvc() {
|
||||
AuthInterceptor authInterceptor = new AuthInterceptor();
|
||||
ReflectionTestUtils.setField(authInterceptor, "loginService", new LoginServiceImpl());
|
||||
ReflectionTestUtils.setField(authInterceptor, "loginService", loginService);
|
||||
ReflectionTestUtils.setField(loginService, "userContext", userContext);
|
||||
MockMvc innerMockMvc = MockMvcBuilders.standaloneSetup(getTestController())
|
||||
.addInterceptors(authInterceptor)
|
||||
.alwaysDo(MyPrintingResultHandler.me())
|
||||
@@ -65,7 +78,7 @@ public class ProducerControllerTest extends BaseControllerTest {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init(){
|
||||
public void init() {
|
||||
createMockMvc();
|
||||
}
|
||||
|
||||
@@ -88,6 +101,7 @@ public class ProducerControllerTest extends BaseControllerTest {
|
||||
conn.setVersion(LanguageCode.JAVA.getCode());
|
||||
connections.add(conn);
|
||||
producerConnection.setConnectionSet(connections);
|
||||
when(userContext.queryByUsername(any(String.class))).thenReturn(UserInfo.of("admin", "admin", "super"));
|
||||
when(mqAdminExt.examineProducerConnectionInfo(anyString(), anyString()))
|
||||
.thenThrow(new MQClientException("Not found the producer group connection", null))
|
||||
.thenReturn(producerConnection);
|
||||
|
@@ -19,49 +19,52 @@ package org.apache.rocketmq.dashboard.controller;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import org.apache.rocketmq.client.impl.MQClientAPIImpl;
|
||||
import org.apache.rocketmq.client.impl.factory.MQClientInstance;
|
||||
import org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl;
|
||||
import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.client.producer.SendStatus;
|
||||
import org.apache.rocketmq.common.MixAll;
|
||||
import org.apache.rocketmq.common.TopicConfig;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.ConsumeStats;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.TopicStatsTable;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.dashboard.model.request.SendTopicMessageRequest;
|
||||
import org.apache.rocketmq.dashboard.model.request.TopicConfigInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.TopicTypeList;
|
||||
import org.apache.rocketmq.dashboard.service.ClusterInfoService;
|
||||
import org.apache.rocketmq.dashboard.service.ConsumerService;
|
||||
import org.apache.rocketmq.dashboard.service.impl.TopicServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.RPCHook;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.ConsumeStats;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.TopicStatsTable;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumerConnection;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumerRunningInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.GroupList;
|
||||
import org.apache.rocketmq.remoting.protocol.body.TopicList;
|
||||
import org.apache.rocketmq.remoting.protocol.route.BrokerData;
|
||||
import org.apache.rocketmq.remoting.protocol.route.QueueData;
|
||||
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
|
||||
import org.apache.rocketmq.dashboard.model.request.SendTopicMessageRequest;
|
||||
import org.apache.rocketmq.dashboard.model.request.TopicConfigInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.TopicTypeList;
|
||||
import org.apache.rocketmq.dashboard.service.impl.ConsumerServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.service.impl.TopicServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.RPCHook;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -77,14 +80,23 @@ public class TopicControllerTest extends BaseControllerTest {
|
||||
@Spy
|
||||
private TopicServiceImpl topicService;
|
||||
|
||||
@Spy
|
||||
private ConsumerServiceImpl consumerService;
|
||||
@Mock
|
||||
private ConsumerService consumerService;
|
||||
|
||||
@Mock
|
||||
private DefaultMQProducer producer;
|
||||
|
||||
@Mock
|
||||
private ClusterInfoService clusterInfoService;
|
||||
|
||||
private String topicName = "topic_test";
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
super.mockRmqConfigure();
|
||||
ClusterInfo mockClusterInfo = getClusterInfo();
|
||||
when(clusterInfoService.get()).thenReturn(mockClusterInfo);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -139,7 +151,7 @@ public class TopicControllerTest extends BaseControllerTest {
|
||||
// 3、filter system topic
|
||||
requestBuilder = MockMvcRequestBuilders.get(url);
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
String[] topicString = {"%SYS%system_topic2","common_topic2","%SYS%system_topic1","common_topic1"};
|
||||
String[] topicString = {"%SYS%system_topic2", "common_topic2", "%SYS%system_topic1", "common_topic1"};
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.data.topicList").value(containsInAnyOrder(topicString)));
|
||||
}
|
||||
@@ -258,15 +270,35 @@ public class TopicControllerTest extends BaseControllerTest {
|
||||
public void testSendTopicMessage() throws Exception {
|
||||
final String url = "/topic/sendTopicMessage.do";
|
||||
{
|
||||
DefaultMQProducer producer = mock(DefaultMQProducer.class);
|
||||
doNothing().when(producer).start();
|
||||
doNothing().when(producer).shutdown();
|
||||
TopicConfig topicConfig = new TopicConfig(topicName);
|
||||
topicConfig.setReadQueueNums(4);
|
||||
topicConfig.setWriteQueueNums(4);
|
||||
topicConfig.setPerm(6);
|
||||
topicConfig.setOrder(false);
|
||||
TopicRouteData topicRouteData = new TopicRouteData();
|
||||
BrokerData brokerData = new BrokerData();
|
||||
brokerData.setBrokerName("broker-a");
|
||||
brokerData.setCluster("DefaultCluster");
|
||||
HashMap<Long, String> brokerAddrs = new HashMap<>();
|
||||
brokerAddrs.put(0L, "127.0.0.1:9876");
|
||||
brokerData.setBrokerAddrs(brokerAddrs);
|
||||
topicRouteData.setBrokerDatas(List.of(brokerData));
|
||||
topicRouteData.setQueueDatas(List.of(new QueueData()));
|
||||
topicRouteData.getQueueDatas().get(0).setReadQueueNums(4);
|
||||
topicRouteData.getQueueDatas().get(0).setWriteQueueNums(4);
|
||||
topicRouteData.getQueueDatas().get(0).setPerm(6);
|
||||
topicRouteData.getQueueDatas().get(0).setBrokerName("broker-a");
|
||||
when(mqAdminExt.examineTopicRouteInfo(topicName)).thenReturn(topicRouteData);
|
||||
when(topicService.examineTopicConfig(topicName,"broker-a")).thenReturn(topicConfig);
|
||||
|
||||
SendResult result = new SendResult(SendStatus.SEND_OK, "7F000001E41A2E5D6D978B82C20F003D",
|
||||
"0A8E83C300002A9F00000000000013D3", new MessageQueue(), 1000L);
|
||||
when(producer.send(any(Message.class))).thenReturn(result);
|
||||
doReturn(producer).when(topicService).buildDefaultMQProducer(anyString(), any(), anyBoolean());
|
||||
when(producer.send((Message) argThat(msg -> msg != null))).thenReturn(result);
|
||||
doReturn(producer).when(topicService).buildDefaultMQProducer(any(String.class), any(RPCHook.class), anyBoolean());
|
||||
}
|
||||
Assert.assertNotNull(topicService.buildDefaultMQProducer("group_test", mock(RPCHook.class)));
|
||||
Assert.assertNotNull(topicService.buildDefaultMQProducer(MixAll.SELF_TEST_PRODUCER_GROUP, mock(RPCHook.class),false));
|
||||
SendTopicMessageRequest request = new SendTopicMessageRequest();
|
||||
request.setTopic(topicName);
|
||||
request.setMessageBody("hello world");
|
||||
@@ -275,8 +307,8 @@ public class TopicControllerTest extends BaseControllerTest {
|
||||
requestBuilder.content(JSON.toJSONString(request));
|
||||
perform = mockMvc.perform(requestBuilder);
|
||||
perform.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.status").value(-1))
|
||||
.andExpect(jsonPath("$.errMsg").value(containsString("NullPointerException")));
|
||||
.andExpect(jsonPath("$.data.sendStatus").value(SendStatus.SEND_OK.name()))
|
||||
.andExpect(jsonPath("$.data.msgId").value("7F000001E41A2E5D6D978B82C20F003D"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -358,7 +390,8 @@ public class TopicControllerTest extends BaseControllerTest {
|
||||
.andExpect(jsonPath("$.data.messageTypeList[2]").value("SYSTEM"));
|
||||
}
|
||||
|
||||
@Override protected Object getTestController() {
|
||||
@Override
|
||||
protected Object getTestController() {
|
||||
return topicController;
|
||||
}
|
||||
}
|
||||
|
@@ -18,10 +18,6 @@ package org.apache.rocketmq.dashboard.permission;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.io.Files;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.rocketmq.dashboard.BaseTest;
|
||||
import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.dashboard.model.User;
|
||||
@@ -43,6 +39,11 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -112,7 +113,7 @@ public class PermissionAspectTest extends BaseTest {
|
||||
when(configure.getRocketMqDashboardDataPath()).thenReturn("/tmp/rocketmq-console/test/data");
|
||||
Map<String, Map<String, List<String>>> rolePermsMap = new HashMap<>();
|
||||
Map<String, List<String>> rolePerms = new HashMap<>();
|
||||
List<String> accessUrls = Lists.asList("/topic/route.query", new String[] {"/topic/stats.query"});
|
||||
List<String> accessUrls = Lists.asList("/topic/route.query", new String[]{"/topic/stats.query"});
|
||||
rolePerms.put("admin", accessUrls);
|
||||
rolePermsMap.put("rolePerms", rolePerms);
|
||||
File file = createTestFile(rolePermsMap);
|
||||
|
@@ -19,21 +19,21 @@ package org.apache.rocketmq.dashboard.service.impl;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import org.apache.rocketmq.acl.common.AclClientRPCHook;
|
||||
import org.apache.rocketmq.acl.common.SessionCredentials;
|
||||
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
|
||||
import org.apache.rocketmq.client.consumer.PullResult;
|
||||
import org.apache.rocketmq.client.consumer.PullStatus;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.common.Pair;
|
||||
import org.apache.rocketmq.common.message.MessageClientIDSetter;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.dashboard.exception.ServiceException;
|
||||
import org.apache.rocketmq.dashboard.model.MessagePage;
|
||||
import org.apache.rocketmq.dashboard.model.MessageQueryByPage;
|
||||
import org.apache.rocketmq.dashboard.model.MessageView;
|
||||
import org.apache.rocketmq.dashboard.model.QueueOffsetInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.MessageQuery;
|
||||
import org.apache.rocketmq.dashboard.model.MessageQueryByPage;
|
||||
import org.apache.rocketmq.dashboard.support.AutoCloseConsumerWrapper;
|
||||
import org.apache.rocketmq.remoting.RPCHook;
|
||||
import org.apache.rocketmq.remoting.protocol.body.Connection;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumeMessageDirectlyResult;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ConsumerConnection;
|
||||
@@ -42,42 +42,24 @@ import org.apache.rocketmq.tools.admin.api.MessageTrack;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
public class MessageServiceImplTest {
|
||||
@@ -93,11 +75,15 @@ public class MessageServiceImplTest {
|
||||
private RMQConfigure configure;
|
||||
|
||||
@Mock
|
||||
private DefaultMQPullConsumer consumer;
|
||||
private DefaultMQPullConsumer defaultMQPullConsumer;
|
||||
|
||||
@Mock
|
||||
private AutoCloseConsumerWrapper autoCloseConsumerWrapper;
|
||||
|
||||
@Mock
|
||||
private Cache<String, MessagePage> messagePageCache;
|
||||
|
||||
|
||||
private static final String TOPIC = "testTopic";
|
||||
private static final String MSG_ID = "testMsgId";
|
||||
private static final String CONSUMER_GROUP = "testConsumerGroup";
|
||||
@@ -109,10 +95,10 @@ public class MessageServiceImplTest {
|
||||
public void setUp() throws Exception {
|
||||
// Set up default mock responses
|
||||
when(configure.getNamesrvAddr()).thenReturn("localhost:9876");
|
||||
when(configure.getAccessKey()).thenReturn("12345678");
|
||||
when(configure.getSecretKey()).thenReturn("rocketmq");
|
||||
when(configure.isUseTLS()).thenReturn(false);
|
||||
|
||||
// Mock the consumer creation to avoid actual RocketMQ calls
|
||||
lenient().doReturn(consumer).when(messageService).buildDefaultMQPullConsumer(any(), anyBoolean());
|
||||
when(autoCloseConsumerWrapper.getConsumer(any(RPCHook.class), anyBoolean())).thenReturn(defaultMQPullConsumer);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -194,8 +180,9 @@ public class MessageServiceImplTest {
|
||||
Set<MessageQueue> messageQueues = new HashSet<>();
|
||||
messageQueues.add(new MessageQueue(TOPIC, "broker-1", 0));
|
||||
messageQueues.add(new MessageQueue(TOPIC, "broker-2", 1));
|
||||
when(consumer.fetchSubscribeMessageQueues(TOPIC)).thenReturn(messageQueues);
|
||||
|
||||
System.out.println("Consumer from wrapper: " + autoCloseConsumerWrapper.getConsumer(new AclClientRPCHook(new SessionCredentials(configure.getAccessKey(), configure.getSecretKey())), false));
|
||||
when(defaultMQPullConsumer.fetchSubscribeMessageQueues(TOPIC)).thenReturn(messageQueues);
|
||||
System.out.println(defaultMQPullConsumer.fetchSubscribeMessageQueues(TOPIC));
|
||||
// Setup pull results for both queues
|
||||
PullResult pullResult1 = createPullResult(PullStatus.FOUND, Arrays.asList(
|
||||
createMessageExt("id1", TOPIC, "body1", 1500),
|
||||
@@ -210,7 +197,7 @@ public class MessageServiceImplTest {
|
||||
PullResult emptyResult = createPullResult(PullStatus.NO_NEW_MSG, Collections.emptyList(), 10, 10);
|
||||
|
||||
// First pull gets messages, second pull gets empty to terminate loop
|
||||
when(consumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt()))
|
||||
when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt()))
|
||||
.thenReturn(pullResult1)
|
||||
.thenReturn(emptyResult)
|
||||
.thenReturn(pullResult2)
|
||||
@@ -230,9 +217,8 @@ public class MessageServiceImplTest {
|
||||
assertEquals("id3", result.get(2).getMsgId()); // 1800
|
||||
assertEquals("id1", result.get(3).getMsgId()); // 1500
|
||||
|
||||
verify(consumer, times(4)).pull(any(MessageQueue.class), eq("*"), anyLong(), anyInt());
|
||||
verify(consumer).start();
|
||||
verify(consumer).shutdown();
|
||||
verify(defaultMQPullConsumer, times(4)).pull(any(MessageQueue.class), eq("*"), anyLong(), anyInt());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -240,7 +226,7 @@ public class MessageServiceImplTest {
|
||||
// Setup message queues
|
||||
Set<MessageQueue> messageQueues = new HashSet<>();
|
||||
messageQueues.add(new MessageQueue(TOPIC, "broker-1", 0));
|
||||
when(consumer.fetchSubscribeMessageQueues(TOPIC)).thenReturn(messageQueues);
|
||||
when(defaultMQPullConsumer.fetchSubscribeMessageQueues(TOPIC)).thenReturn(messageQueues);
|
||||
|
||||
// Setup pull results - some messages are outside time range
|
||||
PullResult pullResult = createPullResult(PullStatus.FOUND, Arrays.asList(
|
||||
@@ -251,7 +237,7 @@ public class MessageServiceImplTest {
|
||||
|
||||
PullResult emptyResult = createPullResult(PullStatus.NO_NEW_MSG, Collections.emptyList(), 10, 10);
|
||||
|
||||
when(consumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt()))
|
||||
when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt()))
|
||||
.thenReturn(pullResult)
|
||||
.thenReturn(emptyResult);
|
||||
|
||||
@@ -270,7 +256,7 @@ public class MessageServiceImplTest {
|
||||
// Setup message queues
|
||||
Set<MessageQueue> messageQueues = new HashSet<>();
|
||||
messageQueues.add(new MessageQueue(TOPIC, "broker-1", 0));
|
||||
when(consumer.fetchSubscribeMessageQueues(TOPIC)).thenReturn(messageQueues);
|
||||
when(defaultMQPullConsumer.fetchSubscribeMessageQueues(TOPIC)).thenReturn(messageQueues);
|
||||
|
||||
// Test all different pull statuses
|
||||
PullResult pullResult1 = createPullResult(PullStatus.FOUND,
|
||||
@@ -285,7 +271,7 @@ public class MessageServiceImplTest {
|
||||
PullResult pullResult4 = createPullResult(PullStatus.OFFSET_ILLEGAL,
|
||||
Collections.emptyList(), 7, 8);
|
||||
|
||||
when(consumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt()))
|
||||
when(defaultMQPullConsumer.pull(any(MessageQueue.class), anyString(), anyLong(), anyInt()))
|
||||
.thenReturn(pullResult1)
|
||||
.thenReturn(pullResult2)
|
||||
.thenReturn(pullResult3)
|
||||
@@ -447,22 +433,6 @@ public class MessageServiceImplTest {
|
||||
assertEquals(10, (qo1.getEndOffset() - 5L) + (qo2.getEndOffset() - 10L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildDefaultMQPullConsumer() {
|
||||
// Test with TLS enabled
|
||||
DefaultMQPullConsumer tlsConsumer = messageService.buildDefaultMQPullConsumer(null, true);
|
||||
assertNotNull(tlsConsumer);
|
||||
|
||||
// Test with TLS disabled
|
||||
DefaultMQPullConsumer nonTlsConsumer = messageService.buildDefaultMQPullConsumer(null, false);
|
||||
assertNotNull(nonTlsConsumer);
|
||||
|
||||
// Test with RPC hook
|
||||
AclClientRPCHook rpcHook = mock(AclClientRPCHook.class);
|
||||
DefaultMQPullConsumer hookConsumer = messageService.buildDefaultMQPullConsumer(rpcHook, false);
|
||||
assertNotNull(hookConsumer);
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
|
||||
private MessageExt createMessageExt(String msgId, String topic, String body, long storeTimestamp) {
|
||||
|
@@ -17,15 +17,27 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard.service.impl;
|
||||
|
||||
import org.apache.rocketmq.acl.common.AclClientRPCHook;
|
||||
import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.client.producer.SendStatus;
|
||||
import org.apache.rocketmq.client.producer.TransactionMQProducer;
|
||||
import org.apache.rocketmq.common.attribute.TopicMessageType;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.dashboard.BaseTest;
|
||||
import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.dashboard.model.request.SendTopicMessageRequest;
|
||||
import org.apache.rocketmq.dashboard.model.request.TopicConfigInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.TopicTypeList;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.dashboard.service.ClusterInfoService;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.TopicList;
|
||||
import org.apache.rocketmq.tools.admin.MQAdminExt;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
@@ -36,38 +48,11 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.apache.rocketmq.acl.common.AclClientRPCHook;
|
||||
import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.client.producer.SendStatus;
|
||||
import org.apache.rocketmq.client.producer.TransactionMQProducer;
|
||||
import org.apache.rocketmq.common.attribute.TopicMessageType;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.dashboard.model.request.SendTopicMessageRequest;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class TopicServiceImplTest {
|
||||
public class TopicServiceImplTest extends BaseTest {
|
||||
|
||||
@InjectMocks
|
||||
@Spy
|
||||
@@ -79,59 +64,16 @@ public class TopicServiceImplTest {
|
||||
@Mock
|
||||
private RMQConfigure configure;
|
||||
|
||||
@Mock
|
||||
private ClusterInfoService clusterInfoService;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// Setup common mocks
|
||||
when(configure.getNamesrvAddr()).thenReturn("localhost:9876");
|
||||
|
||||
// Use lenient() to prevent the unnecessary stubbing error
|
||||
lenient().when(configure.isUseTLS()).thenReturn(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExamineAllTopicType() throws Exception {
|
||||
// Create mock TopicList with different types of topics
|
||||
TopicList topicList = new TopicList();
|
||||
Set<String> topicSet = new HashSet<>();
|
||||
topicSet.add("normalTopic");
|
||||
topicSet.add("%RETRY%someGroup");
|
||||
topicSet.add("%DLQ%someGroup");
|
||||
topicSet.add("%SYS%sysTopic");
|
||||
topicList.setTopicList(topicSet);
|
||||
|
||||
// Mock fetchAllTopicList to return our test topics
|
||||
doReturn(topicList).when(topicService).fetchAllTopicList(anyBoolean(), anyBoolean());
|
||||
|
||||
// Mock examineTopicConfig for the normal topic
|
||||
TopicConfigInfo configInfo = new TopicConfigInfo();
|
||||
configInfo.setMessageType("NORMAL");
|
||||
List<TopicConfigInfo> topicConfigInfos = new ArrayList<>();
|
||||
topicConfigInfos.add(configInfo);
|
||||
doReturn(topicConfigInfos).when(topicService).examineTopicConfig(anyString());
|
||||
|
||||
// Call the method being tested
|
||||
TopicTypeList result = topicService.examineAllTopicType();
|
||||
|
||||
// Verify the results
|
||||
Assert.assertNotNull(result);
|
||||
Assert.assertEquals(4, result.getTopicNameList().size());
|
||||
Assert.assertEquals(4, result.getMessageTypeList().size());
|
||||
|
||||
// Verify that the topics contain the expected names and types
|
||||
// Note: the actual order might be different due to sorting in the method
|
||||
// So we're checking that all expected items are included
|
||||
Assert.assertTrue(result.getTopicNameList().contains("normalTopic"));
|
||||
Assert.assertTrue(result.getTopicNameList().contains("%RETRY%someGroup"));
|
||||
Assert.assertTrue(result.getTopicNameList().contains("%DLQ%someGroup"));
|
||||
Assert.assertTrue(result.getTopicNameList().contains("%SYS%sysTopic"));
|
||||
|
||||
// Verify message types
|
||||
Assert.assertTrue(result.getMessageTypeList().contains("NORMAL"));
|
||||
Assert.assertTrue(result.getMessageTypeList().contains("RETRY"));
|
||||
Assert.assertTrue(result.getMessageTypeList().contains("DELAY"));
|
||||
Assert.assertTrue(result.getMessageTypeList().contains("SYSTEM"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendTopicMessageRequestNormal() throws Exception {
|
||||
// Prepare test data
|
||||
|
@@ -21,6 +21,28 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.google.common.cache.LoadingCache;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Sets;
|
||||
import org.apache.rocketmq.common.MixAll;
|
||||
import org.apache.rocketmq.dashboard.BaseTest;
|
||||
import org.apache.rocketmq.dashboard.config.CollectExecutorConfig;
|
||||
import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.dashboard.service.impl.DashboardCollectServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.JsonUtil;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.remoting.protocol.body.BrokerStatsData;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.GroupList;
|
||||
import org.apache.rocketmq.remoting.protocol.body.KVTable;
|
||||
import org.apache.rocketmq.remoting.protocol.body.TopicList;
|
||||
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
|
||||
import org.apache.rocketmq.tools.admin.MQAdminExt;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -32,27 +54,6 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.apache.rocketmq.common.MixAll;
|
||||
import org.apache.rocketmq.remoting.protocol.body.BrokerStatsData;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
import org.apache.rocketmq.remoting.protocol.body.GroupList;
|
||||
import org.apache.rocketmq.remoting.protocol.body.KVTable;
|
||||
import org.apache.rocketmq.remoting.protocol.body.TopicList;
|
||||
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
|
||||
import org.apache.rocketmq.dashboard.BaseTest;
|
||||
import org.apache.rocketmq.dashboard.config.CollectExecutorConfig;
|
||||
import org.apache.rocketmq.dashboard.config.RMQConfigure;
|
||||
import org.apache.rocketmq.dashboard.service.impl.DashboardCollectServiceImpl;
|
||||
import org.apache.rocketmq.dashboard.util.JsonUtil;
|
||||
import org.apache.rocketmq.dashboard.util.MockObjectUtil;
|
||||
import org.apache.rocketmq.tools.admin.MQAdminExt;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -210,8 +211,8 @@ public class DashboardCollectTaskTest extends BaseTest {
|
||||
|
||||
private void mockBrokerFileExistBeforeSaveData() throws Exception {
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
map.put("broker-a" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[] {"1000"}));
|
||||
map.put("broker-b" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[] {"1000"}));
|
||||
map.put("broker-a" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[]{"1000"}));
|
||||
map.put("broker-b" + ":" + MixAll.MASTER_ID, Lists.asList("1000", new String[]{"1000"}));
|
||||
MixAll.string2File(JsonUtil.obj2String(map), brokerFile.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
@@ -18,8 +18,7 @@ package org.apache.rocketmq.dashboard.testbase;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.Lists;
|
||||
import java.util.List;
|
||||
import javax.annotation.Resource;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
|
||||
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
|
||||
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
|
||||
@@ -29,16 +28,18 @@ import org.apache.rocketmq.client.producer.SendResult;
|
||||
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
import org.apache.rocketmq.remoting.protocol.subscription.SubscriptionGroupConfig;
|
||||
import org.apache.rocketmq.dashboard.model.request.ConsumerConfigInfo;
|
||||
import org.apache.rocketmq.dashboard.model.request.TopicConfigInfo;
|
||||
import org.apache.rocketmq.dashboard.service.ConsumerService;
|
||||
import org.apache.rocketmq.dashboard.service.TopicService;
|
||||
import org.apache.rocketmq.dashboard.util.JsonUtil;
|
||||
import org.apache.rocketmq.remoting.protocol.subscription.SubscriptionGroupConfig;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ComponentScan(basePackageClasses = {TestRocketMQServer.class})
|
||||
public abstract class RocketMQConsoleTestBase {
|
||||
private Logger consoleTestBaseLog = LoggerFactory.getLogger(RocketMQConsoleTestBase.class);
|
||||
@@ -65,13 +66,13 @@ public abstract class RocketMQConsoleTestBase {
|
||||
|
||||
public static abstract class RetryTempLate<T> {
|
||||
protected abstract T process() throws Exception;
|
||||
|
||||
public T execute(int times, long waitTime) throws Exception {
|
||||
Exception exception = null;
|
||||
for (int i = 0; i < times; i++) {
|
||||
try {
|
||||
return process();
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
} catch (Exception ignore) {
|
||||
exception = ignore;
|
||||
if (waitTime > 0) {
|
||||
Thread.sleep(waitTime);
|
||||
@@ -84,14 +85,12 @@ public abstract class RocketMQConsoleTestBase {
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected void startTestMQProducer() {
|
||||
producer = new DefaultMQProducer(TEST_PRODUCER_GROUP);
|
||||
producer.setInstanceName(String.valueOf(System.currentTimeMillis()));
|
||||
try {
|
||||
producer.start();
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
Throwables.throwIfUnchecked(e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -107,8 +106,7 @@ public abstract class RocketMQConsoleTestBase {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
return producer.send(message);
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
} catch (Exception ignore) {
|
||||
Thread.sleep(500);
|
||||
}
|
||||
}
|
||||
@@ -137,8 +135,7 @@ public abstract class RocketMQConsoleTestBase {
|
||||
}
|
||||
});
|
||||
consumer.start();
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
Throwables.throwIfUnchecked(e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@@ -17,11 +17,8 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard.testbase;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.apache.rocketmq.broker.BrokerController;
|
||||
import org.apache.rocketmq.common.BrokerConfig;
|
||||
import org.apache.rocketmq.common.MixAll;
|
||||
@@ -34,6 +31,10 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import static java.io.File.separator;
|
||||
import static org.apache.rocketmq.dashboard.testbase.TestConstant.TEST_BROKER_NAME;
|
||||
import static org.apache.rocketmq.dashboard.testbase.TestConstant.TEST_CLUSTER_NAME;
|
||||
@@ -100,8 +101,7 @@ public class TestRocketMQServer {
|
||||
namesrvController.initialize();
|
||||
log.info("Success to start Name Server:{}", TestConstant.NAME_SERVER_ADDRESS);
|
||||
namesrvController.start();
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to start Name Server", e);
|
||||
System.exit(1);
|
||||
}
|
||||
@@ -124,8 +124,7 @@ public class TestRocketMQServer {
|
||||
log.info("Broker Start name:{} address:{}", brokerConfig.getBrokerName(), brokerController.getBrokerAddr());
|
||||
brokerController.start();
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to start Broker", e);
|
||||
System.exit(1);
|
||||
}
|
||||
@@ -144,8 +143,7 @@ public class TestRocketMQServer {
|
||||
}
|
||||
if (file.isFile()) {
|
||||
file.delete();
|
||||
}
|
||||
else if (file.isDirectory()) {
|
||||
} else if (file.isDirectory()) {
|
||||
File[] files = file.listFiles();
|
||||
for (File file1 : files) {
|
||||
deleteFile(file1);
|
||||
|
@@ -18,17 +18,22 @@
|
||||
package org.apache.rocketmq.dashboard.util;
|
||||
|
||||
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.apache.rocketmq.dashboard.support.AutoCloseConsumerWrapper;
|
||||
import org.apache.rocketmq.remoting.RPCHook;
|
||||
import java.lang.reflect.Field;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import org.apache.rocketmq.client.exception.MQClientException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.Instant;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class AutoCloseConsumerWrapperTests {
|
||||
@@ -66,7 +71,6 @@ class AutoCloseConsumerWrapperTests {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
void shouldCloseIdleConsumer() throws Exception {
|
||||
TestableWrapper wrapper = new TestableWrapper();
|
||||
|
@@ -16,34 +16,21 @@
|
||||
*/
|
||||
package org.apache.rocketmq.dashboard.util;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import org.apache.rocketmq.client.producer.LocalTransactionState;
|
||||
import org.apache.rocketmq.client.trace.TraceConstants;
|
||||
import org.apache.rocketmq.client.trace.TraceType;
|
||||
import org.apache.rocketmq.common.AclConfig;
|
||||
import org.apache.rocketmq.remoting.protocol.DataVersion;
|
||||
import org.apache.rocketmq.common.MixAll;
|
||||
import org.apache.rocketmq.common.PlainAccessConfig;
|
||||
import org.apache.rocketmq.common.TopicConfig;
|
||||
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.dashboard.model.DlqMessageRequest;
|
||||
import org.apache.rocketmq.remoting.protocol.DataVersion;
|
||||
import org.apache.rocketmq.remoting.protocol.LanguageCode;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.ConsumeStats;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.OffsetWrapper;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.TopicOffset;
|
||||
import org.apache.rocketmq.remoting.protocol.admin.TopicStatsTable;
|
||||
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
import org.apache.rocketmq.common.message.MessageQueue;
|
||||
import org.apache.rocketmq.remoting.protocol.body.BrokerStatsData;
|
||||
import org.apache.rocketmq.remoting.protocol.body.BrokerStatsItem;
|
||||
import org.apache.rocketmq.remoting.protocol.body.ClusterInfo;
|
||||
@@ -61,9 +48,18 @@ import org.apache.rocketmq.remoting.protocol.route.BrokerData;
|
||||
import org.apache.rocketmq.remoting.protocol.route.QueueData;
|
||||
import org.apache.rocketmq.remoting.protocol.route.TopicRouteData;
|
||||
import org.apache.rocketmq.remoting.protocol.subscription.SubscriptionGroupConfig;
|
||||
import org.apache.rocketmq.dashboard.model.DlqMessageRequest;
|
||||
import org.apache.rocketmq.remoting.protocol.LanguageCode;
|
||||
import org.checkerframework.checker.units.qual.A;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static org.apache.rocketmq.remoting.protocol.heartbeat.ConsumeType.CONSUME_ACTIVELY;
|
||||
|
||||
@@ -317,25 +313,5 @@ public class MockObjectUtil {
|
||||
return dlqMessages;
|
||||
}
|
||||
|
||||
public static AclConfig createAclConfig() {
|
||||
PlainAccessConfig adminConfig = new PlainAccessConfig();
|
||||
adminConfig.setAdmin(true);
|
||||
adminConfig.setAccessKey("rocketmq2");
|
||||
adminConfig.setSecretKey("12345678");
|
||||
|
||||
PlainAccessConfig normalConfig = new PlainAccessConfig();
|
||||
normalConfig.setAdmin(false);
|
||||
normalConfig.setAccessKey("rocketmq");
|
||||
normalConfig.setSecretKey("123456789");
|
||||
normalConfig.setDefaultGroupPerm("SUB");
|
||||
normalConfig.setDefaultTopicPerm("DENY");
|
||||
normalConfig.setTopicPerms(Lists.newArrayList("topicA=DENY", "topicB=PUB|SUB"));
|
||||
normalConfig.setGroupPerms(Lists.newArrayList("groupA=DENY", "groupB=PUB|SUB"));
|
||||
|
||||
|
||||
AclConfig aclConfig = new AclConfig();
|
||||
aclConfig.setPlainAccessConfigs(Lists.newArrayList(adminConfig, normalConfig));
|
||||
aclConfig.setGlobalWhiteAddrs(Lists.newArrayList("localhost"));
|
||||
return aclConfig;
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,6 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard.util;
|
||||
|
||||
import java.util.List;
|
||||
import org.apache.rocketmq.client.trace.TraceConstants;
|
||||
import org.apache.rocketmq.client.trace.TraceContext;
|
||||
import org.apache.rocketmq.common.UtilAll;
|
||||
@@ -25,6 +24,8 @@ import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MsgTraceDecodeUtilTest {
|
||||
private StringBuilder pubTraceDataBase;
|
||||
private StringBuilder subTraceDataBase;
|
||||
|
@@ -17,13 +17,14 @@
|
||||
|
||||
package org.apache.rocketmq.dashboard.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.web.servlet.result.PrintingResultHandler;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class MyPrintingResultHandler extends PrintingResultHandler {
|
||||
public static MyPrintingResultHandler me() {
|
||||
return new MyPrintingResultHandler();
|
||||
|
@@ -18,10 +18,8 @@
|
||||
package org.apache.rocketmq.dashboard.web;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
@@ -32,6 +30,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@@ -61,21 +61,21 @@ public class WebStaticApplicationTests {
|
||||
|
||||
@Test
|
||||
public void testResources() throws Exception {
|
||||
for(Map.Entry<String,String> entry : resourcesMap().entrySet()){
|
||||
resources(entry.getValue(),entry.getKey());
|
||||
for (Map.Entry<String, String> entry : resourcesMap().entrySet()) {
|
||||
resources(entry.getValue(), entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String,String> resourcesMap(){
|
||||
Map<String,String> map = Maps.newHashMap();
|
||||
map.put("text/css;charset=UTF-8","/vendor/bootstrap/css/bootstrap.css");
|
||||
map.put("text/css;charset=UTF-8","/vendor/bootstrap/css/bootstrap-theme.css");
|
||||
map.put("text/css;charset=UTF-8","/vendor/bootstrap-material-design/css/bootstrap-material-design.css");
|
||||
map.put("text/css;charset=UTF-8","/vendor/bootstrap-material-design/css/ripples.css");
|
||||
private Map<String, String> resourcesMap() {
|
||||
Map<String, String> map = Maps.newHashMap();
|
||||
map.put("text/css;charset=UTF-8", "/vendor/bootstrap/css/bootstrap.css");
|
||||
map.put("text/css;charset=UTF-8", "/vendor/bootstrap/css/bootstrap-theme.css");
|
||||
map.put("text/css;charset=UTF-8", "/vendor/bootstrap-material-design/css/bootstrap-material-design.css");
|
||||
map.put("text/css;charset=UTF-8", "/vendor/bootstrap-material-design/css/ripples.css");
|
||||
return map;
|
||||
}
|
||||
|
||||
private void resources(String path,String type){
|
||||
private void resources(String path, String type) {
|
||||
ResponseEntity<String> entity = this.restTemplate.getForEntity(path, String.class);
|
||||
assertThat(entity.getHeaders().getContentType())
|
||||
.isEqualTo(MediaType.valueOf(type));
|
||||
|
Reference in New Issue
Block a user