Skip to content

Commit 00412b6

Browse files
committed
Move validation logic to getRequiredEnvironmentVar.
1 parent 3a51528 commit 00412b6

2 files changed

Lines changed: 21 additions & 24 deletions

File tree

powertools-lambda-metadata/src/main/java/software/amazon/lambda/powertools/metadata/internal/LambdaMetadataHttpClient.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,8 @@ public LambdaMetadataHttpClient() {
6161
* @throws LambdaMetadataException if the fetch fails
6262
*/
6363
public LambdaMetadata fetchMetadata() {
64-
String token = getEnvironmentVariable(ENV_METADATA_TOKEN);
65-
String api = getEnvironmentVariable(ENV_METADATA_API);
66-
67-
if (token == null || token.isEmpty()) {
68-
throw new LambdaMetadataException(
69-
"Lambda metadata token not available. Ensure " + ENV_METADATA_TOKEN + " is set.");
70-
}
71-
72-
if (api == null || api.isEmpty()) {
73-
throw new LambdaMetadataException(
74-
"Lambda metadata API endpoint not available. Ensure " + ENV_METADATA_API + " is set.");
75-
}
64+
String token = getRequiredEnvironmentVariable(ENV_METADATA_TOKEN);
65+
String api = getRequiredEnvironmentVariable(ENV_METADATA_API);
7666

7767
String urlString = "http://" + api + "/" + API_VERSION + METADATA_PATH;
7868
LOG.debug("Fetching Lambda metadata from: {}", urlString);
@@ -111,14 +101,21 @@ public LambdaMetadata fetchMetadata() {
111101
}
112102

113103
/**
114-
* Gets an environment variable value.
104+
* Gets a required environment variable value.
105+
* Throws {@link LambdaMetadataException} if the value is null or empty.
115106
* This method is package-private to allow overriding in tests.
116107
*
117108
* @param name the environment variable name
118-
* @return the value, or null if not set
109+
* @return the value, never null or empty
110+
* @throws LambdaMetadataException if the environment variable is not set or empty
119111
*/
120-
String getEnvironmentVariable(String name) {
121-
return System.getenv(name);
112+
String getRequiredEnvironmentVariable(String name) {
113+
String value = System.getenv(name);
114+
if (value == null || value.isEmpty()) {
115+
throw new LambdaMetadataException(
116+
"Environment variable " + name + " is not set. Ensure " + name + " is set.");
117+
}
118+
return value;
122119
}
123120

124121
/**

powertools-lambda-metadata/src/test/java/software/amazon/lambda/powertools/metadata/internal/LambdaMetadataHttpClientTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ void fetchMetadata_shouldThrowOnMissingToken() {
101101
// Given
102102
LambdaMetadataHttpClient client = new LambdaMetadataHttpClient() {
103103
@Override
104-
String getEnvironmentVariable(String name) {
105-
if (LambdaMetadataHttpClient.ENV_METADATA_TOKEN.equals(name)) {
106-
return null;
104+
String getRequiredEnvironmentVariable(String name) {
105+
if (LambdaMetadataHttpClient.ENV_METADATA_API.equals(name)) {
106+
return "localhost:8080";
107107
}
108-
return "localhost:8080";
108+
return super.getRequiredEnvironmentVariable(name);
109109
}
110110
};
111111

@@ -120,11 +120,11 @@ void fetchMetadata_shouldThrowOnMissingApi() {
120120
// Given
121121
LambdaMetadataHttpClient client = new LambdaMetadataHttpClient() {
122122
@Override
123-
String getEnvironmentVariable(String name) {
123+
String getRequiredEnvironmentVariable(String name) {
124124
if (LambdaMetadataHttpClient.ENV_METADATA_TOKEN.equals(name)) {
125125
return TEST_TOKEN;
126126
}
127-
return null;
127+
return super.getRequiredEnvironmentVariable(name);
128128
}
129129
};
130130

@@ -156,14 +156,14 @@ void fetchMetadata_shouldThrowOn404(WireMockRuntimeInfo wmRuntimeInfo) {
156156
private LambdaMetadataHttpClient createClient(WireMockRuntimeInfo wmRuntimeInfo) {
157157
return new LambdaMetadataHttpClient() {
158158
@Override
159-
String getEnvironmentVariable(String name) {
159+
String getRequiredEnvironmentVariable(String name) {
160160
if (LambdaMetadataHttpClient.ENV_METADATA_TOKEN.equals(name)) {
161161
return TEST_TOKEN;
162162
}
163163
if (LambdaMetadataHttpClient.ENV_METADATA_API.equals(name)) {
164164
return "localhost:" + wmRuntimeInfo.getHttpPort();
165165
}
166-
return null;
166+
return super.getRequiredEnvironmentVariable(name);
167167
}
168168
};
169169
}

0 commit comments

Comments
 (0)