Merge "Bug 1348 - TopologyCommitHandler NullPointerException on edgeUpdate CHANGED"
authorTony Tkacik <ttkacik@cisco.com>
Fri, 12 Sep 2014 11:10:53 +0000 (11:10 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 12 Sep 2014 11:10:53 +0000 (11:10 +0000)
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreProperties.java [deleted file]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/jmx/mbeans/AbstractBaseMBean.java [deleted file]
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/jmx/mbeans/shard/ShardStatsMBean.java [deleted file]
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMDataCommitCoordinatorImpl.java
opendaylight/netconf/netconf-testtool/src/main/java/org/opendaylight/controller/netconf/test/tool/NetconfDeviceSimulator.java
opendaylight/netconf/netconf-util/pom.xml
opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/CloseableUtilTest.java [new file with mode: 0644]
opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/HardcodedNamespaceResolverTest.java [new file with mode: 0644]
opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlElementTest.java [new file with mode: 0644]
opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlUtilTest.java [new file with mode: 0644]

diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreProperties.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/DistributedDataStoreProperties.java
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/jmx/mbeans/AbstractBaseMBean.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/jmx/mbeans/AbstractBaseMBean.java
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/jmx/mbeans/shard/ShardStatsMBean.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/jmx/mbeans/shard/ShardStatsMBean.java
deleted file mode 100644 (file)
index e69de29..0000000
index 3fde8d360f8af6df8cb0bcd705a9e3289d9fd35e..d796ab35fa88522bd7f89d4802adaffe9c3d0a8e 100644 (file)
@@ -6,13 +6,18 @@
  */
 package org.opendaylight.controller.md.sal.dom.broker.impl;
 
-import java.util.List;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Throwables;
+import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.ListeningExecutorService;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.RejectedExecutionException;
-
 import javax.annotation.concurrent.GuardedBy;
-
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
@@ -21,17 +26,6 @@ import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableList.Builder;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.ListeningExecutorService;
-
 /**
  *
  * Implementation of blocking three phase commit coordinator, which which
@@ -49,28 +43,8 @@ import com.google.common.util.concurrent.ListeningExecutorService;
 public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
 
     private static final Logger LOG = LoggerFactory.getLogger(DOMDataCommitCoordinatorImpl.class);
-
-    /**
-     * Runs AND binary operation between all booleans in supplied iteration of booleans.
-     *
-     * This method will stop evaluating iterables if first found is false.
-     */
-    private static final Function<Iterable<Boolean>, Boolean> AND_FUNCTION = new Function<Iterable<Boolean>, Boolean>() {
-
-        @Override
-        public Boolean apply(final Iterable<Boolean> input) {
-            for(boolean value : input) {
-               if(!value) {
-                   return Boolean.FALSE;
-               }
-            }
-            return Boolean.TRUE;
-        }
-    };
-
-    private final ListeningExecutorService executor;
-
     private final DurationStatsTracker commitStatsTracker = new DurationStatsTracker();
+    private final ListeningExecutorService executor;
 
     /**
      *
@@ -163,6 +137,7 @@ public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
         private final DOMDataWriteTransaction tx;
         private final Iterable<DOMStoreThreePhaseCommitCohort> cohorts;
         private final DurationStatsTracker commitStatTracker;
+        private final int cohortSize;
 
         @GuardedBy("this")
         private CommitPhase currentPhase;
@@ -175,6 +150,7 @@ public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
             this.cohorts = Preconditions.checkNotNull(cohorts, "cohorts must not be null");
             this.currentPhase = CommitPhase.SUBMITTED;
             this.commitStatTracker = commitStatTracker;
+            this.cohortSize = Iterables.size(cohorts);
         }
 
         @Override
@@ -210,10 +186,39 @@ public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
          *
          */
         private void canCommitBlocking() throws TransactionCommitFailedException {
-            final Boolean canCommitResult = canCommitAll().checkedGet();
-            if (!canCommitResult) {
-                throw new TransactionCommitFailedException("Can Commit failed, no detailed cause available.");
+            for (ListenableFuture<?> canCommit : canCommitAll()) {
+                try {
+                    final Boolean result = (Boolean)canCommit.get();
+                    if (result == null || !result) {
+                        throw new TransactionCommitFailedException("Can Commit failed, no detailed cause available.");
+                    }
+                } catch (InterruptedException | ExecutionException e) {
+                    throw TransactionCommitFailedExceptionMapper.CAN_COMMIT_ERROR_MAPPER.apply(e);
+                }
+            }
+        }
+
+        /**
+         *
+         * Invokes canCommit on underlying cohorts and returns composite future
+         * which will contains {@link Boolean#TRUE} only and only if
+         * all cohorts returned true.
+         *
+         * Valid state transition is from SUBMITTED to CAN_COMMIT,
+         * if currentPhase is not SUBMITTED throws IllegalStateException.
+         *
+         * @return List of all cohorts futures from can commit phase.
+         *
+         */
+        private ListenableFuture<?>[] canCommitAll() {
+            changeStateFrom(CommitPhase.SUBMITTED, CommitPhase.CAN_COMMIT);
+
+            final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohortSize];
+            int i = 0;
+            for (DOMStoreThreePhaseCommitCohort cohort : cohorts) {
+                ops[i++] = cohort.canCommit();
             }
+            return ops;
         }
 
         /**
@@ -230,7 +235,39 @@ public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
          *
          */
         private void preCommitBlocking() throws TransactionCommitFailedException {
-            preCommitAll().checkedGet();
+            final ListenableFuture<?>[] preCommitFutures = preCommitAll();
+            try {
+                for(ListenableFuture<?> future : preCommitFutures) {
+                    future.get();
+                }
+            } catch (InterruptedException | ExecutionException e) {
+                throw TransactionCommitFailedExceptionMapper.PRE_COMMIT_MAPPER.apply(e);
+            }
+        }
+
+        /**
+         *
+         * Invokes preCommit on underlying cohorts and returns future
+         * which will complete once all preCommit on cohorts completed or
+         * failed.
+         *
+         *
+         * Valid state transition is from CAN_COMMIT to PRE_COMMIT, if current
+         * state is not CAN_COMMIT
+         * throws IllegalStateException.
+         *
+         * @return List of all cohorts futures from can commit phase.
+         *
+         */
+        private ListenableFuture<?>[] preCommitAll() {
+            changeStateFrom(CommitPhase.CAN_COMMIT, CommitPhase.PRE_COMMIT);
+
+            final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohortSize];
+            int i = 0;
+            for (DOMStoreThreePhaseCommitCohort cohort : cohorts) {
+                ops[i++] = cohort.preCommit();
+            }
+            return ops;
         }
 
         /**
@@ -246,7 +283,37 @@ public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
          *
          */
         private void commitBlocking() throws TransactionCommitFailedException {
-            commitAll().checkedGet();
+            final ListenableFuture<?>[] commitFutures = commitAll();
+            try {
+                for(ListenableFuture<?> future : commitFutures) {
+                    future.get();
+                }
+            } catch (InterruptedException | ExecutionException e) {
+                throw TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER.apply(e);
+            }
+        }
+
+        /**
+         *
+         * Invokes commit on underlying cohorts and returns future which
+         * completes
+         * once all commits on cohorts are completed.
+         *
+         * Valid state transition is from PRE_COMMIT to COMMIT, if not throws
+         * IllegalStateException
+         *
+         * @return List of all cohorts futures from can commit phase.
+         *
+         */
+        private ListenableFuture<?>[] commitAll() {
+            changeStateFrom(CommitPhase.PRE_COMMIT, CommitPhase.COMMIT);
+
+            final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohortSize];
+            int i = 0;
+            for (DOMStoreThreePhaseCommitCohort cohort : cohorts) {
+                ops[i++] = cohort.commit();
+            }
+            return ops;
         }
 
         /**
@@ -284,100 +351,6 @@ public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
             Throwables.propagateIfPossible(cause, TransactionCommitFailedException.class);
         }
 
-        /**
-         *
-         * Invokes preCommit on underlying cohorts and returns future
-         * which will complete once all preCommit on cohorts completed or
-         * failed.
-         *
-         *
-         * Valid state transition is from CAN_COMMIT to PRE_COMMIT, if current
-         * state is not CAN_COMMIT
-         * throws IllegalStateException.
-         *
-         * @return Future which will complete once all cohorts completed
-         *         preCommit.
-         *         Future throws TransactionCommitFailedException
-         *         If any of cohorts failed preCommit
-         *
-         */
-        private CheckedFuture<Void, TransactionCommitFailedException> preCommitAll() {
-            changeStateFrom(CommitPhase.CAN_COMMIT, CommitPhase.PRE_COMMIT);
-            Builder<ListenableFuture<Void>> ops = ImmutableList.builder();
-            for (DOMStoreThreePhaseCommitCohort cohort : cohorts) {
-                ops.add(cohort.preCommit());
-            }
-            /*
-             * We are returing all futures as list, not only succeeded ones in
-             * order to fail composite future if any of them failed.
-             * See Futures.allAsList for this description.
-             */
-            @SuppressWarnings({ "unchecked", "rawtypes" })
-            ListenableFuture<Void> compositeResult = (ListenableFuture) Futures.allAsList(ops.build());
-            return MappingCheckedFuture.create(compositeResult,
-                                         TransactionCommitFailedExceptionMapper.PRE_COMMIT_MAPPER);
-        }
-
-        /**
-         *
-         * Invokes commit on underlying cohorts and returns future which
-         * completes
-         * once all commits on cohorts are completed.
-         *
-         * Valid state transition is from PRE_COMMIT to COMMIT, if not throws
-         * IllegalStateException
-         *
-         * @return Future which will complete once all cohorts completed
-         *         commit.
-         *         Future throws TransactionCommitFailedException
-         *         If any of cohorts failed preCommit
-         *
-         */
-        private CheckedFuture<Void, TransactionCommitFailedException> commitAll() {
-            changeStateFrom(CommitPhase.PRE_COMMIT, CommitPhase.COMMIT);
-            Builder<ListenableFuture<Void>> ops = ImmutableList.builder();
-            for (DOMStoreThreePhaseCommitCohort cohort : cohorts) {
-                ops.add(cohort.commit());
-            }
-            /*
-             * We are returing all futures as list, not only succeeded ones in
-             * order to fail composite future if any of them failed.
-             * See Futures.allAsList for this description.
-             */
-            @SuppressWarnings({ "unchecked", "rawtypes" })
-            ListenableFuture<Void> compositeResult = (ListenableFuture) Futures.allAsList(ops.build());
-            return MappingCheckedFuture.create(compositeResult,
-                                     TransactionCommitFailedExceptionMapper.COMMIT_ERROR_MAPPER);
-        }
-
-        /**
-         *
-         * Invokes canCommit on underlying cohorts and returns composite future
-         * which will contains {@link Boolean#TRUE} only and only if
-         * all cohorts returned true.
-         *
-         * Valid state transition is from SUBMITTED to CAN_COMMIT,
-         * if currentPhase is not SUBMITTED throws IllegalStateException.
-         *
-         * @return Future which will complete once all cohorts completed
-         *         preCommit.
-         *         Future throws TransactionCommitFailedException
-         *         If any of cohorts failed preCommit
-         *
-         */
-        private CheckedFuture<Boolean, TransactionCommitFailedException> canCommitAll() {
-            changeStateFrom(CommitPhase.SUBMITTED, CommitPhase.CAN_COMMIT);
-            Builder<ListenableFuture<Boolean>> canCommitOperations = ImmutableList.builder();
-            for (DOMStoreThreePhaseCommitCohort cohort : cohorts) {
-                canCommitOperations.add(cohort.canCommit());
-            }
-            ListenableFuture<List<Boolean>> allCanCommits = Futures.allAsList(canCommitOperations.build());
-            ListenableFuture<Boolean> allSuccessFuture = Futures.transform(allCanCommits, AND_FUNCTION);
-            return MappingCheckedFuture.create(allSuccessFuture,
-                                       TransactionCommitFailedExceptionMapper.CAN_COMMIT_ERROR_MAPPER);
-
-        }
-
         /**
          *
          * Invokes abort on underlying cohorts and returns future which
@@ -390,17 +363,20 @@ public class DOMDataCommitCoordinatorImpl implements DOMDataCommitExecutor {
          */
         private ListenableFuture<Void> abortAsyncAll() {
             changeStateFrom(currentPhase, CommitPhase.ABORT);
-            Builder<ListenableFuture<Void>> ops = ImmutableList.builder();
+
+            final ListenableFuture<?>[] ops = new ListenableFuture<?>[cohortSize];
+            int i = 0;
             for (DOMStoreThreePhaseCommitCohort cohort : cohorts) {
-                ops.add(cohort.abort());
+                ops[i++] = cohort.abort();
             }
+
             /*
              * We are returing all futures as list, not only succeeded ones in
              * order to fail composite future if any of them failed.
              * See Futures.allAsList for this description.
              */
             @SuppressWarnings({ "unchecked", "rawtypes" })
-            ListenableFuture<Void> compositeResult = (ListenableFuture) Futures.allAsList(ops.build());
+            ListenableFuture<Void> compositeResult = (ListenableFuture) Futures.allAsList(ops);
             return compositeResult;
         }
 
