RestconfServer requires ApiPath identifiers
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / parser / ParserIdentifierTest.java
index 88640fb50e940a712fa731f284af7ba9f93529cb..15cc983d18c21f7f6580fe81a85cf105ed3e4c52 100644 (file)
@@ -12,9 +12,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThrows;
-import static org.mockito.Mockito.when;
 
-import java.util.Optional;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -26,6 +24,7 @@ import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
 import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
+import org.opendaylight.restconf.api.ApiPath;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.nb.rfc8040.legacy.ErrorTags;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
@@ -85,8 +84,8 @@ public class ParserIdentifierTest {
         YangParserTestUtils.parseYangResourceDirectory("/parser-identifier");
 
     // mount point and mount point service
+    private final DOMMountPointService mountPointService = new DOMMountPointServiceImpl();
     private DOMMountPoint mountPoint;
-    private DOMMountPointService mountPointService;
 
     // mock mount point and mount point service
     @Mock
@@ -100,8 +99,6 @@ public class ParserIdentifierTest {
 
     @Before
     public void setup() throws Exception {
-        mountPointService = new DOMMountPointServiceImpl();
-
         // create and register mount point
         final var mountPointId = YangInstanceIdentifier.builder()
                 .node(QName.create("mount:point", "2016-06-02", "mount-container"))
@@ -112,10 +109,6 @@ public class ParserIdentifierTest {
                 .addService(DOMSchemaService.class, FixedDOMSchemaService.of(MODEL_CONTEXT_ON_MOUNT_POINT))
                 .register()
                 .getInstance();
-
-        // register mount point with null schema context
-        when(mockMountPointService.getMountPoint(YangInstanceIdentifier.of()))
-                .thenReturn(Optional.of(mockMountPoint));
     }
 
     /**
@@ -154,16 +147,6 @@ public class ParserIdentifierTest {
         assertEquals(MODEL_CONTEXT_ON_MOUNT_POINT, context.getSchemaContext());
     }
 
-    /**
-     * Test of creating <code>InstanceIdentifierContext</code> when identifier is <code>null</code>.
-     * <code>{@link YangInstanceIdentifier#empty()}</code> should be returned.
-     */
-    @Test
-    public void toInstanceIdentifierNullIdentifierTest() {
-        final var context = ParserIdentifier.toInstanceIdentifier(null, MODEL_CONTEXT, null);
-        assertEquals(YangInstanceIdentifier.of(), context.getInstanceIdentifier());
-    }
-
     /**
      * Negative test of creating <code>InstanceIdentifierContext</code> when <code>SchemaContext</code> is
      * <code>null</code>. Test fails expecting <code>NullPointerException</code>.
@@ -221,11 +204,11 @@ public class ParserIdentifierTest {
     @Test
     public void toInstanceIdentifierMissingMountPointNegativeTest() {
         final var ex = assertThrows(RestconfDocumentedException.class,
-            () -> ParserIdentifier.toInstanceIdentifier("/yang-ext:mount", MODEL_CONTEXT, mountPointService));
+            () -> ParserIdentifier.toInstanceIdentifier("yang-ext:mount", MODEL_CONTEXT, mountPointService));
         final var errors = ex.getErrors();
         assertEquals(1, errors.size());
         final var error = errors.get(0);
-        assertEquals("Mount point does not exist.", error.getErrorMessage());
+        assertEquals("Mount point '' does not exist", error.getErrorMessage());
         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
         assertEquals(ErrorTags.RESOURCE_DENIED_TRANSPORT, error.getErrorTag());
     }
@@ -406,19 +389,24 @@ public class ParserIdentifierTest {
 
     @Test
     public void toSchemaExportContextFromIdentifierNullSchemaContextBehindMountPointNegativeTest() {
-        assertThrows(IllegalStateException.class, () -> ParserIdentifier.toSchemaExportContextFromIdentifier(
-                MODEL_CONTEXT, "/yang-ext:mount/" + TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION,
-                mockMountPointService, sourceProvider));
+        final var ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.toSchemaExportContextFromIdentifier(MODEL_CONTEXT,
+                "/yang-ext:mount/" + TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION, mockMountPointService,
+                sourceProvider));
+        final var errors = ex.getErrors();
+        assertEquals(1, errors.size());
+        final var error = errors.get(0);
+        // FIXME: this should be something different
+        assertEquals("Identifier may not be empty", error.getErrorMessage());
+        assertEquals(ErrorType.PROTOCOL, error.getErrorType());
+        assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
     }
 
     /**
-     * Test invoke RPC.
-     *
-     * <p>
-     * Verify if RPC schema node was found.
+     * Test invoke RPC. Verify if RPC schema node was found.
      */
     @Test
-    public void invokeRpcTest() {
+    public void invokeRpcTest() throws Exception {
         final var result = ParserIdentifier.toInstanceIdentifier(INVOKE_RPC, MODEL_CONTEXT, null);
 
         // RPC schema node
@@ -427,58 +415,56 @@ public class ParserIdentifierTest {
         assertEquals("rpc-test", rpcQName.getLocalName());
 
         // other fields
-        assertEquals(IdentifierCodec.deserialize(INVOKE_RPC, MODEL_CONTEXT), result.getInstanceIdentifier());
+        assertEquals(IdentifierCodec.deserialize(ApiPath.parse(INVOKE_RPC), MODEL_CONTEXT),
+            result.getInstanceIdentifier());
         assertEquals(null, result.getMountPoint());
         assertEquals(MODEL_CONTEXT, result.getSchemaContext());
     }
 
     /**
-     * Test invoke RPC on mount point.
-     *
-     * <p>
-     * Verify if RPC schema node was found.
+     * Test invoke RPC on mount point. Verify if RPC schema node was found.
      */
     @Test
-    public void invokeRpcOnMountPointTest() {
+    public void invokeRpcOnMountPointTest() throws Exception {
         final var result = ParserIdentifier.toInstanceIdentifier(MOUNT_POINT_IDENT + "/" + INVOKE_RPC, MODEL_CONTEXT,
             mountPointService);
 
         // RPC schema node
-        final QName rpcQName = result.getSchemaNode().getQName();
+        final var rpcQName = result.getSchemaNode().getQName();
         assertEquals("invoke:rpc:module", rpcQName.getModule().getNamespace().toString());
         assertEquals("rpc-test", rpcQName.getLocalName());
 
         // other fields
-        assertEquals(IdentifierCodec.deserialize(INVOKE_RPC, MODEL_CONTEXT), result.getInstanceIdentifier());
+        assertEquals(IdentifierCodec.deserialize(ApiPath.parse(INVOKE_RPC), MODEL_CONTEXT),
+            result.getInstanceIdentifier());
         assertEquals(mountPoint, result.getMountPoint());
         assertEquals(MODEL_CONTEXT_ON_MOUNT_POINT, result.getSchemaContext());
     }
 
     /**
-     * Test Action.
-     * Verify if Action schema node was found.
+     * Test Action. Verify if Action schema node was found.
      */
     @Test
-    public void invokeActionTest() {
+    public void invokeActionTest() throws Exception {
         final var result = ParserIdentifier.toInstanceIdentifier(INVOKE_ACTION, MODEL_CONTEXT, null);
 
         // Action schema node
-        final QName actionQName = result.getSchemaNode().getQName();
+        final var actionQName = result.getSchemaNode().getQName();
         assertEquals("https://example.com/ns/example-actions", actionQName.getModule().getNamespace().toString());
         assertEquals("reset", actionQName.getLocalName());
 
         // other fields
-        assertEquals(IdentifierCodec.deserialize(INVOKE_ACTION, MODEL_CONTEXT), result.getInstanceIdentifier());
+        assertEquals(IdentifierCodec.deserialize(ApiPath.parse(INVOKE_ACTION), MODEL_CONTEXT),
+            result.getInstanceIdentifier());
         assertNull(result.getMountPoint());
         assertSame(MODEL_CONTEXT, result.getSchemaContext());
     }
 
     /**
-     * Test invoke Action on mount point.
-     * Verify if Action schema node was found.
+     * Test invoke Action on mount point. Verify if Action schema node was found.
      */
     @Test
-    public void invokeActionOnMountPointTest() {
+    public void invokeActionOnMountPointTest() throws Exception {
         final var result = ParserIdentifier.toInstanceIdentifier(MOUNT_POINT_IDENT + "/" + INVOKE_ACTION,
             MODEL_CONTEXT, mountPointService);
 
@@ -488,7 +474,8 @@ public class ParserIdentifierTest {
         assertEquals("reset", actionQName.getLocalName());
 
         // other fields
-        assertEquals(IdentifierCodec.deserialize(INVOKE_ACTION, MODEL_CONTEXT), result.getInstanceIdentifier());
+        assertEquals(IdentifierCodec.deserialize(ApiPath.parse(INVOKE_ACTION), MODEL_CONTEXT),
+            result.getInstanceIdentifier());
         assertEquals(mountPoint, result.getMountPoint());
         assertEquals(MODEL_CONTEXT_ON_MOUNT_POINT, result.getSchemaContext());
     }