From 15cca98bcafa1aca61481113193a77a97dbeebe5 Mon Sep 17 00:00:00 2001 From: Oleksandr Zharov Date: Thu, 5 Oct 2023 14:19:57 +0200 Subject: [PATCH] Throws RDE exception in case model was not found 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 Signed-off-by: Ivan Hrasko (cherry picked from commit e17f784409f8d6a42a9529ef45423dfcd06c39f7) --- .../nb/rfc8040/utils/parser/ParserIdentifier.java | 15 +++++++++++++-- .../services/impl/RestconfSchemaServiceTest.java | 11 ++++++++--- .../utils/parser/ParserIdentifierTest.java | 11 ++++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifier.java b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifier.java index ca84c374e6..fc7d28379b 100644 --- a/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifier.java +++ b/restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifier.java @@ -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); diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfSchemaServiceTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfSchemaServiceTest.java index bd90201ac6..21692c760b 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfSchemaServiceTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfSchemaServiceTest.java @@ -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()); } /** diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifierTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifierTest.java index ed26a81307..ae6bdf99fd 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifierTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifierTest.java @@ -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()); } /** -- 2.36.6