From 645bf6615ff492331ee09594fcbcde413fadb449 Mon Sep 17 00:00:00 2001 From: Ewan Cahen Date: Thu, 1 Aug 2024 15:22:33 +0200 Subject: [PATCH] chore: implement SonarCloud suggestions --- authentication/pom.xml | 14 +++ .../rsd/authentication/AzureLogin.java | 20 +---- .../rsd/authentication/Config.java | 15 ++-- .../rsd/authentication/HelmholtzIdLogin.java | 10 +-- .../rsd/authentication/JwtCreator.java | 36 ++++---- .../rsd/authentication/Login.java | 6 +- .../rsd/authentication/Main.java | 21 +++-- .../rsd/authentication/OrcidLogin.java | 20 +---- .../rsd/authentication/PostgrestAccount.java | 70 +++++++++------ .../authentication/PostgrestConnector.java | 7 +- .../authentication/RsdResponseException.java | 26 ++++++ .../rsd/authentication/SurfconextLogin.java | 20 +---- .../rsd/authentication/Utils.java | 19 +++- .../nl/esciencecenter/rsd/scraper/Config.java | 3 + .../nl/esciencecenter/rsd/scraper/Utils.java | 3 + .../rsd/scraper/git/GithubScraper.java | 4 +- .../rsd/scraper/git/GitlabScraper.java | 11 +-- .../esciencecenter/rsd/scraper/UtilsIT.java | 4 +- .../esciencecenter/rsd/scraper/UtilsTest.java | 2 +- .../doi/DataciteMentionRepositoryTest.java | 4 +- .../rsd/scraper/doi/MainMentionsTest.java | 2 +- .../scraper/doi/OpenAlexConnectorTest.java | 2 +- .../rsd/scraper/git/CommitsPerWeekTest.java | 22 +++-- .../rsd/scraper/git/GithubScraperIT.java | 2 +- .../rsd/scraper/git/GithubScraperTest.java | 2 +- .../rsd/scraper/git/GitlabScraperIT.java | 2 +- .../scraper/git/PostgrestConnectorTest.java | 86 +++++++++---------- .../scrapers/CratesScraperTest.java | 10 +-- .../scrapers/GoScraperTest.java | 14 +-- .../scrapers/MavenScraperTest.java | 30 +++---- 30 files changed, 269 insertions(+), 218 deletions(-) create mode 100644 authentication/src/main/java/nl/esciencecenter/rsd/authentication/RsdResponseException.java diff --git a/authentication/pom.xml b/authentication/pom.xml index ea84309e7..418113192 100644 --- a/authentication/pom.xml +++ b/authentication/pom.xml @@ -139,6 +139,20 @@ SPDX-License-Identifier: Apache-2.0 11.10.1 + + + org.slf4j + slf4j-api + 2.0.13 + + + + + ch.qos.logback + logback-classic + 1.5.6 + + org.junit.jupiter diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/AzureLogin.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/AzureLogin.java index 4e91ed43f..dddf41c67 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/AzureLogin.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/AzureLogin.java @@ -13,9 +13,6 @@ import java.io.IOException; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -38,7 +35,7 @@ public AzureLogin(String code, String redirectUrl) { } @Override - public OpenIdInfo openidInfo() throws IOException, InterruptedException { + public OpenIdInfo openidInfo() throws IOException, InterruptedException, RsdResponseException { Map form = createForm(); String tokenResponse = getTokensFromAzureconext(form); String idToken = extractIdToken(tokenResponse); @@ -61,23 +58,12 @@ private Map createForm() { return form; } - private String getTokensFromAzureconext(Map form) throws IOException, InterruptedException { + private String getTokensFromAzureconext(Map form) throws IOException, InterruptedException, RsdResponseException { URI tokenEndpoint = Utils.getTokenUrlFromWellKnownUrl(URI.create(Config.azureWellknown())); - return postForm(tokenEndpoint, form); + return Utils.postForm(tokenEndpoint, form); } private String extractIdToken(String response) { return JsonParser.parseString(response).getAsJsonObject().getAsJsonPrimitive("id_token").getAsString(); } - - private String postForm(URI uri, Map form) throws IOException, InterruptedException { - HttpRequest request = Utils.formToHttpRequest(uri, form); - try (HttpClient client = HttpClient.newHttpClient()) { - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() >= 300) { - throw new RuntimeException("Error fetching data from " + uri.toString() + ": " + response.body()); - } - return response.body(); - } - } } diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Config.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Config.java index 216334eb9..c0b56b167 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Config.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Config.java @@ -1,6 +1,6 @@ -// SPDX-FileCopyrightText: 2022 - 2023 Ewan Cahen (Netherlands eScience Center) -// SPDX-FileCopyrightText: 2022 - 2023 Netherlands eScience Center +// SPDX-FileCopyrightText: 2022 - 2024 Ewan Cahen (Netherlands eScience Center) // SPDX-FileCopyrightText: 2022 - 2024 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences +// SPDX-FileCopyrightText: 2022 - 2024 Netherlands eScience Center // SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all) // SPDX-FileCopyrightText: 2022 Matthias Rüster (GFZ) // SPDX-FileCopyrightText: 2022 dv4all @@ -21,6 +21,9 @@ public static String jwtSigningSecret() { return System.getenv("PGRST_JWT_SECRET"); } + private Config() { + } + private static Collection rsdAuthCoupleProviders() { return Optional.ofNullable(System.getenv("RSD_AUTH_COUPLE_PROVIDERS")) .map(String::toUpperCase) @@ -39,10 +42,10 @@ public static boolean isDevEnv() { private static Collection rsdLoginProviders() { return Optional.ofNullable(System.getenv("RSD_AUTH_PROVIDERS")) - .map(String::toUpperCase) - .map(s -> s.split(";")) - .map(Set::of) - .orElse(Collections.emptySet()); + .map(String::toUpperCase) + .map(s -> s.split(";")) + .map(Set::of) + .orElse(Collections.emptySet()); } public static boolean isLocalLoginEnabled() { diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/HelmholtzIdLogin.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/HelmholtzIdLogin.java index 72ed9d0f2..0da8ed0a2 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/HelmholtzIdLogin.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/HelmholtzIdLogin.java @@ -1,6 +1,6 @@ -// SPDX-FileCopyrightText: 2022 - 2023 Ewan Cahen (Netherlands eScience Center) -// SPDX-FileCopyrightText: 2022 - 2023 Netherlands eScience Center +// SPDX-FileCopyrightText: 2022 - 2024 Ewan Cahen (Netherlands eScience Center) // SPDX-FileCopyrightText: 2022 - 2024 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences +// SPDX-FileCopyrightText: 2022 - 2024 Netherlands eScience Center // SPDX-FileCopyrightText: 2022 Matthias Rüster (GFZ) // SPDX-FileCopyrightText: 2023 - 2024 Christian Meeßen (GFZ) // @@ -50,7 +50,7 @@ public class HelmholtzIdLogin implements Login { static final String DEFAULT_ORGANISATION = "Helmholtz"; // See https://hifis.net/doc/helmholtz-aai/list-of-vos/#vos-representing-helmholtz-centres - static private final Collection knownHgfOrganisations = Set.of( + private static final Collection knownHgfOrganisations = Set.of( "AWI", "CISPA", "DESY", "DKFZ", "DLR", "DZNE", "FZJ", "GEOMAR", "GFZ", "GSI", "hereon", "HMGU", "HZB", "KIT", "MDC", "UFZ" ); @@ -198,8 +198,8 @@ public OpenIdInfo openidInfo() throws IOException, InterruptedException { JSONArray entitlements = new JSONArray(); Object edupersonClaim = userInfo.getClaim("eduperson_entitlement"); - if (edupersonClaim instanceof JSONArray) { - entitlements = (JSONArray) edupersonClaim; + if (edupersonClaim instanceof JSONArray jsonArray) { + entitlements = jsonArray; } else if (edupersonClaim instanceof String) { entitlements.appendElement(edupersonClaim); } else if (edupersonClaim == null) { diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/JwtCreator.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/JwtCreator.java index c600a3379..56d3ad08a 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/JwtCreator.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/JwtCreator.java @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2022 - 2023 Ewan Cahen (Netherlands eScience Center) -// SPDX-FileCopyrightText: 2022 - 2023 Netherlands eScience Center +// SPDX-FileCopyrightText: 2022 - 2024 Ewan Cahen (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2022 - 2024 Netherlands eScience Center // SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all) // SPDX-FileCopyrightText: 2022 dv4all // SPDX-FileCopyrightText: 2024 Christian Meeßen (GFZ) @@ -25,29 +25,29 @@ public class JwtCreator { private final Algorithm signingAlgorithm; public JwtCreator(String signingSecret) { - signingSecret = Objects.requireNonNull(signingSecret); + Objects.requireNonNull(signingSecret); this.signingSecret = signingSecret; this.signingAlgorithm = Algorithm.HMAC256(this.signingSecret); } String createUserJwt(AccountInfo accountInfo) { return JWT.create() - .withClaim("iss", "rsd_auth") - .withClaim("role", accountInfo.isAdmin() ? "rsd_admin" : "rsd_user") - .withClaim("account", accountInfo.account().toString()) - .withClaim("name", accountInfo.name()) - .withClaim("data", accountInfo.data()) - .withExpiresAt(new Date(System.currentTimeMillis() + ONE_HOUR_IN_MILLISECONDS)) - .sign(signingAlgorithm); + .withClaim("iss", "rsd_auth") + .withClaim("role", accountInfo.isAdmin() ? "rsd_admin" : "rsd_user") + .withClaim("account", accountInfo.account().toString()) + .withClaim("name", accountInfo.name()) + .withClaim("data", accountInfo.data()) + .withExpiresAt(new Date(System.currentTimeMillis() + ONE_HOUR_IN_MILLISECONDS)) + .sign(signingAlgorithm); } String createAdminJwt() { return JWT.create() - .withClaim("iss", "rsd_auth") - .withClaim("role", "rsd_admin") - .withExpiresAt(new Date(System.currentTimeMillis() + ONE_HOUR_IN_MILLISECONDS)) - .sign(signingAlgorithm); + .withClaim("iss", "rsd_auth") + .withClaim("role", "rsd_admin") + .withExpiresAt(new Date(System.currentTimeMillis() + ONE_HOUR_IN_MILLISECONDS)) + .sign(signingAlgorithm); } String refreshToken(String token) { @@ -55,10 +55,10 @@ String refreshToken(String token) { String payloadEncoded = oldJwt.getPayload(); String payloadDecoded = Main.decode(payloadEncoded); Gson gson = new Gson(); - Map claimsMap = gson.fromJson(payloadDecoded, Map.class); + Map claimsMap = gson.>fromJson(payloadDecoded, Map.class); return JWT.create() - .withPayload(claimsMap) - .withExpiresAt(new Date(System.currentTimeMillis() + ONE_HOUR_IN_MILLISECONDS)) - .sign(signingAlgorithm); + .withPayload(claimsMap) + .withExpiresAt(new Date(System.currentTimeMillis() + ONE_HOUR_IN_MILLISECONDS)) + .sign(signingAlgorithm); } } diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Login.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Login.java index 49a7bc8ea..318a893e0 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Login.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Login.java @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2021 - 2023 Ewan Cahen (Netherlands eScience Center) -// SPDX-FileCopyrightText: 2021 - 2023 Netherlands eScience Center +// SPDX-FileCopyrightText: 2021 - 2024 Ewan Cahen (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2021 - 2024 Netherlands eScience Center // // SPDX-License-Identifier: Apache-2.0 @@ -9,5 +9,5 @@ public interface Login { - OpenIdInfo openidInfo() throws IOException, InterruptedException; + OpenIdInfo openidInfo() throws IOException, InterruptedException, RsdResponseException; } diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Main.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Main.java index f673e1529..ff1ec555e 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Main.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Main.java @@ -14,6 +14,8 @@ import com.auth0.jwt.interfaces.DecodedJWT; import io.javalin.Javalin; import io.javalin.http.Context; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Base64; import java.util.Collections; @@ -25,6 +27,9 @@ public class Main { static final long ONE_HOUR_IN_SECONDS = 3600; // 60 * 60 static final long ONE_MINUTE_IN_SECONDS = 60; + private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); + private static final String LOGIN_FAILED_PATH = "/login/failed"; + public static boolean userIsAllowed(OpenIdInfo info) { String whitelist = Config.userMailWhitelist(); @@ -191,27 +196,33 @@ public static void main(String[] args) { String token = jwtCreator.refreshToken(tokenToVerify); setJwtCookie(ctx, token); } catch (RuntimeException ex) { - ex.printStackTrace(); + LOGGER.error("RuntimeException", ex); ctx.status(400); ctx.json("{\"message\": \"failed to refresh token\"}"); } }); app.exception(JWTVerificationException.class, (ex, ctx) -> { - ex.printStackTrace(); + LOGGER.error("JWTVerificationException", ex); ctx.status(400); ctx.json("{\"message\": \"invalid JWT\"}"); }); app.exception(RsdAuthenticationException.class, (ex, ctx) -> { setLoginFailureCookie(ctx, ex.getMessage()); - ctx.redirect("/login/failed"); + ctx.redirect(LOGIN_FAILED_PATH); }); app.exception(RuntimeException.class, (ex, ctx) -> { - ex.printStackTrace(); + LOGGER.error("RuntimeException", ex); + setLoginFailureCookie(ctx, "Something unexpected went wrong, please try again or contact us."); + ctx.redirect(LOGIN_FAILED_PATH); + }); + + app.exception(Exception.class, (ex, ctx) -> { + LOGGER.error("Exception", ex); setLoginFailureCookie(ctx, "Something unexpected went wrong, please try again or contact us."); - ctx.redirect("/login/failed"); + ctx.redirect(LOGIN_FAILED_PATH); }); } diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/OrcidLogin.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/OrcidLogin.java index eb6355993..366fa32f4 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/OrcidLogin.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/OrcidLogin.java @@ -13,9 +13,6 @@ import java.io.IOException; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -33,7 +30,7 @@ public OrcidLogin(String code, String redirectUrl) { } @Override - public OpenIdInfo openidInfo() throws IOException, InterruptedException { + public OpenIdInfo openidInfo() throws IOException, InterruptedException, RsdResponseException { Map form = createForm(); String tokenResponse = getTokensFromOrcidconext(form); String idToken = extractIdToken(tokenResponse); @@ -63,23 +60,12 @@ private Map createForm() { return form; } - private String getTokensFromOrcidconext(Map form) throws IOException, InterruptedException { + private String getTokensFromOrcidconext(Map form) throws IOException, InterruptedException, RsdResponseException { URI tokenEndpoint = Utils.getTokenUrlFromWellKnownUrl(URI.create(Config.orcidWellknown())); - return postForm(tokenEndpoint, form); + return Utils.postForm(tokenEndpoint, form); } private String extractIdToken(String response) { return JsonParser.parseString(response).getAsJsonObject().getAsJsonPrimitive("id_token").getAsString(); } - - private String postForm(URI uri, Map form) throws IOException, InterruptedException { - HttpRequest request = Utils.formToHttpRequest(uri, form); - try (HttpClient client = HttpClient.newHttpClient()) { - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() >= 300) { - throw new RuntimeException("Error fetching data from " + uri.toString() + ": " + response.body()); - } - return response.body(); - } - } } diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestAccount.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestAccount.java index c9c749197..4670dd859 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestAccount.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestAccount.java @@ -26,6 +26,10 @@ public class PostgrestAccount implements Account { + private static final String AUTHORIZATION_HEADER_KEY = "Authorization"; + private static final String BEARER_HEADER_VALUE_PREFIX = "bearer"; + private static final String PREFER_HEADER_KEY = "Prefer"; + @Override public AccountInfo account(OpenIdInfo openIdInfo, OpenidProvider provider) throws IOException, InterruptedException { Objects.requireNonNull(openIdInfo); @@ -57,10 +61,17 @@ else if (accountsWithSub.size() == 1) { String name = openIdInfo.name(); boolean isAdmin = accountInfo.getAsJsonObject("account").get("admin_account").isJsonObject() - && - accountInfo.getAsJsonObject("account").getAsJsonObject("admin_account").get("account_id").isJsonPrimitive() - && - accountInfo.getAsJsonObject("account").getAsJsonObject("admin_account").getAsJsonPrimitive("account_id").getAsString().equals(account.toString()); + && + accountInfo.getAsJsonObject("account") + .getAsJsonObject("admin_account") + .get("account_id") + .isJsonPrimitive() + && + accountInfo.getAsJsonObject("account") + .getAsJsonObject("admin_account") + .getAsJsonPrimitive("account_id") + .getAsString() + .equals(account.toString()); if (createAdminIfDevAndNoAdminsExist(backendUri, token, account)) { isAdmin = true; @@ -73,7 +84,12 @@ else if (accountsWithSub.size() == 1) { // create account URI createAccountEndpoint = URI.create(backendUri + "/account"); String newAccountJson = postJsonAsAdmin(createAccountEndpoint, "{}", token); - String newAccountId = JsonParser.parseString(newAccountJson).getAsJsonArray().get(0).getAsJsonObject().getAsJsonPrimitive("id").getAsString(); + String newAccountId = JsonParser.parseString(newAccountJson) + .getAsJsonArray() + .get(0) + .getAsJsonObject() + .getAsJsonPrimitive("id") + .getAsString(); UUID accountId = UUID.fromString(newAccountId); createLoginForAccount(accountId, openIdInfo, provider, backendUri, token); @@ -97,7 +113,7 @@ private boolean createAdminIfDevAndNoAdminsExist(String backendUri, String token } URI adminAccountUri = URI.create(backendUri + "/admin_account"); - HttpResponse countAdminResponse = headAsAdmin(adminAccountUri, token, "Prefer", "count=exact"); + HttpResponse countAdminResponse = headAsAdmin(adminAccountUri, token, PREFER_HEADER_KEY, "count=exact"); String contentRange = countAdminResponse.headers().firstValue("Content-Range").orElseThrow(); if (!"0".equals(contentRange.split("/")[1])) { @@ -118,7 +134,8 @@ private void createLoginForAccount(UUID accountId, OpenIdInfo openIdInfo, Openid loginForAccountData.addProperty("email", openIdInfo.email()); loginForAccountData.addProperty("home_organisation", openIdInfo.organisation()); loginForAccountData.addProperty("provider", provider.toString()); - loginForAccountData.addProperty("last_login_date", ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); + loginForAccountData.addProperty("last_login_date", ZonedDateTime.now() + .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); URI createLoginUri = URI.create(backendUri + "/login_for_account"); HttpResponse response = postJsonAsAdminWithResponse(createLoginUri, loginForAccountData.toString(), adminJwt); @@ -134,7 +151,8 @@ private void updateLoginForAccount(UUID id, OpenIdInfo openIdInfo, String authTo loginForAccountData.addProperty("name", openIdInfo.name()); loginForAccountData.addProperty("email", openIdInfo.email()); loginForAccountData.addProperty("home_organisation", openIdInfo.organisation()); - loginForAccountData.addProperty("last_login_date", ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); + loginForAccountData.addProperty("last_login_date", ZonedDateTime.now() + .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); String resultingJson = loginForAccountData.toString(); String backendUri = Config.backendBaseUrl(); @@ -145,10 +163,10 @@ private void updateLoginForAccount(UUID id, OpenIdInfo openIdInfo, String authTo static String getAsAdmin(URI uri, String token) throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() - .GET() - .uri(uri) - .header("Authorization", "bearer " + token) - .build(); + .GET() + .uri(uri) + .header(AUTHORIZATION_HEADER_KEY, BEARER_HEADER_VALUE_PREFIX + " " + token) + .build(); try (HttpClient client = HttpClient.newHttpClient()) { HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() >= 300) { @@ -168,11 +186,11 @@ public static String postJsonAsAdmin(URI uri, String json, String token, String. public static HttpResponse postJsonAsAdminWithResponse(URI uri, String json, String token, String... headers) throws IOException, InterruptedException { HttpRequest.Builder httpRequestBuilder = HttpRequest.newBuilder() - .POST(HttpRequest.BodyPublishers.ofString(json)) - .uri(uri) - .header("Content-Type", "application/json") - .header("Prefer", "return=representation") - .header("Authorization", "bearer " + token); + .POST(HttpRequest.BodyPublishers.ofString(json)) + .uri(uri) + .header("Content-Type", "application/json") + .header(PREFER_HEADER_KEY, "return=representation") + .header(AUTHORIZATION_HEADER_KEY, BEARER_HEADER_VALUE_PREFIX + " " + token); if (headers != null && headers.length > 0 && headers.length % 2 == 0) { httpRequestBuilder.headers(headers); } @@ -184,12 +202,12 @@ public static HttpResponse postJsonAsAdminWithResponse(URI uri, String j private String patchJsonAsAdmin(URI uri, String json, String authToken) throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() - .method("PATCH", HttpRequest.BodyPublishers.ofString(json)) - .uri(uri) - .header("Content-Type", "application/json") - .header("Prefer", "return=representation") - .header("Authorization", "bearer " + authToken) - .build(); + .method("PATCH", HttpRequest.BodyPublishers.ofString(json)) + .uri(uri) + .header("Content-Type", "application/json") + .header(PREFER_HEADER_KEY, "return=representation") + .header(AUTHORIZATION_HEADER_KEY, BEARER_HEADER_VALUE_PREFIX + " " + authToken) + .build(); HttpResponse response; try (HttpClient client = HttpClient.newHttpClient()) { response = client.send(request, HttpResponse.BodyHandlers.ofString()); @@ -202,9 +220,9 @@ private String patchJsonAsAdmin(URI uri, String json, String authToken) throws I private HttpResponse headAsAdmin(URI uri, String token, String... headers) throws IOException, InterruptedException { HttpRequest.Builder httpRequestBuilder = HttpRequest.newBuilder() - .HEAD() - .uri(uri) - .header("Authorization", "bearer " + token); + .HEAD() + .uri(uri) + .header(AUTHORIZATION_HEADER_KEY, BEARER_HEADER_VALUE_PREFIX + " " + token); if (headers != null && headers.length > 0 && headers.length % 2 == 0) { httpRequestBuilder.headers(headers); } diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestConnector.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestConnector.java index e09b6d8c5..63fa50fb2 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestConnector.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/PostgrestConnector.java @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Ewan Cahen (Netherlands eScience Center) -// SPDX-FileCopyrightText: 2023 Netherlands eScience Center +// SPDX-FileCopyrightText: 2023 - 2024 Ewan Cahen (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2023 - 2024 Netherlands eScience Center // // SPDX-License-Identifier: Apache-2.0 @@ -12,6 +12,9 @@ public class PostgrestConnector { + private PostgrestConnector() { + } + public static void addOrcidToAllowList(String orcid) throws IOException, InterruptedException { String backendUri = Config.backendBaseUrl(); URI allowListUri = URI.create(backendUri + "/orcid_whitelist"); diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/RsdResponseException.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/RsdResponseException.java new file mode 100644 index 000000000..48e42c3d5 --- /dev/null +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/RsdResponseException.java @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2022 - 2024 Ewan Cahen (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2022 - 2024 Netherlands eScience Center +// SPDX-FileCopyrightText: 2022 Christian Meeßen (GFZ) +// SPDX-FileCopyrightText: 2022 Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences +// +// SPDX-License-Identifier: Apache-2.0 + +package nl.esciencecenter.rsd.authentication; + +import java.net.URI; + +public class RsdResponseException extends Exception { + + private static final long serialVersionUID = 1L; + + public final int statusCode; + public final URI uri; + public final String body; + + public RsdResponseException(int statusCode, URI uri, String body, String message) { + super(message); + this.statusCode = statusCode; + this.uri = uri; + this.body = body; + } +} diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/SurfconextLogin.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/SurfconextLogin.java index df286b39f..8f5c6ef90 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/SurfconextLogin.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/SurfconextLogin.java @@ -16,9 +16,6 @@ import java.io.IOException; import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -36,7 +33,7 @@ public SurfconextLogin(String code, String redirectUrl) { } @Override - public OpenIdInfo openidInfo() throws IOException, InterruptedException { + public OpenIdInfo openidInfo() throws IOException, InterruptedException, RsdResponseException { Map form = createForm(); String tokenResponse = getTokensFromSurfconext(form); String idToken = extractIdToken(tokenResponse); @@ -61,23 +58,12 @@ private Map createForm() { return form; } - private String getTokensFromSurfconext(Map form) throws IOException, InterruptedException { + private String getTokensFromSurfconext(Map form) throws IOException, InterruptedException, RsdResponseException { URI tokenEndpoint = Utils.getTokenUrlFromWellKnownUrl(URI.create(Config.surfconextWellknown())); - return postForm(tokenEndpoint, form); + return Utils.postForm(tokenEndpoint, form); } private String extractIdToken(String response) { return JsonParser.parseString(response).getAsJsonObject().getAsJsonPrimitive("id_token").getAsString(); } - - private String postForm(URI uri, Map form) throws IOException, InterruptedException { - HttpRequest request = Utils.formToHttpRequest(uri, form); - try (HttpClient client = HttpClient.newHttpClient()) { - HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() >= 300) { - throw new RuntimeException("Error fetching data from " + uri.toString() + ": " + response.body()); - } - return response.body(); - } - } } diff --git a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Utils.java b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Utils.java index f55a9b8d5..efe8e9811 100644 --- a/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Utils.java +++ b/authentication/src/main/java/nl/esciencecenter/rsd/authentication/Utils.java @@ -47,9 +47,20 @@ public static HttpRequest formToHttpRequest(URI uri, Map form) { String body = formMapToxWwwFormUrlencoded(form); return HttpRequest.newBuilder() - .POST(HttpRequest.BodyPublishers.ofString(body)) - .uri(uri) - .header("Content-Type", "application/x-www-form-urlencoded") - .build(); + .POST(HttpRequest.BodyPublishers.ofString(body)) + .uri(uri) + .header("Content-Type", "application/x-www-form-urlencoded") + .build(); + } + + public static String postForm(URI uri, Map form) throws IOException, InterruptedException, RsdResponseException { + HttpRequest request = formToHttpRequest(uri, form); + try (HttpClient client = HttpClient.newHttpClient()) { + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() >= 300) { + throw new RsdResponseException(response.statusCode(), response.uri(), response.body(), "Error posting form"); + } + return response.body(); + } } } diff --git a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Config.java b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Config.java index 41165483e..9b3c473da 100644 --- a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Config.java +++ b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Config.java @@ -23,6 +23,9 @@ public class Config { private static final Logger LOGGER = LoggerFactory.getLogger(Config.class); + private Config() { + } + /** * Retrieves the value of the environment variable with the given name. Variable is not allowed to unset, and an Error * will be thrown if this is the case. diff --git a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Utils.java b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Utils.java index e535faabc..5124dbbfb 100644 --- a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Utils.java +++ b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/Utils.java @@ -40,6 +40,9 @@ public class Utils { // Default timeout used for http connections. private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30); + private Utils() { + } + /** * Base64encode a string. * diff --git a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GithubScraper.java b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GithubScraper.java index a15a45e1e..428c0f3b0 100644 --- a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GithubScraper.java +++ b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GithubScraper.java @@ -27,11 +27,11 @@ public class GithubScraper implements GitScraper { - private final String BASE_API_URL = "https://api.github.com"; + private static final String BASE_API_URL = "https://api.github.com"; public final String organisation; public final String repo; private static final Pattern LINK_PATTERN = Pattern.compile("<([^>]+page=(\\d+)[^>]*)>; rel=\"([^\"]+)\""); - public static final Pattern GITHUB_URL_PATTERN = Pattern.compile("^https?://github\\.com/([^\\s/]+)/([^\\s/]+)/?$"); + private static final Pattern GITHUB_URL_PATTERN = Pattern.compile("^https?://github\\.com/([^\\s/]+)/([^\\s/]+)/?$"); private GithubScraper(String organisation, String repo) { this.organisation = Objects.requireNonNull(organisation); diff --git a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GitlabScraper.java b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GitlabScraper.java index 9388ff98b..069412667 100644 --- a/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GitlabScraper.java +++ b/scrapers/src/main/java/nl/esciencecenter/rsd/scraper/git/GitlabScraper.java @@ -24,20 +24,17 @@ import java.time.ZonedDateTime; public class GitlabScraper implements GitScraper { - public String projectPath; - public String apiUri; + private final String projectPath; + private final String apiUri; /** * A GitLab scraper for API version 4. * - * @param gitLabApiUrl The API Url without version, e.g. https:///api + * @param gitLabApiUrl The API Url without a version, e.g. https:///api * @param projectPath The full path to the project */ public GitlabScraper(String gitLabApiUrl, String projectPath) { - this.projectPath = projectPath; - if (this.projectPath.endsWith(".git")) { - this.projectPath = this.projectPath.substring(0, this.projectPath.length() - 4); - } + this.projectPath = projectPath.endsWith(".git") ? projectPath.substring(0, projectPath.length() - 4) : projectPath; this.apiUri = gitLabApiUrl + "/v4"; } diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsIT.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsIT.java index 9ad12e8f9..864378f0b 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsIT.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsIT.java @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -public class UtilsIT { +class UtilsIT { private final String gitHubApiUrl = "https://api.github.com"; private final String gitHubRepository = "research-software-directory/RSD-as-a-service"; @@ -28,7 +28,7 @@ void getWithoutToken() { void getWrongUri() { final String wrongUri = gitHubApiUrl + "/repos/research-software-directory/wrongRepo"; Exception thrown = Assertions.assertThrows( - Exception.class, () -> Utils.get(wrongUri) + Exception.class, () -> Utils.get(wrongUri) ); Assertions.assertTrue(thrown.getMessage().startsWith("Error fetching data")); } diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsTest.java index 8a9a71864..2d5be15ca 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/UtilsTest.java @@ -10,7 +10,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class UtilsTest { +class UtilsTest { @Test void urlEncode() { diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/DataciteMentionRepositoryTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/DataciteMentionRepositoryTest.java index 8441b4702..9a814defe 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/DataciteMentionRepositoryTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/DataciteMentionRepositoryTest.java @@ -11,10 +11,10 @@ import java.util.Collection; import java.util.List; -public class DataciteMentionRepositoryTest { +class DataciteMentionRepositoryTest { @Test - public void givenCollectionOfStrings_whenJoining_thenCorrectStringReturned() { + void givenCollectionOfStrings_whenJoining_thenCorrectStringReturned() { Doi doi1 = Doi.fromString("10.000/1"); Doi doi2 = Doi.fromString("10.2/2"); Doi doi3 = Doi.fromString("10.3/abc-def"); diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/MainMentionsTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/MainMentionsTest.java index ac8c116df..3eb6e1bb3 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/MainMentionsTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/MainMentionsTest.java @@ -10,7 +10,7 @@ import java.util.Map; -public class MainMentionsTest { +class MainMentionsTest { @Test diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/OpenAlexConnectorTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/OpenAlexConnectorTest.java index 730fc1395..fab34b536 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/OpenAlexConnectorTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/doi/OpenAlexConnectorTest.java @@ -14,7 +14,7 @@ import java.util.Collection; import java.util.Collections; -public class OpenAlexConnectorTest { +class OpenAlexConnectorTest { @Test void givenLocationWithBackSlashes_whenExtractedAsLocation_thenSlashesUrlEncoded() { diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/CommitsPerWeekTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/CommitsPerWeekTest.java index 7763960f1..2c823d2e1 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/CommitsPerWeekTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/CommitsPerWeekTest.java @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2023 Ewan Cahen (Netherlands eScience Center) -// SPDX-FileCopyrightText: 2023 Netherlands eScience Center +// SPDX-FileCopyrightText: 2023 - 2024 Ewan Cahen (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2023 - 2024 Netherlands eScience Center // // SPDX-License-Identifier: Apache-2.0 @@ -17,7 +17,7 @@ import java.time.Period; import java.util.Map; -public class CommitsPerWeekTest { +class CommitsPerWeekTest { @Test void givenInstance_whenValidOperations_thenCorrectResults() { @@ -39,7 +39,8 @@ void givenInstance_whenValidOperations_thenCorrectResults() { Assertions.assertEquals(30, underlyingMap.get(sundayMidnight1)); - Instant smallTimeAfterSundayMidnight1 = sundayMidnight1.plus(Duration.ofDays(3)).plus(Duration.ofSeconds(12345)); + Instant smallTimeAfterSundayMidnight1 = sundayMidnight1.plus(Duration.ofDays(3)) + .plus(Duration.ofSeconds(12345)); commitsPerWeek.addCommits(smallTimeAfterSundayMidnight1, 10); Assertions.assertEquals(1, underlyingMap.size()); @@ -70,9 +71,10 @@ void givenInstance_whenValidOperations_thenCorrectResults() { @Test void givenInstance_whenValidOperations_thenCorrectJsonProduced() { Gson gson = new GsonBuilder() - .registerTypeAdapter(Instant.class, (JsonDeserializer) (json, typeOfT, context) -> Instant.ofEpochSecond(Long.parseLong(json.getAsString()))) - .create(); - TypeToken> mapTypeToken = new TypeToken<>(){}; + .registerTypeAdapter(Instant.class, (JsonDeserializer) (json, typeOfT, context) -> Instant.ofEpochSecond(Long.parseLong(json.getAsString()))) + .create(); + TypeToken> mapTypeToken = new TypeToken<>() { + }; CommitsPerWeek commitsPerWeek = new CommitsPerWeek(); Map shouldBeEmptyMap = gson.fromJson(commitsPerWeek.toJson(), mapTypeToken.getType()); @@ -82,7 +84,8 @@ void givenInstance_whenValidOperations_thenCorrectJsonProduced() { commitsPerWeek.addCommits(sundayMidnight1, 10); commitsPerWeek.addCommits(sundayMidnight1, 20); - Instant smallTimeAfterSundayMidnight1 = sundayMidnight1.plus(Duration.ofDays(3)).plus(Duration.ofSeconds(12345)); + Instant smallTimeAfterSundayMidnight1 = sundayMidnight1.plus(Duration.ofDays(3)) + .plus(Duration.ofSeconds(12345)); commitsPerWeek.addCommits(smallTimeAfterSundayMidnight1, 10); Instant sundayMidnight2 = sundayMidnight1.plus(Period.ofWeeks(5)); @@ -95,7 +98,8 @@ void givenInstance_whenValidOperations_thenCorrectJsonProduced() { Assertions.assertEquals(5, dataFromJson.get(sundayMidnight2)); commitsPerWeek.addMissingZeros(); - dataFromJson = gson.fromJson(commitsPerWeek.toJson(), new TypeToken>(){}.getType()); + dataFromJson = gson.fromJson(commitsPerWeek.toJson(), new TypeToken>() { + }.getType()); Assertions.assertEquals(6, dataFromJson.size()); Assertions.assertEquals(0, dataFromJson.get(sundayMidnight1.plus(Period.ofWeeks(1)))); Assertions.assertEquals(0, dataFromJson.get(sundayMidnight1.plus(Period.ofWeeks(2)))); diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperIT.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperIT.java index 114b12e92..12f127581 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperIT.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperIT.java @@ -11,7 +11,7 @@ import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -public class GithubScraperIT { +class GithubScraperIT { private final String githubUrlPrefix = "https://github.com/"; private final String repo = "research-software-directory/RSD-as-a-service"; diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperTest.java index c84d32de9..52af1d59e 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GithubScraperTest.java @@ -13,7 +13,7 @@ import java.util.List; import java.util.Optional; -public class GithubScraperTest { +class GithubScraperTest { private final String githubUrlPrefix = "https://github.com/"; private final String repo = "research-software-directory/RSD-as-a-service"; diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GitlabScraperIT.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GitlabScraperIT.java index a4a49932e..f3e4b5fa2 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GitlabScraperIT.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/GitlabScraperIT.java @@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test; -public class GitlabScraperIT { +class GitlabScraperIT { private final String baseApiUrl = "https://git.gfz-potsdam.de/api"; private final String repo = "swc-bb/swc-templates/swc-l/python"; private final GitlabScraper scraper = new GitlabScraper(baseApiUrl, repo); diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/PostgrestConnectorTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/PostgrestConnectorTest.java index 59eea5979..4a53d3c57 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/PostgrestConnectorTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/git/PostgrestConnectorTest.java @@ -1,5 +1,5 @@ -// SPDX-FileCopyrightText: 2022 - 2023 Ewan Cahen (Netherlands eScience Center) -// SPDX-FileCopyrightText: 2022 - 2023 Netherlands eScience Center +// SPDX-FileCopyrightText: 2022 - 2024 Ewan Cahen (Netherlands eScience Center) +// SPDX-FileCopyrightText: 2022 - 2024 Netherlands eScience Center // // SPDX-License-Identifier: Apache-2.0 @@ -11,7 +11,7 @@ import java.util.Collection; import java.util.UUID; -public class PostgrestConnectorTest { +class PostgrestConnectorTest { @Test void givenEmptyJsonArray_whenParsingJsonData_thenEmptyCollectionReturned() { @@ -25,12 +25,12 @@ void givenEmptyJsonArray_whenParsingJsonData_thenEmptyCollectionReturned() { @Test void givenValidJson_whenParsingJsonData_thenRecordWithDataReturned() { String arrayWithSingleValidObjectJson = """ - [ - { - "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf", - "url": "https://www.example.com" - } - ]"""; + [ + { + "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf", + "url": "https://www.example.com" + } + ]"""; Collection result = PostgrestConnector.parseBasicJsonData(arrayWithSingleValidObjectJson); @@ -43,12 +43,12 @@ void givenValidJson_whenParsingJsonData_thenRecordWithDataReturned() { @Test void givenJsonWithInvalidUuid_whenParsingJsonData_thenExceptionThrown() { String invalidUuidJson = """ - [ - { - "software": "not-a-UUID", - "url": "https://www.example.com" - } - ]"""; + [ + { + "software": "not-a-UUID", + "url": "https://www.example.com" + } + ]"""; Assertions.assertThrowsExactly(IllegalArgumentException.class, () -> PostgrestConnector.parseBasicJsonData(invalidUuidJson)); } @@ -57,12 +57,12 @@ void givenJsonWithInvalidUuid_whenParsingJsonData_thenExceptionThrown() { void givenInvalidJson_whenParsingJsonData_thenExceptionThrown() { // missing comma after software UUID String invalidJson = """ - [ - { - "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf" - "url": "https://www.example.com" - } - ]"""; + [ + { + "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf" + "url": "https://www.example.com" + } + ]"""; Assertions.assertThrows(RuntimeException.class, () -> PostgrestConnector.parseBasicJsonData(invalidJson)); } @@ -70,20 +70,20 @@ void givenInvalidJson_whenParsingJsonData_thenExceptionThrown() { @Test void givenValidJsonWithMissingFields_whenParsingJsonData_thenExceptionThrown() { String missingSoftwareJson = """ - [ - { - "url": "https://www.example.com" - } - ]"""; + [ + { + "url": "https://www.example.com" + } + ]"""; Assertions.assertThrows(RuntimeException.class, () -> PostgrestConnector.parseBasicJsonData(missingSoftwareJson)); String missingUrlJson = """ - [ - { - "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf" - } - ]"""; + [ + { + "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf" + } + ]"""; Assertions.assertThrows(RuntimeException.class, () -> PostgrestConnector.parseBasicJsonData(missingUrlJson)); } @@ -91,22 +91,22 @@ void givenValidJsonWithMissingFields_whenParsingJsonData_thenExceptionThrown() { @Test void givenValidJsonWithNullFields_whenParsingJsonData_thenExceptionThrown() { String nullSoftwareJson = """ - [ - { - "software": null, - "url": "https://www.example.com" - } - ]"""; + [ + { + "software": null, + "url": "https://www.example.com" + } + ]"""; Assertions.assertThrows(RuntimeException.class, () -> PostgrestConnector.parseBasicJsonData(nullSoftwareJson)); String nullUrlJson = """ - [ - { - "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf", - "url": null - } - ]"""; + [ + { + "software": "3a07a021-743e-4adf-a2d9-3c85075fe9cf", + "url": null + } + ]"""; Assertions.assertThrows(RuntimeException.class, () -> PostgrestConnector.parseBasicJsonData(nullUrlJson)); } diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/CratesScraperTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/CratesScraperTest.java index 4fad2a6eb..b0dacb029 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/CratesScraperTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/CratesScraperTest.java @@ -10,16 +10,16 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.NullSource; -public class CratesScraperTest { +class CratesScraperTest { @ParameterizedTest @CsvSource({ - "https://crates.io/crates/tokio,tokio", - "https://crates.io/crates/tokio/,tokio", + "https://crates.io/crates/tokio,tokio", + "https://crates.io/crates/tokio/,tokio", }) void givenValidCratesUrl_whenCallingConstructor_thenNoExceptionThrownAndPackageNamesCorrect( - String url, - String expectedPackageName + String url, + String expectedPackageName ) { CratesScraper cratesScraper = Assertions.assertDoesNotThrow(() -> new CratesScraper(url)); Assertions.assertEquals(expectedPackageName, cratesScraper.packageName); diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/GoScraperTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/GoScraperTest.java index 3ebd07b0f..452c671f7 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/GoScraperTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/GoScraperTest.java @@ -10,18 +10,18 @@ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.NullSource; -public class GoScraperTest { +class GoScraperTest { @ParameterizedTest @CsvSource({ - "https://pkg.go.dev/github.com/gin-gonic/gin,github.com/gin-gonic/gin", - "https://pkg.go.dev/github.com/gin-gonic/gin/,github.com/gin-gonic/gin", - "https://pkg.go.dev/google.golang.org/grpc,google.golang.org/grpc", - "https://pkg.go.dev/google.golang.org/grpc/,google.golang.org/grpc", + "https://pkg.go.dev/github.com/gin-gonic/gin,github.com/gin-gonic/gin", + "https://pkg.go.dev/github.com/gin-gonic/gin/,github.com/gin-gonic/gin", + "https://pkg.go.dev/google.golang.org/grpc,google.golang.org/grpc", + "https://pkg.go.dev/google.golang.org/grpc/,google.golang.org/grpc", }) void givenValidGoUrl_whenCallingConstructor_thenNoExceptionThrownAndPackageNameCorrect( - String url, - String expectedPackageName + String url, + String expectedPackageName ) { GoScraper goScraper = Assertions.assertDoesNotThrow(() -> new GoScraper(url)); Assertions.assertEquals(expectedPackageName, goScraper.packageName); diff --git a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/MavenScraperTest.java b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/MavenScraperTest.java index a8bf54f00..2f7cf994f 100644 --- a/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/MavenScraperTest.java +++ b/scrapers/src/test/java/nl/esciencecenter/rsd/scraper/package_manager/scrapers/MavenScraperTest.java @@ -11,19 +11,19 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; -public class MavenScraperTest { +class MavenScraperTest { @ParameterizedTest @CsvSource({ - "https://central.sonatype.com/artifact/org.openscience.cdk/cdk-bundle,org.openscience.cdk,cdk-bundle", - "https://central.sonatype.com/artifact/org.openscience.cdk/cdk-bundle/,org.openscience.cdk,cdk-bundle", - "https://mvnrepository.com/artifact/io.github.sanctuuary/APE,io.github.sanctuuary,APE", - "https://mvnrepository.com/artifact/io.github.sanctuuary/APE/,io.github.sanctuuary,APE", + "https://central.sonatype.com/artifact/org.openscience.cdk/cdk-bundle,org.openscience.cdk,cdk-bundle", + "https://central.sonatype.com/artifact/org.openscience.cdk/cdk-bundle/,org.openscience.cdk,cdk-bundle", + "https://mvnrepository.com/artifact/io.github.sanctuuary/APE,io.github.sanctuuary,APE", + "https://mvnrepository.com/artifact/io.github.sanctuuary/APE/,io.github.sanctuuary,APE", }) void givenValidMavenOrSonatypeUrl_whenCallingConstructor_thenNoExceptionThrownAndPackageNameCorrect( - String url, - String expectedGroupId, - String expectedArtifactID + String url, + String expectedGroupId, + String expectedArtifactID ) { MavenScraper mavenScraper = Assertions.assertDoesNotThrow(() -> new MavenScraper(url)); Assertions.assertEquals(expectedGroupId, mavenScraper.groupId); @@ -32,13 +32,13 @@ void givenValidMavenOrSonatypeUrl_whenCallingConstructor_thenNoExceptionThrownAn @ParameterizedTest @ValueSource(strings = { - "https://central.sonatype.com/artifact", - "https://mvnrepository.com/artifact/", - "https://central.sonatype.com/artifact/org.openscience.cdk", - "https://mvnrepository.com/artifact/io.github.sanctuuary/", - "https://www.example.com", - "https://www.example.com/artifact/org.openscience.cdk/cdk-bundle", - "" + "https://central.sonatype.com/artifact", + "https://mvnrepository.com/artifact/", + "https://central.sonatype.com/artifact/org.openscience.cdk", + "https://mvnrepository.com/artifact/io.github.sanctuuary/", + "https://www.example.com", + "https://www.example.com/artifact/org.openscience.cdk/cdk-bundle", + "" }) void givenInvalidUrl_whenCallingConstructor_thenExceptionThrown(String url) { Assertions.assertThrowsExactly(RuntimeException.class, () -> new MavenScraper(url));