Skip to content

Commit

Permalink
Adding actions
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasmiller committed Apr 18, 2022
1 parent 28f36ca commit cddb4c8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 60 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI
on: [push]
concurrency:
group: ${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
name: Java ${{ matrix.java }} tests
strategy:
matrix:
java: [8, 11, 16, 17]
steps:
- uses: actions/checkout@v3
- name: Setup java
uses: actions/setup-java@v3
with:
distribution: temurin
java-version: ${{ matrix.java }}
- run: ./scripts/build
- run: ./scripts/test
buildall:
if: ${{ always() }}
runs-on: ubuntu-latest
name: Build (matrix)
needs: build
steps:
- name: Check build matrix status
if: ${{ needs.build.result != 'success' }}
run: exit 1
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,13 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.0</version>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/recurly/v3/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ private static OkHttpClient newHttpClient(final String apiKey) {
new HeaderInterceptor(authToken, Client.API_VERSION);
httpClientBuilder.addInterceptor(headerInterceptor);

if ("true".equals(System.getenv("RECURLY_INSECURE"))
&& "true".equals(System.getenv("RECURLY_DEBUG"))) {
if (envEnabled("RECURLY_INSECURE") && envEnabled("RECURLY_DEBUG")) {
final HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpClientBuilder.addInterceptor(logging);
Expand All @@ -73,23 +72,25 @@ private static OkHttpClient newHttpClient(final String apiKey) {
return httpClientBuilder.build();
}

protected static boolean envEnabled(final String envVar) {
return "true".equals(System.getenv(envVar));
}

protected void makeRequest(final String method, final String url) {
final okhttp3.Request request = buildRequest(method, url, null, null);

try (final Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
String responseString = response.body().string();
if ("true".equals(System.getenv("RECURLY_INSECURE"))
&& "true".equals(System.getenv("RECURLY_DEBUG"))) {
if (envEnabled("RECURLY_INSECURE") && envEnabled("RECURLY_DEBUG")) {
System.out.println(responseString);
}
throw jsonSerializer.deserializeError(responseString);
}

final Headers responseHeaders = response.headers();

if ("true".equals(System.getenv("RECURLY_INSECURE"))
&& "true".equals(System.getenv("RECURLY_DEBUG"))) {
if (envEnabled("RECURLY_INSECURE") && envEnabled("RECURLY_DEBUG")) {
for (int i = 0; i < responseHeaders.size(); i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
Expand Down Expand Up @@ -228,8 +229,7 @@ private okhttp3.Request buildRequest(

final HttpUrl requestUrl = httpBuilder.build();

if ("true".equals(System.getenv("RECURLY_INSECURE"))
&& "true".equals(System.getenv("RECURLY_DEBUG"))) {
if (envEnabled("RECURLY_INSECURE") && envEnabled("RECURLY_DEBUG")) {
System.out.println("Performing " + method + " request to " + requestUrl);
}

Expand Down Expand Up @@ -293,7 +293,7 @@ public void _setApiUrl(final String uri) {
System.out.println(
"[SECURITY WARNING] _setApiUrl is for testing only and not supported in production.");

if ("true".equals(System.getenv("RECURLY_INSECURE"))) {
if (envEnabled("RECURLY_INSECURE")) {
this.apiUrl = uri;
} else {
System.out.println(
Expand Down
51 changes: 11 additions & 40 deletions src/test/java/com/recurly/v3/BaseClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.mockito.MockedStatic;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.eq;

@SuppressWarnings("unchecked")
public class BaseClientTest {
Expand Down Expand Up @@ -354,19 +357,16 @@ public void testSetApiUrl() {

@Test
public void testCantSetApiUrlWithoutRecurlyInsecure() throws Exception {
final HashMap<String, String> environmentVariables = new HashMap<String, String>();
environmentVariables.put("RECURLY_INSECURE", "false");
setEnv(environmentVariables);
final MockClient client = new MockClient("apiKey");
final String originalUrl = client.getApiUrl();
final String newApiUrl = "https://my.base.url/";
client._setApiUrl(newApiUrl);
try (MockedStatic<BaseClient> theMock = mockStatic(BaseClient.class)) {
theMock.when(() -> BaseClient.envEnabled(eq("RECURLY_INSECURE"))).thenReturn(false);

assertEquals(originalUrl, client.getApiUrl());
final MockClient client = new MockClient("apiKey");
final String originalUrl = client.getApiUrl();
final String newApiUrl = "https://my.base.url/";
client._setApiUrl(newApiUrl);

environmentVariables.clear();
environmentVariables.put("RECURLY_INSECURE", "true");
setEnv(environmentVariables);
assertEquals(originalUrl, client.getApiUrl());
}
}

@Test
Expand Down Expand Up @@ -439,35 +439,6 @@ public void testInterpolatePathMatching() {
assertEquals("/url_path/replacement", interpolatedPath);
}

protected static void setEnv(Map<String, String> newenv) throws Exception {
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newenv);
Field theCaseInsensitiveEnvironmentField =
processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv =
(Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newenv);
} catch (NoSuchFieldException e) {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for (Class cl : classes) {
if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.putAll(newenv);
}
}
}
}

private static String getResponseJson() {
return "{ \"my_string\": \"aaron\" }";
}
Expand Down

0 comments on commit cddb4c8

Please sign in to comment.