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 69c79c37bf..9d08594006 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 @@ -52,7 +52,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); } /** @@ -90,6 +95,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..f17dc5e2e9 100644 --- a/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js +++ b/scm-plugins/scm-git-plugin/src/main/js/ProtocolInformation.js @@ -2,9 +2,11 @@ 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 { @@ -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
index a405e36d23..1dc0e254c2 100644
--- 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
@@ -1,5 +1,9 @@
 {
-  "git": {
-    "description": "die git repo ist super "
+  "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
index 0412a162b3..65594bae19 100644
--- 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
@@ -1,5 +1,9 @@
 {
-  "git": {
-    "description": "the git repo is great "
+  "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..6a55578926 100644
--- a/scm-plugins/scm-hg-plugin/src/main/js/ProtocolInformation.js
+++ b/scm-plugins/scm-hg-plugin/src/main/js/ProtocolInformation.js
@@ -2,9 +2,11 @@
 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 {
@@ -17,11 +19,11 @@ class ProtocolInformation extends React.Component {
     }
     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
index 49a3d97570..0824a4ad38 100644
--- 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
@@ -1,5 +1,9 @@
 {
-  "hg": {
-    "description": "die hg repo ist super "
+  "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
index 28c628e81e..4ec1d4e4d2 100644
--- 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
@@ -1,5 +1,9 @@
 {
-  "hg": {
-    "description": "the hg repo is great "
+  "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..bcfe223b83 100644
--- a/scm-plugins/scm-svn-plugin/src/main/js/ProtocolInformation.js
+++ b/scm-plugins/scm-svn-plugin/src/main/js/ProtocolInformation.js
@@ -2,9 +2,11 @@
 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 {
@@ -17,7 +19,7 @@ class ProtocolInformation extends React.Component {
     }
     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 index 1c25a5bcf5..7c58498ef1 100644 --- 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 @@ -1,5 +1,7 @@ { - "svn": { - "description": "die svn repo ist super " + "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 index cbfbaac63b..07b34baf10 100644 --- 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 @@ -1,5 +1,7 @@ { - "svn": { - "description": "the svn repo is great " + "scm-svn-plugin": { + "information": { + "checkout" : "Checkout repository" + } } } 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 index 00d1f3aaf4..d0c78bf1f5 100644 --- a/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java +++ b/scm-webapp/src/main/java/sonia/scm/web/i18n/I18nServlet.java @@ -4,7 +4,6 @@ package sonia.scm.web.i18n; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.legman.Subscribe; import com.google.inject.Singleton; -import com.sun.org.apache.regexp.internal.RE; import lombok.extern.slf4j.Slf4j; import sonia.scm.NotFoundException; import sonia.scm.SCMContext; @@ -34,7 +33,7 @@ import java.util.function.Function; /** - * Collect + * Collect the plugin translations. */ @Singleton @WebElement(value = I18nServlet.PATTERN, regex = true) @@ -47,9 +46,8 @@ public class I18nServlet extends HttpServlet { public static final String CACHE_NAME = "sonia.cache.plugins.translations"; private final UberClassLoader uberClassLoader; - private final Cache> cache; - private RE languagePathPostfix = new RE(".*(\\-[A-Z]+)/.*"); - private ObjectMapper objectMapper = new ObjectMapper(); + private final Cache cache; + private static ObjectMapper objectMapper = new ObjectMapper(); @Inject @@ -58,18 +56,18 @@ public class I18nServlet extends HttpServlet { this.cache = cacheManager.getCache(CACHE_NAME); } - @Subscribe + @Subscribe(async = false) public void handleRestartEvent(RestartEvent event) { - log.info("clear cache on restart event with reason {}", event.getReason()); + log.info("Clear cache on restart event with reason {}", event.getReason()); cache.clear(); } - public Map getCollectedJson(String path, - Function>> jsonFileProvider, - BiConsumer> createdJsonFileConsumer) throws NotFoundException { + private Map getCollectedJson(String path, + Function> jsonFileProvider, + BiConsumer createdJsonFileConsumer) { return Optional.ofNullable(jsonFileProvider.apply(path) .orElseGet(() -> { - Optional> createdFile = collectJsonFile(path); + Optional createdFile = collectJsonFile(path); createdFile.ifPresent(map -> createdJsonFileConsumer.accept(path, map)); return createdFile.orElse(null); } @@ -82,9 +80,9 @@ public class I18nServlet extends HttpServlet { response.setContentType("application/json"); PrintWriter out = response.getWriter(); String path = req.getServletPath(); - Function>> jsonFileProvider = usedPath -> Optional.empty(); - BiConsumer> createdJsonFileConsumer = (usedPath, foundJsonMap) -> log.info("A json File is created from the path {}", usedPath); - if (SCMContext.getContext().getStage() == Stage.PRODUCTION) { + Function> jsonFileProvider = usedPath -> Optional.empty(); + BiConsumer createdJsonFileConsumer = (usedPath, foundJsonMap) -> log.info("A json File is created from the path {}", usedPath); + if (isProductionStage()) { log.info("In Production Stage get the plugin translations from the cache"); jsonFileProvider = usedPath -> Optional.ofNullable( cache.get(usedPath)); @@ -94,58 +92,40 @@ public class I18nServlet extends HttpServlet { } out.write(objectMapper.writeValueAsString(getCollectedJson(path, jsonFileProvider, createdJsonFileConsumer))); } catch (IOException e) { - log.error("error on getting the translation of the plugins", 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); } -// ScmEventBus.getInstance().post(new RestartEvent(I18nServlet.class,"einfach so")); + } + + protected boolean isProductionStage() { + return SCMContext.getContext().getStage() == Stage.PRODUCTION; } /** - * Return a collected Json File as map with the given path from all plugins in the class path + * Return a collected Json File as map from the given path from all plugins in the class path * * @param path the searched resource path - * @return a collected Json File as map with the given path from all plugins in the class path + * @return a collected Json File as map from the given path from all plugins in the class path */ - private Optional> collectJsonFile(String path) { + protected Optional collectJsonFile(String path) { log.info("Collect plugin translations from path {} for every plugin", path); - HashMap result = null; + Map result = null; try { Enumeration resources = uberClassLoader.getResources(path.replaceFirst("/", "")); if (resources.hasMoreElements()) { - result = new HashMap<>(); + result = new HashMap(); while (resources.hasMoreElements()) { URL url = resources.nextElement(); - result.putAll(mergeJSONs(objectMapper, url)); + result.putAll(objectMapper.readValue(Files.readAllBytes(Paths.get(url.getPath())), Map.class)); } } } catch (IOException e) { log.error("Error on loading sources from {}", path, e); + return Optional.empty(); } return Optional.ofNullable(result); } - - private boolean hasLanguagePostfix(String path) { - return languagePathPostfix.match(path); - } - - /** - * remove the -DE from the path locales/de-DE/plugins - * - * @param servletPath - * @return - * @throws IOException - */ - private String removeLanguagePostfix(String servletPath) { - return servletPath.replace(languagePathPostfix.getParen(1), ""); - } - - // TODO simplify - private HashMap mergeJSONs(ObjectMapper objectMapper, URL url) throws IOException { - byte[] src = Files.readAllBytes(Paths.get(url.getPath())); - Map json = objectMapper.readValue(src, HashMap.class); - return objectMapper.readerForUpdating(json).readValue(src); - } } 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..b8deb4b069 --- /dev/null +++ b/scm-webapp/src/test/java/sonia/scm/web/i18n/I18nServletTest.java @@ -0,0 +1,253 @@ +package sonia.scm.web.i18n; + +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.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.ArgumentCaptor; +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 sonia.scm.plugin.UberClassLoader; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URL; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Map; +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.doCallRealMethod; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@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 = "{\n" + + " \"scm-git-plugin\": {\n" + + " \"information\": {\n" + + " \"clone\" : \"Clone\",\n" + + " \"create\" : \"Create\",\n" + + " \"replace\" : \"Push\"\n" + + " }\n" + + " }\n" + + "}\n"; + private static final String HG_PLUGIN_JSON = "{\n" + + " \"scm-hg-plugin\": {\n" + + " \"information\": {\n" + + " \"clone\" : \"Clone\",\n" + + " \"create\" : \"Create\",\n" + + " \"replace\" : \"Push\"\n" + + " }\n" + + " }\n" + + "}\n"; + private static String SVN_PLUGIN_JSON = "{\n" + + " \"scm-svn-plugin\": {\n" + + " \"information\": {\n" + + " \"checkout\" : \"Checkout\"\n" + + " }\n" + + " }\n" + + "}\n"; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Mock + PluginLoader pluginLoader; + + @Mock + CacheManager cacheManager; + + @Mock + UberClassLoader uberClassLoader; + + 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(uberClassLoader); + 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(uberClassLoader.getResources("locales/de/plugins.json")).thenThrow(IOException.class); + + servlet.doGet(request, response); + + verify(response).setStatus(404); + } + + @Test + @SuppressWarnings("unchecked") + public void shouldFailWith500OnIOException() 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(uberClassLoader.getResources("locales/de/plugins.json")).thenReturn(resources); + doThrow(IOException.class).when(writer).write(any(String.class)); + + 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); + PrintWriter writer = mock(PrintWriter.class); + when(response.getWriter()).thenReturn(writer); + when(request.getServletPath()).thenReturn(path); + when(uberClassLoader.getResources("locales/de/plugins.json")).thenReturn(resources); + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + doCallRealMethod().when(writer).write(captor.capture()); + + servlet.doGet(request, response); + + assertJsonMap(jsonStringToMap(captor.getValue())); + 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); + PrintWriter writer = mock(PrintWriter.class); + when(response.getWriter()).thenReturn(writer); + when(request.getServletPath()).thenReturn(path); + when(uberClassLoader.getResources("locales/de/plugins.json")).thenReturn(resources); + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + doCallRealMethod().when(writer).write(captor.capture()); + + servlet.doGet(request, response); + + assertJsonMap(jsonStringToMap(captor.getValue())); + 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); + PrintWriter writer = mock(PrintWriter.class); + when(response.getWriter()).thenReturn(writer); + when(request.getServletPath()).thenReturn(path); + when(uberClassLoader.getResources("locales/de/plugins.json")).thenReturn(resources); + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + doCallRealMethod().when(writer).write(captor.capture()); + Map cachedMap = jsonStringToMap(GIT_PLUGIN_JSON); + cachedMap.putAll(jsonStringToMap(HG_PLUGIN_JSON)); + cachedMap.putAll(jsonStringToMap(SVN_PLUGIN_JSON)); + when(cache.get(path)).thenReturn(cachedMap); + servlet.doGet(request, response); + verify(servlet, never()).collectJsonFile(path); + verify(cache, never()).put(eq(path), any()); + verify(cache).get(path); + assertJsonMap(jsonStringToMap(captor.getValue())); + } + + @Test + @SuppressWarnings("unchecked") + public void shouldCollectJsonFile() throws IOException { + String path = "locales/de/plugins.json"; + + when(uberClassLoader.getResources(path)).thenReturn(resources); + Optional mapOptional = servlet.collectJsonFile("/" + path); + + assertJsonMap(mapOptional.orElse(null)); + } + + @SuppressWarnings("unchecked") + public void assertJsonMap(Map actual) throws IOException { + assertThat(actual) + .isNotEmpty() + .containsAllEntriesOf(jsonStringToMap(GIT_PLUGIN_JSON)) + .containsAllEntriesOf(jsonStringToMap(HG_PLUGIN_JSON)) + .containsAllEntriesOf(jsonStringToMap(SVN_PLUGIN_JSON)); + } + + public File createFileFromString(String json) throws IOException { + File file = temporaryFolder.newFile(); + Files.write(json.getBytes(Charsets.UTF_8), file); + return file; + } + + private Map jsonStringToMap(String fileAsString) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + return mapper.readValue(fileAsString, Map.class); + } + +}