plugin translation servlet

This commit is contained in:
Mohamed Karray
2018-10-20 14:40:03 +02:00
parent 3ac803ce82
commit 9cb661b460
13 changed files with 357 additions and 69 deletions

View 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");
}
}

View File

@@ -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()

View File

@@ -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<Props> {
@@ -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);

View File

@@ -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"
}
}
}

View File

@@ -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"
}
}
}

View File

@@ -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<Props> {
@@ -17,11 +19,11 @@ class ProtocolInformation extends React.Component<Props> {
}
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);

View File

@@ -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"
}
}
}

View File

@@ -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"
}
}
}

View File

@@ -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<Props> {
@@ -17,7 +19,7 @@ class ProtocolInformation extends React.Component<Props> {
}
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);

View File

@@ -1,5 +1,7 @@
{
"svn": {
"description": "die svn repo ist super "
"scm-svn-plugin": {
"information": {
"checkout" : "Repository auschecken"
}
}
}

View File

@@ -1,5 +1,7 @@
{
"svn": {
"description": "the svn repo is great "
"scm-svn-plugin": {
"information": {
"checkout" : "Checkout repository"
}
}
}

View File

@@ -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<String, HashMap<String, String>> cache;
private RE languagePathPostfix = new RE(".*(\\-[A-Z]+)/.*");
private ObjectMapper objectMapper = new ObjectMapper();
private final Cache<String, Map> 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<String, String> getCollectedJson(String path,
Function<String, Optional<HashMap<String, String>>> jsonFileProvider,
BiConsumer<String, HashMap<String, String>> createdJsonFileConsumer) throws NotFoundException {
private Map getCollectedJson(String path,
Function<String, Optional<Map>> jsonFileProvider,
BiConsumer<String, Map> createdJsonFileConsumer) {
return Optional.ofNullable(jsonFileProvider.apply(path)
.orElseGet(() -> {
Optional<HashMap<String, String>> createdFile = collectJsonFile(path);
Optional<Map> 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<String, Optional<HashMap<String, String>>> jsonFileProvider = usedPath -> Optional.empty();
BiConsumer<String, HashMap<String, String>> createdJsonFileConsumer = (usedPath, foundJsonMap) -> log.info("A json File is created from the path {}", usedPath);
if (SCMContext.getContext().getStage() == Stage.PRODUCTION) {
Function<String, Optional<Map>> jsonFileProvider = usedPath -> Optional.empty();
BiConsumer<String, Map> 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<HashMap<String, String>> collectJsonFile(String path) {
protected Optional<Map> collectJsonFile(String path) {
log.info("Collect plugin translations from path {} for every plugin", path);
HashMap<String, String> result = null;
Map result = null;
try {
Enumeration<URL> 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<String, String> mergeJSONs(ObjectMapper objectMapper, URL url) throws IOException {
byte[] src = Files.readAllBytes(Paths.get(url.getPath()));
Map<String, String> json = objectMapper.readValue(src, HashMap.class);
return objectMapper.readerForUpdating(json).readValue(src);
}
}

View File

@@ -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<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(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<String> 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<String> 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<String> 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<Map> 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);
}
}