Fixed wrong exception types
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / parser / ParserIdentifierTest.java
index ba33cd6ef0c608ff2a6ea1a5e34cf565d1574c3d..d5f3568b6b1a0baf16307a83c48c52ad4d8175c1 100644 (file)
@@ -5,30 +5,28 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.when;
 
-import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableClassToInstanceMap;
-import com.google.common.collect.Maps;
+import java.util.Map.Entry;
+import java.util.Optional;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
-import org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl;
-import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
+import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
@@ -38,7 +36,7 @@ import org.opendaylight.restconf.common.schema.SchemaExportContext;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -89,6 +87,7 @@ public class ParserIdentifierTest {
     private static final String TEST_MODULE_NAMESPACE = "test:module";
 
     private static final String INVOKE_RPC = "invoke-rpc-module:rpc-test";
+    private static final String INVOKE_ACTION = "example-actions:interfaces/interface=eth0/reset";
 
     // mount point and mount point service
     private DOMMountPoint mountPoint;
@@ -110,26 +109,26 @@ public class ParserIdentifierTest {
     @Before
     public void setup() throws Exception {
         MockitoAnnotations.initMocks(this);
-        this.schemaContext = YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/parser-identifier"));
+        this.schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/parser-identifier"));
         this.schemaContextOnMountPoint =
-                YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/parser-identifier"));
+                YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/parser-identifier"));
+
+        this.mountPointService = new DOMMountPointServiceImpl();
 
         // create and register mount point
-        this.mountPoint = SimpleDOMMountPoint.create(
-                YangInstanceIdentifier.builder()
-                        .node(QName.create("mount:point", "2016-06-02", "mount-container"))
-                        .node(QName.create("mount:point", "2016-06-02", "point-number"))
-                        .build(),
-                ImmutableClassToInstanceMap.copyOf(Maps.newHashMap()),
-                this.schemaContextOnMountPoint
-        );
+        final YangInstanceIdentifier mountPointId = YangInstanceIdentifier.builder()
+                .node(QName.create("mount:point", "2016-06-02", "mount-container"))
+                .node(QName.create("mount:point", "2016-06-02", "point-number"))
+                .build();
 
-        this.mountPointService = new DOMMountPointServiceImpl();
-        ((DOMMountPointServiceImpl) this.mountPointService).registerMountPoint(this.mountPoint);
+        mountPoint = mountPointService.createMountPoint(mountPointId)
+                .addInitialSchemaContext(this.schemaContextOnMountPoint)
+                .register()
+                .getInstance();
 
         // register mount point with null schema context
         when(this.mockMountPoint.getSchemaContext()).thenReturn(null);
-        when(this.mockMountPointService.getMountPoint(YangInstanceIdentifier.EMPTY))
+        when(this.mockMountPointService.getMountPoint(YangInstanceIdentifier.empty()))
                 .thenReturn(Optional.of(this.mockMountPoint));
     }
 
@@ -144,7 +143,7 @@ public class ParserIdentifierTest {
     @Test
     public void toInstanceIdentifierTest() {
         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
-                TEST_IDENT, this.schemaContext, Optional.absent());
+                TEST_IDENT, this.schemaContext, Optional.empty());
 
         assertEquals("Returned not expected identifier",
                 TEST_IDENT_RESULT, context .getInstanceIdentifier().toString());
@@ -157,7 +156,7 @@ public class ParserIdentifierTest {
     @Test
     public void toInstanceIdentifierOtherModulesTest() {
         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
-                TEST_IDENT_OTHERS, this.schemaContext, Optional.absent());
+                TEST_IDENT_OTHERS, this.schemaContext, Optional.empty());
 
         assertEquals("Returned not expected identifier",
                 TEST_IDENT_OTHERS_RESULT, context.getInstanceIdentifier().toString());
@@ -189,9 +188,9 @@ public class ParserIdentifierTest {
     @Test
     public void toInstanceIdentifierNullIdentifierTest() {
         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
-                null, this.schemaContext, Optional.absent());
+                null, this.schemaContext, Optional.empty());
         assertEquals("Returned not expected identifier",
-                YangInstanceIdentifier.EMPTY, context.getInstanceIdentifier());
+                YangInstanceIdentifier.empty(), context.getInstanceIdentifier());
     }
 
     /**
@@ -201,7 +200,7 @@ public class ParserIdentifierTest {
     @Test
     public void toInstanceIdentifierNullSchemaContextNegativeTest() {
         this.thrown.expect(NullPointerException.class);
-        ParserIdentifier.toInstanceIdentifier(TEST_IDENT, null, Optional.absent());
+        ParserIdentifier.toInstanceIdentifier(TEST_IDENT, null, Optional.empty());
     }
 
     /**
@@ -210,27 +209,27 @@ public class ParserIdentifierTest {
     @Test
     public void toInstanceIdentifierEmptyIdentifierTest() {
         final InstanceIdentifierContext<?> context = ParserIdentifier.toInstanceIdentifier(
-                "", this.schemaContext, Optional.absent());
+                "", this.schemaContext, Optional.empty());
         assertEquals("Returned not expected identifier",
-                YangInstanceIdentifier.EMPTY, context.getInstanceIdentifier());
+                YangInstanceIdentifier.empty(), context.getInstanceIdentifier());
     }
 
     /**
-     * Negative test with invalid test identifier. Test should fail with <code>IllegalArgumentException</code>.
+     * Negative test with invalid test identifier. Test should fail with <code>RestconfDocumentedException</code>.
      */
     @Test
     public void toInstanceIdentifierInvalidIdentifierNegativeTest() {
-        this.thrown.expect(IllegalArgumentException.class);
-        ParserIdentifier.toInstanceIdentifier(INVALID_TEST_IDENT, this.schemaContext, Optional.absent());
+        this.thrown.expect(RestconfDocumentedException.class);
+        ParserIdentifier.toInstanceIdentifier(INVALID_TEST_IDENT, this.schemaContext, Optional.empty());
     }
 
     /**
      * Negative test when identifier contains {@link RestconfConstants#MOUNT} but identifier part is not valid. Test
-     * should fail with <code>IllegalArgumentException</code>.
+     * should fail with <code>RestconfDocumentedException</code>.
      */
     @Test
     public void toInstanceIdentifierMountPointInvalidIdentifierNegativeTest() {
-        this.thrown.expect(IllegalArgumentException.class);
+        this.thrown.expect(RestconfDocumentedException.class);
         ParserIdentifier.toInstanceIdentifier(
                 INVALID_MOUNT_POINT_IDENT, this.schemaContext, Optional.of(this.mountPointService));
     }
