Skip to content

Commit 33908c8

Browse files
authored
another attempt to workaround bug in coverage reporting for Sonar scanner (#354)
* added additional unit tests to improve coverage * removed xmlReportPath from POMs and Jenkinsfile
1 parent 88ee6e3 commit 33908c8

File tree

6 files changed

+246
-77
lines changed

6 files changed

+246
-77
lines changed

imagetool/pom.xml

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
<relativePath>../pom.xml</relativePath>
1818
</parent>
1919

20-
<properties>
21-
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
22-
</properties>
23-
2420
<dependencies>
2521
<dependency>
2622
<groupId>org.apache.httpcomponents</groupId>
@@ -74,18 +70,6 @@
7470
</resource>
7571
</resources>
7672
<plugins>
77-
<plugin>
78-
<groupId>org.jacoco</groupId>
79-
<artifactId>jacoco-maven-plugin</artifactId>
80-
<executions>
81-
<execution>
82-
<id>prepare-agent</id>
83-
<goals>
84-
<goal>prepare-agent</goal>
85-
</goals>
86-
</execution>
87-
</executions>
88-
</plugin>
8973
<plugin>
9074
<groupId>org.apache.maven.plugins</groupId>
9175
<artifactId>maven-jar-plugin</artifactId>
@@ -119,6 +103,10 @@
119103
<groupId>org.apache.maven.plugins</groupId>
120104
<artifactId>maven-checkstyle-plugin</artifactId>
121105
</plugin>
106+
<plugin>
107+
<groupId>org.jacoco</groupId>
108+
<artifactId>jacoco-maven-plugin</artifactId>
109+
</plugin>
122110
</plugins>
123111
</build>
124112
</project>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private String getReleaseNumber(AruProduct product, String version, String userI
374374
* @param password password
375375
* @return true if credentials are valid
376376
*/
377-
public static boolean checkCredentials(String username, String password) {
377+
public boolean checkCredentials(String username, String password) {
378378
if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
379379
return false;
380380
}

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

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void initializeOptions() throws IOException, InvalidCredentialException, Invalid
4545
password = Utils.getPasswordFromInputs(passwordStr, passwordFile, passwordEnv);
4646

4747
// if userid or password is provided, validate the pair of provided values
48-
if ((userId != null || password != null) && !AruUtil.checkCredentials(userId, password)) {
48+
if ((userId != null || password != null) && !AruUtil.rest().checkCredentials(userId, password)) {
4949
throw new InvalidCredentialException();
5050
}
5151

@@ -107,39 +107,7 @@ void handlePatchFiles(FmwInstallerType installerType, List<InstalledPatch> insta
107107
return;
108108
}
109109

110-
List<AruPatch> aruPatches = new ArrayList<>();
111-
if (recommendedPatches || latestPsu) {
112-
if (userId == null || password == null) {
113-
throw new IllegalArgumentException(Utils.getMessage("IMG-0031"));
114-
}
115-
116-
if (recommendedPatches) {
117-
// Get the latest PSU and its recommended patches
118-
aruPatches = AruUtil.rest()
119-
.getRecommendedPatches(installerType, getInstallerVersion(), userId, password);
120-
121-
if (aruPatches.isEmpty()) {
122-
recommendedPatches = false;
123-
logger.info("IMG-0084", getInstallerVersion());
124-
} else if (FmwInstallerType.isBaseWeblogicServer(installerType)) {
125-
// find and remove all ADR patches in the recommended patches list for base WLS installers
126-
List<AruPatch> discard = aruPatches.stream()
127-
.filter(p -> p.description().startsWith("ADR FOR WEBLOGIC SERVER"))
128-
.collect(Collectors.toList());
129-
// let the user know that the ADR patches will be discarded
130-
discard.forEach(p -> logger.info("IMG-0085", p.patchId()));
131-
aruPatches.removeAll(discard);
132-
}
133-
} else {
134-
// PSUs for WLS and JRF installers are considered WLS patches
135-
aruPatches = AruUtil.rest().getLatestPsu(installerType, getInstallerVersion(), userId, password);
136-
137-
if (aruPatches.isEmpty()) {
138-
latestPsu = false;
139-
logger.fine("Latest PSU NOT FOUND, ignoring latestPSU flag");
140-
}
141-
}
142-
}
110+
List<AruPatch> aruPatches = getRecommendedPatchList(installerType);
143111

144112
if (!aruPatches.isEmpty()) {
145113
// when applying a new PSU, use that PSU version to find patches where user did not qualify the patch number
@@ -216,6 +184,52 @@ void handlePatchFiles(FmwInstallerType installerType, List<InstalledPatch> insta
216184
logger.exiting();
217185
}
218186

187+
/**
188+
* Get all the latest PSU patches for a given installer type (WLS, SOA, etc.) if the user
189+
* requested them with --latestPSU. --recommendedPatches takes precedence over --latestPSU, and
190+
* returns all the recommended patches including the PSUs.
191+
*
192+
* @return recommended patch list or empty list if neither latestPSU nor recommendedPatches was requested
193+
* by the user.
194+
*/
195+
List<AruPatch> getRecommendedPatchList(FmwInstallerType installerType) throws AruException {
196+
List<AruPatch> aruPatches = new ArrayList<>();
197+
if (recommendedPatches || latestPsu) {
198+
if (userId == null || password == null) {
199+
throw new IllegalArgumentException(Utils.getMessage("IMG-0031"));
200+
}
201+
202+
if (recommendedPatches) {
203+
// Get the latest PSU and its recommended patches
204+
aruPatches = AruUtil.rest()
205+
.getRecommendedPatches(installerType, getInstallerVersion(), userId, password);
206+
207+
if (aruPatches.isEmpty()) {
208+
recommendedPatches = false;
209+
logger.info("IMG-0084", getInstallerVersion());
210+
} else if (FmwInstallerType.isBaseWeblogicServer(installerType)) {
211+
// find and remove all ADR patches in the recommended patches list for base WLS installers
212+
List<AruPatch> discard = aruPatches.stream()
213+
.filter(p -> p.description().startsWith("ADR FOR WEBLOGIC SERVER"))
214+
.collect(Collectors.toList());
215+
// let the user know that the ADR patches will be discarded
216+
discard.forEach(p -> logger.info("IMG-0085", p.patchId()));
217+
aruPatches.removeAll(discard);
218+
}
219+
} else {
220+
// PSUs for WLS and JRF installers are considered WLS patches
221+
aruPatches = AruUtil.rest().getLatestPsu(installerType, getInstallerVersion(), userId, password);
222+
223+
if (aruPatches.isEmpty()) {
224+
latestPsu = false;
225+
logger.fine("Latest PSU NOT FOUND, ignoring latestPSU flag");
226+
}
227+
}
228+
}
229+
230+
return aruPatches;
231+
}
232+
219233
private Path createPatchesTempDirectory() throws IOException {
220234
Path tmpPatchesDir = Files.createDirectory(Paths.get(buildDir(), "patches"));
221235
Files.createFile(Paths.get(tmpPatchesDir.toAbsolutePath().toString(), "dummy.txt"));
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// Copyright (c) 2022, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.cli.menu;
5+
6+
import java.lang.reflect.Field;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.logging.Level;
10+
11+
import com.oracle.weblogic.imagetool.aru.AruException;
12+
import com.oracle.weblogic.imagetool.aru.AruPatch;
13+
import com.oracle.weblogic.imagetool.aru.AruUtil;
14+
import com.oracle.weblogic.imagetool.aru.InvalidCredentialException;
15+
import com.oracle.weblogic.imagetool.installer.FmwInstallerType;
16+
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
17+
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
18+
import com.oracle.weblogic.imagetool.util.InvalidPatchIdFormatException;
19+
import org.junit.jupiter.api.AfterAll;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Tag;
22+
import org.junit.jupiter.api.Test;
23+
import picocli.CommandLine;
24+
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
30+
@Tag("unit")
31+
class CommonPatchingOptionsTest {
32+
private static final LoggingFacade logger = LoggingFactory.getLogger(AruUtil.class);
33+
private static Level oldLevel;
34+
35+
@BeforeAll
36+
static void setUp() {
37+
oldLevel = logger.getLevel();
38+
logger.setLevel(Level.SEVERE);
39+
}
40+
41+
public static class TestAruUtil extends AruUtil {
42+
43+
public TestAruUtil() {
44+
}
45+
46+
@Override
47+
public boolean checkCredentials(String username, String password) {
48+
return true;
49+
}
50+
51+
@Override
52+
public List<AruPatch> getRecommendedPatches(FmwInstallerType type, String version,
53+
String userId, String password) throws AruException {
54+
List<AruPatch> list = new ArrayList<>();
55+
list.add(new AruPatch().patchId("1").description("psu").psuBundle("x"));
56+
list.add(new AruPatch().patchId("2").description("blah"));
57+
list.add(new AruPatch().patchId("3").description("ADR FOR WEBLOGIC SERVER"));
58+
return list;
59+
}
60+
}
61+
62+
@AfterAll
63+
static void tearDown() {
64+
logger.setLevel(oldLevel);
65+
}
66+
67+
@Test
68+
void noPassword() {
69+
CreateImage createImage = new CreateImage();
70+
new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek");
71+
assertThrows(InvalidCredentialException.class, createImage::initializeOptions);
72+
}
73+
74+
@Test
75+
void withPassword() throws Exception {
76+
CreateImage createImage = new CreateImage();
77+
new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx");
78+
79+
// insert test class into AruUtil to intercept REST calls to ARU
80+
Field aruRest = AruUtil.class.getDeclaredField("instance");
81+
aruRest.setAccessible(true);
82+
aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil());
83+
84+
assertFalse(createImage.applyingPatches(), "No patches on the command line, but applyingPatches is true");
85+
}
86+
87+
@Test
88+
void invalidPatchId() throws Exception {
89+
CreateImage createImage = new CreateImage();
90+
new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx",
91+
"--patches", "abc");
92+
93+
// insert test class into AruUtil to intercept REST calls to ARU
94+
Field aruRest = AruUtil.class.getDeclaredField("instance");
95+
aruRest.setAccessible(true);
96+
aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil());
97+
98+
assertThrows(InvalidPatchIdFormatException.class, createImage::initializeOptions);
99+
}
100+
101+
@Test
102+
void invalidPatchIdTooFewDigits() throws Exception {
103+
CreateImage createImage = new CreateImage();
104+
new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx",
105+
"--patches", "1234567");
106+
107+
// insert test class into AruUtil to intercept REST calls to ARU
108+
Field aruRest = AruUtil.class.getDeclaredField("instance");
109+
aruRest.setAccessible(true);
110+
aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil());
111+
112+
assertThrows(InvalidPatchIdFormatException.class, createImage::initializeOptions);
113+
}
114+
115+
@Test
116+
void validPatchId() throws Exception {
117+
CreateImage createImage = new CreateImage();
118+
new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx",
119+
"--patches", "12345678");
120+
121+
// insert test class into AruUtil to intercept REST calls to ARU
122+
Field aruRest = AruUtil.class.getDeclaredField("instance");
123+
aruRest.setAccessible(true);
124+
aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil());
125+
126+
createImage.initializeOptions();
127+
assertTrue(createImage.applyingPatches(), "User provided patches but applyingPatches still false");
128+
}
129+
130+
@Test
131+
void doNotGetRecommendedPatches() throws Exception {
132+
CreateImage createImage = new CreateImage();
133+
new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx",
134+
"--patches", "12345678");
135+
136+
// insert test class into AruUtil to intercept REST calls to ARU
137+
Field aruRest = AruUtil.class.getDeclaredField("instance");
138+
aruRest.setAccessible(true);
139+
aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil());
140+
141+
List<AruPatch> patches = createImage.getRecommendedPatchList(FmwInstallerType.WLS);
142+
assertTrue(patches.isEmpty(), "Neither latestPSU nor recommendedPatches was specified, but returned patches");
143+
}
144+
145+
@Test
146+
void getRecommendedPatches() throws Exception {
147+
CreateImage createImage = new CreateImage();
148+
new CommandLine(createImage).parseArgs("--tag", "tag:1", "--user", "derek", "--password", "xxx",
149+
"--patches", "12345678", "--recommendedPatches");
150+
151+
// insert test class into AruUtil to intercept REST calls to ARU
152+
Field aruRest = AruUtil.class.getDeclaredField("instance");
153+
aruRest.setAccessible(true);
154+
aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil());
155+
156+
createImage.initializeOptions();
157+
List<AruPatch> patches = createImage.getRecommendedPatchList(FmwInstallerType.WLS);
158+
assertFalse(patches.isEmpty(), "recommendedPatches was specified, but returned patches was empty");
159+
assertEquals(2, patches.size(),"ADR patch was not removed?");
160+
}
161+
162+
@Test
163+
void getRecommendedPatchesWithoutCredentials() throws Exception {
164+
CreateImage createImage = new CreateImage();
165+
new CommandLine(createImage).parseArgs("--tag", "tag:1",
166+
"--patches", "12345678", "--recommendedPatches");
167+
168+
// insert test class into AruUtil to intercept REST calls to ARU
169+
Field aruRest = AruUtil.class.getDeclaredField("instance");
170+
aruRest.setAccessible(true);
171+
aruRest.set(aruRest, new CommonPatchingOptionsTest.TestAruUtil());
172+
173+
createImage.initializeOptions();
174+
assertThrows(IllegalArgumentException.class, () -> createImage.getRecommendedPatchList(FmwInstallerType.WLS));
175+
}
176+
177+
}

pom.xml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@
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>
3736

3837
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3938
<maven.compiler.source>1.8</maven.compiler.source>
4039
<maven.compiler.target>1.8</maven.compiler.target>
4140
<test.groups>gate</test.groups>
42-
<aggregate.report.dir>tests/target/site/jacoco-aggregate/jacoco.xml</aggregate.report.dir>
4341
<skipITs>true</skipITs>
4442
</properties>
4543

@@ -262,6 +260,21 @@
262260
<groupId>org.jacoco</groupId>
263261
<artifactId>jacoco-maven-plugin</artifactId>
264262
<version>0.8.7</version>
263+
<executions>
264+
<execution>
265+
<id>prepare-agent</id>
266+
<goals>
267+
<goal>prepare-agent</goal>
268+
</goals>
269+
</execution>
270+
<execution>
271+
<id>report</id>
272+
<phase>test</phase>
273+
<goals>
274+
<goal>report</goal>
275+
</goals>
276+
</execution>
277+
</executions>
265278
</plugin>
266279
<plugin>
267280
<groupId>org.sonarsource.scanner.maven</groupId>

0 commit comments

Comments
 (0)