Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add ErrorProne #258

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>wildfly-elytron-x500-cert</artifactId>
</dependency>

<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
</dependency>

<!-- Test dependencies -->

<dependency>
Expand Down
1 change: 1 addition & 0 deletions api/src/main/java/com/redhat/insights/Filtering.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum Filtering implements Function<Map<String, Object>, Map<String, Objec
this.mask = mask;
}

@Override
public Map<String, Object> apply(Map<String, Object> unfiltered) {
return mask.apply(unfiltered);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2022-2023 */
/* Copyright (C) Red Hat 2022-2024 */
package com.redhat.insights;

/**
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/com/redhat/insights/InsightsException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights;

import static com.redhat.insights.InsightsErrorCode.NONE;
Expand Down Expand Up @@ -42,6 +42,6 @@ public InsightsException(InsightsErrorCode error, Throwable cause) {

@Override
public String getMessage() {
return error.formatMessage(super.getMessage());
return error.formatMessage(String.valueOf(super.getMessage()));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.http;

import static com.redhat.insights.InsightsErrorCode.ERROR_GZIP_FILE;
Expand All @@ -8,6 +8,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.zip.GZIPOutputStream;

/**
Expand Down Expand Up @@ -72,7 +73,8 @@ static byte[] gzipReport(final byte[] report) {
gzip.close();
return baos.toByteArray();
} catch (IOException iox) {
throw new InsightsException(ERROR_GZIP_FILE, "Failed to GZIP report: " + report, iox);
throw new InsightsException(
ERROR_GZIP_FILE, "Failed to GZIP report: " + Arrays.toString(report), iox);
}
}
}
31 changes: 15 additions & 16 deletions api/src/main/java/com/redhat/insights/jars/JarAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,21 +138,20 @@ private JarInfo getJarInfo(String jarFilename, URL url, Map<String, String> attr
try {
getExtraAttributes(jarInputStream, attributes);

Map<String, String> pom = getPom(jarInputStream);
Optional<Map<String, String>> oPom = getPom(jarInputStream);

// if we find exactly one pom, use it
if (pom != null) {
if (oPom.isPresent()) {
Map<String, String> pom = oPom.get();
attributes.putAll(pom);
return new JarInfo(jarFilename, pom.get("version"), attributes);
return new JarInfo(jarFilename, pom.getOrDefault("version", UNKNOWN_VERSION), attributes);
}
} catch (Exception ex) {
logger.error(url + "Exception getting extra attributes or pom", ex);
}

String version = getVersion(jarInputStream);
if (version == null || "".equals(version)) {
version = UNKNOWN_VERSION;
}
Optional<String> oVersion = getVersion(jarInputStream);
String version = oVersion.orElse(UNKNOWN_VERSION);

return new JarInfo(jarFilename, version, attributes);
}
Expand All @@ -163,26 +162,26 @@ private JarInfo getJarInfo(String jarFilename, URL url, Map<String, String> attr
* are found, return null.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static Map<String, String> getPom(JarInputStream jarFile) throws IOException {
Map<String, String> pom = null;
private static Optional<Map<String, String>> getPom(JarInputStream jarFile) throws IOException {
Optional<Map<String, String>> out = Optional.empty();

for (JarEntry entry = jarFile.getNextJarEntry();
entry != null;
entry = jarFile.getNextJarEntry()) {
if (entry.getName().startsWith("META-INF/maven")
&& entry.getName().endsWith("pom.properties")) {
if (pom != null) {
if (out.isPresent()) {
// we've found multiple pom files. bail!
return null;
return Optional.empty();
}
Properties props = new Properties();
props.load(jarFile);

pom = (Map) props;
out = Optional.of((Map) props);
}
}

return pom;
return out;
}

static void getExtraAttributes(JarInputStream jarFile, Map<String, String> map) {
Expand All @@ -200,13 +199,13 @@ static void getExtraAttributes(JarInputStream jarFile, Map<String, String> map)
}
}

static String getVersion(JarInputStream jarFile) {
static Optional<String> getVersion(JarInputStream jarFile) {
Manifest manifest = jarFile.getManifest();
if (manifest == null) {
return null;
return Optional.empty();
}

return JarUtils.getVersionFromManifest(manifest);
return Optional.of(JarUtils.getVersionFromManifest(manifest));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;

/**
* Base class that collects basic information and delegates to the subreport for product-specific
Expand Down Expand Up @@ -84,7 +85,7 @@ public abstract class AbstractTopLevelReportBase implements InsightsReport {
private final JsonSerializer<InsightsReport> serializer;
private final InsightsLogger logger;
private final InsightsConfiguration config;
private byte[] subReport;
private byte @Nullable [] subReport;

// Can't be set properly until after report has been generated
private String idHash = "";
Expand Down
8 changes: 6 additions & 2 deletions api/src/main/java/com/redhat/insights/reports/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.reports;

import static java.util.Collections.emptyMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -11,11 +13,13 @@ private Utils() {}
@SuppressWarnings("unchecked")
public static Map<String, Object> defaultMasking(final Map<String, Object> inArgs) {
List<String> jvmArgs = new ArrayList<>();
for (String arg : (List<String>) (inArgs.get("jvm.args"))) {
List<String> args = (List<String>) (inArgs.getOrDefault("jvm.args", emptyMap()));
for (String arg : args) {
jvmArgs.add(sanitizeJavaParameters(arg));
}
inArgs.put("jvm.args", jvmArgs);
inArgs.put("java.command", sanitizeJavaParameters((String) inArgs.get("java.command")));
String command = (String) inArgs.getOrDefault("java.command", "");
inArgs.put("java.command", sanitizeJavaParameters(command));
return inArgs;
}

Expand Down
43 changes: 16 additions & 27 deletions api/src/test/java/com/redhat/insights/TestTopLevelReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import java.io.IOException;
import java.lang.management.*;
import java.util.*;
import org.jspecify.annotations.NullUnmarked;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

@NullUnmarked
public class TestTopLevelReport extends AbstractReportTest {
@Test
public void testGenerateReportWithoutSubreports() throws IOException {
Expand Down Expand Up @@ -97,14 +99,10 @@ public void testEmptyReport() throws IOException {
public void testGenerateReportWithEmptySubreport() throws IOException {
JarInfoSubreport jarInfoSubreport =
new JarInfoSubreport(logger, Collections.singletonList(JarInfo.MISSING));
InsightsReport insightsReport =
new DummyTopLevelReport(
logger,
new HashMap<String, InsightsSubreport>() {
{
put("jarsSubreport", jarInfoSubreport);
}
});
Map<String, InsightsSubreport> reports = new HashMap<>();
reports.put("jarsSubreport", jarInfoSubreport);

InsightsReport insightsReport = new DummyTopLevelReport(logger, reports);

String report = generateReport(insightsReport);

Expand Down Expand Up @@ -139,33 +137,24 @@ public void testGenerateReportWithEmptySubreport() throws IOException {

@Test
public void testGenerateReportWithJarInfoSubreports() throws IOException {
Map<String, String> attrs = new HashMap<>();
attrs.put("attr1", "value1");
attrs.put("attr2", "value2 \t \t ");

// prepare JarInfo
JarInfo jarInfoWithoutAttrs = new JarInfo("RandomName", "0.9", Collections.emptyMap());
JarInfo jarInfoWithAttrs =
new JarInfo(
"DifferentName :\" \n",
"0.1",
new HashMap<String, String>() {
{
put("attr1", "value1");
put("attr2", "value2 \t \t ");
}
});
JarInfo jarInfoWithAttrs = new JarInfo("DifferentName :\" \n", "0.1", attrs);

// put JarInfos into subreport
JarInfoSubreport jarInfoSubreport =
new JarInfoSubreport(logger, Arrays.asList(jarInfoWithoutAttrs, jarInfoWithAttrs));

Map<String, InsightsSubreport> reports = new HashMap<>();
reports.put("jarsSubreport", jarInfoSubreport);
reports.put("classpathSubreport", new ClasspathJarInfoSubreport(logger));

// create top level report with subreports
InsightsReport insightsReport =
new DummyTopLevelReport(
logger,
new HashMap<String, InsightsSubreport>() {
{
put("jarsSubreport", jarInfoSubreport);
put("classpathSubreport", new ClasspathJarInfoSubreport(logger));
}
});
InsightsReport insightsReport = new DummyTopLevelReport(logger, reports);

String report = generateReport(insightsReport);

Expand Down
19 changes: 8 additions & 11 deletions api/src/test/java/com/redhat/insights/TestUpdateReport.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -10,8 +10,10 @@
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.jspecify.annotations.NullUnmarked;
import org.junit.jupiter.api.Test;

@NullUnmarked
public class TestUpdateReport extends AbstractReportTest {

@Test
Expand Down Expand Up @@ -55,17 +57,12 @@ public void testFilledQueue() throws IOException {
String idHash = "Lorem ipsum dolor sit amet";
BlockingQueue<JarInfo> blockingQueue = new ArrayBlockingQueue<>(10);

Map<String, String> attrs = new HashMap<>();
attrs.put("attr1", "value1");
attrs.put("attr2", "value2 \t \t ");

JarInfo jarInfoWithoutAttrs = new JarInfo("RandomName", "0.9", Collections.emptyMap());
JarInfo jarInfoWithAttrs =
new JarInfo(
"DifferentName",
"0.1",
new HashMap<String, String>() {
{
put("attr1", "value1");
put("attr2", "value2 \t \t ");
}
});
JarInfo jarInfoWithAttrs = new JarInfo("DifferentName", "0.1", attrs);

blockingQueue.add(jarInfoWithoutAttrs);
blockingQueue.add(jarInfoWithAttrs);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.doubles;

import com.redhat.insights.config.DefaultInsightsConfiguration;

public class DefaultConfiguration extends DefaultInsightsConfiguration {
@Override
public String getIdentificationName() {
return null;
return "[default]";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* Copyright (C) Red Hat 2022-2023 */
/* Copyright (C) Red Hat 2022-2024 */
package com.redhat.insights.doubles;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.redhat.insights.reports.InsightsSubreport;
import java.io.IOException;

/** A generic Java application {@link InsightsSubreport} implementation. */
public final class DummyInsightsSubreport implements InsightsSubreport {
Expand All @@ -17,6 +20,11 @@ public String getVersion() {

@Override
public JsonSerializer<InsightsSubreport> getSerializer() {
return null;
return new JsonSerializer<InsightsSubreport>() {
@Override
public void serialize(
InsightsSubreport value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.doubles;

import com.redhat.insights.http.InsightsHttpClient;
import com.redhat.insights.reports.InsightsReport;
import org.jspecify.annotations.Nullable;

/**
* Fake http client, which will store content of report sent. Designed to test @link
* InsightsReportController
*/
public class StoringInsightsHttpClient implements InsightsHttpClient {
private String reportFilename = null;
private InsightsReport reportContent = null;
private @Nullable String reportFilename = null;
private @Nullable InsightsReport reportContent = null;
private int reportsSent = 0;

private boolean readyToSend = true;
Expand Down Expand Up @@ -40,11 +41,11 @@ public void setReadyToSend(boolean readyToSend) {
this.readyToSend = readyToSend;
}

public String getReportFilename() {
public @Nullable String getReportFilename() {
return reportFilename;
}

public InsightsReport getReportContent() {
public @Nullable InsightsReport getReportContent() {
return reportContent;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) Red Hat 2023 */
/* Copyright (C) Red Hat 2023-2024 */
package com.redhat.insights.http;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -47,6 +47,7 @@ public String getIdentificationName() {
return "TEST";
}

@Override
public String getArchiveUploadDir() {
return tmpdir.toString();
}
Expand Down
Loading
Loading