X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-it%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fit%2FNetconfConfigPersisterITTest.java;h=92c96d92f28962eeb88ccc961665d852cd39364c;hp=78933e4b0bededee3245505cf4f30610a4a1c72e;hb=8eb2fc510df912207d98589b82a22d84bf8ada66;hpb=490e58f15373444b94642aebb2afbec9a62b9a57 diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java index 78933e4b0b..92c96d92f2 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfConfigPersisterITTest.java @@ -7,148 +7,118 @@ */ package org.opendaylight.controller.netconf.it; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +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 com.google.common.collect.Lists; import com.google.common.collect.Sets; -import io.netty.channel.ChannelFuture; -import org.apache.commons.lang3.StringUtils; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; +import java.io.IOException; +import java.lang.management.ManagementFactory; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import javax.management.InstanceNotFoundException; +import javax.management.Notification; +import javax.management.NotificationListener; 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.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.NetconfClientDispatcher; -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.client.TestingNetconfClient; 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.mapping.api.NetconfOperationServiceFactory; 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 javax.management.InstanceNotFoundException; -import javax.management.Notification; -import javax.management.NotificationListener; -import java.io.IOException; -import java.io.InputStream; -import java.net.InetSocketAddress; -import java.util.Collection; -import java.util.List; -import java.util.Set; - -import static junit.framework.Assert.assertEquals; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyLong; -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 org.xml.sax.SAXException; public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest { - private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023); - - - - private NetconfClientDispatcher clientDispatcher; - - DefaultCommitNotificationProducer commitNotifier; - - @Before - public void setUp() throws Exception { - super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(NetconfITTest.getModuleFactoriesS().toArray( - new ModuleFactory[0]))); - - NetconfMonitoringServiceImpl monitoringService = new NetconfMonitoringServiceImpl(getFactoriesListener()); + public static final int PORT = 12026; + private static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT); - NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl(); - factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore())); - factoriesListener - .onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory( - new NetconfMonitoringOperationService(monitoringService))); + private NetconfMonitoringServiceImpl netconfMonitoringService; - - commitNotifier = new DefaultCommitNotificationProducer(platformMBeanServer); - NetconfServerDispatcher dispatch = createDispatcher(factoriesListener, mockSessionMonitoringService(), commitNotifier); - ChannelFuture s = dispatch.createServer(tcpAddress); - s.await(); - - clientDispatcher = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000); + @Override + protected void setUpTestInitial() { + netconfMonitoringService = new NetconfMonitoringServiceImpl(getNetconfOperationProvider()); } - @After - public void cleanUp(){ - commitNotifier.close(); + @Override + protected SessionMonitoringService getNetconfMonitoringService() throws Exception { + return netconfMonitoringService; } - private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException { - final Collection yangDependencies = NetconfITTest.getBasicYangs(); - return new HardcodedYangStoreService(yangDependencies); + @Override + protected SocketAddress getTcpServerAddress() { + return TCP_ADDRESS; } - - protected SessionMonitoringService mockSessionMonitoringService() { - SessionMonitoringService mockedSessionMonitor = mock(SessionMonitoringService.class); - doNothing().when(mockedSessionMonitor).onSessionUp(any(NetconfManagementSession.class)); - doNothing().when(mockedSessionMonitor).onSessionDown(any(NetconfManagementSession.class)); - return mockedSessionMonitor; + @Override + protected Iterable getAdditionalServiceFactories() { + return Collections.singletonList(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory( + new NetconfMonitoringOperationService(netconfMonitoringService))); } - + @Override + protected DefaultCommitNotificationProducer getNotificationProducer() { + return new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); + } @Test public void testNetconfCommitNotifications() throws Exception { + final VerifyingNotificationListener notificationVerifier = createCommitNotificationListener(); + final VerifyingPersister mockedAggregator = mockAggregator(); - VerifyingNotificationListener notificationVerifier = createCommitNotificationListener(); - VerifyingPersister mockedAggregator = mockAggregator(); - - try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", tcpAddress, 4000, clientDispatcher)) { + try (TestingNetconfClient persisterClient = new TestingNetconfClient("persister", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) { try (ConfigPersisterNotificationHandler configPersisterNotificationHandler = new ConfigPersisterNotificationHandler( platformMBeanServer, mockedAggregator)) { - try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", tcpAddress, 4000, clientDispatcher)) { + try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", getClientDispatcher(), getClientConfiguration(TCP_ADDRESS, 4000))) { NetconfMessage response = netconfClient.sendMessage(loadGetConfigMessage()); - assertResponse(response, " caps = Sets.newHashSet(); + public NetconfOperationProvider getNetconfOperationProvider() { + final NetconfOperationProvider factoriesListener = mock(NetconfOperationProvider.class); + final NetconfOperationServiceSnapshotImpl snap = mock(NetconfOperationServiceSnapshotImpl.class); + final NetconfOperationService service = mock(NetconfOperationService.class); + final Set caps = Sets.newHashSet(); doReturn(caps).when(service).getCapabilities(); - Set services = Sets.newHashSet(service); + final Set services = Sets.newHashSet(service); doReturn(services).when(snap).getServices(); - doReturn(snap).when(factoriesListener).getSnapshot(anyLong()); + doReturn(snap).when(factoriesListener).openSnapshot(anyString()); return factoriesListener; } @@ -195,23 +161,23 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest { public List notifications = Lists.newArrayList(); @Override - public void handleNotification(Notification notification, Object handback) { + public void handleNotification(final Notification notification, final Object handback) { this.notifications.add(notification); } - void assertNotificationCount(Object size) { + void assertNotificationCount(final Object size) { assertEquals(size, notifications.size()); } - void assertNotificationContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize) { - Notification notification = notifications.get(notificationIndex); + void assertNotificationContent(final int notificationIndex, final int expectedModulesSize, final int expectedServicesSize, final int expectedCapsSize) { + final Notification notification = notifications.get(notificationIndex); assertEquals(CommitJMXNotification.class, notification.getClass()); - int capsSize = ((CommitJMXNotification) notification).getCapabilities().size(); + final int capsSize = ((CommitJMXNotification) notification).getCapabilities().size(); assertEquals("Expected capabilities count", expectedCapsSize, capsSize); - Element configSnapshot = ((CommitJMXNotification) notification).getConfigSnapshot(); - int modulesSize = configSnapshot.getElementsByTagName("module").getLength(); + final Element configSnapshot = ((CommitJMXNotification) notification).getConfigSnapshot(); + final int modulesSize = configSnapshot.getElementsByTagName("module").getLength(); assertEquals("Expected modules count", expectedModulesSize, modulesSize); - int servicesSize = configSnapshot.getElementsByTagName("instance").getLength(); + final int servicesSize = configSnapshot.getElementsByTagName("instance").getLength(); assertEquals("Expected services count", expectedServicesSize, servicesSize); } } @@ -222,12 +188,12 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest { private Persister mockedPersister; public VerifyingPersister() throws IOException { - Persister mockedAggregator = mock(Persister.class); + final Persister mockedAggregator = mock(Persister.class); doAnswer(new Answer() { @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0]; + public Object answer(final InvocationOnMock invocation) throws Throwable { + final ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0]; snapshots.add(configSnapshot); return null; } @@ -236,23 +202,22 @@ public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest { this.mockedPersister = mockedAggregator; } - void assertSnapshotCount(Object size) { + void assertSnapshotCount(final Object size) { assertEquals(size, snapshots.size()); } - void assertSnapshotContent(int notificationIndex, int expectedModulesSize, int expectedServicesSize, int expectedCapsSize) { - ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex); - int capsSize = snapshot.getCapabilities().size(); + void assertSnapshotContent(final int notificationIndex, final int expectedModulesSize, final int expectedServicesSize, final int expectedCapsSize) + throws SAXException, IOException { + final ConfigSnapshotHolder snapshot = snapshots.get(notificationIndex); + final int capsSize = snapshot.getCapabilities().size(); assertEquals("Expected capabilities count", expectedCapsSize, capsSize); - String configSnapshot = snapshot.getConfigSnapshot(); - int modulesSize = StringUtils.countMatches(configSnapshot, ""); - assertEquals("Expected modules count", expectedModulesSize, modulesSize); - int servicesSize = StringUtils.countMatches(configSnapshot, ""); - assertEquals("Expected services count", expectedServicesSize, servicesSize); + final Document configSnapshot = readXmlToDocument(snapshot.getConfigSnapshot()); + assertElementsCount(configSnapshot, "module", expectedModulesSize); + assertElementsCount(configSnapshot, "instance", expectedServicesSize); } @Override - public void persistConfig(ConfigSnapshotHolder configSnapshotHolder) throws IOException { + public void persistConfig(final ConfigSnapshotHolder configSnapshotHolder) throws IOException { mockedPersister.persistConfig(configSnapshotHolder); }