Skip to content

Commit ba83b88

Browse files
authored
fest: increase code coverage for DownloadSourcesAction (#910)
1 parent cb92c22 commit ba83b88

File tree

1 file changed

+109
-14
lines changed

1 file changed

+109
-14
lines changed

src/test/java/com/crowdin/cli/commands/actions/DownloadSourcesActionTest.java

+109-14
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
package com.crowdin.cli.commands.actions;
22

33
import com.crowdin.cli.MockitoUtils;
4-
import com.crowdin.cli.client.CrowdinProject;
4+
import com.crowdin.cli.client.CrowdinProjectFull;
55
import com.crowdin.cli.client.ProjectBuilder;
66
import com.crowdin.cli.client.ProjectClient;
77
import com.crowdin.cli.commands.NewAction;
88
import com.crowdin.cli.commands.Outputter;
99
import com.crowdin.cli.commands.functionality.FilesInterface;
10+
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
1011
import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder;
1112
import com.crowdin.cli.properties.PropertiesWithFiles;
1213
import com.crowdin.cli.properties.helper.TempProject;
1314
import com.crowdin.cli.utils.Utils;
14-
import com.crowdin.cli.utils.console.ExecutionStatus;
15-
import org.apache.commons.lang3.StringUtils;
15+
import com.crowdin.client.projectsgroups.model.Type;
16+
import com.crowdin.client.sourcefiles.model.BuildReviewedSourceFilesRequest;
17+
import com.crowdin.client.sourcefiles.model.ReviewedStringsBuild;
1618
import org.hamcrest.Matchers;
1719
import org.junit.jupiter.api.AfterEach;
1820
import org.junit.jupiter.api.BeforeEach;
1921
import org.junit.jupiter.api.Test;
20-
import org.junit.jupiter.api.Assertions;
2122
import org.junit.jupiter.api.condition.DisabledOnOs;
2223
import org.junit.jupiter.api.condition.OS;
23-
import org.mockito.Mockito;
2424

2525
import java.io.ByteArrayOutputStream;
2626
import java.io.File;
2727
import java.io.IOException;
2828
import java.io.PrintStream;
29+
import java.net.MalformedURLException;
2930
import java.net.URL;
3031
import java.util.ArrayList;
3132

3233
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;
3334
import static com.crowdin.cli.utils.console.ExecutionStatus.WARNING;
3435
import static org.hamcrest.MatcherAssert.assertThat;
35-
import static org.junit.jupiter.api.Assertions.*;
36+
import static org.junit.jupiter.api.Assertions.assertEquals;
37+
import static org.junit.jupiter.api.Assertions.assertThrows;
3638
import static org.mockito.ArgumentMatchers.any;
3739
import static org.mockito.ArgumentMatchers.eq;
3840
import static org.mockito.Mockito.*;
@@ -52,19 +54,18 @@ public void deleteProj() {
5254
}
5355

5456
@Test
55-
public void testDest() throws IOException {
57+
public void testDownloadSourcesAction() throws IOException {
5658
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
5759
.minimalBuiltPropertiesBean(
58-
"/values/strings.xml", "/values-%two_letters_code%/%original_file_name%",
59-
null, "/common/%original_file_name%")
60+
"/strings.xml", "/values-%two_letters_code%/%original_file_name%",
61+
null, null)
6062
.setBasePath(project.getBasePath())
6163
.build();
6264

6365
ProjectClient client = mock(ProjectClient.class);
6466
when(client.downloadFullProject(null))
6567
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
66-
.addDirectory("common", 201L, null, null)
67-
.addFile("strings.xml", "gettext", 101L, 201L, null, "/values-%two_letters_code%/%original_file_name%").build());
68+
.addFile("strings.xml", "gettext", 101L, null, null, "/values-%two_letters_code%/%original_file_name%").build());
6869
URL urlMock = MockitoUtils.getMockUrl(getClass());
6970
when(client.downloadFile(eq(101L)))
7071
.thenReturn(urlMock);
@@ -79,10 +80,60 @@ public void testDest() throws IOException {
7980
verify(client).downloadFile(eq(101L));
8081
verifyNoMoreInteractions(client);
8182

82-
verify(files).writeToFile(eq(Utils.joinPaths(project.getBasePath(), "values/strings.xml")), any());
83+
verify(files).writeToFile(eq(Utils.joinPaths(project.getBasePath(), "strings.xml")), any());
8384
verifyNoMoreInteractions(files);
8485
}
8586

