Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 committed Jan 22, 2024
1 parent 362cf2b commit b9afcef
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
}

String fileFullPath = file.getPath();
String fileDestName = file.getName();
if (Objects.equals(Type.FILES_BASED, project.getType())) {
String commonPath = SourcesUtils.getCommonPath(Collections.singletonList(this.file.getAbsolutePath()), properties.getBasePath());
final String filePath = (nonNull(dest))
? PropertiesBeanUtils.prepareDest(dest, StringUtils.removeStart(file.getAbsolutePath(), properties.getBasePath()), placeholderUtil)
: StringUtils.removeStart(file.getAbsolutePath(), properties.getBasePath() + commonPath);
fileFullPath = (nonNull(branchName) ? branchName + Utils.PATH_SEPARATOR : "") + filePath;
fileDestName = fileFullPath.substring(fileFullPath.lastIndexOf(Utils.PATH_SEPARATOR) + 1);
Map<String, FileInfo> paths = ProjectFilesUtils.buildFilePaths(project.getDirectories(), project.getBranches(), project.getFileInfos());
FileInfo projectFile = paths.get(fileFullPath);

Expand All @@ -73,7 +75,7 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
final Long sourceId = projectFile.getId();
attachLabelIds.ifPresent(request::setAttachLabelIds);

Long storageId = getStorageId(client);
Long storageId = getStorageId(client, fileDestName);
request.setStorageId(storageId);

try {
Expand Down Expand Up @@ -112,7 +114,7 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.branch_required_string_project"));

Check warning on line 114 in src/main/java/com/crowdin/cli/commands/actions/FileUploadAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/FileUploadAction.java#L114

Added line #L114 was not covered by tests
}

Long storageId = getStorageId(client);
Long storageId = getStorageId(client, fileDestName);

Optional<List<String>> excludedLanguageNames = Optional.empty();
if (nonNull(excludedLanguages) && !excludedLanguages.isEmpty()) {
Expand All @@ -121,7 +123,7 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien

if (Objects.equals(Type.FILES_BASED, project.getType())) {
AddFileRequest request = new AddFileRequest();
request.setName(file.getName());
request.setName(fileDestName);
request.setStorageId(storageId);

Optional<Long> directoryId = getOrCreateDirectoryId(out, client, project, properties, branch.orElse(null));
Expand Down Expand Up @@ -183,7 +185,7 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
private List<String> filterExcludedLanguages(List<String> excludedLanguages, CrowdinProjectFull project) {
List<String> projectLanguageNames = project.getProjectLanguages(false)
.stream()
.map(Language::getName)
.map(Language::getId)
.collect(Collectors.toList());

return excludedLanguages
Expand Down Expand Up @@ -221,9 +223,9 @@ private Optional<Long> getOrCreateDirectoryId(Outputter out, ProjectClient clien
return directoryId;
}

private Long getStorageId(ProjectClient client) {
private Long getStorageId(ProjectClient client, String fileName) {
try (InputStream fileStream = Files.newInputStream(file.toPath())) {
return client.uploadStorage(file.getName(), fileStream);
return client.uploadStorage(fileName, fileStream);
} catch (FileNotFoundException e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.local_file_not_found"), file.getAbsolutePath()));
} catch (EmptyFileException e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,24 @@ public void testDelete() {
verify(client).deleteSource(eq(101L));
verifyNoMoreInteractions(client);
}

@Test
public void testDelete_StringBasedProject() {
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
.addFile("/first.po", "gettext", 101L, null, null, null).build();
build.setType(Type.STRINGS_BASED);
when(client.downloadFullProject())
.thenReturn(build);

NewAction<ProjectProperties, ProjectClient> action = new FileDeleteAction("first.po", null);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
verifyNoMoreInteractions(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import org.junit.jupiter.api.Test;

import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -57,5 +60,25 @@ public void testDownload() {
verify(client).downloadFullProject(any());
verify(client).downloadFile(eq(101L));
verifyNoMoreInteractions(client);
assertTrue(Files.exists(Paths.get("dest" + Utils.PATH_SEPARATOR + "first.po")), "File should exist at the specified path");
}

@Test
public void testDownload_StringBasedProject() {
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build();
build.setType(Type.STRINGS_BASED);
when(client.downloadFullProject(any()))
.thenReturn(build);

NewAction<ProjectProperties, ProjectClient> action = new FileDownloadAction("first.po", "main", "dest");
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject(any());
verifyNoMoreInteractions(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import org.junit.jupiter.api.Test;

import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -58,9 +61,9 @@ public void testDownloadTranslation() {
NewAction<ProjectProperties, ProjectClient> action = new FileDownloadTranslationAction("first.po", "ua", null, null);
action.act(Outputter.getDefault(), pb, client);


verify(client).downloadFullProject();
verify(client).buildProjectFileTranslation(eq(101L), eq(request));
verifyNoMoreInteractions(client);
assertTrue(Files.exists(Paths.get("ua" + Utils.PATH_SEPARATOR + "first.po")), "File should exist at the specified path");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@
import com.crowdin.cli.properties.helper.FileHelperTest;
import com.crowdin.cli.properties.helper.TempProject;
import com.crowdin.cli.utils.Utils;
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.projectsgroups.model.Type;
import com.crowdin.client.sourcefiles.model.*;
import com.crowdin.client.sourcefiles.model.AddFileRequest;
import com.crowdin.client.sourcefiles.model.Branch;
import com.crowdin.client.sourcefiles.model.Directory;
import com.crowdin.client.sourcefiles.model.UpdateFileRequest;
import com.crowdin.client.sourcestrings.model.UploadStringsProgress;
import com.crowdin.client.sourcestrings.model.UploadStringsRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -142,4 +148,212 @@ public void testUploadUpdate_FileBasedProject() throws ResponseException {
verify(client).updateSource(any(), eq(addFileRequest));
verifyNoMoreInteractions(client);
}

@Test
public void testUploadNoAutoUpdate_FileBasedProject() throws ResponseException {
File fileToUpload = new File(project.getBasePath() + "first.po");
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
.addFile("first.po", "gettext", 101L, null, null).build();
build.setType(Type.FILES_BASED);
when(client.downloadFullProject())
.thenReturn(build);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
verifyNoMoreInteractions(client);
}

@Test
public void testUploadWhenBranchMissed_FileBasedProject() throws ResponseException {
File fileToUpload = new File(project.getBasePath() + "first.po");
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
Branch branch = new Branch() {{
setId(3L);
setName("main");
}};
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build();
build.setType(Type.FILES_BASED);
when(client.downloadFullProject())
.thenReturn(build);
when(client.uploadStorage(eq("first.po"), any()))
.thenReturn(1L);
when(client.addBranch(any())).thenReturn(branch);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "main", false, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
verify(client).uploadStorage(eq("first.po"), any());
verify(client).addBranch(any());
AddFileRequest addFileRequest = new AddFileRequest() {{
setName("first.po");
setStorageId(1L);
setBranchId(3L);
}};
verify(client).addSource(eq(addFileRequest));
verifyNoMoreInteractions(client);
}

@Test
public void testUploadLabel_FileBasedProject() throws ResponseException {
File fileToUpload = new File(project.getBasePath() + "first.po");
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
Label label = new Label() {{
setId(3L);
setTitle("main_label");
}};
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build();
build.setType(Type.FILES_BASED);
when(client.downloadFullProject())
.thenReturn(build);
when(client.uploadStorage(eq("first.po"), any()))
.thenReturn(1L);
when(client.listLabels()).thenReturn(Collections.emptyList());
when(client.addLabel(any())).thenReturn(label);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, Collections.singletonList("main_label"), null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
verify(client).uploadStorage(eq("first.po"), any());
verify(client).listLabels();
verify(client).addLabel(any());
AddFileRequest addFileRequest = new AddFileRequest() {{
setName("first.po");
setStorageId(1L);
setAttachLabelIds(Collections.singletonList(3L));
}};
verify(client).addSource(eq(addFileRequest));
verifyNoMoreInteractions(client);
}

@Test
public void testUploadExcludedLangs_FileBasedProject() throws ResponseException {
File fileToUpload = new File(project.getBasePath() + "first.po");
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build();
build.setType(Type.FILES_BASED);
when(client.downloadFullProject())
.thenReturn(build);
when(client.uploadStorage(eq("first.po"), any()))
.thenReturn(1L);
when(client.listLabels()).thenReturn(Collections.emptyList());

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, false, false, Arrays.asList("ua", "fr"), false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
verify(client).uploadStorage(eq("first.po"), any());
AddFileRequest addFileRequest = new AddFileRequest() {{
setName("first.po");
setStorageId(1L);
setExcludedTargetLanguages(Collections.singletonList("ua"));
}};
verify(client).addSource(eq(addFileRequest));
verifyNoMoreInteractions(client);
}

@Test
public void testUploadWithDest_FileBasedProject() throws ResponseException {
File fileToUpload = new File(project.getBasePath() + "first.po");
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build();
build.setType(Type.FILES_BASED);
Directory directory = new Directory(){{
setId(2L);
}};
when(client.downloadFullProject())
.thenReturn(build);
when(client.addDirectory(any()))
.thenReturn(directory);
when(client.uploadStorage(eq("save.po"), any()))
.thenReturn(1L);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, "path/to/save.po", false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
verify(client, times(2)).addDirectory(any());
verify(client).uploadStorage(eq("save.po"), any());
AddFileRequest addFileRequest = new AddFileRequest() {{
setName("save.po");
setStorageId(1L);
setDirectoryId(2L);
}};
verify(client).addSource(eq(addFileRequest));
verifyNoMoreInteractions(client);
}

@Test
public void testUploadCleanUpModeAndUpdateStrings_StringBasedProject() throws ResponseException {
File fileToUpload = new File(project.getBasePath() + "first.po");
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
Branch branch = mock(Branch.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build();
build.setType(Type.STRINGS_BASED);
UploadStringsProgress progress = new UploadStringsProgress() {{
setIdentifier("id");
setProgress(100);
}};
UploadStringsProgress progressFinished = new UploadStringsProgress() {{
setIdentifier("id");
setStatus("finished");
setProgress(100);
}};

when(branch.getId()).thenReturn(2L);
when(client.downloadFullProject()).thenReturn(build);
when(client.uploadStorage(eq("first.po"), any())).thenReturn(1L);
when(client.addBranch(any())).thenReturn(branch);
when(client.addSourceStringsBased(any())).thenReturn(progress);
when(client.getUploadStringsStatus(any())).thenReturn(progressFinished);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, true, true, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
verify(client).uploadStorage(eq("first.po"), any());
verify(client).addBranch(any());
verify(client).getUploadStringsStatus(any());
UploadStringsRequest addFileRequest = new UploadStringsRequest() {{
setBranchId(2L);
setStorageId(1L);
setCleanupMode(true);
setUpdateStrings(true);
}};
verify(client).addSourceStringsBased(eq(addFileRequest));
verifyNoMoreInteractions(client);
}
}

0 comments on commit b9afcef

Please sign in to comment.