Throws RDE exception in case model was not found 08/108408/4
authorOleksandr Zharov <oleksandr.zharov@pantheon.tech>
Thu, 5 Oct 2023 12:19:57 +0000 (14:19 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 17 Oct 2023 09:03:10 +0000 (09:03 +0000)
Added proper response in case model was not found using
RestconfDocumentedException's DATA_MISSING error tag.

Added unit tests to cover this cases.

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

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 ca84c374e6fbc8b0ca0ffdfbcccd9a76a69d5391..fc7d28379bd692b404b1c31b3f115c91c606ba89 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).orElseThrow();
+            final Module module = coerceModule(schemaContext, moduleName, revision, null);
             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).orElseThrow();
+            final Module module = coerceModule(context, moduleName, revision, point.getMountPoint());
             return new SchemaExportContext(context, module, sourceProvider);
         }
     }
@@ -255,6 +255,17 @@ public final class ParserIdentifier {
         return name;
     }
 
+    private static Module coerceModule(final EffectiveModelContext context, final String moduleName,
+            final Revision revision, final DOMMountPoint mountPoint) {
+        final var module = context.findModule(moduleName, revision);
+        if (module.isEmpty()) {
+            final var msg = "Module %s %s cannot be found on %s.".formatted(moduleName, revision,
+                mountPoint == null ? "controller" : mountPoint.getIdentifier());
+            throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
+        }
+        return module.orElseThrow();
+    }
+
     private static EffectiveModelContext coerceModelContext(final DOMMountPoint mountPoint) {
         final EffectiveModelContext context = modelContext(mountPoint);
         checkState(context != null, "Mount point %s does not have a model context", mountPoint);
index bd90201ac6cb3600562aa8708f76427c1596a92c..21692c760b018d893941c32e475b7463642784c4 100644 (file)
@@ -13,7 +13,6 @@ 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;
@@ -134,7 +133,10 @@ public class RestconfSchemaServiceTest {
         when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT);
 
         // make test & verify
-        assertThrows(NoSuchElementException.class, () -> schemaService.getSchema(NOT_EXISTING_MODULE));
+        final var ex = assertThrows(RestconfDocumentedException.class, () ->
+            schemaService.getSchema(NOT_EXISTING_MODULE));
+        assertEquals("Not expected error tag", ErrorTag.DATA_MISSING, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Not expected error type", ErrorType.APPLICATION, ex.getErrors().get(0).getErrorType());
     }
 
     /**
@@ -170,7 +172,10 @@ public class RestconfSchemaServiceTest {
         when(this.mockContextHandler.get()).thenReturn(SCHEMA_CONTEXT_WITH_MOUNT_POINTS);
 
         // make test & verify
-        assertThrows(NoSuchElementException.class, () -> schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE));
+        final var ex = assertThrows(RestconfDocumentedException.class, () ->
+            schemaService.getSchema(MOUNT_POINT + NOT_EXISTING_MODULE));
+        assertEquals("Not expected error tag", ErrorTag.DATA_MISSING, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Not expected error type", ErrorType.APPLICATION, ex.getErrors().get(0).getErrorType());
     }
 
     /**
index ed26a81307886ab51135beda6b1530807b969839..ae6bdf99fd04652720a681ff04f294e8702d5187 100644 (file)
@@ -16,7 +16,6 @@ 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;
@@ -432,10 +431,13 @@ public class ParserIdentifierTest {
      */
     @Test
     public void toSchemaExportContextFromIdentifierNotFoundTest() {
-        assertThrows(NoSuchElementException.class, () -> ParserIdentifier.toSchemaExportContextFromIdentifier(
+        final var ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.toSchemaExportContextFromIdentifier(
                 SCHEMA_CONTEXT,
                 "not-existing-module" + "/" + "2016-01-01",
                 null, sourceProvider));
+        assertEquals("Not expected error tag", ErrorTag.DATA_MISSING, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Not expected error type", ErrorType.APPLICATION, ex.getErrors().get(0).getErrorType());
     }
 
     /**
@@ -481,10 +483,13 @@ public class ParserIdentifierTest {
      */
     @Test
     public void toSchemaExportContextFromIdentifierMountPointNotFoundTest() {
-        assertThrows(NoSuchElementException.class, () -> ParserIdentifier.toSchemaExportContextFromIdentifier(
+        final var ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.toSchemaExportContextFromIdentifier(
                 SCHEMA_CONTEXT,
                 MOUNT_POINT_IDENT + "/" + "not-existing-module" + "/" + "2016-01-01",
                 mountPointService, sourceProvider));
+        assertEquals("Not expected error tag", ErrorTag.DATA_MISSING, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Not expected error type", ErrorType.APPLICATION, ex.getErrors().get(0).getErrorType());
     }
 
     /**