Skip to content

Commit 7f3836c

Browse files
committed
refactor: avoid mocking system.getenv
Signed-off-by: Ruben Romero Montes <rromerom@redhat.com>
1 parent e24ab22 commit 7f3836c

19 files changed

+364
-448
lines changed

src/main/java/com/redhat/exhort/image/ImageUtils.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.redhat.exhort.image;
1717

1818
import static com.redhat.exhort.image.Platform.EMPTY_PLATFORM;
19-
import static com.redhat.exhort.impl.ExhortApi.getStringValueEnvironment;
2019

2120
import com.fasterxml.jackson.core.JsonProcessingException;
2221
import com.fasterxml.jackson.databind.JsonNode;
@@ -25,10 +24,10 @@
2524
import com.fasterxml.jackson.databind.node.TextNode;
2625
import com.github.packageurl.MalformedPackageURLException;
2726
import com.redhat.exhort.tools.Operations;
27+
import com.redhat.exhort.utils.Environment;
2828
import java.io.File;
2929
import java.io.IOException;
3030
import java.util.AbstractMap;
31-
import java.util.ArrayList;
3231
import java.util.Collections;
3332
import java.util.EnumSet;
3433
import java.util.List;
@@ -91,7 +90,7 @@ public class ImageUtils {
9190
new AbstractMap.SimpleEntry<>("aarch64", "v8"));
9291

