Skip to content

Commit

Permalink
fixed unit test for repometaanalysistask
Browse files Browse the repository at this point in the history
Signed-off-by: mehab <[email protected]>
  • Loading branch information
mehab committed Oct 2, 2023
1 parent 4de4546 commit e52bdf5
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public abstract class AbstractMetaHandler implements Handler {

ComponentProjection componentProjection;
ComponentProjectionWithPurl componentProjection;
QueryManager queryManager;
KafkaEventDispatcher kafkaEventDispatcher;
boolean fetchLatestVersion;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.dependencytrack.event.kafka.componentmeta;

public record ComponentProjectionWithPurl(String purlCoordinates, Boolean internal, String purl) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class HandlerFactory {

public static Handler createHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, boolean fetchLatestVersion) throws MalformedPackageURLException {
public static Handler createHandler(ComponentProjectionWithPurl componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, boolean fetchLatestVersion) throws MalformedPackageURLException {
PackageURL packageURL = new PackageURL(componentProjection.purl());
boolean result = RepoMetaConstants.SUPPORTED_PACKAGE_URLS_FOR_INTEGRITY_CHECK.contains(packageURL.getType());
if (result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class SupportedMetaHandler extends AbstractMetaHandler {

public SupportedMetaHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, boolean fetchLatestVersion) {
public SupportedMetaHandler(ComponentProjectionWithPurl componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, boolean fetchLatestVersion) {
this.componentProjection = componentProjection;
this.kafkaEventDispatcher = kafkaEventDispatcher;
this.queryManager = queryManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class UnSupportedMetaHandler extends AbstractMetaHandler {

public UnSupportedMetaHandler(ComponentProjection componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher,boolean fetchLatestVersion) {
public UnSupportedMetaHandler(ComponentProjectionWithPurl componentProjection, QueryManager queryManager, KafkaEventDispatcher kafkaEventDispatcher, boolean fetchLatestVersion) {
this.componentProjection = componentProjection;
this.kafkaEventDispatcher = kafkaEventDispatcher;
this.queryManager = queryManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.dependencytrack.event.ComponentVulnerabilityAnalysisEvent;
import org.dependencytrack.event.InternalComponentIdentificationEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.event.kafka.componentmeta.ComponentProjection;
import org.dependencytrack.event.kafka.componentmeta.ComponentProjectionWithPurl;
import org.dependencytrack.event.kafka.componentmeta.Handler;
import org.dependencytrack.event.kafka.componentmeta.HandlerFactory;
import org.dependencytrack.model.Component;
Expand Down Expand Up @@ -320,8 +320,8 @@ public Response createComponent(@PathParam("uuid") String uuid, Component jsonCo
component.setNotes(StringUtils.trimToNull(jsonComponent.getNotes()));

component = qm.createComponent(component, true);
ComponentProjection componentProjection =
new ComponentProjection(component.getPurlCoordinates().toString(),
ComponentProjectionWithPurl componentProjection =
new ComponentProjectionWithPurl(component.getPurlCoordinates().toString(),
component.isInternal(), component.getPurl().toString());
try {
Handler repoMetaHandler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, true);
Expand Down Expand Up @@ -415,8 +415,8 @@ public Response updateComponent(Component jsonComponent) {
component.setNotes(StringUtils.trimToNull(jsonComponent.getNotes()));

component = qm.updateComponent(component, true);
ComponentProjection componentProjection =
new ComponentProjection(component.getPurlCoordinates().toString(),
ComponentProjectionWithPurl componentProjection =
new ComponentProjectionWithPurl(component.getPurlCoordinates().toString(),
component.isInternal(), component.getPurl().toString());
try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.dependencytrack.event.PortfolioRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.ProjectRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.event.kafka.componentmeta.ComponentProjection;
import org.dependencytrack.event.kafka.componentmeta.ComponentProjectionWithPurl;
import org.dependencytrack.event.kafka.componentmeta.Handler;
import org.dependencytrack.event.kafka.componentmeta.HandlerFactory;
import org.dependencytrack.model.Component;
Expand Down Expand Up @@ -139,10 +139,10 @@ private void processPortfolio() throws Exception {
private void dispatchComponents(final List<ComponentProjection> components, QueryManager queryManager) {
for (final var component : components) {
try {
Handler repoMetaHandler = HandlerFactory.createHandler(new ComponentProjection(component.purlCoordinates(), component.internal(), component.purl()), queryManager, kafkaEventDispatcher, true);
Handler repoMetaHandler = HandlerFactory.createHandler(new ComponentProjectionWithPurl(component.purlCoordinates(), component.internal(), component.purlCoordinates()), queryManager, kafkaEventDispatcher, true);
repoMetaHandler.handle();
} catch (MalformedPackageURLException ex) {
LOGGER.warn("Unable to determine package url type for this purl %s".formatted(component.purl()), ex);
LOGGER.warn("Unable to determine package url type for this purl %s".formatted(component.purlCoordinates()), ex);
}
}
}
Expand All @@ -165,4 +165,7 @@ private List<ComponentProjection> fetchNextComponentsPage(final PersistenceManag
}
}

public record ComponentProjection(String purlCoordinates, Boolean internal) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void createHandlerForSupportedPackageTest() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
try {
PackageURL packageUrl = new PackageURL("pkg:maven/org.http4s/blaze-core_2.12");
ComponentProjection componentProjection = new ComponentProjection(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
ComponentProjectionWithPurl componentProjection = new ComponentProjectionWithPurl(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false);
assertTrue(handler instanceof SupportedMetaHandler);
} catch (MalformedPackageURLException e) {
Expand All @@ -35,7 +35,7 @@ public void createHandlerForUnSupportedPackageTest() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
try {
PackageURL packageUrl = new PackageURL("pkg:golang/github.com/foo/[email protected]");
ComponentProjection componentProjection = new ComponentProjection(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
ComponentProjectionWithPurl componentProjection = new ComponentProjectionWithPurl(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false);
assertTrue(handler instanceof UnSupportedMetaHandler);
} catch (MalformedPackageURLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testHandleIntegrityComponentNotInDB() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
try {
PackageURL packageUrl = new PackageURL("pkg:maven/org.http4s/blaze-core_2.12");
ComponentProjection componentProjection = new ComponentProjection(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
ComponentProjectionWithPurl componentProjection = new ComponentProjectionWithPurl(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
IntegrityMetaComponent integrityMetaComponent = qm.getIntegrityMetaComponent(componentProjection.purl());
Assertions.assertNull(integrityMetaComponent);
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false);
Expand Down Expand Up @@ -57,7 +57,7 @@ public void testHandleIntegrityComponentInDB() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
try {
PackageURL packageUrl = new PackageURL("pkg:maven/org.http4s/blaze-core_2.12");
ComponentProjection componentProjection = new ComponentProjection(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
ComponentProjectionWithPurl componentProjection = new ComponentProjectionWithPurl(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
var integrityMeta = new IntegrityMetaComponent();
integrityMeta.setPurl("pkg:maven/org.http4s/blaze-core_2.12");
integrityMeta.setStatus(FetchStatus.IN_PROGRESS);
Expand Down Expand Up @@ -90,7 +90,7 @@ public void testHandleIntegrityComponentInDBForMoreThanAnHour() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
try {
PackageURL packageUrl = new PackageURL("pkg:maven/org.http4s/blaze-core_2.12");
ComponentProjection componentProjection = new ComponentProjection(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
ComponentProjectionWithPurl componentProjection = new ComponentProjectionWithPurl(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
var integrityMeta = new IntegrityMetaComponent();
integrityMeta.setPurl("pkg:maven/org.http4s/blaze-core_2.12");
integrityMeta.setStatus(FetchStatus.IN_PROGRESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.dependencytrack.AbstractPostgresEnabledTest;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.event.kafka.KafkaTopics;
import org.dependencytrack.model.FetchStatus;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.util.PurlUtil;
import org.junit.Test;
Expand All @@ -25,7 +24,7 @@ public void testHandleComponentInDb() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
try {
PackageURL packageUrl = new PackageURL("pkg:golang/foo/bar@baz?ping=pong#1/2/3");
ComponentProjection componentProjection = new ComponentProjection(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
ComponentProjectionWithPurl componentProjection = new ComponentProjectionWithPurl(PurlUtil.silentPurlCoordinatesOnly(packageUrl).toString(), false, packageUrl.toString());
IntegrityMetaComponent integrityMetaComponent = qm.getIntegrityMetaComponent(componentProjection.purl());
Assertions.assertNull(integrityMetaComponent);
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false);
Expand Down

0 comments on commit e52bdf5

Please sign in to comment.