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

Update TM and Glossary uploads: '--language' is required for creating new instances #419

Merged
merged 2 commits into from
Jan 11, 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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies {

implementation 'info.picocli:picocli:4.6.1'

implementation 'com.github.crowdin:crowdin-api-client-java:1.3.14'
implementation 'com.github.crowdin:crowdin-api-client-java:1.3.17'

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
testImplementation 'org.hamcrest:hamcrest:2.2'
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ NewAction<PropertiesWithFiles, ProjectClient> uploadTranslations(
NewAction<BaseProperties, ClientGlossary> glossaryList(boolean plainView, boolean isVerbose);

NewAction<BaseProperties, ClientGlossary> glossaryUpload(
java.io.File file, Long id, String name, Map<String, Integer> scheme, Boolean firstLineContainsHeader);
java.io.File file, Long id, String name, String languageId, Map<String, Integer> scheme, Boolean firstLineContainsHeader);

NewAction<BaseProperties, ClientGlossary> glossaryDownload(
Long id, String name, GlossariesFormat format, boolean noProgress, File to, FilesInterface files);

NewAction<BaseProperties, ClientTm> tmList(boolean plainView);

NewAction<BaseProperties, ClientTm> tmUpload(
File file, Long id, String name, Map<String, Integer> scheme, Boolean firstLineContainsHeader);
File file, Long id, String name, String languageId, Map<String, Integer> scheme, Boolean firstLineContainsHeader);

