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

Relax assertions for default header, request body, and response styles #662

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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 @@ -16,6 +16,7 @@

package org.eclipse.microprofile.openapi.tck;

import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.hasOptionalEntry;
import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.itemOrSingleton;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anEmptyMap;
Expand Down Expand Up @@ -408,8 +409,7 @@ private void testBookingIdMethods(ValidatableResponse vr) {
}

bookingParameters = "paths.'/bookings/{id}'.get.parameters";
vr.body(bookingParameters + ".findAll { it.name == 'id' }.style", both(hasSize(1)).and(contains("simple")));

vr.body(bookingParameters + ".findAll { it.name == 'id' }", contains(hasOptionalEntry("style", "simple")));
}

private void testAvailabilityGetParamater(ValidatableResponse vr) {
Expand Down Expand Up @@ -690,7 +690,7 @@ public void testEncodingRequestBody(String type) {
ValidatableResponse vr = callEndpoint(type);
String s = "paths.'/user'.post.requestBody.content.'application/json'.encoding.email.";
vr.body(s + "contentType", equalTo("text/plain"));
vr.body(s + "style", equalTo("form"));
vr.body(s, hasOptionalEntry("style", "form"));
vr.body(s + "explode", equalTo(true));
vr.body(s + "allowReserved", equalTo(true));
vr.body(s + "x-encoding", equalTo("test-encoding"));
Expand All @@ -702,13 +702,13 @@ public void testEncodingResponses(String type) {
String s =
"paths.'/user/username/{username}'.put.responses.'200'.content.'application/json'.encoding.password.";
vr.body(s + "contentType", equalTo("text/plain"));
vr.body(s + "style", equalTo("form"));
vr.body(s, hasOptionalEntry("style", "form"));
MikeEdgar marked this conversation as resolved.
Show resolved Hide resolved
vr.body(s + "explode", equalTo(true));
vr.body(s + "allowReserved", equalTo(true));

String t = "paths.'/user/username/{username}'.put.responses.'200'.content.'application/xml'.encoding.password.";
vr.body(t + "contentType", equalTo("text/plain"));
vr.body(t + "style", equalTo("form"));
vr.body(t, hasOptionalEntry("style", "form"));
vr.body(t + "explode", equalTo(true));
vr.body(t + "allowReserved", equalTo(true));

Expand Down Expand Up @@ -930,7 +930,7 @@ public void testHeaderInAPIResponse(String type) {
vr.body(responseHeader1 + ".required", equalTo(true));
vr.body(responseHeader1 + ".deprecated", equalTo(true));
vr.body(responseHeader1 + ".allowEmptyValue", equalTo(true));
vr.body(responseHeader1 + ".style", equalTo("simple"));
vr.body(responseHeader1, hasOptionalEntry("style", "simple"));
vr.body(responseHeader1 + ".schema.type", itemOrSingleton("integer"));

String responseHeader2 = "paths.'/reviews/{id}'.get.responses.'200'.headers.responseHeader2";
Expand All @@ -939,7 +939,7 @@ public void testHeaderInAPIResponse(String type) {
vr.body(responseHeader2 + ".required", equalTo(true));
vr.body(responseHeader2 + ".deprecated", equalTo(true));
vr.body(responseHeader2 + ".allowEmptyValue", equalTo(true));
vr.body(responseHeader2 + ".style", equalTo("simple"));
vr.body(responseHeader2, hasOptionalEntry("style", "simple"));
vr.body(responseHeader2 + ".schema.type", itemOrSingleton("string"));
}

Expand All @@ -955,7 +955,7 @@ public void testHeaderInEncoding(String type) {
vr.body(testHeader + ".required", equalTo(true));
vr.body(testHeader + ".deprecated", equalTo(true));
vr.body(testHeader + ".allowEmptyValue", equalTo(true));
vr.body(testHeader + ".style", equalTo("simple"));
vr.body(testHeader, hasOptionalEntry("style", "simple"));
vr.body(testHeader + ".schema.type", itemOrSingleton("integer"));
}

Expand Down Expand Up @@ -988,7 +988,7 @@ public void testHeaderInComponents(String type) {
vr.body(maxRate + ".required", equalTo(true));
vr.body(maxRate + ".deprecated", equalTo(true));
vr.body(maxRate + ".allowEmptyValue", equalTo(true));
vr.body(maxRate + ".style", equalTo("simple"));
vr.body(maxRate, hasOptionalEntry("style", "simple"));
vr.body(maxRate + ".schema.type", itemOrSingleton("integer"));
vr.body(maxRate + ".x-header", equalTo("test-header"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.eclipse.microprofile.openapi.tck;

import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.hasOptionalEntry;
import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.itemOrSingleton;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.contains;
Expand Down Expand Up @@ -161,7 +162,7 @@ public void testFilterHeader(String type) {
vr.body(maxRate + ".required", equalTo(true));
vr.body(maxRate + ".deprecated", equalTo(true));
vr.body(maxRate + ".allowEmptyValue", equalTo(true));
vr.body(maxRate + ".style", equalTo("simple"));
vr.body(maxRate, hasOptionalEntry("style", "simple"));
vr.body(maxRate + ".schema.type", itemOrSingleton("integer"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
*/
package org.eclipse.microprofile.openapi.tck.utils;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.comparator.ComparatorMatcherBuilder.comparedBy;

import java.math.BigDecimal;
Expand Down Expand Up @@ -169,4 +175,21 @@ private static BigDecimal toBigDecimal(Number number) {
}

}

/**
* Creates a matcher which matches a map entry with an allowed value or the absence of a map entry from the object.
*
* @param entryName
* name of the entry in the map
* @param value
* a single allowed value in the map entry, if present
* @return the matcher
*/
@SuppressWarnings("unchecked")
public static <T> Matcher<T> hasOptionalEntry(String entryName, Object value) {
Matcher<T> hasEntry = (Matcher<T>) hasEntry(entryName, value);
Matcher<T> entryMissing = (Matcher<T>) not(hasKey(entryName));
MikeEdgar marked this conversation as resolved.
Show resolved Hide resolved

return allOf(isA(java.util.Map.class), either(hasEntry).or(entryMissing));
}
}