Skip to content

Commit

Permalink
Fix build.
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 11, 2024
1 parent ff1c7e1 commit 79e06f8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@
*/
package com.epam.reportportal.service;

import com.google.common.net.HttpHeaders;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import javax.annotation.Nonnull;
import java.io.IOException;

/**
* Adds Bearer TOKEN to the request headers
*/
public class BearerAuthInterceptor implements Interceptor {

private final String uuid;
private final String apiKey;

public BearerAuthInterceptor(String uuid) {
this.uuid = uuid;
public BearerAuthInterceptor(String apiKey) {
this.apiKey = apiKey;
}

@Override
public Response intercept(Chain chain) throws IOException {
Request rq = chain.request().newBuilder().addHeader(HttpHeaders.AUTHORIZATION, "bearer " + uuid).build();
@Nonnull
public Response intercept(Chain chain) throws IOException {
Request rq = chain.request().newBuilder().addHeader("Authorization", "Bearer " + apiKey).build();
return chain.proceed(rq);
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/epam/reportportal/utils/SslUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package com.epam.reportportal.utils;

import com.epam.reportportal.exception.InternalReportPortalClientException;
import com.google.common.io.Resources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.InputStream;
import java.security.KeyStore;

import static com.epam.reportportal.utils.files.Utils.getFile;

/**
* @author Andrei Varabyeu
*/
Expand All @@ -37,7 +39,7 @@ public class SslUtils {
* @return JKD keystore representation
*/
public static KeyStore loadKeyStore(String keyStore, String password) {
try (InputStream is = Resources.asByteSource(Resources.getResource(keyStore)).openStream()) {
try (InputStream is = getFile(new File(keyStore)).openStream()) {
KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(is, password.toCharArray());
return trustStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

import com.epam.reportportal.exception.InternalReportPortalClientException;
import com.epam.reportportal.message.TypeAwareByteSource;
import com.google.common.net.MediaType;
import com.epam.reportportal.utils.http.ContentType;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import static java.util.Optional.ofNullable;

/**
* This class contains functionality for converting images to Black and white
* colors
Expand Down Expand Up @@ -73,24 +75,14 @@ public static boolean isImage(TypeAwareByteSource source) {
return isImage(source.getMediaType());
}

/**
* Check is input file is image
*
* @param contentType ContentType
* @return true if image
*/
public static boolean isImage(MediaType contentType) {
return contentType.type().equalsIgnoreCase(IMAGE_TYPE);
}

/**
* Check is input file is image
*
* @param contentType ContentType
* @return true if image
*/
public static boolean isImage(String contentType) {
return isImage(MediaType.parse(contentType));
return ofNullable(ContentType.parse(contentType)).map(type -> type.startsWith(IMAGE_TYPE)).orElse(false);
}

/**
Expand All @@ -105,8 +97,6 @@ private static TypeAwareByteSource convertToInputStream(BufferedImage image) {
} catch (IOException e) {
throw new InternalReportPortalClientException("Unable to transform file to byte array.", e);
}
return new TypeAwareByteSource(ByteSource.wrap(byteOutputStream.toByteArray()), MediaType.PNG.toString());

return new TypeAwareByteSource(ByteSource.wrap(byteOutputStream.toByteArray()), ContentType.IMAGE_PNG);
}

}
87 changes: 87 additions & 0 deletions src/main/java/com/epam/reportportal/utils/http/ContentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2023 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.epam.reportportal.utils.http;

import javax.annotation.Nullable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Content-Type header constants and utility methods.
*/
@SuppressWarnings("unused")
public class ContentType {
private static final Pattern HTTP_HEADER_DELIMITER_PATTERN = Pattern.compile("[=;,]");

// Binary types
public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
public static final String IMAGE_BMP = "image/bmp";
public static final String IMAGE_GIF = "image/gif";
public static final String IMAGE_JPEG = "image/jpeg";
public static final String IMAGE_PNG = "image/png";
public static final String IMAGE_TIFF = "image/tiff";
public static final String IMAGE_WEBP = "image/webp";


// Text types
public static final String APPLICATION_ATOM_XML = "application/atom+xml";
public static final String APPLICATION_JSON = "application/json";
public static final String APPLICATION_SOAP_XML = "application/soap+xml";
public static final String APPLICATION_SVG_XML = "application/svg+xml";
public static final String APPLICATION_XHTML_XML = "application/xhtml+xml";
public static final String APPLICATION_XML = "application/xml";
public static final String IMAGE_SVG = "image/svg+xml";
public static final String TEXT_PLAIN = "text/plain";
public static final String TEXT_HTML = "text/html";
public static final String TEXT_XML = "text/xml";

// Form types
public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded";

// Multipart types
public static final String MULTIPART_FORM_DATA = "multipart/form-data";
public static final String MULTIPART_MIXED = "multipart/mixed";
public static final String MULTIPART_ALTERNATIVE = "multipart/alternative";
public static final String MULTIPART_DIGEST = "multipart/digest";
public static final String MULTIPART_PARALLEL = "multipart/parallel";

private ContentType() {
throw new RuntimeException("No instances should exist for the class!");
}

/**
* Extract Media Type from a Content-Type header.
*
* @param contentType Content-Type header value
* @return Media Type
*/
@Nullable
public static String parse(@Nullable String contentType) {
if (contentType == null || contentType.trim().isEmpty()) {
return null;
}
String trimmed = contentType.trim();
Matcher m = HTTP_HEADER_DELIMITER_PATTERN.matcher(trimmed);
String mimeType;
if (m.find()) {
mimeType = trimmed.substring(0, m.start());
} else {
mimeType = trimmed;
}
return mimeType.isEmpty() ? null : mimeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.epam.reportportal.utils.properties;

import com.epam.reportportal.util.test.ProcessUtils;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Triple;
import org.junit.jupiter.api.Test;
Expand All @@ -25,6 +24,7 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Properties;

import static com.epam.reportportal.util.test.ProcessUtils.waitForLine;
Expand All @@ -40,7 +40,7 @@ public void testOverride() {
properties.setProperty(propertyKey, "testvalue");

PropertiesLoader.overrideWith(properties,
ImmutableMap.<String, String>builder().put(propertyKey, "anothervalue").build()
Collections.singletonMap(propertyKey, "anothervalue")
);
assertThat("Incorrect override behaviour", properties.getProperty(propertyKey), equalTo("anothervalue"));

Expand All @@ -59,7 +59,7 @@ public void testOverrideVariableWithUnderscore() {
properties.setProperty("rp.description", "testvalue");

PropertiesLoader.overrideWith(properties,
ImmutableMap.<String, String>builder().put("rp_description", "anothervalue").build()
Collections.singletonMap("rp_description", "anothervalue")
);
assertThat("Incorrect override behaviour",
properties.getProperty(ListenerProperty.DESCRIPTION.getPropertyName()),
Expand All @@ -73,7 +73,7 @@ public void test_loader_ignores_upper_case() {
properties.setProperty("rp.description", "testvalue");

PropertiesLoader.overrideWith(properties,
ImmutableMap.<String, String>builder().put("RP_DESCRIPTION", "anothervalue").build()
Collections.singletonMap("RP_DESCRIPTION", "anothervalue")
);
assertThat("Incorrect override behaviour",
properties.getProperty(ListenerProperty.DESCRIPTION.getPropertyName()),
Expand All @@ -94,12 +94,11 @@ public void verify_override_duplicate_key() {
Properties properties = new Properties();
properties.setProperty("rp.description", "testvalue");

PropertiesLoader.overrideWith(properties,
ImmutableMap.<String, String>builder()
.put("rp_description", "anothervalue")
.put("rp.description", "thirdvalue")
.build()
);
HashMap<Object, Object> override = new HashMap<>();
override.put("rp_description", "anothervalue");
override.put("rp.description", "thirdvalue");

PropertiesLoader.overrideWith(properties, override);
assertThat("Incorrect override behaviour",
properties.getProperty(ListenerProperty.DESCRIPTION.getPropertyName()),
equalTo("anothervalue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

import com.epam.reportportal.annotations.Step;
import com.epam.reportportal.utils.ParameterUtils;
import com.google.common.collect.Lists;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -52,18 +53,18 @@ public void retrieveValue(String template, String expected) throws NoSuchMethodE
}

public static Object[][] data() {
return new Object[][] { { "someObject.outerName", "outer" }, { "someObject.innerName", "inner" },
{ "someObject.innerStrings", "[firstInner, secondInner, thirdInner]" }, { "someObject", "INNER" },
{ "someObject.outers", "[OUTER]" },
{ "someObject.outers.outerStrings", "[[{first, second, third}, {fourth, fifth, sixth}]]" },
{ "someObject.outers.outerName", "[outer]" }, { "someObject.innerNullString", ParameterUtils.NULL_VALUE },
{ "someObject.innerNullList", ParameterUtils.NULL_VALUE }};
return new Object[][]{{"someObject.outerName", "outer"}, {"someObject.innerName", "inner"},
{"someObject.innerStrings", "[firstInner, secondInner, thirdInner]"}, {"someObject", "INNER"},
{"someObject.outers", "[OUTER]"},
{"someObject.outers.outerStrings", "[[{first, second, third}, {fourth, fifth, sixth}]]"},
{"someObject.outers.outerName", "[outer]"}, {"someObject.innerNullString", ParameterUtils.NULL_VALUE},
{"someObject.innerNullList", ParameterUtils.NULL_VALUE}};
}

private Outer.Inner createInnerObject() {

final String[] strings = { "first", "second", "third" };
final String[] moreStrings = { "fourth", "fifth", "sixth" };
final String[] strings = {"first", "second", "third"};
final String[] moreStrings = {"fourth", "fifth", "sixth"};
List<String[]> outerStrings = new ArrayList<String[]>() {
{
add(strings);
Expand All @@ -75,11 +76,12 @@ private Outer.Inner createInnerObject() {
final String innerName = "inner";

List<String> innerStrings = getInnerStrings();
return new Outer.Inner(outerName, outerStrings, innerName, innerStrings, Lists.newArrayList(new Outer(outerName, outerStrings)));
return new Outer.Inner(outerName, outerStrings, innerName, innerStrings,
Collections.singletonList(new Outer(outerName, outerStrings)));
}

private static List<String> getInnerStrings() {
return Lists.newArrayList("firstInner", "secondInner", "thirdInner");
return Arrays.asList("firstInner", "secondInner", "thirdInner");
}

@SuppressWarnings({"unused", "FieldCanBeLocal"})
Expand Down

0 comments on commit 79e06f8

Please sign in to comment.