From b407146dd932e8c8f752e5a5de3f880ced2f803d Mon Sep 17 00:00:00 2001 From: Sahiba Mittal Date: Mon, 20 May 2024 10:02:46 +0100 Subject: [PATCH] add date format to support offset Signed-off-by: Sahiba Mittal --- .../tasks/repositories/NugetMetaAnalyzer.java | 3 ++- .../repositories/NugetMetaAnalyzerTest.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzer.java b/src/main/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzer.java index 4b7484a13..b8559741a 100644 --- a/src/main/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzer.java +++ b/src/main/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzer.java @@ -46,7 +46,8 @@ public class NugetMetaAnalyzer extends AbstractMetaAnalyzer { public static final DateFormat[] SUPPORTED_DATE_FORMATS = new DateFormat[]{ new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"), - new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss") }; private static final Logger LOGGER = Logger.getLogger(NugetMetaAnalyzer.class); private static final String DEFAULT_BASE_URL = "https://api.nuget.org"; diff --git a/src/test/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzerTest.java b/src/test/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzerTest.java index 5c9572230..e591d02f2 100644 --- a/src/test/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzerTest.java +++ b/src/test/java/org/dependencytrack/tasks/repositories/NugetMetaAnalyzerTest.java @@ -31,7 +31,12 @@ import java.nio.file.Files; import java.nio.file.Paths; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import static org.dependencytrack.tasks.repositories.NugetMetaAnalyzer.SUPPORTED_DATE_FORMATS; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; @@ -121,6 +126,19 @@ public void testAnalyzerWithPrivatePackageRepository() throws Exception { Assert.assertEquals("5.0.2", metaModel.getLatestVersion()); Assert.assertNotNull(metaModel.getPublishedTimestamp()); } + + @Test + public void testPublishedDateTimeFormat() throws ParseException { + Date dateParsed = null; + for (DateFormat dateFormat : SUPPORTED_DATE_FORMATS) { + try { + dateParsed = dateFormat.parse("1900-01-01T00:00:00+00:00"); + } catch (ParseException e) {} + } + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + Assert.assertEquals(dateFormat.parse("1900-01-01T00:00:00+00:00"), dateParsed); + } + private String readResourceFileToString(String fileName) throws Exception { return Files.readString(Paths.get(getClass().getResource(fileName).toURI())); }