Uploaded image for project: 'yangtools'
  1. yangtools
  2. YANGTOOLS-1345

Remove StmtTestUtils.parseYangSource(String)

XMLWordPrintable

    • Icon: Task Task
    • Resolution: Done
    • Icon: Medium Medium
    • 8.0.3
    • None
    • parser

      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");
          }
      

       

            SamoSchneider Samuel Schneider
            rovarga Robert Varga
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: