-
Notifications
You must be signed in to change notification settings - Fork 25k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new IsAfterAssertion for yaml rest tests (#103122)
Adds a new assertion "is_after", which can be used in the yaml based rest tests to check, whether one instant comes after the other.
- Loading branch information
Showing
8 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...-rest-runner/src/main/java/org/elasticsearch/test/rest/yaml/section/IsAfterAssertion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.test.rest.yaml.section; | ||
|
||
import org.elasticsearch.core.Tuple; | ||
import org.elasticsearch.logging.LogManager; | ||
import org.elasticsearch.logging.Logger; | ||
import org.elasticsearch.xcontent.XContentLocation; | ||
import org.elasticsearch.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.time.format.DateTimeParseException; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Represents an is after assert section: | ||
* | ||
* - is_after: { result.some_instant: "2023-05-25T12:30:00.000Z" } | ||
* | ||
*/ | ||
public class IsAfterAssertion extends Assertion { | ||
|
||
public static IsAfterAssertion parse(XContentParser parser) throws IOException { | ||
XContentLocation location = parser.getTokenLocation(); | ||
Tuple<String, Object> stringObjectTuple = ParserUtils.parseTuple(parser); | ||
return new IsAfterAssertion(location, stringObjectTuple.v1(), stringObjectTuple.v2()); | ||
} | ||
|
||
private static final Logger logger = LogManager.getLogger(IsAfterAssertion.class); | ||
|
||
public IsAfterAssertion(XContentLocation location, String field, Object expectedValue) { | ||
super(location, field, expectedValue); | ||
} | ||
|
||
@Override | ||
protected void doAssert(Object actualValue, Object expectedValue) { | ||
assertNotNull("field [" + getField() + "] is null", actualValue); | ||
assertNotNull("value to test against cannot be null", expectedValue); | ||
|
||
Instant fieldInstant = parseToInstant( | ||
actualValue.toString(), | ||
"field [" + getField() + "] cannot be parsed to " + Instant.class.getSimpleName() + ", got [" + actualValue + "]" | ||
); | ||
Instant valueInstant = parseToInstant( | ||
expectedValue.toString(), | ||
"value to test against [" + expectedValue + "] cannot be parsed to " + Instant.class.getSimpleName() | ||
); | ||
|
||
logger.trace("assert that [{}] is after [{}] (field [{}])", fieldInstant, valueInstant); | ||
assertTrue("field [" + getField() + "] should be after [" + actualValue + "], but was not", fieldInstant.isAfter(valueInstant)); | ||
} | ||
|
||
private Instant parseToInstant(String string, String onErrorMessage) { | ||
try { | ||
return Instant.parse(string); | ||
} catch (DateTimeParseException e) { | ||
throw new AssertionError(onErrorMessage, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...-runner/src/test/java/org/elasticsearch/test/rest/yaml/section/IsAfterAssertionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.test.rest.yaml.section; | ||
|
||
import org.elasticsearch.xcontent.XContentLocation; | ||
|
||
import java.io.IOException; | ||
|
||
public class IsAfterAssertionTests extends AbstractClientYamlTestFragmentParserTestCase { | ||
|
||
public void testParseIsAfterAssertionWithNonInstantValue() { | ||
XContentLocation xContentLocation = new XContentLocation(0, 0); | ||
IsAfterAssertion isAfterAssertion = new IsAfterAssertion(xContentLocation, "some_field", "non instant value"); | ||
|
||
expectThrows(AssertionError.class, () -> isAfterAssertion.doAssert("2022-05-25T12:30:00.000Z", "non instant value")); | ||
} | ||
|
||
public void testIsAfter() { | ||
String field = "some_field"; | ||
|
||
// actual value one year after value to test against | ||
String actualValue = "2022-05-25T12:30:00.000Z"; | ||
String expectedValue = "2021-05-25T12:30:00.000Z"; | ||
|
||
XContentLocation xContentLocation = new XContentLocation(0, 0); | ||
IsAfterAssertion isAfterAssertion = new IsAfterAssertion(xContentLocation, field, expectedValue); | ||
|
||
isAfterAssertion.doAssert(actualValue, expectedValue); | ||
} | ||
|
||
public void testIsNotAfter() { | ||
String field = "some_field"; | ||
|
||
// actual value one year before value to test against | ||
String actualValue = "2020-05-25T12:30:00.000Z"; | ||
String expectedValue = "2021-05-25T12:30:00.000Z"; | ||
|
||
XContentLocation xContentLocation = new XContentLocation(0, 0); | ||
IsAfterAssertion isAfterAssertion = new IsAfterAssertion(xContentLocation, field, expectedValue); | ||
|
||
expectThrows(AssertionError.class, () -> isAfterAssertion.doAssert(actualValue, expectedValue)); | ||
} | ||
|
||
public void testActualValueIsNull() { | ||
XContentLocation xContentLocation = new XContentLocation(0, 0); | ||
IsAfterAssertion isAfterAssertion = new IsAfterAssertion(xContentLocation, "field", "2021-05-25T12:30:00.000Z"); | ||
|
||
expectThrows(AssertionError.class, () -> isAfterAssertion.doAssert(null, "2021-05-25T12:30:00.000Z")); | ||
} | ||
|
||
public void testExpectedValueIsNull() throws IOException { | ||
XContentLocation xContentLocation = new XContentLocation(0, 0); | ||
IsAfterAssertion isAfterAssertion = new IsAfterAssertion(xContentLocation, "field", "2021-05-25T12:30:00.000Z"); | ||
|
||
expectThrows(AssertionError.class, () -> isAfterAssertion.doAssert("2021-05-25T12:30:00.000Z", null)); | ||
} | ||
} |