Skip to content

Commit 012d014

Browse files
authored
add error prone and fix some potential bugs (#220)
1 parent 9081e75 commit 012d014

File tree

11 files changed

+144
-103
lines changed

11 files changed

+144
-103
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,32 @@ jobs:
2323
${{ runner.os }}-maven-
2424
- name: Install dependencies
2525
run: make install
26+
- name: Set up JDK for compilation
27+
uses: actions/setup-java@v3
28+
with:
29+
distribution: "zulu"
30+
java-version: "19" # Always use the latest JDK for building
31+
- name: Compile
32+
run: make build
2633
- name: Set up Java ${{ matrix.javaversion }}
2734
uses: actions/setup-java@v3
2835
with:
2936
distribution: "zulu"
3037
java-version: ${{ matrix.javaversion }}
31-
- name: Build and test with Maven
38+
- name: Run test with Java ${{ matrix.javaversion }}
39+
run: EASYPOST_TEST_API_KEY=123 EASYPOST_PROD_API_KEY=123 make test
40+
coverage:
41+
runs-on: ubuntu-20.04
42+
steps:
43+
- uses: actions/checkout@v3
44+
- name: Install dependencies
45+
run: make install
46+
- name: Set up JDK for compilation
47+
uses: actions/setup-java@v3
48+
with:
49+
distribution: "zulu"
50+
java-version: "19" # Always use the latest JDK for building
51+
- name: Test coverage
3252
run: EASYPOST_TEST_API_KEY=123 EASYPOST_PROD_API_KEY=123 make coverage
3353
- name: Load Rust cache
3454
if: github.ref == 'refs/heads/master'

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ scan:
5555

5656
## test - Test the project
5757
test:
58-
mvn --batch-mode install -Dgpg.skip=true -Dcheckstyle.skip=true -Dcheckstyle.skip=true -Ddependency-check.skip=true -Djavadoc.skip=true
58+
mvn surefire:test
5959

6060
.PHONY: help build clean coverage docs install-checkstyle install lint publish publish-dry release scan scan-strict test

dependency-check-suppressions.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<suppressions xmlns="https://jeremylong.github.io/DependencyCheck/dependency-suppression.1.3.xsd">
3+
<suppress>
4+
<!--
5+
Below vulnerabilities are from outdated Protocol Buffers which is the dependency of Error Prone.
6+
This will not affect our code
7+
-->
8+
<vulnerabilityName>CVE-2022-3171</vulnerabilityName>
9+
<vulnerabilityName>CVE-2022-3509</vulnerabilityName>
10+
<vulnerabilityName>CVE-2022-3510</vulnerabilityName>
11+
</suppress>
12+
</suppressions>

pom.xml

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<artifactId>gson</artifactId>
4848
<version>2.8.9</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>com.google.errorprone</groupId>
52+
<artifactId>error_prone_core</artifactId>
53+
<version>2.17.0</version>
54+
<scope>provided</scope>
55+
</dependency>
5056
<dependency>
5157
<groupId>org.junit.jupiter</groupId>
5258
<artifactId>junit-jupiter</artifactId>
@@ -101,6 +107,7 @@
101107
<properties>
102108
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
103109
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
110+
<maven.compiler.release>8</maven.compiler.release>
104111
</properties>
105112
<build>
106113
<resources>
@@ -198,8 +205,37 @@
198205
<artifactId>maven-compiler-plugin</artifactId>
199206
<version>3.9.0</version>
200207
<configuration>
201-
<source>1.8</source>
202-
<target>1.8</target>
208+
<release>8</release>
209+
<encoding>UTF-8</encoding>
210+
<fork>true</fork>
211+
<compilerArgs>
212+
<arg>-XDcompilePolicy=simple</arg>
213+
<arg>-Xplugin:ErrorProne</arg>
214+
<arg>-XDcompilePolicy=simple</arg>
215+
<arg>-Xplugin:ErrorProne</arg>
216+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
217+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
218+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
219+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
220+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
221+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED</arg>
222+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
223+
<arg>-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
224+
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
225+
<arg>-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
226+
</compilerArgs>
227+
<annotationProcessorPaths>
228+
<path>
229+
<groupId>com.google.errorprone</groupId>
230+
<artifactId>error_prone_core</artifactId>
231+
<version>2.17.0</version>
232+
</path>
233+
<path>
234+
<groupId>org.projectlombok</groupId>
235+
<artifactId>lombok</artifactId>
236+
<version>1.18.24</version>
237+
</path>
238+
</annotationProcessorPaths>
203239
</configuration>
204240
</plugin>
205241
<plugin>
@@ -279,6 +315,7 @@
279315
<artifactId>dependency-check-maven</artifactId>
280316
<version>7.1.1</version>
281317
<configuration>
318+
<suppressionFile>dependency-check-suppressions.xml</suppressionFile>
282319
<failBuildOnCVSS>7</failBuildOnCVSS>
283320
<junitFailOnCVSS>7</junitFailOnCVSS>
284321
</configuration>

src/main/java/com/easypost/Constants.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import com.easypost.model.ErrorDeserializer;
66
import com.easypost.model.SmartrateCollection;
77
import com.easypost.model.SmartrateCollectionDeserializer;
8+
import com.google.common.collect.ImmutableList;
89
import com.google.gson.FieldNamingPolicy;
910
import com.google.gson.Gson;
1011
import com.google.gson.GsonBuilder;
1112

12-
import java.util.ArrayList;
1313
import java.util.HashMap;
1414
import java.util.List;
1515

@@ -52,10 +52,8 @@ public abstract static class ErrorCodes {
5252
}
5353

5454
public abstract static class CarrierAccountTypes {
55-
public static final List<String> CARRIER_TYPES_WITH_CUSTOM_WORKFLOW = new ArrayList<String>() {{
56-
add("FedexAccount");
57-
add("UpsAccount");
58-
}};
55+
public static final List<String> CARRIER_TYPES_WITH_CUSTOM_WORKFLOW = ImmutableList.of("FedexAccount",
56+
"UpsAccount");
5957
}
6058

6159
public abstract static class Http {
@@ -64,11 +62,11 @@ public abstract static class Http {
6462
public static final int DEFAULT_CONNECT_TIMEOUT_MILLISECONDS = 30000;
6563
public static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60000;
6664

67-
public static final Gson GSON =
68-
new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
69-
.registerTypeAdapter(HashMap.class, new HashMapSerializer())
70-
.registerTypeAdapter(SmartrateCollection.class, new SmartrateCollectionDeserializer())
71-
.registerTypeAdapter(Error.class, new ErrorDeserializer()).create();
65+
public static final Gson GSON = new GsonBuilder()
66+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
67+
.registerTypeAdapter(HashMap.class, new HashMapSerializer())
68+
.registerTypeAdapter(SmartrateCollection.class, new SmartrateCollectionDeserializer())
69+
.registerTypeAdapter(Error.class, new ErrorDeserializer()).create();
7270
public static final Gson PRETTY_PRINT_GSON = new GsonBuilder().setPrettyPrinting().serializeNulls()
7371
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
7472
}

src/main/java/com/easypost/utils/Cryptography.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static String hexEncodeToString(byte @NotNull [] bytes) {
5656
* @return Hex digest of data.
5757
*/
5858
public static String toHMACSHA256HexDigest(byte @NotNull [] data, @NotNull String key,
59-
@Nullable Normalizer.Form normalizationForm) {
59+
Normalizer.@Nullable Form normalizationForm) {
6060
if (normalizationForm != null) {
6161
key = Normalizer.normalize(key, normalizationForm);
6262
}

src/test/java/com/easypost/CarrierAccountTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.easypost.exception.API.InvalidRequestError;
55
import com.easypost.model.CarrierAccount;
66
import com.easypost.model.CarrierType;
7+
import com.google.common.collect.ImmutableMap;
8+
79
import org.junit.jupiter.api.AfterEach;
810
import org.junit.jupiter.api.BeforeAll;
911
import org.junit.jupiter.api.Test;
@@ -75,9 +77,7 @@ public void testCreateWithCustomWorkflow() throws EasyPostException {
7577

7678
Map<String, Object> data = new HashMap<>();
7779
data.put("type", "FedexAccount");
78-
data.put("registration_data", new HashMap<String, Object>() {{
79-
put("some", "data");
80-
}});
80+
data.put("registration_data", ImmutableMap.of("some", "data"));
8181

8282
try {
8383
CarrierAccount carrierAccount = vcr.client.carrierAccount.create(data);

src/test/java/com/easypost/ErrorTest.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,28 @@ public void testError() throws EasyPostException {
6666
*/
6767
@Test
6868
public void testKnownApiException() throws EasyPostException {
69-
HashMap<Integer, Class<?>> apiErrorsMap = new HashMap<Integer, Class<?>>() {{
70-
put(300, RedirectError.class);
71-
put(301, RedirectError.class);
72-
put(302, RedirectError.class);
73-
put(303, RedirectError.class);
74-
put(304, RedirectError.class);
75-
put(305, RedirectError.class);
76-
put(306, RedirectError.class);
77-
put(307, RedirectError.class);
78-
put(308, RedirectError.class);
79-
put(401, UnauthorizedError.class);
80-
put(402, PaymentError.class);
81-
put(403, ForbiddenError.class);
82-
put(404, NotFoundError.class);
83-
put(405, MethodNotAllowedError.class);
84-
put(408, TimeoutError.class);
85-
put(422, InvalidRequestError.class);
86-
put(429, RateLimitError.class);
87-
put(444, UnknownApiError.class);
88-
put(500, InternalServerError.class);
89-
put(503, ServiceUnavailableError.class);
90-
put(504, GatewayTimeoutError.class);
91-
}};
69+
HashMap<Integer, Class<?>> apiErrorsMap = new HashMap<Integer, Class<?>>();
70+
apiErrorsMap.put(300, RedirectError.class);
71+
apiErrorsMap.put(301, RedirectError.class);
72+
apiErrorsMap.put(302, RedirectError.class);
73+
apiErrorsMap.put(303, RedirectError.class);
74+
apiErrorsMap.put(304, RedirectError.class);
75+
apiErrorsMap.put(305, RedirectError.class);
76+
apiErrorsMap.put(306, RedirectError.class);
77+
apiErrorsMap.put(307, RedirectError.class);
78+
apiErrorsMap.put(308, RedirectError.class);
79+
apiErrorsMap.put(401, UnauthorizedError.class);
80+
apiErrorsMap.put(402, PaymentError.class);
81+
apiErrorsMap.put(403, ForbiddenError.class);
82+
apiErrorsMap.put(404, NotFoundError.class);
83+
apiErrorsMap.put(405, MethodNotAllowedError.class);
84+
apiErrorsMap.put(408, TimeoutError.class);
85+
apiErrorsMap.put(422, InvalidRequestError.class);
86+
apiErrorsMap.put(429, RateLimitError.class);
87+
apiErrorsMap.put(444, UnknownApiError.class);
88+
apiErrorsMap.put(500, InternalServerError.class);
89+
apiErrorsMap.put(503, ServiceUnavailableError.class);
90+
apiErrorsMap.put(504, GatewayTimeoutError.class);
9291

9392
for (Map.Entry<Integer, Class<?>> entry: apiErrorsMap.entrySet()) {
9493
APIException exception = assertThrows(APIException.class,

src/test/java/com/easypost/Fixtures.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.easypost;
22

33
import com.easypost.fixtures.FixtureStructure;
4+
import com.google.common.collect.ImmutableList;
45
import com.google.gson.Gson;
56
import com.google.gson.reflect.TypeToken;
67

@@ -10,7 +11,6 @@
1011
import java.nio.file.Files;
1112
import java.nio.file.Path;
1213
import java.nio.file.Paths;
13-
import java.util.ArrayList;
1414
import java.util.HashMap;
1515
import java.util.Objects;
1616

@@ -206,16 +206,15 @@ public static HashMap<String, Object> fullShipment() {
206206
* @return The default one-call-buy shipment
207207
*/
208208
public static HashMap<String, Object> oneCallBuyShipment() {
209-
return new HashMap<String, Object>() {{
210-
put("to_address", caAddress1());
211-
put("from_address", caAddress2());
212-
put("parcel", basicParcel());
213-
put("service", uspsService());
214-
put("carrier_accounts", new ArrayList<String>() {{
215-
add(uspsCarrierAccountID());
216-
}});
217-
put("carrier", usps());
218-
}};
209+
HashMap<String, Object> values = new HashMap<>();
210+
values.put("to_address", caAddress1());
211+
values.put("from_address", caAddress2());
212+
values.put("parcel", basicParcel());
213+
values.put("service", uspsService());
214+
values.put("carrier_accounts", ImmutableList.of(uspsCarrierAccountID()));
215+
values.put("carrier", usps());
216+
217+
return values;
219218
}
220219

221220
/**

src/test/java/com/easypost/TestUtils.java

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import com.easypost.easyvcr.TimeFrame;
1111
import com.easypost.exception.General.MissingParameterError;
1212
import com.easypost.service.EasyPostClient;
13+
import com.google.common.collect.ImmutableList;
1314

1415
import java.io.File;
1516
import java.io.IOException;
1617
import java.nio.file.Path;
1718
import java.nio.file.Paths;
18-
import java.util.ArrayList;
1919
import java.util.List;
2020

2121
public abstract class TestUtils {
@@ -28,37 +28,21 @@ public enum ApiKey {
2828

2929
private static final String API_KEY_FAILED_TO_PULL = "couldNotPullApiKey";
3030
private static final String CASSETTES_PATH = "src/test/cassettes";
31-
private static final java.util.List<String> HEADER_CENSORS = new ArrayList<String>() {
32-
{
33-
add("Authorization");
34-
add("User-Agent");
35-
}
36-
};
37-
private static final List<String> QUERY_CENSORS = new ArrayList<String>() {
38-
{
39-
add("card[cvc]");
40-
add("card[number]");
41-
}
42-
};
43-
private static final List<String> BODY_CENSORS = new ArrayList<String>() {
44-
{
45-
add("api_keys");
46-
add("client_ip");
47-
add("credentials");
48-
add("key");
49-
add("keys");
50-
add("phone_number");
51-
add("phone");
52-
add("test_credentials");
53-
}
54-
};
55-
private static final List<CensorElement> BODY_ELEMENTS_TO_IGNORE_ON_MATCH = new ArrayList<CensorElement>() {
56-
{
57-
// Timezone difference between machines causing failure on replay
58-
add(new CensorElement("createdAt", false));
59-
add(new CensorElement("updatedAt", false));
60-
}
61-
};
31+
private static final List<String> HEADER_CENSORS = ImmutableList.of("Authorization", "User-Agent");
32+
private static final List<String> QUERY_CENSORS = ImmutableList.of("card[cvc]", "card[number]");
33+
private static final List<String> BODY_CENSORS = ImmutableList.of(
34+
"api_keys",
35+
"client_ip",
36+
"credentials",
37+
"key",
38+
"keys",
39+
"phone_number",
40+
"phone",
41+
"test_credentials"
42+
);
43+
private static final List<CensorElement> BODY_ELEMENTS_TO_IGNORE_ON_MATCH = ImmutableList.of(
44+
new CensorElement("createdAt", false),
45+
new CensorElement("updatedAt", false));
6246

6347
/**
6448
* Get the directory where the program is currently executing.
@@ -144,9 +128,10 @@ public boolean isRecording() {
144128
}
145129

146130
/**
147-
* Constructor.
131+
* Constructor.
132+
*
148133
* @throws MissingParameterError
149-
*/
134+
*/
150135
public VCR() throws MissingParameterError {
151136
this(null, ApiKey.TEST);
152137
}
@@ -238,7 +223,7 @@ public void setUpTest(String cassetteName) throws MissingParameterError {
238223
public void setUpTest(String cassetteName, String overrideApiKey) throws MissingParameterError {
239224
// override api key if needed
240225
client = new EasyPostClient(overrideApiKey.isEmpty() ? this.apiKey : overrideApiKey);
241-
226+
242227
// set up cassette
243228
Cassette cassette = new Cassette(testCassettesFolder, cassetteName);
244229

0 commit comments

Comments
 (0)