4747import okhttp3 .ResponseBody ;
4848import okio .Buffer ;
4949import org .datatransferproject .api .launcher .Monitor ;
50- import org .datatransferproject .datatransfer .synology .constant .SynologyConstant ;
5150import org .datatransferproject .datatransfer .synology .exceptions .SynologyImportException ;
5251import org .datatransferproject .datatransfer .synology .exceptions .SynologyMaxRetriesExceededException ;
52+ import org .datatransferproject .datatransfer .synology .utils .TestConfigs ;
5353import org .datatransferproject .spi .cloud .storage .JobStore ;
5454import org .datatransferproject .spi .cloud .storage .TemporaryPerJobDataStore .InputStreamWrapper ;
5555import org .datatransferproject .types .common .DownloadableFile ;
5656import org .datatransferproject .types .common .models .media .MediaAlbum ;
5757import org .datatransferproject .types .common .models .photos .PhotoModel ;
5858import org .datatransferproject .types .common .models .videos .VideoModel ;
59+ import org .datatransferproject .types .transfer .serviceconfig .TransferServiceConfig ;
5960import org .junit .jupiter .api .BeforeEach ;
6061import org .junit .jupiter .api .Nested ;
6162import org .junit .jupiter .api .Test ;
@@ -76,15 +77,22 @@ public class SynologyDTPServiceTest {
7677 private final UUID jobId = UUID .randomUUID ();
7778 protected SynologyDTPService dtpService ;
7879 @ Mock protected Monitor monitor ;
80+ @ Mock protected TransferServiceConfig transferServiceConfig ;
7981 @ Mock protected JobStore jobStore ;
8082 @ Mock protected SynologyOAuthTokenManager tokenManager ;
8183 @ Captor ArgumentCaptor <RequestBody > requestBodyCaptor ;
8284 @ Mock private OkHttpClient client ;
8385
8486 @ BeforeEach
85- public void setUp () throws Exception {
86- dtpService = new SynologyDTPService (monitor , exportingService , jobStore , tokenManager , client );
87+ public void setUp () {
8788 lenient ().when (tokenManager .getAccessToken (jobId )).thenReturn ("mockAccessToken" );
89+ lenient ()
90+ .when (transferServiceConfig .getServiceConfig ())
91+ .thenReturn (TestConfigs .createServiceConfigJson ());
92+
93+ dtpService =
94+ new SynologyDTPService (
95+ monitor , transferServiceConfig , exportingService , jobStore , tokenManager , client );
8896 }
8997
9098 @ Nested
@@ -121,7 +129,9 @@ public void shouldSendPostRequestWithCorrectFormBody() {
121129 public void shouldThrowExceptionIfSendPostRequestFailed () {
122130 SynologyDTPService spyService = Mockito .spy (dtpService );
123131
124- doThrow (new SynologyMaxRetriesExceededException ("MockException" , 3 , new Exception ("Failed" )))
132+ doThrow (
133+ new SynologyMaxRetriesExceededException (
134+ "MockException" , TestConfigs .TEST_MAX_ATTEMPTS , new Exception ("Failed" )))
125135 .when (spyService )
126136 .sendPostRequest (anyString (), any (), any ());
127137
@@ -171,7 +181,9 @@ public void shouldSendPostRequestWithCorrectFormBody() {
171181 public void shouldThrowExceptionIfSendPostRequestFailed () {
172182 SynologyDTPService spyService = Mockito .spy (dtpService );
173183
174- doThrow (new SynologyMaxRetriesExceededException ("MockException" , 3 , new Exception ("Failed" )))
184+ doThrow (
185+ new SynologyMaxRetriesExceededException (
186+ "MockException" , TestConfigs .TEST_MAX_ATTEMPTS , new Exception ("Failed" )))
175187 .when (spyService )
176188 .sendPostRequest (anyString (), any (), any ());
177189
@@ -475,7 +487,7 @@ public void shouldRetryIfGotError() throws IOException {
475487 when (mockCall .execute ()).thenReturn (mockResponseFail ).thenReturn (mockResponseSuccess );
476488
477489 Map <String , Object > result =
478- dtpService .sendPostRequest (SynologyConstant . C2_BASE_URL , requestBody , jobId );
490+ dtpService .sendPostRequest (TestConfigs . TEST_C2_BASE_URL , requestBody , jobId );
479491
480492 assertEquals (Map .of ("success" , true ), result );
481493
@@ -497,11 +509,11 @@ public void shouldThrowExceptionIfReachMaxRetries() throws IOException {
497509
498510 assertThrows (
499511 SynologyMaxRetriesExceededException .class ,
500- () -> dtpService .sendPostRequest (SynologyConstant . C2_BASE_URL , requestBody , jobId ),
501- "Failed to send POST request 3 times" );
512+ () -> dtpService .sendPostRequest (TestConfigs . TEST_C2_BASE_URL , requestBody , jobId ),
513+ String . format ( "Failed to send POST request %d times" , TestConfigs . TEST_MAX_ATTEMPTS ) );
502514 verify (tokenManager , never ())
503515 .refreshToken (any (UUID .class ), eq (client ), any (ObjectMapper .class ));
504- verify (client , times (3 )).newCall (any (Request .class ));
516+ verify (client , times (TestConfigs . TEST_MAX_ATTEMPTS )).newCall (any (Request .class ));
505517 }
506518
507519 @ Test
@@ -525,7 +537,7 @@ public void shouldRefreshTokenAndRetryIfGotUnauthorizedHttpError()
525537 .thenReturn (true );
526538
527539 Map <String , Object > result =
528- dtpService .sendPostRequest (SynologyConstant . C2_BASE_URL , requestBody , jobId );
540+ dtpService .sendPostRequest (TestConfigs . TEST_C2_BASE_URL , requestBody , jobId );
529541
530542 assertEquals (Map .of ("success" , true ), result );
531543
@@ -550,12 +562,12 @@ public void shouldInvokeRefreshTokenOnlyOnceIfGotUnauthorizedMultipleTimes()
550562
551563 assertThrows (
552564 SynologyMaxRetriesExceededException .class ,
553- () -> dtpService .sendPostRequest (SynologyConstant . C2_BASE_URL , requestBody , jobId ),
554- "Failed to send POST request 3 times" );
565+ () -> dtpService .sendPostRequest (TestConfigs . TEST_C2_BASE_URL , requestBody , jobId ),
566+ String . format ( "Failed to send POST request %d times" , TestConfigs . TEST_MAX_ATTEMPTS ) );
555567
556568 verify (tokenManager , times (1 ))
557569 .refreshToken (any (UUID .class ), eq (client ), any (ObjectMapper .class ));
558- verify (client , times (3 )).newCall (any (Request .class ));
570+ verify (client , times (TestConfigs . TEST_MAX_ATTEMPTS )).newCall (any (Request .class ));
559571 }
560572
561573 @ Test
@@ -573,11 +585,11 @@ public void shouldThrowExceptionIfParseResponseFailed() throws IOException {
573585
574586 assertThrows (
575587 SynologyMaxRetriesExceededException .class ,
576- () -> dtpService .sendPostRequest (SynologyConstant . C2_BASE_URL , requestBody , jobId ),
577- "Failed to send POST request 3 times" );
588+ () -> dtpService .sendPostRequest (TestConfigs . TEST_C2_BASE_URL , requestBody , jobId ),
589+ String . format ( "Failed to send POST request %d times" , TestConfigs . TEST_MAX_ATTEMPTS ) );
578590 verify (tokenManager , never ())
579591 .refreshToken (any (UUID .class ), eq (client ), any (ObjectMapper .class ));
580- verify (client , times (3 )).newCall (any (Request .class ));
592+ verify (client , times (TestConfigs . TEST_MAX_ATTEMPTS )).newCall (any (Request .class ));
581593 }
582594
583595 @ Test
@@ -593,7 +605,7 @@ public void shouldReturnResponseData() throws IOException, JsonProcessingExcepti
593605 when (mockResponseBody .string ()).thenReturn ("{\" success\" : true}" );
594606
595607 Map <String , Object > result =
596- dtpService .sendPostRequest (SynologyConstant . C2_BASE_URL , requestBody , jobId );
608+ dtpService .sendPostRequest (TestConfigs . TEST_C2_BASE_URL , requestBody , jobId );
597609 assertEquals (Map .of ("success" , true ), result );
598610 verify (tokenManager , never ())
599611 .refreshToken (any (UUID .class ), eq (client ), any (ObjectMapper .class ));
0 commit comments