Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Nov 15, 2024
1 parent 85b0fc0 commit 84b4967
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import com.crowdin.client.stringcomments.model.StringComment;
import com.crowdin.client.translations.model.*;
import com.crowdin.client.translationstatus.model.LanguageProgress;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.URL;
Expand Down Expand Up @@ -551,4 +554,17 @@ public void installApplication(String url) {
req.setUrl(url);
executeRequest(() -> this.client.getApplicationsApi().installApplication(req));
}

@Override
@SneakyThrows
public Optional<String> findManifestUrl(String id) {
var url = new URL("https://developer.app.crowdin.net/items/Item?filter={\"slug\":{\"_eq\":\"" + id + "\"}}&fields=manifest");
var res = new String(url.openStream().readAllBytes());
JSONObject json = new JSONObject(res);
var apps = (JSONArray) json.get("data");
if (apps.isEmpty()) {
return Optional.empty();
}
return Optional.ofNullable(JSONObject.class.cast(apps.get(0)).get("manifest").toString());
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Optional;

public interface ProjectClient extends Client {

Expand Down Expand Up @@ -137,4 +138,6 @@ default CrowdinProjectFull downloadFullProject() {
void uninstallApplication(String id, boolean force);

void installApplication(String url);

Optional<String> findManifestUrl(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ExecutionStatus;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,12 +17,11 @@ class AppInstallAction implements NewAction<ProjectProperties, ProjectClient> {

@Override
public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
client.installApplication(this.findManifestUrl(id));
var manifestUrl = client.findManifestUrl(id);
if (manifestUrl.isEmpty()) {
throw new ExitCodeExceptionMapper.NotFoundException(String.format(RESOURCE_BUNDLE.getString("error.application_not_found"), this.id));
}
client.installApplication(manifestUrl.get());
out.println(ExecutionStatus.OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.application.install"), id)));
}

private String findManifestUrl(String id) {
//TODO fix me
return id;
}
}
1 change: 1 addition & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ error.file_not_exists=Project doesn't contain the '%s' file
error.file_required=The '--file' parameter is required for this type of project
error.dir_not_exists=Project doesn't contain the '%s' directory
error.branch_not_exists=Project doesn't contain the '%s' branch
error.application_not_found=Application with identifier '%s' doesn't exist in Crowdin Store
error.source_string_no_edit=Specify some parameters to edit the string
error.branch_no_edit=Specify some parameters to edit the branch
error.unexpected_response=Unexpected response from %s: %s
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.properties.PropertiesWithFiles;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

public class AppInstallActionTest {

Outputter out = Outputter.getDefault();
PropertiesWithFiles pb;

ProjectClient clientMock = mock(ProjectClient.class);
NewAction<ProjectProperties, ProjectClient> action;

@Test
public void testInstall() {
String id = "test";
String url = "test.com/manifest.json";
when(clientMock.findManifestUrl(id)).thenReturn(Optional.of(url));
doNothing().when(clientMock).installApplication(url);

action = new AppInstallAction(id);
action.act(out, pb, clientMock);

verify(clientMock).findManifestUrl(id);
verify(clientMock).installApplication(url);
verifyNoMoreInteractions(clientMock);
}

@Test
public void testInstallFailed() {
String id = "test";
when(clientMock.findManifestUrl(id)).thenReturn(Optional.empty());

action = new AppInstallAction(id);
assertThrows(ExitCodeExceptionMapper.NotFoundException.class, () -> action.act(out, pb, clientMock));

verify(clientMock).findManifestUrl(id);
verifyNoMoreInteractions(clientMock);
}
}

0 comments on commit 84b4967

Please sign in to comment.