diff --git a/src/main/java/com/crowdin/cli/commands/Actions.java b/src/main/java/com/crowdin/cli/commands/Actions.java index 35272203d..1e3f4f041 100644 --- a/src/main/java/com/crowdin/cli/commands/Actions.java +++ b/src/main/java/com/crowdin/cli/commands/Actions.java @@ -90,13 +90,13 @@ NewAction tmDownload( NewAction taskAdd( boolean noProgress, String title, Integer type, String language, List files, String branch, Long workflowStep, String description, Boolean skipAssignedStrings, Boolean includePreTranslatedStringsOnly, - List labels, ProjectClient projectClient, boolean plainView); + List labels, boolean plainView); NewAction distributionList(boolean plainView); - NewAction distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List files, List bundleIds, String branch, ProjectClient projectClient); + NewAction distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List files, List bundleIds, String branch); - NewAction distributionRelease(boolean noProgress, boolean plainView, String hash, ProjectClient projectClient); + NewAction distributionRelease(boolean noProgress, boolean plainView, String hash); NewAction commentList(boolean plainView, boolean isVerbose, String stringId, com.crowdin.client.stringcomments.model.Type type, com.crowdin.client.issues.model.Type issueType, IssueStatus status); @@ -126,7 +126,7 @@ NewAction preTranslate( NewAction screenshotList(Long stringId, boolean plainView); - NewAction screenshotUpload(File file, String branchName, List labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient); + NewAction screenshotUpload(File file, String branchName, List labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress); NewAction screenshotDelete(Long id); diff --git a/src/main/java/com/crowdin/cli/commands/actions/CliActions.java b/src/main/java/com/crowdin/cli/commands/actions/CliActions.java index 1c01c3d9b..572c13da2 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/CliActions.java +++ b/src/main/java/com/crowdin/cli/commands/actions/CliActions.java @@ -172,9 +172,9 @@ public NewAction taskList(boolean plainView, bool @Override public NewAction taskAdd(boolean noProgress, String title, Integer type, String language, List files, String branch, Long workflowStep, String description, Boolean skipAssignedStrings, - Boolean includePreTranslatedStringsOnly, List labels, ProjectClient projectClient, boolean plainView + Boolean includePreTranslatedStringsOnly, List labels, boolean plainView ) { - return new TaskAddAction(noProgress, title, type, language, files, branch, workflowStep, description, skipAssignedStrings, includePreTranslatedStringsOnly, labels, projectClient, plainView); + return new TaskAddAction(noProgress, title, type, language, files, branch, workflowStep, description, skipAssignedStrings, includePreTranslatedStringsOnly, labels, plainView); } @Override @@ -183,13 +183,13 @@ public NewAction distributionList(boolean } @Override - public NewAction distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List files, List bundleIds, String branch, ProjectClient projectClient) { - return new DistributionAddAction(noProgress, plainView, name, exportMode, files, bundleIds, branch, projectClient); + public NewAction distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List files, List bundleIds, String branch) { + return new DistributionAddAction(noProgress, plainView, name, exportMode, files, bundleIds, branch); } @Override - public NewAction distributionRelease(boolean noProgress, boolean plainView, String hash, ProjectClient projectClient) { - return new DistributionReleaseAction(noProgress, plainView, hash, projectClient); + public NewAction distributionRelease(boolean noProgress, boolean plainView, String hash) { + return new DistributionReleaseAction(noProgress, plainView, hash); } @Override @@ -264,8 +264,8 @@ public NewAction screenshotList(Long string } @Override - public NewAction screenshotUpload(File file, String branchName, List labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient) { - return new ScreenshotUploadAction(file, branchName, labelNames, filePath, directoryPath, autoTag, plainView, noProgress, projectClient); + public NewAction screenshotUpload(File file, String branchName, List labelNames, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress) { + return new ScreenshotUploadAction(file, branchName, labelNames, filePath, directoryPath, autoTag, plainView, noProgress); } @Override diff --git a/src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java b/src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java index 8e15a9c32..fae6ff908 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java @@ -2,11 +2,11 @@ import com.crowdin.cli.client.ClientDistribution; import com.crowdin.cli.client.CrowdinProjectFull; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.commands.functionality.RequestBuilder; import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.utils.Utils; import com.crowdin.cli.utils.console.ConsoleSpinner; @@ -41,16 +41,15 @@ class DistributionAddAction implements NewAction bundleIds; private String branch; - private ProjectClient projectClient; - @Override public void act(Outputter out, ProjectProperties pb, ClientDistribution client) { + var projectClient = GenericActCommand.getProjectClient(pb); CrowdinProjectFull project = ConsoleSpinner.execute( out, "message.spinner.fetching_project_info", "error.collect_project_info", this.noProgress, this.plainView, - () -> this.projectClient.downloadFullProject(this.branch) + () -> projectClient.downloadFullProject(this.branch) ); boolean isStringsBasedProject = Objects.equals(project.getType(), Type.STRINGS_BASED); @@ -64,12 +63,12 @@ public void act(Outputter out, ProjectProperties pb, ClientDistribution client) List projectFiles = project.getFiles().stream() .filter(file -> branch == null || file.getBranchId().equals(projectBranches.get(branch))) .map(FileInfo::getPath) - .collect(Collectors.toList()); + .toList(); List notExistingFiles = files.stream() .map(file -> branch == null ? file : Paths.get(branch, file).toString()) .map(Utils::sepAtStart) .filter(file -> !projectFiles.contains(file)) - .collect(Collectors.toList()); + .toList(); if (!notExistingFiles.isEmpty()) { throw new ExitCodeExceptionMapper.NotFoundException(notExistingFiles.stream().map(Utils::noSepAtStart) .map(file -> String.format(RESOURCE_BUNDLE.getString("error.file_not_found"), file)) diff --git a/src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java b/src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java index 5b6ae340f..2a6893e91 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java @@ -2,9 +2,9 @@ import com.crowdin.cli.client.ClientDistribution; import com.crowdin.cli.client.CrowdinProjectInfo; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.utils.console.ConsoleSpinner; import com.crowdin.client.distributions.model.DistributionRelease; @@ -24,16 +24,16 @@ class DistributionReleaseAction implements NewAction this.projectClient.downloadProjectInfo() + projectClient::downloadProjectInfo ); boolean isStringsBasedProject = Objects.equals(project.getType(), Type.STRINGS_BASED); diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java index 4eadb1259..0ebc08f3f 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java @@ -8,6 +8,7 @@ import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.commands.functionality.RequestBuilder; import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.utils.Utils; import com.crowdin.cli.utils.console.ConsoleSpinner; @@ -46,16 +47,15 @@ class ScreenshotUploadAction implements NewAction this.projectClient.downloadFullProject()); + projectClient::downloadFullProject); Long branchId = null; if (nonNull(branchName)) { @@ -84,7 +84,7 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl Optional existingScreenshot = screenshotList.stream().findFirst(); if (existingScreenshot.isPresent()) { UpdateScreenshotRequest request = new UpdateScreenshotRequest(); - request.setStorageId(uploadToStorage(file)); + request.setStorageId(uploadToStorage(projectClient, file)); request.setName(file.getName()); request.setUsePreviousTags(!autoTag); try { @@ -111,13 +111,13 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl AddScreenshotRequest request = new AddScreenshotRequest(); if (nonNull(labelNames) && !labelNames.isEmpty()) { - request.setLabelIds(prepareLabelIds()); + request.setLabelIds(prepareLabelIds(projectClient)); } request.setBranchId(branchId); request.setDirectoryId(directoryId); request.setFileId(fileId); - request.setStorageId(uploadToStorage(file)); + request.setStorageId(uploadToStorage(projectClient, file)); request.setName(file.getName()); request.setAutoTag(autoTag); @@ -140,17 +140,17 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl } } - private Long uploadToStorage(File fileToUpload) { + private Long uploadToStorage(ProjectClient projectClient, File fileToUpload) { Long storageId; try (InputStream fileStream = Files.newInputStream(fileToUpload.toPath())) { - storageId = this.projectClient.uploadStorage(fileToUpload.getName(), fileStream); + storageId = projectClient.uploadStorage(fileToUpload.getName(), fileStream); } catch (Exception e) { throw ExitCodeExceptionMapper.remap(e, String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), fileToUpload.getName())); } return storageId; } - private Long[] prepareLabelIds() { + private Long[] prepareLabelIds(ProjectClient projectClient) { Map labels = projectClient.listLabels().stream() .collect(Collectors.toMap(Label::getTitle, Label::getId)); labelNames.stream() diff --git a/src/main/java/com/crowdin/cli/commands/actions/TaskAddAction.java b/src/main/java/com/crowdin/cli/commands/actions/TaskAddAction.java index f5dead9e5..e16ec23d7 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/TaskAddAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/TaskAddAction.java @@ -2,13 +2,13 @@ import com.crowdin.cli.client.ClientTask; import com.crowdin.cli.client.CrowdinProjectFull; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.commands.functionality.ProjectFilesUtils; import com.crowdin.cli.commands.functionality.PropertiesBeanUtils; import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.utils.Utils; @@ -38,16 +38,16 @@ class TaskAddAction implements NewAction { private Boolean skipAssignedStrings; private Boolean includePreTranslatedStringsOnly; private List labels; - private ProjectClient projectClient; private boolean plainView; @Override public void act(Outputter out, ProjectProperties pb, ClientTask client) { + var projectClient = GenericActCommand.getProjectClient(pb); boolean isOrganization = PropertiesBeanUtils.isOrganization(pb.getBaseUrl()); Task task; AddTaskRequest addTaskRequest; CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info", - this.noProgress, this.plainView, () -> this.projectClient.downloadFullProject(this.branch)); + this.noProgress, this.plainView, () -> projectClient.downloadFullProject(this.branch)); List fileIds = new ArrayList<>(); Map paths = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getBranches(), project.getFileInfos()); diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandBundle.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandBundle.java index 262c0841e..15b4a2d69 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandBundle.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandBundle.java @@ -1,8 +1,6 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientBundle; -import com.crowdin.cli.client.Clients; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectParams; import com.crowdin.cli.properties.ProjectProperties; @@ -24,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, @Override protected ClientBundle getClient(ProjectProperties properties) { - return Clients.getClientBundle(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); + return GenericActCommand.getBundleClient(properties); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandComment.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandComment.java index 9645ed855..295fb2199 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandComment.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandComment.java @@ -1,7 +1,6 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientComment; -import com.crowdin.cli.client.Clients; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectParams; import com.crowdin.cli.properties.ProjectProperties; @@ -23,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, @Override protected ClientComment getClient(ProjectProperties properties) { - return Clients.getClientComment(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); + return GenericActCommand.getCommentClient(properties); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandDistribution.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandDistribution.java index 0348392c5..8bb13616c 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandDistribution.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandDistribution.java @@ -2,7 +2,6 @@ import com.crowdin.cli.client.ClientDistribution; import com.crowdin.cli.client.Clients; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectParams; import com.crowdin.cli.properties.ProjectProperties; @@ -26,8 +25,4 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, protected ClientDistribution getClient(ProjectProperties properties) { return Clients.getClientDistribution(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); } - - protected ProjectClient getProjectClient(ProjectProperties properties) { - return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId())); - } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandGlossary.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandGlossary.java index 4227ef7af..aa977ff30 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandGlossary.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandGlossary.java @@ -1,13 +1,12 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientGlossary; -import com.crowdin.cli.client.Clients; import com.crowdin.cli.properties.BaseProperties; public abstract class ActCommandGlossary extends ActCommand { @Override protected ClientGlossary getClient(BaseProperties properties) { - return Clients.getClientGlossary(properties.getApiToken(), properties.getBaseUrl()); + return GenericActCommand.getGlossaryClient(properties); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandLabel.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandLabel.java index 2d391d7c8..b6f8c496f 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandLabel.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandLabel.java @@ -1,9 +1,6 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientLabel; -import com.crowdin.cli.client.ClientScreenshot; -import com.crowdin.cli.client.Clients; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectParams; import com.crowdin.cli.properties.ProjectProperties; @@ -25,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, @Override protected ClientLabel getClient(ProjectProperties properties) { - return Clients.getClientLabel(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); + return GenericActCommand.getLabelClient(properties); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandProject.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandProject.java index 98d681926..9e1964e97 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandProject.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandProject.java @@ -1,6 +1,5 @@ package com.crowdin.cli.commands.picocli; -import com.crowdin.cli.client.Clients; import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectProperties; @@ -23,6 +22,6 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, @Override protected ProjectClient getClient(ProjectProperties properties) { - return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId())); + return GenericActCommand.getProjectClient(properties); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java index 15c68bb11..5cb68e46c 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java @@ -2,7 +2,6 @@ import com.crowdin.cli.client.ClientScreenshot; import com.crowdin.cli.client.Clients; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectParams; import com.crowdin.cli.properties.ProjectProperties; @@ -26,8 +25,4 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, protected ClientScreenshot getClient(ProjectProperties properties) { return Clients.getClientScreenshot(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); } - - protected ProjectClient getProjectClient(ProjectProperties properties) { - return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId())); - } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTask.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTask.java index d29cfe869..c6c85a7ce 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTask.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTask.java @@ -2,7 +2,6 @@ import com.crowdin.cli.client.ClientTask; import com.crowdin.cli.client.Clients; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectParams; import com.crowdin.cli.properties.ProjectProperties; @@ -26,8 +25,4 @@ protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, protected ClientTask getClient(ProjectProperties properties) { return Clients.getClientTask(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); } - - protected ProjectClient getProjectClient(ProjectProperties properties) { - return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId())); - } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTm.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTm.java index 4e1368528..123e421f5 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTm.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandTm.java @@ -1,13 +1,12 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientTm; -import com.crowdin.cli.client.Clients; import com.crowdin.cli.properties.BaseProperties; public abstract class ActCommandTm extends ActCommand { @Override protected ClientTm getClient(BaseProperties properties) { - return Clients.getClientTm(properties.getApiToken(), properties.getBaseUrl()); + return GenericActCommand.getTmClient(properties); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandWithFiles.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandWithFiles.java index 932c5d421..cd8930c2e 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandWithFiles.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandWithFiles.java @@ -1,6 +1,5 @@ package com.crowdin.cli.commands.picocli; -import com.crowdin.cli.client.Clients; import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ParamsWithFiles; @@ -27,7 +26,7 @@ protected PropertiesWithFiles getProperties(PropertiesBuilders propertiesBuilder @Override protected ProjectClient getClient(PropertiesWithFiles props) { - return Clients.getProjectClient(props.getApiToken(), props.getBaseUrl(), Long.parseLong(props.getProjectId())); + return GenericActCommand.getProjectClient(props); } protected void updateParams(ParamsWithFiles params) { diff --git a/src/main/java/com/crowdin/cli/commands/picocli/DistributionAddSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/DistributionAddSubcommand.java index af0bfad8b..ab7aa81ed 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/DistributionAddSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/DistributionAddSubcommand.java @@ -1,10 +1,8 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientDistribution; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Actions; import com.crowdin.cli.commands.NewAction; -import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.client.distributions.model.ExportMode; import org.apache.logging.log4j.util.Strings; @@ -42,10 +40,7 @@ class DistributionAddSubcommand extends ActCommandDistribution { @Override protected NewAction getAction(Actions actions) { - Outputter out = new PicocliOutputter(System.out, isAnsi()); - ProjectClient projectClient = this.getProjectClient(this.getProperties(propertiesBuilders, out)); - - return actions.distributionAdd(noProgress, plainView, name, exportMode, files, bundleIds, branch, projectClient); + return actions.distributionAdd(noProgress, plainView, name, exportMode, files, bundleIds, branch); } @Override diff --git a/src/main/java/com/crowdin/cli/commands/picocli/DistributionReleaseSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/DistributionReleaseSubcommand.java index 3232ef2f9..c14cc1b4b 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/DistributionReleaseSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/DistributionReleaseSubcommand.java @@ -1,10 +1,8 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientDistribution; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Actions; import com.crowdin.cli.commands.NewAction; -import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectProperties; import picocli.CommandLine; @@ -22,8 +20,6 @@ class DistributionReleaseSubcommand extends ActCommandDistribution { @Override protected NewAction getAction(Actions actions) { - Outputter out = new PicocliOutputter(System.out, isAnsi()); - ProjectClient projectClient = this.getProjectClient(this.getProperties(propertiesBuilders, out)); - return actions.distributionRelease(noProgress, plainView, hash, projectClient); + return actions.distributionRelease(noProgress, plainView, hash); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/GenericActCommand.java b/src/main/java/com/crowdin/cli/commands/picocli/GenericActCommand.java index 79142823b..b8ce04b79 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/GenericActCommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/GenericActCommand.java @@ -1,14 +1,18 @@ package com.crowdin.cli.commands.picocli; -import com.crowdin.cli.client.Client; +import com.crowdin.cli.client.*; import com.crowdin.cli.commands.Actions; import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.properties.BaseProperties; +import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.properties.Properties; import com.crowdin.cli.properties.PropertiesBuilders; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; public abstract class GenericActCommand

extends GenericCommand { @@ -26,15 +30,19 @@ public final void run() { Outputter out = new PicocliOutputter(System.out, isAnsi()); P properties = this.getProperties(propertiesBuilders, out); - List errors = checkOptions(); - if (errors != null && !errors.isEmpty()) { + List errors = new ArrayList<>(Optional.ofNullable(checkOptions()).orElse(new ArrayList<>())); + errors.addAll(checkOptions(properties)); + + if (!errors.isEmpty()) { String errorsInOne = errors.stream() .map(error -> String.format(RESOURCE_BUNDLE.getString("message.item_list"), error)) .collect(Collectors.joining("\n")); throw new ExitCodeExceptionMapper.ValidationException(RESOURCE_BUNDLE.getString("error.params_are_invalid") + "\n" + errorsInOne); } + NewAction action = this.getAction(actions); C client = this.getClient(properties); + action.act(out, properties, client); } @@ -48,7 +56,37 @@ protected List checkOptions() { return Collections.emptyList(); } + protected List checkOptions(P props) { + return Collections.emptyList(); + } + protected boolean isAnsi() { return !this.noColors; } + + //Utilities to get different clients inside the action + + public static ProjectClient getProjectClient(ProjectProperties properties) { + return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId())); + } + + public static ClientBundle getBundleClient(ProjectProperties properties) { + return Clients.getClientBundle(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); + } + + public static ClientComment getCommentClient(ProjectProperties properties) { + return Clients.getClientComment(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); + } + + public static ClientLabel getLabelClient(ProjectProperties properties) { + return Clients.getClientLabel(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId()); + } + + public static ClientGlossary getGlossaryClient(BaseProperties properties) { + return Clients.getClientGlossary(properties.getApiToken(), properties.getBaseUrl()); + } + + public static ClientTm getTmClient(BaseProperties properties) { + return Clients.getClientTm(properties.getApiToken(), properties.getBaseUrl()); + } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommand.java index 119004e0b..3b1ad351a 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommand.java @@ -1,10 +1,8 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientScreenshot; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Actions; import com.crowdin.cli.commands.NewAction; -import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectProperties; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.StringUtils; @@ -45,9 +43,7 @@ class ScreenshotUploadSubcommand extends ActCommandScreenshot{ @Override protected NewAction getAction(Actions actions) { - Outputter out = new PicocliOutputter(System.out, isAnsi()); - ProjectClient projectClient = this.getProjectClient(this.getProperties(propertiesBuilders, out)); - return actions.screenshotUpload(file, branchName, labelNames, directoryPath, filePath, autoTag, plainView, this.noProgress, projectClient); + return actions.screenshotUpload(file, branchName, labelNames, directoryPath, filePath, autoTag, plainView, this.noProgress); } @Override diff --git a/src/main/java/com/crowdin/cli/commands/picocli/TaskAddSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/TaskAddSubcommand.java index dab62ba5c..9b687ce8d 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/TaskAddSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/TaskAddSubcommand.java @@ -1,10 +1,8 @@ package com.crowdin.cli.commands.picocli; import com.crowdin.cli.client.ClientTask; -import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.Actions; import com.crowdin.cli.commands.NewAction; -import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.commands.functionality.PropertiesBeanUtils; import com.crowdin.cli.properties.ProjectProperties; @@ -13,8 +11,6 @@ import java.util.*; -import static java.lang.System.out; - @CommandLine.Command( name = CommandNames.ADD, sortOptions = false @@ -60,8 +56,6 @@ class TaskAddSubcommand extends ActCommandTask { @Override protected NewAction getAction(Actions actions) { int intType = TRANSLATE_TASK_TYPE.equalsIgnoreCase(type) ? 0 : 1; - Outputter out = new PicocliOutputter(System.out, isAnsi()); - ProjectClient projectClient = this.getProjectClient(this.getProperties(propertiesBuilders, out)); return actions.taskAdd( noProgress, @@ -75,14 +69,13 @@ protected NewAction getAction(Actions actions) { skipAssignedStrings, includePreTranslatedStringsOnly, labels, - projectClient, plainView ); } @Override - protected List checkOptions() { - String url = this.getProperties(propertiesBuilders, new PicocliOutputter(out, isAnsi())).getBaseUrl(); + protected List checkOptions(ProjectProperties properties) { + String url = properties.getBaseUrl(); boolean isEnterprise = PropertiesBeanUtils.isOrganization(url); return checkOptions(isEnterprise); } diff --git a/src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java b/src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java index 80b34ca7d..ad42e8edf 100644 --- a/src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java +++ b/src/main/java/com/crowdin/cli/properties/PropertiesWithFiles.java @@ -1,7 +1,7 @@ package com.crowdin.cli.properties; -import com.crowdin.cli.client.Clients; import com.crowdin.cli.client.ProjectClient; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.client.languages.model.Language; import lombok.Data; import lombok.EqualsAndHashCode; @@ -79,7 +79,7 @@ public PropertiesBuilder.Messages checkProperties(PropertiesWithFiles props, Che messages.addError(RESOURCE_BUNDLE.getString("error.dest_and_preserve_hierarchy")); } if (fileBean.getLanguagesMapping() != null) { - ProjectClient projectClient = Clients.getProjectClient(props.getApiToken(), props.getBaseUrl(), Long.parseLong(props.getProjectId())); + ProjectClient projectClient = GenericActCommand.getProjectClient(props); List languages = projectClient.listSupportedLanguages(); if (languages != null) { diff --git a/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java b/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java index bc3fe8c24..770ebad0d 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java @@ -109,7 +109,7 @@ public void testTmDownload() { @Test public void testDistributionAdd() { - assertNotNull(actions.distributionAdd(false, false, null, null, null, null, null, null)); + assertNotNull(actions.distributionAdd(false, false, null, null, null, null, null)); } @Test @@ -119,7 +119,7 @@ public void testDistributionList() { @Test public void testDistributionRelease() { - assertNotNull(actions.distributionRelease(false,true,null, null)); + assertNotNull(actions.distributionRelease(false,true, null)); } @Test @@ -139,7 +139,7 @@ void testScreenshotList() { @Test void testScreenshotUpload() { - assertNotNull(actions.screenshotUpload(null, null, null, null, null, false, false, false, null)); + assertNotNull(actions.screenshotUpload(null, null, null, null, null, false, false, false)); } @Test diff --git a/src/test/java/com/crowdin/cli/commands/actions/DistributionAddActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/DistributionAddActionTest.java index bc149f075..8f64c8520 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/DistributionAddActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/DistributionAddActionTest.java @@ -7,6 +7,7 @@ import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.commands.functionality.RequestBuilder; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.properties.PropertiesWithFiles; @@ -76,10 +77,14 @@ public void testDistributionAdd(String name, ExportMode exportMode, List when(projectClient.downloadFullProject(branch)) .thenReturn(build); - action = new DistributionAddAction(true, true, name, exportMode, files, bundleIds, branch, projectClient); - action.act(Outputter.getDefault(), pb, client); - verify(client).addDistribution(request); - verifyNoMoreInteractions(client); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new DistributionAddAction(true, true, name, exportMode, files, bundleIds, branch); + action.act(Outputter.getDefault(), pb, client); + verify(client).addDistribution(request); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } public static Stream testDistributionAdd() { @@ -104,10 +109,14 @@ public void testAddDistributionThrows() { when(client.addDistribution(request)) .thenThrow(new RuntimeException("Whoops")); - action = new DistributionAddAction(true, false, null, null, null, null, null, null); - assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(null); + action = new DistributionAddAction(true, false, null, null, null, null, null); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); - verifyNoMoreInteractions(client); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -138,10 +147,14 @@ public void testDistributionAdd_StringsBasedProject() { when(projectClient.downloadFullProject("main")) .thenReturn(build); - action = new DistributionAddAction(true, true, "My Distribution 1", ExportMode.BUNDLE, null, Arrays.asList(9), "main", projectClient); - action.act(Outputter.getDefault(), pb, client); - verify(client).addDistributionStringsBased(request); - verifyNoMoreInteractions(client); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new DistributionAddAction(true, true, "My Distribution 1", ExportMode.BUNDLE, null, Arrays.asList(9), "main"); + action.act(Outputter.getDefault(), pb, client); + verify(client).addDistributionStringsBased(request); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } } diff --git a/src/test/java/com/crowdin/cli/commands/actions/DistributionReleaseActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/DistributionReleaseActionTest.java index 159efa812..98e4eb80e 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/DistributionReleaseActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/DistributionReleaseActionTest.java @@ -2,10 +2,10 @@ import com.crowdin.cli.client.ClientDistribution; import com.crowdin.cli.client.CrowdinProjectInfo; -import com.crowdin.cli.client.ProjectBuilder; import com.crowdin.cli.client.ProjectClient; import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.properties.PropertiesWithFiles; @@ -41,9 +41,13 @@ public void releaseDistributionTest_whenSuccess() { distributionRelease.setStatus("success"); when(client.release(HASH)).thenReturn(distributionRelease); - action = new DistributionReleaseAction(true, true, HASH, projectClient); - action.act(Outputter.getDefault(), pb, client); - verify(client).release(HASH); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new DistributionReleaseAction(true, true, HASH); + action.act(Outputter.getDefault(), pb, client); + verify(client).release(HASH); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -64,10 +68,14 @@ public void releaseDistributionTest_whenFailed() { when(client.release(any())).thenReturn(distributionRelease); when(client.getDistributionRelease(eq(HASH))).thenReturn(distributionRelease); - action = new DistributionReleaseAction(true, true, HASH, projectClient); - assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); - verify(client).release(HASH); - verify(client).getDistributionRelease(HASH); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new DistributionReleaseAction(true, true, HASH); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + verify(client).release(HASH); + verify(client).getDistributionRelease(HASH); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -87,8 +95,12 @@ public void releaseDistributionTest_StringsBasedProject() { distributionRelease.setStatus("success"); when(client.releaseStringsBased(HASH)).thenReturn(distributionRelease); - action = new DistributionReleaseAction(true, true, HASH, projectClient); - action.act(Outputter.getDefault(), pb, client); - verify(client).releaseStringsBased(HASH); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new DistributionReleaseAction(true, true, HASH); + action.act(Outputter.getDefault(), pb, client); + verify(client).releaseStringsBased(HASH); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } } diff --git a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java index 38c34a5fa..64b94576f 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java @@ -6,6 +6,7 @@ import com.crowdin.cli.client.ResponseException; import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.properties.PropertiesWithFiles; @@ -103,12 +104,16 @@ public void testUploadScreenshot(String fileName, String sourceFilePath, Long so setId(1L); }}); - action = new ScreenshotUploadAction(fileToUpload, branchName, labelNames, sourceFilePath, directoryPath, autoTag, false, false, projectClient); - action.act(Outputter.getDefault(), pb, client); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new ScreenshotUploadAction(fileToUpload, branchName, labelNames, sourceFilePath, directoryPath, autoTag, false, false); + action.act(Outputter.getDefault(), pb, client); - verify(client).listScreenshotsByName(eq(fileName)); - verify(client).uploadScreenshot(request); - verifyNoMoreInteractions(client); + verify(client).listScreenshotsByName(eq(fileName)); + verify(client).uploadScreenshot(request); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } public static Stream testUploadScreenshot() { @@ -150,12 +155,16 @@ public void testUploadScreenshotToUpdate() throws ResponseException { setId(123L); }}); - action = new ScreenshotUploadAction(fileToUpload, null, null, null, null, false, false, false, projectClient); - action.act(Outputter.getDefault(), pb, client); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new ScreenshotUploadAction(fileToUpload, null, null, null, null, false, false, false); + action.act(Outputter.getDefault(), pb, client); - verify(client).listScreenshotsByName(eq(fileName)); - verify(client).updateScreenshot(123L, request); - verifyNoMoreInteractions(client); + verify(client).listScreenshotsByName(eq(fileName)); + verify(client).updateScreenshot(123L, request); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -174,8 +183,12 @@ public void testUploadScreenshotNotExistingBranch() { when(projectFull.findBranchByName("main")).thenReturn(Optional.empty()); - action = new ScreenshotUploadAction(new File("screenshot.png"), "main", null, null, null, true, false, false, projectClient); - assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new ScreenshotUploadAction(new File("screenshot.png"), "main", null, null, null, true, false, false); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -194,8 +207,12 @@ public void testUploadScreenshotNotExistingSourceFile() { when(projectFull.getFileInfos()).thenReturn(new ArrayList<>()); - action = new ScreenshotUploadAction(new File("screenshot.png"), null, null, "/path/to/file", null, true, false, false, projectClient); - assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new ScreenshotUploadAction(new File("screenshot.png"), null, null, "/path/to/file", null, true, false, false); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -214,8 +231,12 @@ public void testUploadScreenshotNotExistingDirectory() { when(projectFull.getDirectories()).thenReturn(new HashMap<>()); - action = new ScreenshotUploadAction(new File("screenshot.png"), null, null, null, "/path/to/directory", true, false, false, projectClient); - assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new ScreenshotUploadAction(new File("screenshot.png"), null, null, null, "/path/to/directory", true, false, false); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -260,16 +281,20 @@ public void testUploadScreenshotWithLabels() throws ResponseException { setId(1L); }}); - action = new ScreenshotUploadAction(fileToUpload, null, Arrays.asList("label1", "label2"), null, null, false, false, false, projectClient); - action.act(Outputter.getDefault(), pb, client); - - verify(client).listScreenshotsByName(eq(fileToUpload.getName())); - verify(client).uploadScreenshot(request); - verify(projectClient).downloadFullProject(); - verify(projectClient).listLabels(); - verify(projectClient).uploadStorage(any(), any()); - verifyNoMoreInteractions(client); - verifyNoMoreInteractions(projectClient); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new ScreenshotUploadAction(fileToUpload, null, Arrays.asList("label1", "label2"), null, null, false, false, false); + action.act(Outputter.getDefault(), pb, client); + + verify(client).listScreenshotsByName(eq(fileToUpload.getName())); + verify(client).uploadScreenshot(request); + verify(projectClient).downloadFullProject(); + verify(projectClient).listLabels(); + verify(projectClient).uploadStorage(any(), any()); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(projectClient); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } @Test @@ -312,16 +337,20 @@ public void testUploadScreenshotNotExistingLabel() throws ResponseException { setId(1L); }}); - action = new ScreenshotUploadAction(fileToUpload, null, Arrays.asList("label1"), null, null, false, false, false, projectClient); - action.act(Outputter.getDefault(), pb, client); - - verify(client).listScreenshotsByName(eq(fileToUpload.getName())); - verify(client).uploadScreenshot(request); - verify(projectClient).downloadFullProject(); - verify(projectClient).listLabels(); - verify(projectClient).addLabel(any()); - verify(projectClient).uploadStorage(any(), any()); - verifyNoMoreInteractions(client); - verifyNoMoreInteractions(projectClient); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new ScreenshotUploadAction(fileToUpload, null, Arrays.asList("label1"), null, null, false, false, false); + action.act(Outputter.getDefault(), pb, client); + + verify(client).listScreenshotsByName(eq(fileToUpload.getName())); + verify(client).uploadScreenshot(request); + verify(projectClient).downloadFullProject(); + verify(projectClient).listLabels(); + verify(projectClient).addLabel(any()); + verify(projectClient).uploadStorage(any(), any()); + verifyNoMoreInteractions(client); + verifyNoMoreInteractions(projectClient); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } } diff --git a/src/test/java/com/crowdin/cli/commands/actions/TaskAddActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/TaskAddActionTest.java index 564afb02d..2a2bece49 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/TaskAddActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/TaskAddActionTest.java @@ -6,6 +6,7 @@ import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.commands.functionality.RequestBuilder; +import com.crowdin.cli.commands.picocli.GenericActCommand; import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; import com.crowdin.cli.properties.ProjectProperties; import com.crowdin.cli.properties.PropertiesWithFiles; @@ -60,12 +61,16 @@ public void testTaskAdd(String title, Integer type, String languageId, Map GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new TaskAddAction(true, title, type, languageId, files, null, null, description, skipAssignedStrings, includePreTranslatedStringsOnly, labelIds, false); + action.act(Outputter.getDefault(), pb, client); - verify(client).addTask(request); - verify(projectClient).downloadFullProject(null); - verifyNoMoreInteractions(client); + verify(client).addTask(request); + verify(projectClient).downloadFullProject(null); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } public static Stream testTaskAdd() { @@ -112,12 +117,16 @@ public void testEnterpriseTaskAdd(String title, String languageId, Map GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new TaskAddAction(false, title, null, languageId, files, null, workflowStepId, description, skipAssignedStrings, false, labelIds, true); + action.act(Outputter.getDefault(), pb, client); - verify(client).addTask(request); - verify(projectClient).downloadFullProject(null); - verifyNoMoreInteractions(client); + verify(client).addTask(request); + verify(projectClient).downloadFullProject(null); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } public static Stream testEnterpriseTaskAdd() { @@ -147,11 +156,15 @@ public void testAddTaskThrows() { when(client.addTask(request)) .thenThrow(new RuntimeException("Whoops")); - action = new TaskAddAction(false, null, null, null, Arrays.asList("file.txt"), null, null, null, false, false, null, projectClient, false); + try (var mocked = mockStatic(GenericActCommand.class)) { + mocked.when(() -> GenericActCommand.getProjectClient(pb)).thenReturn(projectClient); + action = new TaskAddAction(false, null, null, null, Arrays.asList("file.txt"), null, null, null, false, false, null, false); - assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); - verify(client).addTask(request); - verifyNoMoreInteractions(client); + verify(client).addTask(request); + verifyNoMoreInteractions(client); + mocked.verify(() -> GenericActCommand.getProjectClient(pb)); + } } } diff --git a/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java b/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java index b34c2583f..b05adff84 100644 --- a/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java +++ b/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java @@ -94,11 +94,11 @@ void mockActions() { .thenReturn(actionMock); when(actionsMock.taskList(anyBoolean(), anyBoolean(), any(), any())) .thenReturn(actionMock); - when(actionsMock.taskAdd(anyBoolean(), any(), any(), any(), any(), any(), anyLong(), any(), anyBoolean(), anyBoolean(), any(), any(), anyBoolean())) + when(actionsMock.taskAdd(anyBoolean(), any(), any(), any(), any(), any(), anyLong(), any(), anyBoolean(), anyBoolean(), any(), anyBoolean())) .thenReturn(actionMock); when(actionsMock.distributionList(anyBoolean())) .thenReturn(actionMock); - when(actionsMock.distributionAdd(anyBoolean(), anyBoolean(), any(), any(), any(), any(), any(), any())) + when(actionsMock.distributionAdd(anyBoolean(), anyBoolean(), any(), any(), any(), any(), any())) .thenReturn(actionMock); when(actionsMock.bundleList(anyBoolean(), anyBoolean())) .thenReturn(actionMock); @@ -110,7 +110,7 @@ void mockActions() { .thenReturn(actionMock); when(actionsMock.screenshotList(any(), anyBoolean())) .thenReturn(actionMock); - when(actionsMock.screenshotUpload(any(), any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), any())) + when(actionsMock.screenshotUpload(any(), any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean())) .thenReturn(actionMock); when(actionsMock.screenshotDelete(any())) .thenReturn(actionMock); diff --git a/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 000000000..ca6ee9cea --- /dev/null +++ b/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline \ No newline at end of file