mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-07 00:48:24 +01:00
Wrote some tests for LangCodes and fixed some issues in LocaleJSONResolver
- Removed unnecessary regex that was marked as bug by sonar
This commit is contained in:
parent
9d852ac989
commit
09e71cd7ba
@ -34,10 +34,7 @@ import javax.inject.Inject;
|
|||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves JSON requests for /v1/locale and /v1/locale/{@link LangCode#toString()}.
|
* 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) {
|
private Response getResponse(Request request) {
|
||||||
ResponseBuilder builder = Response.builder();
|
ResponseBuilder builder = Response.builder();
|
||||||
|
|
||||||
String path = request.omitFirstInPath().getPath().asString().replaceAll("^/|/$", "");
|
Optional<String> langCode = request.getPath().getPart(1);
|
||||||
Map<String, Object> json = "".equals(path) ? getLanguageListJSON() : getLocaleJSON(path);
|
Map<String, Object> json = langCode
|
||||||
|
.map(this::getLocaleJSON)
|
||||||
|
.orElseGet(this::getLanguageListJSON);
|
||||||
|
|
||||||
if (!json.isEmpty()) {
|
if (!json.isEmpty()) {
|
||||||
return builder.setJSONContent(json).build();
|
return builder.setJSONContent(json).build();
|
||||||
@ -112,23 +111,25 @@ public class LocaleJSONResolver implements NoAuthResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, Object> getLocaleJSON(String langCode) {
|
private Map<String, Object> getLocaleJSON(String langCode) {
|
||||||
LangCode code = LangCode.valueOf(langCode.toUpperCase());
|
|
||||||
Map<String, Object> 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 {
|
try {
|
||||||
|
LangCode code = LangCode.valueOf(langCode.toUpperCase());
|
||||||
|
Map<String, Object> 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);
|
return dfs(loadLocale(file), json);
|
||||||
} catch (IOException e) {
|
} catch (IllegalArgumentException noSuchEnum) {
|
||||||
throw new UncheckedIOException(e);
|
return Collections.emptyMap();
|
||||||
|
} catch (IOException dfsFileLookupError) {
|
||||||
|
throw new UncheckedIOException(dfsFileLookupError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,6 +211,7 @@ class AccessControlTest {
|
|||||||
"/v1/metadata,200",
|
"/v1/metadata,200",
|
||||||
"/v1/locale,200",
|
"/v1/locale,200",
|
||||||
"/v1/locale/EN,200",
|
"/v1/locale/EN,200",
|
||||||
|
"/v1/locale/NonexistingLanguage,404",
|
||||||
})
|
})
|
||||||
void levelZeroCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
void levelZeroCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||||
int responseCode = access(resource, cookieLevel0);
|
int responseCode = access(resource, cookieLevel0);
|
||||||
@ -280,6 +281,7 @@ class AccessControlTest {
|
|||||||
"/v1/metadata,200",
|
"/v1/metadata,200",
|
||||||
"/v1/locale,200",
|
"/v1/locale,200",
|
||||||
"/v1/locale/EN,200",
|
"/v1/locale/EN,200",
|
||||||
|
"/v1/locale/NonexistingLanguage,404",
|
||||||
})
|
})
|
||||||
void levelOneCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
void levelOneCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||||
int responseCode = access(resource, cookieLevel1);
|
int responseCode = access(resource, cookieLevel1);
|
||||||
@ -349,6 +351,7 @@ class AccessControlTest {
|
|||||||
"/v1/metadata,200",
|
"/v1/metadata,200",
|
||||||
"/v1/locale,200",
|
"/v1/locale,200",
|
||||||
"/v1/locale/EN,200",
|
"/v1/locale/EN,200",
|
||||||
|
"/v1/locale/NonexistingLanguage,404",
|
||||||
})
|
})
|
||||||
void levelTwoCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
void levelTwoCanAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||||
int responseCode = access(resource, cookieLevel2);
|
int responseCode = access(resource, cookieLevel2);
|
||||||
@ -416,6 +419,7 @@ class AccessControlTest {
|
|||||||
"/v1/metadata,200",
|
"/v1/metadata,200",
|
||||||
"/v1/locale,200",
|
"/v1/locale,200",
|
||||||
"/v1/locale/EN,200",
|
"/v1/locale/EN,200",
|
||||||
|
"/v1/locale/NonexistingLanguage,404",
|
||||||
})
|
})
|
||||||
void levelHundredCanNotAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
void levelHundredCanNotAccess(String resource, String expectedResponseCode) throws NoSuchAlgorithmException, IOException, KeyManagementException {
|
||||||
int responseCode = access(resource, cookieLevel100);
|
int responseCode = access(resource, cookieLevel100);
|
||||||
|
@ -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<String> 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/")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user