Skip to content

Commit

Permalink
Relax assertions for default header, request body, and response styles
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Oct 11, 2024
1 parent 112b7b1 commit 01ce093
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
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,7 +409,8 @@ 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' }",
both(hasSize(1)).and(hasOptionalEntry("style", "simple")));

}

Expand Down Expand Up @@ -690,7 +692,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 +704,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"));
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(s, hasOptionalEntry("style", "form"));
vr.body(t + "explode", equalTo(true));
vr.body(t + "allowReserved", equalTo(true));

Expand Down Expand Up @@ -930,7 +932,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 +941,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 +957,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 +990,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,11 @@
*/
package org.eclipse.microprofile.openapi.tck.utils;

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.not;
import static org.hamcrest.comparator.ComparatorMatcherBuilder.comparedBy;

import java.math.BigDecimal;
Expand Down Expand Up @@ -169,4 +173,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 validValue
* a single allowed value in the map entry, if present
* @return the matcher
*/
@SuppressWarnings("unchecked")
public static <T> Matcher<T> hasOptionalEntry(String entryName, Object validValue) {
Matcher<T> hasEntry = (Matcher<T>) hasEntry(entryName, validValue);
Matcher<T> entryMissing = (Matcher<T>) not(hasKey(entryName));

return either(hasEntry).or(entryMissing);
}
}

0 comments on commit 01ce093

Please sign in to comment.