diff --git a/package-lock.json b/package-lock.json index 7d61233e4..de8a4ffb3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -283,4 +283,4 @@ } } } -} +} \ No newline at end of file diff --git a/src/test/java/com/crowdin/cli/commands/functionality/PropertiesBuilderTest.java b/src/test/java/com/crowdin/cli/commands/functionality/PropertiesBuilderTest.java index addc476ef..520db8489 100644 --- a/src/test/java/com/crowdin/cli/commands/functionality/PropertiesBuilderTest.java +++ b/src/test/java/com/crowdin/cli/commands/functionality/PropertiesBuilderTest.java @@ -1,10 +1,7 @@ package com.crowdin.cli.commands.functionality; import com.crowdin.cli.commands.Outputter; -import com.crowdin.cli.properties.ParamsWithFiles; -import com.crowdin.cli.properties.PropertiesWithFiles; -import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; -import com.crowdin.cli.properties.PropertiesBuilders; +import com.crowdin.cli.properties.*; import com.crowdin.cli.properties.helper.TempProject; import com.crowdin.cli.utils.Utils; import org.junit.jupiter.api.AfterEach; @@ -15,8 +12,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class PropertiesBuilderTest { + public static final String TEST_API_TOKEN = "123abc456"; + public static final String TEST_BASE_URL = "https://crowdin.com"; + public static final String TEST_BASE_PATH = "."; private TempProject tempProject; @@ -95,4 +97,88 @@ public void testOkBasePath_Params_WithConfigFile() { assertEquals(tempProject.getBasePath() + "folder2" + Utils.PATH_SEPARATOR, pb.getBasePath()); } + + @Test + public void testBuildNoProperties() { + PropertiesBuilders pb = mock(PropertiesBuilders.class); + NoProperties np = mock(NoProperties.class); + when(pb.buildNoProperties()).thenReturn(np); + } + + @Test + public void testBuildBaseProperties() { + SettingsBean set = new SettingsBean(); + BaseProperties bp = new BaseProperties(); + String bpath = "."; + String url = "https://crowdin.com"; + String api = "123abc456"; + bp.setBasePath(bpath); + bp.setBaseUrl(url); + bp.setApiToken(api); + bp.setSettings(set); + + assertEquals(bp.getBasePath(), "."); + assertEquals(bp.getBaseUrl(), "https://crowdin.com"); + assertEquals(bp.getApiToken(), "123abc456"); + assertEquals(bp.getSettings(), set); + } + + @Test + public void testBasePropertiesWithNoValues() { + BaseProperties bp = new BaseProperties(); + String bpath = ""; + String url = ""; + String api = ""; + bp.setBasePath(bpath); + bp.setBaseUrl(url); + bp.setApiToken(api); + + assertEquals(bp.getBasePath(), ""); + assertEquals(bp.getBaseUrl(), ""); + assertEquals(bp.getApiToken(), ""); + } + + @Test + public void testProjectProperties() { + ProjectParams params = new ProjectParams(){{ + setIdParam("123"); + setTokenParam("token"); + }}; + + ProjectProperties pp = propertiesBuilders.buildProjectProperties(out, null, null, params); + String projectId = "123"; + pp.setProjectId(projectId); + + assertEquals(pp.getProjectId(), params.getIdParam()); + assertEquals(pp.getApiToken(), params.getTokenParam()); + } + + @Test + public void testBuildNoTargets() { + assertThrows(RuntimeException.class, () -> propertiesBuilders.buildPropertiesWithTargets(out, null, null, null)); + } + + @Test + public void testBuildNoConfigFileTargets() { + ParamsWithTargets okParams = new ParamsWithTargets(); + assertThrows(NullPointerException.class, () -> propertiesBuilders.buildPropertiesWithTargets(out, null, null, okParams)); + } + + @Test + public void testPropertiesWithTarget() { + File configFile = new File("folder/crowdinTest.yml"); + String minimalConfigFileText = NewPropertiesWithTargetsUtilBuilder + .minimalBuilt().buildToString(); + configFile = tempProject.addFile(configFile.getPath(), minimalConfigFileText); + + ParamsWithTargets okParams = new ParamsWithTargets(); + okParams.setSkipTranslatedOnly(true); + + System.out.println("configText = " + minimalConfigFileText); + PropertiesWithTargets pb = propertiesBuilders.buildPropertiesWithTargets(out, configFile, null, okParams); + + assertEquals(pb.getTargets().size(), 1); + assertEquals(pb.getTargets().get(0).getName(), "android"); + assertEquals(pb.getProjectId(), "666"); + } } diff --git a/src/test/java/com/crowdin/cli/properties/NewPropertiesWithTargetsUtilBuilder.java b/src/test/java/com/crowdin/cli/properties/NewPropertiesWithTargetsUtilBuilder.java index 18528c2d6..96076c71a 100644 --- a/src/test/java/com/crowdin/cli/properties/NewPropertiesWithTargetsUtilBuilder.java +++ b/src/test/java/com/crowdin/cli/properties/NewPropertiesWithTargetsUtilBuilder.java @@ -1,5 +1,8 @@ package com.crowdin.cli.properties; +import java.util.ArrayList; +import java.util.List; + public class NewPropertiesWithTargetsUtilBuilder { public static final String TEST_API_TOKEN = "123abc456"; @@ -9,14 +12,19 @@ public class NewPropertiesWithTargetsUtilBuilder { private PropertiesWithTargets pb; public static NewPropertiesWithTargetsUtilBuilder minimalBuilt() { - return minimalBuilt(TEST_API_TOKEN, TEST_BASE_URL, TEST_BASE_PATH); + List targetBeans = new ArrayList(); + TargetBean bean = new TargetBean(); + bean.setName("android"); + targetBeans.add(bean); + return minimalBuilt(TEST_API_TOKEN, TEST_BASE_URL, TEST_BASE_PATH, targetBeans); } - public static NewPropertiesWithTargetsUtilBuilder minimalBuilt(String apiToken, String baseUrl, String basePath) { + public static NewPropertiesWithTargetsUtilBuilder minimalBuilt(String apiToken, String baseUrl, String basePath, List targets) { PropertiesWithTargets pb = new PropertiesWithTargets(); pb.setApiToken(apiToken); pb.setBaseUrl(baseUrl); pb.setBasePath(basePath); + pb.setTargets(targets); NewPropertiesWithTargetsUtilBuilder builder = new NewPropertiesWithTargetsUtilBuilder(); builder.pb = pb; return builder; @@ -30,4 +38,31 @@ public NewPropertiesWithTargetsUtilBuilder setApiToken(String apiToken) { public PropertiesWithTargets build() { return pb; } + + public String buildToString() { + StringBuilder sb = new StringBuilder(); + sb.append("\"project_id\": \"").append("666").append("\"\n"); + + if (pb.getApiToken() != null) { + sb.append("\"api_token\": \"").append(pb.getApiToken()).append("\"\n"); + } + if (pb.getBasePath() != null) { + sb.append("\"base_path\": \"").append(pb.getBasePath().replaceAll("\\\\", "\\\\\\\\")).append("\"\n"); + } + if (pb.getBaseUrl() != null) { + sb.append("\"base_url\": \"").append(pb.getBaseUrl()).append("\"\n"); + } + if (pb.getTargets() != null && !pb.getTargets().isEmpty()) { + sb.append("targets: [\n"); + for (TargetBean tb : pb.getTargets()) { + sb.append("{\n"); + if (tb.getName() != null) { + sb.append("\"name\": \"").append(tb.getName().replaceAll("\\\\", "\\\\\\\\")).append("\",\n"); + } + sb.append("},\n"); + } + sb.append("]"); + } + return sb.toString(); + } } diff --git a/src/test/resources/crowdinTest.yml b/src/test/resources/crowdinTest.yml new file mode 100644 index 000000000..4d4abafb9 --- /dev/null +++ b/src/test/resources/crowdinTest.yml @@ -0,0 +1,145 @@ +# +# Your Crowdin credentials +# +"project_id" : "" +"api_token" : "" +"base_path" : "" +"base_url" : "" + +# +# Choose file structure in Crowdin +# e.g. true or false +# +"preserve_hierarchy": true + +# +# Files configuration +# +files: [ + { + # + # Source files filter + # e.g. "/resources/en/*.json" + # + "source" : "", + + # + # Where translations will be placed + # e.g. "/resources/%two_letters_code%/%original_file_name%" + # + "translation" : "", + + # + # Files or directories for ignore + # e.g. ["/**/?.txt", "/**/[0-9].txt", "/**/*\?*.txt"] + # + #"ignore" : [], + + # + # The dest allows you to specify a file name in Crowdin + # e.g. "/messages.json" + # + #"dest" : "", + + # + # File type + # e.g. "json" + # + #"type" : "", + + # + # The parameter "update_option" is optional. If it is not set, after the files update the translations for changed strings will be removed. Use to fix typos and for minor changes in the source strings + # e.g. "update_as_unapproved" or "update_without_changes" + # + #"update_option" : "", + + # + # Start block (for XML only) + # + + # + # Defines whether to translate tags attributes. + # e.g. 0 or 1 (Default is 1) + # + # "translate_attributes" : 1, + + # + # Defines whether to translate texts placed inside the tags. + # e.g. 0 or 1 (Default is 1) + # + # "translate_content" : 1, + + # + # This is an array of strings, where each item is the XPaths to DOM element that should be imported + # e.g. ["/content/text", "/content/text[@value]"] + # + # "translatable_elements" : [], + + # + # Defines whether to split long texts into smaller text segments + # e.g. 0 or 1 (Default is 1) + # + # "content_segmentation" : 1, + + # + # End block (for XML only) + # + + # + # Start .properties block + # + + # + # Defines whether single quote should be escaped by another single quote or backslash in exported translations + # e.g. 0 or 1 or 2 or 3 (Default is 3) + # 0 - do not escape single quote; + # 1 - escape single quote by another single quote; + # 2 - escape single quote by backslash; + # 3 - escape single quote by another single quote only in strings containing variables ( {0} ). + # + # "escape_quotes" : 3, + + # + # Defines whether any special characters (=, :, ! and #) should be escaped by backslash in exported translations. + # e.g. 0 or 1 (Default is 0) + # 0 - do not escape special characters + # 1 - escape special characters by a backslash + # + # "escape_special_characters": 0 + # + + # + # End .properties block + # + + # + # Does the first line contain header? + # e.g. true or false + # + #"first_line_contains_header" : true, + + # + # for spreadsheets + # e.g. "identifier,source_phrase,context,uk,ru,fr" + # + # "scheme" : "", + } +] + +targets: [ + { + name: "android", + files: [ + { + file: "targets/%two_letters_code%/android.xml", + sources: [ + "file.xlsx" + ], + labels: [ + "mobile", + "ui" + ] + } + ] + } +] \ No newline at end of file