--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.dom.broker;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import com.google.common.collect.Multimap;
+import java.lang.reflect.Field;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nonnull;
+import org.junit.Test;
+import org.opendaylight.mdsal.dom.api.DOMNotification;
+import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
+import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.util.ListenerRegistry;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+public class DOMNotificationRouterTest extends TestUtils {
+
+ @Test
+ public void create() throws Exception {
+ assertNotNull(DOMNotificationRouter.create(1,1,1,TimeUnit.SECONDS));
+ assertNotNull(DOMNotificationRouter.create(1));
+ }
+
+ @Test
+ public void complexTest() throws Exception {
+ final DOMNotificationSubscriptionListener domNotificationSubscriptionListener =
+ mock(DOMNotificationSubscriptionListener.class);
+ final DOMNotificationListener domNotificationListener = new TestListener();
+ final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1);
+
+ final Field listenersField = DOMNotificationRouter.class.getDeclaredField("listeners");
+ listenersField.setAccessible(true);
+
+ final Field subscriptionListenersField = DOMNotificationRouter.class.getDeclaredField("subscriptionListeners");
+ subscriptionListenersField.setAccessible(true);
+
+ Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> listeners =
+ (Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>>)
+ listenersField.get(domNotificationRouter);
+
+ assertTrue(listeners.isEmpty());
+ assertNotNull(domNotificationRouter.registerNotificationListener(domNotificationListener, SchemaPath.ROOT));
+ assertNotNull(domNotificationRouter.registerNotificationListener(domNotificationListener, SchemaPath.SAME));
+
+ listeners = (Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>>)
+ listenersField.get(domNotificationRouter);
+
+ assertFalse(listeners.isEmpty());
+
+ ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
+ (ListenerRegistry<DOMNotificationSubscriptionListener>)
+ subscriptionListenersField.get(domNotificationRouter);
+
+ assertFalse(subscriptionListeners.iterator().hasNext());
+ assertNotNull(domNotificationRouter.registerSubscriptionListener(domNotificationSubscriptionListener));
+
+ subscriptionListeners = (ListenerRegistry<DOMNotificationSubscriptionListener>)
+ subscriptionListenersField.get(domNotificationRouter);
+ assertTrue(subscriptionListeners.iterator().hasNext());
+
+ final DOMNotification domNotification = mock(DOMNotification.class);
+ doReturn("test").when(domNotification).toString();
+ doReturn(SchemaPath.ROOT).when(domNotification).getType();
+ doReturn(TEST_CHILD).when(domNotification).getBody();
+
+ assertNotNull(domNotificationRouter.offerNotification(domNotification));
+
+ Exception exception = null;
+ try {
+ assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
+ assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
+ } catch (UnsupportedOperationException e) {
+ exception = e;
+ }
+ assertNotNull(exception);
+ assertNotNull(domNotificationRouter.putNotification(domNotification));
+ }
+
+ @Test
+ public void offerNotification() throws Exception {
+ final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1);
+ final DOMNotification domNotification = mock(DOMNotification.class);
+ doReturn(SchemaPath.ROOT).when(domNotification).getType();
+ doReturn(TEST_CHILD).when(domNotification).getBody();
+ assertNotNull(domNotificationRouter.putNotification(domNotification));
+ assertNotNull(domNotificationRouter.offerNotification(domNotification));
+ assertNotNull(domNotificationRouter.offerNotification(domNotification, 1, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void close() throws Exception {
+ final DOMNotificationRouter domNotificationRouter = DOMNotificationRouter.create(1);
+
+ final Field executorField = DOMNotificationRouter.class.getDeclaredField("executor");
+ executorField.setAccessible(true);
+
+ final ExecutorService executor = (ExecutorService) executorField.get(domNotificationRouter);
+
+ assertFalse(executor.isShutdown());
+ domNotificationRouter.close();
+ assertTrue(executor.isShutdown());
+ }
+
+ private class TestListener implements DOMNotificationListener {
+ @Override
+ public void onNotification(@Nonnull DOMNotification notification) {}
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.dom.broker;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.concurrent.RejectedExecutionException;
+import org.junit.Test;
+import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
+import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
+import org.opendaylight.mdsal.dom.broker.test.util.TestModel;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+public class DOMRpcRouterTest extends TestUtils {
+
+ @Test
+ public void registerRpcImplementation() throws Exception {
+ final DOMRpcRouter rpcRouter = new DOMRpcRouter();
+ final Field routingTableField = DOMRpcRouter.class.getDeclaredField("routingTable");
+ routingTableField.setAccessible(true);
+ DOMRpcRoutingTable routingTable = (DOMRpcRoutingTable) routingTableField.get(rpcRouter);
+ assertFalse(routingTable.getRpcs().containsKey(SchemaPath.ROOT));
+
+ rpcRouter.registerRpcImplementation(getTestRpcImplementation(), DOMRpcIdentifier.create(SchemaPath.ROOT, null));
+ routingTable = (DOMRpcRoutingTable) routingTableField.get(rpcRouter);
+ assertTrue(routingTable.getRpcs().containsKey(SchemaPath.ROOT));
+
+ rpcRouter.registerRpcImplementation(getTestRpcImplementation(), DOMRpcIdentifier.create(SchemaPath.SAME, null));
+ routingTable = (DOMRpcRoutingTable) routingTableField.get(rpcRouter);
+ assertTrue(routingTable.getRpcs().containsKey(SchemaPath.SAME));
+ }
+
+ @Test
+ public void invokeRpc() throws Exception {
+ final DOMRpcRouter rpcRouter = new DOMRpcRouter();
+ assertNotNull(rpcRouter.invokeRpc(SchemaPath.create(false, TestModel.TEST_QNAME), null));
+ }
+
+ @Test
+ public void registerRpcListener() throws Exception {
+ final DOMRpcRouter rpcRouter = new DOMRpcRouter();
+ final DOMRpcAvailabilityListener listener = mock(DOMRpcAvailabilityListener.class);
+
+ final Field listenersField = DOMRpcRouter.class.getDeclaredField("listeners");
+ listenersField.setAccessible(true);
+ Collection<ListenerRegistration<? extends DOMRpcAvailabilityListener>> listenersOriginal =
+ (Collection<ListenerRegistration<? extends DOMRpcAvailabilityListener>>) listenersField.get(rpcRouter);
+
+ assertNotNull(rpcRouter.registerRpcListener(listener));
+
+ Collection<ListenerRegistration<? extends DOMRpcAvailabilityListener>> listenersChanged =
+ (Collection<ListenerRegistration<? extends DOMRpcAvailabilityListener>>) listenersField.get(rpcRouter);
+ assertNotEquals(listenersOriginal, listenersChanged);
+ assertTrue(listenersOriginal.isEmpty());
+ assertFalse(listenersChanged.isEmpty());
+ }
+
+ @Test
+ public void onGlobalContextUpdated() throws Exception {
+ final DOMRpcRouter rpcRouter = new DOMRpcRouter();
+ final Field routingTableField = DOMRpcRouter.class.getDeclaredField("routingTable");
+ routingTableField.setAccessible(true);
+
+ final DOMRpcRoutingTable routingTableOriginal = (DOMRpcRoutingTable) routingTableField.get(rpcRouter);
+
+ rpcRouter.onGlobalContextUpdated(TestModel.createTestContext());
+
+ final DOMRpcRoutingTable routingTableChanged = (DOMRpcRoutingTable) routingTableField.get(rpcRouter);
+ assertNotEquals(routingTableOriginal, routingTableChanged);
+ }
+
+ @Test(expected = RejectedExecutionException.class)
+ public void close() throws Exception {
+ final DOMRpcRouter rpcRouter = new DOMRpcRouter();
+ rpcRouter.close();
+ rpcRouter.registerRpcImplementation(getTestRpcImplementation(), DOMRpcIdentifier.create(SchemaPath.ROOT, null));
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.dom.broker;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import java.util.HashMap;
+import org.junit.Test;
+import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.mdsal.dom.broker.test.util.TestModel;
+import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+public class RoutedDOMRpcRoutingTableEntryTest extends TestUtils {
+
+ @Test
+ public void basicTest() throws Exception {
+ final RpcDefinition rpcDefinition = mock(RpcDefinition.class);
+ doReturn(SchemaPath.ROOT).when(rpcDefinition).getPath();
+
+ RoutedDOMRpcRoutingTableEntry routedDOMRpcRoutingTableEntry =
+ new RoutedDOMRpcRoutingTableEntry(rpcDefinition, TestModel.TEST_PATH, new HashMap<>());
+ assertNotNull(routedDOMRpcRoutingTableEntry.newInstance(new HashMap<>()));
+
+ try {
+ routedDOMRpcRoutingTableEntry.invokeRpc(TEST_CHILD).checkedGet();
+ fail("Expected DOMRpcImplementationNotAvailableException");
+ } catch (Exception e) {
+ assertTrue(e instanceof DOMRpcImplementationNotAvailableException);
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.dom.broker;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import org.junit.Test;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public class ShardedDOMDataWriteTransactionTest {
+ private static final Map<YangInstanceIdentifier, List<NormalizedNode<?, ?>>> TEST_MAP = new HashMap<>();
+
+ @Test
+ public void basicTests() throws Exception {
+ final ShardedDOMDataTree shardedDOMDataTree =
+ new ShardedDOMDataTree();
+ final ShardedDOMDataTreeProducer shardedDOMDataTreeProducer =
+ new ShardedDOMDataTreeProducer(shardedDOMDataTree, new HashMap<>(), new HashSet<>());
+ final TestDOMStoreWriteTransaction testDOMStoreWriteTransaction =
+ new TestDOMStoreWriteTransaction();
+ final Map<DOMDataTreeIdentifier, DOMStoreWriteTransaction> idToTransaction =
+ new HashMap<>();
+ final YangInstanceIdentifier yangInstanceIdentifier = YangInstanceIdentifier.of(QName.create("test"));
+ idToTransaction.put(new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangInstanceIdentifier),
+ testDOMStoreWriteTransaction);
+
+ final ShardedDOMDataWriteTransaction shardedDOMDataWriteTransaction =
+ new ShardedDOMDataWriteTransaction(shardedDOMDataTreeProducer, idToTransaction);
+ final ShardedDOMDataWriteTransaction otherShardedDOMDataWriteTransaction =
+ new ShardedDOMDataWriteTransaction(shardedDOMDataTreeProducer, idToTransaction);
+
+ assertFalse(TEST_MAP.containsKey(yangInstanceIdentifier));
+ shardedDOMDataWriteTransaction.put(
+ LogicalDatastoreType.OPERATIONAL, yangInstanceIdentifier, TestUtils.TEST_CONTAINER);
+ assertTrue(TEST_MAP.containsKey(yangInstanceIdentifier));
+ shardedDOMDataWriteTransaction.delete(
+ LogicalDatastoreType.OPERATIONAL, yangInstanceIdentifier);
+ assertFalse(TEST_MAP.containsKey(yangInstanceIdentifier));
+ shardedDOMDataWriteTransaction.merge(
+ LogicalDatastoreType.OPERATIONAL, yangInstanceIdentifier, TestUtils.TEST_CONTAINER);
+ assertTrue(TEST_MAP.get(yangInstanceIdentifier).contains(TestUtils.TEST_CONTAINER));
+
+ try {
+ shardedDOMDataWriteTransaction.put(
+ LogicalDatastoreType.CONFIGURATION, yangInstanceIdentifier, TestUtils.TEST_CONTAINER);
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().contains(LogicalDatastoreType.CONFIGURATION.toString()));
+ }
+
+ shardedDOMDataTreeProducer.createTransaction(true);
+ assertTrue(shardedDOMDataWriteTransaction.cancel());
+ assertFalse(shardedDOMDataWriteTransaction.cancel());
+
+ assertTrue(shardedDOMDataWriteTransaction.getIdentifier().contains("0"));
+ assertTrue(otherShardedDOMDataWriteTransaction.getIdentifier().contains("1"));
+ }
+
+ private final class TestDOMStoreWriteTransaction implements DOMStoreWriteTransaction {
+
+ @Override
+ public void write(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
+ List<NormalizedNode<?, ?>> dataList = TEST_MAP.get(path);
+ if(dataList == null) dataList = new ArrayList<>();
+ dataList.add(data);
+ TEST_MAP.put(path, dataList);
+ }
+
+ @Override
+ public void merge(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
+ List<NormalizedNode<?, ?>> dataList = TEST_MAP.get(path);
+ if(dataList == null) dataList = new ArrayList<>();
+ dataList.add(data);
+ TEST_MAP.put(path, dataList);
+ }
+
+ @Override
+ public void delete(YangInstanceIdentifier path) {
+ TEST_MAP.remove(path);
+ }
+
+ @Override
+ public DOMStoreThreePhaseCommitCohort ready() {
+ return null;
+ }
+
+ @Override
+ public Object getIdentifier() {
+ return null;
+ }
+
+ @Override
+ public void close() {
+ // NOOP
+ }
+ }
+}
\ No newline at end of file
* 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.mdsal.dom.broker.test;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
-import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
-
-import org.opendaylight.mdsal.dom.api.DOMMountPoint;
-import org.opendaylight.mdsal.dom.api.DOMMountPointService;
-import org.opendaylight.mdsal.dom.api.DOMMountPointService.DOMMountPointBuilder;
import com.google.common.base.Optional;
+import com.google.common.collect.ClassToInstanceMap;
+import java.lang.reflect.Field;
import org.junit.Before;
import org.junit.Test;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService.DOMMountPointBuilder;
+import org.opendaylight.mdsal.dom.api.DOMService;
+import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl;
+import org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl.DOMMountPointBuilderImpl;
+import org.opendaylight.yangtools.concepts.ObjectRegistration;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
public class MountPointServiceTest {
- private DOMMountPointService mountService;
- private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.of(QName.create("namespace", "12-12-2012", "top"));
+ private static DOMMountPointService mountService;
+ private static final YangInstanceIdentifier PATH =
+ YangInstanceIdentifier.of(QName.create("namespace", "12-12-2012", "top"));
@Before
public void setup() {
}
@Test
- public void createSimpleMountPoint() {
- Optional<DOMMountPoint> mountNotPresent = mountService.getMountPoint(PATH);
- assertFalse(mountNotPresent.isPresent());
- DOMMountPointBuilder mountBuilder = mountService.createMountPoint(PATH);
+ public void createSimpleMountPoint() throws Exception {
+ final DOMMountPointListener listener = mock(DOMMountPointListener.class);
+ doNothing().when(listener).onMountPointCreated(PATH);
+ mountService.registerProvisionListener(listener);
+
+ assertFalse(mountService.getMountPoint(PATH).isPresent());
+
+ mountService.createMountPoint(PATH).register();
+
+ assertTrue(mountService.getMountPoint(PATH).isPresent());
+ }
+
+ @Test
+ public void unregisterTest() throws Exception {
+ final DOMMountPointListener listener = mock(DOMMountPointListener.class);
+ doNothing().when(listener).onMountPointCreated(PATH);
+ doNothing().when(listener).onMountPointRemoved(PATH);
+ final DOMMountPointServiceImpl mountService = new DOMMountPointServiceImpl();
+ mountService.registerProvisionListener(listener);
+ mountService.createMountPoint(PATH).register();
+
+ assertTrue(mountService.getMountPoint(PATH).isPresent());
+
+ mountService.unregisterMountPoint(PATH);
+
+ assertFalse(mountService.getMountPoint(PATH).isPresent());
+ }
+
+ @Test
+ public void mountRegistrationTest() throws Exception {
+ final DOMMountPointBuilder mountBuilder = mountService.createMountPoint(PATH);
+ final ObjectRegistration<DOMMountPoint> objectRegistration = mountBuilder.register();
+
+ assertTrue(mountService.getMountPoint(PATH).isPresent());
+ assertSame(objectRegistration.getInstance(), mountService.getMountPoint(PATH).get());
+
+ objectRegistration.close();
+
+ assertFalse(mountService.getMountPoint(PATH).isPresent());
+ }
+
+ @Test
+ public void mountBuilderTest() throws Exception {
+ final DOMMountPointBuilderImpl mountBuilder = (DOMMountPointBuilderImpl) mountService.createMountPoint(PATH);
mountBuilder.register();
- Optional<DOMMountPoint> mountPresent = mountService.getMountPoint(PATH);
- assertTrue(mountPresent.isPresent());
+ final SchemaContext mockSchemaContext = mock(SchemaContext.class);
+ mountBuilder.addInitialSchemaContext(mockSchemaContext);
+
+ final Field schemaContextField = DOMMountPointBuilderImpl.class.getDeclaredField("schemaContext");
+ schemaContextField.setAccessible(true);
+
+ final SchemaContext schemaContext = (SchemaContext) schemaContextField.get(mountBuilder);
+
+ assertSame(mockSchemaContext, schemaContext);
+
+ final Field servicesField = DOMMountPointBuilderImpl.class.getDeclaredField("services");
+ servicesField.setAccessible(true);
+
+ final ClassToInstanceMap<DOMService> services =
+ (ClassToInstanceMap<DOMService>) servicesField.get(mountBuilder);
+ assertTrue(services.isEmpty());
+ assertFalse(services.containsKey(DOMService.class));
+ mountBuilder.addService(DOMService.class, null);
+ assertFalse(services.isEmpty());
+ assertTrue(services.containsKey(DOMService.class));
}
}