Skip to content

Commit 11da2bc

Browse files
committed
Update types to be nullable instead of taking in Optional params. Add preliminary unit test
1 parent e01ab9f commit 11da2bc

5 files changed

Lines changed: 18 additions & 18 deletions

File tree

core/src/main/java/io/snyk/plugins/artifactory/model/MonitoredArtifact.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static Optional<MonitoredArtifact> read(ArtifactProperties properties) {
7575
properties.getArtifactPath(),
7676
testResult,
7777
Ignores.read(properties),
78-
Optional.empty() // Created date not stored in properties
78+
null // Created date not stored in properties
7979
)
8080
);
8181
} catch (RuntimeException e) {

core/src/main/java/io/snyk/plugins/artifactory/model/ValidationSettings.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public class ValidationSettings {
1313

1414
private final Optional<Severity> vulnSeverityThreshold;
1515
private final Optional<Severity> licenseSeverityThreshold;
16-
private final Optional<int> createdDelayDays;
16+
private final Optional<Integer> createdDelayDays;
1717

1818
public ValidationSettings() {
1919
this(Optional.of(Severity.HIGH), Optional.of(Severity.HIGH), Optional.of(0));
2020
}
2121

22-
private ValidationSettings(Optional<Severity> vulnSeverityThreshold, Optional<Severity> licenseSeverityThreshold, Optional<int> createdDelayDays) {
22+
private ValidationSettings(Optional<Severity> vulnSeverityThreshold, Optional<Severity> licenseSeverityThreshold, Optional<Integer> createdDelayDays) {
2323
this.vulnSeverityThreshold = vulnSeverityThreshold;
2424
this.licenseSeverityThreshold = licenseSeverityThreshold;
2525
this.createdDelayDays = createdDelayDays;
@@ -33,7 +33,7 @@ public ValidationSettings withLicenseSeverityThreshold(Optional<Severity> thresh
3333
return new ValidationSettings(vulnSeverityThreshold, threshold, createdDelayDays);
3434
}
3535

36-
public ValidationSettings withCreatedDelayDays(Optional<int> days) {
36+
public ValidationSettings withCreatedDelayDays(Optional<Integer> days) {
3737
return new ValidationSettings(vulnSeverityThreshold, licenseSeverityThreshold, days);
3838
}
3939

@@ -45,7 +45,7 @@ public Optional<Severity> getLicenseSeverityThreshold() {
4545
return licenseSeverityThreshold;
4646
}
4747

48-
public Optional<int> getCreatedDelayDays() {
48+
public Optional<Integer> getCreatedDelayDays() {
4949
return createdDelayDays;
5050
}
5151

@@ -65,7 +65,7 @@ public static ValidationSettings from(String vulnThreshold, String licenseThresh
6565
return new ValidationSettings(
6666
parseSeverity(vulnThreshold),
6767
parseSeverity(licenseThreshold),
68-
Integer.parseInt(createdDelayDaysStr)
68+
Optional.of(Integer.parseInt(createdDelayDaysStr))
6969
);
7070
}
7171

core/src/main/java/io/snyk/plugins/artifactory/scanner/PackageValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public void validate(MonitoredArtifact artifact) {
3131
}
3232

3333
private void validateCreatedDelay(MonitoredArtifact artifact) {
34-
int delayDays = settings.getCreatedDelayDays();
35-
if (delayDays <= 0) {
34+
Integer delayDays = settings.getCreatedDelayDays().get();
35+
if (delayDays == null || delayDays <= 0) {
3636
LOG.debug("Created delay is disabled ({} days)", delayDays);
3737
return;
3838
}

core/src/main/java/io/snyk/plugins/artifactory/scanner/ScannerModule.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.time.Duration;
2323
import java.time.Instant;
2424
import java.time.ZoneId;
25-
import java.util.LocalDate;
25+
import java.time.LocalDate;
2626
import java.util.Optional;
2727

2828
import static java.util.Objects.requireNonNull;
@@ -98,23 +98,23 @@ private void filter(MonitoredArtifact artifact) {
9898

9999
private @NotNull MonitoredArtifact toMonitoredArtifact(TestResult testResult, @NotNull RepoPath repoPath) {
100100
Ignores ignores = Ignores.read(new RepositoryArtifactProperties(repoPath, repositories));
101-
Optional<Instant> createdDate = getCreatedDate(repoPath);
101+
Instant createdDate = getCreatedDate(repoPath);
102102
return new MonitoredArtifact(repoPath.toString(), testResult, ignores, createdDate);
103103
}
104104

105-
private Optional<Instant> getCreatedDate(RepoPath repoPath) {
105+
private Instant getCreatedDate(RepoPath repoPath) {
106106
try {
107107
ItemInfo itemInfo = repositories.getItemInfo(repoPath);
108108
if (itemInfo != null) {
109-
LocalDate created = Instant.ofEpochMilli(createdDate).atZone(ZoneId.systemDefault()).toLocalDate();
109+
LocalDate created = Instant.ofEpochMilli(itemInfo.getCreated()).atZone(ZoneId.systemDefault()).toLocalDate();
110110
if (created != null) {
111-
return Optional.of(created.atStartOfDay(ZoneId.systemDefault()).toInstant());
111+
return created.atStartOfDay(ZoneId.systemDefault()).toInstant();
112112
}
113113
}
114114
} catch (Exception e) {
115115
LOG.debug("Could not retrieve created date for {}: {}", repoPath, e);
116116
}
117-
return Optional.empty();
117+
return null;
118118
}
119119

120120
private boolean shouldTestContinuously() {

core/src/test/java/io/snyk/plugins/artifactory/scanner/PackageValidatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ void validate_includesSnykDetailsUrlInCancelException() {
145145

146146
@Test
147147
void validate_includesCreatedDateDelay() {
148-
int createdDelayDays = 14
148+
Integer createdDelayDays = 14;
149149
ValidationSettings settings = new ValidationSettings()
150-
.withCreatedDelayDays(createdDelayDays)
150+
.withCreatedDelayDays(Optional.of(createdDelayDays))
151151
.withVulnSeverityThreshold(Optional.empty())
152152
.withLicenseSeverityThreshold(Optional.empty());
153153

@@ -159,12 +159,12 @@ void validate_includesCreatedDateDelay() {
159159
IssueSummary.from(Stream.empty()),
160160
URI.create("https://snyk.io/package/details")
161161
),
162-
new Ignores()
162+
new Ignores(),
163163
Instant.now()
164164
);
165165

166166
assertThatThrownBy(() -> validator.validate(artifact))
167167
.isExactlyInstanceOf(CancelException.class)
168-
.hasMessageContaining(format("Artifact was created 0 days ago, which is less than the configured delay of %d days", createdDelayDays))
168+
.hasMessageContaining("Artifact was created 0 days ago, which is less than the configured delay of 14 days");
169169
}
170170
}

0 commit comments

Comments
 (0)