Skip to content

Commit

Permalink
Add null check on publishedAt
Browse files Browse the repository at this point in the history
Signed-off-by: vithikashukla <[email protected]>
  • Loading branch information
vithikashukla committed Nov 1, 2023
1 parent fb5a520 commit d8fd1d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/dependencytrack/persistence/QueryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1862,8 +1862,14 @@ public ComponentMetaInformation getMetaInformation(UUID uuid) {
preparedStatement.setString(1, uuid.toString());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
var publishedDate = Date.from(resultSet.getTimestamp("PUBLISHED_AT").toInstant());
Date lastFetch = Date.from(resultSet.getTimestamp("LAST_FETCH").toInstant());
Date publishedDate = null;
Date lastFetch = null;
if(resultSet.getTimestamp("PUBLISHED_AT") != null) {
publishedDate = Date.from(resultSet.getTimestamp("PUBLISHED_AT").toInstant());
}
if(resultSet.getTimestamp("LAST_FETCH") != null) {
lastFetch = Date.from(resultSet.getTimestamp("LAST_FETCH").toInstant());
}
return new ComponentMetaInformation(publishedDate,
IntegrityMatchStatus.valueOf(resultSet.getString("INTEGRITY_CHECK_STATUS")),
lastFetch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import org.dependencytrack.model.IntegrityMatchStatus;
import org.dependencytrack.model.IntegrityMetaComponent;
import org.dependencytrack.model.Project;
import org.junit.Assert;
import org.junit.Test;

import java.util.Date;

import static org.dependencytrack.model.IntegrityMatchStatus.HASH_MATCH_PASSED;
import static org.dependencytrack.model.IntegrityMatchStatus.HASH_MATCH_UNKNOWN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class QueryManagerTest extends PersistenceCapableTest {
@Test
Expand Down Expand Up @@ -43,8 +44,36 @@ public void testGetMetaInformation() {
qm.createIntegrityMetaComponent(integrityMetaComponent);
component = qm.createComponent(component, false);
ComponentMetaInformation componentMetaInformation = qm.getMetaInformation(component.getUuid());
Assert.assertEquals(HASH_MATCH_PASSED, componentMetaInformation.integrityMatchStatus());
Assert.assertEquals(integrityMetaComponent.getPublishedAt(), componentMetaInformation.publishedDate());
Assert.assertEquals(integrityMetaComponent.getLastFetch(), componentMetaInformation.lastFetched());
assertEquals(HASH_MATCH_PASSED, componentMetaInformation.integrityMatchStatus());
assertEquals(integrityMetaComponent.getPublishedAt(), componentMetaInformation.publishedDate());
assertEquals(integrityMetaComponent.getLastFetch(), componentMetaInformation.lastFetched());
}

@Test
public void testGetMetaInformationWhenPublishedAtIsMissing() {
Project project = qm.createProject("Acme Application", null, null, null, null, null, true, false);
Component component = new Component();
component.setProject(project);
component.setName("ABC");
component.setPurl("pkg:maven/org.acme/abc");
IntegrityAnalysis integrityAnalysis = new IntegrityAnalysis();
integrityAnalysis.setComponent(component);
integrityAnalysis.setIntegrityCheckStatus(IntegrityMatchStatus.HASH_MATCH_PASSED);
integrityAnalysis.setUpdatedAt(new Date());
integrityAnalysis.setId(component.getId());
integrityAnalysis.setMd5HashMatchStatus(IntegrityMatchStatus.HASH_MATCH_PASSED);
integrityAnalysis.setSha1HashMatchStatus(HASH_MATCH_UNKNOWN);
integrityAnalysis.setSha256HashMatchStatus(HASH_MATCH_UNKNOWN);
integrityAnalysis.setSha512HashMatchStatus(HASH_MATCH_PASSED);
qm.persist(integrityAnalysis);
IntegrityMetaComponent integrityMetaComponent = new IntegrityMetaComponent();
integrityMetaComponent.setPurl(component.getPurl().toString());
integrityMetaComponent.setStatus(FetchStatus.PROCESSED);
qm.createIntegrityMetaComponent(integrityMetaComponent);
component = qm.createComponent(component, false);
ComponentMetaInformation componentMetaInformation = qm.getMetaInformation(component.getUuid());
assertEquals(HASH_MATCH_PASSED, componentMetaInformation.integrityMatchStatus());
assertNull(componentMetaInformation.publishedDate());
assertNull(componentMetaInformation.lastFetched());
}
}

0 comments on commit d8fd1d8

Please sign in to comment.