Skip to content

Commit

Permalink
Remove JetBrains annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 17, 2024
1 parent 61f73b5 commit 5cb0490
Showing 1 changed file with 39 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpCoreContext;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -50,6 +49,7 @@
import org.mockito.Mockito;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand All @@ -60,9 +60,8 @@
import static com.epam.reportportal.formatting.http.Constants.*;
import static java.util.Optional.ofNullable;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
Expand All @@ -89,8 +88,8 @@ public class ReportPortalHttpLoggingInterceptorTest {
}

public static Iterable<Object[]> requestData() {
return Arrays.asList(new Object[] { JSON_TYPE, "{\"object\": {\"key\": \"value\"}}",
"{\"object\": {\"key\": \"value\"}}", JsonPrettier.INSTANCE, null, null },
return Arrays.asList(new Object[] { JSON_TYPE, "{\"object\": {\"key\": \"value\"}}", "{\"object\": {\"key\": \"value\"}}",
JsonPrettier.INSTANCE, null, null },
new Object[] { "application/xml", "<test><key><value>value</value></key></test>",
"<test><key><value>value</value></key></test>", XmlPrettier.INSTANCE, null, null }
);
Expand All @@ -113,8 +112,7 @@ private List<String> runChainTextMessageCapture(HttpRequest request, HttpRespons
ArgumentCaptor<String> logCapture = ArgumentCaptor.forClass(String.class);
runChain(request,
response,
mock -> mock.when(() -> ReportPortal.emitLog(logCapture.capture(), anyString(), any(Date.class)))
.thenReturn(Boolean.TRUE)
mock -> mock.when(() -> ReportPortal.emitLog(logCapture.capture(), anyString(), any(Date.class))).thenReturn(Boolean.TRUE)
);
return logCapture.getAllValues();
}
Expand All @@ -123,14 +121,13 @@ private List<ReportPortalMessage> runChainBinaryMessageCapture(HttpRequest reque
ArgumentCaptor<ReportPortalMessage> logCapture = ArgumentCaptor.forClass(ReportPortalMessage.class);
runChain(request,
response,
mock -> mock.when(() -> ReportPortal.emitLog(logCapture.capture(), anyString(), any(Date.class)))
.thenReturn(Boolean.TRUE)
mock -> mock.when(() -> ReportPortal.emitLog(logCapture.capture(), anyString(), any(Date.class))).thenReturn(Boolean.TRUE)
);
return logCapture.getAllValues();
}

private Triple<List<String>, List<String>, List<ReportPortalMessage>> runChainComplexMessageCapture(
HttpRequest request, HttpResponse response, ReportPortalHttpLoggingInterceptor interceptor) {
private Triple<List<String>, List<String>, List<ReportPortalMessage>> runChainComplexMessageCapture(HttpRequest request,
HttpResponse response, ReportPortalHttpLoggingInterceptor interceptor) {
ArgumentCaptor<String> stepCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<ReportPortalMessage> messageArgumentCaptor = ArgumentCaptor.forClass(ReportPortalMessage.class);
Expand All @@ -147,20 +144,16 @@ private Triple<List<String>, List<String>, List<ReportPortalMessage>> runChainCo
.thenReturn(Boolean.TRUE);
}, interceptor);
}
return Triple.of(stepCaptor.getAllValues(),
stringArgumentCaptor.getAllValues(),
messageArgumentCaptor.getAllValues()
);
return Triple.of(stepCaptor.getAllValues(), stringArgumentCaptor.getAllValues(), messageArgumentCaptor.getAllValues());
}

