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

Add fluent problem properties #132

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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 @@ -2,6 +2,7 @@

import java.net.URI;

import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.ProblemType;
import io.github.belgif.rest.problem.api.ServerProblem;

Expand All @@ -12,7 +13,7 @@
* https://www.belgif.be/specification/rest/api-guide/#bad-gateway</a>
*/
@ProblemType(BadGatewayProblem.TYPE)
public class BadGatewayProblem extends ServerProblem {
public class BadGatewayProblem extends ServerProblem implements FluentProblem<BadGatewayProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.URI;
import java.util.List;

import io.github.belgif.rest.problem.api.FluentInputValidationProblem;
import io.github.belgif.rest.problem.api.InputValidationIssue;
import io.github.belgif.rest.problem.api.InputValidationProblem;
import io.github.belgif.rest.problem.api.InvalidParam;
Expand All @@ -16,7 +17,8 @@
* @see InputValidationProblem
*/
@ProblemType(BadRequestProblem.TYPE)
public class BadRequestProblem extends InputValidationProblem {
public class BadRequestProblem extends InputValidationProblem
implements FluentInputValidationProblem<BadRequestProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.Problem;
import io.github.belgif.rest.problem.api.ProblemType;

Expand All @@ -16,7 +17,7 @@
* with the matching problem type URI has been found on the classpath.
* </p>
*/
public class DefaultProblem extends Problem {
public class DefaultProblem extends Problem implements FluentProblem<DefaultProblem> {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

import io.github.belgif.rest.problem.api.ClientProblem;
import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.HttpResponseHeaders;
import io.github.belgif.rest.problem.api.ProblemType;

Expand All @@ -15,7 +16,8 @@
* https://www.belgif.be/specification/rest/api-guide/#expired-access-token</a>
*/
@ProblemType(ExpiredAccessTokenProblem.TYPE)
public class ExpiredAccessTokenProblem extends ClientProblem implements HttpResponseHeaders {
public class ExpiredAccessTokenProblem extends ClientProblem
implements FluentProblem<ExpiredAccessTokenProblem>, HttpResponseHeaders {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;

import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.ProblemType;
import io.github.belgif.rest.problem.api.ServerProblem;

Expand All @@ -13,7 +14,7 @@
* https://www.belgif.be/specification/rest/api-guide/#internal-server-error</a>
*/
@ProblemType(InternalServerErrorProblem.TYPE)
public class InternalServerErrorProblem extends ServerProblem {
public class InternalServerErrorProblem extends ServerProblem implements FluentProblem<InternalServerErrorProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Objects;

import io.github.belgif.rest.problem.api.ClientProblem;
import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.HttpResponseHeaders;
import io.github.belgif.rest.problem.api.ProblemType;

Expand All @@ -16,7 +17,8 @@
* https://www.belgif.be/specification/rest/api-guide/#invalid-access-token</a>
*/
@ProblemType(InvalidAccessTokenProblem.TYPE)
public class InvalidAccessTokenProblem extends ClientProblem implements HttpResponseHeaders {
public class InvalidAccessTokenProblem extends ClientProblem
implements FluentProblem<InvalidAccessTokenProblem>, HttpResponseHeaders {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;

import io.github.belgif.rest.problem.api.FluentInputValidationProblem;
import io.github.belgif.rest.problem.api.InputValidationIssue;
import io.github.belgif.rest.problem.api.InputValidationProblem;
import io.github.belgif.rest.problem.api.ProblemType;
Expand All @@ -13,7 +14,8 @@
* https://www.belgif.be/specification/rest/api-guide/#missing-permission</a>
*/
@ProblemType(MissingPermissionProblem.TYPE)
public class MissingPermissionProblem extends InputValidationProblem {
public class MissingPermissionProblem extends InputValidationProblem
implements FluentInputValidationProblem<MissingPermissionProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.fasterxml.jackson.annotation.JsonSetter;

import io.github.belgif.rest.problem.api.ClientProblem;
import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.HttpResponseHeaders;
import io.github.belgif.rest.problem.api.ProblemType;

Expand All @@ -21,7 +22,8 @@
* https://www.belgif.be/specification/rest/api-guide/#missing-scope</a>
*/
@ProblemType(MissingScopeProblem.TYPE)
public class MissingScopeProblem extends ClientProblem implements HttpResponseHeaders {
public class MissingScopeProblem extends ClientProblem
implements FluentProblem<MissingScopeProblem>, HttpResponseHeaders {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

import io.github.belgif.rest.problem.api.ClientProblem;
import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.HttpResponseHeaders;
import io.github.belgif.rest.problem.api.ProblemType;

Expand All @@ -15,7 +16,8 @@
* https://www.belgif.be/specification/rest/api-guide/#no-access-token</a>
*/
@ProblemType(NoAccessTokenProblem.TYPE)
public class NoAccessTokenProblem extends ClientProblem implements HttpResponseHeaders {
public class NoAccessTokenProblem extends ClientProblem
implements FluentProblem<NoAccessTokenProblem>, HttpResponseHeaders {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Objects;

import io.github.belgif.rest.problem.api.ClientProblem;
import io.github.belgif.rest.problem.api.FluentProblem;
import io.github.belgif.rest.problem.api.ProblemType;

/**
Expand All @@ -13,7 +14,7 @@
* https://www.belgif.be/specification/rest/api-guide/#payloadTooLargeProblem</a>
*/
@ProblemType(PayloadTooLargeProblem.TYPE)
public class PayloadTooLargeProblem extends ClientProblem {
public class PayloadTooLargeProblem extends ClientProblem implements FluentProblem<PayloadTooLargeProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;

import io.github.belgif.rest.problem.api.FluentInputValidationProblem;
import io.github.belgif.rest.problem.api.InEnum;
import io.github.belgif.rest.problem.api.InputValidationIssue;
import io.github.belgif.rest.problem.api.InputValidationProblem;
Expand All @@ -16,7 +17,8 @@
* @see InputValidationProblem
*/
@ProblemType(ResourceNotFoundProblem.TYPE)
public class ResourceNotFoundProblem extends InputValidationProblem {
public class ResourceNotFoundProblem extends InputValidationProblem
implements FluentInputValidationProblem<ResourceNotFoundProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;

import io.github.belgif.rest.problem.api.FluentRetryAfterProblem;
import io.github.belgif.rest.problem.api.ProblemType;
import io.github.belgif.rest.problem.api.RetryAfterServerProblem;

Expand All @@ -12,7 +13,8 @@
* https://www.belgif.be/specification/rest/api-guide/#service-unavailable</a>
*/
@ProblemType(ServiceUnavailableProblem.TYPE)
public class ServiceUnavailableProblem extends RetryAfterServerProblem {
public class ServiceUnavailableProblem extends RetryAfterServerProblem
implements FluentRetryAfterProblem<ServiceUnavailableProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.URI;
import java.util.Objects;

import io.github.belgif.rest.problem.api.FluentRetryAfterProblem;
import io.github.belgif.rest.problem.api.ProblemType;
import io.github.belgif.rest.problem.api.RetryAfterClientProblem;

Expand All @@ -14,7 +15,8 @@
* https://www.belgif.be/specification/rest/api-guide/#too-many-failed-requests</a>
*/
@ProblemType(TooManyFailedRequestsProblem.TYPE)
public class TooManyFailedRequestsProblem extends RetryAfterClientProblem {
public class TooManyFailedRequestsProblem extends RetryAfterClientProblem
implements FluentRetryAfterProblem<TooManyFailedRequestsProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.URI;
import java.util.Objects;

import io.github.belgif.rest.problem.api.FluentRetryAfterProblem;
import io.github.belgif.rest.problem.api.ProblemType;
import io.github.belgif.rest.problem.api.RetryAfterClientProblem;

Expand All @@ -13,7 +14,8 @@
* https://www.belgif.be/specification/rest/api-guide/#too-many-requests</a>
*/
@ProblemType(TooManyRequestsProblem.TYPE)
public class TooManyRequestsProblem extends RetryAfterClientProblem {
public class TooManyRequestsProblem extends RetryAfterClientProblem
implements FluentRetryAfterProblem<TooManyRequestsProblem> {

/**
* The problem type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.belgif.rest.problem.api;

import java.util.List;

/**
* Provides default methods with fluent InputValidationProblem properties (issues).
*
* @param <SELF> the concrete InputValidationProblem self-type
*/
@SuppressWarnings("unchecked")
public interface FluentInputValidationProblem<SELF extends InputValidationProblem & FluentInputValidationProblem<SELF>>
extends FluentProblem<SELF> {

void setIssues(InputValidationIssue... issues);

void setIssues(List<InputValidationIssue> issues);

default SELF issues(InputValidationIssue... issues) {
setIssues(issues);
return (SELF) this;
}

default SELF issues(List<InputValidationIssue> issues) {
setIssues(issues);
return (SELF) this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.belgif.rest.problem.api;

import java.net.URI;

/**
* Provides default methods with fluent Problem properties (detail, href, instance, additionalProperty).
*
* @param <SELF> the concrete Problem self-type
*/
@SuppressWarnings("unchecked")
public interface FluentProblem<SELF extends Problem & FluentProblem<SELF>> {

void setDetail(String detail);

void setHref(URI href);

void setInstance(URI instance);

void setAdditionalProperty(String name, Object value);

default SELF detail(String detail) {
setDetail(detail);
return (SELF) this;
}

default SELF href(URI href) {
setHref(href);
return (SELF) this;
}

default SELF instance(URI instance) {
setInstance(instance);
return (SELF) this;
}

default SELF additionalProperty(String name, Object value) {
setAdditionalProperty(name, value);
return (SELF) this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.belgif.rest.problem.api;

import java.time.OffsetDateTime;

/**
* Provides default methods with fluent RetryAfter problem properties (retryAfter, retryAfterSec).
*
* @param <SELF> the concrete RetryAfter Problem self-type
*/
@SuppressWarnings("unchecked")
public interface FluentRetryAfterProblem<SELF extends Problem & RetryAfter & FluentRetryAfterProblem<SELF>>
extends FluentProblem<SELF> {

void setRetryAfter(OffsetDateTime retryAfter);

void setRetryAfterSec(Long retryAfterSec);

default SELF retryAfter(OffsetDateTime retryAfter) {
setRetryAfter(retryAfter);
return (SELF) this;
}

default SELF retryAfterSec(Long retryAfterSec) {
setRetryAfterSec(retryAfterSec);
return (SELF) this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import static org.assertj.core.api.Assertions.*;

import java.net.URI;
import java.util.Collections;

import org.junit.jupiter.api.Test;

import io.github.belgif.rest.problem.api.InEnum;
import io.github.belgif.rest.problem.api.InputValidationIssue;
import io.github.belgif.rest.problem.api.InputValidationIssues;
import io.github.belgif.rest.problem.api.InvalidParam;
import io.github.belgif.rest.problem.api.ProblemType;

Expand Down Expand Up @@ -72,6 +74,21 @@ void problemTypeAnnotation() {
.isEqualTo("urn:problem-type:belgif:badRequest");
}

@Test
void fluentProperties() {
BadRequestProblem problem = new BadRequestProblem()
.detail("detail")
.issues(InputValidationIssues.requiredInput(InEnum.QUERY, "test"))
.href(URI.create("href"))
.instance(URI.create("instance"))
.additionalProperty("foo", "bar");
assertThat(problem.getDetail()).isEqualTo("detail");
assertThat(problem.getHref()).hasToString("href");
assertThat(problem.getInstance()).hasToString("instance");
assertThat(problem.getAdditionalProperties()).containsEntry("foo", "bar");
assertThat(problem.getIssues()).containsExactly(InputValidationIssues.requiredInput(InEnum.QUERY, "test"));
}

@Test
void equalsAndHashCode() {
InputValidationIssue issue = new InputValidationIssue(InEnum.QUERY, "test");
Expand Down
Loading