mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-01 11:05:56 +01:00
Merged in feature/i18n_for_plugins_v2 (pull request #92)
Feature/i18n for plugins v2
This commit is contained in:
19
scm-it/src/test/java/sonia/scm/it/I18nServletITCase.java
Normal file
19
scm-it/src/test/java/sonia/scm/it/I18nServletITCase.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package sonia.scm.it;
|
||||
|
||||
import org.junit.Test;
|
||||
import sonia.scm.it.utils.ScmRequests;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class I18nServletITCase {
|
||||
|
||||
@Test
|
||||
public void shouldGetCollectedPluginTranslations() {
|
||||
ScmRequests.start()
|
||||
.requestPluginTranslations("de")
|
||||
.assertStatusCode(200)
|
||||
.assertSingleProperty(value -> assertThat(value).isNotNull(), "scm-git-plugin")
|
||||
.assertSingleProperty(value -> assertThat(value).isNotNull(), "scm-hg-plugin")
|
||||
.assertSingleProperty(value -> assertThat(value).isNotNull(), "scm-svn-plugin");
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,12 @@ public class ScmRequests {
|
||||
setUsername(username);
|
||||
setPassword(password);
|
||||
return new ChangePasswordResponse<>(applyPUTRequest(RestUtil.REST_BASE_URL.resolve("users/"+userPathParam+"/password").toString(), VndMediaType.PASSWORD_OVERWRITE, TestData.createPasswordChangeJson(password,newPassword)), null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ModelResponse requestPluginTranslations(String language) {
|
||||
Response response = applyGETRequest(RestUtil.BASE_URL.resolve("locales/" + language + "/plugins.json").toString());
|
||||
return new ModelResponse(response, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,6 +99,11 @@ public class ScmRequests {
|
||||
*/
|
||||
private Response applyGETRequestWithQueryParams(String url, String params) {
|
||||
LOG.info("GET {}", url);
|
||||
if (username == null || password == null){
|
||||
return RestAssured.given()
|
||||
.when()
|
||||
.get(url + params);
|
||||
}
|
||||
return RestAssured.given()
|
||||
.auth().preemptive().basic(username, password)
|
||||
.when()
|
||||
|
||||
@@ -2,15 +2,17 @@
|
||||
import React from "react";
|
||||
import { repositories } from "@scm-manager/ui-components";
|
||||
import type { Repository } from "@scm-manager/ui-types";
|
||||
import { translate } from "react-i18next";
|
||||
|
||||
type Props = {
|
||||
repository: Repository
|
||||
repository: Repository,
|
||||
t: string => string
|
||||
}
|
||||
|
||||
class ProtocolInformation extends React.Component<Props> {
|
||||
|
||||
render() {
|
||||
const { repository } = this.props;
|
||||
const { repository, t } = this.props;
|
||||
const href = repositories.getProtocolLinkByType(repository, "http");
|
||||
if (!href) {
|
||||
return null;
|
||||
@@ -18,11 +20,11 @@ class ProtocolInformation extends React.Component<Props> {
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h4>Clone the repository</h4>
|
||||
<h4>{t("scm-git-plugin.information.clone")}</h4>
|
||||
<pre>
|
||||
<code>git clone {href}</code>
|
||||
</pre>
|
||||
<h4>Create a new repository</h4>
|
||||
<h4>{t("scm-git-plugin.information.create")}</h4>
|
||||
<pre>
|
||||
<code>
|
||||
git init {repository.name}
|
||||
@@ -39,7 +41,7 @@ class ProtocolInformation extends React.Component<Props> {
|
||||
<br />
|
||||
</code>
|
||||
</pre>
|
||||
<h4>Push an existing repository</h4>
|
||||
<h4>{t("scm-git-plugin.information.replace")}</h4>
|
||||
<pre>
|
||||
<code>
|
||||
git remote add origin {href}
|
||||
@@ -54,4 +56,4 @@ class ProtocolInformation extends React.Component<Props> {
|
||||
|
||||
}
|
||||
|
||||
export default ProtocolInformation;
|
||||
export default translate("plugins")(ProtocolInformation);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"scm-git-plugin": {
|
||||
"information": {
|
||||
"clone" : "Repository Klonen",
|
||||
"create" : "Neue Repository erstellen",
|
||||
"replace" : "Eine existierende Repository aktualisieren"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"scm-git-plugin": {
|
||||
"information": {
|
||||
"clone" : "Clone the repository",
|
||||
"create" : "Create a new repository",
|
||||
"replace" : "Push an existing repository"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,26 +2,28 @@
|
||||
import React from "react";
|
||||
import { repositories } from "@scm-manager/ui-components";
|
||||
import type { Repository } from "@scm-manager/ui-types";
|
||||
import { translate } from "react-i18next";
|
||||
|
||||
type Props = {
|
||||
repository: Repository
|
||||
repository: Repository,
|
||||
t: string => string
|
||||
}
|
||||
|
||||
class ProtocolInformation extends React.Component<Props> {
|
||||
|
||||
render() {
|
||||
const { repository } = this.props;
|
||||
const { repository, t } = this.props;
|
||||
const href = repositories.getProtocolLinkByType(repository, "http");
|
||||
if (!href) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<h4>Clone the repository</h4>
|
||||
<h4>{t("scm-hg-plugin.information.clone")}</h4>
|
||||
<pre>
|
||||
<code>hg clone {href}</code>
|
||||
</pre>
|
||||
<h4>Create a new repository</h4>
|
||||
<h4>{t("scm-hg-plugin.information.create")}</h4>
|
||||
<pre>
|
||||
<code>
|
||||
hg init {repository.name}
|
||||
@@ -41,7 +43,7 @@ class ProtocolInformation extends React.Component<Props> {
|
||||
<br />
|
||||
</code>
|
||||
</pre>
|
||||
<h4>Push an existing repository</h4>
|
||||
<h4>{t("scm-hg-plugin.information.replace")}</h4>
|
||||
<pre>
|
||||
<code>
|
||||
# add the repository url as default to your .hg/hgrc e.g:
|
||||
@@ -59,4 +61,4 @@ class ProtocolInformation extends React.Component<Props> {
|
||||
|
||||
}
|
||||
|
||||
export default ProtocolInformation;
|
||||
export default translate("plugins")(ProtocolInformation);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"scm-hg-plugin": {
|
||||
"information": {
|
||||
"clone" : "Repository Klonen",
|
||||
"create" : "Neue Repository erstellen",
|
||||
"replace" : "Eine existierende Repository aktualisieren"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"scm-hg-plugin": {
|
||||
"information": {
|
||||
"clone" : "Clone the repository",
|
||||
"create" : "Create a new repository",
|
||||
"replace" : "Push an existing repository"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,22 +2,24 @@
|
||||
import React from "react";
|
||||
import { repositories } from "@scm-manager/ui-components";
|
||||
import type { Repository } from "@scm-manager/ui-types";
|
||||
import { translate } from "react-i18next";
|
||||
|
||||
type Props = {
|
||||
repository: Repository
|
||||
repository: Repository,
|
||||
t: string => string
|
||||
}
|
||||
|
||||
class ProtocolInformation extends React.Component<Props> {
|
||||
|
||||
render() {
|
||||
const { repository } = this.props;
|
||||
const { repository, t } = this.props;
|
||||
const href = repositories.getProtocolLinkByType(repository, "http");
|
||||
if (!href) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<h4>Checkout the repository</h4>
|
||||
<h4>{t("scm-svn-plugin.information.checkout")}</h4>
|
||||
<pre>
|
||||
<code>svn checkout {href}</code>
|
||||
</pre>
|
||||
@@ -27,4 +29,4 @@ class ProtocolInformation extends React.Component<Props> {
|
||||
|
||||
}
|
||||
|
||||
export default ProtocolInformation;
|
||||
export default translate("plugins")(ProtocolInformation);
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"scm-svn-plugin": {
|
||||
"information": {
|
||||
"checkout" : "Repository auschecken"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"scm-svn-plugin": {
|
||||
"information": {
|
||||
"checkout" : "Checkout repository"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import java.net.URL;
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Singleton
|
||||
@Priority(WebResourceServlet.PRIORITY)
|
||||
@WebElement(value = WebResourceServlet.PATTERN, regex = true)
|
||||
public class WebResourceServlet extends HttpServlet {
|
||||
|
||||
@@ -35,6 +36,9 @@ public class WebResourceServlet extends HttpServlet {
|
||||
@VisibleForTesting
|
||||
static final String PATTERN = "/(?!api/|git/|hg/|svn/|hook/|repo/).*";
|
||||
|
||||
// Be sure that this servlet is the last one in the servlet chain.
|
||||
static final int PRIORITY = Integer.MAX_VALUE;
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(WebResourceServlet.class);
|
||||
|
||||
private final WebResourceSender sender = WebResourceSender.create()
|
||||
|
||||
188
scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java
Normal file
188
scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java
Normal file
@@ -0,0 +1,188 @@
|
||||
package sonia.scm.web.i18n;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.github.legman.Subscribe;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.inject.Singleton;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.SCMContext;
|
||||
import sonia.scm.Stage;
|
||||
import sonia.scm.boot.RestartEvent;
|
||||
import sonia.scm.cache.Cache;
|
||||
import sonia.scm.cache.CacheManager;
|
||||
import sonia.scm.filter.WebElement;
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URL;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
||||
/**
|
||||
* Collect the plugin translations.
|
||||
*/
|
||||
@Singleton
|
||||
@WebElement(value = I18nServlet.PATTERN, regex = true)
|
||||
@Slf4j
|
||||
public class I18nServlet extends HttpServlet {
|
||||
|
||||
public static final String PLUGINS_JSON = "plugins.json";
|
||||
public static final String PATTERN = "/locales/[a-z\\-A-Z]*/" + PLUGINS_JSON;
|
||||
public static final String CACHE_NAME = "sonia.cache.plugins.translations";
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
private final Cache<String, JsonNode> cache;
|
||||
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
|
||||
@Inject
|
||||
public I18nServlet(PluginLoader pluginLoader, CacheManager cacheManager) {
|
||||
this.classLoader = pluginLoader.getUberClassLoader();
|
||||
this.cache = cacheManager.getCache(CACHE_NAME);
|
||||
}
|
||||
|
||||
@Subscribe(async = false)
|
||||
public void handleRestartEvent(RestartEvent event) {
|
||||
log.debug("Clear cache on restart event with reason {}", event.getReason());
|
||||
cache.clear();
|
||||
}
|
||||
|
||||
private JsonNode getCollectedJson(String path,
|
||||
Function<String, Optional<JsonNode>> jsonFileProvider,
|
||||
BiConsumer<String, JsonNode> createdJsonFileConsumer) {
|
||||
return Optional.ofNullable(jsonFileProvider.apply(path)
|
||||
.orElseGet(() -> {
|
||||
Optional<JsonNode> createdFile = collectJsonFile(path);
|
||||
createdFile.ifPresent(map -> createdJsonFileConsumer.accept(path, map));
|
||||
return createdFile.orElse(null);
|
||||
}
|
||||
)).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse response) {
|
||||
try (PrintWriter out = response.getWriter()) {
|
||||
response.setContentType("application/json");
|
||||
String path = req.getServletPath();
|
||||
Function<String, Optional<JsonNode>> jsonFileProvider = usedPath -> Optional.empty();
|
||||
BiConsumer<String, JsonNode> createdJsonFileConsumer = (usedPath, jsonNode) -> log.debug("A json File is created from the path {}", usedPath);
|
||||
if (isProductionStage()) {
|
||||
log.debug("In Production Stage get the plugin translations from the cache");
|
||||
jsonFileProvider = usedPath -> Optional.ofNullable(
|
||||
cache.get(usedPath));
|
||||
createdJsonFileConsumer = createdJsonFileConsumer
|
||||
.andThen((usedPath, jsonNode) -> log.debug("Put the created json File in the cache with the key {}", usedPath))
|
||||
.andThen(cache::put);
|
||||
}
|
||||
objectMapper.writeValue(out, getCollectedJson(path, jsonFileProvider, createdJsonFileConsumer));
|
||||
} catch (IOException e) {
|
||||
log.error("Error on getting the translation of the plugins", e);
|
||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
||||
} catch (NotFoundException e) {
|
||||
log.error("Plugin translations are not found", e);
|
||||
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
protected boolean isProductionStage() {
|
||||
return SCMContext.getContext().getStage() == Stage.PRODUCTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a collected Json File as JsonNode from the given path from all plugins in the class path
|
||||
*
|
||||
* @param path the searched resource path
|
||||
* @return a collected Json File as JsonNode from the given path from all plugins in the class path
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected Optional<JsonNode> collectJsonFile(String path) {
|
||||
log.debug("Collect plugin translations from path {} for every plugin", path);
|
||||
JsonNode mergedJsonNode = null;
|
||||
try {
|
||||
Enumeration<URL> resources = classLoader.getResources(path.replaceFirst("/", ""));
|
||||
while (resources.hasMoreElements()) {
|
||||
URL url = resources.nextElement();
|
||||
JsonNode jsonNode = objectMapper.readTree(url);
|
||||
if (mergedJsonNode != null) {
|
||||
merge(mergedJsonNode, jsonNode);
|
||||
} else {
|
||||
mergedJsonNode = jsonNode;
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Error on loading sources from {}", path, e);
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.ofNullable(mergedJsonNode);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Merge the <code>updateNode</code> into the <code>mainNode</code> and return it.
|
||||
*
|
||||
* This is not a deep merge.
|
||||
*
|
||||
* @param mainNode the main node
|
||||
* @param updateNode the update node
|
||||
* @return the merged mainNode
|
||||
*/
|
||||
@VisibleForTesting
|
||||
protected JsonNode merge(JsonNode mainNode, JsonNode updateNode) {
|
||||
Iterator<String> fieldNames = updateNode.fieldNames();
|
||||
|
||||
while (fieldNames.hasNext()) {
|
||||
|
||||
String fieldName = fieldNames.next();
|
||||
JsonNode jsonNode = mainNode.get(fieldName);
|
||||
|
||||
if (jsonNode != null) {
|
||||
mergeNode(updateNode, fieldName, jsonNode);
|
||||
} else {
|
||||
mergeField(mainNode, updateNode, fieldName);
|
||||
}
|
||||
}
|
||||
return mainNode;
|
||||
}
|
||||
|
||||
private void mergeField(JsonNode mainNode, JsonNode updateNode, String fieldName) {
|
||||
if (mainNode instanceof ObjectNode) {
|
||||
JsonNode value = updateNode.get(fieldName);
|
||||
if (value.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (value.isIntegralNumber() && value.toString().equals("0")) {
|
||||
return;
|
||||
}
|
||||
if (value.isFloatingPointNumber() && value.toString().equals("0.0")) {
|
||||
return;
|
||||
}
|
||||
((ObjectNode) mainNode).set(fieldName, value);
|
||||
}
|
||||
}
|
||||
|
||||
private void mergeNode(JsonNode updateNode, String fieldName, JsonNode jsonNode) {
|
||||
if (jsonNode.isObject()) {
|
||||
merge(jsonNode, updateNode.get(fieldName));
|
||||
} else if (jsonNode.isArray()) {
|
||||
for (int i = 0; i < jsonNode.size(); i++) {
|
||||
merge(jsonNode.get(i), updateNode.get(fieldName).get(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
255
scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java
Normal file
255
scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java
Normal file
@@ -0,0 +1,255 @@
|
||||
package sonia.scm.web.i18n;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockSettings;
|
||||
import org.mockito.internal.creation.MockSettingsImpl;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.boot.RestartEvent;
|
||||
import sonia.scm.cache.Cache;
|
||||
import sonia.scm.cache.CacheManager;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.plugin.PluginLoader;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.Silent.class)
|
||||
@SubjectAware(configuration = "classpath:sonia/scm/shiro-001.ini")
|
||||
public class I18nServletTest {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiro = new ShiroRule();
|
||||
|
||||
private static final String GIT_PLUGIN_JSON = json(
|
||||
"{",
|
||||
"'scm-git-plugin': {",
|
||||
"'information': {",
|
||||
"'clone' : 'Clone',",
|
||||
"'create' : 'Create',",
|
||||
"'replace' : 'Push'",
|
||||
"}",
|
||||
"}",
|
||||
"}"
|
||||
);
|
||||
private static final String HG_PLUGIN_JSON = json(
|
||||
"{",
|
||||
"'scm-hg-plugin': {",
|
||||
"'information': {",
|
||||
"'clone' : 'Clone',",
|
||||
"'create' : 'Create',",
|
||||
"'replace' : 'Push'",
|
||||
"}",
|
||||
"}",
|
||||
"}"
|
||||
);
|
||||
private static String SVN_PLUGIN_JSON = json(
|
||||
"{",
|
||||
"'scm-svn-plugin': {",
|
||||
"'information': {",
|
||||
"'checkout' : 'Checkout'",
|
||||
"}",
|
||||
"}",
|
||||
"}"
|
||||
);
|
||||
|
||||
private static String json(String... parts) {
|
||||
return String.join("\n", parts ).replaceAll("'", "\"");
|
||||
}
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Mock
|
||||
PluginLoader pluginLoader;
|
||||
|
||||
@Mock
|
||||
CacheManager cacheManager;
|
||||
|
||||
@Mock
|
||||
ClassLoader classLoader;
|
||||
|
||||
I18nServlet servlet;
|
||||
|
||||
@Mock
|
||||
private Cache cache;
|
||||
private Enumeration<URL> resources;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void init() throws IOException {
|
||||
resources = Collections.enumeration(Lists.newArrayList(
|
||||
createFileFromString(SVN_PLUGIN_JSON).toURL(),
|
||||
createFileFromString(GIT_PLUGIN_JSON).toURL(),
|
||||
createFileFromString(HG_PLUGIN_JSON).toURL()
|
||||
));
|
||||
when(pluginLoader.getUberClassLoader()).thenReturn(classLoader);
|
||||
when(cacheManager.getCache(I18nServlet.CACHE_NAME)).thenReturn(cache);
|
||||
MockSettings settings = new MockSettingsImpl<>();
|
||||
settings.useConstructor(pluginLoader, cacheManager);
|
||||
settings.defaultAnswer(InvocationOnMock::callRealMethod);
|
||||
servlet = mock(I18nServlet.class, settings);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCleanCacheOnRestartEvent() {
|
||||
ScmEventBus.getInstance().register(servlet);
|
||||
|
||||
ScmEventBus.getInstance().post(new RestartEvent(I18nServlet.class, "Restart to reload the plugin resources"));
|
||||
|
||||
verify(cache).clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldFailWith404OnMissingResources() throws IOException {
|
||||
String path = "/locales/de/plugins.json";
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
PrintWriter writer = mock(PrintWriter.class);
|
||||
when(response.getWriter()).thenReturn(writer);
|
||||
when(request.getServletPath()).thenReturn(path);
|
||||
when(classLoader.getResources("locales/de/plugins.json")).thenThrow(IOException.class);
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
verify(response).setStatus(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldFailWith500OnIOException() throws IOException {
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
doThrow(IOException.class).when(response).getWriter();
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
verify(response).setStatus(500);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void inDevelopmentStageShouldNotUseCache() throws IOException {
|
||||
String path = "/locales/de/plugins.json";
|
||||
when(servlet.isProductionStage()).thenReturn(false);
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
File file = temporaryFolder.newFile();
|
||||
PrintWriter writer = new PrintWriter(new FileOutputStream(file));
|
||||
when(response.getWriter()).thenReturn(writer);
|
||||
when(request.getServletPath()).thenReturn(path);
|
||||
when(classLoader.getResources("locales/de/plugins.json")).thenReturn(resources);
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
String json = Files.readLines(file, Charset.defaultCharset()).get(0);
|
||||
assertJson(json);
|
||||
verify(cache, never()).get(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void inProductionStageShouldUseCache() throws IOException {
|
||||
String path = "/locales/de/plugins.json";
|
||||
when(servlet.isProductionStage()).thenReturn(true);
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
File file = temporaryFolder.newFile();
|
||||
PrintWriter writer = new PrintWriter(new FileOutputStream(file));
|
||||
when(response.getWriter()).thenReturn(writer);
|
||||
when(request.getServletPath()).thenReturn(path);
|
||||
when(classLoader.getResources("locales/de/plugins.json")).thenReturn(resources);
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
String json = Files.readLines(file, Charset.defaultCharset()).get(0);
|
||||
assertJson(json);
|
||||
verify(cache).get(path);
|
||||
verify(cache).put(eq(path), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void inProductionStageShouldGetFromCache() throws IOException {
|
||||
String path = "/locales/de/plugins.json";
|
||||
when(servlet.isProductionStage()).thenReturn(true);
|
||||
HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
HttpServletResponse response = mock(HttpServletResponse.class);
|
||||
File file = temporaryFolder.newFile();
|
||||
PrintWriter writer = new PrintWriter(new FileOutputStream(file));
|
||||
when(response.getWriter()).thenReturn(writer);
|
||||
when(request.getServletPath()).thenReturn(path);
|
||||
when(classLoader.getResources("locales/de/plugins.json")).thenReturn(resources);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode node = objectMapper.readTree(GIT_PLUGIN_JSON);
|
||||
node = servlet.merge(node, objectMapper.readTree(HG_PLUGIN_JSON));
|
||||
node = servlet.merge(node, objectMapper.readTree(SVN_PLUGIN_JSON));
|
||||
when(cache.get(path)).thenReturn(node);
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
String json = Files.readLines(file, Charset.defaultCharset()).get(0);
|
||||
verify(servlet, never()).collectJsonFile(path);
|
||||
verify(cache, never()).put(eq(path), any());
|
||||
verify(cache).get(path);
|
||||
assertJson(json);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void shouldCollectJsonFile() throws IOException {
|
||||
String path = "locales/de/plugins.json";
|
||||
when(classLoader.getResources(path)).thenReturn(resources);
|
||||
|
||||
Optional<JsonNode> jsonNodeOptional = servlet.collectJsonFile("/" + path);
|
||||
|
||||
assertJson(jsonNodeOptional.orElse(null));
|
||||
}
|
||||
|
||||
public void assertJson(JsonNode actual) throws IOException {
|
||||
assertJson(actual.toString());
|
||||
}
|
||||
|
||||
public void assertJson(String actual) throws IOException {
|
||||
assertThat(actual)
|
||||
.isNotEmpty()
|
||||
.contains(StringUtils.deleteWhitespace(GIT_PLUGIN_JSON.substring(1, GIT_PLUGIN_JSON.length() - 1)))
|
||||
.contains(StringUtils.deleteWhitespace(HG_PLUGIN_JSON.substring(1, HG_PLUGIN_JSON.length() - 1)))
|
||||
.contains(StringUtils.deleteWhitespace(SVN_PLUGIN_JSON.substring(1, SVN_PLUGIN_JSON.length() - 1)));
|
||||
}
|
||||
|
||||
public File createFileFromString(String json) throws IOException {
|
||||
File file = temporaryFolder.newFile();
|
||||
Files.write(json.getBytes(Charsets.UTF_8), file);
|
||||
return file;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user