Use AssertJ instead of the more obvious uses of Hamcrest.
Change-Id: I7ad281229f71132a60dc64b2f59c7cc5db502987
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
-import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.google.common.collect.Sets;
assertNotNull(schemaContext);
for (var expectedContainer : expectedContainers) {
- assertThat(String.format("Expected container %s not found.", expectedContainer),
- schemaContext.findDataTreeChild(QName.create(FOO_NS, expectedContainer)).orElseThrow(),
- instanceOf(ContainerSchemaNode.class));
+ assertInstanceOf(ContainerSchemaNode.class,
+ schemaContext.findDataTreeChild(QName.create(FOO_NS, expectedContainer)).orElseThrow());
}
for (var unexpectedContainer : Sets.difference(ALL_CONTAINERS, expectedContainers)) {
*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.containsString;
-import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
class Bug6878Test extends AbstractYangTest {
-
@Test
void testParsingXPathWithYang11Functions() {
- final String testLog = parseAndcaptureLog("/rfc7950/bug6878/foo.yang");
- assertFalse(testLog.contains("Could not find function: "));
+ final var testLog = parseAndcaptureLog("/rfc7950/bug6878/foo.yang");
+ assertThat(testLog).doesNotContain("Could not find function: ");
}
@Test
void shouldLogInvalidYang10XPath() {
- final String testLog = parseAndcaptureLog("/rfc7950/bug6878/foo10-invalid.yang");
- assertThat(testLog, containsString("RFC7950 features required in RFC6020 context to parse expression "));
+ final var testLog = parseAndcaptureLog("/rfc7950/bug6878/foo10-invalid.yang");
+ assertThat(testLog).contains("RFC7950 features required in RFC6020 context to parse expression ");
}
@Test
void shouldLogInvalidYang10XPath2() {
- final String testLog = parseAndcaptureLog("/rfc7950/bug6878/foo10-invalid-2.yang");
- assertThat(testLog, containsString("RFC7950 features required in RFC6020 context to parse expression "));
+ final var testLog = parseAndcaptureLog("/rfc7950/bug6878/foo10-invalid-2.yang");
+ assertThat(testLog).contains("RFC7950 features required in RFC6020 context to parse expression ");
}
@SuppressWarnings("checkstyle:regexpSinglelineJava")
private static String parseAndcaptureLog(final String yangFile) {
- final PrintStream stdout = System.out;
- final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ final var stdout = System.out;
+ final var output = new ByteArrayOutputStream();
- try (PrintStream out = new PrintStream(output, true, StandardCharsets.UTF_8)) {
+ try (var out = new PrintStream(output, true, StandardCharsets.UTF_8)) {
System.setOut(out);
assertEffectiveModel(yangFile);
} finally {
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
import org.opendaylight.yangtools.yang.parser.stmt.reactor.ReactorDeclaredModel;
class AugmentArgumentParsingTest {
@Test
void invalidAugEmptyTest() {
- final ReactorException ex = assertReactorThrows(INVALID_EMPTY);
- final Throwable cause = ex.getCause();
- assertInstanceOf(SourceException.class, cause);
- assertThat(cause.getMessage(), startsWith("Schema node identifier must not be empty"));
+ final var ex = assertReactorThrows(INVALID_EMPTY);
+ final var cause = assertInstanceOf(SourceException.class, ex.getCause());
+ assertThat(cause.getMessage()).startsWith("Schema node identifier must not be empty");
}
@Test
void invalidAugXPathTest() {
- final ReactorException ex = assertReactorThrows(INVALID_XPATH);
- final Throwable cause = ex.getCause();
- assertInstanceOf(SourceException.class, cause);
- assertThat(cause.getMessage(), startsWith("Failed to parse node '-' in path '/aug1/-'"));
+ final var ex = assertReactorThrows(INVALID_XPATH);
+ final var cause = assertInstanceOf(SourceException.class, ex.getCause());
+ assertThat(cause.getMessage()).startsWith("Failed to parse node '-' in path '/aug1/-'");
- final Throwable nested = cause.getCause();
- assertInstanceOf(SourceException.class, nested);
- assertThat(nested.getMessage(), startsWith("Invalid identifier '-'"));
+ final var nested = assertInstanceOf(SourceException.class, cause.getCause());
+ assertThat(nested.getMessage()).startsWith("Invalid identifier '-'");
}
private static ReactorException assertReactorThrows(final StatementStreamSource source) {
- final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(source);
+ final var reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(source);
return assertThrows(ReactorException.class, () -> reactor.build());
}
private static void assertSourceExceptionCause(final Throwable exception, final String start) {
- final Throwable cause = exception.getCause();
- assertInstanceOf(SourceException.class, cause);
- assertThat(cause.getMessage(), startsWith(start));
+ final var cause = assertInstanceOf(SourceException.class, exception.getCause());
+ assertThat(cause.getMessage()).startsWith(start);
}
}
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.allOf;
-import static org.hamcrest.CoreMatchers.containsString;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import org.junit.jupiter.api.Test;
class Bug4410Test extends AbstractYangTest {
@Test
void test() {
- final var cause = assertInferenceExceptionDir("/bugs/bug4410",
- startsWith("Yang model processing phase EFFECTIVE_MODEL failed [at ")).getCause();
- assertInstanceOf(InferenceException.class, cause);
- assertThat(cause.getMessage(), allOf(startsWith("Type [(foo)"), containsString("was not found")));
+ final var cause = assertInstanceOf(InferenceException.class, assertInferenceExceptionDir("/bugs/bug4410",
+ startsWith("Yang model processing phase EFFECTIVE_MODEL failed [at ")).getCause());
+ assertThat(cause.getMessage()).startsWith("Type [(foo)").contains("was not found");
}
}
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
class Bug6240Test extends AbstractYangTest {
assertNotNull(bar);
assertInstanceOf(ContainerSchemaNode.class, bar.getDataChildByName(QName.create(NS, REV, "foo-grp-con")));
- assertThat(bar.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")),
- instanceOf(ContainerSchemaNode.class));
+ assertInstanceOf(ContainerSchemaNode.class, bar.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")));
assertEquals(1, bar.getSubmodules().size());
- final DataSchemaNode dataChildByName = bar.getDataChildByName(QName.create(NS, REV, "sub-bar-con"));
- assertInstanceOf(ContainerSchemaNode.class, dataChildByName);
- final ContainerSchemaNode subBarCon = (ContainerSchemaNode) dataChildByName;
+ final var subBarCon = assertInstanceOf(ContainerSchemaNode.class,
+ bar.getDataChildByName(QName.create(NS, REV, "sub-bar-con")));
- assertThat(subBarCon.getDataChildByName(QName.create(NS, REV, "foo-grp-con")),
- instanceOf(ContainerSchemaNode.class));
- assertThat(subBarCon.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")),
- instanceOf(ContainerSchemaNode.class));
+ assertInstanceOf(ContainerSchemaNode.class, subBarCon.getDataChildByName(QName.create(NS, REV, "foo-grp-con")));
+ assertInstanceOf(ContainerSchemaNode.class,
+ subBarCon.getDataChildByName(QName.create(NS, REV, "sub-foo-grp-con")));
}
@Test
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
final var cause = assertThrows(IllegalArgumentException.class,
() -> StmtTestUtils.parseYangSources(sourceForResource("/bugs/bug7146/foo.yang"))).getCause();
assertInstanceOf(YangSyntaxErrorException.class, cause);
- assertThat(cause.getMessage(), containsString("extraneous input '#'"));
+ assertThat(cause.getMessage()).contains("extraneous input '#'");
}
}
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
() -> parseYangSources("/bugs/bug7480/files-2", "/bugs/bug7480/lib-2"));
final var message = ex.getSuppressed().length > 0 ? ex.getSuppressed()[0].getMessage()
: ex.getCause().getMessage();
- assertThat(message, startsWith("Imported module [missing-lib] was not found."));
+ assertThat(message).startsWith("Imported module [missing-lib] was not found.");
}
@Test
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
final var cause = assertInstanceOf(InferenceException.class,
assertThrows(ReactorException.class, reactor::buildEffective).getCause());
- assertThat(cause.getMessage(),
- startsWith("Deviation must not target the same module as the one it is defined in"));
+ assertThat(cause.getMessage())
+ .startsWith("Deviation must not target the same module as the one it is defined in");
}
@Test
final var cause = assertInstanceOf(InferenceException.class,
assertThrows(ReactorException.class, reactor::buildEffective).getCause());
- assertThat(cause.getMessage(),
- startsWith("Deviation must not target the same module as the one it is defined in"));
+ assertThat(cause.getMessage())
+ .startsWith("Deviation must not target the same module as the one it is defined in");
}
}
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.containsString;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
final var testLog = output.toString();
System.setOut(stdout);
- assertThat(testLog, containsString("""
+ assertThat(testLog).contains("""
Deviation cannot replace substatement (urn:ietf:params:xml:ns:yang:yin:1)default in target leaf-list \
- (bar?revision=2017-01-20)my-leaf-list because a leaf-list can have multiple default statements."""));
+ (bar?revision=2017-01-20)my-leaf-list because a leaf-list can have multiple default statements.""");
}
@Test
testLog = output.toString();
System.setOut(stdout);
- assertThat(testLog, containsString(
+ assertThat(testLog).contains(
"Deviation cannot delete substatement (urn:ietf:params:xml:ns:yang:yin:1)units with argument 'seconds' in "
- + "target node (bar?revision=2017-01-20)my-leaf because it does not exist in the target node."));
+ + "target node (bar?revision=2017-01-20)my-leaf because it does not exist in the target node.");
}
@Test
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
final var reactor = RFC7950Reactors.defaultReactor().newBuild().addSources(CYCLIC_IDENTITY_TEST);
final var cause = assertThrows(SomeModifiersUnresolvedException.class, reactor::buildEffective).getCause();
assertInstanceOf(InferenceException.class, cause);
- assertThat(cause.getMessage(), startsWith("Yang model processing phase STATEMENT_DEFINITION failed [at "));
+ assertThat(cause.getMessage()).startsWith("Yang model processing phase STATEMENT_DEFINITION failed [at ");
// This is a bit complicated, as the order of exceptions may differ
final var causes = new ArrayList<Throwable>();
assertEquals(4, causes.size());
causes.forEach(throwable -> assertInstanceOf(InferenceException.class, throwable));
- assertThat(causes.get(0).getMessage(),
- startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-1 and base identity "
- + "(cyclic.identity.test)child-identity-2 [at "));
- assertThat(causes.get(1).getMessage(),
- startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-2 and base identity "
- + "(cyclic.identity.test)child-identity-3 [at "));
- assertThat(causes.get(2).getMessage(),
- startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-3 and base identity "
- + "(cyclic.identity.test)child-identity-4 [at "));
- assertThat(causes.get(3).getMessage(),
- startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-4 and base identity "
- + "(cyclic.identity.test)child-identity-1 [at "));
+ assertThat(causes.get(0).getMessage())
+ .startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-1 and base identity "
+ + "(cyclic.identity.test)child-identity-2 [at ");
+ assertThat(causes.get(1).getMessage())
+ .startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-2 and base identity "
+ + "(cyclic.identity.test)child-identity-3 [at ");
+ assertThat(causes.get(2).getMessage())
+ .startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-3 and base identity "
+ + "(cyclic.identity.test)child-identity-4 [at ");
+ assertThat(causes.get(3).getMessage())
+ .startsWith("Unable to resolve identity (cyclic.identity.test)child-identity-4 and base identity "
+ + "(cyclic.identity.test)child-identity-1 [at ");
}
@Test
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
final var ex = assertThrows(SomeModifiersUnresolvedException.class, callable::call);
assertEquals(ModelProcessingPhase.SOURCE_LINKAGE, ex.getPhase());
var cause = assertInstanceOf(InferenceException.class, ex.getCause());
- assertThat(cause.getMessage(), startsWith(startStr));
+ assertThat(cause.getMessage()).startsWith(startStr);
return cause;
}
}
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.containsString;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
assertEquals(14, typedefs.size());
var type = TestUtils.findTypedef(typedefs, "ip-version");
- assertThat(type.getDescription().orElseThrow(),
- containsString("This value represents the version of the IP protocol."));
- assertThat(type.getReference().orElseThrow(),
- containsString("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification"));
+ assertThat(type.getDescription().orElseThrow())
+ .contains("This value represents the version of the IP protocol.");
+ assertThat(type.getReference().orElseThrow())
+ .contains("RFC 2460: Internet Protocol, Version 6 (IPv6) Specification");
var enumType = assertInstanceOf(EnumTypeDefinition.class, type.getBaseType());
var values = enumType.getValues();
var typedefs = tested.getTypeDefinitions();
var testedType = TestUtils.findTypedef(typedefs, "iana-timezone");
- assertThat(testedType.getDescription().orElseThrow(),
- containsString("A timezone location as defined by the IANA timezone"));
+ assertThat(testedType.getDescription().orElseThrow())
+ .contains("A timezone location as defined by the IANA timezone");
assertFalse(testedType.getReference().isPresent());
assertEquals(Status.CURRENT, testedType.getStatus());
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.hamcrest.CoreMatchers.not;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayOutputStream;
@Test
@SuppressWarnings("checkstyle:regexpSinglelineJava")
void testAugmentKeys() {
- final PrintStream stdout = System.out;
- final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ final var stdout = System.out;
+ final var output = new ByteArrayOutputStream();
final EffectiveModelContext ctx;
- try (PrintStream out = new PrintStream(output, true, StandardCharsets.UTF_8)) {
+ try (var out = new PrintStream(output, true, StandardCharsets.UTF_8)) {
System.setOut(out);
ctx = assertEffectiveModelDir("/bugs/YT1133");
} finally {
}
assertEquals(2, ctx.getModules().size());
- final String log = output.toString();
- assertThat(log, not(containsString("Configuration list (bar)values")));
+ assertThat(output.toString()).doesNotContain("Configuration list (bar)values");
}
}
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
+import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.common.YangVersion;
}
private static void assertFailedImport(final String subdir) {
- assertThat(assertYangVersionLinkageException(subdir),
- startsWith("Cannot import by revision version 1.1 module new [at "));
+ assertThat(assertYangVersionLinkageException(subdir))
+ .startsWith("Cannot import by revision version 1.1 module new [at ");
}
private static void assertFailedInclude(final String subdir, final YangVersion subVer, final YangVersion modVer) {
- assertThat(assertYangVersionLinkageException(subdir),
- startsWith("Cannot include a version " + subVer + " submodule in a version " + modVer + " module [at "));
+ assertThat(assertYangVersionLinkageException(subdir))
+ .startsWith("Cannot include a version " + subVer + " submodule in a version " + modVer + " module [at ");
}
private static String assertYangVersionLinkageException(final String subdir) {
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.startsWith;
-import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import org.junit.jupiter.api.Test;
@Test
void testRFC7950() {
final var module = assertEffectiveModel("/bugs/YT1410/bar.yang").getModuleStatement(QName.create("bar", "bar"));
- final var one = module.findSchemaTreeNode(QName.create("bar", "one")).orElseThrow();
- assertInstanceOf(ChoiceEffectiveStatement.class, one);
- final var two = ((ChoiceEffectiveStatement) one).findSchemaTreeNode(QName.create("bar", "two")).orElseThrow();
- assertInstanceOf(CaseEffectiveStatement.class, two);
- assertThat(((CaseEffectiveStatement) two).findSchemaTreeNode(QName.create("bar", "two")).orElseThrow(),
- instanceOf(ChoiceEffectiveStatement.class));
+ final var one = assertInstanceOf(ChoiceEffectiveStatement.class,
+ module.findSchemaTreeNode(QName.create("bar", "one")).orElseThrow());
+ final var two = assertInstanceOf(CaseEffectiveStatement.class,
+ one.findSchemaTreeNode(QName.create("bar", "two")).orElseThrow());
+ assertInstanceOf(ChoiceEffectiveStatement.class,
+ two.findSchemaTreeNode(QName.create("bar", "two")).orElseThrow());
}
}
*/
package org.opendaylight.yangtools.yang.stmt;
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.contains;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
@Test
void testAugmentNestedGroupingWithFeatureNotSupported() {
- assertThat(assertFoo("nested", Set.of()).effectiveSubstatements(),
- contains(instanceOf(UsesEffectiveStatement.class)));
+ assertThat(assertFoo("nested", Set.of()).effectiveSubstatements())
+ .anyMatch(UsesEffectiveStatement.class::isInstance);
}
private static ContainerEffectiveStatement assertFoo(final String dirName, final Set<QName> supportedFeatures) {