1
1
package com .crowdin .cli .commands .actions ;
2
2
3
3
import com .crowdin .cli .MockitoUtils ;
4
- import com .crowdin .cli .client .CrowdinProject ;
4
+ import com .crowdin .cli .client .CrowdinProjectFull ;
5
5
import com .crowdin .cli .client .ProjectBuilder ;
6
6
import com .crowdin .cli .client .ProjectClient ;
7
7
import com .crowdin .cli .commands .NewAction ;
8
8
import com .crowdin .cli .commands .Outputter ;
9
9
import com .crowdin .cli .commands .functionality .FilesInterface ;
10
+ import com .crowdin .cli .commands .picocli .ExitCodeExceptionMapper ;
10
11
import com .crowdin .cli .properties .NewPropertiesWithFilesUtilBuilder ;
11
12
import com .crowdin .cli .properties .PropertiesWithFiles ;
12
13
import com .crowdin .cli .properties .helper .TempProject ;
13
14
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 ;
16
18
import org .hamcrest .Matchers ;
17
19
import org .junit .jupiter .api .AfterEach ;
18
20
import org .junit .jupiter .api .BeforeEach ;
19
21
import org .junit .jupiter .api .Test ;
20
- import org .junit .jupiter .api .Assertions ;
21
22
import org .junit .jupiter .api .condition .DisabledOnOs ;
22
23
import org .junit .jupiter .api .condition .OS ;
23
- import org .mockito .Mockito ;
24
24
25
25
import java .io .ByteArrayOutputStream ;
26
26
import java .io .File ;
27
27
import java .io .IOException ;
28
28
import java .io .PrintStream ;
29
+ import java .net .MalformedURLException ;
29
30
import java .net .URL ;
30
31
import java .util .ArrayList ;
31
32
32
33
import static com .crowdin .cli .utils .console .ExecutionStatus .OK ;
33
34
import static com .crowdin .cli .utils .console .ExecutionStatus .WARNING ;
34
35
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 ;
36
38
import static org .mockito .ArgumentMatchers .any ;
37
39
import static org .mockito .ArgumentMatchers .eq ;
38
40
import static org .mockito .Mockito .*;
@@ -52,19 +54,18 @@ public void deleteProj() {
52
54
}
53
55
54
56
@ Test
55
- public void testDest () throws IOException {
57
+ public void testDownloadSourcesAction () throws IOException {
56
58
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
57
59
.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 )
60
62
.setBasePath (project .getBasePath ())
61
63
.build ();
62
64
63
65
ProjectClient client = mock (ProjectClient .class );
64
66
when (client .downloadFullProject (null ))
65
67
.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 ());
68
69
URL urlMock = MockitoUtils .getMockUrl (getClass ());
69
70
when (client .downloadFile (eq (101L )))
70
71
.thenReturn (urlMock );
@@ -79,10 +80,60 @@ public void testDest() throws IOException {
79
80
verify (client ).downloadFile (eq (101L ));
80
81
verifyNoMoreInteractions (client );
81
82
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 ());
83
84
verifyNoMoreInteractions (files );
84
85
}
85
86
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
+
86
137
@ Test
87
138
public void testDest_doubleAsterisks () throws IOException {
88
139
PropertiesWithFiles pb = NewPropertiesWithFilesUtilBuilder
@@ -140,7 +191,7 @@ public void testDestAndUnaryAsterisk() throws IOException {
140
191
FilesInterface files = mock (FilesInterface .class );
141
192
142
193
NewAction <PropertiesWithFiles , ProjectClient > action =
143
- new DownloadSourcesAction (files , false , false , null , true , false , false );
194
+ new DownloadSourcesAction (files , false , true , null , true , false , false );
144
195
action .act (Outputter .getDefault (), pb , client );
145
196
146
197
verify (client ).downloadFullProject (null );
@@ -348,7 +399,7 @@ public void testDryRun() {
348
399
}
349
400
350
401
@ Test
351
- public void testReviewedOnly () {
402
+ public void testReviewedOnly_NotEnterprise () {
352
403
ByteArrayOutputStream outContent = new ByteArrayOutputStream ();
353
404
PrintStream ps = System .out ;
354
405
System .setOut (new PrintStream (outContent ));
@@ -381,4 +432,48 @@ public void testReviewedOnly() {
381
432
client .downloadFullProject (null );
382
433
assertEquals (warnMessage , outContent .toString ());
383
434
}
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
+ }
384
479
}
0 commit comments