Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Basic JSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
djansen-redhat committed Jul 31, 2016
1 parent ba05071 commit d6e90bd
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 26 deletions.
@@ -0,0 +1,144 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.feature.editor;

import org.junit.*;
import org.junit.experimental.categories.Category;
import org.junit.rules.Timeout;
import org.zanata.feature.Feature;
import org.zanata.feature.testharness.TestPlan.DetailedTest;
import org.zanata.feature.testharness.ZanataTestCase;
import org.zanata.page.webtrans.EditorPage;
import org.zanata.util.CleanDocumentStorageRule;
import org.zanata.util.TestFileGenerator;
import org.zanata.util.ZanataRestCaller;
import org.zanata.workflow.BasicWorkFlow;
import org.zanata.workflow.LoginWorkFlow;
import org.zanata.workflow.ProjectWorkFlow;

import java.io.File;

import static org.assertj.core.api.Assertions.assertThat;
import static org.zanata.util.FunctionalTestHelper.assumeFalse;

/**
* @author Damian Jansen <a
* href="mailto:djansen@redhat.com">djansen@redhat.com</a>
*/
@Category(DetailedTest.class)
public class TranslateJSONTest extends ZanataTestCase {

@Rule
public Timeout timeout = new Timeout(ZanataTestCase.MAX_LONG_TEST_DURATION);

@ClassRule
public static CleanDocumentStorageRule documentStorageRule =
new CleanDocumentStorageRule();

private ZanataRestCaller zanataRestCaller = new ZanataRestCaller();
private TestFileGenerator testFileGenerator = new TestFileGenerator();

@BeforeClass
public static void beforeClass() {
new BasicWorkFlow().goToHome().deleteCookiesAndRefresh();
assumeFalse(
"",
new File(CleanDocumentStorageRule.getDocumentStoragePath()
.concat(File.separator).concat("documents")
.concat(File.separator)).exists());
}

@Before
public void before() {
assertThat(new LoginWorkFlow().signIn("admin", "admin").loggedInAs())
.isEqualTo("admin")
.as("Admin is logged in");
}

@Feature(summary = "The user can translate JavaScript Object Notation files",
tcmsTestPlanIds = 5316, tcmsTestCaseIds = 0)
@Test(timeout = ZanataTestCase.MAX_SHORT_TEST_DURATION)
public void translateBasicJSONFile() {
// Create a file with nested content and array
File testfile = testFileGenerator.generateTestFileWithContent(
"basicjson", ".json",
"{ \"test\": { \"title\": \"Line One\", \"test1\": { \"title\": \"Line Two\", " +
"\"test2\": { \"test3\": { \"ID\": \"Line Three\", " +
"\"testarray\": [\"first\", \"second\"] } } } } }");
zanataRestCaller.createProjectAndVersion("json-translate",
"json", "file");

EditorPage editorPage = new ProjectWorkFlow()
.goToProjectByName("json-translate")
.gotoVersion("json")
.gotoSettingsTab()
.gotoSettingsDocumentsTab()
.pressUploadFileButton()
.enterFilePath(testfile.getAbsolutePath())
.submitUpload()
.clickUploadDone()
.gotoLanguageTab()
.translate("fr", testfile.getName());

assertThat(editorPage.getMessageSourceAtRowIndex(0))
.isEqualTo("Line One")
.as("Item 1 shows Line One");
assertThat(editorPage.getMessageSourceAtRowIndex(1))
.isEqualTo("Line Two")
.as("Item 2 shows Line Two");
assertThat(editorPage.getMessageSourceAtRowIndex(2))
.isEqualTo("Line Three")
.as("Item 3 shows Line Three");
assertThat(editorPage.getMessageSourceAtRowIndex(3))
.isEqualTo("First")
.as("Item 4 (from array) shows First");
assertThat(editorPage.getMessageSourceAtRowIndex(4))
.isEqualTo("Second")
.as("Item 5 (from array) shows Second");

editorPage = editorPage
.translateTargetAtRowIndex(0, "Une Ligne")
.approveTranslationAtRow(0)
.translateTargetAtRowIndex(1, "Deux Ligne")
.approveTranslationAtRow(1)
.translateTargetAtRowIndex(2, "Ligne Trois")
.approveTranslationAtRow(2);

assertTranslations(editorPage);

// Close and reopen the editor to test save
editorPage.reload();

assertTranslations(editorPage);
}

private void assertTranslations(EditorPage editorPage) {
assertThat(editorPage.getBasicTranslationTargetAtRowIndex(0))
.isEqualTo("Une Ligne")
.as("Item 1 shows a translation of Line One");
assertThat(editorPage.getBasicTranslationTargetAtRowIndex(1))
.isEqualTo("Deux Ligne")
.as("Item 2 shows a translation of Line Two");
assertThat(editorPage.getBasicTranslationTargetAtRowIndex(2))
.isEqualTo("Ligne Trois")
.as("Item 3 shows a translation of Line Three");
}
}
Expand Up @@ -131,6 +131,7 @@
TranslateOpenOfficeTest.class,
TranslateTextTest.class,
TranslationHistoryTest.class,
TranslateJSONTest.class,

