Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] ID field usage in string representation of EasyPost objects #311

Merged
merged 4 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading