Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ void legacyInvalidParamProblem() throws IOException {
assertThat(writeProblem(badRequestProblem)).isEqualToIgnoringWhitespace(json);
}

@Test
void titleAndStatusOverride() throws IOException {
String json = "{\n"
+ " \"type\": \"urn:problem-type:belgif:badRequest\",\n"
+ " \"href\": \"https://www.test.be\",\n"
+ " \"title\": \"TEST\",\n"
+ " \"status\": 401,\n"
+ " \"detail\": \"Test\"\n"
+ "}";
Problem problem = readProblem(json);
assertThat(problem).isInstanceOf(BadRequestProblem.class);
BadRequestProblem badRequestProblem = (BadRequestProblem) problem;
assertThat(badRequestProblem.getType()).isEqualTo(BadRequestProblem.TYPE_URI);
assertThat(badRequestProblem.getHref()).isEqualTo(URI.create("https://www.test.be"));
assertThat(badRequestProblem.getStatus()).isEqualTo(BadRequestProblem.STATUS);
assertThat(badRequestProblem.getTitle()).isEqualTo(BadRequestProblem.TITLE);
}

@Test
void unknownProblemWithMessage() throws IOException {
String json = "{\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ public void jacksonMismatchedInputFromBackend(String client) {
.body("type", equalTo("urn:problem-type:belgif:internalServerError"));
}

@ParameterizedTest
@MethodSource("getClients")
public void inconsistentProblemStatusFromBackend(String client) {
getSpec().when().queryParam("client", client)
.get("/inconsistentProblemStatusFromBackend").then().assertThat()
.statusCode(400)
.body("type", equalTo("urn:problem-type:belgif:badRequest"))
.body("detail", equalTo("Bad Request with inconsistent problem status code"
+ " (caught successfully by frontend)"))
.body("status", equalTo(400));
}

