From 09e71cd7ba29079eb52f8bedf148d7d05dc9cfe6 Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sun, 5 Jun 2022 10:49:20 +0300 Subject: [PATCH] Wrote some tests for LangCodes and fixed some issues in LocaleJSONResolver - Removed unnecessary regex that was marked as bug by sonar --- .../resolver/json/LocaleJSONResolver.java | 43 ++++++++------- .../delivery/webserver/AccessControlTest.java | 4 ++ .../plan/settings/locale/LangCodeTest.java | 55 +++++++++++++++++++ 3 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/settings/locale/LangCodeTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/LocaleJSONResolver.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/LocaleJSONResolver.java index 264fb435b..a8c2c9c54 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/LocaleJSONResolver.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/resolver/json/LocaleJSONResolver.java @@ -34,10 +34,7 @@ import javax.inject.Inject; import javax.inject.Singleton; import java.io.IOException; import java.io.UncheckedIOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.TreeMap; +import java.util.*; /** * Resolves JSON requests for /v1/locale and /v1/locale/{@link LangCode#toString()}. @@ -73,8 +70,10 @@ public class LocaleJSONResolver implements NoAuthResolver { private Response getResponse(Request request) { ResponseBuilder builder = Response.builder(); - String path = request.omitFirstInPath().getPath().asString().replaceAll("^/|/$", ""); - Map json = "".equals(path) ? getLanguageListJSON() : getLocaleJSON(path); + Optional langCode = request.getPath().getPart(1); + Map json = langCode + .map(this::getLocaleJSON) + .orElseGet(this::getLanguageListJSON); if (!json.isEmpty()) { return builder.setJSONContent(json).build(); @@ -112,23 +111,25 @@ public class LocaleJSONResolver implements NoAuthResolver { } private Map getLocaleJSON(String langCode) { - LangCode code = LangCode.valueOf(langCode.toUpperCase()); - Map json = new TreeMap<>(); - Resource file; - - if (code == LangCode.CUSTOM) { - if (locale.getLangCode() != LangCode.CUSTOM || !files.getFileFromPluginFolder("locale.yml").exists()) { - return json; - } - file = files.getResourceFromPluginFolder("locale.yml"); - } else { - file = files.getResourceFromJar("locale/" + code.getFileName()); - } - try { + LangCode code = LangCode.valueOf(langCode.toUpperCase()); + Map json = new TreeMap<>(); + Resource file; + + if (code == LangCode.CUSTOM) { + if (locale.getLangCode() != LangCode.CUSTOM || !files.getFileFromPluginFolder("locale.yml").exists()) { + return json; + } + file = files.getResourceFromPluginFolder("locale.yml"); + } else { + file = files.getResourceFromJar("locale/" + code.getFileName()); + } + return dfs(loadLocale(file), json); - } catch (IOException e) { - throw new UncheckedIOException(e); + } catch (IllegalArgumentException noSuchEnum) { + return Collections.emptyMap(); + } catch (IOException dfsFileLookupError) { + throw new UncheckedIOException(dfsFileLookupError); } } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/AccessControlTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/AccessControlTest.java index aeef07b11..b16db099f 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/AccessControlTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/AccessControlTest.java @@ -211,6 +211,7 @@ class AccessControlTest { "/v1/metadata,200", "/v1/locale,200", "/v1/locale/EN,200", + "/v1/locale/NonexistingLanguage,404", }) void levelZeroCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException { int responseCode = access(resource, cookieLevel0); @@ -280,6 +281,7 @@ class AccessControlTest { "/v1/metadata,200", "/v1/locale,200", "/v1/locale/EN,200", + "/v1/locale/NonexistingLanguage,404", }) void levelOneCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException { int responseCode = access(resource, cookieLevel1); @@ -349,6 +351,7 @@ class AccessControlTest { "/v1/metadata,200", "/v1/locale,200", "/v1/locale/EN,200", + "/v1/locale/NonexistingLanguage,404", }) void levelTwoCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException { int responseCode = access(resource, cookieLevel2); @@ -416,6 +419,7 @@ class AccessControlTest { "/v1/metadata,200", "/v1/locale,200", "/v1/locale/EN,200", + "/v1/locale/NonexistingLanguage,404", }) void levelHundredCanNotAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException { int responseCode = access(resource, cookieLevel100); diff --git a/Plan/common/src/test/java/com/djrapitops/plan/settings/locale/LangCodeTest.java b/Plan/common/src/test/java/com/djrapitops/plan/settings/locale/LangCodeTest.java new file mode 100644 index 000000000..67629edaf --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/settings/locale/LangCodeTest.java @@ -0,0 +1,55 @@ +package com.djrapitops.plan.settings.locale; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Arrays; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.*; + +class LangCodeTest { + + @Test + @DisplayName("All LangCodes have matching file") + void allLangCodesHaveFile() { + assertAll(Arrays.stream(LangCode.values()) + .filter(Predicate.not(LangCode.CUSTOM::equals)) + .map(langCode -> () -> assertFileExists(langCode)) + ); + } + + private void assertFileExists(LangCode langCode) throws URISyntaxException { + URL resource = getClass().getClassLoader().getResource("assets/plan/locale/" + langCode.getFileName()); + assertNotNull(resource, () -> "Resource assets/plan/locale/" + langCode.getFileName() + " does not exist, but it is needed for LangCode." + langCode.name()); + File file = new File(resource.toURI()); + assertTrue(file.exists(), () -> "File: " + file.getAbsolutePath() + " does not exist, but it is needed for LangCode." + langCode.name()); + } + + @Test + @DisplayName("All locale files have matching LangCode") + void allFilesHaveLangCode() throws URISyntaxException { + Set fileNames = Arrays.stream(LangCode.values()) + .filter(Predicate.not(LangCode.CUSTOM::equals)) + .map(LangCode::getFileName) + .collect(Collectors.toSet()); + + URL resource = getClass().getClassLoader().getResource("assets/plan/locale"); + assertNotNull(resource, "assets/plan/locale folder has gone missing for some reason - It's needed to access locales"); + File localeFolder = new File(resource.toURI()); + + File[] localeFiles = localeFolder.listFiles(); + assertNotNull(localeFiles); + assertAll(Arrays.stream(localeFiles) + .map(File::getName) + .map(fileName -> () -> + assertTrue(fileNames.contains(fileName), () -> "'" + fileName + "' was not found from assets/plan/locale/") + )); + } + +} \ No newline at end of file