87+
@Test
88+
public void testDownloadSourcesAction_WithDestination() {
89+
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
90+
.minimalBuiltPropertiesBean(
91+
"/values/strings.xml", "/values-%two_letters_code%/%original_file_name%",
92+
null, "/common/%original_file_name%")
93+
.setBasePath(project.getBasePath())
94+
.build();
95+
96+
ProjectClient client = mock(ProjectClient.class);
97+
when(client.downloadFullProject(null))
98+
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build());
99+
URL urlMock = MockitoUtils.getMockUrl(getClass());
100+
when(client.downloadFile(eq(101L)))
101+
.thenReturn(urlMock);
102+
103+
FilesInterface files = mock(FilesInterface.class);
104+
105+
NewAction<PropertiesWithFiles, ProjectClient> action =
106+
new DownloadSourcesAction(files, false, false, null, true, false, false);
107+
action.act(Outputter.getDefault(), pb, client);
108+
109+
verify(client).downloadFullProject(null);
110+
verifyNoMoreInteractions(client);
111+
}
112+
113+
@Test
114+
public void testDownloadSourcesAction_StringBased_ThrowsValidationException() {
115+
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
116+
.minimalBuiltPropertiesBean(
117+
"/values/strings.xml", "/values-%two_letters_code%/%original_file_name%",
118+
null, "/common/%original_file_name%")
119+
.setBasePath(project.getBasePath())
120+
.build();
121+
122+
ProjectClient client = mock(ProjectClient.class);
123+
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
124+
.build();
125+
build.setType(Type.STRINGS_BASED);
126+
when(client.downloadFullProject(null))
127+
.thenReturn(build);
128+
129+
NewAction<PropertiesWithFiles, ProjectClient> action =
130+
new DownloadSourcesAction(null, false, false, null, true, false, false);
131+
assertThrows(ExitCodeExceptionMapper.ValidationException.class, () -> action.act(Outputter.getDefault(), pb, client));
132+
133+
verify(client).downloadFullProject(null);
134+
verifyNoMoreInteractions(client);
135+
}
136+
86137
@Test
87138
public void testDest_doubleAsterisks() throws IOException {
88139
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
@@ -140,7 +191,7 @@ public void testDestAndUnaryAsterisk() throws IOException {
140191
FilesInterface files = mock(FilesInterface.class);
141192

142193
NewAction<PropertiesWithFiles, ProjectClient> action =
143-
new DownloadSourcesAction(files, false, false, null, true, false, false);
194+
new DownloadSourcesAction(files, false, true, null, true, false, false);
144195
action.act(Outputter.getDefault(), pb, client);
145196

146197
verify(client).downloadFullProject(null);
@@ -348,7 +399,7 @@ public void testDryRun() {
348399
}
349400

350401
@Test
351-
public void testReviewedOnly() {
402+
public void testReviewedOnly_NotEnterprise() {
352403
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
353404
PrintStream ps = System.out;
354405
System.setOut(new PrintStream(outContent));
@@ -381,4 +432,48 @@ public void testReviewedOnly() {
381432
client.downloadFullProject(null);
382433
assertEquals(warnMessage, outContent.toString());
383434
}
435+
436+
@Test
437+
public void testDownloadReviewedOnly() throws MalformedURLException {
438+
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
439+
System.setOut(new PrintStream(outContent));
440+
ReviewedStringsBuild build = new ReviewedStringsBuild() {{
441+
setId(10L);
442+
setProgress(40L);
443+
setStatus("in_progress");
444+
}};
445+
ReviewedStringsBuild finishedBuild = new ReviewedStringsBuild() {{
446+
setId(10L);
447+
setStatus("finished");
448+
}};
449+
450+
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
451+
.minimalBuiltPropertiesBean(
452+
Utils.toUnixPath("/values/strings.xml"), Utils.toUnixPath("/values-%two_letters_code%/%original_file_name%"),
453+
null, Utils.toUnixPath("/common/%original_file_name%"))
454+
.setBasePath(project.getBasePath())
455+
.build();
456+
pb.setBaseUrl("https://companyname.crowdin.com");
457+
458+
FilesInterface files = mock(FilesInterface.class);
459+
ProjectClient client = mock(ProjectClient.class);
460+
461+
when(client.downloadFullProject(null))
462+
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
463+
.addDirectory("common", 201L, null, null)
464+
.addFile("strings.xml", "gettext", 101L, 201L, null, Utils.toUnixPath("/values-%two_letters_code%/%original_file_name%")).build());
465+
when(client.startBuildingReviewedSources(eq(new BuildReviewedSourceFilesRequest()))).thenReturn(build);
466+
when(client.checkBuildingReviewedSources(10L)).thenReturn(finishedBuild);
467+
when(client.downloadReviewedSourcesBuild(10L)).thenReturn(new URL("https://companyname.crowdin.com"));
468+
469+
NewAction<PropertiesWithFiles, ProjectClient> action =
470+
new DownloadSourcesAction(files, false, false, null, true, true, false);
471+
action.act(Outputter.getDefault(), pb, client);
472+
473+
verify(client).downloadFullProject(null);
474+
verify(client).startBuildingReviewedSources(any());
475+
verify(client).checkBuildingReviewedSources(any());
476+
verify(client).downloadReviewedSourcesBuild(any());
477+
verifyNoMoreInteractions(client);
478+
}
384479
}

0 commit comments

Comments
 (0)