Throw exception when module cannot be found 07/108407/4
authorOleksandr Zharov <oleksandr.zharov@pantheon.tech>
Thu, 5 Oct 2023 09:01:49 +0000 (11:01 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 17 Oct 2023 09:03:10 +0000 (09:03 +0000)
Added exception in case module not found in
ParserIdentifier#toSchemaExportContextFromIdentifier.

JIRA: NETCONF-1166
Change-Id: I9571496d789a1c499fd4871787e25e562bfec8ae
Signed-off-by: Oleksandr Zharov <oleksandr.zharov@pantheon.tech>
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
(cherry picked from commit 820f7aba74e9f71b46da6d69611ce120feb26148)

restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifier.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfSchemaServiceTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifierTest.java

index 4601c144ac666acf07e59f929333228d9ffaeedd..ca84c374e6fbc8b0ca0ffdfbcccd9a76a69d5391 100644 (file)
@@ -186,7 +186,7 @@ public final class ParserIdentifier {
         if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
             final String moduleName = validateAndGetModulName(componentIter);
             final Revision revision = validateAndGetRevision(componentIter);
-            final Module module = schemaContext.findModule(moduleName, revision).orElse(null);
+            final Module module = schemaContext.findModule(moduleName, revision).orElseThrow();
             return new SchemaExportContext(schemaContext, module, sourceProvider);
         } else {
             final StringBuilder pathBuilder = new StringBuilder();
@@ -209,7 +209,7 @@ public final class ParserIdentifier {
             final String moduleName = validateAndGetModulName(componentIter);
             final Revision revision = validateAndGetRevision(componentIter);
             final EffectiveModelContext context = coerceModelContext(point.getMountPoint());
-            final Module module = context.findModule(moduleName, revision).orElse(null);
+            final Module module = context.findModule(moduleName, revision).orElseThrow();
             return new SchemaExportContext(context, module, sourceProvider);
         }
     }
index 209effc758adf703dc88ebd9a79642962129f307..bd90201ac6cb3600562aa8708f76427c1596a92c 100644 (file)
@@ -9,11 +9,11 @@ package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThrows;
 import static org.mockito.Mockito.when;
 
 import java.io.FileNotFoundException;
+import java.util.NoSuchElementException;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -125,20 +125,16 @@ public class RestconfSchemaServiceTest {
     }
 
     /**
-     * Get schema with identifier of not-existing module. <code>SchemaExportContext</code> is still created, but module
-     * should be set to <code>null</code>.
+     * Get schema with identifier of not-existing module. Trying to create <code>SchemaExportContext</code> with
+     * not-existing module should result in error.
      */
     @Test
     public void getSchemaForNotExistingModuleTest() {
         // prepare conditions - return not-mount point schema context
         when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
 
-        // make test
-        final SchemaExportContext exportContext = schemaService.getSchema(NOT_EXISTING_MODULE);
-
-        // verify
-        assertNotNull("Export context should not be null", exportContext);
-        assertNull("Not-existing module should not be found", exportContext.getModule());
+        // make test & verify
+        assertThrows(NoSuchElementException.class, () -> schemaService.getSchema(NOT_EXISTING_MODULE));
     }
 
     /**
@@ -165,20 +161,16 @@ public class RestconfSchemaServiceTest {
     }
 
     /**
-     * Get schema with identifier of not-existing module behind mount point. <code>SchemaExportContext</code> is still
-     * created, but module should be set to <code>null</code>.
+     * Get schema with identifier of not-existing module behind mount point. Trying to create
+     * <code>SchemaExportContext</code> with not-existing module behind mount point should result in error.
      */
     @Test
     public void getSchemaForNotExistingModuleMountPointTest() {
         // prepare conditions - return schema context with mount points
         when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
 
-        // make test
-        final SchemaExportContext exportContext = schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE);
-
-        // verify
-        assertNotNull("Export context should not be null", exportContext);
-        assertNull("Not-existing module should not be found", exportContext.getModule());
+        // make test & verify
+        assertThrows(NoSuchElementException.class, () -> schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE));
     }
 
     /**
index fd4f6a67ffcbaacc02df47a6194203fce80b80cd..ed26a81307886ab51135beda6b1530807b969839 100644 (file)
@@ -16,6 +16,7 @@ import static org.mockito.Mockito.when;
 
 import java.util.List;
 import java.util.Map.Entry;
+import java.util.NoSuchElementException;
 import java.util.Optional;
 import org.junit.AfterClass;
 import org.junit.Before;
@@ -427,17 +428,14 @@ public class ParserIdentifierTest {
 
     /**
      * Test of getting <code>SchemaExportContext</code> when desired module is not found.
-     * <code>SchemaExportContext</code> should be created but module should be set to <code>null</code>.
+     * <code>SchemaExportContext</code> should not be created and exception is thrown.
      */
     @Test
     public void toSchemaExportContextFromIdentifierNotFoundTest() {
-        final SchemaExportContext exportContext = ParserIdentifier.toSchemaExportContextFromIdentifier(
+        assertThrows(NoSuchElementException.class, () -> ParserIdentifier.toSchemaExportContextFromIdentifier(
                 SCHEMA_CONTEXT,
                 "not-existing-module" + "/" + "2016-01-01",
-                null, sourceProvider);
-
-        assertNotNull("Export context should be parsed", exportContext);
-        assertNull("Not-existing module should be null", exportContext.getModule());
+                null, sourceProvider));
     }
 
     /**
@@ -479,17 +477,14 @@ public class ParserIdentifierTest {
 
     /**
      * Negative test of getting <code>SchemaExportContext</code> when desired module is not found behind mount point.
-     * <code>SchemaExportContext</code> should be still created but module should be set to <code>null</code>.
+     * <code>SchemaExportContext</code> should not be created and exception is thrown.
      */
     @Test
     public void toSchemaExportContextFromIdentifierMountPointNotFoundTest() {
-        final SchemaExportContext exportContext = ParserIdentifier.toSchemaExportContextFromIdentifier(
+        assertThrows(NoSuchElementException.class, () -> ParserIdentifier.toSchemaExportContextFromIdentifier(
                 SCHEMA_CONTEXT,
                 MOUNT_POINT_IDENT + "/" + "not-existing-module" + "/" + "2016-01-01",
-                mountPointService, sourceProvider);
-
-        assertNotNull("Export context should be parsed", exportContext);
-        assertNull("Not-existing module should be null", exportContext.getModule());
+                mountPointService, sourceProvider));
     }
 
     /**