@SuppressWarnings("SameParameterValue")
private Triple<List<String>, List<String>, List<ReportPortalMessage>> runChainComplexMessageCapture(
HttpRequest request, HttpResponse response) {
private Triple<List<String>, List<String>, List<ReportPortalMessage>> runChainComplexMessageCapture(HttpRequest request,
HttpResponse response) {
return runChainComplexMessageCapture(request, response, new ReportPortalHttpLoggingInterceptor(LogLevel.INFO));
}

private static HttpRequest mockBasicRequest(@Nonnull Collection<Pair<String, String>> headers,
@Nullable HttpEntity body) {
private static HttpRequest mockBasicRequest(@Nonnull Collection<Pair<String, String>> headers, @Nullable HttpEntity body) {
HttpPost request = new HttpPost(URI);
headers.forEach(h -> request.addHeader(h.getKey(), h.getValue()));
if (body != null) {
Expand All @@ -177,8 +170,7 @@ private static HttpRequest mockBasicRequest() {
return mockBasicRequest(Collections.emptyList());
}

private static HttpResponse createBasicResponse(@Nonnull Collection<Pair<String, String>> headers,
@Nullable HttpEntity body) {
private static HttpResponse createBasicResponse(@Nonnull Collection<Pair<String, String>> headers, @Nullable HttpEntity body) {
StatusLine statusLine = mock(StatusLine.class);
when(statusLine.getStatusCode()).thenReturn(STATUS_CODE);
when(statusLine.getReasonPhrase()).thenReturn("Created");
Expand Down Expand Up @@ -215,17 +207,12 @@ public void test_logger_null_values() {

@ParameterizedTest
@MethodSource("requestData")
public void test_logger_text_body(String mimeType, String requestBodyStr, String responseBodyStr,
Function<String, String> prettier) {
public void test_logger_text_body(String mimeType, String requestBodyStr, String responseBodyStr, Function<String, String> prettier) {
Charset charset = StandardCharsets.UTF_8;
HttpEntity requestBody = new ByteArrayEntity(requestBodyStr.getBytes(charset),
ContentType.create(mimeType, charset)
);
HttpEntity requestBody = new ByteArrayEntity(requestBodyStr.getBytes(charset), ContentType.create(mimeType, charset));
HttpRequest request = mockBasicRequest(Collections.emptyList(), requestBody);

HttpEntity responseBody = new ByteArrayEntity(requestBodyStr.getBytes(charset),
ContentType.create(mimeType, charset)
);
HttpEntity responseBody = new ByteArrayEntity(requestBodyStr.getBytes(charset), ContentType.create(mimeType, charset));
HttpResponse response = createBasicResponse(Collections.emptyList(), responseBody);

List<String> logs = runChainTextMessageCapture(request, response);
Expand Down Expand Up @@ -280,8 +267,7 @@ public void test_logger_cookies() {
assertThat(logs, hasSize(2)); // Request + Response

String requestHeaderString = "\n\n**Cookies**\n" + "test: value";
String responseHeaderString =
"\n\n**Cookies**\n" + "test: value; Path=/; Secure=true; HttpOnly=true; Expires=" + expiryDate;
String responseHeaderString = "\n\n**Cookies**\n" + "test: value; Path=/; Secure=true; HttpOnly=true; Expires=" + expiryDate;

assertThat(logs.get(0), equalTo(EMPTY_REQUEST + requestHeaderString));
assertThat(logs.get(1), equalTo(EMPTY_RESPONSE + responseHeaderString));
Expand All @@ -297,12 +283,8 @@ public void test_logger_headers_and_cookies(String contentType) {
String expiryDate1 = "Tue, 06 Sep 2022 09:32:51 UTC";
String expiryDate2 = "Tue, 06 Sep 2022 09:32:51 UTC";
Collection<Pair<String, String>> responseHeaders = Arrays.asList(Pair.of(HTTP_HEADER, HTTP_HEADER_VALUE),
Pair.of("Set-cookie",
"test=value; comment=test comment; expires=" + expiryDate1 + "; path=/; version=1"
),
Pair.of("Set-cookie",
"tz=Europe%2FMinsk; path=/; expires=" + expiryDate2 + "; secure; HttpOnly; SameSite=Lax"
)
Pair.of("Set-cookie", "test=value; comment=test comment; expires=" + expiryDate1 + "; path=/; version=1"),
Pair.of("Set-cookie", "tz=Europe%2FMinsk; path=/; expires=" + expiryDate2 + "; secure; HttpOnly; SameSite=Lax")
);
HttpRequest request = mockBasicRequest(headers);
HttpResponse response = createBasicResponse(responseHeaders);
Expand All @@ -313,8 +295,7 @@ public void test_logger_headers_and_cookies(String contentType) {
assertThat(logs, hasSize(2)); // Request + Response

String requestHeaderString =
"\n\n**Headers**\n" + HTTP_HEADER + ": " + HTTP_HEADER_VALUE + "\n\n**Cookies**\n" + "test: value\n"
+ "tz: Europe/Minsk";
"\n\n**Headers**\n" + HTTP_HEADER + ": " + HTTP_HEADER_VALUE + "\n\n**Cookies**\n" + "test: value\n" + "tz: Europe/Minsk";

String responseHeaderString = "\n\n**Headers**\n" + HTTP_HEADER + ": " + HTTP_HEADER_VALUE + "\n\n**Cookies**\n"
+ "test: value; Comment=test comment; Path=/; Expires=" + expiryDate1 + "; Version=1\n"
Expand Down Expand Up @@ -419,19 +400,16 @@ public void test_logger_image_multipart() throws IOException {
HttpEntity body = getBinaryBody(imageType, IMAGE);
HttpRequest request = mockBasicRequest(Collections.emptyList(), body);

Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(request,
createBasicResponse()
);
Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(request, createBasicResponse());
assertThat(logs.getLeft(), hasSize(1));
assertThat(logs.getMiddle(), hasSize(1));
assertThat(logs.getRight(), hasSize(1));

assertThat(logs.getLeft().get(0), equalTo(EMPTY_REQUEST));
assertThat(logs.getMiddle().get(0), equalTo(EMPTY_RESPONSE));
assertThat(logs.getRight().get(0).getMessage(),
equalTo(HEADERS_TAG + LINE_DELIMITER
+ "Content-Disposition: form-data; name=\"file\"; filename=\"pug/lucky.jpg\"" + LINE_DELIMITER
+ "Content-Type: image/jpeg" + LINE_DELIMITER + "Content-Transfer-Encoding: binary"
equalTo(HEADERS_TAG + LINE_DELIMITER + "Content-Disposition: form-data; name=\"file\"; filename=\"pug/lucky.jpg\""
+ LINE_DELIMITER + "Content-Type: image/jpeg" + LINE_DELIMITER + "Content-Transfer-Encoding: binary"
+ LINE_DELIMITER + LINE_DELIMITER + BODY_PART_TAG + LINE_DELIMITER + imageType.getMimeType())
);
assertThat(logs.getRight().get(0).getData().getMediaType(), equalTo(imageType.getMimeType()));
Expand All @@ -440,10 +418,7 @@ public void test_logger_image_multipart() throws IOException {

@SuppressWarnings("SameParameterValue")
private HttpEntity getBinaryTextBody(ContentType textType, String text, ContentType binaryType, String filePath) {
FormBodyPart textPart = FormBodyPartBuilder.create()
.setName("text")
.setBody(new StringBody(text, textType))
.build();
FormBodyPart textPart = FormBodyPartBuilder.create().setName("text").setBody(new StringBody(text, textType)).build();
FormBodyPart binaryPart = getBinaryPart(binaryType, filePath);

return MultipartEntityBuilder.create().addPart(binaryPart).addPart(textPart).build();
Expand All @@ -469,25 +444,23 @@ public void test_logger_text_and_image_multipart() throws IOException {

assertThat(logs.getMiddle().get(0),
equalTo(HEADERS_TAG + LINE_DELIMITER + "Content-Disposition: form-data; name=\"text\"" + LINE_DELIMITER
+ "Content-Type: text/plain; charset=ISO-8859-1" + LINE_DELIMITER
+ "Content-Transfer-Encoding: 8bit" + LINE_DELIMITER + LINE_DELIMITER + BODY_PART_TAG
+ "\n```\n" + message + "\n```")
+ "Content-Type: text/plain; charset=ISO-8859-1" + LINE_DELIMITER + "Content-Transfer-Encoding: 8bit"
+ LINE_DELIMITER + LINE_DELIMITER + BODY_PART_TAG + "\n```\n" + message + "\n```")
);
assertThat(logs.getMiddle().get(1), equalTo(EMPTY_RESPONSE));

assertThat(logs.getRight().get(0).getMessage(),
equalTo(HEADERS_TAG + LINE_DELIMITER
+ "Content-Disposition: form-data; name=\"file\"; filename=\"pug/lucky.jpg\"" + LINE_DELIMITER
+ "Content-Type: image/jpeg" + LINE_DELIMITER + "Content-Transfer-Encoding: binary"
equalTo(HEADERS_TAG + LINE_DELIMITER + "Content-Disposition: form-data; name=\"file\"; filename=\"pug/lucky.jpg\""
+ LINE_DELIMITER + "Content-Type: image/jpeg" + LINE_DELIMITER + "Content-Transfer-Encoding: binary"
+ LINE_DELIMITER + LINE_DELIMITER + BODY_PART_TAG + LINE_DELIMITER + imageType.getMimeType())
);
assertThat(logs.getRight().get(0).getData().getMediaType(), equalTo(imageType.getMimeType()));
assertThat(logs.getRight().get(0).getData().read(), equalTo(image));
}

public static Iterable<Object[]> invalidContentTypes() {
return Arrays.asList(new Object[] { "", ContentType.APPLICATION_OCTET_STREAM.getMimeType(),
ContentType.APPLICATION_OCTET_STREAM.getMimeType() },
return Arrays.asList(
new Object[] { "", ContentType.APPLICATION_OCTET_STREAM.getMimeType(), ContentType.APPLICATION_OCTET_STREAM.getMimeType() },
new Object[] { "*/*", ContentType.APPLICATION_OCTET_STREAM.getMimeType(),
ContentType.APPLICATION_OCTET_STREAM.getMimeType() },
new Object[] { "something invalid", ContentType.APPLICATION_OCTET_STREAM.getMimeType(),
Expand All @@ -503,8 +476,8 @@ public static Iterable<Object[]> invalidContentTypes() {

@ParameterizedTest
@MethodSource("invalidContentTypes")
public void test_logger_invalid_content_type(String mimeType, String expectedRequestType,
String expectedResponseType) throws IOException {
public void test_logger_invalid_content_type(String mimeType, String expectedRequestType, String expectedResponseType)
throws IOException {
byte[] image = getResource(IMAGE);

HttpRequest request = mockBasicRequest(Collections.singletonList(Pair.of(HttpHeaders.CONTENT_TYPE, mimeType)),
Expand All @@ -514,18 +487,16 @@ public void test_logger_invalid_content_type(String mimeType, String expectedReq
new ByteArrayEntity(image)
);

Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(request,
response
);
Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(request, response);
String escapedMimeType = mimeType.replace("*", "\\*");
assertThat(logs.getRight(), hasSize(2)); // Request + Response
assertThat(logs.getRight().get(0).getMessage(),
equalTo(EMPTY_REQUEST + LINE_DELIMITER + LINE_DELIMITER + HEADERS_TAG + LINE_DELIMITER
+ HttpHeaders.CONTENT_TYPE + ": " + escapedMimeType)
equalTo(EMPTY_REQUEST + LINE_DELIMITER + LINE_DELIMITER + HEADERS_TAG + LINE_DELIMITER + HttpHeaders.CONTENT_TYPE + ": "
+ escapedMimeType)
);
assertThat(logs.getRight().get(1).getMessage(),
equalTo(EMPTY_RESPONSE + LINE_DELIMITER + LINE_DELIMITER + HEADERS_TAG + LINE_DELIMITER
+ HttpHeaders.CONTENT_TYPE + ": " + escapedMimeType)
equalTo(EMPTY_RESPONSE + LINE_DELIMITER + LINE_DELIMITER + HEADERS_TAG + LINE_DELIMITER + HttpHeaders.CONTENT_TYPE + ": "
+ escapedMimeType)
);

assertThat(logs.getRight().get(0).getData().getMediaType(), equalTo(expectedRequestType));
Expand All @@ -539,8 +510,7 @@ public void test_logger_invalid_content_type(String mimeType, String expectedReq
public void test_request_log_filter_type() {
HttpRequest requestSpecification = mockBasicRequest();
HttpResponse responseObject = createBasicResponse();
Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(
requestSpecification,
Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(requestSpecification,
responseObject,
new ReportPortalHttpLoggingInterceptor(LogLevel.INFO).addRequestFilter(r -> true)
);
Expand All @@ -552,8 +522,7 @@ public void test_request_log_filter_type() {
public void test_response_log_filter_type() {
HttpRequest requestSpecification = mockBasicRequest();
HttpResponse responseObject = createBasicResponse();
Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(
requestSpecification,
Triple<List<String>, List<String>, List<ReportPortalMessage>> logs = runChainComplexMessageCapture(requestSpecification,
responseObject,
new ReportPortalHttpLoggingInterceptor(LogLevel.INFO).addResponseFilter(r -> true)
);
Expand Down

0 comments on commit 5cb0490

Please sign in to comment.