Skip to content

Commit 6214c8c

Browse files
committed
Make OpenAPI spec conformance assertions more descriptive
Signed-off-by: nscuro <[email protected]>
1 parent 5194ee4 commit 6214c8c

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

apiserver/src/test/java/org/dependencytrack/resources/v2/OpenApiValidationClientResponseFilter.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
import io.swagger.v3.oas.models.responses.ApiResponse;
3636
import io.swagger.v3.parser.ObjectMapperFactory;
3737
import io.swagger.v3.parser.core.models.ParseOptions;
38-
import org.junit.Assert;
39-
4038
import jakarta.ws.rs.client.ClientRequestContext;
4139
import jakarta.ws.rs.client.ClientResponseContext;
4240
import jakarta.ws.rs.client.ClientResponseFilter;
41+
import org.junit.Assert;
42+
4343
import java.io.ByteArrayInputStream;
4444
import java.io.IOException;
4545
import java.io.InputStream;
@@ -96,19 +96,41 @@ public void filter(
9696

9797
// Identity the correct response object in the spec based on the status.
9898
final String responseStatus = String.valueOf(responseContext.getStatus());
99-
assertThat(operationDef.getResponses().keySet()).contains(responseStatus);
99+
assertThat(operationDef.getResponses().keySet())
100+
.as("""
101+
Got response with status %s, but the OpenAPI spec of \
102+
%s %s does not define it""",
103+
responseStatus,
104+
requestContext.getMethod(),
105+
requestContext.getUri())
106+
.contains(responseStatus);
100107
final ApiResponse responseDef = operationDef.getResponses().get(responseStatus);
101108

102109
// If the spec does not define a response, ensure that the actual
103110
// response is also empty.
104111
if (responseDef.getContent() == null) {
105-
assertThat(responseBytes).asString().isEmpty();
112+
assertThat(responseBytes).asString()
113+
.as("""
114+
Got response with content, but the OpenAPI spec of \
115+
%s %s -> %s does not define any""",
116+
requestContext.getMethod(),
117+
requestContext.getUri(),
118+
responseStatus)
119+
.isEmpty();
106120
return;
107121
}
108122

109123
// Identity the correct media type in the spec response.
110124
final String responseContentType = responseContext.getHeaderString("Content-Type");
111-
assertThat(responseDef.getContent().keySet()).contains(responseContentType);
125+
assertThat(responseDef.getContent().keySet())
126+
.as("""
127+
Got response with content-type %s, but the OpenAPI spec of \
128+
%s %s -> %s does not define any responses for it""",
129+
responseContentType,
130+
requestContext.getMethod(),
131+
requestContext.getUri(),
132+
responseStatus)
133+
.contains(responseContentType);
112134
final MediaType mediaType = responseDef.getContent().get(responseContentType);
113135

114136
// Serialize the response schema to JSON so it can be used for validation.
@@ -118,7 +140,15 @@ public void filter(
118140

119141
final Set<ValidationMessage> messages = schema.validate(
120142
new String(responseBytes), InputFormat.JSON);
121-
assertThat(messages).isEmpty();
143+
assertThat(messages)
144+
.as("""
145+
Got response content that failed to validate against the \
146+
OpenAPI spec of %s %s -> %s (%s)""",
147+
requestContext.getMethod(),
148+
requestContext.getUri(),
149+
responseStatus,
150+
responseContentType)
151+
.isEmpty();
122152
}
123153

124154
private Operation findOpenApiOperation(final ClientRequestContext requestContext) {

0 commit comments

Comments
 (0)