Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static class SampleParam implements Request.Param {
String slug = "abc";
}

// Checks that path variables are replaced with matching parameter fields
@Test
void replacePathVariablesSubstitutesFields() throws Exception {
Operation op = new Operation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

class RequestParamTest {

// Ensures a request parameter defaults to using the PATH kind
@Test
void defaultKindIsPath() {
Request.Param param = new Request.Param() {};
assertThat(param.getKind()).isEqualTo(Request.Param.Kind.PATH);
}

// Verifies enum access and placeholder classes work as expected
@Test
void enumValuesAndVoidClasses() {
assertThat(Request.Param.Kind.valueOf("QUERY")).isEqualTo(Request.Param.Kind.QUERY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class RequestTest {

// Ensures the default getResponse implementation throws an exception
@Test
void defaultGetResponseThrowsUnsupportedOperationException() {
Request<Request.Param, Response> request = new Request<Request.Param, Response>() {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void setUp() {
configuration = new Configuration("test", url);
}

// Verifies all getter methods return expected defaults or file values
@Test
void keysAndGettersHandleDefaultsProperly() {
assertThat(configuration.keys()).containsExactlyInAnyOrder("string", "int", "long", "double", "boolean");
Expand Down Expand Up @@ -47,12 +48,14 @@ void keysAndGettersHandleDefaultsProperly() {
assertThat(configuration.getBoolean("boolean", false)).isTrue();
}

// Ensures the default constructor loads properties from the classpath
@Test
void defaultConstructorLoadsAppProperties() {
Configuration cfg = new Configuration("test");
assertThat(cfg.get("string")).isEqualTo("fromFile");
}

// Confirms environment variables take precedence over property files
@Test
void environmentVariablesOverrideProperties() throws Exception {
String classpath = System.getProperty("java.class.path");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class ConstantsTest {

// Validates that HTTP method constants expose the correct strings
@Test
void httpMethodConstantsMatchValues() {
assertThat(Constants.HTTP_GET_METHOD).isEqualTo("GET");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class CreateInvoiceParamTest {

// Ensures toString properly URL-encodes provided values
@Test
void toStringEncodesValues() throws MalformedURLException {
CreateInvoiceParam param = new CreateInvoiceParam();
Expand All @@ -26,6 +27,7 @@ void toStringEncodesValues() throws MalformedURLException {
.contains("webhookUrl=https%3A%2F%2Fexample.com%2Fhook%3Ffoo%3Dbar%26baz%3Dqux");
}

// Verifies getters, setters and toString when a webhook URL is supplied
@Test
void settersGettersAndToStringWithWebhook() throws Exception {
CreateInvoiceParam param = new CreateInvoiceParam();
Expand All @@ -46,6 +48,7 @@ void settersGettersAndToStringWithWebhook() throws Exception {
"description=desc&amountSat=1&expirySeconds=2&externalId=id&webhookUrl=https://example.com");
}

// Confirms toString omits the webhook URL when it is not set
@Test
void toStringWithoutWebhook() {
CreateInvoiceParam param = new CreateInvoiceParam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class DecodeInvoiceParamTest {

// Ensures the invoice field is handled correctly in getters and toString
@Test
void settersGettersAndToString() {
DecodeInvoiceParam param = new DecodeInvoiceParam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class PayBolt11InvoiceParamTest {

// Validates getters, setters and string representation of the param
@Test
void settersGettersAndToString() {
PayBolt11InvoiceParam param = new PayBolt11InvoiceParam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class PayLightningAddressParamTest {

// Ensures setters, getters, and toString handle a message field
@Test
void settersGettersAndToStringWithMessage() {
PayLightningAddressParam param = new PayLightningAddressParam();
Expand All @@ -19,6 +20,7 @@ void settersGettersAndToStringWithMessage() {
assertThat(param.toString()).isEqualTo("amountSat=70&address=addr&message=hello");
}

// Confirms toString omits the message when it is empty or null
@Test
void toStringWithoutMessage() {
PayLightningAddressParam param = new PayLightningAddressParam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class CreateInvoiceResponseTest {

// Validates fields and string representation for invoice creation responses
@Test
void settersGettersAndToString() {
CreateInvoiceResponse response = new CreateInvoiceResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class DecodeInvoiceResponseTest {

// Checks getters, setters, and string output for complex invoice responses
@Test
void settersGettersAndToString() {
DecodeInvoiceResponse response = new DecodeInvoiceResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class GetLightningAddressResponseTest {

// Ensures lightning addresses are handled and formatted correctly
@Test
void settersGettersAndToString() {
GetLightningAddressResponse response = new GetLightningAddressResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class PayBolt11InvoiceInvoiceResponseTest {

// Validates Bolt11 payment response fields and string output
@Test
void settersGettersAndToString() {
PayBolt11InvoiceInvoiceResponse response = new PayBolt11InvoiceInvoiceResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class PayInvoiceResponseTest {

// Confirms fields and string output for generic payment invoices
@Test
void settersGettersAndToString() {
PayInvoiceResponse response = new PayInvoiceResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class PayLightningAddressInvoiceResponseTest {

// Checks values and string output for lightning address payment invoices
@Test
void settersGettersAndToString() {
PayLightningAddressInvoiceResponse response = new PayLightningAddressInvoiceResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.Test;
import xyz.tcheeric.phoenixd.operation.impl.GetOperation;
import xyz.tcheeric.phoenixd.operation.impl.PostOperation;

import java.net.http.HttpRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class AbstractOperationTest {

// Confirms operations execute once and capture the response body
@Test
void executeMakesSingleNetworkCall() throws Exception {
MockWebServer server = new MockWebServer();
Expand All @@ -31,4 +34,36 @@ void executeMakesSingleNetworkCall() throws Exception {
server.shutdown();
}
}

// Ensures non-success HTTP status codes trigger exceptions
@Test
void executeThrowsOnNon2xxResponse() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setResponseCode(400));
server.start();
try {
HttpRequest request = HttpRequest.newBuilder(server.url("/fail").uri()).GET().build();
GetOperation operation = new GetOperation(request);
assertThatThrownBy(operation::execute).isInstanceOf(Exception.class);
} finally {
server.shutdown();
}
}

// Verifies adding a header replaces any existing value
@Test
void addHeaderReplacesExisting() {
PostOperation op = new PostOperation("/items", "data");
String original = op.getHeader("Authorization");
op.addHeader("Authorization", "Basic new");
assertThat(op.getHeader("Authorization")).isEqualTo("Basic new");
assertThat(original).isNotEqualTo(op.getHeader("Authorization"));
}

// Checks that removing headers from a POST operation is unsupported
@Test
void removeHeaderUnsupported() {
PostOperation op = new PostOperation("/items", "data");
assertThatThrownBy(() -> op.removeHeader("X")).isInstanceOf(UnsupportedOperationException.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package xyz.tcheeric.phoenixd.operation;

import org.junit.jupiter.api.Test;
import xyz.tcheeric.phoenixd.common.rest.Request;
import xyz.tcheeric.phoenixd.operation.impl.DeleteOperation;
import xyz.tcheeric.phoenixd.operation.impl.PatchOperation;
import xyz.tcheeric.phoenixd.operation.impl.PostOperation;
import xyz.tcheeric.phoenixd.operation.impl.GetOperation;

import java.net.URI;
import java.net.http.HttpRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class OperationsTest {

static class PathParam implements Request.Param {
String id = "123";
@Override
public String toString() {
return "id=" + id;
}
}

// Verifies POST operations set content type and expand path variables
@Test
void postOperationSetsContentTypeAndResolvesPath() {
PostOperation op = new PostOperation("/items/{id}", new PathParam(), "data");
assertThat(op.getHeader("Content-Type")).isEqualTo("application/x-www-form-urlencoded");
assertThat(op.getHttpRequest().uri().getPath()).isEqualTo("/items/123");
}

// Ensures POST operations with body still assign content type header
@Test
void postOperationWithBodyOnlySetsHeader() {
PostOperation op = new PostOperation("/items", "data");
assertThat(op.getHeader("Content-Type")).isEqualTo("application/x-www-form-urlencoded");
}

// Confirms constructing a POST operation from an HttpRequest retains the path
@Test
void postOperationFromHttpRequest() {
HttpRequest request = HttpRequest.newBuilder(URI.create("http://localhost/post"))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
PostOperation op = new PostOperation(request);
assertThat(op.getHttpRequest().uri().getPath()).isEqualTo("/post");
}

// Checks that PATCH operations support adding headers
@Test
void patchOperationAddsHeader() {
PatchOperation op = new PatchOperation("/patch");
op.addHeader("X-Test", "value");
assertThat(op.getHeader("X-Test")).isEqualTo("value");
}

// Validates removing headers from PATCH operations is unsupported
@Test
void patchOperationRemoveHeaderThrows() {
PatchOperation op = new PatchOperation("/patch");
assertThatThrownBy(() -> op.removeHeader("X"))
.isInstanceOf(UnsupportedOperationException.class);
}

// Ensures DELETE operations allow removal of default headers
@Test
void deleteOperationRemovesHeader() {
DeleteOperation op = new DeleteOperation("/delete");
assertThat(op.getHeader("Authorization")).isNotNull();
op.removeHeader("Authorization");
assertThat(op.getHeader("Authorization")).isNull();
}

// Verifies DELETE operations resolve path variables from parameters
@Test
void deleteOperationWithParamResolvesPath() {
DeleteOperation op = new DeleteOperation("/items/{id}", new PathParam());
assertThat(op.getHttpRequest().uri().getPath()).isEqualTo("/items/123");
}

// Confirms DELETE operations built from HttpRequest keep the same path
@Test
void deleteOperationFromHttpRequest() {
HttpRequest request = HttpRequest.newBuilder(URI.create("http://localhost/delete"))
.DELETE()
.build();
DeleteOperation op = new DeleteOperation(request);
assertThat(op.getHttpRequest().uri().getPath()).isEqualTo("/delete");
}

// Checks GET operations append query parameters for QUERY-kind params
@Test
void getOperationWithQueryParamAppendsQuery() {
class QueryParam extends PathParam {
@Override
public Request.Param.Kind getKind() {
return Request.Param.Kind.QUERY;
}
}
GetOperation op = new GetOperation("/query", new QueryParam());
assertThat(op.getHttpRequest().uri().getQuery()).isEqualTo("id=123");
}

// Ensures constructors validate against null arguments
@Test
void nullArgumentsThrow() {
assertThatThrownBy(() -> new PostOperation(null, "data")).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new PostOperation("/x", (Request.Param) null, "data")).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new PostOperation("/x", new PathParam(), null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new PatchOperation((String) null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new PatchOperation("/x", null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new DeleteOperation((String) null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new DeleteOperation("/x", null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new GetOperation((String) null)).isInstanceOf(NullPointerException.class);
assertThatThrownBy(() -> new GetOperation("/x", null)).isInstanceOf(NullPointerException.class);
}
}
Loading
Loading