diff --git a/scm-it/src/test/java/sonia/scm/it/I18nServletITCase.java b/scm-it/src/test/java/sonia/scm/it/I18nServletITCase.java new file mode 100644 index 0000000000..6597880665 --- /dev/null +++ b/scm-it/src/test/java/sonia/scm/it/I18nServletITCase.java @@ -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"); + } +} diff --git a/scm-it/src/test/java/sonia/scm/it/utils/ScmRequests.java b/scm-it/src/test/java/sonia/scm/it/utils/ScmRequests.java index d42a64b98e..14caa57beb 100644 --- a/scm-it/src/test/java/sonia/scm/it/utils/ScmRequests.java +++ b/scm-it/src/test/java/sonia/scm/it/utils/ScmRequests.java @@ -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() diff --git a/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js b/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js index d8eb4ae0e0..c6aed483e7 100644 --- a/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js +++ b/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js @@ -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 { 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 { return (
-

Clone the repository

+

{t("scm-git-plugin.information.clone")}

           git clone {href}
         
-

Create a new repository

+

{t("scm-git-plugin.information.create")}

           
             git init {repository.name}
@@ -39,7 +41,7 @@ class ProtocolInformation extends React.Component {
             
-

Push an existing repository

+

{t("scm-git-plugin.information.replace")}

           
             git remote add origin {href}
@@ -54,4 +56,4 @@ class ProtocolInformation extends React.Component {
 
 }
 
-export default ProtocolInformation;
+export default translate("plugins")(ProtocolInformation);
diff --git a/scm-plugins/scm-git-plugin/src/main/resources/locales/de/plugins.json b/scm-plugins/scm-git-plugin/src/main/resources/locales/de/plugins.json
new file mode 100644
index 0000000000..1dc0e254c2
--- /dev/null
+++ b/scm-plugins/scm-git-plugin/src/main/resources/locales/de/plugins.json
@@ -0,0 +1,9 @@
+{
+  "scm-git-plugin": {
+    "information": {
+      "clone" : "Repository Klonen",
+      "create" : "Neue Repository erstellen",
+      "replace" : "Eine existierende Repository aktualisieren"
+    }
+  }
+}
diff --git a/scm-plugins/scm-git-plugin/src/main/resources/locales/en/plugins.json b/scm-plugins/scm-git-plugin/src/main/resources/locales/en/plugins.json
new file mode 100644
index 0000000000..65594bae19
--- /dev/null
+++ b/scm-plugins/scm-git-plugin/src/main/resources/locales/en/plugins.json
@@ -0,0 +1,9 @@
+{
+  "scm-git-plugin": {
+    "information": {
+      "clone" : "Clone the repository",
+      "create" : "Create a new repository",
+      "replace" : "Push an existing repository"
+    }
+  }
+}
diff --git a/scm-plugins/scm-hg-plugin/src/main/js/ProtocolInformation.js b/scm-plugins/scm-hg-plugin/src/main/js/ProtocolInformation.js
index 03fc41450a..a8ae91cdfb 100644
--- a/scm-plugins/scm-hg-plugin/src/main/js/ProtocolInformation.js
+++ b/scm-plugins/scm-hg-plugin/src/main/js/ProtocolInformation.js
@@ -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 {
 
   render() {
-    const { repository } = this.props;
+    const { repository, t } = this.props;
     const href = repositories.getProtocolLinkByType(repository, "http");
     if (!href) {
       return null;
     }
     return (
       
-

Clone the repository

+

{t("scm-hg-plugin.information.clone")}

           hg clone {href}
         
-

Create a new repository

+

{t("scm-hg-plugin.information.create")}

           
             hg init {repository.name}
@@ -41,7 +43,7 @@ class ProtocolInformation extends React.Component {
             
-

Push an existing repository

+

{t("scm-hg-plugin.information.replace")}

           
             # add the repository url as default to your .hg/hgrc e.g:
@@ -59,4 +61,4 @@ class ProtocolInformation extends React.Component {
 
 }
 
-export default ProtocolInformation;
+export default translate("plugins")(ProtocolInformation);
diff --git a/scm-plugins/scm-hg-plugin/src/main/resources/locales/de/plugins.json b/scm-plugins/scm-hg-plugin/src/main/resources/locales/de/plugins.json
new file mode 100644
index 0000000000..0824a4ad38
--- /dev/null
+++ b/scm-plugins/scm-hg-plugin/src/main/resources/locales/de/plugins.json
@@ -0,0 +1,9 @@
+{
+  "scm-hg-plugin": {
+    "information": {
+      "clone" : "Repository Klonen",
+      "create" : "Neue Repository erstellen",
+      "replace" : "Eine existierende Repository aktualisieren"
+    }
+  }
+}
diff --git a/scm-plugins/scm-hg-plugin/src/main/resources/locales/en/plugins.json b/scm-plugins/scm-hg-plugin/src/main/resources/locales/en/plugins.json
new file mode 100644
index 0000000000..4ec1d4e4d2
--- /dev/null
+++ b/scm-plugins/scm-hg-plugin/src/main/resources/locales/en/plugins.json
@@ -0,0 +1,9 @@
+{
+  "scm-hg-plugin": {
+    "information": {
+      "clone" : "Clone the repository",
+      "create" : "Create a new repository",
+      "replace" : "Push an existing repository"
+    }
+  }
+}
diff --git a/scm-plugins/scm-svn-plugin/src/main/js/ProtocolInformation.js b/scm-plugins/scm-svn-plugin/src/main/js/ProtocolInformation.js
index 0ba195887f..68fdc68f74 100644
--- a/scm-plugins/scm-svn-plugin/src/main/js/ProtocolInformation.js
+++ b/scm-plugins/scm-svn-plugin/src/main/js/ProtocolInformation.js
@@ -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 {
 
   render() {
-    const { repository } = this.props;
+    const { repository, t } = this.props;
     const href = repositories.getProtocolLinkByType(repository, "http");
     if (!href) {
       return null;
     }
     return (
       
-

Checkout the repository

+

{t("scm-svn-plugin.information.checkout")}

           svn checkout {href}
         
@@ -27,4 +29,4 @@ class ProtocolInformation extends React.Component { } -export default ProtocolInformation; +export default translate("plugins")(ProtocolInformation); diff --git a/scm-plugins/scm-svn-plugin/src/main/resources/locales/de/plugins.json b/scm-plugins/scm-svn-plugin/src/main/resources/locales/de/plugins.json new file mode 100644 index 0000000000..7c58498ef1 --- /dev/null +++ b/scm-plugins/scm-svn-plugin/src/main/resources/locales/de/plugins.json @@ -0,0 +1,7 @@ +{ + "scm-svn-plugin": { + "information": { + "checkout" : "Repository auschecken" + } + } +} diff --git a/scm-plugins/scm-svn-plugin/src/main/resources/locales/en/plugins.json b/scm-plugins/scm-svn-plugin/src/main/resources/locales/en/plugins.json new file mode 100644 index 0000000000..07b34baf10 --- /dev/null +++ b/scm-plugins/scm-svn-plugin/src/main/resources/locales/en/plugins.json @@ -0,0 +1,7 @@ +{ + "scm-svn-plugin": { + "information": { + "checkout" : "Checkout repository" + } + } +} diff --git a/scm-webapp/src/main/java/sonia/scm/WebResourceServlet.java b/scm-webapp/src/main/java/sonia/scm/WebResourceServlet.java index 6b4a29961d..222800c9e3 100644 --- a/scm-webapp/src/main/java/sonia/scm/WebResourceServlet.java +++ b/scm-webapp/src/main/java/sonia/scm/WebResourceServlet.java @@ -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() diff --git a/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java b/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java new file mode 100644 index 0000000000..9773b91cf7 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java @@ -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 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> jsonFileProvider, + BiConsumer createdJsonFileConsumer) { + return Optional.ofNullable(jsonFileProvider.apply(path) + .orElseGet(() -> { + Optional 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> jsonFileProvider = usedPath -> Optional.empty(); + BiConsumer 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 collectJsonFile(String path) { + log.debug("Collect plugin translations from path {} for every plugin", path); + JsonNode mergedJsonNode = null; + try { + Enumeration 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 updateNode into the mainNode 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 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)); + } + } + } + +} diff --git a/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java b/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java new file mode 100644 index 0000000000..a912f738e2 --- /dev/null +++ b/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java @@ -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 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 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; + } + +}