Skip to content

Commit 88ee6e3

Browse files
authored
Code smell cleanup (#353)
* Code smell cleanup * attempt to correct code coverage reporting for Sonar * moved static code inside constructor for easier unit testing
1 parent 3b320a3 commit 88ee6e3

File tree

6 files changed

+76
-47
lines changed

6 files changed

+76
-47
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/AruUtil.java

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,31 @@ public class AruUtil {
3737

3838
private static final String BUG_SEARCH_URL = ARU_REST_URL + "/search?bug=%s";
3939

40-
private static int REST_RETRIES = 10;
41-
private static int REST_INTERVAL = 500;
40+
private int restRetries = 10;
41+
private int restInterval = 500;
4242

43-
static {
44-
// static block to parst environment variable overrides for REST call defaults
43+
/**
44+
* Get ARU HTTP helper instance.
45+
* @return ARU helper.
46+
*/
47+
public static AruUtil rest() {
48+
if (instance == null) {
49+
instance = new AruUtil();
50+
}
51+
return instance;
52+
}
53+
54+
protected AruUtil() {
4555
final String retriesEnvVar = "WLSIMG_REST_RETRY_MAX";
4656
final String retriesString = System.getenv(retriesEnvVar);
4757
try {
4858
if (retriesString != null) {
49-
REST_RETRIES = Integer.parseInt(retriesString);
50-
if (REST_RETRIES < 1) {
51-
REST_RETRIES = 10;
52-
logger.severe("IMG-0109", retriesEnvVar, retriesString, 1, REST_RETRIES);
59+
restRetries = Integer.parseInt(retriesString);
60+
if (restRetries < 1) {
61+
restRetries = 10;
62+
logger.severe("IMG-0109", retriesEnvVar, retriesString, 1, restRetries);
5363
}
54-
logger.fine("Retry max set to {0}", REST_RETRIES);
64+
logger.fine("Retry max set to {0}", restRetries);
5565
}
5666
} catch (NumberFormatException nfe) {
5767
logger.warning("IMG-0108", retriesEnvVar, retriesString);
@@ -61,33 +71,18 @@ public class AruUtil {
6171
final String waitString = System.getenv(waitEnvVar);
6272
try {
6373
if (waitString != null) {
64-
REST_INTERVAL = Integer.parseInt(waitString);
65-
if (REST_INTERVAL < 0) {
66-
REST_INTERVAL = 500;
67-
logger.severe("IMG-0109", waitEnvVar, waitString, 0, REST_INTERVAL);
74+
restInterval = Integer.parseInt(waitString);
75+
if (restInterval < 0) {
76+
restInterval = 500;
77+
logger.severe("IMG-0109", waitEnvVar, waitString, 0, restInterval);
6878
}
69-
logger.fine("Retry interval set to {0}", REST_INTERVAL);
79+
logger.fine("Retry interval set to {0}", restInterval);
7080
}
7181
} catch (NumberFormatException nfe) {
7282
logger.warning("IMG-0108", waitEnvVar, waitString);
7383
}
7484
}
7585

76-
/**
77-
* Get ARU HTTP helper instance.
78-
* @return ARU helper.
79-
*/
80-
public static AruUtil rest() {
81-
if (instance == null) {
82-
instance = new AruUtil();
83-
}
84-
return instance;
85-
}
86-
87-
protected AruUtil() {
88-
// hide constructor
89-
}
90-
9186
/**
9287
* Get list of PSU available for each of the ARU products for the given FMW install type.
9388
*
@@ -482,24 +477,44 @@ public String downloadAruPatch(AruPatch aruPatch, String targetDir, String usern
482477
return filename;
483478
}
484479

480+
/**
481+
* The maximum number of retries that will be attempted when trying to reach the ARU REST API method.
482+
* This value can be set by using the environment variable WLSIMG_REST_RETRY_MAX.
483+
*
484+
* @return The maximum number of retries to attempt.
485+
*/
486+
public int getMaxRetries() {
487+
return restRetries;
488+
}
489+
490+
/**
491+
* The time between each ARU REST retry.
492+
* This value can be set by using the environment variable WLSIMG_REST_RETRY_INTERVAL.
493+
*
494+
* @return The time to wait between each ARU REST API attempt during the retry loop.
495+
*/
496+
public int getRetryInterval() {
497+
return restInterval;
498+
}
499+
485500
private interface CallToRetry {
486501
Document process() throws IOException, XPathExpressionException, AruException;
487502
}
488503

489504
// create an environment variable that can override the tries count (undocumented)
490505
private static Document retry(CallToRetry call) throws AruException, RetryFailedException {
491-
for (int i = 0; i < REST_RETRIES; i++) {
506+
for (int i = 0; i < rest().getMaxRetries(); i++) {
492507
try {
493508
return call.process();
494509
} catch (UnknownHostException e) {
495510
throw new AruException(e.getLocalizedMessage(), e);
496511
} catch (IOException | XPathExpressionException e) {
497-
logger.info("IMG-0106", e.getMessage(), (i + 1), REST_RETRIES);
512+
logger.info("IMG-0106", e.getMessage(), (i + 1), rest().getMaxRetries());
498513
}
499514
try {
500-
if (REST_INTERVAL > 0) {
501-
logger.finer("Waiting {0} ms before retry...", REST_INTERVAL);
502-
Thread.sleep(REST_INTERVAL);
515+
if (rest().getRetryInterval() > 0) {
516+
logger.finer("Waiting {0} ms before retry...", rest().getRetryInterval());
517+
Thread.sleep(rest().getRetryInterval());
503518
}
504519
} catch (InterruptedException wakeAndAbort) {
505520
logger.warning("Process interrupted!");

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/InstalledPatch.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,15 @@ public static List<InstalledPatch> getPatchList(String oraclePatches) {
3232
return result;
3333
}
3434
String[] tokens = oraclePatches.split(";");
35+
// Each patch record is made up of 3 fields.
3536
if (tokens.length % 3 != 0) {
3637
logger.severe("IMG-0095", tokens.length);
3738
}
38-
for (int i = 0; i < tokens.length; i++) {
39+
for (int j = 0; j < tokens.length; j = j + 3) {
3940
InstalledPatch found = new InstalledPatch();
40-
found.bugNumber = tokens[i];
41-
if (tokens.length > i + 1) {
42-
found.uniquePatchNumber = tokens[++i];
43-
}
44-
if (tokens.length > i + 1) {
45-
found.patchDescription = tokens[++i].replaceAll("^\"|\"$", "");
46-
}
41+
found.bugNumber = tokens[j];
42+
found.uniquePatchNumber = tokens[j + 1];
43+
found.patchDescription = tokens[j + 2].replaceAll("(^\")|(\"$)", "");
4744
result.add(found);
4845
}
4946

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CommonOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ void initializeOptions() throws InvalidCredentialException, IOException, Invalid
167167
handleChown();
168168
handleAdditionalBuildCommands();
169169

170-
if (kubernetesTarget == KubernetesTarget.OpenShift) {
170+
if (kubernetesTarget == KubernetesTarget.OPENSHIFT) {
171171
dockerfileOptions.setDomainGroupAsUser(true);
172172
// if the user did not set the OS user:group, make the default oracle:root, instead of oracle:oracle
173173
if (!isOptionSet(osUserAndGroup)) {
@@ -373,7 +373,7 @@ public String buildId() {
373373
description = "Apply settings appropriate to the target environment. Default: ${DEFAULT-VALUE}."
374374
+ " Supported values: ${COMPLETION-CANDIDATES}."
375375
)
376-
KubernetesTarget kubernetesTarget = KubernetesTarget.Default;
376+
KubernetesTarget kubernetesTarget = KubernetesTarget.DEFAULT;
377377

378378
@SuppressWarnings("unused")
379379
@Unmatched

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/KubernetesTarget.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
package com.oracle.weblogic.imagetool.cli.menu;
55

66
public enum KubernetesTarget {
7-
Default,
8-
OpenShift
7+
DEFAULT("Default"),
8+
OPENSHIFT("OpenShift");
9+
10+
private String value;
11+
12+
/**
13+
* Create an Enum value for the installer type.
14+
* @param value value is the first part of the key for the installer in the cache, and the filename of the
15+
* response file
16+
*/
17+
KubernetesTarget(String value) {
18+
this.value = value;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return value;
24+
}
925
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<properties>
3434
<sonar.organization>oracle</sonar.organization>
3535
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
36+
<sonar.coverage.jacoco.xmlReportPaths>tests/target/site/jacoco-aggregate/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
3637

3738
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3839
<maven.compiler.source>1.8</maven.compiler.source>

tests/src/test/java/com/oracle/weblogic/imagetool/tests/ITImagetool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ void createWlsImgWithOpenShiftSettings(TestInfo testInfo) throws Exception {
11191119
.wdtDomainHome("/u01/domains/simple_domain")
11201120
.wdtModel(WDT_MODEL, WDT_MODEL2)
11211121
.wdtVariables(WDT_VARIABLES)
1122-
.target(KubernetesTarget.OpenShift)
1122+
.target(KubernetesTarget.OPENSHIFT)
11231123
.build();
11241124

11251125
try (PrintWriter out = getTestMethodWriter(testInfo)) {

0 commit comments

Comments
 (0)