NewAction<BaseProperties, ClientTm> tmDownload(
Long id, String name, TranslationMemoryFormat format, String sourceLanguageId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ public NewAction<BaseProperties, ClientGlossary> glossaryList(boolean plainView,

@Override
public NewAction<BaseProperties, ClientGlossary> glossaryUpload(
java.io.File file, Long id, String name, Map<String, Integer> scheme, Boolean firstLineContainsHeader
java.io.File file, Long id, String name, String languageId, Map<String, Integer> scheme, Boolean firstLineContainsHeader
) {
return new GlossaryUploadAction(file, id, name, scheme, firstLineContainsHeader);
return new GlossaryUploadAction(file, id, name, languageId, scheme, firstLineContainsHeader);
}

@Override
Expand All @@ -146,9 +146,9 @@ public NewAction<BaseProperties, ClientTm> tmList(boolean plainView) {

@Override
public NewAction<BaseProperties, ClientTm> tmUpload(
File file, Long id, String name, Map<String, Integer> scheme, Boolean firstLineContainsHeader
File file, Long id, String name, String languageId, Map<String, Integer> scheme, Boolean firstLineContainsHeader
) {
return new TmUploadAction(file, id, name, scheme, firstLineContainsHeader);
return new TmUploadAction(file, id, name, languageId, scheme, firstLineContainsHeader);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.crowdin.client.glossaries.model.AddGlossaryRequest;
import com.crowdin.client.glossaries.model.Glossary;
import lombok.NonNull;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.InputStream;
Expand All @@ -25,13 +26,15 @@ class GlossaryUploadAction implements NewAction<BaseProperties, ClientGlossary>
private final java.io.File file;
private final Long id;
private final String name;
private final String languageId;
private final Map<String, Integer> scheme;
private final Boolean firstLineContainsHeader;

public GlossaryUploadAction(@NonNull java.io.File file, Long id, String name, Map<String, Integer> scheme, Boolean firstLineContainsHeader) {
public GlossaryUploadAction(@NonNull java.io.File file, Long id, String name, String languageId, Map<String, Integer> scheme, Boolean firstLineContainsHeader) {
this.file = file;
this.id = id;
this.name = name;
this.languageId = languageId;
this.scheme = scheme;
this.firstLineContainsHeader = firstLineContainsHeader;
}
Expand All @@ -48,16 +51,19 @@ public void act(Outputter out, BaseProperties pb, ClientGlossary client) {
.filter(gl -> gl.getName() != null && gl.getName().equals(name))
.collect(Collectors.toList());
if (foundGlossaries.isEmpty()) {
targetGlossary = client.addGlossary(RequestBuilder.addGlossary(name));
if (StringUtils.isEmpty(languageId)) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.glossary.no_language_id"));
}
targetGlossary = client.addGlossary(RequestBuilder.addGlossary(name, languageId));
} else if (foundGlossaries.size() == 1) {
targetGlossary = foundGlossaries.get(0);
} else {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.glossary.more_than_one_glossary_by_that_name"));
}
} else {
AddGlossaryRequest addGlossaryRequest = (isOrganization)
? RequestBuilder.addGlossaryEnterprise(String.format(DEFAULT_GLOSSARY_NAME, file.getName()), 0L)
: RequestBuilder.addGlossary(String.format(DEFAULT_GLOSSARY_NAME, file.getName()));
? RequestBuilder.addGlossaryEnterprise(String.format(DEFAULT_GLOSSARY_NAME, file.getName()), languageId, 0L)
: RequestBuilder.addGlossary(String.format(DEFAULT_GLOSSARY_NAME, file.getName()), languageId);
targetGlossary = client.addGlossary(addGlossaryRequest);
}
Long storageId;
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/crowdin/cli/commands/actions/TmUploadAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.crowdin.cli.properties.BaseProperties;
import com.crowdin.client.translationmemory.model.AddTranslationMemoryRequest;
import com.crowdin.client.translationmemory.model.TranslationMemory;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -25,13 +26,15 @@ class TmUploadAction implements NewAction<BaseProperties, ClientTm> {
private final File file;
private final Long id;
private final String name;
private final String languageId;
private final Map<String, Integer> scheme;
private final Boolean firstLineContainsHeader;

public TmUploadAction(File file, Long id, String name, Map<String, Integer> scheme, Boolean firstLineContainsHeader) {
public TmUploadAction(File file, Long id, String name, String languageId, Map<String, Integer> scheme, Boolean firstLineContainsHeader) {
this.file = file;
this.id = id;
this.name = name;
this.languageId = languageId;
this.scheme = scheme;
this.firstLineContainsHeader = firstLineContainsHeader;
}
Expand Down Expand Up @@ -60,16 +63,19 @@ private TranslationMemory getTm(ClientTm client, boolean isEnterprise) {
.filter(gl -> gl.getName() != null && gl.getName().equals(name))
.collect(Collectors.toList());
if (foundTms.isEmpty()) {
return client.addTm(RequestBuilder.addTm(name));
if (StringUtils.isEmpty(languageId)) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.tm.no_language_id"));
}
return client.addTm(RequestBuilder.addTm(name, languageId));
} else if (foundTms.size() == 1) {
return foundTms.get(0);
} else {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.tm.more_than_one_tm_by_that_name"));
}
} else {
AddTranslationMemoryRequest addTmRequest = (isEnterprise)
? RequestBuilder.addTmEnterprise(String.format(DEFAULT_TM_NAME, file.getName()), 0L)
: RequestBuilder.addTm(String.format(DEFAULT_TM_NAME, file.getName()));
? RequestBuilder.addTmEnterprise(String.format(DEFAULT_TM_NAME, file.getName()), languageId, 0L)
: RequestBuilder.addTm(String.format(DEFAULT_TM_NAME, file.getName()), languageId);
return client.addTm(addTmRequest);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ public static PatchRequest patch(Object value, PatchOperation op, String path) {
return request;
}

public static AddGlossaryRequest addGlossary(String name) {
public static AddGlossaryRequest addGlossary(String name, String languageId) {
AddGlossaryRequest request = new AddGlossaryRequest();
request.setName(name);
request.setLanguageId(languageId);
return request;
}

public static AddGlossaryRequest addGlossaryEnterprise(String name, Long groupId) {
public static AddGlossaryRequest addGlossaryEnterprise(String name, String languageId, Long groupId) {
AddGlossaryRequest request = new AddGlossaryRequest();
request.setName(name);
request.setLanguageId(languageId);
request.setGroupId(groupId);
return request;
}
Expand All @@ -85,15 +87,17 @@ public static ExportGlossaryRequest exportGlossary(GlossariesFormat format) {
return request;
}

public static AddTranslationMemoryRequest addTm(String name) {
public static AddTranslationMemoryRequest addTm(String name, String languageId) {
AddTranslationMemoryRequest request = new AddTranslationMemoryRequest();
request.setName(name);
request.setLanguageId(languageId);
return request;
}

public static AddTranslationMemoryRequest addTmEnterprise(String name, Long groupId) {
public static AddTranslationMemoryRequest addTmEnterprise(String name, String languageId, Long groupId) {
AddTranslationMemoryRequest request = new AddTranslationMemoryRequest();
request.setName(name);
request.setLanguageId(languageId);
request.setGroupId(groupId);
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class GlossaryUploadSubcommand extends ActCommandGlossary {
@CommandLine.Option(names = {"--name"}, paramLabel = "...")
private String name;

@CommandLine.Option(names = {"--language"})
private String languageId;

@CommandLine.Option(names = {"--scheme"}, paramLabel = "...")
private Map<String, Integer> scheme;

Expand All @@ -35,7 +38,7 @@ class GlossaryUploadSubcommand extends ActCommandGlossary {

@Override
protected NewAction<BaseProperties, ClientGlossary> getAction(Actions actions) {
return actions.glossaryUpload(file, id, name, scheme, firstLineContainsHeader);
return actions.glossaryUpload(file, id, name, languageId, scheme, firstLineContainsHeader);
}

@Override
Expand All @@ -58,6 +61,9 @@ protected List<String> checkOptions() {
if (id != null && name != null) {
errors.add(RESOURCE_BUNDLE.getString("error.glossary.id_and_name"));
}
if (id == null && name == null && languageId == null) {
errors.add(RESOURCE_BUNDLE.getString("error.glossary.no_language_id"));
}
return errors;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class TmUploadSubcommand extends ActCommandTm {
@CommandLine.Option(names = {"--name"}, paramLabel = "...")
private String name;

@CommandLine.Option(names = {"--language"})
private String languageId;

@CommandLine.Option(names = {"--scheme"}, paramLabel = "...")
private Map<String, Integer> scheme;

Expand All @@ -35,7 +38,7 @@ class TmUploadSubcommand extends ActCommandTm {

@Override
protected NewAction<BaseProperties, ClientTm> getAction(Actions actions) {
return actions.tmUpload(file, id, name, scheme, firstLineContainsHeader);
return actions.tmUpload(file, id, name, languageId, scheme, firstLineContainsHeader);
}

@Override
Expand All @@ -51,6 +54,9 @@ protected List<String> checkOptions() {
if (!equalsAny(FilenameUtils.getExtension(file.getName()), "csv", "xls", "xlsx") && firstLineContainsHeader != null) {
errors.add(RESOURCE_BUNDLE.getString("error.tm.first_line_contains_header_and_wrong_format"));
}
if (id == null && name == null && languageId == null) {
errors.add(RESOURCE_BUNDLE.getString("error.tm.no_language_id"));
}
return errors;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ error.glossary.scheme_and_wrong_format=Scheme is used only for CSV or XLS/XLSX f
error.glossary.scheme_is_required=Scheme is required for CSV or XLS/XLSX files
error.glossary.first_line_contains_header_and_wrong_format='--first-line-contains-header' is used only for CSV or XLS/XLSX files
error.glossary.no_permission=You do not have permission to this glossary.
error.glossary.no_language_id='--language' is required for creating new glossary

error.tm.build_tm=Failed to build the translation memory
error.tm.not_found_by_id=Couldn't find translation memory by the specified ID
Expand All @@ -331,6 +332,7 @@ error.tm.scheme_is_required=Scheme is required for CSV or XLS/XLSX files
error.tm.first_line_contains_header_and_wrong_format='--first-line-contains-header' is used only for CSV or XLS/XLSX files
error.tm.target_language_id_is_null='--target-language-id' must be specified along with '--source-language-id'
error.tm.source_language_id_is_null='--source-language-id' must be specified along with '--target-language-id'
error.tm.no_language_id='--language' is required for creating new translation memory

error.pre_translate.engine_id=Machine Translation should be used with 'engineId' parameter
error.pre_translate.duplicate_translations='--duplicate-translations' works only with TM pre-translation method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testGlossaryList() {

@Test
public void testGlossaryUpload() {
assertNotNull(actions.glossaryUpload(new File("nowhere.txt"), null, null, null, null));
assertNotNull(actions.glossaryUpload(new File("nowhere.txt"), null, null, null, null, null));
}

@Test
Expand All @@ -99,7 +99,7 @@ public void testTmList() {

@Test
public void testTmUpload() {
assertNotNull(actions.tmUpload(null, null, null, null, null));
assertNotNull(actions.tmUpload(null, null, null, null, null, null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void beforeEach() {

@Test
public void test_withId() {
action = new GlossaryUploadAction(file, glossaryId, null, null, null);
action = new GlossaryUploadAction(file, glossaryId, null, null, null, null);
action.act(Outputter.getDefault(), pb, clientMock);

verify(clientMock).getGlossary(eq(glossaryId));
Expand All @@ -90,7 +90,7 @@ public void test_withId() {

@Test
public void test_withName() {
action = new GlossaryUploadAction(file, null, glossaryNameUnique, null, null);
action = new GlossaryUploadAction(file, null, glossaryNameUnique, null, null, null);
action.act(Outputter.getDefault(), pb, clientMock);

verify(clientMock).listGlossaries();
Expand All @@ -101,7 +101,7 @@ public void test_withName() {

@Test
public void test_noIdentifiers_createNew() {
action = new GlossaryUploadAction(file, null, null, null, null);
action = new GlossaryUploadAction(file, null, null, null, null, null);
action.act(Outputter.getDefault(), pb, clientMock);

verify(clientMock).addGlossary(any());
Expand All @@ -112,19 +112,16 @@ public void test_noIdentifiers_createNew() {

@Test
public void test_withName_notFound_createNew() {
action = new GlossaryUploadAction(file, null, glossaryNameNotExist, null, null);
action.act(Outputter.getDefault(), pb, clientMock);
action = new GlossaryUploadAction(file, null, glossaryNameNotExist, null, null, null);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, clientMock));

verify(clientMock).listGlossaries();
verify(clientMock).addGlossary(any());
verify(clientMock).uploadStorage(any(), any());
verify(clientMock).importGlossary(eq(glossaryId), any());
verifyNoMoreInteractions(clientMock);
}

@Test
public void test_withId_throwsNotFound() {
action = new GlossaryUploadAction(file, glossaryIdNotExist, null, null, null);
action = new GlossaryUploadAction(file, glossaryIdNotExist, null, null, null, null);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, clientMock));

verify(clientMock).getGlossary(eq(glossaryIdNotExist));
Expand All @@ -133,7 +130,7 @@ public void test_withId_throwsNotFound() {

@Test
public void test_withName_throwsTooManyWithTargetName() {
action = new GlossaryUploadAction(file, null, glossaryNameRepeated, null, null);
action = new GlossaryUploadAction(file, null, glossaryNameRepeated, null, null, null);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, clientMock));

verify(clientMock).listGlossaries();
Expand All @@ -142,7 +139,7 @@ public void test_withName_throwsTooManyWithTargetName() {

@Test
public void test_standard_throwsFileNotFound() {
action = new GlossaryUploadAction(fileNotExist, glossaryId, null, null, null);
action = new GlossaryUploadAction(fileNotExist, glossaryId, null, null, null, null);
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, clientMock));

verify(clientMock).getGlossary(eq(glossaryId));
Expand Down
Loading