9392
static String updatePATHEnv(String execPath) {
94-
String path = System.getenv("PATH");
93+
String path = Environment.get("PATH");
9594
if (path != null) {
9695
return String.format("PATH=%s%s%s", path, File.pathSeparator, execPath);
9796
} else {
@@ -129,8 +128,8 @@ static Operations.ProcessExecOutput execSyft(ImageRef imageRef) {
129128
var docker = Operations.getCustomPathOrElse("docker");
130129
var podman = Operations.getCustomPathOrElse("podman");
131130

132-
var syftConfigPath = getStringValueEnvironment(EXHORT_SYFT_CONFIG_PATH, "");
133-
var imageSource = getStringValueEnvironment(EXHORT_SYFT_IMAGE_SOURCE, "");
131+
var syftConfigPath = Environment.get(EXHORT_SYFT_CONFIG_PATH, "");
132+
var imageSource = Environment.get(EXHORT_SYFT_IMAGE_SOURCE, "");
134133
SyftImageSource.getImageSource(imageSource);
135134

136135
var dockerPath =
@@ -195,29 +194,23 @@ static List<String> getSyftEnvs(String dockerPath, String podmanPath) {
195194
} else if (!podmanPath.isEmpty()) {
196195
path = podmanPath;
197196
}
198-
var envPath = path != null ? updatePATHEnv(path) : null;
199-
200-
List<String> envs = new ArrayList<>(1);
201-
if (envPath != null) {
202-
envs.add(envPath);
203-
}
204-
return envs;
197+
return path != null ? List.of(updatePATHEnv(path)) : Collections.emptyList();
205198
}
206199

207200
public static Platform getImagePlatform() {
208-
var platform = getStringValueEnvironment(EXHORT_IMAGE_PLATFORM, "");
201+
var platform = Environment.get(EXHORT_IMAGE_PLATFORM, "");
209202
if (!platform.isEmpty()) {
210203
return new Platform(platform);
211204
}
212205

213-
var imageSource = getStringValueEnvironment(EXHORT_SYFT_IMAGE_SOURCE, "");
206+
var imageSource = Environment.get(EXHORT_SYFT_IMAGE_SOURCE, "");
214207
SyftImageSource source = SyftImageSource.getImageSource(imageSource);
215208

216-
var os = getStringValueEnvironment(EXHORT_IMAGE_OS, "");
209+
var os = Environment.get(EXHORT_IMAGE_OS, "");
217210
if (os.isEmpty()) {
218211
os = source.getOs();
219212
}
220-
var arch = getStringValueEnvironment(EXHORT_IMAGE_ARCH, "");
213+
var arch = Environment.get(EXHORT_IMAGE_ARCH, "");
221214
if (arch.isEmpty()) {
222215
arch = source.getArch();
223216
}
@@ -226,7 +219,7 @@ public static Platform getImagePlatform() {
226219
return new Platform(os, arch, null);
227220
}
228221

229-
var variant = getStringValueEnvironment(EXHORT_IMAGE_VARIANT, "");
222+
var variant = Environment.get(EXHORT_IMAGE_VARIANT, "");
230223
if (variant.isEmpty()) {
231224
variant = source.getVariant();
232225
}
@@ -425,8 +418,8 @@ static Map<Platform, String> getSingleImageDigest(ImageRef imageRef)
425418
static Operations.ProcessExecOutput execSkopeoInspect(ImageRef imageRef, boolean raw) {
426419
var skopeo = Operations.getCustomPathOrElse("skopeo");
427420

428-
var configPath = getStringValueEnvironment(EXHORT_SKOPEO_CONFIG_PATH, "");
429-
var daemonHost = getStringValueEnvironment(EXHORT_IMAGE_SERVICE_ENDPOINT, "");
421+
var configPath = Environment.get(EXHORT_SKOPEO_CONFIG_PATH, "");
422+
var daemonHost = Environment.get(EXHORT_IMAGE_SERVICE_ENDPOINT, "");
430423

431424
String[] cmd;
432425
if (daemonHost.isEmpty()) {

src/main/java/com/redhat/exhort/impl/ExhortApi.java

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.redhat.exhort.image.ImageUtils;
2929
import com.redhat.exhort.logging.LoggersFactory;
3030
import com.redhat.exhort.tools.Ecosystem;
31+
import com.redhat.exhort.utils.Environment;
3132
import jakarta.mail.MessagingException;
3233
import jakarta.mail.internet.MimeMultipart;
3334
import jakarta.mail.util.ByteArrayDataSource;
@@ -61,6 +62,12 @@
6162
/** Concrete implementation of the Exhort {@link Api} Service. */
6263
public final class ExhortApi implements Api {
6364

65+
private static final String DEV_EXHORT_BACKEND_URL = "DEV_EXHORT_BACKEND_URL";
66+
67+
private static final String EXHORT_DEV_MODE = "EXHORT_DEV_MODE";
68+
69+
private static final String HTTP_VERSION_EXHORT_CLIENT = "HTTP_VERSION_EXHORT_CLIENT";
70+
6471
private static final Logger LOG = LoggersFactory.getLogger(ExhortApi.class.getName());
6572

6673
public static final String DEFAULT_ENDPOINT = "https://rhda.rhcloud.com";
@@ -126,8 +133,8 @@ public ExhortApi() {
126133
* @return i.e. HttpClient.Version.HTTP_1.1
127134
*/
128135
static HttpClient.Version getHttpVersion() {
129-
return (System.getenv("HTTP_VERSION_EXHORT_CLIENT") != null
130-
&& System.getenv("HTTP_VERSION_EXHORT_CLIENT").contains("2"))
136+
var version = Environment.get(HTTP_VERSION_EXHORT_CLIENT);
137+
return (version != null && version.contains("2"))
131138
? HttpClient.Version.HTTP_2
132139
: HttpClient.Version.HTTP_1_1;
133140
}
@@ -141,27 +148,27 @@ static HttpClient.Version getHttpVersion() {
141148
this.client = client;
142149
this.mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
143150
// Take default from config.properties in case client didn't override DEV MODE
144-
if (System.getProperty("EXHORT_DEV_MODE") == null) {
151+
if (Environment.get(EXHORT_DEV_MODE) == null) {
145152
try {
146153
InputStream exhortConfig =
147154
this.getClass().getClassLoader().getResourceAsStream("config.properties");
148155
if (exhortConfig == null) {
149156
LOG.info(
150157
"config.properties not found on the class path, fallback to default DEV MODE ="
151158
+ " false");
152-
System.setProperty("EXHORT_DEV_MODE", "false");
159+
System.setProperty(EXHORT_DEV_MODE, "false");
153160
} else {
154161
Properties properties = new Properties();
155162
properties.load(exhortConfig);
156-
System.setProperty("EXHORT_DEV_MODE", (String) properties.get("EXHORT_DEV_MODE"));
163+
System.setProperty(EXHORT_DEV_MODE, (String) properties.get(EXHORT_DEV_MODE));
157164
}
158165
} catch (IOException e) {
159166
LOG.info(
160167
String.format(
161168
"Error loading config.properties , fallback to set default property DEV MODE ="
162169
+ " false, Error message = %s",
163170
e.getMessage()));
164-
System.setProperty("EXHORT_DEV_MODE", "false");
171+
System.setProperty(EXHORT_DEV_MODE, "false");
165172
}
166173
}
167174

@@ -198,8 +205,8 @@ private static String getClientRequestId() {
198205

199206
public String getExhortUrl() {
200207
String endpoint;
201-
if (getBooleanValueEnvironment("EXHORT_DEV_MODE", "false")) {
202-
endpoint = getStringValueEnvironment("DEV_EXHORT_BACKEND_URL", DEFAULT_ENDPOINT_DEV);
208+
if (Environment.getBoolean(EXHORT_DEV_MODE, false)) {
209+
endpoint = Environment.get(DEV_EXHORT_BACKEND_URL, DEFAULT_ENDPOINT_DEV);
203210

204211
} else {
205212
endpoint = DEFAULT_ENDPOINT;
@@ -209,29 +216,15 @@ public String getExhortUrl() {
209216
String.format(
210217
"EXHORT_DEV_MODE=%s,DEV_EXHORT_BACKEND_URL=%s, Chosen Backend URL=%s ,"
211218
+ " DEFAULT_ENDPOINT_DEV=%s , DEFAULT_ENDPOINT=%s",
212-
getBooleanValueEnvironment("EXHORT_DEV_MODE", "false"),
213-
getStringValueEnvironment("DEV_EXHORT_BACKEND_URL", DEFAULT_ENDPOINT_DEV),
219+
Environment.getBoolean(EXHORT_DEV_MODE, false),
220+
Environment.get(DEV_EXHORT_BACKEND_URL, DEFAULT_ENDPOINT_DEV),
214221
endpoint,
215222
DEFAULT_ENDPOINT_DEV,
216223
DEFAULT_ENDPOINT));
217224
}
218225
return endpoint;
219226
}
220227

221-
public static boolean getBooleanValueEnvironment(String key, String defaultValue) {
222-
String result =
223-
Objects.requireNonNullElse(
224-
System.getenv(key), Objects.requireNonNullElse(System.getProperty(key), defaultValue));
225-
return Boolean.parseBoolean(result.trim().toLowerCase());
226-
}
227-
228-
public static String getStringValueEnvironment(String key, String defaultValue) {
229-
String result =
230-
Objects.requireNonNullElse(
231-
System.getenv(key), Objects.requireNonNullElse(System.getProperty(key), defaultValue));
232-
return result;
233-
}
234-
235228
@Override
236229
public CompletableFuture<MixedReport> stackAnalysisMixed(final String manifestFile)
237230
throws IOException {
@@ -390,7 +383,7 @@ private static void logExhortRequestId(HttpResponse<?> response) {
390383
}
391384

392385
public static boolean debugLoggingIsNeeded() {
393-
return Boolean.parseBoolean(getStringValueEnvironment("EXHORT_DEBUG", "false"));
386+
return Environment.getBoolean("EXHORT_DEBUG", false);
394387
}
395388

396389
@Override
@@ -639,20 +632,20 @@ private HttpRequest buildRequest(
639632
Stream.of(ExhortApi.TokenProvider.values())
640633
.forEach(
641634
p -> {
642-
var envToken = System.getenv(p.getVarName());
635+
var envToken = Environment.get(p.getVarName());
643636
if (Objects.nonNull(envToken)) {
644637
request.setHeader(p.getHeaderName(), envToken);
645638
} else {
646-
var propToken = System.getProperty(p.getVarName());
639+
var propToken = Environment.get(p.getVarName());
647640
if (Objects.nonNull(propToken)) {
648641
request.setHeader(p.getHeaderName(), propToken);
649642
}
650643
}
651-
var envUser = System.getenv(p.getUserHeaderName());
644+
var envUser = Environment.get(p.getUserHeaderName());
652645
if (Objects.nonNull(envUser)) {
653646
request.setHeader(p.getUserHeaderName(), envUser);
654647
} else {
655-
var propUser = System.getProperty(p.getUserVarName());
648+
var propUser = Environment.get(p.getUserVarName());
656649
if (Objects.nonNull(propUser)) {
657650
request.setHeader(p.getUserHeaderName(), propUser);
658651
}
@@ -685,11 +678,6 @@ private String calculateHeaderValue(String headerName) {
685678
}
686679

687680
private String calculateHeaderValueActual(String headerName) {
688-
String result = null;
689-
result = System.getenv(headerName);
690-
if (result == null) {
691-
result = System.getProperty(headerName);
692-
}
693-
return result;
681+
return Environment.get(headerName);
694682
}
695683
}

src/main/java/com/redhat/exhort/providers/GoModulesProvider.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.redhat.exhort.providers;
1717

1818
import static com.redhat.exhort.impl.ExhortApi.debugLoggingIsNeeded;
19-
import static com.redhat.exhort.impl.ExhortApi.getBooleanValueEnvironment;
2019

2120
import com.fasterxml.jackson.core.JsonProcessingException;
2221
import com.fasterxml.jackson.databind.JsonMappingException;
@@ -29,6 +28,7 @@
2928
import com.redhat.exhort.sbom.SbomFactory;
3029
import com.redhat.exhort.tools.Ecosystem.Type;
3130
import com.redhat.exhort.tools.Operations;
31+
import com.redhat.exhort.utils.Environment;
3232
import com.redhat.exhort.vcs.GitVersionControlSystemImpl;
3333
import com.redhat.exhort.vcs.TagInfo;
3434
import com.redhat.exhort.vcs.VersionControlSystem;
@@ -129,7 +129,7 @@ Sbom getDependenciesSbom(Path manifestPath, boolean buildTree) throws IOExceptio
129129
Sbom sbom;
130130
List<PackageURL> ignoredDeps = getIgnoredDeps(manifestPath);
131131
boolean matchManifestVersions =
132-
getBooleanValueEnvironment(Provider.PROP_MATCH_MANIFEST_VERSIONS, "false");
132+
Environment.getBoolean(Provider.PROP_MATCH_MANIFEST_VERSIONS, false);
133133
if (matchManifestVersions) {
134134
String[] goModGraphLines = goModulesResult.split(System.lineSeparator());
135135
performManifestVersionsCheck(goModGraphLines, manifestPath);
@@ -281,8 +281,7 @@ private Sbom buildSbomFromGraph(
281281
startingIndex += deps.size();
282282
}
283283
}
284-
boolean goMvsLogicEnabled =
285-
getBooleanValueEnvironment(PROP_EXHORT_GO_MVS_LOGIC_ENABLED, "false");
284+
boolean goMvsLogicEnabled = Environment.getBoolean(PROP_EXHORT_GO_MVS_LOGIC_ENABLED, false);
286285
if (goMvsLogicEnabled) {
287286
edges = getFinalPackagesVersionsForModule(edges, manifestPath);
288287
}

src/main/java/com/redhat/exhort/providers/JavaMavenProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import com.github.packageurl.PackageURL;
2222
import com.redhat.exhort.Api;
2323
import com.redhat.exhort.Provider;
24-
import com.redhat.exhort.impl.ExhortApi;
2524
import com.redhat.exhort.logging.LoggersFactory;
2625
import com.redhat.exhort.sbom.Sbom;
2726
import com.redhat.exhort.sbom.SbomFactory;
2827
import com.redhat.exhort.tools.Ecosystem.Type;
2928
import com.redhat.exhort.tools.Operations;
29+
import com.redhat.exhort.utils.Environment;
3030
import java.io.IOException;
3131
import java.nio.file.Files;
3232
import java.nio.file.Path;
@@ -50,6 +50,7 @@
5050
*/
5151
public final class JavaMavenProvider extends BaseJavaProvider {
5252

53+
private static final String PROP_JAVA_HOME = "JAVA_HOME";
5354
private Logger log = LoggersFactory.getLogger(this.getClass().getName());
5455

5556
public JavaMavenProvider(Path manifest) {
@@ -303,9 +304,9 @@ private List<DependencyAggregator> getDependencies(final Path manifestPath) thro
303304
}
304305

305306
Map<String, String> getMvnExecEnvs() {
306-
var javaHome = ExhortApi.getStringValueEnvironment("JAVA_HOME", "");
307+
var javaHome = Environment.get(PROP_JAVA_HOME);
307308
if (javaHome != null && !javaHome.isBlank()) {
308-
return Collections.singletonMap("JAVA_HOME", javaHome);
309+
return Collections.singletonMap(PROP_JAVA_HOME, javaHome);
309310
}
310311
return null;
311312
}

src/main/java/com/redhat/exhort/providers/JavaScriptNpmProvider.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.redhat.exhort.tools.Ecosystem;
3131
import com.redhat.exhort.tools.Ecosystem.Type;
3232
import com.redhat.exhort.tools.Operations;
33+
import com.redhat.exhort.utils.Environment;
3334
import java.io.File;
3435
import java.io.IOException;
3536
import java.nio.charset.StandardCharsets;
@@ -48,6 +49,7 @@
4849
*/
4950
public final class JavaScriptNpmProvider extends Provider {
5051

52+
private static final String PROP_PATH = "PATH";
5153
private System.Logger log = System.getLogger(this.getClass().getName());
5254

5355
public JavaScriptNpmProvider(Path manifest) {
@@ -219,13 +221,13 @@ private List<String> getIgnoredDeps(Path manifestPath) throws IOException {
219221
}
220222

221223
Map<String, String> getNpmExecEnv() {
222-
String nodeHome = System.getProperty("NODE_HOME");
224+
String nodeHome = Environment.get("NODE_HOME");
223225
if (nodeHome != null && !nodeHome.isBlank()) {
224-
String path = System.getenv("PATH");
226+
String path = Environment.get(PROP_PATH);
225227
if (path != null) {
226-
return Collections.singletonMap("PATH", path + File.pathSeparator + nodeHome);
228+
return Collections.singletonMap(PROP_PATH, path + File.pathSeparator + nodeHome);
227229
} else {
228-
return Collections.singletonMap("PATH", nodeHome);
230+
return Collections.singletonMap(PROP_PATH, nodeHome);
229231
}
230232
}
231233
return null;

0 commit comments

Comments
 (0)