Skip to content

Commit 3807ef8

Browse files
authored
[fix] ID field usage in string representation of EasyPost objects (#311)
- Fix ID field usage in string representation of EasyPost objects - Add tests to verify string representation and pretty print of objects
1 parent 9ea0bce commit 3807ef8

File tree

5 files changed

+295
-25
lines changed

5 files changed

+295
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
## Next Release
44

5-
- Fix payment method funding and deletion failures due to undetermined payment method type
65
- Adds `refund` function in Insurance service for requesting a refund for a standalone insurance
6+
- Fix payment method funding and deletion failures due to undetermined payment method type
7+
- Fix `toString` method for all EasyPost models
78

89
## v7.1.1 (2024-03-21)
910

src/main/java/com/easypost/model/EasyPostResource.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
package com.easypost.model;
1010

11-
import java.lang.reflect.Field;
12-
1311
import java.util.Date;
1412

1513
import com.easypost.Constants;
@@ -30,27 +28,10 @@ public abstract class EasyPostResource {
3028
*/
3129
@Override
3230
public String toString() {
33-
return (String) this.getIdString();
34-
}
35-
36-
/**
37-
* Get the ID of the object.
38-
*
39-
* @return ID of the object.
40-
*/
41-
private Object getIdString() {
42-
try {
43-
Field idField = this.getClass().getDeclaredField("id");
44-
return idField.get(this);
45-
} catch (SecurityException e) {
46-
return "";
47-
} catch (NoSuchFieldException e) {
48-
return "";
49-
} catch (IllegalArgumentException e) {
50-
return "";
51-
} catch (IllegalAccessException e) {
52-
return "";
31+
if (this.id == null) {
32+
return String.format("<%s@%s>", this.getClass().getName(), System.identityHashCode(this));
5333
}
34+
return String.format("<%s@%s id=%s>", this.getClass().getName(), System.identityHashCode(this), this.id);
5435
}
5536

5637
/**
@@ -59,8 +40,9 @@ private Object getIdString() {
5940
* @return the JSON representation of the object.
6041
*/
6142
public String prettyPrint() {
62-
return String.format("<%s@%s id=%s> JSON: %s", this.getClass().getName(), System.identityHashCode(this),
63-
this.getIdString(), Constants.Http.PRETTY_PRINT_GSON.toJson(this));
43+
String identifier = this.toString();
44+
String json = Constants.Http.PRETTY_PRINT_GSON.toJson(this);
45+
return String.format("%s JSON: %s", identifier, json);
6446
}
6547

6648
/**

src/test/cassettes/easypost_resource/pretty_print.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/test/cassettes/easypost_resource/to_string.json

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.easypost;
2+
3+
import com.easypost.exception.EasyPostException;
4+
import com.easypost.model.Address;
5+
import com.google.common.collect.ImmutableList;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
public final class EasyPostResourceTest {
15+
private static TestUtils.VCR vcr;
16+
17+
/**
18+
* Set up the testing environment for this file.
19+
*
20+
* @throws EasyPostException when the request fails.
21+
*/
22+
@BeforeAll
23+
public static void setup() throws EasyPostException {
24+
vcr = new TestUtils.VCR("easypost_resource", TestUtils.ApiKey.TEST);
25+
}
26+
27+
/**
28+
* Test string representation of an EasyPostResource.
29+
*
30+
* @throws EasyPostException when the request fails.
31+
*/
32+
@Test
33+
public void testToString() throws EasyPostException {
34+
vcr.setUpTest("to_string");
35+
36+
Address address = vcr.client.address.create(Fixtures.caAddress1());
37+
38+
String stringRepresentation = address.toString();
39+
List<String> substringsToContain = new ArrayList<String>(ImmutableList.of(
40+
"<",
41+
address.getClass().getName(),
42+
"@",
43+
"" + System.identityHashCode(address), // cast to string
44+
" id=",
45+
address.getId(),
46+
">"
47+
));
48+
49+
// Regex matching doesn't work in run mode, only in debug mode (timing issue?)
50+
boolean matches = false;
51+
52+
for (String substring : substringsToContain) {
53+
matches = stringRepresentation.contains(substring);
54+
if (!matches) {
55+
break;
56+
}
57+
}
58+
59+
assertTrue(matches);
60+
}
61+
62+
/**
63+
* Test pretty print of an EasyPostResource.
64+
*
65+
* @throws EasyPostException when the request fails.
66+
*/
67+
@Test
68+
public void testPrettyPrint() throws EasyPostException {
69+
vcr.setUpTest("pretty_print");
70+
71+
Address address = vcr.client.address.create(Fixtures.caAddress1());
72+
73+
String prettyPrint = address.prettyPrint();
74+
75+
List<String> substringsToContain = new ArrayList<String>(ImmutableList.of(
76+
"<",
77+
address.getClass().getName(),
78+
"@",
79+
"" + System.identityHashCode(address), // cast to string
80+
" id=",
81+
address.getId(),
82+
">",
83+
" JSON: {",
84+
"}"
85+
));
86+
87+
// Regex matching doesn't work in run mode, only in debug mode (timing issue?)
88+
boolean matches = false;
89+
90+
for (String substring : substringsToContain) {
91+
matches = prettyPrint.contains(substring);
92+
if (!matches) {
93+
break;
94+
}
95+
}
96+
97+
assertTrue(matches);
98+
}
99+
}

0 commit comments

Comments
 (0)