From 911d625fa72a7df7ecc97a4dec5d6e1588772a3d Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Mon, 4 Sep 2023 17:54:46 +0300 Subject: [PATCH 1/8] feat: New command: screenshot --- .../client/AutoTagInProgressException.java | 4 + .../crowdin/cli/client/ClientScreenshot.java | 18 +++ .../java/com/crowdin/cli/client/Clients.java | 5 + .../cli/client/CrowdinClientScreenshot.java | 52 ++++++++ .../com/crowdin/cli/commands/Actions.java | 5 + .../cli/commands/actions/CliActions.java | 15 +++ .../commands/actions/ScreenshotAddAction.java | 120 ++++++++++++++++++ .../actions/ScreenshotDeleteAction.java | 35 +++++ .../actions/ScreenshotListAction.java | 43 +++++++ .../picocli/ActCommandScreenshot.java | 33 +++++ .../cli/commands/picocli/CommandNames.java | 5 + .../cli/commands/picocli/RootCommand.java | 3 +- .../picocli/ScreenshotAddSubcommand.java | 80 ++++++++++++ .../picocli/ScreenshotDeleteSubcommand.java | 21 +++ .../picocli/ScreenshotListSubcommand.java | 25 ++++ .../picocli/ScreenshotSubcommand.java | 20 +++ .../resources/messages/messages.properties | 39 ++++++ 17 files changed, 522 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/crowdin/cli/client/AutoTagInProgressException.java create mode 100644 src/main/java/com/crowdin/cli/client/ClientScreenshot.java create mode 100644 src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java create mode 100644 src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java create mode 100644 src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java create mode 100644 src/main/java/com/crowdin/cli/commands/actions/ScreenshotListAction.java create mode 100644 src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java create mode 100644 src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java create mode 100644 src/main/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommand.java create mode 100644 src/main/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommand.java create mode 100644 src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java diff --git a/src/main/java/com/crowdin/cli/client/AutoTagInProgressException.java b/src/main/java/com/crowdin/cli/client/AutoTagInProgressException.java new file mode 100644 index 000000000..143635302 --- /dev/null +++ b/src/main/java/com/crowdin/cli/client/AutoTagInProgressException.java @@ -0,0 +1,4 @@ +package com.crowdin.cli.client; + +public class AutoTagInProgressException extends ResponseException { +} diff --git a/src/main/java/com/crowdin/cli/client/ClientScreenshot.java b/src/main/java/com/crowdin/cli/client/ClientScreenshot.java new file mode 100644 index 000000000..0e8163d4b --- /dev/null +++ b/src/main/java/com/crowdin/cli/client/ClientScreenshot.java @@ -0,0 +1,18 @@ +package com.crowdin.cli.client; + +import com.crowdin.client.screenshots.model.AddScreenshotRequest; +import com.crowdin.client.screenshots.model.Screenshot; +import com.crowdin.client.screenshots.model.UpdateScreenshotRequest; + +import java.util.List; + +public interface ClientScreenshot extends Client { + + List listScreenshots(Long stringId); + + Screenshot addScreenshot(AddScreenshotRequest request) throws ResponseException; + + Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request); + + void deleteScreenshot(Long id); +} diff --git a/src/main/java/com/crowdin/cli/client/Clients.java b/src/main/java/com/crowdin/cli/client/Clients.java index 00d32e253..bcc02ad25 100644 --- a/src/main/java/com/crowdin/cli/client/Clients.java +++ b/src/main/java/com/crowdin/cli/client/Clients.java @@ -49,6 +49,11 @@ public static ClientBundle getClientBundle(String apiToken, String baseUrl, Stri return new CrowdinClientBundle(client, projectId); } + public static ClientScreenshot getClientScreenshot(String apiToken, String baseUrl, String projectId) { + com.crowdin.client.Client client = prepareClient(apiToken, baseUrl); + return new CrowdinClientScreenshot(client, projectId); + } + // mb divide args to move token and url to constructor? public static ProjectClient getProjectClient(String apiToken, String baseUrl, long projectId) { com.crowdin.client.Client client = prepareClient(apiToken, baseUrl); diff --git a/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java new file mode 100644 index 000000000..c4804b8e8 --- /dev/null +++ b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java @@ -0,0 +1,52 @@ +package com.crowdin.cli.client; + +import com.crowdin.client.Client; +import com.crowdin.client.screenshots.model.AddScreenshotRequest; +import com.crowdin.client.screenshots.model.Screenshot; +import com.crowdin.client.screenshots.model.UpdateScreenshotRequest; +import lombok.AllArgsConstructor; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiPredicate; + +import static java.lang.Long.parseLong; + +@AllArgsConstructor +public class CrowdinClientScreenshot extends CrowdinClientCore implements ClientScreenshot { + + private final Client client; + private final String projectId; + + @Override + public List listScreenshots(Long stringId) { + return executeRequestFullList((limit, offset) -> this.client.getScreenshotsApi() + .listScreenshots(parseLong(this.projectId), stringId, limit, offset)); + } + + @Override + public Screenshot addScreenshot(AddScreenshotRequest request) throws ResponseException { + Map, ResponseException> errorHandler = new LinkedHashMap, ResponseException>() {{ + put((code, message) -> code.equals("409") && message.contains("Auto tag is currently in progress"), + new AutoTagInProgressException()); + }}; + return executeRequest(errorHandler, () -> this.client.getScreenshotsApi() + .addScreenshot(parseLong(this.projectId), request) + .getData()); + } + + @Override + public Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request) { + return executeRequest(() -> this.client.getScreenshotsApi() + .updateScreenshot(parseLong(this.projectId), screenshotId, request).getData()); + } + + @Override + public void deleteScreenshot(Long id) { + executeRequest(() -> { + this.client.getScreenshotsApi().deleteScreenshot(parseLong(this.projectId), id); + return null; + }); + } +} diff --git a/src/main/java/com/crowdin/cli/commands/Actions.java b/src/main/java/com/crowdin/cli/commands/Actions.java index f88599ea0..443e994f5 100644 --- a/src/main/java/com/crowdin/cli/commands/Actions.java +++ b/src/main/java/com/crowdin/cli/commands/Actions.java @@ -117,4 +117,9 @@ NewAction preTranslate( NewAction branchDelete(String name); + NewAction screenshotList(Long stringId, boolean plainView); + + NewAction screenshotAdd(File file, String branchName, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient); + + NewAction screenshotDelete(String name); } 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 c1bc933e5..35fa659f4 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/CliActions.java +++ b/src/main/java/com/crowdin/cli/commands/actions/CliActions.java @@ -241,4 +241,19 @@ public NewAction branchAdd(String name, String public NewAction branchDelete(String name) { return new BranchDeleteAction(name); } + + @Override + public NewAction screenshotList(Long stringId, boolean plainView) { + return new ScreenshotListAction(stringId, plainView); + } + + @Override + public NewAction screenshotAdd(File file, String branchName, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient) { + return new ScreenshotAddAction(file, branchName, filePath, directoryPath, autoTag, plainView, noProgress, projectClient); + } + + @Override + public NewAction screenshotDelete(String name) { + return new ScreenshotDeleteAction(name); + } } diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java new file mode 100644 index 000000000..3e1ca7bf6 --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java @@ -0,0 +1,120 @@ +package com.crowdin.cli.commands.actions; + +import com.crowdin.cli.client.AutoTagInProgressException; +import com.crowdin.cli.client.ClientScreenshot; +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.properties.ProjectProperties; +import com.crowdin.cli.utils.console.ConsoleSpinner; +import com.crowdin.client.screenshots.model.AddScreenshotRequest; +import com.crowdin.client.screenshots.model.Screenshot; +import com.crowdin.client.screenshots.model.UpdateScreenshotRequest; +import com.crowdin.client.sourcefiles.model.Branch; +import com.crowdin.client.sourcefiles.model.Directory; +import com.crowdin.client.sourcefiles.model.FileInfo; +import lombok.AllArgsConstructor; + +import java.io.File; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.List; +import java.util.Optional; + +import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; +import static com.crowdin.cli.utils.console.ExecutionStatus.OK; +import static com.crowdin.cli.utils.console.ExecutionStatus.WARNING; +import static java.util.Objects.nonNull; + +@AllArgsConstructor +class ScreenshotAddAction implements NewAction { + + private final File file; + private final String branchName; + private final String pathToSourceFile; + private final String directoryPath; + private final boolean autoTag; + private final boolean plainView; + private final boolean noProgress; + + private ProjectClient projectClient; + + @Override + public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) { + Screenshot screenshot; + List screenshotList = client.listScreenshots(null); + Optional existingScreenshot = screenshotList.stream() + .filter((s) -> file.getName().equals(s.getName())).findFirst(); + if (existingScreenshot.isPresent()) { + UpdateScreenshotRequest request = new UpdateScreenshotRequest(); + request.setStorageId(uploadToStorage(file)); + request.setName(file.getName()); + try { + screenshot = client.updateScreenshot(existingScreenshot.get().getId(), request); + } catch (Exception e) { + throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_updated"), request), e); + } + if (!plainView) { + out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.updated"), screenshot.getName()))); + } else { + out.println(String.valueOf(screenshot.getName())); + } + return; + } + AddScreenshotRequest request = new AddScreenshotRequest(); + CrowdinProjectFull project = ConsoleSpinner.execute( + out, + "message.spinner.fetching_project_info", "error.collect_project_info", + this.noProgress, + this.plainView, + () -> this.projectClient.downloadFullProject()); + if (nonNull(branchName)) { + Branch branch = project.findBranchByName(branchName) + .orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), branchName))); + request.setBranchId(branch.getId()); + } + if (nonNull(pathToSourceFile)) { + FileInfo fileInfo = project.getFileInfos().stream() + .filter(f -> pathToSourceFile.equals(f.getPath())).findFirst() + .orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), pathToSourceFile))); + request.setFileId(fileInfo.getId()); + } + if (nonNull(directoryPath)) { + Directory directory = project.getDirectories().values().stream() + .filter(d -> directoryPath.equals(d.getPath())).findFirst() + .orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.dir_not_exists"), directoryPath))); + request.setDirectoryId(directory.getId()); + } + request.setStorageId(uploadToStorage(file)); + request.setName(file.getName()); + request.setAutoTag(autoTag); + + try { + client.addScreenshot(request); + } catch (AutoTagInProgressException e) { + if (!plainView) { + out.println(WARNING.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.not_auto-tagged"), file.getName()))); + } else { + out.println(String.format(RESOURCE_BUNDLE.getString("message.screenshot.not_auto-tagged"), file.getName())); + } + } catch (Exception e) { + throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_added"), request), e); + } + if (!plainView) { + out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.added"), file.getName()))); + } else { + out.println(file.getName()); + } + } + + private Long uploadToStorage(File fileToUpload) { + Long storageId; + try (InputStream fileStream = Files.newInputStream(fileToUpload.toPath())) { + storageId = this.projectClient.uploadStorage(fileToUpload.getName(), fileStream); + } catch (Exception e) { + throw new RuntimeException(RESOURCE_BUNDLE.getString("error.upload_to_storage"), e); + } + return storageId; + } +} diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java new file mode 100644 index 000000000..54798f538 --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java @@ -0,0 +1,35 @@ +package com.crowdin.cli.commands.actions; + +import com.crowdin.cli.client.ClientScreenshot; +import com.crowdin.cli.commands.NewAction; +import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.properties.ProjectProperties; +import com.crowdin.cli.utils.console.ExecutionStatus; +import com.crowdin.client.screenshots.model.Screenshot; + +import java.util.Map; +import java.util.stream.Collectors; + +import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; +import static com.crowdin.cli.utils.console.ExecutionStatus.SKIPPED; + +class ScreenshotDeleteAction implements NewAction { + + private final String name; + + public ScreenshotDeleteAction(String name) { + this.name = name; + } + + @Override + public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) { + Map screenshots = client.listScreenshots(null).stream() + .collect(Collectors.toMap(Screenshot::getName, Screenshot::getId)); + if (screenshots.containsKey(name)) { + client.deleteScreenshot(screenshots.get(name)); + out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.deleted"), name))); + } else { + out.println(SKIPPED.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.not_found"), name))); + } + } +} diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotListAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotListAction.java new file mode 100644 index 000000000..3bcdb679d --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotListAction.java @@ -0,0 +1,43 @@ +package com.crowdin.cli.commands.actions; + +import com.crowdin.cli.client.ClientScreenshot; +import com.crowdin.cli.commands.NewAction; +import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.properties.ProjectProperties; +import com.crowdin.client.screenshots.model.Screenshot; + +import java.util.List; + +import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; +import static com.crowdin.cli.utils.console.ExecutionStatus.OK; + +class ScreenshotListAction implements NewAction { + + private final Long stringId; + private final boolean plainView; + + public ScreenshotListAction(Long stringId, boolean plainView) { + this.stringId = stringId; + this.plainView = plainView; + } + + @Override + public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) { + List screenshots = client.listScreenshots(stringId); + for (Screenshot screenshot : screenshots) { + if (!plainView) { + out.println(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"), + screenshot.getId(), screenshot.getName(), screenshot.getTagsCount())); + } else { + out.println(screenshot.getId() + " " + screenshot.getName()); + } + } + if (screenshots.isEmpty()) { + if (!plainView) { + out.println(OK.withIcon(RESOURCE_BUNDLE.getString("message.screenshot.list_empty"))); + } else { + out.println(RESOURCE_BUNDLE.getString("message.screenshot.list_empty")); + } + } + } +} diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java new file mode 100644 index 000000000..15c68bb11 --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/picocli/ActCommandScreenshot.java @@ -0,0 +1,33 @@ +package com.crowdin.cli.commands.picocli; + +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; +import com.crowdin.cli.properties.PropertiesBuilders; +import picocli.CommandLine; + +public abstract class ActCommandScreenshot extends GenericActCommand { + + @CommandLine.Mixin + private ConfigurationFilesProperties properties; + + @CommandLine.ArgGroup(exclusive = false, headingKey = "params.heading") + private ProjectParams params; + + @Override + protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, Outputter out) { + return propertiesBuilders.buildProjectProperties(out, properties.getConfigFile(), properties.getIdentityFile(), params); + } + + @Override + 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/CommandNames.java b/src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java index 27cbf373c..10a9d4ad8 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java @@ -69,4 +69,9 @@ public final class CommandNames { public static final String DISTRIBUTION_ADD = "add"; public static final String DISTRIBUTION_LIST = "list"; public static final String DISTRIBUTION_RELEASE = "release"; + + public static final String SCREENSHOT = "screenshot"; + public static final String SCREENSHOT_LIST = "list"; + public static final String SCREENSHOT_ADD = "add"; + public static final String SCREENSHOT_DELETE = "delete"; } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/RootCommand.java b/src/main/java/com/crowdin/cli/commands/picocli/RootCommand.java index 3f2b5399e..de2e679e8 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/RootCommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/RootCommand.java @@ -20,7 +20,8 @@ PreTranslateSubcommand.class, BranchSubcommand.class, CommentSubcommand.class, - DistributionSubcommand.class + DistributionSubcommand.class, + ScreenshotSubcommand.class }) class RootCommand extends HelpCommand { @Override diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java new file mode 100644 index 000000000..48bebd27c --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java @@ -0,0 +1,80 @@ +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; +import picocli.CommandLine; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import static java.util.Objects.nonNull; + +@CommandLine.Command( + name = CommandNames.SCREENSHOT_ADD, + sortOptions = false +) +class ScreenshotAddSubcommand extends ActCommandScreenshot{ + + @CommandLine.Parameters(descriptionKey = "crowdin.screenshot.add.file") + private File file; + + @CommandLine.Option(names = {"--auto-tag"}, negatable = true, descriptionKey = "crowdin.screenshot.add.auto-tag", order = -2) + private boolean autoTag; + + @CommandLine.Option(names = {"-f", "--file"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.file-path", order = -2) + private String filePath; + + @CommandLine.Option(names = {"-b", "--branch"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.branch-name", order = -2) + private String branchName; + + @CommandLine.Option(names = {"-d", "--directory"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.directory-path", order = -2) + private String directoryPath; + + @CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain") + protected boolean plainView; + + @Override + protected NewAction getAction(Actions actions) { + Outputter out = new PicocliOutputter(System.out, isAnsi()); + ProjectClient projectClient = this.getProjectClient(this.getProperties(propertiesBuilders, out)); + return actions.screenshotAdd(file, branchName, directoryPath, filePath, autoTag, plainView, this.noProgress, projectClient); + } + + @Override + protected List checkOptions() { + List errors = new ArrayList<>(); + String extension = FilenameUtils.getExtension(file.getName()); + if (!equalsAny(extension, "jpeg", "jpg", "png", "gif")) { + errors.add(RESOURCE_BUNDLE.getString("error.screenshot.wrong_format")); + } + if (nonNull(filePath) && !autoTag) { + errors.add(RESOURCE_BUNDLE.getString("error.screenshot.auto-tag_required_for_file")); + } + if (nonNull(branchName) && !autoTag) { + errors.add(RESOURCE_BUNDLE.getString("error.screenshot.auto-tag_required_for_branch")); + } + if (nonNull(directoryPath) && !autoTag) { + errors.add(RESOURCE_BUNDLE.getString("error.screenshot.auto-tag_required_for_directory")); + } + if (nonNull(filePath) && (nonNull(directoryPath) || nonNull(branchName)) || nonNull(directoryPath) && nonNull(branchName)) { + errors.add(RESOURCE_BUNDLE.getString("error.screenshot.only_one_allowed")); + } + return errors; + } + + private boolean equalsAny(String toCheck, String... strings) { + for (String string : strings) { + if (StringUtils.equalsIgnoreCase(toCheck, string)) { + return true; + } + } + return false; + } +} diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommand.java new file mode 100644 index 000000000..2650a026f --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommand.java @@ -0,0 +1,21 @@ +package com.crowdin.cli.commands.picocli; + +import com.crowdin.cli.client.ClientScreenshot; +import com.crowdin.cli.commands.Actions; +import com.crowdin.cli.commands.NewAction; +import com.crowdin.cli.properties.ProjectProperties; +import picocli.CommandLine; + +@CommandLine.Command( + name = CommandNames.SCREENSHOT_DELETE +) +class ScreenshotDeleteSubcommand extends ActCommandScreenshot { + + @CommandLine.Parameters(descriptionKey = "crowdin.screenshot.delete.name") + protected String name; + + @Override + protected NewAction getAction(Actions actions) { + return actions.screenshotDelete(name); + } +} diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommand.java new file mode 100644 index 000000000..1ff5935c1 --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommand.java @@ -0,0 +1,25 @@ +package com.crowdin.cli.commands.picocli; + +import com.crowdin.cli.client.ClientScreenshot; +import com.crowdin.cli.commands.Actions; +import com.crowdin.cli.commands.NewAction; +import com.crowdin.cli.properties.ProjectProperties; +import picocli.CommandLine; + +@CommandLine.Command( + sortOptions = false, + name = CommandNames.SCREENSHOT_LIST +) +class ScreenshotListSubcommand extends ActCommandScreenshot { + + @CommandLine.Option(names = {"--string-id"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.list.string-id", order = -2) + private Long stringId; + + @CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain") + protected boolean plainView; + + @Override + protected NewAction getAction(Actions actions) { + return actions.screenshotList(this.stringId, this.plainView); + } +} diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java new file mode 100644 index 000000000..b3b359ee7 --- /dev/null +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java @@ -0,0 +1,20 @@ +package com.crowdin.cli.commands.picocli; + +import picocli.CommandLine; +import picocli.CommandLine.Command; + +@Command( + name = CommandNames.SCREENSHOT, + subcommands = { + ScreenshotListSubcommand.class, + ScreenshotAddSubcommand.class, + ScreenshotDeleteSubcommand.class + } +) +class ScreenshotSubcommand extends HelpCommand { + + @Override + protected CommandLine getCommand(CommandLine rootCommand) { + return rootCommand.getSubcommands().get(CommandNames.SCREENSHOT); + } +} diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index df26cf0e7..7e5287edb 100755 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -350,6 +350,29 @@ crowdin.distribution.release.usage.description=Release a distribution crowdin.distribution.release.usage.customSynopsis=@|fg(green) crowdin distribution release|@ [CONFIG OPTIONS] [OPTIONS] crowdin.distribution.release.hash=Distribution hash +# CROWDIN SCREENSHOT COMMAND +crowdin.screenshot.usage.description=Manage screenshots +crowdin.screenshot.usage.customSynopsis=@|fg(green) crowdin screenshot|@ [SUBCOMMAND] [CONFIG OPTIONS] [OPTIONS] + +# CROWDIN SCREENSHOT LIST +crowdin.screenshot.list.usage.description=List screenshots +crowdin.screenshot.list.usage.customSynopsis=@|fg(green) crowdin screenshot list|@ [CONFIG OPTIONS] [OPTIONS] +crowdin.screenshot.list.string-id=Numeric string identifier + +# CROWDIN SCREENSHOT DELETE +crowdin.screenshot.delete.usage.description=Delete screenshot +crowdin.screenshot.delete.usage.customSynopsis=@|fg(green) crowdin screenshot delete|@ [CONFIG OPTIONS] [OPTIONS] +crowdin.screenshot.delete.name=Screenshot name + +# CROWDIN SCREENSHOT ADD +crowdin.screenshot.add.usage.description=Add screenshot +crowdin.screenshot.add.usage.customSynopsis=@|fg(green) crowdin screenshot add|@ [CONFIG OPTIONS] [OPTIONS] +crowdin.screenshot.add.file=File path to add +crowdin.screenshot.add.auto-tag=Choose whether or not to tag screenshot automatically +crowdin.screenshot.add.file-path=Path to source file in Crowdin +crowdin.screenshot.add.branch-name=Branch name +crowdin.screenshot.add.directory-path=Path to directory in Crowdin + error.collect_project_info=Failed to collect project info. Please contact our support team for help error.no_sources=No sources found for '%s' pattern. Check the source paths in your configuration file error.only_enterprise=Operation is available only for Crowdin Enterprise @@ -475,6 +498,14 @@ error.pre_translate.translate_untranslated_only='--translate-untranslated-only' error.pre_translate.translate_with_perfect_match_only='--translate-with-perfect-match-only' only works with the TM pre-translation method error.pre_translate.auto_approve_option=Wrong '--auto-approve-option' parameter. Supported values: all, except-auto-substituted, perfect-match-only, none +error.screenshot.wrong_format=Wrong format of the file. Supported formats: jpeg, jpg, png, gif +error.screenshot.auto-tag_required_for_file='--auto-tag' is required for '--file' option +error.screenshot.auto-tag_required_for_branch='--auto-tag' is required for '--branch' option +error.screenshot.auto-tag_required_for_directory='--auto-tag' is required for '--directory' option +error.screenshot.only_one_allowed=Only one of the following options can be used at a time: '--file', '--branch' or '--directory' +error.screenshot.not_added=Screenshot was not added +error.screenshot.not_updated=Screenshot was not updated + error.response.401=Couldn't authorize. Check your 'api_token' error.response.403=You do not have permission to view/edit project with provided id error.response.403_upgrade_subscription=Please, upgrade your subscription plan to upload more file formats @@ -598,6 +629,14 @@ message.tm.list=Translation memory @|green %s|@ (@|yellow #%d|@, segments: @|bol message.tm.import_success=Imported in @|yellow #%s|@ @|green '%s'|@ translation memory message.tm.list_empty=No translation memories found +message.screenshot.added=@|green,bold Screenshot '%s'|@ @|green added successfully|@ +message.screenshot.updated=@|green,bold Screenshot '%s'|@ @|green updated successfully|@ +message.screenshot.deleted=@|green,bold Screenshot '%s'|@ @|green deleted successfully|@ +message.screenshot.not_auto-tagged=Tags were not applied for %s because auto tag is currently in progress +message.screenshot.not_found=Screenshot %s not found +message.screenshot.list_empty=No screenshot found +message.screenshot.list=@|yellow #%d|@ @|bold '%s'|@ @|green %d tags|@ + message.task.list=@|yellow #%d|@ @|bold %s|@ @|green '%s'|@ message.task.list.verbose=@|yellow #%d|@ @|bold %s|@ @|green '%s'|@ @|bold %s|@ @|yellow %d|@ @|red %s|@ message.task.list_empty=No tasks found From 72f8f9de5a37661b9c2bc31d5ff8ff6d2db65cbe Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Tue, 5 Sep 2023 19:06:10 +0300 Subject: [PATCH 2/8] feat: tests for screenshot command --- .../cli/client/CrowdinClientScreenshot.java | 3 +- .../commands/actions/ScreenshotAddAction.java | 2 +- .../actions/ScreenshotDeleteAction.java | 10 +- .../picocli/ScreenshotAddSubcommand.java | 10 +- .../resources/messages/messages.properties | 4 +- .../client/CrowdinClientScreenshotTest.java | 94 ++++++++ .../cli/commands/actions/CliActionsTest.java | 15 ++ .../actions/ScreenshotAddActionTest.java | 215 ++++++++++++++++++ .../actions/ScreenshotDeleteActionTest.java | 89 ++++++++ .../actions/ScreenshotListActionTest.java | 84 +++++++ .../commands/picocli/PicocliTestUtils.java | 6 + .../picocli/ScreenshotAddSubcommandTest.java | 73 ++++++ .../ScreenshotDeleteSubcommandTest.java | 21 ++ .../picocli/ScreenshotListSubcommandTest.java | 17 ++ 14 files changed, 628 insertions(+), 15 deletions(-) create mode 100644 src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java create mode 100644 src/test/java/com/crowdin/cli/commands/actions/ScreenshotAddActionTest.java create mode 100644 src/test/java/com/crowdin/cli/commands/actions/ScreenshotDeleteActionTest.java create mode 100644 src/test/java/com/crowdin/cli/commands/actions/ScreenshotListActionTest.java create mode 100644 src/test/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommandTest.java create mode 100644 src/test/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommandTest.java create mode 100644 src/test/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommandTest.java diff --git a/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java index c4804b8e8..e08600501 100644 --- a/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java +++ b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java @@ -39,7 +39,8 @@ public Screenshot addScreenshot(AddScreenshotRequest request) throws ResponseExc @Override public Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request) { return executeRequest(() -> this.client.getScreenshotsApi() - .updateScreenshot(parseLong(this.projectId), screenshotId, request).getData()); + .updateScreenshot(parseLong(this.projectId), screenshotId, request) + .getData()); } @Override diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java index 3e1ca7bf6..05d29ebd3 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java @@ -113,7 +113,7 @@ private Long uploadToStorage(File fileToUpload) { try (InputStream fileStream = Files.newInputStream(fileToUpload.toPath())) { storageId = this.projectClient.uploadStorage(fileToUpload.getName(), fileStream); } catch (Exception e) { - throw new RuntimeException(RESOURCE_BUNDLE.getString("error.upload_to_storage"), e); + throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), fileToUpload.getName()), e); } return storageId; } diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java index 54798f538..4b7979971 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotDeleteAction.java @@ -11,7 +11,6 @@ import java.util.stream.Collectors; import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE; -import static com.crowdin.cli.utils.console.ExecutionStatus.SKIPPED; class ScreenshotDeleteAction implements NewAction { @@ -25,11 +24,10 @@ public ScreenshotDeleteAction(String name) { public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) { Map screenshots = client.listScreenshots(null).stream() .collect(Collectors.toMap(Screenshot::getName, Screenshot::getId)); - if (screenshots.containsKey(name)) { - client.deleteScreenshot(screenshots.get(name)); - out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.deleted"), name))); - } else { - out.println(SKIPPED.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.not_found"), name))); + if (!screenshots.containsKey(name)) { + throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_found"), name)); } + client.deleteScreenshot(screenshots.get(name)); + out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.deleted"), name))); } } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java index 48bebd27c..0a62b3ccc 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java @@ -23,19 +23,19 @@ class ScreenshotAddSubcommand extends ActCommandScreenshot{ @CommandLine.Parameters(descriptionKey = "crowdin.screenshot.add.file") - private File file; + protected File file; @CommandLine.Option(names = {"--auto-tag"}, negatable = true, descriptionKey = "crowdin.screenshot.add.auto-tag", order = -2) - private boolean autoTag; + protected boolean autoTag; @CommandLine.Option(names = {"-f", "--file"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.file-path", order = -2) - private String filePath; + protected String filePath; @CommandLine.Option(names = {"-b", "--branch"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.branch-name", order = -2) - private String branchName; + protected String branchName; @CommandLine.Option(names = {"-d", "--directory"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.directory-path", order = -2) - private String directoryPath; + protected String directoryPath; @CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain") protected boolean plainView; diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index 7e5287edb..4ad071902 100755 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -503,6 +503,7 @@ error.screenshot.auto-tag_required_for_file='--auto-tag' is required for '--file error.screenshot.auto-tag_required_for_branch='--auto-tag' is required for '--branch' option error.screenshot.auto-tag_required_for_directory='--auto-tag' is required for '--directory' option error.screenshot.only_one_allowed=Only one of the following options can be used at a time: '--file', '--branch' or '--directory' +error.screenshot.not_found=Screenshot %s not found error.screenshot.not_added=Screenshot was not added error.screenshot.not_updated=Screenshot was not updated @@ -552,7 +553,7 @@ error.init.path_not_exist=Path '%s' doesn't exist error.spinner.pre_translate=Failed to pre-translate the project. Please contact our support team for help error.spinner.reviewed_sources=Failed to load reviewed sources. Please contact our support team for help -error.file_not_found=File '%s' not found int the Crowdin project +error.file_not_found=File '%s' not found in the Crowdin project message.new_version_text=New version of Crowdin CLI is available! %s -> %s message.new_version_text.2=Changelog: @|cyan https://github.com/crowdin/crowdin-cli/releases/latest|@ @@ -633,7 +634,6 @@ message.screenshot.added=@|green,bold Screenshot '%s'|@ @|green added successful message.screenshot.updated=@|green,bold Screenshot '%s'|@ @|green updated successfully|@ message.screenshot.deleted=@|green,bold Screenshot '%s'|@ @|green deleted successfully|@ message.screenshot.not_auto-tagged=Tags were not applied for %s because auto tag is currently in progress -message.screenshot.not_found=Screenshot %s not found message.screenshot.list_empty=No screenshot found message.screenshot.list=@|yellow #%d|@ @|bold '%s'|@ @|green %d tags|@ diff --git a/src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java b/src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java new file mode 100644 index 000000000..1973c62ad --- /dev/null +++ b/src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java @@ -0,0 +1,94 @@ +package com.crowdin.cli.client; + +import com.crowdin.client.core.http.HttpClient; +import com.crowdin.client.core.http.impl.json.JacksonJsonTransformer; +import com.crowdin.client.core.model.ClientConfig; +import com.crowdin.client.core.model.Credentials; +import com.crowdin.client.screenshots.model.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +class CrowdinClientScreenshotTest { + + private HttpClient httpClientMock; + private ClientScreenshot client; + + private static final long projectId = 42; + private static final long screenshotId = 52; + private static final String preUrl = "https://testme.crowdin.com"; + private static final String url = "https://testme.crowdin.com/api/v2"; + + private static final String listScreenshotUrl = String.format("%s/projects/%d/screenshots", url, projectId); + private static final String deleteScreenshotUrl = String.format("%s/projects/%d/screenshots/%d", url, projectId, screenshotId); + private static final String addScreenshotUrl = String.format("%s/projects/%d/screenshots", url, projectId); + private static final String updateScreenshotUrl = String.format("%s/projects/%d/screenshots/%d", url, projectId, screenshotId); + + @BeforeEach + public void init() { + Credentials creds = new Credentials("VeryBigToken", "TestingCompany", preUrl); + httpClientMock = mock(HttpClient.class); + ClientConfig clientConfig = ClientConfig.builder() + .jsonTransformer(new JacksonJsonTransformer()) + .httpClient(httpClientMock) + .build(); + com.crowdin.client.Client internalClient = new com.crowdin.client.Client(creds, clientConfig); + client = new CrowdinClientScreenshot(internalClient, Long.toString(projectId)); + } + + @Test + public void testListScreenshots() { + ScreenshotResponseList response = new ScreenshotResponseList() {{ + setData(new ArrayList<>()); + }}; + when(httpClientMock.get(eq(listScreenshotUrl), any(), eq(ScreenshotResponseList.class))) + .thenReturn(response); + + client.listScreenshots(null); + + verify(httpClientMock).get(eq(listScreenshotUrl), any(), eq(ScreenshotResponseList.class)); + verifyNoMoreInteractions(httpClientMock); + } + + @Test + public void testDeleteScreenshot() { + client.deleteScreenshot(screenshotId); + + verify(httpClientMock).delete(eq(deleteScreenshotUrl), any(), eq(Void.class)); + verifyNoMoreInteractions(httpClientMock); + } + + @Test + public void testAddScreenshot() throws ResponseException { + ScreenshotResponseObject response = new ScreenshotResponseObject() {{ + setData(new Screenshot()); + }}; + when(httpClientMock.post(eq(addScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class))) + .thenReturn(response); + + client.addScreenshot(new AddScreenshotRequest()); + + verify(httpClientMock).post(eq(addScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class)); + verifyNoMoreInteractions(httpClientMock); + } + + @Test + public void testUpdateScreenshot() { + ScreenshotResponseObject response = new ScreenshotResponseObject() {{ + setData(new Screenshot()); + }}; + when(httpClientMock.put(eq(updateScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class))) + .thenReturn(response); + + client.updateScreenshot(screenshotId, new UpdateScreenshotRequest()); + + verify(httpClientMock).put(eq(updateScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class)); + verifyNoMoreInteractions(httpClientMock); + } +} \ No newline at end of file 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 9ead42a9f..eb7f99d27 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java @@ -126,4 +126,19 @@ public void testDistributionRelease() { public void testCheckNewVersion() { assertNotNull(actions.checkNewVersion()); } + + @Test + void testScreenshotList() { + assertNotNull(actions.screenshotList(null, false)); + } + + @Test + void testScreenshotAdd() { + assertNotNull(actions.screenshotAdd(null, null, null, null, false, false, false, null)); + } + + @Test + void testScreenshotDelete() { + assertNotNull(actions.screenshotDelete(null)); + } } diff --git a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotAddActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotAddActionTest.java new file mode 100644 index 000000000..3a197857e --- /dev/null +++ b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotAddActionTest.java @@ -0,0 +1,215 @@ +package com.crowdin.cli.commands.actions; + +import com.crowdin.cli.client.ClientScreenshot; +import com.crowdin.cli.client.CrowdinProjectFull; +import com.crowdin.cli.client.ProjectClient; +import com.crowdin.cli.client.ResponseException; +import com.crowdin.cli.commands.NewAction; +import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; +import com.crowdin.cli.properties.ProjectProperties; +import com.crowdin.cli.properties.PropertiesWithFiles; +import com.crowdin.cli.properties.helper.FileHelperTest; +import com.crowdin.cli.properties.helper.TempProject; +import com.crowdin.cli.utils.Utils; +import com.crowdin.client.screenshots.model.AddScreenshotRequest; +import com.crowdin.client.screenshots.model.Screenshot; +import com.crowdin.client.screenshots.model.UpdateScreenshotRequest; +import com.crowdin.client.sourcefiles.model.Branch; +import com.crowdin.client.sourcefiles.model.Directory; +import com.crowdin.client.sourcefiles.model.FileInfo; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.File; +import java.util.*; +import java.util.stream.Stream; + +import static java.util.Objects.nonNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.params.provider.Arguments.arguments; +import static org.mockito.Mockito.*; + +public class ScreenshotAddActionTest { + + NewAction action; + + private TempProject project; + + @BeforeEach + public void createProj() { + project = new TempProject(FileHelperTest.class); + } + + @AfterEach + public void deleteProj() { + project.delete(); + } + + @ParameterizedTest + @MethodSource + public void testAddScreenshot(String fileName, String sourceFilePath, Long sourceFileId, String branchName, + Long branchId, String directoryPath, Long directoryId, boolean autoTag) throws ResponseException { + File fileToUpload = new File(project.getBasePath() + fileName); + project.addFile(Utils.normalizePath(fileName)); + NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder + .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") + .setBasePath(project.getBasePath()); + PropertiesWithFiles pb = pbBuilder.build(); + + AddScreenshotRequest request = new AddScreenshotRequest(); + request.setStorageId(1L); + request.setName(fileName); + request.setAutoTag(autoTag); + request.setBranchId(branchId); + request.setDirectoryId(directoryId); + request.setFileId(sourceFileId); + ClientScreenshot client = mock(ClientScreenshot.class); + + ProjectClient projectClient = mock(ProjectClient.class); + CrowdinProjectFull projectFull = mock(CrowdinProjectFull.class); + + Branch branch = mock(Branch.class); + FileInfo fileInfo = mock(FileInfo.class); + Directory directory = mock(Directory.class); + + when(projectClient.downloadFullProject()).thenReturn(projectFull); + + when(branch.getId()).thenReturn(branchId); + when(projectFull.findBranchByName(branchName)).thenReturn(Optional.of(branch)); + + when(fileInfo.getId()).thenReturn(sourceFileId); + when(fileInfo.getPath()).thenReturn(sourceFilePath); + when(projectFull.getFileInfos()).thenReturn(Arrays.asList(fileInfo)); + + when(directory.getId()).thenReturn(directoryId); + when(directory.getPath()).thenReturn(directoryPath); + when(projectFull.getDirectories()).thenReturn(nonNull(directoryId) ? Map.of(directoryId, directory) : new HashMap<>()); + + when(projectClient.uploadStorage(eq(fileName), any())).thenReturn(1L); + when(client.listScreenshots(null)).thenReturn(new ArrayList<>()); + + when(client.addScreenshot(request)) + .thenReturn(new Screenshot() {{ + setName(request.getName()); + setId(1L); + }}); + + action = new ScreenshotAddAction(fileToUpload, branchName, sourceFilePath, directoryPath, autoTag, false, false, projectClient); + action.act(Outputter.getDefault(), pb, client); + + verify(client).listScreenshots(null); + verify(client).addScreenshot(request); + verifyNoMoreInteractions(client); + } + + public static Stream testAddScreenshot() { + return Stream.of( + arguments("screenshot.png", null, null, null, null, null, null, false), + arguments("screenshot.png", "/path/to/source/file", 10L, null, null, null, null, true), + arguments("screenshot.png", null, null, "main", 11L, null, null, true), + arguments("screenshot.png", null, null, null, null, "/path/to/directory", 12L, true)); + } + + @Test + public void testAddScreenshotToUpdate() throws ResponseException { + String fileName = "to-upload.png"; + File fileToUpload = new File(project.getBasePath() + fileName); + project.addFile(Utils.normalizePath(fileName)); + NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder + .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") + .setBasePath(project.getBasePath()); + PropertiesWithFiles pb = pbBuilder.build(); + + ClientScreenshot client = mock(ClientScreenshot.class); + Screenshot screenshot = mock(Screenshot.class); + + when(screenshot.getName()).thenReturn(fileName); + when(screenshot.getId()).thenReturn(123L); + when(client.listScreenshots(null)).thenReturn(Arrays.asList(screenshot)); + + UpdateScreenshotRequest request = new UpdateScreenshotRequest(); + request.setStorageId(1L); + request.setName(fileName); + + ProjectClient projectClient = mock(ProjectClient.class); + when(projectClient.uploadStorage(eq(fileName), any())).thenReturn(1L); + + when(client.updateScreenshot(123L, request)) + .thenReturn(new Screenshot() {{ + setName(request.getName()); + setId(123L); + }}); + + action = new ScreenshotAddAction(fileToUpload, null, null, null, false, false, false, projectClient); + action.act(Outputter.getDefault(), pb, client); + + verify(client).listScreenshots(null); + verify(client).updateScreenshot(123L, request); + verifyNoMoreInteractions(client); + } + + @Test + public void testAddScreenshotNotExistingBranch() { + NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder + .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") + .setBasePath(Utils.PATH_SEPARATOR); + PropertiesWithFiles pb = pbBuilder.build(); + + ClientScreenshot client = mock(ClientScreenshot.class); + when(client.listScreenshots(null)).thenReturn(new ArrayList<>()); + + ProjectClient projectClient = mock(ProjectClient.class); + CrowdinProjectFull projectFull = mock(CrowdinProjectFull.class); + when(projectClient.downloadFullProject()).thenReturn(projectFull); + + when(projectFull.findBranchByName("main")).thenReturn(Optional.empty()); + + action = new ScreenshotAddAction(new File("screenshot.png"), "main", null, null, true, false, false, projectClient); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + } + + @Test + public void testAddScreenshotNotExistingSourceFile() { + NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder + .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") + .setBasePath(Utils.PATH_SEPARATOR); + PropertiesWithFiles pb = pbBuilder.build(); + + ClientScreenshot client = mock(ClientScreenshot.class); + when(client.listScreenshots(null)).thenReturn(new ArrayList<>()); + + ProjectClient projectClient = mock(ProjectClient.class); + CrowdinProjectFull projectFull = mock(CrowdinProjectFull.class); + when(projectClient.downloadFullProject()).thenReturn(projectFull); + + when(projectFull.getFileInfos()).thenReturn(new ArrayList<>()); + + action = new ScreenshotAddAction(new File("screenshot.png"), null, "/path/to/file", null, true, false, false, projectClient); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + } + + @Test + public void testAddScreenshotNotExistingDirectory() { + NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder + .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") + .setBasePath(Utils.PATH_SEPARATOR); + PropertiesWithFiles pb = pbBuilder.build(); + + ClientScreenshot client = mock(ClientScreenshot.class); + when(client.listScreenshots(null)).thenReturn(new ArrayList<>()); + + ProjectClient projectClient = mock(ProjectClient.class); + CrowdinProjectFull projectFull = mock(CrowdinProjectFull.class); + when(projectClient.downloadFullProject()).thenReturn(projectFull); + + when(projectFull.getDirectories()).thenReturn(new HashMap<>()); + + action = new ScreenshotAddAction(new File("screenshot.png"), null, null, "/path/to/directory", true, false, false, projectClient); + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + } +} \ No newline at end of file diff --git a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotDeleteActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotDeleteActionTest.java new file mode 100644 index 000000000..1f1ca0404 --- /dev/null +++ b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotDeleteActionTest.java @@ -0,0 +1,89 @@ +package com.crowdin.cli.commands.actions; + +import com.crowdin.cli.client.ClientScreenshot; +import com.crowdin.cli.commands.NewAction; +import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder; +import com.crowdin.cli.properties.ProjectProperties; +import com.crowdin.cli.properties.PropertiesWithFiles; +import com.crowdin.cli.utils.Utils; +import com.crowdin.client.screenshots.model.Screenshot; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.params.provider.Arguments.arguments; +import static org.mockito.Mockito.*; + +public class ScreenshotDeleteActionTest { + + private static final Long SCREENSHOT_ID = 12L; + private static final String SCREENSHOT_NAME = "screenshot.jpg"; + + private NewAction action; + + @ParameterizedTest + @MethodSource + public void testScreenshotDelete(List screenshots) { + NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder + .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") + .setBasePath(Utils.PATH_SEPARATOR); + PropertiesWithFiles pb = pbBuilder.build(); + + ClientScreenshot client = mock(ClientScreenshot.class); + + when(client.listScreenshots(null)) + .thenReturn(screenshots); + doNothing().when(client).deleteScreenshot(SCREENSHOT_ID); + + action = new ScreenshotDeleteAction(SCREENSHOT_NAME); + action.act(Outputter.getDefault(), pb, client); + + verify(client).listScreenshots(null); + verify(client).deleteScreenshot(SCREENSHOT_ID); + verifyNoMoreInteractions(client); + } + + public static Stream testScreenshotDelete() { + return Stream.of( + arguments(Arrays.asList( + new Screenshot() {{ + setName("screenshot1.gif"); + setId(6L); + }}, + new Screenshot() {{ + setName(SCREENSHOT_NAME); + setId(SCREENSHOT_ID); + }} + )) + ); + } + + @Test + public void testScreenshotDelete_throwsNotFound() { + NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder + .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") + .setBasePath(Utils.PATH_SEPARATOR); + PropertiesWithFiles pb = pbBuilder.build(); + + ClientScreenshot client = mock(ClientScreenshot.class); + + when(client.listScreenshots(null)) + .thenReturn(new ArrayList<>()); + doNothing().when(client).deleteScreenshot(SCREENSHOT_ID); + + action = new ScreenshotDeleteAction("not_existing.png"); + + assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); + + verify(client).listScreenshots(null); + verifyNoMoreInteractions(client); + } +} \ No newline at end of file diff --git a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotListActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotListActionTest.java new file mode 100644 index 000000000..e87e2ab9b --- /dev/null +++ b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotListActionTest.java @@ -0,0 +1,84 @@ +package com.crowdin.cli.commands.actions; + +import com.crowdin.cli.client.ClientScreenshot; +import com.crowdin.cli.commands.NewAction; +import com.crowdin.cli.commands.Outputter; +import com.crowdin.cli.properties.ProjectProperties; +import com.crowdin.cli.properties.PropertiesWithFiles; +import com.crowdin.client.screenshots.model.Screenshot; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.mockito.Mockito.*; + +public class ScreenshotListActionTest { + List standardList = Arrays.asList(new Screenshot(), new Screenshot()); + List emptyList = Collections.emptyList(); + + Outputter out = Outputter.getDefault(); + PropertiesWithFiles pb; + + ClientScreenshot clientMock = mock(ClientScreenshot.class); + NewAction action; + + @Test + public void test_standard() { + when(clientMock.listScreenshots(any())).thenReturn(standardList); + + action = new ScreenshotListAction(null, false); + action.act(out, pb, clientMock); + + verify(clientMock).listScreenshots(null); + verifyNoMoreInteractions(clientMock); + } + + @Test + public void test_stringId() { + when(clientMock.listScreenshots(12L)).thenReturn(standardList); + + action = new ScreenshotListAction(12L, false); + action.act(out, pb, clientMock); + + verify(clientMock).listScreenshots(12L); + verifyNoMoreInteractions(clientMock); + } + + @Test + public void test_plainView() { + when(clientMock.listScreenshots(null)) + .thenReturn(standardList); + + action = new ScreenshotListAction(null, true); + action.act(out, pb, clientMock); + + verify(clientMock).listScreenshots(null); + verifyNoMoreInteractions(clientMock); + } + + @Test + public void test_emptyList() { + when(clientMock.listScreenshots(null)) + .thenReturn(emptyList); + + action = new ScreenshotListAction(null, false); + action.act(out, pb, clientMock); + + verify(clientMock).listScreenshots(null); + verifyNoMoreInteractions(clientMock); + } + + @Test + public void test_emptyList_plainView() { + when(clientMock.listScreenshots(null)) + .thenReturn(emptyList); + + action = new ScreenshotListAction(null, true); + action.act(out, pb, clientMock); + + verify(clientMock).listScreenshots(null); + verifyNoMoreInteractions(clientMock); + } +} \ No newline at end of file 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 e2231818f..a12094e71 100644 --- a/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java +++ b/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java @@ -108,6 +108,12 @@ void mockActions() { .thenReturn(actionMock); when(actionsMock.bundleAdd(any(), any(), any(), any(), any(), any(), anyBoolean())) .thenReturn(actionMock); + when(actionsMock.screenshotList(any(), anyBoolean())) + .thenReturn(actionMock); + when(actionsMock.screenshotAdd(any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), any())) + .thenReturn(actionMock); + when(actionsMock.screenshotDelete(any())) + .thenReturn(actionMock); when(actionsMock.resolve(any())) .thenReturn(actionMock); when(actionsMock.commentList(anyBoolean(), anyBoolean(),any(),any(),any(),any())) diff --git a/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommandTest.java b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommandTest.java new file mode 100644 index 000000000..4435b5266 --- /dev/null +++ b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommandTest.java @@ -0,0 +1,73 @@ +package com.crowdin.cli.commands.picocli; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.File; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Stream; + +import static com.crowdin.cli.commands.picocli.GenericCommand.RESOURCE_BUNDLE; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +public class ScreenshotAddSubcommandTest extends PicocliTestUtils { + + @Test + public void testScreenshotAddInvalidOptions() { + this.executeInvalidParams(CommandNames.SCREENSHOT, CommandNames.SCREENSHOT_ADD); + } + + @ParameterizedTest + @MethodSource + public void testSubCommandCheckValidOptions(File file, boolean autoTag, String filePath, String branchName, String directoryPath) { + ScreenshotAddSubcommand screenshotAddSubcommand = new ScreenshotAddSubcommand(); + screenshotAddSubcommand.file = file; + screenshotAddSubcommand.autoTag = autoTag; + screenshotAddSubcommand.filePath = filePath; + screenshotAddSubcommand.branchName = branchName; + screenshotAddSubcommand.directoryPath = directoryPath; + + List errors = screenshotAddSubcommand.checkOptions(); + assertEquals(Collections.emptyList(), errors); + } + + public static Stream testSubCommandCheckValidOptions() { + return Stream.of( + arguments("/path/screenshot1.png", false, null, null, null), + arguments("/path/screenshot2.jpg", true, "/path/sourse/file", null, null), + arguments("/path/screenshot3.jpeg", true, null, "branchTest", null), + arguments("/path/screenshot4.gif", true, null, null, "/path/to/directory")); + } + + @ParameterizedTest + @MethodSource + public void testSubCommandCheckInvalidOptions(File file, boolean autoTag, String filePath, String branchName, String directoryPath, List expErrors) { + ScreenshotAddSubcommand screenshotAddSubcommand = new ScreenshotAddSubcommand(); + screenshotAddSubcommand.file = file; + screenshotAddSubcommand.autoTag = autoTag; + screenshotAddSubcommand.filePath = filePath; + screenshotAddSubcommand.branchName = branchName; + screenshotAddSubcommand.directoryPath = directoryPath; + + List errors = screenshotAddSubcommand.checkOptions(); + assertThat(errors, Matchers.equalTo(expErrors)); + } + + public static Stream testSubCommandCheckInvalidOptions() { + return Stream.of( + arguments("/path/screenshot1.zip", false, null, null, null, Arrays.asList(RESOURCE_BUNDLE.getString("error.screenshot.wrong_format"))), + arguments("/path/screenshot2.jpg", false, "/path/sourse/file", null, null, Arrays.asList(RESOURCE_BUNDLE.getString("error.screenshot.auto-tag_required_for_file"))), + arguments("/path/screenshot3.jpeg", false, null, "branchTest", null, Arrays.asList(RESOURCE_BUNDLE.getString("error.screenshot.auto-tag_required_for_branch"))), + arguments("/path/screenshot4.png", false, null, null, "/path/to/directory", Arrays.asList(RESOURCE_BUNDLE.getString("error.screenshot.auto-tag_required_for_directory"))), + arguments("/path/screenshot5.jpg", true, "/path/sourse/file", null, "/path/to/directory", Arrays.asList(RESOURCE_BUNDLE.getString("error.screenshot.only_one_allowed"))), + arguments("/path/screenshot6.gif", true, null, "branchTest", "/path/to/directory", Arrays.asList(RESOURCE_BUNDLE.getString("error.screenshot.only_one_allowed"))) + ); + } +} \ No newline at end of file diff --git a/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommandTest.java b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommandTest.java new file mode 100644 index 000000000..f4e3009ea --- /dev/null +++ b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotDeleteSubcommandTest.java @@ -0,0 +1,21 @@ +package com.crowdin.cli.commands.picocli; + +import org.junit.jupiter.api.Test; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +public class ScreenshotDeleteSubcommandTest extends PicocliTestUtils { + + @Test + public void testScreenshotDelete() { + this.execute(CommandNames.SCREENSHOT, CommandNames.SCREENSHOT_DELETE, "screenshot.png"); + verify(actionsMock).screenshotDelete(any()); + this.check(true); + } + + @Test + public void testScreenshotDeleteInvalidOptions() { + this.executeInvalidParams(CommandNames.STRING, CommandNames.STRING_DELETE); + } +} \ No newline at end of file diff --git a/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommandTest.java b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommandTest.java new file mode 100644 index 000000000..26c41a767 --- /dev/null +++ b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotListSubcommandTest.java @@ -0,0 +1,17 @@ +package com.crowdin.cli.commands.picocli; + +import org.junit.jupiter.api.Test; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.verify; + +public class ScreenshotListSubcommandTest extends PicocliTestUtils { + + @Test + public void testScreenshotList() { + this.execute(CommandNames.SCREENSHOT, CommandNames.SCREENSHOT_LIST); + verify(actionsMock).screenshotList(any(), anyBoolean()); + this.check(true); + } +} From e52fb54a50d239c6dd323a9a35b0b57db53d4972 Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Tue, 5 Sep 2023 19:26:18 +0300 Subject: [PATCH 3/8] feat: docs for screenshot command --- website/mantemplates/crowdin-screenshot-add.adoc | 16 ++++++++++++++++ .../mantemplates/crowdin-screenshot-delete.adoc | 16 ++++++++++++++++ .../mantemplates/crowdin-screenshot-list.adoc | 16 ++++++++++++++++ website/mantemplates/crowdin-screenshot.adoc | 16 ++++++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 website/mantemplates/crowdin-screenshot-add.adoc create mode 100644 website/mantemplates/crowdin-screenshot-delete.adoc create mode 100644 website/mantemplates/crowdin-screenshot-list.adoc create mode 100644 website/mantemplates/crowdin-screenshot.adoc diff --git a/website/mantemplates/crowdin-screenshot-add.adoc b/website/mantemplates/crowdin-screenshot-add.adoc new file mode 100644 index 000000000..9e7a0703a --- /dev/null +++ b/website/mantemplates/crowdin-screenshot-add.adoc @@ -0,0 +1,16 @@ +:includedir: ../generated-picocli-docs +:command: crowdin-screenshot-add + +== crowdin screenshot add + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-description] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-synopsis] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-arguments] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-commands] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-options] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-footer] diff --git a/website/mantemplates/crowdin-screenshot-delete.adoc b/website/mantemplates/crowdin-screenshot-delete.adoc new file mode 100644 index 000000000..26c052537 --- /dev/null +++ b/website/mantemplates/crowdin-screenshot-delete.adoc @@ -0,0 +1,16 @@ +:includedir: ../generated-picocli-docs +:command: crowdin-screenshot-delete + +== crowdin screenshot delete + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-description] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-synopsis] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-arguments] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-commands] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-options] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-footer] diff --git a/website/mantemplates/crowdin-screenshot-list.adoc b/website/mantemplates/crowdin-screenshot-list.adoc new file mode 100644 index 000000000..7ce56567e --- /dev/null +++ b/website/mantemplates/crowdin-screenshot-list.adoc @@ -0,0 +1,16 @@ +:includedir: ../generated-picocli-docs +:command: crowdin-screenshot-list + +== crowdin screenshot list + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-description] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-synopsis] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-arguments] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-commands] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-options] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-footer] diff --git a/website/mantemplates/crowdin-screenshot.adoc b/website/mantemplates/crowdin-screenshot.adoc new file mode 100644 index 000000000..8abcd2efe --- /dev/null +++ b/website/mantemplates/crowdin-screenshot.adoc @@ -0,0 +1,16 @@ +:includedir: ../generated-picocli-docs +:command: crowdin-screenshot + +== crowdin screenshot + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-description] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-synopsis] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-arguments] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-commands] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-options] + +include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-footer] From 83f7bb1c4044f52de68cbc33245a517d674ba511 Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Tue, 5 Sep 2023 19:58:16 +0300 Subject: [PATCH 4/8] feat: rename add subcommand to upload --- .../crowdin/cli/client/ClientScreenshot.java | 2 +- .../cli/client/CrowdinClientScreenshot.java | 2 +- .../com/crowdin/cli/commands/Actions.java | 2 +- .../cli/commands/actions/CliActions.java | 4 +-- ...ction.java => ScreenshotUploadAction.java} | 8 ++--- .../cli/commands/picocli/CommandNames.java | 2 +- .../picocli/ScreenshotSubcommand.java | 2 +- ...d.java => ScreenshotUploadSubcommand.java} | 16 ++++----- .../resources/messages/messages.properties | 20 +++++------ .../client/CrowdinClientScreenshotTest.java | 10 +++--- .../cli/commands/actions/CliActionsTest.java | 4 +-- ...t.java => ScreenshotUploadActionTest.java} | 30 ++++++++-------- .../commands/picocli/PicocliTestUtils.java | 2 +- ...va => ScreenshotUploadSubcommandTest.java} | 34 +++++++++---------- ...dd.adoc => crowdin-screenshot-upload.adoc} | 4 +-- 15 files changed, 71 insertions(+), 71 deletions(-) rename src/main/java/com/crowdin/cli/commands/actions/{ScreenshotAddAction.java => ScreenshotUploadAction.java} (95%) rename src/main/java/com/crowdin/cli/commands/picocli/{ScreenshotAddSubcommand.java => ScreenshotUploadSubcommand.java} (83%) rename src/test/java/com/crowdin/cli/commands/actions/{ScreenshotAddActionTest.java => ScreenshotUploadActionTest.java} (84%) rename src/test/java/com/crowdin/cli/commands/picocli/{ScreenshotAddSubcommandTest.java => ScreenshotUploadSubcommandTest.java} (71%) rename website/mantemplates/{crowdin-screenshot-add.adoc => crowdin-screenshot-upload.adoc} (86%) diff --git a/src/main/java/com/crowdin/cli/client/ClientScreenshot.java b/src/main/java/com/crowdin/cli/client/ClientScreenshot.java index 0e8163d4b..306ade31b 100644 --- a/src/main/java/com/crowdin/cli/client/ClientScreenshot.java +++ b/src/main/java/com/crowdin/cli/client/ClientScreenshot.java @@ -10,7 +10,7 @@ public interface ClientScreenshot extends Client { List listScreenshots(Long stringId); - Screenshot addScreenshot(AddScreenshotRequest request) throws ResponseException; + Screenshot uploadScreenshot(AddScreenshotRequest request) throws ResponseException; Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request); diff --git a/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java index e08600501..785bc6fc2 100644 --- a/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java +++ b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java @@ -26,7 +26,7 @@ public List listScreenshots(Long stringId) { } @Override - public Screenshot addScreenshot(AddScreenshotRequest request) throws ResponseException { + public Screenshot uploadScreenshot(AddScreenshotRequest request) throws ResponseException { Map, ResponseException> errorHandler = new LinkedHashMap, ResponseException>() {{ put((code, message) -> code.equals("409") && message.contains("Auto tag is currently in progress"), new AutoTagInProgressException()); diff --git a/src/main/java/com/crowdin/cli/commands/Actions.java b/src/main/java/com/crowdin/cli/commands/Actions.java index 443e994f5..237fcfa47 100644 --- a/src/main/java/com/crowdin/cli/commands/Actions.java +++ b/src/main/java/com/crowdin/cli/commands/Actions.java @@ -119,7 +119,7 @@ NewAction preTranslate( NewAction screenshotList(Long stringId, boolean plainView); - NewAction screenshotAdd(File file, String branchName, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient); + NewAction screenshotUpload(File file, String branchName, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient); NewAction screenshotDelete(String name); } 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 35fa659f4..0ef479ece 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/CliActions.java +++ b/src/main/java/com/crowdin/cli/commands/actions/CliActions.java @@ -248,8 +248,8 @@ public NewAction screenshotList(Long string } @Override - public NewAction screenshotAdd(File file, String branchName, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient) { - return new ScreenshotAddAction(file, branchName, filePath, directoryPath, autoTag, plainView, noProgress, projectClient); + public NewAction screenshotUpload(File file, String branchName, String directoryPath, String filePath, boolean autoTag, boolean plainView, boolean noProgress, ProjectClient projectClient) { + return new ScreenshotUploadAction(file, branchName, filePath, directoryPath, autoTag, plainView, noProgress, projectClient); } @Override diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java similarity index 95% rename from src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java rename to src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java index 05d29ebd3..74385fbc1 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotAddAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java @@ -28,7 +28,7 @@ import static java.util.Objects.nonNull; @AllArgsConstructor -class ScreenshotAddAction implements NewAction { +class ScreenshotUploadAction implements NewAction { private final File file; private final String branchName; @@ -91,7 +91,7 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl request.setAutoTag(autoTag); try { - client.addScreenshot(request); + client.uploadScreenshot(request); } catch (AutoTagInProgressException e) { if (!plainView) { out.println(WARNING.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.not_auto-tagged"), file.getName()))); @@ -99,10 +99,10 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl out.println(String.format(RESOURCE_BUNDLE.getString("message.screenshot.not_auto-tagged"), file.getName())); } } catch (Exception e) { - throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_added"), request), e); + throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_uploaded"), request), e); } if (!plainView) { - out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.added"), file.getName()))); + out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.uploaded"), file.getName()))); } else { out.println(file.getName()); } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java b/src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java index 10a9d4ad8..8cf92fbd2 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/CommandNames.java @@ -72,6 +72,6 @@ public final class CommandNames { public static final String SCREENSHOT = "screenshot"; public static final String SCREENSHOT_LIST = "list"; - public static final String SCREENSHOT_ADD = "add"; + public static final String SCREENSHOT_UPLOAD = "upload"; public static final String SCREENSHOT_DELETE = "delete"; } diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java index b3b359ee7..d52874d68 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotSubcommand.java @@ -7,7 +7,7 @@ name = CommandNames.SCREENSHOT, subcommands = { ScreenshotListSubcommand.class, - ScreenshotAddSubcommand.class, + ScreenshotUploadSubcommand.class, ScreenshotDeleteSubcommand.class } ) diff --git a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommand.java similarity index 83% rename from src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java rename to src/main/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommand.java index 0a62b3ccc..a9035ac49 100644 --- a/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommand.java +++ b/src/main/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommand.java @@ -17,24 +17,24 @@ import static java.util.Objects.nonNull; @CommandLine.Command( - name = CommandNames.SCREENSHOT_ADD, + name = CommandNames.SCREENSHOT_UPLOAD, sortOptions = false ) -class ScreenshotAddSubcommand extends ActCommandScreenshot{ +class ScreenshotUploadSubcommand extends ActCommandScreenshot{ - @CommandLine.Parameters(descriptionKey = "crowdin.screenshot.add.file") + @CommandLine.Parameters(descriptionKey = "crowdin.screenshot.upload.file") protected File file; - @CommandLine.Option(names = {"--auto-tag"}, negatable = true, descriptionKey = "crowdin.screenshot.add.auto-tag", order = -2) + @CommandLine.Option(names = {"--auto-tag"}, negatable = true, descriptionKey = "crowdin.screenshot.upload.auto-tag", order = -2) protected boolean autoTag; - @CommandLine.Option(names = {"-f", "--file"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.file-path", order = -2) + @CommandLine.Option(names = {"-f", "--file"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.upload.file-path", order = -2) protected String filePath; - @CommandLine.Option(names = {"-b", "--branch"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.branch-name", order = -2) + @CommandLine.Option(names = {"-b", "--branch"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.upload.branch-name", order = -2) protected String branchName; - @CommandLine.Option(names = {"-d", "--directory"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.add.directory-path", order = -2) + @CommandLine.Option(names = {"-d", "--directory"}, paramLabel = "...", descriptionKey = "crowdin.screenshot.upload.directory-path", order = -2) protected String directoryPath; @CommandLine.Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain") @@ -44,7 +44,7 @@ class ScreenshotAddSubcommand extends ActCommandScreenshot{ protected NewAction getAction(Actions actions) { Outputter out = new PicocliOutputter(System.out, isAnsi()); ProjectClient projectClient = this.getProjectClient(this.getProperties(propertiesBuilders, out)); - return actions.screenshotAdd(file, branchName, directoryPath, filePath, autoTag, plainView, this.noProgress, projectClient); + return actions.screenshotUpload(file, branchName, directoryPath, filePath, autoTag, plainView, this.noProgress, projectClient); } @Override diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index 4ad071902..215ef9cd6 100755 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -364,14 +364,14 @@ crowdin.screenshot.delete.usage.description=Delete screenshot crowdin.screenshot.delete.usage.customSynopsis=@|fg(green) crowdin screenshot delete|@ [CONFIG OPTIONS] [OPTIONS] crowdin.screenshot.delete.name=Screenshot name -# CROWDIN SCREENSHOT ADD -crowdin.screenshot.add.usage.description=Add screenshot -crowdin.screenshot.add.usage.customSynopsis=@|fg(green) crowdin screenshot add|@ [CONFIG OPTIONS] [OPTIONS] -crowdin.screenshot.add.file=File path to add -crowdin.screenshot.add.auto-tag=Choose whether or not to tag screenshot automatically -crowdin.screenshot.add.file-path=Path to source file in Crowdin -crowdin.screenshot.add.branch-name=Branch name -crowdin.screenshot.add.directory-path=Path to directory in Crowdin +# CROWDIN SCREENSHOT UPLOAD +crowdin.screenshot.upload.usage.description=Add screenshot +crowdin.screenshot.upload.usage.customSynopsis=@|fg(green) crowdin screenshot add|@ [CONFIG OPTIONS] [OPTIONS] +crowdin.screenshot.upload.file=File path to add +crowdin.screenshot.upload.auto-tag=Choose whether or not to tag screenshot automatically +crowdin.screenshot.upload.file-path=Path to source file in Crowdin +crowdin.screenshot.upload.branch-name=Branch name +crowdin.screenshot.upload.directory-path=Path to directory in Crowdin error.collect_project_info=Failed to collect project info. Please contact our support team for help error.no_sources=No sources found for '%s' pattern. Check the source paths in your configuration file @@ -504,7 +504,7 @@ error.screenshot.auto-tag_required_for_branch='--auto-tag' is required for '--br error.screenshot.auto-tag_required_for_directory='--auto-tag' is required for '--directory' option error.screenshot.only_one_allowed=Only one of the following options can be used at a time: '--file', '--branch' or '--directory' error.screenshot.not_found=Screenshot %s not found -error.screenshot.not_added=Screenshot was not added +error.screenshot.not_uploaded=Screenshot was not uploaded error.screenshot.not_updated=Screenshot was not updated error.response.401=Couldn't authorize. Check your 'api_token' @@ -630,7 +630,7 @@ message.tm.list=Translation memory @|green %s|@ (@|yellow #%d|@, segments: @|bol message.tm.import_success=Imported in @|yellow #%s|@ @|green '%s'|@ translation memory message.tm.list_empty=No translation memories found -message.screenshot.added=@|green,bold Screenshot '%s'|@ @|green added successfully|@ +message.screenshot.uploaded=@|green,bold Screenshot '%s'|@ @|green uploaded successfully|@ message.screenshot.updated=@|green,bold Screenshot '%s'|@ @|green updated successfully|@ message.screenshot.deleted=@|green,bold Screenshot '%s'|@ @|green deleted successfully|@ message.screenshot.not_auto-tagged=Tags were not applied for %s because auto tag is currently in progress diff --git a/src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java b/src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java index 1973c62ad..72a4bc815 100644 --- a/src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java +++ b/src/test/java/com/crowdin/cli/client/CrowdinClientScreenshotTest.java @@ -27,7 +27,7 @@ class CrowdinClientScreenshotTest { private static final String listScreenshotUrl = String.format("%s/projects/%d/screenshots", url, projectId); private static final String deleteScreenshotUrl = String.format("%s/projects/%d/screenshots/%d", url, projectId, screenshotId); - private static final String addScreenshotUrl = String.format("%s/projects/%d/screenshots", url, projectId); + private static final String uploadScreenshotUrl = String.format("%s/projects/%d/screenshots", url, projectId); private static final String updateScreenshotUrl = String.format("%s/projects/%d/screenshots/%d", url, projectId, screenshotId); @BeforeEach @@ -65,16 +65,16 @@ public void testDeleteScreenshot() { } @Test - public void testAddScreenshot() throws ResponseException { + public void testUploadScreenshot() throws ResponseException { ScreenshotResponseObject response = new ScreenshotResponseObject() {{ setData(new Screenshot()); }}; - when(httpClientMock.post(eq(addScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class))) + when(httpClientMock.post(eq(uploadScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class))) .thenReturn(response); - client.addScreenshot(new AddScreenshotRequest()); + client.uploadScreenshot(new AddScreenshotRequest()); - verify(httpClientMock).post(eq(addScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class)); + verify(httpClientMock).post(eq(uploadScreenshotUrl), any(), any(), eq(ScreenshotResponseObject.class)); verifyNoMoreInteractions(httpClientMock); } 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 eb7f99d27..b75958835 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java @@ -133,8 +133,8 @@ void testScreenshotList() { } @Test - void testScreenshotAdd() { - assertNotNull(actions.screenshotAdd(null, null, null, null, false, false, false, null)); + void testScreenshotUpload() { + assertNotNull(actions.screenshotUpload(null, null, null, null, false, false, false, null)); } @Test diff --git a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotAddActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java similarity index 84% rename from src/test/java/com/crowdin/cli/commands/actions/ScreenshotAddActionTest.java rename to src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java index 3a197857e..416735b02 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotAddActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java @@ -34,7 +34,7 @@ import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.mockito.Mockito.*; -public class ScreenshotAddActionTest { +public class ScreenshotUploadActionTest { NewAction action; @@ -52,8 +52,8 @@ public void deleteProj() { @ParameterizedTest @MethodSource - public void testAddScreenshot(String fileName, String sourceFilePath, Long sourceFileId, String branchName, - Long branchId, String directoryPath, Long directoryId, boolean autoTag) throws ResponseException { + public void testUploadScreenshot(String fileName, String sourceFilePath, Long sourceFileId, String branchName, + Long branchId, String directoryPath, Long directoryId, boolean autoTag) throws ResponseException { File fileToUpload = new File(project.getBasePath() + fileName); project.addFile(Utils.normalizePath(fileName)); NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder @@ -93,21 +93,21 @@ public void testAddScreenshot(String fileName, String sourceFilePath, Long sourc when(projectClient.uploadStorage(eq(fileName), any())).thenReturn(1L); when(client.listScreenshots(null)).thenReturn(new ArrayList<>()); - when(client.addScreenshot(request)) + when(client.uploadScreenshot(request)) .thenReturn(new Screenshot() {{ setName(request.getName()); setId(1L); }}); - action = new ScreenshotAddAction(fileToUpload, branchName, sourceFilePath, directoryPath, autoTag, false, false, projectClient); + action = new ScreenshotUploadAction(fileToUpload, branchName, sourceFilePath, directoryPath, autoTag, false, false, projectClient); action.act(Outputter.getDefault(), pb, client); verify(client).listScreenshots(null); - verify(client).addScreenshot(request); + verify(client).uploadScreenshot(request); verifyNoMoreInteractions(client); } - public static Stream testAddScreenshot() { + public static Stream testUploadScreenshot() { return Stream.of( arguments("screenshot.png", null, null, null, null, null, null, false), arguments("screenshot.png", "/path/to/source/file", 10L, null, null, null, null, true), @@ -116,7 +116,7 @@ public static Stream testAddScreenshot() { } @Test - public void testAddScreenshotToUpdate() throws ResponseException { + public void testUploadScreenshotToUpdate() throws ResponseException { String fileName = "to-upload.png"; File fileToUpload = new File(project.getBasePath() + fileName); project.addFile(Utils.normalizePath(fileName)); @@ -145,7 +145,7 @@ public void testAddScreenshotToUpdate() throws ResponseException { setId(123L); }}); - action = new ScreenshotAddAction(fileToUpload, null, null, null, false, false, false, projectClient); + action = new ScreenshotUploadAction(fileToUpload, null, null, null, false, false, false, projectClient); action.act(Outputter.getDefault(), pb, client); verify(client).listScreenshots(null); @@ -154,7 +154,7 @@ public void testAddScreenshotToUpdate() throws ResponseException { } @Test - public void testAddScreenshotNotExistingBranch() { + public void testUploadScreenshotNotExistingBranch() { NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") .setBasePath(Utils.PATH_SEPARATOR); @@ -169,12 +169,12 @@ public void testAddScreenshotNotExistingBranch() { when(projectFull.findBranchByName("main")).thenReturn(Optional.empty()); - action = new ScreenshotAddAction(new File("screenshot.png"), "main", null, null, true, false, false, projectClient); + action = new ScreenshotUploadAction(new File("screenshot.png"), "main", null, null, true, false, false, projectClient); assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); } @Test - public void testAddScreenshotNotExistingSourceFile() { + public void testUploadScreenshotNotExistingSourceFile() { NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") .setBasePath(Utils.PATH_SEPARATOR); @@ -189,12 +189,12 @@ public void testAddScreenshotNotExistingSourceFile() { when(projectFull.getFileInfos()).thenReturn(new ArrayList<>()); - action = new ScreenshotAddAction(new File("screenshot.png"), null, "/path/to/file", null, true, false, false, projectClient); + action = new ScreenshotUploadAction(new File("screenshot.png"), null, "/path/to/file", null, true, false, false, projectClient); assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); } @Test - public void testAddScreenshotNotExistingDirectory() { + public void testUploadScreenshotNotExistingDirectory() { NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder .minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%") .setBasePath(Utils.PATH_SEPARATOR); @@ -209,7 +209,7 @@ public void testAddScreenshotNotExistingDirectory() { when(projectFull.getDirectories()).thenReturn(new HashMap<>()); - action = new ScreenshotAddAction(new File("screenshot.png"), null, null, "/path/to/directory", true, false, false, projectClient); + action = new ScreenshotUploadAction(new File("screenshot.png"), null, null, "/path/to/directory", true, false, false, projectClient); assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client)); } } \ No newline at end of file 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 a12094e71..e06a588f8 100644 --- a/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java +++ b/src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java @@ -110,7 +110,7 @@ void mockActions() { .thenReturn(actionMock); when(actionsMock.screenshotList(any(), anyBoolean())) .thenReturn(actionMock); - when(actionsMock.screenshotAdd(any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), any())) + when(actionsMock.screenshotUpload(any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean(), any())) .thenReturn(actionMock); when(actionsMock.screenshotDelete(any())) .thenReturn(actionMock); diff --git a/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommandTest.java b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommandTest.java similarity index 71% rename from src/test/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommandTest.java rename to src/test/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommandTest.java index 4435b5266..93a0e1ce8 100644 --- a/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotAddSubcommandTest.java +++ b/src/test/java/com/crowdin/cli/commands/picocli/ScreenshotUploadSubcommandTest.java @@ -17,24 +17,24 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.params.provider.Arguments.arguments; -public class ScreenshotAddSubcommandTest extends PicocliTestUtils { +public class ScreenshotUploadSubcommandTest extends PicocliTestUtils { @Test - public void testScreenshotAddInvalidOptions() { - this.executeInvalidParams(CommandNames.SCREENSHOT, CommandNames.SCREENSHOT_ADD); + public void testScreenshotUploadInvalidOptions() { + this.executeInvalidParams(CommandNames.SCREENSHOT, CommandNames.SCREENSHOT_UPLOAD); } @ParameterizedTest @MethodSource public void testSubCommandCheckValidOptions(File file, boolean autoTag, String filePath, String branchName, String directoryPath) { - ScreenshotAddSubcommand screenshotAddSubcommand = new ScreenshotAddSubcommand(); - screenshotAddSubcommand.file = file; - screenshotAddSubcommand.autoTag = autoTag; - screenshotAddSubcommand.filePath = filePath; - screenshotAddSubcommand.branchName = branchName; - screenshotAddSubcommand.directoryPath = directoryPath; + ScreenshotUploadSubcommand screenshotUploadSubcommand = new ScreenshotUploadSubcommand(); + screenshotUploadSubcommand.file = file; + screenshotUploadSubcommand.autoTag = autoTag; + screenshotUploadSubcommand.filePath = filePath; + screenshotUploadSubcommand.branchName = branchName; + screenshotUploadSubcommand.directoryPath = directoryPath; - List errors = screenshotAddSubcommand.checkOptions(); + List errors = screenshotUploadSubcommand.checkOptions(); assertEquals(Collections.emptyList(), errors); } @@ -49,14 +49,14 @@ public static Stream testSubCommandCheckValidOptions() { @ParameterizedTest @MethodSource public void testSubCommandCheckInvalidOptions(File file, boolean autoTag, String filePath, String branchName, String directoryPath, List expErrors) { - ScreenshotAddSubcommand screenshotAddSubcommand = new ScreenshotAddSubcommand(); - screenshotAddSubcommand.file = file; - screenshotAddSubcommand.autoTag = autoTag; - screenshotAddSubcommand.filePath = filePath; - screenshotAddSubcommand.branchName = branchName; - screenshotAddSubcommand.directoryPath = directoryPath; + ScreenshotUploadSubcommand screenshotUploadSubcommand = new ScreenshotUploadSubcommand(); + screenshotUploadSubcommand.file = file; + screenshotUploadSubcommand.autoTag = autoTag; + screenshotUploadSubcommand.filePath = filePath; + screenshotUploadSubcommand.branchName = branchName; + screenshotUploadSubcommand.directoryPath = directoryPath; - List errors = screenshotAddSubcommand.checkOptions(); + List errors = screenshotUploadSubcommand.checkOptions(); assertThat(errors, Matchers.equalTo(expErrors)); } diff --git a/website/mantemplates/crowdin-screenshot-add.adoc b/website/mantemplates/crowdin-screenshot-upload.adoc similarity index 86% rename from website/mantemplates/crowdin-screenshot-add.adoc rename to website/mantemplates/crowdin-screenshot-upload.adoc index 9e7a0703a..b09df9617 100644 --- a/website/mantemplates/crowdin-screenshot-add.adoc +++ b/website/mantemplates/crowdin-screenshot-upload.adoc @@ -1,7 +1,7 @@ :includedir: ../generated-picocli-docs -:command: crowdin-screenshot-add +:command: crowdin-screenshot-upload -== crowdin screenshot add +== crowdin screenshot upload include::{includedir}/{command}.adoc[tag=picocli-generated-man-section-description] From 80e73adc84c3eef6b376e8130a553ffb9f7fc2b8 Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Tue, 5 Sep 2023 20:13:50 +0300 Subject: [PATCH 5/8] feat: fix map initializing and doc config --- .../actions/ScreenshotUploadActionTest.java | 5 ++++- website/sidebars.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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 416735b02..36f2c63a0 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java @@ -88,7 +88,10 @@ public void testUploadScreenshot(String fileName, String sourceFilePath, Long so when(directory.getId()).thenReturn(directoryId); when(directory.getPath()).thenReturn(directoryPath); - when(projectFull.getDirectories()).thenReturn(nonNull(directoryId) ? Map.of(directoryId, directory) : new HashMap<>()); + + Map directories = new HashMap<>(); + directories.put(directoryId, directory); + when(projectFull.getDirectories()).thenReturn(nonNull(directoryId) ? directories : new HashMap<>()); when(projectClient.uploadStorage(eq(fileName), any())).thenReturn(1L); when(client.listScreenshots(null)).thenReturn(new ArrayList<>()); diff --git a/website/sidebars.js b/website/sidebars.js index e0bc9b251..8abe87c74 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -203,6 +203,21 @@ const sidebars = { 'commands/crowdin-distribution-release', ] }, + { + type: 'category', + label: 'crowdin screenshot', + link: { + type: 'doc', + id: 'commands/crowdin-screenshot' + }, + collapsible: true, + collapsed: true, + items: [ + 'commands/crowdin-screenshot-upload', + 'commands/crowdin-screenshot-list', + 'commands/crowdin-screenshot-delete', + ] + }, 'commands/crowdin-pre-translate', ], }, From 7f1d9fe4b6171839c617d2b1d769c0c4d0dcec12 Mon Sep 17 00:00:00 2001 From: Andrii Bodnar Date: Wed, 6 Sep 2023 10:36:43 +0300 Subject: [PATCH 6/8] docs: fix --[no-]auto-tag option highlight --- prepare-docs.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/prepare-docs.sh b/prepare-docs.sh index 4cd817aa6..d67ea8b84 100755 --- a/prepare-docs.sh +++ b/prepare-docs.sh @@ -78,6 +78,7 @@ sed -i.bak -e 's/\*\*--\[no-\]auto-approve-imported\*\*/`--[no-]auto-approve-imp sed -i.bak -e 's/\*\*--\[no-\]import-eq-suggestions\*\*/`--[no-]import-eq-suggestions`/g' -- *.md sed -i.bak -e 's/\*\*--\[no-\]translate-hidden\*\*/`--[no-]translate-hidden`/g' -- *.md sed -i.bak -e 's/\*\*--\[no-\]preserve-hierarchy\*\*/`--[no-]preserve-hierarchy`/g' -- *.md +sed -i.bak -e 's/\*\*--\[no-\]auto-tag\*\*/`--[no-]auto-tag`/g' -- *.md rm -- *.md.bak From 0835b775066f73843cbd250d01705fbb2d5bff28 Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Wed, 6 Sep 2023 16:28:26 +0300 Subject: [PATCH 7/8] feat: fix messages --- src/main/resources/messages/messages.properties | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index 215ef9cd6..569e27196 100755 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -351,12 +351,12 @@ crowdin.distribution.release.usage.customSynopsis=@|fg(green) crowdin distributi crowdin.distribution.release.hash=Distribution hash # CROWDIN SCREENSHOT COMMAND -crowdin.screenshot.usage.description=Manage screenshots +crowdin.screenshot.usage.description=Manage screenshots in a Crowdin project crowdin.screenshot.usage.customSynopsis=@|fg(green) crowdin screenshot|@ [SUBCOMMAND] [CONFIG OPTIONS] [OPTIONS] # CROWDIN SCREENSHOT LIST crowdin.screenshot.list.usage.description=List screenshots -crowdin.screenshot.list.usage.customSynopsis=@|fg(green) crowdin screenshot list|@ [CONFIG OPTIONS] [OPTIONS] +crowdin.screenshot.list.usage.customSynopsis=@|fg(green) crowdin screenshot list|@ [CONFIG OPTIONS] [OPTIONS] crowdin.screenshot.list.string-id=Numeric string identifier # CROWDIN SCREENSHOT DELETE @@ -366,12 +366,12 @@ crowdin.screenshot.delete.name=Screenshot name # CROWDIN SCREENSHOT UPLOAD crowdin.screenshot.upload.usage.description=Add screenshot -crowdin.screenshot.upload.usage.customSynopsis=@|fg(green) crowdin screenshot add|@ [CONFIG OPTIONS] [OPTIONS] +crowdin.screenshot.upload.usage.customSynopsis=@|fg(green) crowdin screenshot add|@ [CONFIG OPTIONS] [OPTIONS] crowdin.screenshot.upload.file=File path to add crowdin.screenshot.upload.auto-tag=Choose whether or not to tag screenshot automatically -crowdin.screenshot.upload.file-path=Path to source file in Crowdin -crowdin.screenshot.upload.branch-name=Branch name -crowdin.screenshot.upload.directory-path=Path to directory in Crowdin +crowdin.screenshot.upload.file-path=Path to the source file in Crowdin. Requires the '--auto-tag' to be passed +crowdin.screenshot.upload.branch-name=Branch name. Requires the '--auto-tag' to be passed +crowdin.screenshot.upload.directory-path=Path to the directory in Crowdin. Requires the '--auto-tag' to be passed error.collect_project_info=Failed to collect project info. Please contact our support team for help error.no_sources=No sources found for '%s' pattern. Check the source paths in your configuration file @@ -503,7 +503,7 @@ error.screenshot.auto-tag_required_for_file='--auto-tag' is required for '--file error.screenshot.auto-tag_required_for_branch='--auto-tag' is required for '--branch' option error.screenshot.auto-tag_required_for_directory='--auto-tag' is required for '--directory' option error.screenshot.only_one_allowed=Only one of the following options can be used at a time: '--file', '--branch' or '--directory' -error.screenshot.not_found=Screenshot %s not found +error.screenshot.not_found=Screenshot %s not found in the Crowdin project error.screenshot.not_uploaded=Screenshot was not uploaded error.screenshot.not_updated=Screenshot was not updated From 22cee5636ef45fa1d5926d4b2929d1cb9f0d284f Mon Sep 17 00:00:00 2001 From: Kateryna Oblakevych Date: Wed, 6 Sep 2023 17:32:44 +0300 Subject: [PATCH 8/8] feat: fix paths --- .../cli/commands/actions/ScreenshotUploadAction.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) 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 74385fbc1..98b0c21b1 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java @@ -7,6 +7,7 @@ import com.crowdin.cli.commands.NewAction; import com.crowdin.cli.commands.Outputter; import com.crowdin.cli.properties.ProjectProperties; +import com.crowdin.cli.utils.Utils; import com.crowdin.cli.utils.console.ConsoleSpinner; import com.crowdin.client.screenshots.model.AddScreenshotRequest; import com.crowdin.client.screenshots.model.Screenshot; @@ -75,14 +76,16 @@ public void act(Outputter out, ProjectProperties properties, ClientScreenshot cl request.setBranchId(branch.getId()); } if (nonNull(pathToSourceFile)) { + final String normalizedPath = Utils.unixPath(Utils.sepAtStart(pathToSourceFile)); FileInfo fileInfo = project.getFileInfos().stream() - .filter(f -> pathToSourceFile.equals(f.getPath())).findFirst() + .filter(f -> normalizedPath.equals(f.getPath())).findFirst() .orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), pathToSourceFile))); request.setFileId(fileInfo.getId()); } if (nonNull(directoryPath)) { + final String normalizedPath = Utils.unixPath(Utils.sepAtStart(directoryPath)); Directory directory = project.getDirectories().values().stream() - .filter(d -> directoryPath.equals(d.getPath())).findFirst() + .filter(d -> normalizedPath.equals(d.getPath())).findFirst() .orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.dir_not_exists"), directoryPath))); request.setDirectoryId(directory.getId()); }