Skip to content

Commit 989c0ac

Browse files
committed
time dependent tests parametrized relatively
1 parent 46ab5c3 commit 989c0ac

File tree

3 files changed

+115
-29
lines changed

3 files changed

+115
-29
lines changed

src/test/java/com/sap/oss/phosphor/fosstars/model/score/oss/ScoreVerificationTest.java

+103-17
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,28 @@
99
import com.sap.oss.phosphor.fosstars.model.qa.TestVectors;
1010
import com.sap.oss.phosphor.fosstars.model.qa.VerificationFailedException;
1111
import com.sap.oss.phosphor.fosstars.model.rating.oss.OssSecurityRating;
12+
import java.io.File;
13+
import java.io.FileInputStream;
14+
import java.io.IOException;
1215
import java.io.InputStream;
16+
import java.net.URL;
17+
import java.nio.charset.StandardCharsets;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.time.LocalDate;
21+
import java.time.format.DateTimeFormatter;
1322
import java.util.List;
23+
import java.util.regex.Matcher;
24+
import java.util.regex.Pattern;
25+
import org.apache.commons.io.FileUtils;
1426
import org.junit.jupiter.api.Test;
1527

1628
public class ScoreVerificationTest {
1729

1830
public static final String TEST_VECTORS_FILE_NAME_TEMPLATE_DEFAULT = "%sTestVectors.yml";
1931

20-
public static final String TEST_VECTORS_FILE_NAME_TEMPLATE_TIMEDEPENDENT = "%sTestVectorsTimeDependent.yml";
32+
public static final String TEST_VECTORS_FILE_NAME_TEMPLATE_TIME_DEPENDENT =
33+
"%sTestVectorsTimeDependent.yml";
2134

2235
@Test
2336
public void testVerification() {
@@ -34,51 +47,124 @@ public void testVerification() {
3447

3548
for (Score score : collector.scores()) {
3649
if (!failingScores.contains(score.getClass().getSimpleName())) {
37-
scoreVerification(score, TEST_VECTORS_FILE_NAME_TEMPLATE_DEFAULT);
50+
scoreVerification(score);
3851
}
3952
}
4053
}
4154

4255
@Test
4356
public void testVulnerabilityDiscoveryAndSecurityTestingScoreTimeIndependent() {
4457
scoreVerification(
45-
new VulnerabilityDiscoveryAndSecurityTestingScore(new ProjectSecurityTestingScore()), TEST_VECTORS_FILE_NAME_TEMPLATE_DEFAULT);
58+
new VulnerabilityDiscoveryAndSecurityTestingScore(new ProjectSecurityTestingScore()));
4659
}
4760

4861
@Test
49-
public void testVulnerabilityDiscoveryAndSecurityTestingScoreTimeDependent() {
50-
scoreVerification(
51-
new VulnerabilityDiscoveryAndSecurityTestingScore(new ProjectSecurityTestingScore()), TEST_VECTORS_FILE_NAME_TEMPLATE_TIMEDEPENDENT);
62+
public void testVulnerabilityDiscoveryAndSecurityTestingScoreTimeDependent() throws IOException {
63+
scoreVerificationWithTimeDependentValuesSubstituted(
64+
new VulnerabilityDiscoveryAndSecurityTestingScore(new ProjectSecurityTestingScore()));
5265
}
5366

5467
@Test
5568
public void testSecurityReviewScoreTimeIndependent() {
56-
scoreVerification(new SecurityReviewScore(), TEST_VECTORS_FILE_NAME_TEMPLATE_DEFAULT);
69+
scoreVerification(new SecurityReviewScore());
5770
}
5871

5972
@Test
60-
public void testSecurityReviewScoreTimeDependent() {
61-
scoreVerification(new SecurityReviewScore(), TEST_VECTORS_FILE_NAME_TEMPLATE_TIMEDEPENDENT);
73+
public void testSecurityReviewScoreTimeDependent() throws IOException {
74+
scoreVerificationWithTimeDependentValuesSubstituted(new SecurityReviewScore());
75+
}
76+
77+
protected void scoreVerificationWithTimeDependentValuesSubstituted(Score score)
78+
throws IOException {
79+
String testVectorsFileName = String.format(TEST_VECTORS_FILE_NAME_TEMPLATE_TIME_DEPENDENT,
80+
score.getClass().getSimpleName());
81+
URL url = score.getClass().getResource(testVectorsFileName);
82+
File testYamlFile = FileUtils.getFile(url.getFile());
83+
String yamlString = FileUtils.readFileToString(testYamlFile, StandardCharsets.UTF_8);
84+
String output = substituteYearsAndDaysPlaceholder(yamlString);
85+
// Write the output to a temporary file
86+
Path tempFile = Files.createTempFile("", ".yml");
87+
Files.writeString(tempFile, output);
88+
System.out.println(tempFile + " created for time-dependent test.");
89+
scoreVerification(score, tempFile.toFile());
90+
Files.delete(tempFile);
91+
System.out.println(tempFile + " deleted for time-dependent test.");
92+
}
93+
94+
protected void scoreVerification(Score score) {
95+
String testVectorsFileName =
96+
String.format(TEST_VECTORS_FILE_NAME_TEMPLATE_DEFAULT, score.getClass().getSimpleName());
97+
URL url = score.getClass().getResource(testVectorsFileName);
98+
File testYamlFile = FileUtils.getFile(url.getFile());
99+
scoreVerification(score, testYamlFile);
62100
}
63101

64-
protected void scoreVerification(Score score, String testVectorsFileNameTemplate) {
65-
String className = score.getClass().getSimpleName();
66-
String testVectorsFileName = String.format(testVectorsFileNameTemplate, className);
67-
try (InputStream is = score.getClass().getResourceAsStream(testVectorsFileName)) {
102+
protected void scoreVerification(Score score, File file) {
103+
try (InputStream is = new FileInputStream(file)) {
68104
TestVectors testVectors = TestVectors.loadFromYaml(is);
69105
if (testVectors.isEmpty()) {
70-
System.out.printf("No test vectors for %s%n", className);
106+
System.out.printf("No test vectors for %s%n", score.getClass().getSimpleName());
71107
fail("Verification failed");
72108
}
73-
System.out.printf("Verify %s with %d test vectors%n", className, testVectors.size());
109+
System.out.printf("Verify %s with %d test vectors%n", score.getClass().getSimpleName(),
110+
testVectors.size());
74111
new ScoreVerification(score, testVectors).run();
75112
} catch (VerificationFailedException e) {
76-
System.out.printf("Verification failed for %s%n", className);
113+
System.out.printf("Verification failed for %s%n", score.getClass().getSimpleName());
77114
fail("Verification failed");
78115
} catch (Exception e) {
79-
System.out.printf("Couldn't verify %s%n", className);
116+
System.out.printf("Couldn't verify %s%n", score.getClass().getSimpleName());
80117
e.printStackTrace(System.out);
81118
fail("Verification failed");
82119
}
83120
}
121+
122+
protected String substituteYearsAndDaysPlaceholder(String input) {
123+
String output = substitutePlaceholder(input, TimeUnit.YEARS);
124+
output = substitutePlaceholder(output, TimeUnit.DAYS);
125+
return output;
126+
}
127+
128+
protected String substitutePlaceholder(String input, TimeUnit timeUnit) {
129+
Pattern pattern = Pattern.compile(timeUnit.getRegex());
130+
Matcher matcher = pattern.matcher(input);
131+
132+
LocalDate currentDate = LocalDate.now();
133+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
134+
135+
StringBuilder buf = new StringBuilder();
136+
while (matcher.find()) {
137+
LocalDate newDate = currentDate;
138+
int timeUnitToSubtract = Integer.parseInt(matcher.group(1));
139+
switch (timeUnit) {
140+
case DAYS:
141+
newDate = currentDate.minusDays(timeUnitToSubtract);
142+
break;
143+
case YEARS:
144+
newDate = currentDate.minusYears(timeUnitToSubtract);
145+
break;
146+
default:
147+
break;
148+
}
149+
String replacement = newDate.format(formatter);
150+
matcher.appendReplacement(buf, replacement);
151+
}
152+
matcher.appendTail(buf);
153+
return buf.toString();
154+
}
155+
156+
public enum TimeUnit {
157+
DAYS("\\$CURDATE\\{MINUS_DAYS\\((\\d+)\\)\\}"),
158+
YEARS("\\$CURDATE\\{MINUS_YEARS\\((\\d+)\\)\\}");
159+
160+
private final String regex;
161+
162+
TimeUnit(String regex) {
163+
this.regex = regex;
164+
}
165+
166+
public String getRegex() {
167+
return regex;
168+
}
169+
}
84170
}

src/test/resources/com/sap/oss/phosphor/fosstars/model/score/oss/SecurityReviewScoreTestVectorsTimeDependent.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ elements:
1515
organization:
1616
type: "GitHubOrganization"
1717
name: "org"
18-
date: "2024-01-01"
18+
date: "$CURDATE{MINUS_DAYS(100)}"
1919
expectedScore:
2020
type: "DoubleInterval"
2121
from: 9.5
@@ -40,7 +40,7 @@ elements:
4040
organization:
4141
type: "GitHubOrganization"
4242
name: "org"
43-
date: "2023-01-01"
43+
date: "$CURDATE{MINUS_YEARS(2)}"
4444
expectedScore:
4545
type: "DoubleInterval"
4646
from: 2.0

src/test/resources/com/sap/oss/phosphor/fosstars/model/score/oss/VulnerabilityDiscoveryAndSecurityTestingScoreTestVectorsTimeDependent.yml

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ elements:
2929
resolution: "UNKNOWN"
3030
introduced: null
3131
fixed: null
32-
published: "2018-03-03"
32+
published: "$CURDATE{MINUS_YEARS(6)}"
3333
- id: "CVE-02"
3434
cvss: null
3535
references: [ ]
3636
resolution: "UNKNOWN"
3737
introduced: null
3838
fixed: null
39-
published: "2020-10-28"
39+
published: "$CURDATE{MINUS_YEARS(4)}"
4040
feature:
4141
type: "VulnerabilitiesFeature"
4242
name: "Info about vulnerabilities in open-source project"
@@ -156,14 +156,14 @@ elements:
156156
resolution: "UNKNOWN"
157157
introduced: null
158158
fixed: null
159-
published: "2018-03-03"
159+
published: "$CURDATE{MINUS_YEARS(6)}"
160160
- id: "CVE-02"
161161
cvss: null
162162
references: [ ]
163163
resolution: "UNKNOWN"
164164
introduced: null
165165
fixed: null
166-
published: "2020-10-28"
166+
published: "$CURDATE{MINUS_YEARS(4)}"
167167
feature:
168168
type: "VulnerabilitiesFeature"
169169
name: "Info about vulnerabilities in open-source project"
@@ -188,14 +188,14 @@ elements:
188188
resolution: "UNKNOWN"
189189
introduced: null
190190
fixed: null
191-
published: "2021-03-03"
191+
published: "$CURDATE{MINUS_YEARS(3)}"
192192
- id: "CVE-02"
193193
cvss: null
194194
references: [ ]
195195
resolution: "UNKNOWN"
196196
introduced: null
197197
fixed: null
198-
published: "2024-10-28"
198+
published: "$CURDATE{MINUS_DAYS(100)}"
199199
feature:
200200
type: "VulnerabilitiesFeature"
201201
name: "Info about vulnerabilities in open-source project"
@@ -227,7 +227,7 @@ elements:
227227
resolution: "UNKNOWN"
228228
introduced: null
229229
fixed: null
230-
published: "2018-03-03"
230+
published: "$CURDATE{MINUS_YEARS(6)}"
231231
feature:
232232
type: "VulnerabilitiesFeature"
233233
name: "Info about vulnerabilities in open-source project"
@@ -259,14 +259,14 @@ elements:
259259
resolution: "UNKNOWN"
260260
introduced: null
261261
fixed: null
262-
published: "2020-03-03"
262+
published: "$CURDATE{MINUS_YEARS(4)}"
263263
- id: "CVE-02"
264264
cvss: null
265265
references: [ ]
266266
resolution: "UNKNOWN"
267267
introduced: null
268268
fixed: null
269-
published: "2024-10-28"
269+
published: "$CURDATE{MINUS_DAYS(100)}"
270270
feature:
271271
type: "VulnerabilitiesFeature"
272272
name: "Info about vulnerabilities in open-source project"
@@ -298,7 +298,7 @@ elements:
298298
resolution: "UNKNOWN"
299299
introduced: null
300300
fixed: null
301-
published: "2018-03-03"
301+
published: "$CURDATE{MINUS_YEARS(6)}"
302302
feature:
303303
type: "VulnerabilitiesFeature"
304304
name: "Info about vulnerabilities in open-source project"

0 commit comments

Comments
 (0)