Throw exception when module cannot be found 14/108214/8
authorOleksandr Zharov <oleksandr.zharov@pantheon.tech>
Thu, 5 Oct 2023 09:01:49 +0000 (11:01 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Thu, 12 Oct 2023 06:34:24 +0000 (08:34 +0200)
Made module field in SchemaExportContext class @NonNull.
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>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/api/SchemaExportContext.java
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 a5f752f31242819651660e6b3e31c6539f7ec881..cd7fe8e6cd731c11b3e7bc7a2669e93c8e013cd2 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.restconf.nb.rfc8040.rests.services.api;
 import static java.util.Objects.requireNonNull;
 
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
@@ -20,11 +19,12 @@ import org.opendaylight.yangtools.yang.model.api.Module;
  */
 public record SchemaExportContext(
     @NonNull EffectiveModelContext schemaContext,
-    @Nullable Module module,
+    @NonNull Module module,
     @NonNull DOMYangTextSourceProvider sourceProvider) {
 
     public SchemaExportContext {
         requireNonNull(schemaContext);
+        requireNonNull(module);
         requireNonNull(sourceProvider);
     }
 }
index 8ca93647fd285a9506959bc155114d26477c5605..ea66ef2f25ec9a370da60c92a753121966ff167e 100644 (file)
@@ -191,7 +191,7 @@ public final class ParserIdentifier {
         if (!Iterables.contains(pathComponents, 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();
@@ -214,7 +214,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 252d9c471f739462f904b221132c14e07b84e7a3..f57851f76753dd642221fadba484d6fe60970592 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 com.google.common.collect.ImmutableClassToInstanceMap;
+import java.util.NoSuchElementException;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -121,20 +121,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(mockSchemaService.getGlobalContext()).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.module());
+        // make test & verify
+        assertThrows(NoSuchElementException.class, () -> schemaService.getSchema(NOT_EXISTING_MODULE));
     }
 
     /**
@@ -161,20 +157,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(mockSchemaService.getGlobalContext()).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.module());
+        // make test & verify
+        assertThrows(NoSuchElementException.class, () -> schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE));
     }
 
     /**
index e27a4cfe7c38295549362f1a2338e71185466872..9b31c6382a61f637466278cc4b4eedfc444c55ad 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;
@@ -402,17 +403,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.module());
+                null, sourceProvider));
     }
 
     /**
@@ -454,17 +452,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.module());
+                mountPointService, sourceProvider));
     }
 
     /**