Skip to content

Commit

Permalink
style: Fix some low-severity Sonar issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalluck committed Feb 4, 2025
1 parent 8a134a8 commit f01d8b8
Show file tree
Hide file tree
Showing 41 changed files with 194 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CliIT {
@Test
@DisplayName("Should run the command without any options and print help")
@Launch(exitCode = 2)
void shouldPrintUsageOnWrongCommand(LaunchResult result) throws Exception {
void shouldPrintUsageOnWrongCommand(LaunchResult result) {
assertThat(result.getErrorOutput(), containsString("Usage: sbomerctl [-hvV] [COMMAND]"));
assertThat(result.getErrorOutput(), containsString(" sbom, s SBOM generation"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.hamcrest.Matchers.matchesPattern;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.MalformedURLException;
import java.util.List;

import org.cyclonedx.model.Bom;
Expand All @@ -22,8 +21,6 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import com.fasterxml.jackson.core.JsonProcessingException;

import io.quarkus.test.common.WithTestResource;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
Expand All @@ -38,12 +35,12 @@ class DefaultProcessorIT {
PncService pncService;

@BeforeEach
void init() throws MalformedURLException {
void init() {
this.defaultProcessor = new DefaultProcessor(pncService, Mockito.mock(KojiService.class));
}

@Test
void testProcessing() throws JsonProcessingException {
void testProcessing() {
// Prepare BOM!
Bom bom = new Bom();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void baseTest() throws IOException {

DefaultProcessor defaultProcessor = new DefaultProcessor(pncServiceMock, kojiServiceMock);

assertNotNull(bom);
assertEquals(459, bom.getComponents().size());
assertEquals(459, bom.getDependencies().size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void shouldAddSerialNumber() {
@Test
void parseBrewRpmsBom() throws IOException {
Bom bom = SbomUtils.fromPath(Paths.get("src", "test", "resources", "boms/brew_rpm.json"));
assertNotNull(bom);
assertEquals(1, bom.getDependencies().size());
assertEquals(8, bom.getDependencies().get(0).getProvides().size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void shouldAdjustVendorAndPublisher() {
.stream()
.filter(property -> "sbomer:image:labels:vendor".equals(property.getName()))
.findFirst();
assertTrue(bogusVendor.isPresent());
assertTrue(goodVendor.isPresent());
assertEquals("Red Hat", goodVendor.get().getValue());

Optional<Component> goodComponent = adjusted.getComponents()
Expand All @@ -308,6 +308,7 @@ void shouldSanitizeBogusPurls() {
new PackageURL(bogusPurl);
fail("Should fail the parsing to PURL of " + bogusPurl);
} catch (MalformedPackageURLException e) {
// Expected
}

String sanitizedPurl = PurlSanitizer.sanitizePurl(bogusPurl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
package org.jboss.sbomer.cli.test.unit.generate;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -27,7 +29,6 @@
import org.jboss.sbomer.cli.feature.sbom.generate.MavenDominoGenerator;
import org.jboss.sbomer.cli.feature.sbom.generate.ProcessRunner;
import org.jboss.sbomer.core.errors.ValidationException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.ArgumentCaptor;
Expand All @@ -42,9 +43,9 @@ class MavenDominoGeneratorTest {

@Test
void testFailedWhenNoDominoDirProvided(@TempDir Path projectDir) {
ValidationException thrown = Assertions.assertThrows(
ValidationException.class,
() -> MavenDominoGenerator.builder().withDominoDir(null).build().run(projectDir));
ValidationException thrown = assertThrows(ValidationException.class, () -> {
MavenDominoGenerator.builder().withDominoDir(null).build().run(projectDir);
});

assertEquals("Domino validation failed", thrown.getLocalizedMessage());
assertEquals(1, thrown.getErrors().size());
Expand All @@ -53,9 +54,9 @@ void testFailedWhenNoDominoDirProvided(@TempDir Path projectDir) {

@Test
void testFailedWhenDominoDirDoesntExist(@TempDir Path projectDir) {
ValidationException thrown = Assertions.assertThrows(
ValidationException.class,
() -> MavenDominoGenerator.builder().withDominoDir(Path.of("some/dir")).build().run(projectDir));
ValidationException thrown = assertThrows(ValidationException.class, () -> {
MavenDominoGenerator.builder().withDominoDir(Path.of("some/dir")).build().run(projectDir);
});

assertEquals("Domino validation failed", thrown.getLocalizedMessage());
assertEquals(1, thrown.getErrors().size());
Expand All @@ -64,9 +65,9 @@ void testFailedWhenDominoDirDoesntExist(@TempDir Path projectDir) {

@Test
void testFailedWhenDominoDoesntExistForDefaultVersion(@TempDir Path projectDir, @TempDir Path wrongDir) {
ValidationException thrown = Assertions.assertThrows(
ValidationException.class,
() -> MavenDominoGenerator.builder().withDominoDir(wrongDir).build().run(projectDir));
ValidationException thrown = assertThrows(ValidationException.class, () -> {
MavenDominoGenerator.builder().withDominoDir(wrongDir).build().run(projectDir);
});

assertEquals("Domino validation failed", thrown.getLocalizedMessage());
assertEquals(1, thrown.getErrors().size());
Expand All @@ -77,13 +78,9 @@ void testFailedWhenDominoDoesntExistForDefaultVersion(@TempDir Path projectDir,

@Test
void testFailedWhenDominoDirDoesntExistWithCustomVersion(@TempDir Path projectDir, @TempDir Path dominoDir) {
ValidationException thrown = Assertions.assertThrows(
ValidationException.class,
() -> MavenDominoGenerator.builder()
.withDominoDir(dominoDir)
.withDominoVersion("1.2.3")
.build()
.run(projectDir));
ValidationException thrown = assertThrows(ValidationException.class, () -> {
MavenDominoGenerator.builder().withDominoDir(dominoDir).withDominoVersion("1.2.3").build().run(projectDir);
});

assertEquals("Domino validation failed", thrown.getLocalizedMessage());
assertEquals(1, thrown.getErrors().size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jboss.sbomer.cli.test.unit.process;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Optional;

import org.cyclonedx.model.Bom;
import org.cyclonedx.model.Component;
Expand All @@ -26,15 +29,18 @@ void addAll() {
processor.process(bom);

assertEquals(3, component.getProperties().size());
assertEquals(
"pName",
SbomUtils.findPropertyWithNameInComponent("errata-tool-product-name", component).get().getValue());
assertEquals(
"pVersion",
SbomUtils.findPropertyWithNameInComponent("errata-tool-product-version", component).get().getValue());
assertEquals(
"pVariant",
SbomUtils.findPropertyWithNameInComponent("errata-tool-product-variant", component).get().getValue());
Optional<Property> errataToolProductName = SbomUtils
.findPropertyWithNameInComponent("errata-tool-product-name", component);
assertTrue(errataToolProductName.isPresent());
assertEquals("pName", errataToolProductName.get().getValue());
Optional<Property> errataToolProductVersion = SbomUtils
.findPropertyWithNameInComponent("errata-tool-product-version", component);
assertTrue(errataToolProductVersion.isPresent());
assertEquals("pVersion", errataToolProductVersion.get().getValue());
Optional<Property> errataToolProductVariant = SbomUtils
.findPropertyWithNameInComponent("errata-tool-product-variant", component);
assertTrue(errataToolProductVariant.isPresent());
assertEquals("pVariant", errataToolProductVariant.get().getValue());
}

@Test
Expand All @@ -56,14 +62,16 @@ void addOnlyIfMissing() {
processor.process(bom);

assertEquals(3, component.getProperties().size());
assertEquals(
"original-value",
SbomUtils.findPropertyWithNameInComponent("errata-tool-product-name", component).get().getValue());
assertEquals(
"pVersion",
SbomUtils.findPropertyWithNameInComponent("errata-tool-product-version", component).get().getValue());
assertEquals(
"pVariant",
SbomUtils.findPropertyWithNameInComponent("errata-tool-product-variant", component).get().getValue());
Optional<Property> errataToolProductName = SbomUtils
.findPropertyWithNameInComponent("errata-tool-product-name", component);
assertTrue(errataToolProductName.isPresent());
assertEquals("original-value", errataToolProductName.get().getValue());
Optional<Property> errataToolProductVersion = SbomUtils
.findPropertyWithNameInComponent("errata-tool-product-version", component);
assertTrue(errataToolProductVersion.isPresent());
assertEquals("pVersion", errataToolProductVersion.get().getValue());
Optional<Property> errataToolProductVariant = SbomUtils
.findPropertyWithNameInComponent("errata-tool-product-variant", component);
assertEquals("pVariant", errataToolProductVariant.get().getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.jboss.pnc.build.finder.koji.ClientSession;

import com.redhat.red.build.koji.KojiClientException;
import com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo;
import com.redhat.red.build.koji.model.xmlrpc.KojiArchiveQuery;
import com.redhat.red.build.koji.model.xmlrpc.KojiArchiveType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ void shouldReadSbomFromFile() {
List<Component> components = bom.getComponents();
assertEquals(39, components.size());

assertEquals(
"Apache-2.0",
bom.getMetadata().getComponent().getLicenses().getLicenses().get(0).getId());
assertEquals("Apache-2.0", bom.getMetadata().getComponent().getLicenses().getLicenses().get(0).getId());
}

@Test
Expand All @@ -82,9 +80,7 @@ void shouldReadSbomFromString() throws Exception {
assertNotNull(bom);
assertEquals(39, bom.getComponents().size());

assertEquals(
"Apache-2.0",
bom.getMetadata().getComponent().getLicenses().getLicenses().get(0).getId());
assertEquals("Apache-2.0", bom.getMetadata().getComponent().getLicenses().getLicenses().get(0).getId());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ private PncBuildConfig minimalRuntimeConfig() {

@Test
void shouldGracefullyFailOnNullConfig() {
ApplicationException ex = assertThrows(ApplicationException.class, () -> {
validator.validate(null);
});
ApplicationException ex = assertThrows(ApplicationException.class, () -> validator.validate(null));

assertEquals("No configuration provided", ex.getMessage());
}
Expand Down Expand Up @@ -170,9 +168,7 @@ private OperationConfig minimalRuntimeOperationConfig() {

@Test
void shouldGracefullyFailOnNullConfig() {
ApplicationException ex = assertThrows(ApplicationException.class, () -> {
validator.validate(null);
});
ApplicationException ex = assertThrows(ApplicationException.class, () -> validator.validate(null));

assertEquals("No configuration provided", ex.getMessage());
}
Expand Down Expand Up @@ -238,9 +234,7 @@ private DeliverableAnalysisConfig productDeliverableAnalysisConfig() {

@Test
void shouldGracefullyFailOnNullConfig() {
ApplicationException ex = assertThrows(ApplicationException.class, () -> {
validator.validate(null);
});
ApplicationException ex = assertThrows(ApplicationException.class, () -> validator.validate(null));

assertEquals("No configuration provided", ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,25 @@ void shouldHandleNull() {

@Test
void shouldHandleEmptyObject() {
ApplicationException ex = assertThrows(ApplicationException.class, () -> {
Config.fromString("{}");
});
ApplicationException ex = assertThrows(ApplicationException.class, () -> Config.fromString("{}"));

assertEquals("No configuration type provided", ex.getMessage());
}

@Test
void shouldHandleInvalidContent() {
ApplicationException ex = assertThrows(ApplicationException.class, () -> {
Config.fromString("{\"type\": \"syft-image\", \"wrong\": []}");
});
ApplicationException ex = assertThrows(
ApplicationException.class,
() -> Config.fromString("{\"type\": \"syft-image\", \"wrong\": []}"));

assertEquals("Unknown property 'wrong' at path $.wrong", ex.getMessage());
}

@Test
void shouldHandleInvalidType() {
ApplicationException ex = assertThrows(ApplicationException.class, () -> {
Config.fromString("{\"type\": \"invalid\"}");
});
ApplicationException ex = assertThrows(
ApplicationException.class,
() -> Config.fromString("{\"type\": \"invalid\"}"));

assertEquals("Invalid configuration type provided: invalid", ex.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package org.jboss.sbomer.test.e2e.ro;

import java.io.IOException;

import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.jboss.sbomer.test.e2e.E2EStageBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -42,10 +41,11 @@ static Path sbomPath(String fileName) {
return Paths.get("src", "test", "resources", "requests", fileName);
}

private static final String MANDREL_IMAGE = "registry.redhat.io/quarkus/mandrel-for-jdk-21-rhel8@sha256:a406de0fd344785fb39eba81cbef01cf7fb3e2be43d0e671a8587d1abe1418b4";
// private static final String MANDREL_IMAGE =
// "registry.redhat.io/quarkus/mandrel-for-jdk-21-rhel8@sha256:a406de0fd344785fb39eba81cbef01cf7fb3e2be43d0e671a8587d1abe1418b4";

@Test
void testMultiArchImage() throws IOException, URISyntaxException {
void testMultiArchImage() throws IOException {
String requestBody = Files.readString(sbomPath("mandrel-image.json"));
List<String> generationIds = requestGeneration(requestBody);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ void testSuccessfulGenerationMavenBuild() throws IOException {

log.info("Maven build finished, waiting for UMB message");

publishedUmbMessage(generationId, message -> {
message.body("headers.generation_request_id", CoreMatchers.is(generationId))
.body("headers.pnc_build_id", CoreMatchers.is(MAVEN_BUILD_ID))
.body("msg.build.id", CoreMatchers.is(MAVEN_BUILD_ID))
.body("msg.sbom.generationRequest.id", CoreMatchers.is(generationId));
});
publishedUmbMessage(
generationId,
message -> message.body("headers.generation_request_id", CoreMatchers.is(generationId))
.body("headers.pnc_build_id", CoreMatchers.is(MAVEN_BUILD_ID))
.body("msg.build.id", CoreMatchers.is(MAVEN_BUILD_ID))
.body("msg.sbom.generationRequest.id", CoreMatchers.is(generationId)));

log.info("Maven build passed");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void storeFiles(GenerationRequest generationRequest) {
* @return
*/
public List<String> listLogFilesInBucket(String generationRequestId) {
SbomGenerationRequest generationRequest = SbomGenerationRequest.findById(generationRequestId); // NOSONAR
SbomGenerationRequest generationRequest = SbomGenerationRequest.findById(generationRequestId); // NOSONAR

if (generationRequest == null) {
throw new NotFoundException("GenerationRequest with id '{}' could not be found", generationRequestId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public static class ErrataProductConfig {

/**
* <p>
* Generates the {@link ErrataProductConfig} object based on the data available in the CycloneDX {@link Bom}
* for the main component.
* Generates the {@link ErrataProductConfig} object based on the data available in the CycloneDX {@link Bom} for
* the main component.
* </p>
*
* <p>
Expand Down
Loading

0 comments on commit f01d8b8

Please sign in to comment.