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

Add general Number comparison Matcher, update pet store assertions #418

Merged
merged 1 commit into from
Apr 14, 2020
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 @@ -17,12 +17,13 @@
package org.eclipse.microprofile.openapi.tck;

import static io.restassured.RestAssured.given;
import static org.eclipse.microprofile.openapi.tck.utils.TCKMatchers.comparesEqualToNumber;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
Expand Down Expand Up @@ -60,11 +61,11 @@ public void testSchema(String type) {
vr.body("paths.'/store/order/{orderId}'.get.responses.'900'.schema", nullValue());

// Numerical properties
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.maximum", equalTo(101));
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.maximum", comparesEqualToNumber(101.0));
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.exclusiveMaximum", equalTo(true));
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.minimum", equalTo(9));
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.minimum", comparesEqualToNumber(9));
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.exclusiveMinimum", equalTo(true));
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.multipleOf", equalTo(10));
vr.body("paths.'/pet/{petId}'.get.parameters.find{ it.name == 'petId' }.schema.multipleOf", comparesEqualToNumber(10));

// String properties
vr.body("paths.'/pet/{petId}'.delete.parameters.find{ it.name == 'apiKey' }.schema.maxLength", equalTo(256));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2020 Contributors to the Eclipse Foundation
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http:https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.eclipse.microprofile.openapi.tck.utils;

import static org.hamcrest.comparator.ComparatorMatcherBuilder.comparedBy;

import java.math.BigDecimal;
import java.util.Comparator;

import org.hamcrest.Matcher;

public final class TCKMatchers {

private TCKMatchers() {
}

/**
* Compares two numbers as BigDecimals
*/
private static final Comparator<Number> NUMERIC_COMPARATOR = (value1, value2) -> {
final BigDecimal decimal1 = BigDecimal.valueOf(value1.doubleValue());
final BigDecimal decimal2 = BigDecimal.valueOf(value2.doubleValue());
return decimal1.compareTo(decimal2);
};

/**
* Creates a matcher of {@link Comparable Comparable&lt;Number&gt;} that
* matches when the examined number is equal to the specified value, as
* reported by the <code>compareTo</code> method of the {@link BigDecimal}s
* created by passing both Number values' {@link Number#doubleValue()
* doubleValue} to {@link BigDecimal#valueOf(double)}.
* <p>
* For example:
*
* <pre>
* assertThat(1, comparesEqualToNumber(1))
* </pre>
*
* @param expected
* the value which, when passed to the compareTo method of the
* examined object following conversion to BigDecimal, should
* return zero
* @return a matcher to test the equality of the examined Number
*/
public static Matcher<Number> comparesEqualToNumber(Number expected) {
return comparedBy(NUMERIC_COMPARATOR).comparesEqualTo(expected);
}
}