From 631e2f1d45ca2647705a86414725d8c01c04a20b Mon Sep 17 00:00:00 2001 From: "lingxiao,wu" <51630311+lingxiao-wu@users.noreply.github.com> Date: Wed, 9 Nov 2022 09:44:45 +0800 Subject: [PATCH] Test:support environment variable metadata test (#710) --- CHANGELOG.md | 1 + spring-cloud-tencent-commons/pom.xml | 6 ++++ .../metadata/StaticMetadataManager.java | 4 ++- .../metadata/StaticMetadataManagerTest.java | 34 +++++++++++++++++++ spring-cloud-tencent-dependencies/pom.xml | 8 +++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96b430bcb..6f6acd8c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,3 +6,4 @@ - [Optimize:optimize spring annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/688) - [docs:update PR template.](https://github.com/Tencent/spring-cloud-tencent/pull/691) - [Code optimize & add junit tests.](https://github.com/Tencent/spring-cloud-tencent/pull/703) +- [Test:support environment variable metadata test](https://github.com/Tencent/spring-cloud-tencent/pull/710) diff --git a/spring-cloud-tencent-commons/pom.xml b/spring-cloud-tencent-commons/pom.xml index 6cf993ed3..84a6d7975 100644 --- a/spring-cloud-tencent-commons/pom.xml +++ b/spring-cloud-tencent-commons/pom.xml @@ -99,6 +99,12 @@ spring-boot-actuator-autoconfigure true + + + uk.org.webcompere + system-stubs-junit4 + test + diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java index f74fd6582..63ea080fb 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/metadata/StaticMetadataManager.java @@ -107,7 +107,8 @@ public class StaticMetadataManager { String value = entry.getValue(); if (StringUtils.isNotBlank(key) && (key.startsWith(ENV_METADATA_PREFIX) || key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS)) - && !key.equals(ENV_METADATA_CONTENT_TRANSITIVE)) { + && !key.equals(ENV_METADATA_CONTENT_TRANSITIVE) + && !key.equals(ENV_METADATA_CONTENT_DISPOSABLE)) { String sourceKey = ""; if (key.equals(ENV_TRAFFIC_CONTENT_RAW_TRANSHEADERS)) { sourceKey = key; @@ -229,6 +230,7 @@ public class StaticMetadataManager { mergedMetadataResult.putAll(configMetadata); mergedMetadataResult.putAll(envMetadata); mergedMetadataResult.putAll(customSPIMetadata); + this.mergedStaticMetadata = Collections.unmodifiableMap(mergedMetadataResult); Map mergedTransitiveMetadataResult = new HashMap<>(); diff --git a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java index 64aa20bcb..01dd10428 100644 --- a/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java +++ b/spring-cloud-tencent-commons/src/test/java/com/tencent/cloud/common/metadata/StaticMetadataManagerTest.java @@ -27,10 +27,12 @@ import java.util.Set; import com.tencent.cloud.common.metadata.config.MetadataLocalProperties; import com.tencent.cloud.common.spi.InstanceMetadataProvider; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule; import org.springframework.util.CollectionUtils; @@ -48,6 +50,12 @@ public class StaticMetadataManagerTest { @Mock private MetadataLocalProperties metadataLocalProperties; + /** + * EnvironmentVariablesRule. + */ + @Rule + public EnvironmentVariablesRule rule = new EnvironmentVariablesRule(); + @Test public void testParseConfigMetadata() { Map content = new HashMap<>(); @@ -147,6 +155,32 @@ public class StaticMetadataManagerTest { } + @Test + public void testEnvMetadata() { + // set env + rule.set("SCT_METADATA_CONTENT_TRANSITIVE", "transitiveKey"); + rule.set("SCT_METADATA_CONTENT_DISPOSABLE", "disposableKey"); + rule.set("SCT_METADATA_CONTENT_transitiveKey", "transitiveValue"); + rule.set("SCT_METADATA_CONTENT_disposableKey", "disposableValue"); + rule.set("SCT_TRAFFIC_CONTENT_RAW_TRANSHEADERS", "header1,header2,header3"); + + StaticMetadataManager metadataManager = new StaticMetadataManager(metadataLocalProperties, null); + Map allEnvMetadata = metadataManager.getAllEnvMetadata(); + Assert.assertTrue(allEnvMetadata.containsKey("transitiveKey")); + Assert.assertTrue(allEnvMetadata.containsKey("disposableKey")); + + Map envDisposableMetadata = metadataManager.getEnvDisposableMetadata(); + Assert.assertTrue(envDisposableMetadata.containsKey("disposableKey")); + Assert.assertEquals(envDisposableMetadata.get("disposableKey"), "disposableValue"); + + Map envTransitiveMetadata = metadataManager.getEnvTransitiveMetadata(); + Assert.assertTrue(envTransitiveMetadata.containsKey("transitiveKey")); + Assert.assertEquals(envTransitiveMetadata.get("transitiveKey"), "transitiveValue"); + + String transHeaderFromEnv = metadataManager.getTransHeaderFromEnv(); + Assert.assertEquals(transHeaderFromEnv, "header1,header2,header3"); + } + static class MockedMetadataProvider implements InstanceMetadataProvider { @Override diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index e38aa3ec5..df2af662a 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -81,6 +81,7 @@ 3.16.1 1.69 31.0.1-jre + 2.0.1 3.2.0 @@ -267,6 +268,13 @@ ${mocktio.version} test + + + uk.org.webcompere + system-stubs-junit4 + ${system-stubs-junit4.version} + test +