Skip to content

Commit 20b9c22

Browse files
authored
Merge pull request #662 from MikeEdgar/issue-661
Relax assertions for default header, request body, and response styles
2 parents 112b7b1 + d544d00 commit 20b9c22

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

tck/src/main/java/org/eclipse/microprofile/openapi/tck/AirlinesAppTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.eclipse.microprofile.openapi.tck;
1818

19+
import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.hasOptionalEntry;
1920
import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.itemOrSingleton;
2021
import static org.hamcrest.Matchers.allOf;
2122
import static org.hamcrest.Matchers.anEmptyMap;
@@ -408,8 +409,7 @@ private void testBookingIdMethods(ValidatableResponse vr) {
408409
}
409410

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

415415
private void testAvailabilityGetParamater(ValidatableResponse vr) {
@@ -690,7 +690,7 @@ public void testEncodingRequestBody(String type) {
690690
ValidatableResponse vr = callEndpoint(type);
691691
String s = "paths.'/user'.post.requestBody.content.'application/json'.encoding.email.";
692692
vr.body(s + "contentType", equalTo("text/plain"));
693-
vr.body(s + "style", equalTo("form"));
693+
vr.body(s, hasOptionalEntry("style", "form"));
694694
vr.body(s + "explode", equalTo(true));
695695
vr.body(s + "allowReserved", equalTo(true));
696696
vr.body(s + "x-encoding", equalTo("test-encoding"));
@@ -702,13 +702,13 @@ public void testEncodingResponses(String type) {
702702
String s =
703703
"paths.'/user/username/{username}'.put.responses.'200'.content.'application/json'.encoding.password.";
704704
vr.body(s + "contentType", equalTo("text/plain"));
705-
vr.body(s + "style", equalTo("form"));
705+
vr.body(s, hasOptionalEntry("style", "form"));
706706
vr.body(s + "explode", equalTo(true));
707707
vr.body(s + "allowReserved", equalTo(true));
708708

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

@@ -930,7 +930,7 @@ public void testHeaderInAPIResponse(String type) {
930930
vr.body(responseHeader1 + ".required", equalTo(true));
931931
vr.body(responseHeader1 + ".deprecated", equalTo(true));
932932
vr.body(responseHeader1 + ".allowEmptyValue", equalTo(true));
933-
vr.body(responseHeader1 + ".style", equalTo("simple"));
933+
vr.body(responseHeader1, hasOptionalEntry("style", "simple"));
934934
vr.body(responseHeader1 + ".schema.type", itemOrSingleton("integer"));
935935

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

@@ -955,7 +955,7 @@ public void testHeaderInEncoding(String type) {
955955
vr.body(testHeader + ".required", equalTo(true));
956956
vr.body(testHeader + ".deprecated", equalTo(true));
957957
vr.body(testHeader + ".allowEmptyValue", equalTo(true));
958-
vr.body(testHeader + ".style", equalTo("simple"));
958+
vr.body(testHeader, hasOptionalEntry("style", "simple"));
959959
vr.body(testHeader + ".schema.type", itemOrSingleton("integer"));
960960
}
961961

@@ -988,7 +988,7 @@ public void testHeaderInComponents(String type) {
988988
vr.body(maxRate + ".required", equalTo(true));
989989
vr.body(maxRate + ".deprecated", equalTo(true));
990990
vr.body(maxRate + ".allowEmptyValue", equalTo(true));
991-
vr.body(maxRate + ".style", equalTo("simple"));
991+
vr.body(maxRate, hasOptionalEntry("style", "simple"));
992992
vr.body(maxRate + ".schema.type", itemOrSingleton("integer"));
993993
vr.body(maxRate + ".x-header", equalTo("test-header"));
994994
}

tck/src/main/java/org/eclipse/microprofile/openapi/tck/FilterTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.eclipse.microprofile.openapi.tck;
1818

19+
import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.hasOptionalEntry;
1920
import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.itemOrSingleton;
2021
import static org.hamcrest.Matchers.both;
2122
import static org.hamcrest.Matchers.contains;
@@ -161,7 +162,7 @@ public void testFilterHeader(String type) {
161162
vr.body(maxRate + ".required", equalTo(true));
162163
vr.body(maxRate + ".deprecated", equalTo(true));
163164
vr.body(maxRate + ".allowEmptyValue", equalTo(true));
164-
vr.body(maxRate + ".style", equalTo("simple"));
165+
vr.body(maxRate, hasOptionalEntry("style", "simple"));
165166
vr.body(maxRate + ".schema.type", itemOrSingleton("integer"));
166167
}
167168

tck/src/main/java/org/eclipse/microprofile/openapi/tck/utils/TCKMatchers.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
*/
1616
package org.eclipse.microprofile.openapi.tck.utils;
1717

18+
import static org.hamcrest.Matchers.allOf;
19+
import static org.hamcrest.Matchers.either;
1820
import static org.hamcrest.Matchers.equalTo;
21+
import static org.hamcrest.Matchers.hasEntry;
22+
import static org.hamcrest.Matchers.hasKey;
23+
import static org.hamcrest.Matchers.isA;
24+
import static org.hamcrest.Matchers.not;
1925
import static org.hamcrest.comparator.ComparatorMatcherBuilder.comparedBy;
2026

2127
import java.math.BigDecimal;
@@ -169,4 +175,21 @@ private static BigDecimal toBigDecimal(Number number) {
169175
}
170176

171177
}
178+
179+
/**
180+
* Creates a matcher which matches a map entry with an allowed value or the absence of a map entry from the object.
181+
*
182+
* @param entryName
183+
* name of the entry in the map
184+
* @param value
185+
* a single allowed value in the map entry, if present
186+
* @return the matcher
187+
*/
188+
@SuppressWarnings("unchecked")
189+
public static <T> Matcher<T> hasOptionalEntry(String entryName, Object value) {
190+
Matcher<T> hasEntry = (Matcher<T>) hasEntry(entryName, value);
191+
Matcher<T> entryMissing = (Matcher<T>) not(hasKey(entryName));
192+
193+
return allOf(isA(java.util.Map.class), either(hasEntry).or(entryMissing));
194+
}
172195
}

0 commit comments

Comments
 (0)