Skip to content

Commit f76737c

Browse files
committed
♻️ Refactored test to reduce the lines of code slightly.
1 parent ff438c8 commit f76737c

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

rest-services/client/src/test/java/com/_4point/aem/docservices/rest_services/client/docassurance/RestServicesDocAssuranceServiceAdapterTest.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
2323
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.junit.jupiter.api.function.Executable;
2425
import org.junit.jupiter.params.ParameterizedTest;
2526
import org.junit.jupiter.params.provider.Arguments;
2627
import org.junit.jupiter.params.provider.MethodSource;
28+
import org.mockito.Answers;
2729
import org.mockito.ArgumentCaptor;
2830
import org.mockito.Captor;
2931
import org.mockito.Mock;
@@ -326,7 +328,7 @@ void testSecureDocument() throws Exception {
326328
@Test
327329
void testSecureDocument_RestClientException() throws Exception {
328330
var cause = new RestClientException("cause exception");
329-
var ex = mockForException(cause);
331+
var ex = mockSecureDocumentForException(cause);
330332

331333
assertThat(ex, allOf(ExceptionMatchers.exceptionMsgContainsAll("Error while POSTing to server"),
332334
ExceptionMatchers.hasCause(cause)
@@ -336,31 +338,31 @@ void testSecureDocument_RestClientException() throws Exception {
336338
@Test
337339
void testSecureDocument_IOException() throws Exception {
338340
var cause = new IOException("cause exception");
339-
var ex = mockForException(cause);
341+
var ex = mockSecureDocumentForException(cause);
340342

341343
assertThat(ex, allOf(ExceptionMatchers.exceptionMsgContainsAll("I/O Error while securing document"),
342344
ExceptionMatchers.hasCause(cause)
343345
));
344346
}
345-
346-
<T extends Exception> DocAssuranceServiceException mockForException(T exception) throws Exception {
347+
348+
<T extends Exception> DocAssuranceServiceException mockSecureDocumentForException(T cause) throws Exception {
347349
Document pdf = MockDocumentFactory.GLOBAL_INSTANCE.create("pdf Document Data".getBytes());
348350
ReaderExtensionOptions reOptions = mock(ReaderExtensionOptions.class);
349351
ReaderExtensionsOptionSpec reOptionsSpec = mock(ReaderExtensionsOptionSpec.class);
350352
UsageRights reUsageOptionsSpec = mock(UsageRights.class);
351353
when(reOptions.getReOptions()).thenReturn(reOptionsSpec);
352354
when(reOptionsSpec.getUsageRights()).thenReturn(reUsageOptionsSpec);
353-
//
354355
final String CREDENTIAL_ALIAS = "credentialAlias";
355356
when(reOptions.getCredentialAlias()).thenReturn(CREDENTIAL_ALIAS);
356357

357-
when(mockPayloadBuilder.add(eq(DOCUMENT_PARAM), same(pdf), eq(ContentType.APPLICATION_PDF))).thenReturn(mockPayloadBuilder);
358-
when(mockPayloadBuilder.addIfNotNull(eq(CREDENTIAL_ALIAS_PARAM), eq(CREDENTIAL_ALIAS))).thenReturn(mockPayloadBuilder);
359-
when(mockPayloadBuilder.transformAndAdd(eq(MESSAGE_PARAM), same(reOptionsSpec), any())).thenReturn(mockPayloadBuilder);
360-
when(mockPayloadBuilder.transformAndAddStringVersion(any(), any(), any())).thenReturn(mockPayloadBuilder);
361-
362-
when(mockClient.multipartPayloadBuilder()).thenReturn(mockPayloadBuilder);
363-
when(mockPayloadBuilder.build()).thenReturn(mockPayload);
358+
return mockForException(cause, ()->underTest.secureDocument(pdf, null, null, reOptions, null));
359+
}
360+
361+
362+
<T extends Exception> DocAssuranceServiceException mockForException(T exception, Executable test) throws Exception {
363+
MultipartPayload.Builder localMockPayloadBuilder = Mockito.mock(MultipartPayload.Builder.class, Answers.RETURNS_SELF);
364+
when(mockClient.multipartPayloadBuilder()).thenReturn(localMockPayloadBuilder);
365+
when(localMockPayloadBuilder.build()).thenReturn(mockPayload);
364366

365367
if (exception instanceof IOException) {
366368
when(mockResponse.contentType()).thenReturn(ContentType.APPLICATION_PDF);
@@ -371,7 +373,7 @@ <T extends Exception> DocAssuranceServiceException mockForException(T exception)
371373
when(mockPayload.postToServer(any())).thenThrow(exception);
372374
}
373375

374-
return assertThrows(DocAssuranceServiceException.class, ()->underTest.secureDocument(pdf, null, null, reOptions, null));
376+
return assertThrows(DocAssuranceServiceException.class, test);
375377
}
376378

377379
private static RestServicesDocAssuranceServiceAdapter createAdapter(TriFunction<AemConfig, String, Supplier<String>, RestClient> clientFactory) {

0 commit comments

Comments
 (0)