Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: screenshot delete message #814

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/crowdin/cli/client/ClientScreenshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface ClientScreenshot extends Client {

List<Screenshot> listScreenshots(Long stringId);

Screenshot getScreenshot(Long id);

Screenshot uploadScreenshot(AddScreenshotRequest request) throws ResponseException;

Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/crowdin/cli/client/CrowdinClientCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ abstract class CrowdinClientCore {
(msg) -> new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.response.404_organization_not_found")));
put((code, message) -> code.equals("404") && StringUtils.containsIgnoreCase(message, "Bundle Not Found"),
(msg) -> new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.bundle.not_found_by_id")));
put((code, message) -> code.equals("404") && StringUtils.containsIgnoreCase(message, "Screenshot Not Found"),
(msg) -> new ExitCodeExceptionMapper.NotFoundException(RESOURCE_BUNDLE.getString("error.screenshot.not_found_by_id")));
put((code, message) -> code.equals("429"),
(msg) -> new ExitCodeExceptionMapper.RateLimitException(RESOURCE_BUNDLE.getString("error.response.429")));
put((code, message) -> StringUtils.containsAny(message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public List<Screenshot> listScreenshots(Long stringId) {
.listScreenshots(parseLong(this.projectId), stringId, limit, offset));
}

@Override
public Screenshot getScreenshot(Long id) {
return executeRequest(() -> this.client.getScreenshotsApi()
.getScreenshot(parseLong(this.projectId), id)
.getData());
}

@Override
public Screenshot uploadScreenshot(AddScreenshotRequest request) throws ResponseException {
Map<BiPredicate<String, String>, ResponseException> errorHandler = new LinkedHashMap<BiPredicate<String, String>, ResponseException>() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;

Expand All @@ -18,7 +19,10 @@ public ScreenshotDeleteAction(Long id) {

@Override
public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) {
client.deleteScreenshot(id);
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.deleted"), id)));
Screenshot screenshot = client.getScreenshot(id);
if (screenshot != null) {
client.deleteScreenshot(id);
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.deleted"), id, screenshot.getName())));
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ error.screenshot.auto-tag_required_for_directory='--auto-tag' is required for '-
error.screenshot.only_one_allowed=Only one of the following options can be used at a time: '--file', '--branch' or '--directory'
error.screenshot.not_uploaded=Screenshot was not uploaded
error.screenshot.not_updated=Screenshot was not updated
error.screenshot.not_found_by_id=Couldn't find screenshot by the specified ID

error.label.not_found=Couldn't find label by the specified title

Expand Down Expand Up @@ -711,7 +712,7 @@ message.tm.list=@|yellow #%d|@ %s (@|green segments: %d|@)
message.tm.import_success=Imported in @|yellow #%s|@ @|green '%s'|@ translation memory
message.tm.list_empty=No translation memories found

message.screenshot.deleted=@|green Screenshot '%s' deleted successfully|@
message.screenshot.deleted=@|green Screenshot '#%d %s' deleted successfully|@
message.screenshot.not_auto-tagged=Tags were not applied for %s because auto tag is currently in progress
message.screenshot.list_empty=No screenshot found
message.screenshot.list=@|yellow #%d|@ %d @|green %s|@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CrowdinClientScreenshotTest {
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 getScreenshotUrl = String.format("%s/projects/%d/screenshots/%d", url, projectId, screenshotId);
private static final String deleteScreenshotUrl = String.format("%s/projects/%d/screenshots/%d", url, projectId, screenshotId);
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);
Expand Down Expand Up @@ -56,6 +57,20 @@ public void testListScreenshots() {
verifyNoMoreInteractions(httpClientMock);
}

@Test
public void testGetScreenshot() {
ScreenshotResponseObject response = new ScreenshotResponseObject() {{
setData(new Screenshot());
}};
when(httpClientMock.get(eq(getScreenshotUrl), any(), eq(ScreenshotResponseObject.class)))
.thenReturn(response);

client.getScreenshot(screenshotId);

verify(httpClientMock).get(eq(getScreenshotUrl), any(), eq(ScreenshotResponseObject.class));
verifyNoMoreInteractions(httpClientMock);
}

@Test
public void testDeleteScreenshot() {
client.deleteScreenshot(screenshotId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 java.util.ArrayList;
Expand All @@ -27,12 +28,16 @@ public void testScreenshotDelete() {
PropertiesWithFiles pb = pbBuilder.build();

ClientScreenshot client = mock(ClientScreenshot.class);
Screenshot screenshot = mock(Screenshot.class);

when(screenshot.getName()).thenReturn("sample.gif");
when(client.getScreenshot(SCREENSHOT_ID)).thenReturn(screenshot);
doNothing().when(client).deleteScreenshot(SCREENSHOT_ID);

action = new ScreenshotDeleteAction(SCREENSHOT_ID);
action.act(Outputter.getDefault(), pb, client);

verify(client).getScreenshot(SCREENSHOT_ID);
verify(client).deleteScreenshot(SCREENSHOT_ID);
verifyNoMoreInteractions(client);
}
Expand All @@ -48,13 +53,13 @@ public void testScreenshotDelete_throwsNotFound() {

when(client.listScreenshots(null))
.thenReturn(new ArrayList<>());
doThrow(new RuntimeException("Not found")).when(client).deleteScreenshot(SCREENSHOT_ID);
doThrow(new RuntimeException("Not found")).when(client).getScreenshot(SCREENSHOT_ID);

action = new ScreenshotDeleteAction(SCREENSHOT_ID);

assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client));

verify(client).deleteScreenshot(SCREENSHOT_ID);
verify(client).getScreenshot(SCREENSHOT_ID);
verifyNoMoreInteractions(client);
}
}
Loading