@@ -264,7 +263,7 @@ public class ParserIdentifierTest {
     @Test
     public void toInstanceIdentifierMissingMountPointServiceNegativeTest() {
         try {
-            ParserIdentifier.toInstanceIdentifier(RestconfConstants.MOUNT, this.schemaContext, Optional.absent());
+            ParserIdentifier.toInstanceIdentifier(RestconfConstants.MOUNT, this.schemaContext, Optional.empty());
             fail("Test should fail due to absent mount point service");
         } catch (final RestconfDocumentedException e) {
             assertEquals("Not expected error type",
@@ -286,13 +285,12 @@ public class ParserIdentifierTest {
      */
     @Test
     public void makeQNameFromIdentifierTest() {
-        final QName qName = ParserIdentifier.makeQNameFromIdentifier(TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION);
+        final Entry<String, Revision> qName = ParserIdentifier.makeQNameFromIdentifier(
+            TEST_MODULE_NAME + "/" + TEST_MODULE_REVISION);
 
         assertNotNull("QName should be created", qName);
-        assertEquals("Returned not expected module name",
-                TEST_MODULE_NAME, qName.getLocalName());
-        assertEquals("Returned not expected module revision",
-                TEST_MODULE_REVISION, qName.getFormattedRevision());
+        assertEquals("Returned not expected module name", TEST_MODULE_NAME, qName.getKey());
+        assertEquals("Returned not expected module revision", Revision.of(TEST_MODULE_REVISION), qName.getValue());
     }
 
     /**
@@ -341,7 +339,7 @@ public class ParserIdentifierTest {
      */
     @Test
     public void makeQNameFromIdentifierMountTest() {
-        final QName qName = ParserIdentifier.makeQNameFromIdentifier(
+        final Entry<String, Revision> qName = ParserIdentifier.makeQNameFromIdentifier(
                 MOUNT_POINT_IDENT
                 + "/"
                 + TEST_MODULE_NAME
@@ -349,10 +347,8 @@ public class ParserIdentifierTest {
                 + TEST_MODULE_REVISION);
 
         assertNotNull("QName should be created", qName);
-        assertEquals("Returned not expected module name",
-                TEST_MODULE_NAME, qName.getLocalName());
-        assertEquals("Returned not expected module revision",
-                TEST_MODULE_REVISION, qName.getFormattedRevision());
+        assertEquals("Returned not expected module name", TEST_MODULE_NAME, qName.getKey());
+        assertEquals("Returned not expected module revision", Revision.of(TEST_MODULE_REVISION), qName.getValue());
     }
 
     /**
@@ -477,7 +473,7 @@ public class ParserIdentifierTest {
         assertEquals("Returned not expected module name",
                 TEST_MODULE_NAME, module.getName());
         assertEquals("Returned not expected module revision",
-                TEST_MODULE_REVISION, SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision()));
+                Revision.ofNullable(TEST_MODULE_REVISION), module.getRevision());
         assertEquals("Returned not expected module namespace",
                 TEST_MODULE_NAMESPACE, module.getNamespace().toString());
     }
@@ -535,7 +531,7 @@ public class ParserIdentifierTest {
         assertEquals("Returned not expected module name",
                 TEST_MODULE_NAME, module.getName());
         assertEquals("Returned not expected module revision",
-                TEST_MODULE_REVISION, SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision()));
+                Revision.ofNullable(TEST_MODULE_REVISION), module.getRevision());
         assertEquals("Returned not expected module namespace",
                 TEST_MODULE_NAMESPACE, module.getNamespace().toString());
     }
@@ -666,7 +662,7 @@ public class ParserIdentifierTest {
     @Test
     public void invokeRpcTest() {
         final InstanceIdentifierContext<?> result = ParserIdentifier.toInstanceIdentifier(
-                INVOKE_RPC, this.schemaContext, Optional.absent());
+                INVOKE_RPC, this.schemaContext, Optional.empty());
 
         // RPC schema node
         final QName rpcQName = result.getSchemaNode().getQName();
@@ -700,4 +696,45 @@ public class ParserIdentifierTest {
         assertEquals(this.mountPoint, result.getMountPoint());
         assertEquals(this.schemaContextOnMountPoint, result.getSchemaContext());
     }
+
+    /**
+     * Test Action.
+     * Verify if Action schema node was found.
+     */
+    @Test
+    public void invokeActionTest() {
+        final InstanceIdentifierContext<?> result = ParserIdentifier
+            .toInstanceIdentifier(INVOKE_ACTION, this.schemaContext, Optional.empty());
+
+        // Action schema node
+        final QName 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, schemaContext), result.getInstanceIdentifier());
+        assertNull(result.getMountPoint());
+        assertSame(this.schemaContext, result.getSchemaContext());
+    }
+
+    /**
+     * Test invoke Action on mount point.
+     * Verify if Action schema node was found.
+     */
+    @Test
+    public void invokeActionOnMountPointTest() {
+        final InstanceIdentifierContext<?> result = ParserIdentifier
+            .toInstanceIdentifier(MOUNT_POINT_IDENT + "/" + INVOKE_ACTION, this.schemaContext,
+                Optional.of(this.mountPointService));
+
+        // Action schema node
+        final QName 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, schemaContext), result.getInstanceIdentifier());
+        assertEquals(this.mountPoint, result.getMountPoint());
+        assertEquals(this.schemaContextOnMountPoint, result.getSchemaContext());
+    }
 }