Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration test demo for rocketmq consumption prohibition plugin. #1401

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mq-consume-prohibition-test</artifactId>
<groupId>com.huaweicloud.sermant</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mq-consume-prohibition-common-demo</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<httpclient4x.version>4.5.13</httpclient4x.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient4x.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.mq.prohibition.common.constant;

/**
* rocketmq常量
*
* @author daizhenyu
* @since 2024-01-08
**/
public class RocketMqConstant {
/**
* 消息中间件的topic
*/
public static final String PUSH_CONSUME_TOPIC = "topic-push-1";

/**
* 消息中间件的topic
*/
public static final String PULL_SUBSCRIBE_CONSUME_TOPIC = "topic-pull-subscribe-1";

/**
* 消息中间件的topic
*/
public static final String PULL_ASSIGN_CONSUME_TOPIC = "topic-pull-assign-1";

/**
* 时间格式
*/
public static final String TIME_FORMAT = "yyyy/MM/dd HH:mm:ss";

/**
* rocketmq消息的tag
*/
public static final String TAG = "";

/**
* rocketmq消费者消费消息的标签范围
*/
public static final String TAG_SCOPE = "*";

/**
* rocketmq生产者组
*/
public static final String PRODUCE_GROUP = "producer-group";

/**
* rocketmq push消费者组
*/
public static final String PUSH_CONSUME_GROUP = "push-group";

/**
* rocketmq pull subscribe消费者组
*/
public static final String PULL_SUBSCRIBE_CONSUME_GROUP = "pull-subscribe-group";

/**
* rocketmq pull assign消费者组
*/
public static final String PULL_ASSIGN_CONSUME_GROUP = "pull-assign-group";

/**
* rocketmq消息体
*/
public static final String MESSAGE_BODY_ROCKET = "hello inner rocketmq:";

private RocketMqConstant() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.mq.prohibition.common.utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
* http请求工具类
*
* @author daizhenyu
* @since 2024-01-08
**/
public class HttpRequestUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequestUtils.class);

private static final int SUCCESS_CODE = 200;

/**
* http的get请求
*
* @param url http请求url
* @return 响应体body
*/
private static String doGet(String url) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
RequestConfig requestConfig = RequestConfig.custom()
.build();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
if (response.getStatusLine().getStatusCode() == SUCCESS_CODE) {
return EntityUtils.toString(response.getEntity());
}
LOGGER.info("Request error, the message is: {}", EntityUtils.toString(response.getEntity()));
return "";
}
} catch (IOException e) {
LOGGER.info("Request exception, the message is: {}", e.getMessage());
return "";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.mq.prohibition.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;

/**
* 反射工具类
*
* @author daizhenyu
* @since 2024-01-09
**/
public class ReflectUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(ReflectUtils.class);

private ReflectUtils() {
}

/**
* 获取类的私有属性
*
* @param clazz 类对象
* @param obj 类实例
* @param fieldName 属性名
* @return 属性值
*/
public static Object getField(Class clazz, Object obj, String fieldName) {
Object fieldValue = null;
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
fieldValue = field.get(obj);
} catch (NoSuchFieldException | IllegalAccessException e) {
LOGGER.error("Get field error, the message is: {}", e.getMessage());
}
return fieldValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mq-consume-prohibition-test</artifactId>
<groupId>com.huaweicloud.sermant</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>mq-consume-prohibition-integration-test</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<curator.version>4.3.0</curator.version>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.huaweicloud.sermant</groupId>
<artifactId>mq-consume-prohibition-common-demo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.mq.prohibition.integration.rocketmq;

/**
* 消息队列禁止消费插件rocketmq测试用例
*
* @author daizhenyu
* @since 2024-01-08
**/
public class RocketMqProhibitionTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.mq.prohibition.integration.utils;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.nio.charset.StandardCharsets;

/**
* 下发动态配置的工具类
*
* @author daizhenyu
* @since 2024-01-09
**/
public class DynamicConfigUtils {
public static final String ZOOKEEPER_NODE_PATH = "/app=default&environment/sermant.mq.consume.globalConfig";

private DynamicConfigUtils() {
}

public static void updateConfig(String config) throws Exception {
updateConfig(ZOOKEEPER_NODE_PATH, config);
}

public static void updateConfig(String path, String config) throws Exception {
CuratorFramework curator = CuratorFrameworkFactory.newClient("127.0.0.1:2181",
new ExponentialBackoffRetry(1000, 3));
curator.start();
Stat stat = curator.checkExists().forPath(path);
if (stat == null) {
curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path,
config.getBytes(StandardCharsets.UTF_8));
} else {
curator.setData().forPath(path, config.getBytes(StandardCharsets.UTF_8));
}
}
}
Loading