Skip to content

Commit

Permalink
Merge pull request #107 from picimako/cache-provider-test
Browse files Browse the repository at this point in the history
Unit tests for CrowdinProjectCacheProvider.getInstance()
  • Loading branch information
andrii-bodnar authored May 16, 2023
2 parents a5cc371 + c5dba13 commit 9b2b822
Show file tree
Hide file tree
Showing 17 changed files with 594 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/action/DownloadAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void performInBackground(AnActionEvent anActionEvent, ProgressIndicator i
NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

BranchLogic branchLogic = new BranchLogic(crowdin, project, properties);
String branchName = branchLogic.acquireBranchName(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void performInBackground(@NonNull AnActionEvent anActionEvent, @NonNul
NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
BranchLogic branchLogic = new BranchLogic(crowdin, project, properties);
String branchName = branchLogic.acquireBranchName(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected void performInBackground(@NonNull AnActionEvent anActionEvent, @NonNul
NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

BranchLogic branchLogic = new BranchLogic(crowdin, project, properties);
String branchName = branchLogic.acquireBranchName(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void performInBackground(@NonNull AnActionEvent anActionEvent, @NonNul
NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

BranchLogic branchLogic = new BranchLogic(crowdin, project, properties);
String branchName = branchLogic.acquireBranchName(true);
Expand Down Expand Up @@ -107,7 +107,7 @@ public void update(AnActionEvent e) {
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

VirtualFile root = FileUtil.getProjectBaseDir(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

String branchName = ActionUtils.getBranchName(project, properties, false);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/action/UploadAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void performInBackground(@NotNull final AnActionEvent anActionEvent, Prog
VirtualFile root = FileUtil.getProjectBaseDir(project);

CrowdinProperties properties = CrowdinPropertiesLoader.load(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.crowdin.Constants.MESSAGES_BUNDLE;

Expand All @@ -45,7 +44,7 @@ public void performInBackground(AnActionEvent anActionEvent, ProgressIndicator i
NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
BranchLogic branchLogic = new BranchLogic(crowdin, project, properties);
String branchName = branchLogic.acquireBranchName(true);
indicator.checkCanceled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void performInBackground(@NotNull AnActionEvent e, ProgressIndicator indi
NotificationUtil.setLogDebugLevel(properties.isDebug());
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
indicator.checkCanceled();

BranchLogic branchLogic = new BranchLogic(crowdin, project, properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.crowdin.Constants.MESSAGES_BUNDLE;
Expand All @@ -49,7 +47,7 @@ public void performInBackground(AnActionEvent anActionEvent, ProgressIndicator i

VirtualFile root = FileUtil.getProjectBaseDir(project);
CrowdinProperties properties = CrowdinPropertiesLoader.load(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

BranchLogic branchLogic = new BranchLogic(crowdin, project, properties);
String branchName = branchLogic.acquireBranchName(true);
Expand Down Expand Up @@ -135,7 +133,7 @@ public void update(AnActionEvent e) {
NotificationUtil.logDebugMessage(project, MESSAGES_BUNDLE.getString("messages.debug.started_action"));

VirtualFile root = FileUtil.getProjectBaseDir(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

String branchName = ActionUtils.getBranchName(project, properties, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.crowdin.client.CrowdinPropertiesLoader;
import com.crowdin.event.FileChangeListener;
import com.crowdin.util.ActionUtils;
import com.crowdin.util.GitUtil;
import com.crowdin.util.NotificationUtil;
import com.crowdin.util.PropertyUtil;
import com.intellij.openapi.progress.ProgressIndicator;
Expand All @@ -28,7 +27,7 @@ public void runActivity(@NotNull Project project) {
}
//config validation
properties = CrowdinPropertiesLoader.load(project);
Crowdin crowdin = new Crowdin(project, properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());
Crowdin crowdin = new Crowdin(properties.getProjectId(), properties.getApiToken(), properties.getBaseUrl());

String branchName = ActionUtils.getBranchName(project, properties, false);

Expand Down
32 changes: 26 additions & 6 deletions src/main/java/com/crowdin/client/Crowdin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.crowdin.client.translationstatus.model.LanguageProgress;
import com.crowdin.util.RetryUtil;
import com.crowdin.util.Util;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand All @@ -34,16 +33,13 @@

import static com.crowdin.Constants.MESSAGES_BUNDLE;

public class Crowdin {
public class Crowdin implements CrowdinClient {

private final Long projectId;

private final Project project;

private final com.crowdin.client.Client client;

public Crowdin(@NotNull Project project, @NotNull Long projectId, @NotNull String apiToken, String baseUrl) {
this.project = project;
public Crowdin(@NotNull Long projectId, @NotNull String apiToken, String baseUrl) {
this.projectId = projectId;
Credentials credentials = new Credentials(apiToken, null, baseUrl);
ClientConfig clientConfig = ClientConfig.builder()
Expand All @@ -52,79 +48,93 @@ public Crowdin(@NotNull Project project, @NotNull Long projectId, @NotNull Strin
this.client = new Client(credentials, clientConfig);
}

@Override
public Long addStorage(String fileName, InputStream content) {
return executeRequest(() -> this.client.getStorageApi()
.addStorage(fileName, content)
.getData()
.getId());
}

@Override
public void updateSource(Long sourceId, UpdateFileRequest request) {
executeRequest(() -> this.client.getSourceFilesApi()
.updateOrRestoreFile(this.projectId, sourceId, request));
}

@Override
public URL downloadFile(Long fileId) {
return url(executeRequest(() -> this.client.getSourceFilesApi()
.downloadFile(this.projectId, fileId)
.getData()));
}

@Override
public void addSource(AddFileRequest request) {
executeRequest(() -> this.client.getSourceFilesApi()
.addFile(this.projectId, request));
}

@Override
public void editSource(Long fileId, List<PatchRequest> request) {
executeRequest(() -> this.client.getSourceFilesApi()
.editFile(this.projectId, fileId, request));
}

@Override
public void uploadTranslation(String languageId, UploadTranslationsRequest request) {
executeRequest(() -> this.client.getTranslationsApi()
.uploadTranslations(this.projectId, languageId, request));
}

@Override
public Directory addDirectory(AddDirectoryRequest request) {
return executeRequest(() -> this.client.getSourceFilesApi()
.addDirectory(this.projectId, request)
.getData());
}

@Override
public com.crowdin.client.projectsgroups.model.Project getProject() {
return executeRequest(() -> this.client.getProjectsGroupsApi()
.getProject(this.projectId)
.getData());
}

@Override
public List<Language> extractProjectLanguages(com.crowdin.client.projectsgroups.model.Project crowdinProject) {
return crowdinProject.getTargetLanguages();
}

@Override
public ProjectBuild startBuildingTranslation(BuildProjectTranslationRequest request) {
return executeRequest(() -> this.client.getTranslationsApi()
.buildProjectTranslation(this.projectId, request)
.getData());
}

@Override
public ProjectBuild checkBuildingStatus(Long buildId) {
return executeRequest(() -> this.client.getTranslationsApi()
.checkBuildStatus(projectId, buildId)
.getData());
}

@Override
public URL downloadProjectTranslations(Long buildId) {
return url(executeRequest(() -> this.client.getTranslationsApi()
.downloadProjectTranslations(this.projectId, buildId)
.getData()));
}

@Override
public URL downloadFileTranslation(Long fileId, BuildProjectFileTranslationRequest request) {
return url(executeRequest(() -> client.getTranslationsApi()
.buildProjectFileTranslation(this.projectId, fileId, null, request)
.getData()));
}

@Override
public List<Language> getSupportedLanguages() {
return executeRequest(() -> client.getLanguagesApi().listSupportedLanguages(500, 0)
.getData()
Expand All @@ -133,6 +143,7 @@ public List<Language> getSupportedLanguages() {
.collect(Collectors.toList()));
}

@Override
public Map<Long, Directory> getDirectories(Long branchId) {
return executeRequestFullList((limit, offset) ->
this.client.getSourceFilesApi()
Expand All @@ -145,6 +156,7 @@ public Map<Long, Directory> getDirectories(Long branchId) {
.collect(Collectors.toMap(Directory::getId, Function.identity()));
}

@Override
public List<com.crowdin.client.sourcefiles.model.FileInfo> getFiles(Long branchId) {
return executeRequestFullList((limit, offset) ->
this.client.getSourceFilesApi()
Expand All @@ -157,6 +169,7 @@ public List<com.crowdin.client.sourcefiles.model.FileInfo> getFiles(Long branchI
.collect(Collectors.toList());
}

@Override
public List<SourceString> getStrings() {
return executeRequestFullList((limit, offset) ->
this.client.getSourceStringsApi().listSourceStrings(
Expand Down Expand Up @@ -192,6 +205,7 @@ private <T> List<T> executeRequestFullList(BiFunction<Integer, Integer, List<T>>
return models;
}

@Override
public Branch addBranch(AddBranchRequest request) {
try {
return executeRequest(() -> this.client.getSourceFilesApi()
Expand All @@ -206,6 +220,7 @@ public Branch addBranch(AddBranchRequest request) {
}
}

@Override
public Optional<Branch> getBranch(String name) {
List<ResponseObject<Branch>> branches = executeRequest(() -> this.client.getSourceFilesApi().listBranches(this.projectId, name, 500, null).getData());
return branches.stream()
Expand All @@ -214,6 +229,7 @@ public Optional<Branch> getBranch(String name) {
.findFirst();
}

@Override
public Map<String, Branch> getBranches() {
return executeRequestFullList((limit, offset) ->
this.client.getSourceFilesApi()
Expand All @@ -225,6 +241,7 @@ public Map<String, Branch> getBranches() {
.collect(Collectors.toMap(Branch::getName, Function.identity()));
}

@Override
public List<LanguageProgress> getProjectProgress() {
return executeRequestFullList((limit, offset) -> this.client.getTranslationStatusApi()
.getProjectProgress(this.projectId, limit, offset, null)
Expand All @@ -234,6 +251,7 @@ public List<LanguageProgress> getProjectProgress() {
.collect(Collectors.toList()));
}

@Override
public List<FileProgress> getLanguageProgress(String languageId) {
return executeRequestFullList((limit, offset) -> this.client.getTranslationStatusApi()
.getLanguageProgress(this.projectId, languageId, limit, offset)
Expand All @@ -243,6 +261,7 @@ public List<FileProgress> getLanguageProgress(String languageId) {
.collect(Collectors.toList()));
}

@Override
public List<Label> listLabels() {
return executeRequestFullList((limit, offset) -> this.client.getLabelsApi()
.listLabels(this.projectId, limit, offset)
Expand All @@ -252,6 +271,7 @@ public List<Label> listLabels() {
.collect(Collectors.toList()));
}

@Override
public Label addLabel(AddLabelRequest request) {
return executeRequest(() ->this.client.getLabelsApi()
.addLabel(this.projectId, request)
Expand Down
Loading

0 comments on commit 9b2b822

Please sign in to comment.