Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating test coverage #500

Merged
merged 5 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
@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
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
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");
}
}
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<TargetBean> targetBeans = new ArrayList<TargetBean>();
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<TargetBean> 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;
Expand All @@ -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();
}
}
145 changes: 145 additions & 0 deletions src/test/resources/crowdinTest.yml
Original file line number Diff line number Diff line change
@@ -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"
]
}
]
}
]