Merge "Bug 714 - Fixed creating DOM Document's element with namespace"
[controller.git] / opendaylight / netconf / config-persister-impl / src / test / java / org / opendaylight / controller / netconf / persist / impl / osgi / ConfigPersisterTest.java
index b722496142e4f2014b82e776317488e02ddb4527..b1bf23292875611d40c6875fd1a05bb8d81b328e 100644 (file)
@@ -25,14 +25,13 @@ import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.xml.sax.SAXException;
 
-import javax.management.MBeanServer;
 import java.io.IOException;
-import java.lang.management.ManagementFactory;
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 
 public class ConfigPersisterTest {
@@ -40,7 +39,6 @@ public class ConfigPersisterTest {
 
     private MockedBundleContext ctx;
     private ConfigPersisterActivator configPersisterActivator;
-    private static final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
     private TestingExceptionHandler handler;
 
 
@@ -113,26 +111,29 @@ public class ConfigPersisterTest {
     @Test
     public void testPersisterConflictingVersionException() throws Exception {
         setUpContextAndStartPersister("cap1");
-        NetconfOperationService service = getWorkingService(getConflictVersionDocument());
-        doReturn(service).when(ctx.serviceFactory).createService(anyString());
+
+        doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
         Thread.sleep(2000);
         handler.assertException(IllegalStateException.class, "Max wait for conflicting version stabilization timeout");
     }
 
-    private Document getConflictVersionDocument() throws SAXException, IOException {
-        return XmlUtil.readXmlToDocument(
-                "<rpc-reply message-id=\"1\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
-                        "<rpc-error><error-info><error>" +
-                        ConflictingVersionException.class.getCanonicalName() +
-                        "</error></error-info></rpc-error>\n" +
-                        "</rpc-reply>"
-        );
+    private NetconfOperationService getConflictingService() throws Exception {
+        NetconfOperationService service =  getWorkingService(getOKDocument());
+        ConflictingVersionException cve = new ConflictingVersionException("");
+        try {
+            NetconfDocumentedException.wrap(cve);
+            throw new AssertionError("Should throw an exception");
+        }catch(NetconfDocumentedException e) {
+            NetconfOperation mockedOperation = service.getNetconfOperations().iterator().next();
+            doThrow(e).when(mockedOperation).handle(any(Document.class), any(NetconfOperationChainedExecution.class));
+            return service;
+        }
     }
 
     @Test
     public void testSuccessConflictingVersionException() throws Exception {
         setUpContextAndStartPersister("cap1");
-        doReturn(getWorkingService(getConflictVersionDocument())).when(ctx.serviceFactory).createService(anyString());
+        doReturn(getConflictingService()).when(ctx.serviceFactory).createService(anyString());
         Thread.sleep(500);
         // working service:
         logger.info("Switching to working service **");