@Test
public void notFound() {
getSpec().when().get("/not/found").then().assertThat()
Expand Down Expand Up @@ -562,4 +574,13 @@ public void invalidJsonType() {
.body("issues[0].value", equalTo("twenty-two"));
}

@ParameterizedTest
@MethodSource("getClients")
public void healthDown(String client) {
getSpec().when().queryParam("client", client)
.get("/healthDown").then().assertThat()
.statusCode(503)
.body("status", equalTo("DOWN"));
}

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

/**
* Problem payload constants.
*/
public class ProblemPayloads {

public static final String INCONSISTENT_STATUS_CODE = "{\n"
+ " \"type\": \"urn:problem-type:belgif:badRequest\",\n"
+ " \"href\": \"https://www.belgif.be/specification/rest/api-guide/problems/badRequest.html\",\n"
+ " \"title\": \"Bad Request\",\n"
+ " \"status\": 401,\n" // <- 401 instead of 400
+ " \"detail\": \"Bad Request with inconsistent problem status code\"\n"
+ "}";

private ProblemPayloads() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
<scope>provided</scope>
<version>3.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.19</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public interface Backend {
@Path("/jacksonMismatchedInput")
Response jacksonMismatchedInput();

@GET
@Path("/inconsistentProblemStatus")
Response inconsistentProblemStatus();

@GET
@Path("/healthDown")
Response healthDown();

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

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import jakarta.enterprise.context.RequestScoped;
import jakarta.ws.rs.core.MediaType;
Expand All @@ -9,6 +11,7 @@
import com.acme.custom.CustomProblem;

import io.github.belgif.rest.problem.api.Problem;
import io.github.belgif.rest.problem.it.ProblemPayloads;
import io.github.belgif.rest.problem.it.model.JacksonModel;

@RequestScoped
Expand Down Expand Up @@ -56,4 +59,22 @@ public Response jacksonMismatchedInput() {
return Response.ok(model, MediaType.APPLICATION_JSON_TYPE).build();
}

@Override
public Response inconsistentProblemStatus() {
return Response.status(402) // -> HTTP status code 402 instead of 400
.type("application/problem+json")
.entity(ProblemPayloads.INCONSISTENT_STATUS_CODE) // -> problem status code 401 instead of 400
.build();
}

@Override
public Response healthDown() {
Map<String, String> response = new HashMap<>();
response.put("status", "DOWN");
return Response.status(Response.Status.SERVICE_UNAVAILABLE)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(response)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public interface Frontend {
@Path("/jacksonMismatchedInputFromBackend")
Response jacksonMismatchedInputFromBackend(@QueryParam("client") Client client);

@GET
@Path("/inconsistentProblemStatusFromBackend")
Response inconsistentProblemStatusFromBackend(@QueryParam("client") Client client);

@GET
@Path("/beanValidation/queryParameter")
Response beanValidationQueryParameter(@QueryParam("param") @NotNull @Positive Integer p,
Expand Down Expand Up @@ -118,4 +122,8 @@ Response beanValidationQueryParameter(@QueryParam("param") @NotNull @Positive In
@Path("/jackson/mismatchedInputException")
Response jacksonMismatchedInputException(@Valid JacksonModel body);

@GET
@Path("/healthDown")
Response healthDown(@QueryParam("client") Client client);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.acme.custom.CustomProblem;

Expand All @@ -37,6 +40,8 @@
@Path("/frontend")
public class FrontendImpl implements Frontend {

private static final Logger LOGGER = LoggerFactory.getLogger(FrontendImpl.class);

private static final URI BASE_URI =
URI.create("http://" + System.getProperty("jboss.bind.address") + ":8080/rest-problem");

Expand Down Expand Up @@ -266,6 +271,67 @@ public Response jacksonMismatchedInputFromBackend(Client client) {
throw new IllegalStateException("Unsupported client " + client);
}

@Override
public Response inconsistentProblemStatusFromBackend(Client client) {
try {
if (client == null || client == Client.MICROPROFILE) {
return microprofileClient.inconsistentProblemStatus();
} else if (client == Client.JAXRS) {
return jaxRsClient.target(BASE_URI).path("backend/inconsistentProblemStatus").request().get();
} else if (client == Client.JAXRS_ASYNC) {
try {
jaxRsClient.target(BASE_URI).path("backend/inconsistentProblemStatus").request().async().get()
.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
} else if (client == Client.RESTEASY) {
return resteasyClient.target(BASE_URI).path("backend/inconsistentProblemStatus").request().get();
} else if (client == Client.RESTEASY_PROXY) {
return resteasyProxyClient.inconsistentProblemStatus();
}
throw new IllegalStateException("Unsupported client " + client);
} catch (BadRequestProblem e) {
e.setDetail(e.getDetail() + " (caught successfully by frontend)");
throw e;
}
}

@Override
public Response healthDown(@QueryParam("client") Client client) {
try {
if (client == null || client == Client.MICROPROFILE) {
return microprofileClient.healthDown();
} else if (client == Client.JAXRS) {
return jaxRsClient.target(BASE_URI).path("backend/healthDown").request().get();
} else if (client == Client.JAXRS_ASYNC) {
try {
return jaxRsClient.target(BASE_URI).path("backend/healthDown").request().async()
.get().get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
} else if (client == Client.RESTEASY) {
return resteasyClient.target(BASE_URI).path("backend/healthDown").request().get();
} else if (client == Client.RESTEASY_PROXY) {
return resteasyProxyClient.healthDown();
}
} catch (WebApplicationException e) {
if (e.getResponse().getStatus() == Response.Status.SERVICE_UNAVAILABLE.getStatusCode()) {
LOGGER.info("Caught WebApplicationException with status 503");
return e.getResponse();
}
throw e;
}
throw new IllegalStateException("Unsupported client " + client);
}

@Override
public Response beanValidationQueryParameter(Integer p, String o) {
return Response.ok("param: " + p + ", other: " + o).build();
Expand Down
6 changes: 6 additions & 0 deletions belgif-rest-problem-it/belgif-rest-problem-java-ee-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
<version>8.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.19</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client-microprofile</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public interface Backend {
@Path("/jacksonMismatchedInput")
Response jacksonMismatchedInput();

@GET
@Path("/inconsistentProblemStatus")
Response inconsistentProblemStatus();

@GET
@Path("/healthDown")
Response healthDown();

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

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.RequestScoped;
import javax.ws.rs.core.MediaType;
Expand All @@ -9,6 +11,7 @@
import com.acme.custom.CustomProblem;

import io.github.belgif.rest.problem.api.Problem;
import io.github.belgif.rest.problem.it.ProblemPayloads;
import io.github.belgif.rest.problem.it.model.JacksonModel;

@RequestScoped
Expand Down Expand Up @@ -56,4 +59,22 @@ public Response jacksonMismatchedInput() {
return Response.ok(model, MediaType.APPLICATION_JSON_TYPE).build();
}

@Override
public Response inconsistentProblemStatus() {
return Response.status(402) // -> HTTP status code 402 instead of 400
.type("application/problem+json")
.entity(ProblemPayloads.INCONSISTENT_STATUS_CODE) // -> problem status code 401 instead of 400
.build();
}

@Override
public Response healthDown() {
Map<String, String> response = new HashMap<>();
response.put("status", "DOWN");
return Response.status(Response.Status.SERVICE_UNAVAILABLE)
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(response)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public interface Frontend {
@Path("/jacksonMismatchedInputFromBackend")
Response jacksonMismatchedInputFromBackend(@QueryParam("client") Client client);

@GET
@Path("/inconsistentProblemStatusFromBackend")
Response inconsistentProblemStatusFromBackend(@QueryParam("client") Client client);

@GET
@Path("/beanValidation/queryParameter")
Response beanValidationQueryParameter(@QueryParam("param") @NotNull @Positive Integer p,
Expand Down Expand Up @@ -118,4 +122,8 @@ Response beanValidationQueryParameter(@QueryParam("param") @NotNull @Positive In
@Path("/jackson/mismatchedInputException")
Response jacksonMismatchedInputException(@Valid JacksonModel body);

@GET
@Path("/healthDown")
Response healthDown(@QueryParam("client") Client client);

}
Loading
Loading