InstanceIdentifierContext does not take generics
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfImplTest.java
index f55a97ad7bb88ef07ee445f87af5217d6281d1e9..d9874ec748bfc9c21c9c068f1e8b1d2eeb6454e6 100644 (file)
@@ -9,18 +9,19 @@ package org.opendaylight.controller.sal.restconf.impl.test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.isNull;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 
-import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
-import com.google.common.util.concurrent.Futures;
 import java.io.FileNotFoundException;
 import java.net.URI;
 import java.text.ParseException;
@@ -30,30 +31,34 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Set;
 import javax.ws.rs.core.MultivaluedHashMap;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
-import org.junit.Before;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.Mockito;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
-import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMRpcResult;
+import org.opendaylight.mdsal.dom.api.DOMRpcService;
+import org.opendaylight.netconf.sal.rest.api.Draft02;
+import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
+import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
+import org.opendaylight.yangtools.yang.common.Empty;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
@@ -61,9 +66,14 @@ import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
@@ -73,43 +83,39 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
  */
 public class RestconfImplTest {
 
-    private RestconfImpl restconfImpl = null;
-    private static ControllerContext controllerContext = null;
+    private static EffectiveModelContext schemaContext;
+
+    private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
+    private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
+    private final RestconfImpl restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
 
     @BeforeClass
     public static void init() throws FileNotFoundException, ReactorException {
-        final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
-
-        final Set<Module> allModules = schemaContext.getModules();
-        assertNotNull(allModules);
-
-        controllerContext = ControllerContext.getInstance();
-        controllerContext.setSchemas(schemaContext);
+        schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs", "/modules/restconf-module-testing");
     }
 
-    @Before
-    public void initMethod() {
-        this.restconfImpl = RestconfImpl.getInstance();
-        this.restconfImpl.setControllerContext(controllerContext);
+    @AfterClass
+    public static void cleanUp() {
+        WebSocketServer.destroyInstance(); // NETCONF-604
     }
 
     @Test
     public void binaryKeyTest() {
         final List<Byte> al = new ArrayList<>();
-        al.add(new Byte((byte) 1));
+        al.add((byte) 1);
         binaryKeyTest(al, al);
     }
 
-    private void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
+    private static void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
 
-        final QName keyDef = QName.create("test:key:binary", "2017-14-08", "b1");
+        final QName keyDef = QName.create("test:key:binary", "2017-08-14", "b1");
 
         final Map<QName, Object> uriKeyValues = new HashMap<>();
         uriKeyValues.put(keyDef, al.toArray());
 
         final MapEntryNode payload = mock(MapEntryNode.class);
         final NodeIdentifierWithPredicates nodeIdWithPred =
-                new NodeIdentifierWithPredicates(keyDef, keyDef, al2.toArray());
+                NodeIdentifierWithPredicates.of(keyDef, keyDef, al2.toArray());
         when(payload.getIdentifier()).thenReturn(nodeIdWithPred);
 
         final List<QName> keyDefinitions = new ArrayList<>();
@@ -120,7 +126,7 @@ public class RestconfImplTest {
     @Test
     public void binaryKeyFailTest() {
         final List<Byte> al = new ArrayList<>();
-        al.add(new Byte((byte) 1));
+        al.add((byte) 1);
         final List<Byte> al2 = new ArrayList<>();
         try {
             binaryKeyTest(al, al2);
@@ -136,8 +142,7 @@ public class RestconfImplTest {
     public void testExample() throws FileNotFoundException, ParseException {
         @SuppressWarnings("rawtypes")
         final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
-        final BrokerFacade brokerFacade = mock(BrokerFacade.class);
-        when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
+        when(brokerFacade.readOperationalData(isNull())).thenReturn(normalizedNodeData);
         assertEquals(normalizedNodeData,
                 brokerFacade.readOperationalData(null));
     }
@@ -159,11 +164,11 @@ public class RestconfImplTest {
         doReturn(mount).when(iiCtx).getMountPoint();
         final DOMRpcService rpcService = mock(DOMRpcService.class);
         doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
-        doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService)
-                .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
-        this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
-        this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
-        verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+        doReturn(immediateFluentFuture(mock(DOMRpcResult.class))).when(rpcService)
+                .invokeRpc(any(QName.class), any(NormalizedNode.class));
+        restconfImpl.invokeRpc("randomId", ctx, uriInfo);
+        restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
+        verify(rpcService, times(2)).invokeRpc(any(QName.class), any());
     }
 
     /**
@@ -184,26 +189,69 @@ public class RestconfImplTest {
                 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
         doReturn(null).when(iiCtx).getMountPoint();
 
-        final Set<DataContainerChild<?, ?>> children = Sets.newHashSet();
-        final DataContainerChild<?, ?> child = mock(DataContainerChild.class,
+        final Set<DataContainerChild> children = new HashSet<>();
+        final DataContainerChild child = mock(DataContainerChild.class,
                 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
 
         final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
-        when(entryNode.getValue()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
-        when(((LeafSetNode) child).getValue()).thenReturn(Sets.newHashSet(entryNode));
+        when(entryNode.body()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
+        when(((LeafSetNode) child).body()).thenReturn(Sets.newHashSet(entryNode));
         children.add(child);
 
-        final NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class,
+        final NormalizedNode normalizedNode = mock(NormalizedNode.class,
                 Mockito.withSettings().extraInterfaces(ContainerNode.class));
         doReturn(normalizedNode).when(payload).getData();
-        doReturn(children).when(normalizedNode).getValue();
+        doReturn(children).when(normalizedNode).body();
 
         // register notification
-        final NormalizedNodeContext context = this.restconfImpl
+        final NormalizedNodeContext context = restconfImpl
                 .invokeRpc("sal-remote:create-notification-stream", payload, null);
         assertNotNull(context);
     }
 
+    /**
+     * Tests stream entry node.
+     */
+    @Test
+    public void toStreamEntryNodeTest() {
+        final Module restconfModule = controllerContext.getRestconfModule();
+        final DataSchemaNode streamSchemaNode = controllerContext
+                .getRestconfModuleRestConfSchemaNode(restconfModule, Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
+        final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) streamSchemaNode;
+        final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues =
+            SchemaAwareBuilders.mapEntryBuilder(listStreamSchemaNode);
+        List<DataSchemaNode> instanceDataChildrenByName =
+                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "name");
+        final DataSchemaNode nameSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
+            .withValue("")
+            .build());
+
+        instanceDataChildrenByName =
+                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "description");
+        final DataSchemaNode descriptionSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        streamNodeValues.withChild(SchemaAwareBuilders.leafBuilder((LeafSchemaNode) nameSchemaNode)
+            .withValue("DESCRIPTION_PLACEHOLDER")
+            .build());
+
+        instanceDataChildrenByName =
+                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-support");
+        final DataSchemaNode replaySupportSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        streamNodeValues.withChild(
+            SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replaySupportSchemaNode).withValue(Boolean.TRUE).build());
+
+        instanceDataChildrenByName =
+                ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "replay-log-creation-time");
+        final DataSchemaNode replayLogCreationTimeSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        streamNodeValues.withChild(
+            SchemaAwareBuilders.leafBuilder((LeafSchemaNode) replayLogCreationTimeSchemaNode).withValue("").build());
+        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(listStreamSchemaNode, "events");
+        final DataSchemaNode eventsSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        streamNodeValues.withChild(
+            SchemaAwareBuilders.leafBuilder((LeafSchemaNode) eventsSchemaNode).withValue(Empty.value()).build());
+        assertNotNull(streamNodeValues.build());
+    }
+
     /**
      * Subscribe for notification stream of toaster module.
      */
@@ -214,7 +262,7 @@ public class RestconfImplTest {
         // register test notification stream
         final SchemaPath path = SchemaPath.create(
                 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
-        Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML");
+        Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML", controllerContext);
 
         final UriInfo uriInfo = mock(UriInfo.class);
         final UriBuilder uriBuilder = mock(UriBuilder.class);
@@ -228,11 +276,8 @@ public class RestconfImplTest {
         when(map.entrySet()).thenReturn(set);
         when(uriInfo.getQueryParameters()).thenReturn(map);
 
-        final BrokerFacade brokerFacade = mock(BrokerFacade.class);
-        this.restconfImpl.setBroker(brokerFacade);
-
         // subscribe to stream and verify response
-        final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
+        final NormalizedNodeContext response = restconfImpl.subscribeToStream(identifier, uriInfo);
 
         // remove test notification stream
         Notificator.removeAllListeners();