-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: mehab <[email protected]>
- Loading branch information
Showing
8 changed files
with
229 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
src/main/java/org/dependencytrack/event/kafka/componentmeta/Handler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import org.dependencytrack.model.IntegrityMetaComponent; | ||
|
||
public interface Handler { | ||
void handle(); | ||
IntegrityMetaComponent handle(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/org/dependencytrack/event/kafka/componentmeta/HandlerFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import alpine.common.logging.Logger; | ||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURL; | ||
import org.dependencytrack.PersistenceCapableTest; | ||
import org.dependencytrack.event.kafka.KafkaEventDispatcher; | ||
import org.dependencytrack.util.PurlUtil; | ||
import org.junit.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class HandlerFactoryTest extends PersistenceCapableTest { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(HandlerFactoryTest.class); | ||
|
||
@Test | ||
public void createHandlerForSupportedPackageTest() { | ||
Handler handler; | ||
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()); | ||
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false); | ||
assertTrue(handler instanceof SupportedMetaHandler); | ||
} catch (MalformedPackageURLException e) { | ||
LOGGER.warn("Package url not formed correctly"); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void createHandlerForUnSupportedPackageTest() { | ||
Handler handler; | ||
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()); | ||
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false); | ||
assertTrue(handler instanceof UnSupportedMetaHandler); | ||
} catch (MalformedPackageURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
src/test/java/org/dependencytrack/event/kafka/componentmeta/SupportedMetaHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import alpine.common.logging.Logger; | ||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURL; | ||
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; | ||
import org.junit.jupiter.api.Assertions; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.dependencytrack.util.KafkaTestUtil.deserializeValue; | ||
|
||
public class SupportedMetaHandlerTest extends AbstractPostgresEnabledTest { | ||
private static final Logger LOGGER = Logger.getLogger(SupportedMetaHandlerTest.class); | ||
|
||
@Test | ||
public void testHandleIntegrityComponentNotInDB() { | ||
Handler handler; | ||
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()); | ||
IntegrityMetaComponent integrityMetaComponent = qm.getIntegrityMetaComponent(componentProjection.purl()); | ||
Assertions.assertNull(integrityMetaComponent); | ||
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false); | ||
IntegrityMetaComponent result = handler.handle(); | ||
assertThat(kafkaMockProducer.history()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.topic()).isEqualTo(KafkaTopics.REPO_META_ANALYSIS_COMMAND.name()); | ||
final var command = deserializeValue(KafkaTopics.REPO_META_ANALYSIS_COMMAND, record); | ||
assertThat(command.getComponent().getPurl()).isEqualTo("pkg:maven/org.http4s/blaze-core_2.12"); | ||
assertThat(command.getComponent().getInternal()).isFalse(); | ||
assertThat(command.getFetchIntegrityData()).isTrue(); | ||
assertThat(command.getFetchLatestVersion()).isFalse(); | ||
} | ||
|
||
); | ||
Assertions.assertEquals(FetchStatus.IN_PROGRESS, result.getStatus()); | ||
|
||
} catch (MalformedPackageURLException ex) { | ||
LOGGER.warn("Package url not formed correctly"); | ||
} | ||
} | ||
|
||
@Test | ||
public void testHandleIntegrityComponentInDB() { | ||
Handler handler; | ||
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()); | ||
var integrityMeta = new IntegrityMetaComponent(); | ||
integrityMeta.setPurl("pkg:maven/org.http4s/blaze-core_2.12"); | ||
integrityMeta.setStatus(FetchStatus.IN_PROGRESS); | ||
integrityMeta.setLastFetch(Date.from(Instant.now().minus(2, ChronoUnit.MINUTES))); | ||
qm.createIntegrityMetaComponent(integrityMeta); | ||
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false); | ||
IntegrityMetaComponent result = handler.handle(); | ||
assertThat(kafkaMockProducer.history()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.topic()).isEqualTo(KafkaTopics.REPO_META_ANALYSIS_COMMAND.name()); | ||
final var command = deserializeValue(KafkaTopics.REPO_META_ANALYSIS_COMMAND, record); | ||
assertThat(command.getComponent().getPurl()).isEqualTo("pkg:maven/org.http4s/blaze-core_2.12"); | ||
assertThat(command.getComponent().getInternal()).isFalse(); | ||
assertThat(command.getFetchIntegrityData()).isFalse(); | ||
assertThat(command.getFetchLatestVersion()).isFalse(); | ||
} | ||
|
||
); | ||
Assertions.assertEquals(FetchStatus.IN_PROGRESS, result.getStatus()); | ||
|
||
} catch (MalformedPackageURLException ex) { | ||
LOGGER.warn("Package url not formed correctly"); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testHandleIntegrityComponentInDBForMoreThanAnHour() { | ||
Handler handler; | ||
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()); | ||
var integrityMeta = new IntegrityMetaComponent(); | ||
integrityMeta.setPurl("pkg:maven/org.http4s/blaze-core_2.12"); | ||
integrityMeta.setStatus(FetchStatus.IN_PROGRESS); | ||
integrityMeta.setLastFetch(Date.from(Instant.now().minus(2, ChronoUnit.HOURS))); | ||
qm.createIntegrityMetaComponent(integrityMeta); | ||
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false); | ||
IntegrityMetaComponent integrityMetaComponent = handler.handle(); | ||
assertThat(kafkaMockProducer.history()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.topic()).isEqualTo(KafkaTopics.REPO_META_ANALYSIS_COMMAND.name()); | ||
final var command = deserializeValue(KafkaTopics.REPO_META_ANALYSIS_COMMAND, record); | ||
assertThat(command.getComponent().getPurl()).isEqualTo("pkg:maven/org.http4s/blaze-core_2.12"); | ||
assertThat(command.getComponent().getInternal()).isFalse(); | ||
assertThat(command.getFetchIntegrityData()).isTrue(); | ||
assertThat(command.getFetchLatestVersion()).isFalse(); | ||
} | ||
|
||
); | ||
Assertions.assertEquals(FetchStatus.IN_PROGRESS, integrityMetaComponent.getStatus()); | ||
assertThat(integrityMetaComponent.getLastFetch()).isAfter(Date.from(Instant.now().minus(2, ChronoUnit.MINUTES))); | ||
|
||
} catch (MalformedPackageURLException ex) { | ||
LOGGER.warn("Package url not formed correctly"); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/java/org/dependencytrack/event/kafka/componentmeta/UnSupportedMetaHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.dependencytrack.event.kafka.componentmeta; | ||
|
||
import alpine.common.logging.Logger; | ||
import com.github.packageurl.MalformedPackageURLException; | ||
import com.github.packageurl.PackageURL; | ||
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; | ||
import org.junit.jupiter.api.Assertions; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.dependencytrack.util.KafkaTestUtil.deserializeValue; | ||
|
||
public class UnSupportedMetaHandlerTest extends AbstractPostgresEnabledTest { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(SupportedMetaHandlerTest.class); | ||
|
||
@Test | ||
public void testHandleComponentInDb() { | ||
Handler handler; | ||
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()); | ||
IntegrityMetaComponent integrityMetaComponent = qm.getIntegrityMetaComponent(componentProjection.purl()); | ||
Assertions.assertNull(integrityMetaComponent); | ||
handler = HandlerFactory.createHandler(componentProjection, qm, kafkaEventDispatcher, false); | ||
handler.handle(); | ||
assertThat(kafkaMockProducer.history()).satisfiesExactly( | ||
record -> { | ||
assertThat(record.topic()).isEqualTo(KafkaTopics.REPO_META_ANALYSIS_COMMAND.name()); | ||
final var command = deserializeValue(KafkaTopics.REPO_META_ANALYSIS_COMMAND, record); | ||
assertThat(command.getComponent().getPurl()).isEqualTo("pkg:golang/foo/bar@baz"); | ||
assertThat(command.getComponent().getInternal()).isFalse(); | ||
assertThat(command.getFetchIntegrityData()).isFalse(); | ||
assertThat(command.getFetchLatestVersion()).isFalse(); | ||
} | ||
|
||
); | ||
Assertions.assertNull(integrityMetaComponent); | ||
|
||
} catch (MalformedPackageURLException ex) { | ||
LOGGER.warn("Package url not formed correctly"); | ||
} | ||
} | ||
} |