Skip to content

Commit

Permalink
Improve test infra
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed Jan 16, 2024
1 parent 8cc9a64 commit 43d6ac9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 30 deletions.
64 changes: 51 additions & 13 deletions src/test/java/com/stripe/BaseStripeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import lombok.Cleanup;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -192,6 +193,27 @@ public static <T extends StripeObjectInterface> void verifyRequest(
RequestOptions options)
throws StripeException {

verifyRequest(
(req) -> {
assertEquals(baseAddress, req.getBaseAddress());
assertEquals(method, req.getMethod());
assertEquals(path, req.getPath());
if (params != null) {
final String msg =
String.format(
"Params did not match - expected: %s, received: %s", params, req.getParams());
assertTrue(msg, compareParamObjects(params, req.getParams()));
}
if (options != null) {
assertEquals(options, req.getOptions());
}
});
}

@SuppressWarnings("AssertionFailureIgnored")
public static <T extends StripeObjectInterface> void verifyRequest(
Consumer<ApiRequest> assertOnApiRequest) throws StripeException {

ArgumentCaptor<ApiRequest> requestCaptor = ArgumentCaptor.forClass(ApiRequest.class);
List<AssertionError> exceptions = new ArrayList<AssertionError>();
try {
Expand All @@ -203,19 +225,35 @@ public static <T extends StripeObjectInterface> void verifyRequest(

for (ApiRequest req : requestCaptor.getAllValues()) {
try {
assertEquals(baseAddress, req.getBaseAddress());
assertEquals(method, req.getMethod());
assertEquals(path, req.getPath());
if (params != null) {
final String msg =
String.format(
"Params did not match - expected: %s, received: %s", params, req.getParams());
assertTrue(msg, compareParamObjects(params, req.getParams()));
}
if (options != null) {
assertEquals(options, req.getOptions());
}
// If we get here, we have found a request that triggered no assertion failures.
assertOnApiRequest.accept(req);
return;
} catch (AssertionError e) {
exceptions.add(e);
}
}

// If we get here, each request failed an assertion.
if (exceptions.size() != 0) {
// Combine all exceptions into a single message
String msg = "";
for (AssertionError e : exceptions) {
msg += e.getMessage() + "\n\n";
}
throw new AssertionError(msg);
}
}

@SuppressWarnings("AssertionFailureIgnored")
public static <T extends StripeObjectInterface> void verifyStripeRequest(
Consumer<StripeRequest> assertOnStripeRequest) throws StripeException {

ArgumentCaptor<StripeRequest> requestCaptor = ArgumentCaptor.forClass(StripeRequest.class);
List<AssertionError> exceptions = new ArrayList<AssertionError>();
Mockito.verify(httpClientSpy, Mockito.atLeastOnce()).request(requestCaptor.capture());

for (StripeRequest req : requestCaptor.getAllValues()) {
try {
assertOnStripeRequest.accept(req);
return;
} catch (AssertionError e) {
exceptions.add(e);
Expand Down
33 changes: 16 additions & 17 deletions src/test/java/com/stripe/functional/StripeClientTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.stripe.functional;

import static org.junit.Assert.assertEquals;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.stripe.BaseStripeTest;
Expand All @@ -8,7 +10,6 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class StripeClientTest extends BaseStripeTest {

Expand All @@ -28,22 +29,20 @@ public void tearDown() {
@Test
public void testReportsUsageTelemetry() throws StripeException {
mockClient.customers().create();
Mockito.reset(httpClientSpy);
mockClient.customers().update("cus_xyz");
Mockito.verify(httpClientSpy)
.request(
Mockito.argThat(
stripeRequest -> {
String usage =
new Gson()
.fromJson(
stripeRequest.headers().firstValue("X-Stripe-Client-Telemetry").get(),
JsonObject.class)
.get("last_request_metrics")
.getAsJsonObject()
.get("usage")
.getAsString();
return usage.equals("stripe_client");
}));
verifyStripeRequest(
(stripeRequest) -> {
assert(stripeRequest.headers().firstValue("X-Stripe-Client-Telemetry").isPresent());
String usage =
new Gson()
.fromJson(
stripeRequest.headers().firstValue("X-Stripe-Client-Telemetry").get(),
JsonObject.class)
.get("last_request_metrics")
.getAsJsonObject()
.get("usage")
.getAsString();
assertEquals("stripe_client", usage);
});
}
}

0 comments on commit 43d6ac9

Please sign in to comment.