[YANGTOOLS-1345] Remove StmtTestUtils.parseYangSource(String) Created: 03/Oct/21  Updated: 10/Apr/22  Resolved: 27/Mar/22

Status: Resolved
Project: yangtools
Component/s: parser
Affects Version/s: None
Fix Version/s: 8.0.3

Type: Task Priority: Medium
Reporter: Robert Varga Assignee: Samuel Schneider
Resolution: Done Votes: 0
Labels: pt
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

We have huge amount of code duplication going on in parser unit tests. This is mostly due to us not having a common assert library. Introduce AbstractYangTest to service as an abstract base class serving as host for assertions.

Then migrate all callers of StmtTestUtils.parseYangSource(String) to use its methods instead. There are three basic classes of test files which need to be migrated.

The first are those which are still using Assert.fail() with a catch, i.e. like this:

    @Test
    public void invalid10Test() throws Exception {
        try {
            StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/foo10.yang");
            fail("Test should fail due to invalid Yang 1.0");
        } catch (final SomeModifiersUnresolvedException e) {
            assertTrue(e.getCause().getMessage().startsWith("NOTIFICATION is not valid for"));
        }
    }

these should end up looking like this:

    @Test
    public void invalid10Test() {
        assertInvalidSubstatementException(startsWith("NOTIFICATION is not valid for"),
            "/rfc7950/notifications-in-data-nodes/foo10.yang");
    }

The second class are those which expect a module to compile and perhaps assertNotNull():

    @Test
    public void notificationsInDataContainersTest() throws Exception {
        final SchemaContext schemaContext = StmtTestUtils
                .parseYangSource("/rfc7950/notifications-in-data-nodes/foo.yang");
        assertNotNull(schemaContext);

These should should end up looking like this (variable type and naming included):

    @Test
    public void notificationsInDataContainersTest() throws Exception {
        final var context = assertEffectiveModel("/rfc7950/notifications-in-data-nodes/foo.yang");

The third class are those which are using assertThrows(), and have a number of repetitive assertions:

    @Test
    public void invalid10Test() {
        final ReactorException ex = assertThrows(ReactorException.class,
            () -> StmtTestUtils.parseYangSource("/rfc7950/bug6870/invalid10.yang"));
        final Throwable cause = ex.getCause();
        assertThat(cause, instanceOf(SourceException.class));
        assertThat(cause.getMessage(), startsWith("modifier is not a YANG statement or use of extension"));
    }

this needs to end up looking like this:

    @Test
    public void invalid10Test() {
        assertSourceException(startsWith("modifier is not a YANG statement or use of extension"),
            "/rfc7950/bug6870/invalid10.yang");
    }

 


Generated at Wed Feb 07 20:55:53 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.