Bug 1062 - Disallow implicit serviceref creation.
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfConfigPersisterITTest.java
index ff2efed128676b1da367442b67c4789185132ea7..148b0446e309f4470f55ee0205e7be6cd429499d 100644 (file)
@@ -9,12 +9,14 @@ package org.opendaylight.controller.netconf.it;
 
 import static junit.framework.Assert.assertEquals;
 import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import io.netty.channel.ChannelFuture;
+import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithName;
+import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertElementsCount;
+import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToDocument;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -22,67 +24,62 @@ import java.net.InetSocketAddress;
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
-import java.util.regex.Pattern;
 
 import javax.management.InstanceNotFoundException;
 import javax.management.Notification;
 import javax.management.NotificationListener;
 
-import org.apache.commons.lang3.StringUtils;
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
-import org.junit.matchers.JUnitMatchers;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
 import org.opendaylight.controller.config.persist.api.ConfigSnapshotHolder;
 import org.opendaylight.controller.config.persist.api.Persister;
 import org.opendaylight.controller.config.spi.ModuleFactory;
-import org.opendaylight.controller.config.yang.store.api.YangStoreException;
-import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
 import org.opendaylight.controller.netconf.api.NetconfMessage;
 import org.opendaylight.controller.netconf.api.jmx.CommitJMXNotification;
 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
-import org.opendaylight.controller.netconf.client.NetconfClient;
 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
+import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
+import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
+import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
-import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener;
 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
-import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshot;
+import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
 import org.opendaylight.controller.netconf.mapping.api.Capability;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
 
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
+import io.netty.channel.ChannelFuture;
 
 public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
 
     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
 
-
-
     private NetconfClientDispatcher clientDispatcher;
-
-    DefaultCommitNotificationProducer commitNotifier;
+    private DefaultCommitNotificationProducer commitNotifier;
 
     @Before
     public void setUp() throws Exception {
-        super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(NetconfITTest.getModuleFactoriesS().toArray(
+        super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,NetconfITTest.getModuleFactoriesS().toArray(
                 new ModuleFactory[0])));
 
-        NetconfMonitoringServiceImpl monitoringService = new NetconfMonitoringServiceImpl(getFactoriesListener());
+        NetconfMonitoringServiceImpl monitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider());
 
         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
@@ -96,7 +93,7 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
         ChannelFuture s = dispatch.createServer(tcpAddress);
         s.await();
 
-        clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
+        clientDispatcher = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
     }
 
     @After
@@ -125,34 +122,34 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
         VerifyingNotificationListener notificationVerifier = createCommitNotificationListener();
         VerifyingPersister mockedAggregator = mockAggregator();
 
-        try (NetconfClient persisterClient = new NetconfClient("persister", tcpAddress, 4000, clientDispatcher)) {
+        try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
             try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler(
-                    platformMBeanServer, mockedAggregator, Pattern.compile(""))) {
+                    platformMBeanServer, mockedAggregator)) {
 
 
-                try (NetconfClient netconfClient = new NetconfClient("client", tcpAddress, 4000, clientDispatcher)) {
+                try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", clientDispatcher, getClientConfiguration(tcpAddress, 4000))) {
                     NetconfMessage response = netconfClient.sendMessage(loadGetConfigMessage());
-                    assertResponse(response, "<modules");
-                    assertResponse(response, "<services");
+                    assertContainsElementWithName(response.getDocument(), "modules");
+                    assertContainsElementWithName(response.getDocument(), "services");
                     response = netconfClient.sendMessage(loadCommitMessage());
-                    assertResponse(response, "ok");
+                    assertContainsElementWithName(response.getDocument(), "ok");
 
                     response = netconfClient.sendMessage(loadEditConfigMessage());
-                    assertResponse(response, "ok");
+                    assertContainsElementWithName(response.getDocument(), "ok");
                     response = netconfClient.sendMessage(loadCommitMessage());
-                    assertResponse(response, "ok");
+                    assertContainsElementWithName(response.getDocument(), "ok");
                 }
             }
         }
 
         notificationVerifier.assertNotificationCount(2);
         notificationVerifier.assertNotificationContent(0, 0, 0, 9);
-        notificationVerifier.assertNotificationContent(1, 4, 4, 9);
+        notificationVerifier.assertNotificationContent(1, 4, 3, 9);
 
         mockedAggregator.assertSnapshotCount(2);
         // Capabilities are stripped for persister
         mockedAggregator.assertSnapshotContent(0, 0, 0, 1);
-        mockedAggregator.assertSnapshotContent(1, 4, 4, 3);
+        mockedAggregator.assertSnapshotContent(1, 4, 3, 3);
     }
 
     private VerifyingPersister mockAggregator() throws IOException {
@@ -161,14 +158,10 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
 
     private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
         VerifyingNotificationListener listener = new VerifyingNotificationListener();
-        platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.objectName, listener, null, null);
+        platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
         return listener;
     }
 
-    private void assertResponse(NetconfMessage response, String content) {
-        Assert.assertThat(XmlUtil.toString(response.getDocument()), JUnitMatchers.containsString(content));
-    }
-
     private NetconfMessage loadGetConfigMessage() throws Exception {
         return XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
     }
@@ -182,15 +175,15 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
     }
 
 
-    public NetconfOperationServiceFactoryListener getFactoriesListener() {
-        NetconfOperationServiceFactoryListener factoriesListener = mock(NetconfOperationServiceFactoryListener.class);
-        NetconfOperationServiceSnapshot snap = mock(NetconfOperationServiceSnapshot.class);
+    public NetconfOperationProvider getNetconfOperationProvider() {
+        NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class);
+        NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class);
         NetconfOperationService service = mock(NetconfOperationService.class);
         Set<Capability> caps = Sets.newHashSet();
         doReturn(caps).when(service).getCapabilities();
         Set<NetconfOperationService> services = Sets.newHashSet(service);
         doReturn(services).when(snap).getServices();
-        doReturn(snap).when(factoriesListener).getSnapshot(anyLong());
+        doReturn(snap).when(factoriesListener).openSnapshot(anyString());
 
         return factoriesListener;
     }
@@ -244,15 +237,14 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
             assertEquals(size, snapshots.size());
         }
 
-        void assertSnapshotContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize) {
+        void assertSnapshotContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize)
+                throws SAXException, IOException {
             ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex);
             int capsSize = snapshot.getCapabilities().size();
             assertEquals("Expected capabilities count", expectedCapsSize, capsSize);
-            String configSnapshot = snapshot.getConfigSnapshot();
-            int modulesSize = StringUtils.countMatches(configSnapshot, "<module>");
-            assertEquals("Expected modules count", expectedModulesSize, modulesSize);
-            int servicesSize = StringUtils.countMatches(configSnapshot, "<instance>");
-            assertEquals("Expected services count", expectedServicesSize, servicesSize);
+            Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot());
+            assertElementsCount(configSnapshot, "module", expectedModulesSize);
+            assertElementsCount(configSnapshot, "instance", expectedServicesSize);
         }
 
         @Override