Skip to content

Commit

Permalink
unit tests added
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 7b7f1d6 commit 0c1d184
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void batchProcessPurls(QueryManager qm) {
List<String> purls = qm.fetchNextPurlsPage(offset);
while (!purls.isEmpty()) {
long cumulativeProcessingTime = System.currentTimeMillis() - startTime;
if(isLockToBeExtended(cumulativeProcessingTime, INTEGRITY_META_INITIALIZER_TASK_LOCK)) {
if (isLockToBeExtended(cumulativeProcessingTime, INTEGRITY_META_INITIALIZER_TASK_LOCK)) {
LockExtender.extendActiveLock(Duration.ofMinutes(5).plus(lockConfiguration.getLockAtLeastFor()), lockConfiguration.getLockAtLeastFor());
}
dispatchPurls(qm, purls);
Expand All @@ -88,7 +88,7 @@ private void updateIntegrityMetaForPurls(QueryManager qm, List<String> purls) {
private void dispatchPurls(QueryManager qm, List<String> purls) {
for (final var purl : purls) {
ComponentProjection componentProjection = qm.getComponentByPurl(purl);
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates, componentProjection.internal));
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates, componentProjection.internal, true, false));
}
}

Expand Down
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ public SupportedMetaHandler(ComponentProjection componentProjection, QueryManage
}

@Override
public void handle() {
public IntegrityMetaComponent handle() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
try (QueryManager queryManager = new QueryManager()) {
IntegrityMetaComponent integrityMetaComponent = queryManager.getIntegrityMetaComponent(componentProjection.purl());
if (integrityMetaComponent != null) {
if (integrityMetaComponent.getStatus() == null || (integrityMetaComponent.getStatus() == FetchStatus.IN_PROGRESS && Date.from(Instant.now()).getTime() - integrityMetaComponent.getLastFetch().getTime() > TIME_SPAN)) {
integrityMetaComponent.setLastFetch(Date.from(Instant.now()));
queryManager.updateIntegrityMetaComponent(integrityMetaComponent);
IntegrityMetaComponent integrityMetaComponent1 = queryManager.updateIntegrityMetaComponent(integrityMetaComponent);
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true, fetchLatestVersion));
return integrityMetaComponent1;
} else {
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false, fetchLatestVersion));
return integrityMetaComponent;
}
} else {
queryManager.createIntegrityMetaComponent(createIntegrityMetaComponent(componentProjection.purl()));
IntegrityMetaComponent integrityMetaComponent1 = queryManager.createIntegrityMetaComponent(createIntegrityMetaComponent(componentProjection.purl()));
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), true, fetchLatestVersion));
return integrityMetaComponent1;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.dependencytrack.event.ComponentRepositoryMetaAnalysisEvent;
import org.dependencytrack.event.kafka.KafkaEventDispatcher;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.persistence.QueryManager;

public class UnSupportedMetaHandler extends AbstractMetaHandler {
Expand All @@ -14,8 +15,9 @@ public UnSupportedMetaHandler(ComponentProjection componentProjection, QueryMana
}

@Override
public void handle() {
public IntegrityMetaComponent handle() {
KafkaEventDispatcher kafkaEventDispatcher = new KafkaEventDispatcher();
kafkaEventDispatcher.dispatchAsync(new ComponentRepositoryMetaAnalysisEvent(componentProjection.purlCoordinates(), componentProjection.internal(), false, fetchLatestVersion));
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1721,12 +1721,9 @@ public List<String> fetchNextPurlsPage(long offset) {

public void batchUpdateIntegrityMetaComponent(List<IntegrityMetaComponent> purls) {
getIntegrityMetaQueryManager().batchUpdateIntegrityMetaComponent(purls);
<<<<<<< HEAD
}

public IntegrityMetaComponent createIntegrityMetaComponent(IntegrityMetaComponent integrityMetaComponent) {
return getComponentQueryManager().createIntegrityMetaComponent(integrityMetaComponent);
=======
>>>>>>> a0c2364d6f47ccf6d44f3c44724d1fae65604740
}
}
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);
}

}
}
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");
}
}
}
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");
}
}
}

0 comments on commit 0c1d184

Please sign in to comment.