Throws RDE exception in case model was not found 48/108248/11
authorOleksandr Zharov <oleksandr.zharov@pantheon.tech>
Thu, 5 Oct 2023 12:19:57 +0000 (14:19 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Thu, 12 Oct 2023 14:36:15 +0000 (16:36 +0200)
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>
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 ea66ef2f25ec9a370da60c92a753121966ff167e..0b816ebbc4e22d0372f60e8c04952f226a2fe3a7 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).orElseThrow();
+            final Module module = coerceModule(schemaContext, moduleName, revision, null);
             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).orElseThrow();
+            final Module module = coerceModule(context, moduleName, revision, point.getMountPoint());
             return new SchemaExportContext(context, module, sourceProvider);
         }
     }
@@ -260,6 +260,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 f57851f76753dd642221fadba484d6fe60970592..e405e162e06b71834ff1adae1184e4cc8d9563e4 100644 (file)
@@ -13,7 +13,6 @@ 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;
@@ -130,7 +129,10 @@ public class RestconfSchemaServiceTest {
         when(mockSchemaService.getGlobalContext()).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());
     }
 
     /**
@@ -166,7 +168,10 @@ public class RestconfSchemaServiceTest {
         when(mockSchemaService.getGlobalContext()).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 9b31c6382a61f637466278cc4b4eedfc444c55ad..3b704d9f4bc5d3b5b76a4b4c0467bb015110644b 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;
@@ -407,10 +406,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());
     }
 
     /**
@@ -456,10 +458,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());
     }
 
     /**