parent
6c37780e37
commit
7e2d5d00af
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.rule;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Condition expression.
|
||||
* @author lepdou 2022-07-06
|
||||
*/
|
||||
public class Condition {
|
||||
|
||||
private String key;
|
||||
private String operation;
|
||||
private List<String> values;
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public List<String> getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
public void setValues(List<String> values) {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public String getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public void setOperation(String operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Condition{" +
|
||||
"key='" + key + '\'' +
|
||||
", values='" + values + '\'' +
|
||||
", operation='" + operation + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.rule;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The util for condition expression.
|
||||
* @author lepdou 2022-07-11
|
||||
*/
|
||||
public final class ConditionUtils {
|
||||
|
||||
private ConditionUtils() {
|
||||
}
|
||||
|
||||
public static boolean match(Map<String, String> actualValues, List<Condition> conditions) {
|
||||
for (Condition condition : conditions) {
|
||||
List<String> expectedValues = condition.getValues();
|
||||
String operation = condition.getOperation();
|
||||
String key = condition.getKey();
|
||||
String actualValue = actualValues.get(key);
|
||||
|
||||
if (!Operation.match(expectedValues, actualValue, operation)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.rule;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* The util for key/value pair.
|
||||
* @author lepdou 2022-07-11
|
||||
*/
|
||||
public final class KVPairUtils {
|
||||
|
||||
private KVPairUtils() {
|
||||
}
|
||||
|
||||
public static Map<String, String> toMap(List<KVPair> labels) {
|
||||
if (CollectionUtils.isEmpty(labels)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
Map<String, String> result = new HashMap<>();
|
||||
labels.forEach(label -> {
|
||||
result.put(label.getKey(), label.getValue());
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,132 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.rule;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.tencent.polaris.api.utils.CollectionUtils;
|
||||
import com.tencent.polaris.api.utils.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
* The condition operation.
|
||||
* @author lepdou 2022-07-11
|
||||
*/
|
||||
public enum Operation {
|
||||
|
||||
/**
|
||||
* case sensitive string equals.
|
||||
*/
|
||||
EQUALS("EQUALS"),
|
||||
/**
|
||||
* case sensitive string not equals.
|
||||
*/
|
||||
NOT_EQUALS("NOT_EQUALS"),
|
||||
/**
|
||||
* whether element in collection.
|
||||
*/
|
||||
IN("IN"),
|
||||
/**
|
||||
* whether element not in collection.
|
||||
*/
|
||||
NOT_IN("NOT_IN"),
|
||||
/**
|
||||
* regex operation.
|
||||
*/
|
||||
REGEX("REGEX"),
|
||||
/**
|
||||
* whether element is blank.
|
||||
*/
|
||||
BLANK("BLANK"),
|
||||
/**
|
||||
* whether element is not blank.
|
||||
*/
|
||||
NOT_BLANK("NOT_BLANK");
|
||||
|
||||
private final String value;
|
||||
|
||||
Operation(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static boolean match(List<String> expectedValues, String actualValue, String rawOperation) {
|
||||
String firstExpectedValue = null;
|
||||
if (!CollectionUtils.isEmpty(expectedValues)) {
|
||||
firstExpectedValue = expectedValues.get(0);
|
||||
}
|
||||
|
||||
switch (getOperation(rawOperation)) {
|
||||
case EQUALS:
|
||||
return firstExpectedValue != null && StringUtils.equals(actualValue, firstExpectedValue);
|
||||
case NOT_EQUALS:
|
||||
return firstExpectedValue == null || !StringUtils.equals(actualValue, firstExpectedValue);
|
||||
case BLANK:
|
||||
return StringUtils.isBlank(actualValue);
|
||||
case NOT_BLANK:
|
||||
return !StringUtils.isBlank(actualValue);
|
||||
case IN:
|
||||
if (CollectionUtils.isEmpty(expectedValues)) {
|
||||
return false;
|
||||
}
|
||||
return expectedValues.contains(actualValue);
|
||||
case NOT_IN:
|
||||
if (CollectionUtils.isEmpty(expectedValues)) {
|
||||
return true;
|
||||
}
|
||||
return !expectedValues.contains(actualValue);
|
||||
case REGEX:
|
||||
if (firstExpectedValue == null) {
|
||||
return false;
|
||||
}
|
||||
Pattern r = Pattern.compile(firstExpectedValue);
|
||||
return r.matcher(actualValue).matches();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static Operation getOperation(String operation) {
|
||||
if (StringUtils.equalsIgnoreCase(operation, EQUALS.value)) {
|
||||
return EQUALS;
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase(operation, NOT_EQUALS.value)) {
|
||||
return NOT_EQUALS;
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase(operation, IN.value)) {
|
||||
return IN;
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase(operation, NOT_IN.value)) {
|
||||
return NOT_IN;
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase(operation, REGEX.value)) {
|
||||
return REGEX;
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase(operation, BLANK.value)) {
|
||||
return BLANK;
|
||||
}
|
||||
if (StringUtils.equalsIgnoreCase(operation, NOT_BLANK.value)) {
|
||||
return NOT_BLANK;
|
||||
}
|
||||
throw new RuntimeException("Unsupported operation. operation = " + operation);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.async;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.task.TaskExecutor;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisAsyncConfiguration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisAsyncConfigurationTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunnerWithoutPolarisAsyncConfiguration
|
||||
= new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(PolarisAsyncConfiguration.class));
|
||||
|
||||
private final ApplicationContextRunner contextRunnerWithPolarisAsyncConfiguration
|
||||
= new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(PolarisAsyncConfiguration.class, TestConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.tencent.async.enabled=true");
|
||||
|
||||
@Test
|
||||
public void testAsyncExecutor() {
|
||||
this.contextRunnerWithoutPolarisAsyncConfiguration.run(context -> {
|
||||
assertThat(context).doesNotHaveBean(PolarisAsyncConfiguration.class);
|
||||
});
|
||||
|
||||
this.contextRunnerWithPolarisAsyncConfiguration.run(context -> {
|
||||
assertThat(context).hasSingleBean(PolarisAsyncConfiguration.class);
|
||||
assertThat(context).hasSingleBean(TaskExecutor.class);
|
||||
String[] executorNames = context.getBeanNamesForType(TaskExecutor.class);
|
||||
List<String> executorNameList = Arrays.asList(executorNames);
|
||||
assertThat(executorNameList).contains("polarisAsyncExecutor");
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Async
|
||||
protected static class TestConfiguration {
|
||||
|
||||
}
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.rule;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link Operation}.
|
||||
* @author lepdou 2022-07-12
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class OperationTest {
|
||||
|
||||
@Test
|
||||
public void testEqual() {
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "v1", Operation.EQUALS.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "v2", Operation.EQUALS.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.singletonList(""), "v2", Operation.EQUALS.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "", Operation.EQUALS.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), null, Operation.EQUALS.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.emptyList(), "v1", Operation.EQUALS.getValue())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEqual() {
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "v1", Operation.NOT_EQUALS.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "v2", Operation.NOT_EQUALS.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.singletonList(""), "v2", Operation.NOT_EQUALS.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "", Operation.NOT_EQUALS.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), null, Operation.NOT_EQUALS.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.emptyList(), "v1", Operation.NOT_EQUALS.getValue())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIn() {
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "v1", Operation.IN.getValue())).isTrue();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "v2", Operation.IN.getValue())).isTrue();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "v4", Operation.IN.getValue())).isFalse();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "", Operation.IN.getValue())).isFalse();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), null, Operation.IN.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.emptyList(), null, Operation.IN.getValue())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotIn() {
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "v1", Operation.NOT_IN.getValue())).isFalse();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "v2", Operation.NOT_IN.getValue())).isFalse();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "v4", Operation.NOT_IN.getValue())).isTrue();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), "", Operation.NOT_IN.getValue())).isTrue();
|
||||
assertThat(Operation.match(Arrays.asList("v1", "v2", "v3"), null, Operation.NOT_IN.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.emptyList(), null, Operation.NOT_IN.getValue())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmpty() {
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), null, Operation.BLANK.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "", Operation.BLANK.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.emptyList(), null, Operation.BLANK.getValue())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotEmpty() {
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), null, Operation.NOT_BLANK.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.singletonList("v1"), "", Operation.NOT_BLANK.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.emptyList(), null, Operation.NOT_BLANK.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.emptyList(), "v1", Operation.NOT_BLANK.getValue())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegex() {
|
||||
assertThat(Operation.match(Collections.singletonList("v[1~10]"), "v1", Operation.REGEX.getValue())).isTrue();
|
||||
assertThat(Operation.match(Collections.singletonList("v[1~10]"), "v12", Operation.REGEX.getValue())).isFalse();
|
||||
assertThat(Operation.match(Collections.singletonList("v[1~10]*"), "v12", Operation.REGEX.getValue())).isFalse();
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.util;
|
||||
|
||||
import com.tencent.cloud.common.async.PolarisAsyncConfiguration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* Test for {@link ApplicationContextAwareUtils}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class ApplicationContextAwareUtilsTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(ApplicationContextAwareUtils.class))
|
||||
.withPropertyValues("key1=value1");
|
||||
|
||||
@Test
|
||||
public void testApplicationContextAwareUtils() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(ApplicationContextAwareUtils.class);
|
||||
assertThat(ApplicationContextAwareUtils.getApplicationContext()).isNotNull();
|
||||
|
||||
// test getProperties
|
||||
assertThat(ApplicationContextAwareUtils.getProperties("key1")).isEqualTo("value1");
|
||||
assertThat(ApplicationContextAwareUtils.getProperties("key2")).isNull();
|
||||
|
||||
// test getProperties with default value
|
||||
assertThat(ApplicationContextAwareUtils.getProperties("key1", "defaultValue")).isEqualTo("value1");
|
||||
assertThat(ApplicationContextAwareUtils.getProperties("key2", "defaultValue")).isEqualTo("defaultValue");
|
||||
|
||||
// test getBean
|
||||
assertThat(ApplicationContextAwareUtils.getBean(ApplicationContextAwareUtils.class)).isNotNull();
|
||||
assertThatThrownBy(() -> {
|
||||
ApplicationContextAwareUtils.getBean(PolarisAsyncConfiguration.class);
|
||||
}).isInstanceOf(NoSuchBeanDefinitionException.class);
|
||||
|
||||
// test getBeanIfExists
|
||||
assertThat(ApplicationContextAwareUtils.getBeanIfExists(ApplicationContextAwareUtils.class)).isNotNull();
|
||||
assertThat(ApplicationContextAwareUtils.getBeanIfExists(PolarisAsyncConfiguration.class)).isNull();
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Tests for {@link GzipUtil}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class GzipUtilTest {
|
||||
|
||||
@Test
|
||||
public void testGzip() {
|
||||
try {
|
||||
String testData = "1234567890";
|
||||
String compressed = GzipUtil.compressBase64Encode(testData, "UTF-8");
|
||||
String decompressed = GzipUtil.base64DecodeDecompress(compressed, "UTF-8");
|
||||
assertThat(decompressed).isEqualTo(testData);
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
GzipUtil.compressBase64Encode(testData, "UTF-7");
|
||||
}).isInstanceOf(IOException.class);
|
||||
|
||||
compressed = GzipUtil.compressBase64Encode(testData.getBytes(), "UTF-8");
|
||||
decompressed = GzipUtil.base64DecodeDecompress(compressed, "UTF-8");
|
||||
assertThat(decompressed).isEqualTo(testData);
|
||||
decompressed = GzipUtil.base64DecodeDecompress(compressed);
|
||||
assertThat(decompressed).isEqualTo(testData);
|
||||
}
|
||||
catch (IOException e) {
|
||||
fail("Gzip compress or decompress failed", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.util;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisCompletableFutureUtils}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisCompletableFutureUtilsTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
metadataContext.putContext(MetadataContext.FRAGMENT_TRANSITIVE, "key1", "value1");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
MetadataContextHolder.remove();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSupplyAsync() {
|
||||
PolarisCompletableFutureUtils.supplyAsync(() -> {
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key1")).isEqualTo("value1");
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key2")).isNull();
|
||||
MetadataContextHolder.get().putContext(MetadataContext.FRAGMENT_TRANSITIVE, "key2", "value2");
|
||||
return "test";
|
||||
}).thenAccept(result -> {
|
||||
assertThat(result).isEqualTo("test");
|
||||
}).join();
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key1")).isEqualTo("value1");
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key2")).isEqualTo("value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRunAsync() {
|
||||
PolarisCompletableFutureUtils.runAsync(() -> {
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key1")).isEqualTo("value1");
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key2")).isNull();
|
||||
MetadataContextHolder.get().putContext(MetadataContext.FRAGMENT_TRANSITIVE, "key2", "value3");
|
||||
}).join();
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key1")).isEqualTo("value1");
|
||||
assertThat(MetadataContextHolder.get()
|
||||
.getContext(MetadataContext.FRAGMENT_TRANSITIVE, "key2")).isEqualTo("value3");
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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.tencent.cloud.common.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.tsf.core.entity.Tag;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link ReflectionUtils}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class ReflectionUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testWritableBeanField() {
|
||||
Field field = ReflectionUtils.findField(Tag.class, "key");
|
||||
assertThat(field).isNotNull();
|
||||
assertThat(ReflectionUtils.writableBeanField(field)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCapitalize() {
|
||||
String name = "test";
|
||||
assertThat(ReflectionUtils.capitalize(name)).isEqualTo("Test");
|
||||
|
||||
String gender = "";
|
||||
assertThat(ReflectionUtils.capitalize(gender)).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFieldValue() {
|
||||
Tag tag = new Tag("key", "value");
|
||||
assertThat(ReflectionUtils.getFieldValue(tag, "key")).isEqualTo("key");
|
||||
assertThat(ReflectionUtils.getFieldValue(tag, "value")).isEqualTo("value");
|
||||
assertThat(ReflectionUtils.getFieldValue(tag, "flags")).isNotNull().isInstanceOf(Set.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetFieldValue() {
|
||||
Tag tag = new Tag("key", null);
|
||||
ReflectionUtils.setFieldValue(tag, "key", "newKey");
|
||||
assertThat(ReflectionUtils.getFieldValue(tag, "key")).isEqualTo("newKey");
|
||||
ReflectionUtils.setFieldValue(tag, "value1", "newValue");
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||
*
|
||||
* Copyright (C) 2021 Tencent. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* 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 org.springframework.tsf.core.util;
|
||||
|
||||
import com.tencent.cloud.common.async.PolarisAsyncConfiguration;
|
||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatNoException;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* Test for {@link TsfSpringContextAware}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class TsfSpringContextAwareTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(ApplicationContextAwareUtils.class))
|
||||
.withPropertyValues("key1=value1");
|
||||
|
||||
@Test
|
||||
public void testApplicationContextAwareUtils() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(TsfSpringContextAware.getApplicationContext()).isNotNull();
|
||||
TsfSpringContextAware tsfSpringContextAware = new TsfSpringContextAware();
|
||||
assertThatNoException().isThrownBy(() -> tsfSpringContextAware.setApplicationContext(context));
|
||||
|
||||
// test getProperties
|
||||
assertThat(TsfSpringContextAware.getProperties("key1")).isEqualTo("value1");
|
||||
assertThat(TsfSpringContextAware.getProperties("key2")).isNull();
|
||||
|
||||
// test getProperties with default value
|
||||
assertThat(TsfSpringContextAware.getProperties("key1", "defaultValue")).isEqualTo("value1");
|
||||
assertThat(TsfSpringContextAware.getProperties("key2", "defaultValue")).isEqualTo("defaultValue");
|
||||
|
||||
// test getBean
|
||||
assertThat(TsfSpringContextAware.getBean(ApplicationContextAwareUtils.class)).isNotNull();
|
||||
assertThatThrownBy(() -> {
|
||||
TsfSpringContextAware.getBean(PolarisAsyncConfiguration.class);
|
||||
}).isInstanceOf(NoSuchBeanDefinitionException.class);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in new issue