Skip to content

Commit

Permalink
[fix] ID field usage in string representation of EasyPost objects (#311)
Browse files Browse the repository at this point in the history
- Fix ID field usage in string representation of EasyPost objects
- Add tests to verify string representation and pretty print of objects
  • Loading branch information
nwithan8 committed Apr 10, 2024
1 parent 9ea0bce commit 3807ef8
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 25 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## Next Release

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

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

Expand Down
30 changes: 6 additions & 24 deletions src/main/java/com/easypost/model/EasyPostResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

package com.easypost.model;

import java.lang.reflect.Field;

import java.util.Date;

import com.easypost.Constants;
Expand All @@ -30,27 +28,10 @@ public abstract class EasyPostResource {
*/
@Override
public String toString() {
return (String) this.getIdString();
}

/**
* Get the ID of the object.
*
* @return ID of the object.
*/
private Object getIdString() {
try {
Field idField = this.getClass().getDeclaredField("id");
return idField.get(this);
} catch (SecurityException e) {
return "";
} catch (NoSuchFieldException e) {
return "";
} catch (IllegalArgumentException e) {
return "";
} catch (IllegalAccessException e) {
return "";
if (this.id == null) {
return String.format("<%s@%s>", this.getClass().getName(), System.identityHashCode(this));
}
return String.format("<%s@%s id=%s>", this.getClass().getName(), System.identityHashCode(this), this.id);
}

/**
Expand All @@ -59,8 +40,9 @@ private Object getIdString() {
* @return the JSON representation of the object.
*/
public String prettyPrint() {
return String.format("<%s@%s id=%s> JSON: %s", this.getClass().getName(), System.identityHashCode(this),
this.getIdString(), Constants.Http.PRETTY_PRINT_GSON.toJson(this));
String identifier = this.toString();
String json = Constants.Http.PRETTY_PRINT_GSON.toJson(this);
return String.format("%s JSON: %s", identifier, json);
}

/**
Expand Down
94 changes: 94 additions & 0 deletions src/test/cassettes/easypost_resource/pretty_print.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions src/test/cassettes/easypost_resource/to_string.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

99 changes: 99 additions & 0 deletions src/test/java/com/easypost/EasyPostResourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.easypost;

import com.easypost.exception.EasyPostException;
import com.easypost.model.Address;
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertTrue;

public final class EasyPostResourceTest {
private static TestUtils.VCR vcr;

/**
* Set up the testing environment for this file.
*
* @throws EasyPostException when the request fails.
*/
@BeforeAll
public static void setup() throws EasyPostException {
vcr = new TestUtils.VCR("easypost_resource", TestUtils.ApiKey.TEST);
}

/**
* Test string representation of an EasyPostResource.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testToString() throws EasyPostException {
vcr.setUpTest("to_string");

Address address = vcr.client.address.create(Fixtures.caAddress1());

String stringRepresentation = address.toString();
List<String> substringsToContain = new ArrayList<String>(ImmutableList.of(
"<",
address.getClass().getName(),
"@",
"" + System.identityHashCode(address), // cast to string
" id=",
address.getId(),
">"
));

// Regex matching doesn't work in run mode, only in debug mode (timing issue?)
boolean matches = false;

for (String substring : substringsToContain) {
matches = stringRepresentation.contains(substring);
if (!matches) {
break;
}
}

assertTrue(matches);
}

/**
* Test pretty print of an EasyPostResource.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testPrettyPrint() throws EasyPostException {
vcr.setUpTest("pretty_print");

Address address = vcr.client.address.create(Fixtures.caAddress1());

String prettyPrint = address.prettyPrint();

List<String> substringsToContain = new ArrayList<String>(ImmutableList.of(
"<",
address.getClass().getName(),
"@",
"" + System.identityHashCode(address), // cast to string
" id=",
address.getId(),
">",
" JSON: {",
"}"
));

// Regex matching doesn't work in run mode, only in debug mode (timing issue?)
boolean matches = false;

for (String substring : substringsToContain) {
matches = prettyPrint.contains(substring);
if (!matches) {
break;
}
}

assertTrue(matches);
}
}

0 comments on commit 3807ef8

Please sign in to comment.