/*
* Glossary
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Expand Up @@ -930,6 +930,18 @@
</exclusions>
</dependency>

<dependency>
<groupId>net.sf.okapi.filters</groupId>
<artifactId>okapi-filter-json</artifactId>
<version>${okapi.version}</version>
<exclusions>
<exclusion>
<groupId>net.sf.okapi.logbind</groupId>
<artifactId>build-log4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>net.sf.okapi.filters</groupId>
<artifactId>okapi-filter-openoffice</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions zanata-war/pom.xml
Expand Up @@ -1738,6 +1738,16 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.okapi.filters</groupId>
<artifactId>okapi-filter-json</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sf.okapi.filters</groupId>
<artifactId>okapi-filter-idml</artifactId>
Expand Down
49 changes: 49 additions & 0 deletions zanata-war/src/main/java/org/zanata/adapter/JsonAdapter.java
@@ -0,0 +1,49 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.adapter;

import net.sf.okapi.common.IParameters;
import net.sf.okapi.filters.json.JSONFilter;
import net.sf.okapi.filters.json.Parameters;

/**
* Adapter to handle JavaScript Object Notation (JSON) documents.
* It uses the Okapi's {@link net.sf.okapi.filters.json.JSONFilter} class.
* @see <a href="http://www.json.org/">JSON Specification</a>
* @author Damian Jansen
* <a href="mailto:djansen@redhat.com">djansen@redhat.com</a>
*/
public class JsonAdapter extends OkapiFilterAdapter {

public JsonAdapter() {
super(prepareFilter(), IdSource.contentHash, true, true);
}

private static JSONFilter prepareFilter() {
return new JSONFilter();
}

@Override
protected void updateParamsWithDefaults(IParameters params) {
Parameters p = (Parameters) params;
p.fromString("extractIsolatedStrings.b=true");
}
}
Expand Up @@ -33,18 +33,7 @@

import org.apache.deltaspike.jpa.api.transaction.Transactional;
import org.xml.sax.InputSource;
import org.zanata.adapter.DTDAdapter;
import org.zanata.adapter.FileFormatAdapter;
import org.zanata.adapter.GettextAdapter;
import org.zanata.adapter.HTMLAdapter;
import org.zanata.adapter.IDMLAdapter;
import org.zanata.adapter.OpenOfficeAdapter;
import org.zanata.adapter.PlainTextAdapter;
import org.zanata.adapter.PropertiesLatinOneAdapter;
import org.zanata.adapter.PropertiesUTF8Adapter;
import org.zanata.adapter.SubtitleAdapter;
import org.zanata.adapter.TSAdapter;
import org.zanata.adapter.XliffAdapter;
import org.zanata.adapter.*;
import org.zanata.adapter.po.PoReader2;
import org.zanata.common.DocumentType;
import org.zanata.common.LocaleId;
Expand All @@ -71,20 +60,7 @@
import java.util.Map;
import java.util.Set;

import static org.zanata.common.DocumentType.GETTEXT;
import static org.zanata.common.DocumentType.HTML;
import static org.zanata.common.DocumentType.IDML;
import static org.zanata.common.DocumentType.OPEN_DOCUMENT_GRAPHICS;
import static org.zanata.common.DocumentType.OPEN_DOCUMENT_PRESENTATION;
import static org.zanata.common.DocumentType.OPEN_DOCUMENT_SPREADSHEET;
import static org.zanata.common.DocumentType.OPEN_DOCUMENT_TEXT;
import static org.zanata.common.DocumentType.PLAIN_TEXT;
import static org.zanata.common.DocumentType.PROPERTIES;
import static org.zanata.common.DocumentType.PROPERTIES_UTF8;
import static org.zanata.common.DocumentType.SUBTITLE;
import static org.zanata.common.DocumentType.TS;
import static org.zanata.common.DocumentType.XLIFF;
import static org.zanata.common.DocumentType.XML_DOCUMENT_TYPE_DEFINITION;
import static org.zanata.common.DocumentType.*;

/**
* Default implementation of the TranslationFileService interface.
Expand All @@ -111,6 +87,7 @@ public class TranslationFileServiceImpl implements TranslationFileService {
DOCTYPEMAP.put(XML_DOCUMENT_TYPE_DEFINITION, DTDAdapter.class);
DOCTYPEMAP.put(IDML, IDMLAdapter.class);
DOCTYPEMAP.put(HTML, HTMLAdapter.class);
DOCTYPEMAP.put(JSON, JsonAdapter.class);
DOCTYPEMAP.put(SUBTITLE, SubtitleAdapter.class);
DOCTYPEMAP.put(PROPERTIES, PropertiesLatinOneAdapter.class);
DOCTYPEMAP.put(PROPERTIES_UTF8, PropertiesUTF8Adapter.class);
Expand Down

0 comments on commit d6e90bd

Please sign in to comment.