index 2cd5b19bd12a4c44a222a43acd8004df82debef1..600baa743169744a9e2019ef116ee98f4a194c4e 100644 (file)
@@ -93,7 +93,7 @@ public class NetconfDeviceSimulator implements Closeable {
         this.hashedWheelTimer = hashedWheelTimer;
     }
 
-    private NetconfServerDispatcher createDispatcher(final Map<ModuleBuilder, String> moduleBuilders, final boolean exi) {
+    private NetconfServerDispatcher createDispatcher(final Map<ModuleBuilder, String> moduleBuilders, final boolean exi, final int generateConfigsTimeout) {
 
         final Set<Capability> capabilities = Sets.newHashSet(Collections2.transform(moduleBuilders.keySet(), new Function<ModuleBuilder, Capability>() {
             @Override
@@ -115,7 +115,7 @@ public class NetconfDeviceSimulator implements Closeable {
                 : Sets.newHashSet(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0, XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
 
         final NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
-                hashedWheelTimer, simulatedOperationProvider, idProvider, CONNECTION_TIMEOUT_MILLIS, commitNotifier, new LoggingMonitoringService(), serverCapabilities);
+                hashedWheelTimer, simulatedOperationProvider, idProvider, generateConfigsTimeout, commitNotifier, new LoggingMonitoringService(), serverCapabilities);
 
         final NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
                 serverNegotiatorFactory);
@@ -153,7 +153,7 @@ public class NetconfDeviceSimulator implements Closeable {
     public List<Integer> start(final Main.Params params) {
         final Map<ModuleBuilder, String> moduleBuilders = parseSchemasToModuleBuilders(params);
 
-        final NetconfServerDispatcher dispatcher = createDispatcher(moduleBuilders, params.exi);
+        final NetconfServerDispatcher dispatcher = createDispatcher(moduleBuilders, params.exi, params.generateConfigsTimeout);
 
         int currentPort = params.startingPort;
 
index df4d389705b29eda03cb3e88a553ba14902720f3..bed58beb0f3494a6914986c339af55611c3572b4 100644 (file)
       <artifactId>xmlunit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.opendaylight.yangtools</groupId>
+      <artifactId>mockito-configuration</artifactId>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/CloseableUtilTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/CloseableUtilTest.java
new file mode 100644 (file)
index 0000000..8d41ad7
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2014 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.controller.netconf.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+
+import com.google.common.collect.Lists;
+import org.junit.Test;
+
+public class CloseableUtilTest {
+
+    @Test
+    public void testCloseAllFail() throws Exception {
+        final AutoCloseable failingCloseable = new AutoCloseable() {
+            @Override
+            public void close() throws Exception {
+                throw new RuntimeException("testing failing close");
+            }
+        };
+
+        try {
+            CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable));
+            fail("Exception with suppressed should be thrown");
+        } catch (final RuntimeException e) {
+            assertEquals(1, e.getSuppressed().length);
+        }
+    }
+
+    @Test
+    public void testCloseAll() throws Exception {
+        final AutoCloseable failingCloseable = mock(AutoCloseable.class);
+        doNothing().when(failingCloseable).close();
+        CloseableUtil.closeAll(Lists.newArrayList(failingCloseable, failingCloseable));
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/HardcodedNamespaceResolverTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/HardcodedNamespaceResolverTest.java
new file mode 100644 (file)
index 0000000..f083cc1
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2014 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.controller.netconf.util.xml;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class HardcodedNamespaceResolverTest {
+
+    @Test
+    public void testResolver() throws Exception {
+        final HardcodedNamespaceResolver hardcodedNamespaceResolver = new HardcodedNamespaceResolver("prefix", "namespace");
+
+        assertEquals("namespace", hardcodedNamespaceResolver.getNamespaceURI("prefix"));
+        try{
+            hardcodedNamespaceResolver.getNamespaceURI("unknown");
+            fail("Unknown namespace lookup should fail");
+        } catch(IllegalStateException e) {}
+
+        assertNull(hardcodedNamespaceResolver.getPrefix("any"));
+        assertNull(hardcodedNamespaceResolver.getPrefixes("any"));
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlElementTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlElementTest.java
new file mode 100644 (file)
index 0000000..a88de95
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2014 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.controller.netconf.util.xml;
+
+import static org.hamcrest.CoreMatchers.both;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import com.google.common.base.Optional;
+
+public class XmlElementTest {
+
+    private final String elementAsString = "<top xmlns=\"namespace\" xmlns:a=\"attrNamespace\" a:attr1=\"value1\" attr2=\"value2\">" +
+            "<inner>" +
+            "<deepInner>deepValue</deepInner>" +
+            "</inner>" +
+            "<innerNamespace xmlns=\"innerNamespace\">innerNamespaceValue</innerNamespace>" +
+            "<innerPrefixed xmlns:b=\"prefixedValueNamespace\">b:valueWithPrefix</innerPrefixed>" +
+            "</top>";
+    private Document document;
+    private Element element;
+    private XmlElement xmlElement;
+
+    @Before
+    public void setUp() throws Exception {
+        document = XmlUtil.readXmlToDocument(elementAsString);
+        element = document.getDocumentElement();
+        xmlElement = XmlElement.fromDomElement(element);
+    }
+
+    @Test
+    public void testConstruct() throws Exception {
+        final XmlElement fromString = XmlElement.fromString(elementAsString);
+        assertEquals(fromString, xmlElement);
+        XmlElement.fromDomDocument(document);
+        XmlElement.fromDomElement(element);
+        XmlElement.fromDomElementWithExpected(element, "top");
+        XmlElement.fromDomElementWithExpected(element, "top", "namespace");
+
+        try {
+            XmlElement.fromString("notXml");
+            fail();
+        } catch (final NetconfDocumentedException e) {}
+
+        try {
+            XmlElement.fromDomElementWithExpected(element, "notTop");
+            fail();
+        } catch (final NetconfDocumentedException e) {}
+
+        try {
+            XmlElement.fromDomElementWithExpected(element, "top", "notNamespace");
+            fail();
+        } catch (final NetconfDocumentedException e) {}
+    }
+
+    @Test
+    public void testGetters() throws Exception {
+        assertEquals(element, xmlElement.getDomElement());
+        assertEquals(element.getElementsByTagName("inner").getLength(), xmlElement.getElementsByTagName("inner").getLength());
+
+        assertEquals("top", xmlElement.getName());
+        assertTrue(xmlElement.hasNamespace());
+        assertEquals("namespace", xmlElement.getNamespace());
+        assertEquals("namespace", xmlElement.getNamespaceAttribute());
+        assertEquals(Optional.of("namespace"), xmlElement.getNamespaceOptionally());
+
+        assertEquals("value1", xmlElement.getAttribute("attr1", "attrNamespace"));
+        assertEquals("value2", xmlElement.getAttribute("attr2"));
+        assertEquals(2 + 2/*Namespace definition*/, xmlElement.getAttributes().size());
+
+        assertEquals(3, xmlElement.getChildElements().size());
+        assertEquals(1, xmlElement.getChildElements("inner").size());
+        assertTrue(xmlElement.getOnlyChildElementOptionally("inner").isPresent());
+        assertTrue(xmlElement.getOnlyChildElementWithSameNamespaceOptionally("inner").isPresent());
+        assertEquals(0, xmlElement.getChildElements("unknown").size());
+        assertFalse(xmlElement.getOnlyChildElementOptionally("unknown").isPresent());
+        assertEquals(1, xmlElement.getChildElementsWithSameNamespace("inner").size());
+        assertEquals(0, xmlElement.getChildElementsWithSameNamespace("innerNamespace").size());
+        assertEquals(1, xmlElement.getChildElementsWithinNamespace("innerNamespace", "innerNamespace").size());
+        assertTrue(xmlElement.getOnlyChildElementOptionally("innerNamespace", "innerNamespace").isPresent());
+        assertFalse(xmlElement.getOnlyChildElementOptionally("innerNamespace", "unknownNamespace").isPresent());
+
+        final XmlElement noNamespaceElement = XmlElement.fromString("<noNamespace/>");
+        assertFalse(noNamespaceElement.hasNamespace());
+        try {
+            noNamespaceElement.getNamespace();
+            fail();
+        } catch (final MissingNameSpaceException e) {}
+
+        final XmlElement inner = xmlElement.getOnlyChildElement("inner");
+        final XmlElement deepInner = inner.getOnlyChildElementWithSameNamespaceOptionally().get();
+        assertEquals(deepInner, inner.getOnlyChildElementWithSameNamespace());
+        assertEquals(Optional.<XmlElement>absent(), xmlElement.getOnlyChildElementOptionally("unknown"));
+        assertEquals("deepValue", deepInner.getTextContent());
+        assertEquals("deepValue", deepInner.getOnlyTextContentOptionally().get());
+        assertEquals("deepValue", deepInner.getOnlyTextContentOptionally().get());
+    }
+
+    @Test
+    public void testExtractNamespaces() throws Exception {
+        final XmlElement innerPrefixed = xmlElement.getOnlyChildElement("innerPrefixed");
+        Map.Entry<String, String> namespaceOfTextContent = innerPrefixed.findNamespaceOfTextContent();
+
+        assertNotNull(namespaceOfTextContent);
+        assertEquals("b", namespaceOfTextContent.getKey());
+        assertEquals("prefixedValueNamespace", namespaceOfTextContent.getValue());
+        final XmlElement innerNamespace = xmlElement.getOnlyChildElement("innerNamespace");
+        namespaceOfTextContent = innerNamespace.findNamespaceOfTextContent();
+
+        assertEquals("", namespaceOfTextContent.getKey());
+        assertEquals("innerNamespace", namespaceOfTextContent.getValue());
+    }
+
+    @Test
+    public void testUnrecognisedElements() throws Exception {
+        xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"), xmlElement.getOnlyChildElement("innerPrefixed"), xmlElement.getOnlyChildElement("innerNamespace"));
+
+        try {
+            xmlElement.checkUnrecognisedElements(xmlElement.getOnlyChildElement("inner"));
+            fail();
+        } catch (final NetconfDocumentedException e) {
+            assertThat(e.getMessage(), both(containsString("innerNamespace")).and(containsString("innerNamespace")));
+        }
+    }
+}
diff --git a/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlUtilTest.java b/opendaylight/netconf/netconf-util/src/test/java/org/opendaylight/controller/netconf/util/xml/XmlUtilTest.java
new file mode 100644 (file)
index 0000000..3796dd9
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2014 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.controller.netconf.util.xml;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import com.google.common.base.Optional;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import org.custommonkey.xmlunit.Diff;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.Test;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXParseException;
+
+public class XmlUtilTest {
+
+    private final String xml = "<top xmlns=\"namespace\">\n" +
+            "<innerText>value</innerText>\n" +
+            "<innerPrefixedText xmlns:pref=\"prefixNamespace\">prefix:value</innerPrefixedText>\n" +
+            "<innerPrefixedText xmlns=\"randomNamespace\" xmlns:pref=\"prefixNamespace\">prefix:value</innerPrefixedText>\n" +
+            "</top>";
+
+    @Test
+    public void testCreateElement() throws Exception {
+        final Document document = XmlUtil.newDocument();
+        final Element top = XmlUtil.createElement(document, "top", Optional.of("namespace"));
+
+        top.appendChild(XmlUtil.createTextElement(document, "innerText", "value", Optional.of("namespace")));
+        top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref", "prefixNamespace", "value", Optional.of("namespace")));
+        top.appendChild(XmlUtil.createTextElementWithNamespacedContent(document, "innerPrefixedText", "pref", "prefixNamespace", "value", Optional.of("randomNamespace")));
+
+        document.appendChild(top);
+        assertEquals("top", XmlUtil.createDocumentCopy(document).getDocumentElement().getTagName());
+
+        XMLUnit.setIgnoreAttributeOrder(true);
+        XMLUnit.setIgnoreWhitespace(true);
+
+        final Diff diff = XMLUnit.compareXML(XMLUnit.buildControlDocument(xml), document);
+        assertTrue(diff.toString(), diff.similar());
+    }
+
+    @Test
+    public void testLoadSchema() throws Exception {
+        XmlUtil.loadSchema();
+        try {
+            XmlUtil.loadSchema(getClass().getResourceAsStream("/netconfMessages/commit.xml"));
+            fail("Input stream does not contain xsd");
+        } catch (final IllegalStateException e) {
+            assertTrue(e.getCause() instanceof SAXParseException);
+        }
+
+    }
+
+    @Test
+    public void testXPath() throws Exception {
+        final XPathExpression correctXPath = XMLNetconfUtil.compileXPath("/top/innerText");
+        try {
+            XMLNetconfUtil.compileXPath("!@(*&$!");
+            fail("Incorrect xpath should fail");
+        } catch (IllegalStateException e) {}
+        final Object value = XmlUtil.evaluateXPath(correctXPath, XmlUtil.readXmlToDocument("<top><innerText>value</innerText></top>"), XPathConstants.NODE);
+        assertEquals("value", ((Element) value).getTextContent());
+    }
+}
\ No newline at end of file